Просмотр файла mc-2.7.0/modules/lib/controllers/lib.php

Размер файла: 10.88Kb
  1. <?php
  2.  
  3. /**
  4. * MobileCMS
  5. *
  6. * Open source content management system for mobile sites
  7. *
  8. * @author MobileCMS Team <support@mobilecms.pro>
  9. * @copyright Copyright (c) 2011-2019, MobileCMS Team
  10. * @link https://mobilecms.pro Official site
  11. * @license MIT license
  12. */
  13. defined('IN_SYSTEM') or die('<b>403<br />Запрет доступа!</b>');
  14.  
  15. //---------------------------------------------
  16.  
  17. /**
  18. * Контроллер пользовательской части библиотеки
  19. */
  20. class Lib_Controller extends Controller {
  21.  
  22. /**
  23. * Уровень пользовательского доступа
  24. */
  25. public $access_level = 0;
  26.  
  27. /**
  28. * Construct
  29. */
  30. public function __construct() {
  31. parent::__construct();
  32.  
  33. # Хелпер библиотеки
  34. a_import('modules/lib/helpers/lib');
  35. }
  36.  
  37. /**
  38. * Метод по умолчанию
  39. */
  40. public function action_index() {
  41. $this->action_list_books();
  42. }
  43.  
  44. /**
  45. * Чтение книги
  46. */
  47. public function action_read_book() {
  48. $this->per_page = 300;
  49.  
  50. # Обновляем количество просмотров
  51. if ($this->start == 0)
  52. $this->db->query("UPDATE #__lib_books SET `reads` = `reads` + 1 WHERE book_id = '" . intval($_GET['book_id']) . "'");
  53.  
  54. if (!$book = $this->db->get_row("SELECT b.*, d.name AS directory_name,
  55. (SELECT COUNT(*) FROM #__comments_posts WHERE module = 'lib' AND item_id = b.book_id) AS comments
  56. FROM #__lib_books AS b LEFT JOIN #__lib_directories AS d USING(directory_id) WHERE b.book_id = '" . intval($_GET['book_id']) . "'"))
  57. a_error("Книга не найдена!");
  58.  
  59. $directory_path = lib::get_path($book['directory_id'], $this->db);
  60. $namepath = lib::get_namepath($directory_path, '/');
  61.  
  62. # Получаем навигацию
  63. $navigation = '<a href="' . a_url('lib') . '">Библиотека</a> ';
  64. if (!empty($namepath))
  65. $navigation .= '» ' . $namepath;
  66. if ($book['directory_id'] > 0)
  67. $navigation .= '» <a href="' . a_url('lib/list_books', 'directory_id=' . $book['directory_id']) . '">' . $book['directory_name'] . '</a>';
  68.  
  69. # Получаем контент и делаем разбивку по страницам
  70. $text = file_get_contents(ROOT . 'files/lib' . $book['path_to_file'] . $book['book_id'] . '.txt');
  71. $ex = explode(' ', $text);
  72. $total = count($ex);
  73. $text_page = '';
  74. for ($i = $this->start; $i < $this->start + $this->per_page && $i <= $total; $i++) {
  75. $text_page .= $ex[$i] . ' ';
  76. }
  77. $text_page = htmlspecialchars(stripslashes($text_page));
  78. $text_page = nl2br($text_page);
  79. $text_page = main::bbcode($text_page);
  80.  
  81. # Пагинация
  82. $pg_conf['base_url'] = a_url('lib/read_book', 'book_id=' . $book['book_id'] . '&amp;start=');
  83. $pg_conf['total_rows'] = $total;
  84. $pg_conf['per_page'] = $this->per_page;
  85.  
  86. a_import('libraries/pagination');
  87. $pg = new CI_Pagination($pg_conf);
  88.  
  89. $this->tpl->assign(array(
  90. 'book' => $book,
  91. 'text_page' => $text_page,
  92. 'navigation' => $navigation,
  93. 'pagination' => $pg->create_links()
  94. ));
  95.  
  96. $this->tpl->display('read_book');
  97. }
  98.  
  99. /**
  100. * Скачать книгу
  101. */
  102. public function action_download_book() {
  103. $this->per_page = 7;
  104.  
  105. if (!$book = $this->db->get_row("SELECT b.* FROM #__lib_books AS b WHERE b.book_id = '" . intval($_GET['book_id']) . "'")) {
  106. a_error("Книга не найдена!");
  107. }
  108.  
  109. $file_path = ROOT . 'files/lib' . $book['path_to_file'];
  110. $jar_path = ROOT . 'modules/lib/jar_data';
  111.  
  112. # Подключаем библиотеки для работы с архивами
  113. a_import('libraries/pclzip.lib');
  114.  
  115. switch ($_GET['type']) {
  116. case 'jar':
  117. $filename = main::detranslite($book['name']) . '.jar';
  118. $mime = 'application/java-archive';
  119. $tmp_dir = ROOT . 'tmp/' . main::get_unique_code(32);
  120.  
  121. # Копируем основные файлы книги
  122. lib::r_copy($jar_path, $tmp_dir);
  123.  
  124. #Правим манифест
  125. $manifest = file_get_contents($jar_path . '/META-INF/MANIFEST.MF');
  126. $manifest = str_replace('{NAME}', main::detranslite($book['name']), $manifest);
  127. file_put_contents($tmp_dir . '/META-INF/MANIFEST.MF', $manifest);
  128.  
  129. # Добавляем книгу
  130. $book = file_get_contents($file_path . $book['book_id'] . '.txt');
  131. file_put_contents($tmp_dir . '/textfile.txt', main::wtext($book));
  132.  
  133. # Создаем архив
  134. $archive = new PclZip($tmp_dir . '/tmp.jar');
  135.  
  136. # Добавляем основные файлы книги
  137. $list = $archive->create($tmp_dir, PCLZIP_OPT_REMOVE_PATH, $tmp_dir);
  138. $content = file_get_contents($tmp_dir . '/tmp.jar');
  139. main::delete_dir($tmp_dir);
  140. break;
  141. case 'zip':
  142. $filename = main::detranslite($book['name']) . '.zip';
  143. $mime = 'application/zip';
  144. $tmp_name = ROOT . 'tmp/' . main::get_unique_code(32);
  145. $archive = new PclZip($tmp_name);
  146. $list = $archive->create($file_path . $book['book_id'] . '.txt',
  147. PCLZIP_OPT_REMOVE_PATH, $file_path);
  148. $content = file_get_contents($tmp_name);
  149. unlink($tmp_name);
  150. break;
  151. case 'txt':
  152. default:
  153. $filename = main::detranslite($book['name']) . '.txt';
  154. $mime = 'text/plain';
  155. $content = file_get_contents($file_path . $book['book_id'] . '.txt');
  156. break;
  157. }
  158.  
  159. header('Content-Type: ' . $mime);
  160. header('Content-Disposition: attachment; filename=' . $filename);
  161. header("Content-Transfer-Encoding: binary");
  162. header('Expires: 0');
  163. header('Pragma: no-cache');
  164. header("Content-Length: " . strlen($content));
  165.  
  166. echo $content;
  167. }
  168.  
  169. /**
  170. * Список книг и папок
  171. */
  172. public function action_list_books() {
  173. switch ($_GET['type']) {
  174. # Самые популярные
  175. case 'top':
  176. $sql = "SELECT *, (SELECT 'book') AS type FROM #__lib_books ORDER BY `reads` DESC LIMIT $this->start, $this->per_page";
  177. $navigation = '<a href="' . a_url('lib') . '">Библиотека</a>';
  178. $type = 'top';
  179. break;
  180.  
  181. # Поиск
  182. case 'search':
  183. $sql = "SELECT *, (SELECT 'book') AS type FROM #__lib_books WHERE name LIKE '%" . a_safe($_GET['search_word']) . "%' ORDER BY `reads` DESC LIMIT $this->start, $this->per_page";
  184. $navigation = '<a href="' . a_url('lib') . '">Библиотека</a>';
  185. $type = 'search';
  186. break;
  187.  
  188. # Обычный листинг
  189. default:
  190. if (empty($_GET['directory_id']) OR ! is_numeric($_GET['directory_id']))
  191. $directory_id = 0;
  192. else
  193. $directory_id = intval($_GET['directory_id']);
  194.  
  195. if ($directory_id != 0 && !$directory = $this->db->get_row("SELECT * FROM #__lib_directories WHERE directory_id = '$directory_id'")) {
  196. a_error('Папка не найдена!');
  197. } else {
  198. # Определяем папка с файлами или папками
  199. if ($this->db->get_one("SELECT directory_id FROM #__lib_directories WHERE parent_id = $directory_id")) {
  200. $files_directory = FALSE;
  201. $this->per_page = 100;
  202. } else {
  203. $files_directory = TRUE;
  204. }
  205. }
  206.  
  207. $directory_path = lib::get_path($directory_id, $this->db);
  208. $namepath = lib::get_namepath($directory_path, '/', TRUE);
  209.  
  210. # Получаем навигацию
  211. if ($directory_id > 0) {
  212. $navigation = '<a href="' . a_url('lib') . '">Библиотека</a> ';
  213. if (!empty($namepath))
  214. $navigation .= '» ' . $namepath;
  215. if ($directory['directory_id'] > 0)
  216. $navigation .= '» <a href="' . a_url('lib/list_books', 'directory_id=' . $directory['directory_id']) . '">' . $directory['name'] . '</a>';
  217. }
  218.  
  219. # Получаем список папок и файлов
  220. $sql = "SELECT SQL_CALC_FOUND_ROWS
  221. directory_id AS book_id,
  222. name,
  223. (SELECT 'directory') AS type,
  224. (SELECT 0) AS description,
  225. position,
  226. (SELECT 0) AS `reads`,
  227. (SELECT 0) AS time,
  228. (SELECT COUNT(*) FROM #__lib_books AS lb WHERE lb.path_to_file LIKE CONCAT('%/', ld.directory_id, '/%')) AS count_books,
  229. (SELECT COUNT(*) FROM #__lib_books AS lb WHERE lb.path_to_file LIKE CONCAT('%/', ld.directory_id, '/%') AND time > UNIX_TIMESTAMP() - 86400) AS new_books
  230. FROM #__lib_directories AS ld WHERE parent_id = '$directory_id' " . PHP_EOL;
  231. $sql .= "UNION ALL " . PHP_EOL;
  232. $sql .= "SELECT
  233. book_id,
  234. name,
  235. (SELECT 'book') AS type,
  236. description,
  237. (SELECT 0) AS position,
  238. `reads`,
  239. time,
  240. (SELECT 0) AS count_books,
  241. (SELECT 0) AS new_books
  242. FROM #__lib_books WHERE directory_id = '$directory_id' " . PHP_EOL;
  243.  
  244. $sql .= "ORDER BY type DESC, position ASC, book_id DESC LIMIT $this->start, $this->per_page";
  245.  
  246. $type = 'default_listing';
  247. break;
  248. }
  249.  
  250. $result = $this->db->query($sql);
  251. $total = $this->db->get_one("SELECT FOUND_ROWS()");
  252.  
  253. $books = array();
  254. while ($book = $this->db->fetch_array($result)) {
  255. $books[] = $book;
  256. }
  257.  
  258. # Пагинация
  259. $pg_conf['base_url'] = a_url('lib/list_books', 'type=' . $_GET['type'] . '&amp;search_word=' . $_GET['search_word'] . '&amp;directory_id=' . intval($_GET['directory_id']) . '&amp;start=');
  260. $pg_conf['total_rows'] = $total;
  261. $pg_conf['per_page'] = $this->per_page;
  262.  
  263. a_import('libraries/pagination');
  264. $pg = new CI_Pagination($pg_conf);
  265.  
  266. $this->tpl->assign(array(
  267. 'books' => $books,
  268. 'total' => $total,
  269. 'type' => $type,
  270. 'navigation' => $navigation,
  271. 'pagination' => $pg->create_links(),
  272. 'directory' => $directory
  273. ));
  274.  
  275. $this->tpl->display('list_books');
  276. }
  277.  
  278. }
  279.  
  280. ?>