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

Размер файла: 16.55Kb
  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. class Lib_Admin_Controller extends Controller {
  19.  
  20. /**
  21. * Уровень пользовательского доступа
  22. */
  23. public $access_level = 8;
  24.  
  25. /**
  26. * Тема
  27. */
  28. public $template_theme = 'admin';
  29.  
  30. /**
  31. * Construct
  32. */
  33. public function __construct() {
  34. parent::__construct();
  35.  
  36. # Хелпер библиотеки
  37. a_import('modules/lib/helpers/lib');
  38. }
  39.  
  40. /**
  41. * Метод по умолчанию
  42. */
  43. public function action_index() {
  44. $this->action_list_books();
  45. }
  46.  
  47. /**
  48. * Загрузка книг
  49. */
  50. public function action_add_books() {
  51. # Получем данные о папке для загрузки
  52. if (empty($_GET['directory_id']) OR ! is_numeric($_GET['directory_id']))
  53. $directory_id = 0;
  54. else
  55. $directory_id = intval($_GET['directory_id']);
  56.  
  57. if ($directory_id != 0 && !$directory = $this->db->get_row("SELECT * FROM #__lib_directories WHERE directory_id = '" . $directory_id . "'")) {
  58. a_error('Папка для загрузки не найдена!');
  59. }
  60.  
  61. # Определяем как загружать
  62. switch ($_GET['type']) {
  63. case 'textes':
  64. $type = 'textes';
  65. break;
  66. case 'import':
  67. $type = 'import';
  68. break;
  69. case 'upload':
  70. default:
  71. $type = 'upload';
  72. break;
  73. }
  74.  
  75. if (isset($_POST['submit'])) {
  76. main::is_demo();
  77. if (!$this->error) {
  78. # Определяем путь для загрузки
  79. $directory_path = lib::get_path($directory_id, $this->db);
  80. $realpath = lib::get_realpath($directory_path);
  81. $path_to_file = ($realpath != '' ? $realpath . '/' : '') . ($directory_id == 0 ? '' : $directory_id . '/');
  82.  
  83. $c = 0;
  84. for ($i = 1; $i <= 10; $i++) {
  85. if (!empty($_POST['name_' . $i])) {
  86. # Добавляем файл в базу
  87. $this->db->query("INSERT INTO #__lib_books SET
  88. directory_id = '$directory_id',
  89. name = '" . a_safe($_POST['name_' . $i]) . "',
  90. path_to_file = '/" . $path_to_file . "',
  91. time = UNIX_TIMESTAMP()
  92. ");
  93.  
  94. $book_id = $this->db->insert_id();
  95.  
  96. switch ($type) {
  97. # Загрузка из текста
  98. case 'textes':
  99. if (!empty($_POST['text_' . $i])) {
  100. file_put_contents(ROOT . 'files/lib/' . $path_to_file . $book_id . '.txt', $_POST['text_' . $i]);
  101. $c++;
  102. }
  103. break;
  104. # Импорт
  105. case 'import':
  106. if (!empty($_POST['link_' . $i])) {
  107. copy($_POST['link_' . $i], ROOT . 'files/lib/' . $path_to_file . $book_id . '.txt');
  108. $c++;
  109. }
  110. break;
  111. # Upload
  112. case 'upload':
  113. default:
  114. if (!empty($_FILES['file_' . $i]['tmp_name'])) {
  115. copy($_FILES['file_' . $i]['tmp_name'], ROOT . 'files/lib/' . $path_to_file . $book_id . '.txt');
  116. $c++;
  117. }
  118. break;
  119. }
  120. }
  121. }
  122.  
  123. a_notice('Загружено книг: ' . $c, a_url('lib/admin/list_books', 'directory_id=' . $directory_id));
  124. }
  125. }
  126. if (!isset($_POST['submit']) OR $this->error) {
  127. $this->tpl->assign(array(
  128. 'error' => $this->error,
  129. 'type' => $type
  130. ));
  131.  
  132. $this->tpl->display('add_books');
  133. }
  134. }
  135.  
  136. /**
  137. * Список книг и папок
  138. */
  139. public function action_list_books() {
  140. $this->per_page = 20;
  141.  
  142. if (empty($_GET['directory_id']) OR ! is_numeric($_GET['directory_id']))
  143. $directory_id = 0;
  144. else
  145. $directory_id = intval($_GET['directory_id']);
  146.  
  147. if ($directory_id != 0 && !$directory = $this->db->get_row("SELECT * FROM #__lib_directories WHERE directory_id = '$directory_id'")) {
  148. a_error('Папка не найдена!');
  149. } else {
  150. # Определяем папка с файлами или папками
  151. if ($this->db->get_one("SELECT directory_id FROM #__lib_directories WHERE parent_id = $directory_id")) {
  152. $files_directory = FALSE;
  153. $this->per_page = 100;
  154. } else {
  155. $files_directory = TRUE;
  156. }
  157. }
  158.  
  159. $directory_path = lib::get_path($directory_id, $this->db);
  160. $namepath = lib::get_namepath($directory_path, '/', TRUE);
  161.  
  162. # Получаем список папок и файлов
  163. $sql = "SELECT SQL_CALC_FOUND_ROWS
  164. directory_id AS book_id,
  165. name,
  166. (SELECT 'directory') AS type,
  167. (SELECT 0) AS description,
  168. position
  169. FROM #__lib_directories WHERE parent_id = '$directory_id' " . PHP_EOL;
  170. $sql .= "UNION ALL " . PHP_EOL;
  171. $sql .= "SELECT
  172. book_id,
  173. name,
  174. (SELECT 'book') AS type,
  175. description,
  176. (SELECT 0) AS position
  177. FROM #__lib_books WHERE directory_id = '$directory_id' " . PHP_EOL;
  178.  
  179. $sql .= "ORDER BY type DESC, position ASC, book_id DESC LIMIT $this->start, $this->per_page";
  180.  
  181. $result = $this->db->query($sql);
  182. $total = $this->db->get_one("SELECT FOUND_ROWS()");
  183.  
  184. $min_p = $this->db->get_one("SELECT MIN(position) FROM #__lib_directories WHERE parent_id = '$directory_id'");
  185. $max_p = $this->db->get_one("SELECT MAX(position) FROM #__lib_directories WHERE parent_id = '$directory_id'");
  186.  
  187. $books = array();
  188. while ($book = $this->db->fetch_array($result)) {
  189. if ($book['type'] == 'directory') {
  190. if ($book['position'] != $min_p)
  191. $book['up'] = '<a href="' . a_url('lib/admin/directory_up', 'directory_id=' . $book['book_id']) . '">up</a>';
  192. else
  193. $book['up'] = 'up';
  194.  
  195. if ($book['position'] != $max_p)
  196. $book['down'] = '<a href="' . a_url('lib/admin/directory_down', 'directory_id=' . $book['book_id']) . '">down</a>';
  197. else
  198. $book['down'] = 'down';
  199. } else {
  200. $book['up'] = '-';
  201. $book['down'] = '-';
  202. }
  203.  
  204. $books[] = $book;
  205. }
  206.  
  207. # Пагинация
  208. $pg_conf['base_url'] = a_url('lib/admin/list_books', 'directory_id=' . intval($_GET['directory_id']) . '&amp;start=');
  209. $pg_conf['total_rows'] = $total;
  210. $pg_conf['per_page'] = $this->per_page;
  211.  
  212. a_import('libraries/pagination');
  213. $pg = new CI_Pagination($pg_conf);
  214.  
  215. $this->tpl->assign(array(
  216. 'books' => $books,
  217. 'total' => $total,
  218. 'namepath' => $namepath,
  219. 'pagination' => $pg->create_links(),
  220. 'directory' => $directory
  221. ));
  222.  
  223. $this->tpl->display('list_books');
  224. }
  225.  
  226. /**
  227. * Создание / редактирование папки
  228. */
  229. public function action_directory_edit() {
  230. if (is_numeric($_GET['directory_id'])) {
  231. $directory_id = intval($_GET['directory_id']);
  232. if (!$directory = $this->db->get_row("SELECT * FROM #__lib_directories WHERE directory_id = '$directory_id'")) {
  233. a_error('Папка не найдена!');
  234. }
  235. $parent_directory = $this->db->get_row("SELECT * FROM #__lib_directories WHERE directory_id = '" . $directory['parent_id'] . "'");
  236. $action = 'edit';
  237. } else {
  238. if ($_GET['parent_id'] != '' && !$parent_directory = $this->db->get_row("SELECT * FROM #__lib_directories WHERE directory_id = '" . intval($_GET['parent_id']) . "'"))
  239. a_error('Папка предок не найдена!');
  240. $directory = array();
  241. $action = 'add';
  242. }
  243.  
  244. if (isset($_POST['submit'])) {
  245. main::is_demo();
  246. if (empty($_POST['name'])) {
  247. $this->error .= 'Укажите название папки!<br />';
  248. }
  249.  
  250. if (!$this->error) {
  251. # Создаем нувую папку
  252. if ($action == 'add') {
  253. # Получаем позицию папки
  254. $position = $this->db->get_one("SELECT MAX(position) FROM #__lib_directories WHERE parent_id = '" . $parent_directory['directory_id'] . "'") + 1;
  255.  
  256. $this->db->query("INSERT INTO #__lib_directories SET
  257. name = '" . a_safe($_POST['name']) . "',
  258. parent_id = '" . @$parent_directory['directory_id'] . "',
  259. position = '$position'
  260. ");
  261.  
  262. $directory_id = $this->db->insert_id();
  263.  
  264. # Создаем папку в файловой системе
  265. # Получаем директорию для папки
  266. $directory_path = lib::get_path($directory_id, $this->db);
  267. $realpath = lib::get_realpath($directory_path);
  268.  
  269. mkdir(ROOT . 'files/lib/' . $realpath . '/' . $directory_id);
  270. chmod(ROOT . 'files/lib/' . $realpath . '/' . $directory_id, 0777);
  271.  
  272. a_notice('Папка успешно создана!', a_url('lib/admin/list_books', 'directory_id=' . $parent_directory['directory_id']));
  273. } elseif ($action == 'edit') {
  274. # Изменяем имя папки
  275. $this->db->query("UPDATE #__lib_directories SET
  276. name = '" . a_safe($_POST['name']) . "'
  277. WHERE
  278. directory_id = '" . $directory_id . "'
  279. ");
  280.  
  281. a_notice('Папка успешно изменена!', a_url('lib/admin/list_books', 'directory_id=' . $parent_directory['directory_id']));
  282. }
  283. }
  284. }
  285. if (!isset($_POST['submit']) || $this->error) {
  286. $this->tpl->assign(array(
  287. 'error' => $this->error,
  288. 'directory' => $directory,
  289. 'action' => $action
  290. ));
  291. $this->tpl->display('directory_edit');
  292. }
  293. }
  294.  
  295. /**
  296. * Увеличение позиции папки
  297. */
  298. public function action_directory_up() {
  299. main::is_demo();
  300. if (!$directory = $this->db->get_row("SELECT * FROM #__lib_directories WHERE directory_id = " . intval($_GET['directory_id'])))
  301. a_error('Папка не найдена!');
  302.  
  303. # Меняем позиции
  304. $this->db->query("UPDATE #__lib_directories SET position = " . $directory['position'] . " WHERE parent_id = '" . $directory['parent_id'] . "' AND position = " . ($directory['position'] - 1));
  305. $this->db->query("UPDATE #__lib_directories SET position = " . ($directory['position'] - 1) . " WHERE directory_id = " . intval($_GET['directory_id']));
  306.  
  307. header("Location: " . a_url('lib/admin', 'directory_id=' . $directory['parent_id'], TRUE));
  308. exit;
  309. }
  310.  
  311. /**
  312. * Уменьшение позиции папки
  313. */
  314. public function action_directory_down() {
  315. main::is_demo();
  316. if (!$directory = $this->db->get_row("SELECT * FROM #__lib_directories WHERE directory_id = " . intval($_GET['directory_id'])))
  317. a_error('Папка не найдена!');
  318.  
  319. # Меняем позиции
  320. $this->db->query("UPDATE #__lib_directories SET position = " . $directory['position'] . " WHERE parent_id = '" . $directory['parent_id'] . "' AND position = " . ($directory['position'] + 1));
  321. $this->db->query("UPDATE #__lib_directories SET position = " . ($directory['position'] + 1) . " WHERE directory_id = " . intval($_GET['directory_id']));
  322.  
  323. header("Location: " . a_url('lib/admin', 'directory_id=' . $directory['parent_id'], TRUE));
  324. exit;
  325. }
  326.  
  327. /**
  328. * Удаление книги
  329. */
  330. public function action_book_delete() {
  331. main::is_demo();
  332. if (!$book = $this->db->get_row("SELECT * FROM #__lib_books WHERE book_id = '" . intval($_GET['book_id']) . "'"))
  333. a_error("Книга не найдена!");
  334.  
  335. # Удаляем книгу из ФС
  336. unlink(ROOT . 'files/lib' . $book['path_to_file'] . $book['book_id'] . '.txt');
  337.  
  338. # Удаляем книгу из БД
  339. $this->db->query("DELETE FROM #__lib_books WHERE book_id = '" . $book['book_id'] . "'");
  340.  
  341. a_notice('Книга удалена!', a_url('lib/admin/list_books', 'directory_id=' . $book['directory_id']));
  342. }
  343.  
  344. /**
  345. * Удаление папки
  346. */
  347. public function action_directory_delete() {
  348. main::is_demo();
  349. $directory_id = intval($_GET['directory_id']);
  350.  
  351. if (!$directory = $this->db->get_row("SELECT * FROM #__lib_directories WHERE directory_id = '$directory_id'")) {
  352. a_error('Папка не найдена!');
  353. }
  354.  
  355. if ($this->db->get_one("SELECT directory_id FROM #__lib_directories WHERE parent_id = '$directory_id'") OR
  356. $this->db->get_one("SELECT book_id FROM #__lib_books WHERE directory_id = '$directory_id'")) {
  357. a_error('Папку не возможно удалить, так как она не пуста!');
  358. }
  359.  
  360. # Удаление из ФС
  361. $directory_path = lib::get_path($directory_id, $this->db);
  362. $realpath = lib::get_realpath($directory_path);
  363. rmdir(ROOT . 'files/lib/' . $realpath . '/' . $directory_id);
  364.  
  365. # Удаление папки из базы
  366. $this->db->query("DELETE FROM #__lib_directories WHERE directory_id = '$directory_id'");
  367.  
  368. # Меняем позиции
  369. $this->db->query("UPDATE #__lib_directories SET position = position - 1 WHERE parent_id = '" . $directory['parent_id'] . "' AND position > '" . $directory['position'] . "'");
  370.  
  371. a_notice('Папка успешно удалена!', a_url('lib/admin/list_books', 'directory_id=' . $directory['parent_id']));
  372. }
  373.  
  374. /**
  375. * Удаление всех файлов в папке
  376. */
  377. public function action_directory_clear() {
  378. main::is_demo();
  379. $directory_id = empty($_GET['directory_id']) ? 0 : intval($_GET['directory_id']);
  380.  
  381. # Получаем информацию о папке
  382. if ($directory_id !== 0 && !$this->db->get_one("SELECT directory_id FROM #__lib_directories WHERE directory_id = '" . intval($directory_id) . "'")) {
  383. a_error('Папка не найдена!');
  384. }
  385.  
  386. # Удаляем файлы из ФС
  387. $result = $this->db->query("SELECT * FROM #__lib_books WHERE directory_id = '$directory_id'");
  388. while ($book = $this->db->fetch_array($result)) {
  389. # Удаляем книгу из ФС
  390. unlink(ROOT . 'files/lib' . $book['path_to_file'] . $book['book_id'] . '.txt');
  391. # Удаляем книгу из БД
  392. $this->db->query("DELETE FROM #__lib_books WHERE book_id = '" . $book['book_id'] . "'");
  393. }
  394.  
  395. a_notice('Папка успешно очищена', a_url('lib/admin/list_books', 'directory_id=' . $directory_id));
  396. }
  397.  
  398. }
  399.  
  400. ?>