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

Размер файла: 31.54Kb
  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.  
  14. /**
  15. * Контроллер управления загруз центром
  16. */
  17. class Downloads_Admin_Controller extends Controller {
  18.  
  19. /**
  20. * Доступ
  21. */
  22. public $access_level = 8;
  23.  
  24. /**
  25. * Тема
  26. */
  27. public $template_theme = 'admin';
  28.  
  29. /**
  30. * Папка для загрузки по фтп
  31. */
  32. public $ftp_directory = '_ftp_upload/';
  33.  
  34. /**
  35. * Construct
  36. */
  37. public function __construct() {
  38. parent::__construct();
  39.  
  40. define('DOWNLOADS_DIRECTORY', 'files/downloads/');
  41. # Хелпер загрузок
  42. a_import('modules/downloads/helpers/downloads');
  43. }
  44.  
  45. /**
  46. * Метод по умолчанию
  47. */
  48. public function action_index() {
  49. $this->action_list_files();
  50. }
  51.  
  52. /**
  53. * Конфигурация модуля
  54. */
  55. public function action_config() {
  56. $_config = $this->config['downloads'];
  57.  
  58. if (isset($_POST['submit'])) {
  59. main::is_demo();
  60. $_config = $_POST;
  61.  
  62. main::config($_config, 'downloads', $this->db);
  63.  
  64. a_notice('Данные успешно изменены!', a_url('downloads/admin/config'));
  65. }
  66.  
  67. if (!isset($_POST['submit']) || $this->error) {
  68. $this->tpl->assign(array(
  69. '_config' => $_config
  70. ));
  71.  
  72. $this->tpl->display('config');
  73. }
  74. }
  75.  
  76. /**
  77. * Список файлов и папок
  78. */
  79. public function action_list_files() {
  80. if (empty($_GET['directory_id']) or ! is_numeric($_GET['directory_id']))
  81. $directory_id = 0;
  82. else
  83. $directory_id = intval($_GET['directory_id']);
  84.  
  85. if ($directory_id != 0 && !$directory = $this->db->get_row("SELECT * FROM #__downloads_directories WHERE directory_id = '$directory_id'")) {
  86. a_error('Папка не найдена!');
  87. } else {
  88. # Определяем папка с файлами или папками
  89. if ($this->db->get_one("SELECT directory_id FROM #__downloads_directories WHERE parent_id = $directory_id")) {
  90. $files_directory = false;
  91. $this->per_page = 100;
  92. } else {
  93. $files_directory = true;
  94. $this->per_page = 20;
  95. }
  96. }
  97.  
  98. $directory_path = downloads::get_path($directory_id, $this->db);
  99. $namepath = downloads::get_namepath($directory_path, '/', TRUE);
  100.  
  101. # Получаем список папок и файлов
  102. $sql = "SELECT SQL_CALC_FOUND_ROWS
  103. directory_id AS file_id,
  104. name,
  105. 'directory' AS type,
  106. 0 AS real_name,
  107. 0 AS path_to_file,
  108. position
  109. FROM #__downloads_directories
  110. WHERE parent_id = '$directory_id'" . PHP_EOL;
  111. $sql .= "UNION ALL" . PHP_EOL;
  112. $sql .= "SELECT
  113. file_id,
  114. name,
  115. 'file' AS type,
  116. real_name,
  117. path_to_file,
  118. 0 AS position
  119. FROM #__downloads_files
  120. WHERE directory_id = '$directory_id' AND status = 'active' AND real_name != ''" . PHP_EOL;
  121.  
  122. $sql .= "ORDER BY type ASC, position ASC, file_id DESC LIMIT $this->start, $this->per_page";
  123.  
  124. $result = $this->db->query($sql);
  125. $total = $this->db->get_one("SELECT FOUND_ROWS()");
  126.  
  127. $min_p = $this->db->get_one("SELECT MIN(position) FROM #__downloads_directories WHERE parent_id = '$directory_id'");
  128. $max_p = $this->db->get_one("SELECT MAX(position) FROM #__downloads_directories WHERE parent_id = '$directory_id'");
  129.  
  130. $files = array();
  131. while ($file = $this->db->fetch_array($result)) {
  132. if ($file['type'] == 'directory') {
  133. if ($file['position'] != $min_p)
  134. $file['up'] = '<a href="' . a_url('downloads/admin/directory_up', 'directory_id=' . $file['file_id']) . '"><img src="' . URL . 'views/admin/images/up.png" alt="" /></a>';
  135. else
  136. $file['up'] = '';
  137.  
  138. if ($file['position'] != $max_p)
  139. $file['down'] = '<a href="' . a_url('downloads/admin/directory_down', 'directory_id=' . $file['file_id']) . '"><img src="' . URL . 'views/admin/images/down.png" alt="" /></a>';
  140. else
  141. $file['down'] = '';
  142. } else {
  143. $file['up'] = '-';
  144. $file['down'] = '-';
  145. }
  146.  
  147. $files[] = $file;
  148. }
  149.  
  150. # Пагинация
  151. $pg_conf['base_url'] = a_url('downloads/admin/list_files', 'directory_id=' . intval($_GET['directory_id']) . '&amp;start=');
  152. $pg_conf['total_rows'] = $total;
  153. $pg_conf['per_page'] = $this->per_page;
  154.  
  155. a_import('libraries/pagination');
  156. $pg = new CI_Pagination($pg_conf);
  157.  
  158. $this->tpl->assign(array(
  159. 'files' => $files,
  160. 'total' => $total,
  161. 'namepath' => $namepath,
  162. 'pagination' => $pg->create_links(),
  163. 'directory' => $directory
  164. ));
  165.  
  166. $this->tpl->display('list_files');
  167. }
  168.  
  169. /**
  170. * Создание / редактирование папки
  171. */
  172. public function action_directory_edit() {
  173. if (is_numeric(@$_GET['directory_id'])) {
  174. $directory_id = intval($_GET['directory_id']);
  175. if (!$directory = $this->db->get_row("SELECT * FROM #__downloads_directories WHERE directory_id = '$directory_id'")) {
  176. a_error('Папка не найдена!');
  177. }
  178. $parent_directory = $this->db->get_row("SELECT * FROM #__downloads_directories WHERE directory_id = '" . $directory['parent_id'] . "'");
  179. $action = 'edit';
  180. } else {
  181. if ($_GET['parent_id'] != '' && !$parent_directory = $this->db->get_row("SELECT * FROM #__downloads_directories WHERE directory_id = '" . intval($_GET['parent_id']) . "'"))
  182. a_error('Директория предок не найдена!');
  183. $directory = array();
  184. $action = 'add';
  185. }
  186.  
  187. if (isset($_POST['submit'])) {
  188. main::is_demo();
  189. if (empty($_POST['name'])) {
  190. $this->error .= 'Укажите название папки!<br />';
  191. }
  192.  
  193. if (!$this->error) {
  194. # Определяем, является ли папка c изображениями
  195. if (isset($_POST['images']))
  196. $images = 'yes';
  197. else
  198. $images = 'no';
  199.  
  200. # Определяем, является ли папка c файлами пользователей
  201. if (isset($_POST['user_files']) OR $parent_directory['user_files'] == 'yes')
  202. $user_files = 'yes';
  203. else
  204. $user_files = 'no';
  205.  
  206. # Создаем нувую папку
  207. if ($action == 'add') {
  208. # Получаем позицию папки
  209. if (!isset($_POST['in_up'])) {
  210. $position = $this->db->get_one("SELECT MAX(position) FROM #__downloads_directories WHERE parent_id = '" . $parent_directory['directory_id'] . "'") + 1;
  211. } else {
  212. $position = 1;
  213. $this->db->query("UPDATE #__downloads_directories SET position = position + 1 WHERE parent_id = '" . $parent_directory['directory_id'] . "'");
  214. }
  215.  
  216. $this->db->query("INSERT INTO #__downloads_directories SET
  217. name = '" . a_safe($_POST['name']) . "',
  218. images = '" . $images . "',
  219. user_files = '" . $user_files . "',
  220. parent_id = '" . @$parent_directory['directory_id'] . "',
  221. position = '$position'
  222. ");
  223.  
  224. $directory_id = $this->db->insert_id();
  225.  
  226. # Создаем папку в файловой системе
  227. # Получаем директорию для папки
  228. $directory_path = downloads::get_path($directory_id, $this->db);
  229. $realpath = downloads::get_realpath($directory_path);
  230.  
  231. mkdir(ROOT . DOWNLOADS_DIRECTORY . $realpath . '/' . $directory_id);
  232. chmod(ROOT . DOWNLOADS_DIRECTORY . $realpath . '/' . $directory_id, 0777);
  233.  
  234. a_notice('Папка успешно создана!', a_url('downloads/admin/list_files', 'directory_id=' . @$parent_directory['directory_id']));
  235. } elseif ($action == 'edit') {
  236. # Изменяем имя папки
  237. $this->db->query("UPDATE #__downloads_directories SET
  238. name = '" . a_safe($_POST['name']) . "',
  239. images = '" . $images . "',
  240. user_files = '" . $user_files . "'
  241. WHERE
  242. directory_id = '" . $directory_id . "'
  243. ");
  244.  
  245. a_notice('Папка успешно изменена!', a_url('downloads/admin/list_files', 'directory_id=' . $parent_directory['directory_id']));
  246. }
  247. }
  248. }
  249. if (!isset($_POST['submit']) || $this->error) {
  250. $this->tpl->assign(array(
  251. 'error' => $this->error,
  252. 'directory' => $directory,
  253. 'action' => $action,
  254. '_config' => $this->config['downloads'],
  255. ));
  256. $this->tpl->display('directory_edit');
  257. }
  258. }
  259.  
  260. /**
  261. * Модерация файлов пользователей
  262. */
  263. public function action_list_moderate_files() {
  264. $sql = "SELECT SQL_CALC_FOUND_ROWS *, u.username, (SELECT name FROM #__downloads_directories WHERE directory_id = f.directory_id) AS directory_name
  265. FROM #__downloads_files AS f LEFT JOIN #__users AS u USING(user_id) WHERE f.status = 'moderate'";
  266. $files = $this->db->get_array($sql);
  267. $total = $this->db->get_one("SELECT FOUND_ROWS()");
  268.  
  269. # Пагинация
  270. $pg_conf['base_url'] = a_url('downloads/admin/list_moderate_files', 'start=');
  271. $pg_conf['total_rows'] = $total;
  272. $pg_conf['per_page'] = $this->per_page;
  273.  
  274. a_import('libraries/pagination');
  275. $pg = new CI_Pagination($pg_conf);
  276.  
  277. $this->tpl->assign(array(
  278. 'files' => $files,
  279. 'directory' => $directory
  280. ));
  281.  
  282. $this->tpl->display('list_moderate_files');
  283. }
  284.  
  285. /**
  286. * Удаление папки
  287. */
  288. public function action_directory_delete() {
  289. main::is_demo();
  290. $directory_id = intval($_GET['directory_id']);
  291.  
  292. if (!$directory = $this->db->get_row("SELECT * FROM #__downloads_directories WHERE directory_id = '$directory_id'")) {
  293. a_error('Папка не найдена!');
  294. }
  295.  
  296. # Удаление из ФС
  297. $directory_path = downloads::get_path($directory_id, $this->db);
  298. $realpath = downloads::get_realpath($directory_path);
  299. main::delete_dir(ROOT . DOWNLOADS_DIRECTORY . $realpath . '/' . $directory_id);
  300.  
  301. # Удаляем все файлы из базы
  302. $this->db->query("DELETE FROM #__downloads_files WHERE path_to_file LIKE CONCAT('%/', $directory_id, '/%')");
  303. # Удаляем всех потомков данной папки
  304. downloads::delete_directories($directory_id);
  305.  
  306. # Удаление папки из базы
  307. $this->db->query("DELETE FROM #__downloads_directories WHERE directory_id = '$directory_id'");
  308.  
  309. # Меняем позиции
  310. $this->db->query("UPDATE #__downloads_directories SET position = position - 1 WHERE parent_id = '" . $directory['parent_id'] . "' AND position > '" . $directory['position'] . "'");
  311.  
  312. a_notice('Папка успешно удалена!', a_url('downloads/admin/list_files', 'directory_id=' . $directory['parent_id']));
  313. }
  314.  
  315. /**
  316. * Загрузка файла
  317. */
  318. public function action_file_upload() {
  319. if (function_exists('set_time_limit')) {
  320. set_time_limit(0);
  321. }
  322. # Редактирование файла
  323. if (is_numeric(@$_GET['file_id'])) {
  324. if (!$file = $this->db->get_row("SELECT * FROM #__downloads_files WHERE file_id = '" . intval($_GET['file_id']) . "'")) {
  325. a_error('Файл не найден!');
  326. }
  327. $action = 'edit';
  328. $file_id = intval($_GET['file_id']);
  329. $directory_id = $file['directory_id'];
  330. $new_file = false;
  331. }
  332. # Добавление файла
  333. else {
  334. $file = array();
  335. $action = 'add';
  336. $new_file = true;
  337.  
  338. # Получем данные о папке для загрузки
  339. if (empty($_GET['directory_id']) OR ! is_numeric($_GET['directory_id']))
  340. $directory_id = 0;
  341. else
  342. $directory_id = intval($_GET['directory_id']);
  343.  
  344. if ($directory_id != 0 && !$directory = $this->db->get_row("SELECT * FROM #__downloads_directories WHERE directory_id = '" . $directory_id . "'")) {
  345. a_error('Папка для загрузки не найдена!');
  346. }
  347. }
  348.  
  349. if (isset($_POST['submit'])) {
  350. main::is_demo();
  351. if (!$this->error) {
  352. # Получаем путь к папке
  353. $directory_path = downloads::get_path($directory_id, $this->db);
  354. $realpath = downloads::get_realpath($directory_path);
  355. $realpath = ($realpath != '' ? $realpath . '/' : '') . ($directory_id == 0 ? '' : $directory_id . '/');
  356.  
  357. if ($action == 'add') {
  358. # Создаем пустую строку в базе для файла (чтобы получить его ID)
  359. $this->db->query("INSERT INTO #__downloads_files SET file_id = 'NULL'");
  360. $file_id = $this->db->insert_id();
  361.  
  362. # Создаем папку для файла
  363. mkdir(ROOT . DOWNLOADS_DIRECTORY . $realpath . $file_id);
  364. chmod(ROOT . DOWNLOADS_DIRECTORY . $realpath . $file_id, 0777);
  365. }
  366.  
  367. $path_to_file = DOWNLOADS_DIRECTORY . ($realpath != '' ? $realpath : '') . $file_id;
  368.  
  369. # Загружаем основной файл
  370. if (!empty($_FILES['file_upload']['tmp_name'])) {
  371. if (copy($_FILES['file_upload']['tmp_name'], ROOT . $path_to_file . '/' . $_FILES['file_upload']['name'])) {
  372. # Удаляем файл, если он был перезагружен
  373. if ($action == 'edit') {
  374. if ($file['real_name'] != $_FILES['file_upload']['name']) {
  375. @unlink(ROOT . $path_to_file . '/' . $file['real_name']);
  376. }
  377. }
  378. $file['real_name'] = $_FILES['file_upload']['name'];
  379. }
  380. } elseif (!empty($_POST['file_import']) && $_POST['file_import'] != 'http://') {
  381. $import_file_path = downloads::get_real_file_path($_POST['file_import']);
  382. $import_file_name = basename($import_file_path);
  383. if (copy($import_file_path, ROOT . $path_to_file . '/' . $import_file_name)) {
  384. # Удаляем файл, если он был перезагружен
  385. if ($action == 'edit') {
  386. if ($file['real_name'] != $import_file_name) {
  387. @unlink(ROOT . $path_to_file . '/' . $file['real_name']);
  388. }
  389. }
  390. $file['real_name'] = $import_file_name;
  391. }
  392. }
  393.  
  394. # Работа со скринами
  395. for ($i = 1; $i <= 3; $i++) {
  396. if (!empty($_FILES['screen' . $i]['tmp_name'])) {
  397. $screen_path = ROOT . $path_to_file . '/' . $_FILES['screen' . $i]['name'];
  398. if (copy($_FILES['screen' . $i]['tmp_name'], $screen_path)) {
  399. # Изменяем скриншот
  400. if ($this->config['downloads']['screens_width'] > 0) {
  401. main::image_resize($screen_path, $screen_path, intval($this->config['downloads']['screens_width']), 0, 90);
  402. }
  403.  
  404. # Удаляем файл, если он был перезагружен
  405. if ($action == 'edit') {
  406. if ($file['screen' . $i] != '' && $file['screen' . $i] != $_FILES['screen' . $i]['name']) {
  407. @unlink(ROOT . $path_to_file . '/' . $file['screen' . $i]);
  408. }
  409. }
  410. $file['screen' . $i] = $_FILES['screen' . $i]['name'];
  411. }
  412. } elseif (!empty($_POST['screen' . $i]) && $_POST['screen' . $i] != 'http://') {
  413. $import_file_path = downloads::get_real_file_path($_POST['screen' . $i]);
  414. $import_file_name = basename($import_file_path);
  415. if (copy($import_file_path, ROOT . $path_to_file . '/' . $import_file_name)) {
  416. # Удаляем файл, если он был перезагружен
  417. if ($action == 'edit') {
  418. if ($file['screen' . $i] != '' && $file['screen' . $i] != $import_file_name) {
  419. @unlink(ROOT . $path_to_file . '/' . $file['screen' . $i]);
  420. }
  421. }
  422. $file['screen' . $i] = $import_file_name;
  423. }
  424. }
  425. }
  426.  
  427. # Дополнительные файлы
  428. for ($i = 1; $i <= 5; $i++) {
  429. if (!empty($_FILES['add_file_file_' . $i]['tmp_name'])) {
  430. if (copy($_FILES['add_file_file_' . $i]['tmp_name'], ROOT . $path_to_file . '/' . $_FILES['add_file_file_' . $i]['name'])) {
  431. # Удаляем файл, если он был перезагружен
  432. if ($action == 'edit') {
  433. if ($file['add_file_file_' . $i] != '' && $file['add_file_file_' . $i] != $_FILES['add_file_file_' . $i]['name']) {
  434. @unlink(ROOT . $path_to_file . '/' . $file['add_file_file_' . $i]);
  435. }
  436. }
  437. $file['add_file_real_name_' . $i] = $_FILES['add_file_file_' . $i]['name'];
  438. }
  439. } elseif (!empty($_POST['add_file_file_' . $i]) && $_POST['add_file_file_' . $i] != 'http://') {
  440. $import_file_path = downloads::get_real_file_path($_POST['add_file_file_' . $i]);
  441. $import_file_name = basename($import_file_path);
  442. if (copy($import_file_path, ROOT . $path_to_file . '/' . $import_file_name)) {
  443. # Удаляем файл, если он был перезагружен
  444. if ($action == 'edit') {
  445. if ($file['add_file_real_name_' . $i] != '' && $file['add_file_real_name_' . $i] != $import_file_name) {
  446. @unlink(ROOT . $path_to_file . '/' . $file['add_file_real_name_' . $i]);
  447. }
  448. }
  449. $file['add_file_real_name_' . $i] = $import_file_name;
  450. }
  451. }
  452. }
  453.  
  454. if (!$this->error) {
  455. # Получаем размер файла
  456. $file['filesize'] = filesize(ROOT . $path_to_file . '/' . $file['real_name']) OR a_error('Ошибка в определении размера файла, возможно, он не был загружен!');
  457. # Получаем расширение файла
  458. $file['file_ext'] = array_pop(explode('.', $file['real_name']));
  459.  
  460. $file['path_to_file'] = $path_to_file;
  461. $file['directory_id'] = $directory_id;
  462. $file['about'] = $_POST['about'];
  463. $file['status'] = $_POST['status'];
  464. $file['name'] = $_POST['name'];
  465.  
  466. # Выполняем действия над определенными типами файлов
  467. $file = downloads::filetype_actions($file);
  468.  
  469. # Изменяем файл в базе
  470. downloads::update_file($this->db, $file_id, $file, $new_file);
  471.  
  472. if ($action == 'add')
  473. $message = 'Файл успешно добавлен!';
  474. if ($action == 'edit')
  475. $message = 'Файл успешно изменен!';
  476.  
  477. a_notice($message, a_url('downloads/admin/list_files', 'directory_id=' . $directory_id));
  478. }
  479. }
  480. }
  481. if (!isset($_POST['submit']) || $this->error) {
  482. $this->tpl->assign(array(
  483. 'error' => $this->error,
  484. 'error_file' => $error_file,
  485. 'file' => $file,
  486. 'action' => $action
  487. ));
  488. $this->tpl->display('file_upload');
  489. }
  490. }
  491.  
  492. /**
  493. * Удаление файла
  494. */
  495. public function action_file_delete() {
  496. main::is_demo();
  497. # Получаем информацию о файле
  498. if (!$file = $this->db->get_row("SELECT * FROM #__downloads_files WHERE file_id = '" . intval($_GET['file_id']) . "'")) {
  499. a_error('Удаляемый файл не найден!');
  500. }
  501.  
  502. # Удаление папки из ФС
  503. main::delete_dir(ROOT . $file['path_to_file']);
  504.  
  505. # Удаляем файл из БД
  506. $this->db->query("DELETE FROM #__downloads_files WHERE file_id = '" . $file['file_id'] . "'");
  507.  
  508. a_notice('Файл успешно удален!', a_url('downloads/admin/list_files', 'directory_id=' . $file['directory_id']));
  509. }
  510.  
  511. /**
  512. * Переименование файла
  513. */
  514. public function action_file_rename() {
  515. if (!$file = $this->db->get_row("SELECT * FROM #__downloads_files WHERE file_id = '" . intval($_GET['file_id']) . "'")) {
  516. a_error("Файл не найден!");
  517. }
  518.  
  519. $fields = array('real_name', 'add_file_real_name_1', 'add_file_real_name_2', 'add_file_real_name_3', 'add_file_real_name_4', 'add_file_real_name_5');
  520. if (!in_array($_GET['field_name'], $fields)) {
  521. a_error("Неверное имя поля!");
  522. }
  523.  
  524. if (empty($file[$_GET['field_name']])) {
  525. a_error("Данный файл не был загружен!");
  526. }
  527.  
  528. if (isset($_POST['submit'])) {
  529. if (empty($_POST['new_name'])) {
  530. $this->error .= 'Имя файла необходимо указывать!<br />';
  531. }
  532.  
  533. if (!$this->error) {
  534. $this->db->query("UPDATE #__downloads_files SET " . a_safe($_GET['field_name']) . " = '" . a_safe($_POST['new_name']) . "' WHERE file_id = '" . $file['file_id'] . "'");
  535. rename(ROOT . $file['path_to_file'] . '/' . $file[$_GET['field_name']], ROOT . $file['path_to_file'] . '/' . $_POST['new_name']);
  536. header("Location: " . a_url('downloads/admin/file_upload', 'file_id=' . $file['file_id']));
  537. exit;
  538. }
  539. }
  540. if (!isset($_POST['submit']) OR $this->error) {
  541. $this->tpl->assign(array(
  542. 'error' => $this->error,
  543. 'file' => $file
  544. ));
  545.  
  546. $this->tpl->display('file_rename');
  547. }
  548. }
  549.  
  550. /**
  551. * Загрузка файлов с фтп
  552. */
  553. public function action_ftp_upload() {
  554. set_time_limit(0);
  555. ignore_user_abort(TRUE);
  556.  
  557. if (!$directory = $this->db->get_row("SELECT * FROM #__downloads_directories WHERE directory_id = '" . intval($_GET['directory_id']) . "'"))
  558. a_error('Папка не найдена!');
  559.  
  560. if (isset($_POST['submit'])) {
  561. if (!file_exists(ROOT . DOWNLOADS_DIRECTORY . $this->ftp_directory . $_POST['from_directory'])) {
  562. $this->error .= 'ФТП папка не найдена!<br />';
  563. }
  564.  
  565. if (!$this->error) {
  566. $translite = isset($_POST['translite']) ? TRUE : FALSE;
  567. $this->_ftp_upload_copy_files($directory['directory_id'], $_POST['from_directory'], $translite);
  568.  
  569. a_notice("Копирование файлов завершено!", a_url('downloads/admin/list_files', 'directory_id=' . $directory['directory_id']));
  570. }
  571. }
  572. if (!isset($_POST['submit']) OR $this->error) {
  573. $this->tpl->assign(array(
  574. 'error' => $this->error,
  575. 'directory' => $directory
  576. ));
  577. $this->tpl->display('ftp_upload');
  578. }
  579. }
  580.  
  581. /**
  582. * Увеличение позиции папки
  583. */
  584. public function action_directory_up() {
  585. if (!$directory = $this->db->get_row("SELECT * FROM #__downloads_directories WHERE directory_id = " . intval($_GET['directory_id'])))
  586. a_error('Папка не найдена!');
  587.  
  588. # Меняем позиции
  589. $this->db->query("UPDATE #__downloads_directories SET position = " . $directory['position'] . " WHERE parent_id = '" . $directory['parent_id'] . "' AND position = " . ($directory['position'] - 1));
  590. $this->db->query("UPDATE #__downloads_directories SET position = " . ($directory['position'] - 1) . " WHERE directory_id = " . intval($_GET['directory_id']));
  591.  
  592. header("Location: " . a_url('downloads/admin', 'directory_id=' . $directory['parent_id'], TRUE));
  593. exit;
  594. }
  595.  
  596. /**
  597. * Уменьшение позиции папки
  598. */
  599. public function action_directory_down() {
  600. if (!$directory = $this->db->get_row("SELECT * FROM #__downloads_directories WHERE directory_id = " . intval($_GET['directory_id'])))
  601. a_error('Папка не найдена!');
  602.  
  603. # Меняем позиции
  604. $this->db->query("UPDATE #__downloads_directories SET position = " . $directory['position'] . " WHERE parent_id = '" . $directory['parent_id'] . "' AND position = " . ($directory['position'] + 1));
  605. $this->db->query("UPDATE #__downloads_directories SET position = " . ($directory['position'] + 1) . " WHERE directory_id = " . intval($_GET['directory_id']));
  606.  
  607. header("Location: " . a_url('downloads/admin', 'directory_id=' . $directory['parent_id'], TRUE));
  608. exit;
  609. }
  610.  
  611. /**
  612. * Получение списка файлов для загрузки файлов по фтп
  613. */
  614. public function action_ftp_upload_get_directories() {
  615. $directories = array();
  616. $directory = (!empty($_GET['directory']) ? $_GET['directory'] . '/' : '');
  617. $directory_for_scan = ROOT . DOWNLOADS_DIRECTORY . $this->ftp_directory . '/';
  618. $dir = opendir($directory_for_scan . $directory);
  619. while ($directory1 = readdir($dir)) {
  620. if ($directory1 == '.' || $directory1 == '..')
  621. continue;
  622. if (is_dir($directory_for_scan . $directory . $directory1)) {
  623. $directories[$directory . $directory1] = $directory1;
  624. }
  625. }
  626.  
  627. header('Content-Type: text/javascript; charset=utf-8');
  628. echo json_encode($directories);
  629. }
  630.  
  631. /**
  632. * Копирование фтп файлов в ЗЦ
  633. *
  634. * @access private
  635. */
  636. private function _ftp_upload_copy_files($directory_id, $ftp_directory, $translite = TRUE) {
  637. # Получаем путь к папке
  638. $directory_path = downloads::get_path($directory_id, $this->db);
  639. $directory_realpath = downloads::get_realpath($directory_path) . '/' . $directory_id;
  640.  
  641. # Сканируем фтп папку
  642. $ftp_directory_full = ROOT . DOWNLOADS_DIRECTORY . $this->ftp_directory . $ftp_directory;
  643. $dir = opendir($ftp_directory_full);
  644. while ($f = readdir($dir)) {
  645. if ($f == '.' || $f == '..') {
  646. continue;
  647. }
  648.  
  649. # Если это папка
  650. if (is_dir($ftp_directory_full . '/' . $f)) {
  651. # Создаем новую папку в ЗЦ
  652. $this->db->query("INSERT INTO #__downloads_directories SET
  653. name = '" . a_safe(main::translite($f)) . "',
  654. parent_id = '" . $directory_id . "'
  655. ");
  656.  
  657. $new_directory_id = $this->db->insert_id();
  658.  
  659. # Создаем папку в файловой системе
  660. mkdir(ROOT . DOWNLOADS_DIRECTORY . $directory_realpath . '/' . $new_directory_id);
  661. chmod(ROOT . DOWNLOADS_DIRECTORY . $directory_realpath . '/' . $new_directory_id, 0777);
  662.  
  663. # Запускаем сканер для новой папки
  664. $this->_ftp_upload_copy_files($new_directory_id, $ftp_directory . '/' . $f, $translite);
  665. }
  666.  
  667. # Если это файл
  668. if (is_file($ftp_directory_full . '/' . $f)) {
  669. # Создаем пустую строку в базе для файла (чтобы получить его ID)
  670. $this->db->query("INSERT INTO #__downloads_files SET file_id = 'NULL'");
  671. $new_file_id = $this->db->insert_id();
  672.  
  673. # Создаем папку для файла
  674. mkdir(ROOT . DOWNLOADS_DIRECTORY . $directory_realpath . '/' . $new_file_id);
  675. chmod(ROOT . DOWNLOADS_DIRECTORY . $directory_realpath . '/' . $new_file_id, 0777);
  676.  
  677. # Копируем файл в новую папку
  678. copy($ftp_directory_full . '/' . $f, ROOT . DOWNLOADS_DIRECTORY . $directory_realpath . '/' . $new_file_id . '/' . $f);
  679.  
  680. # Получаем размер файла
  681. $file['filesize'] = filesize(ROOT . DOWNLOADS_DIRECTORY . $directory_realpath . '/' . $new_file_id . '/' . $f) OR a_error('Ошибка в определении размера файла, возможно, он не был загружен!');
  682. # Получаем расширение файла
  683. $file['file_ext'] = array_pop(explode('.', $f));
  684. # Определяем имя файла
  685. $file['name'] = str_replace('.' . $file['file_ext'], '', $f);
  686. if ($translite) {
  687. $file['name'] = main::translite($file['name']);
  688. }
  689. $file['directory_id'] = $directory_id;
  690. $file['real_name'] = $f;
  691. $file['path_to_file'] = DOWNLOADS_DIRECTORY . $directory_realpath . '/' . $new_file_id;
  692. $file['status'] = 'active';
  693.  
  694. # Выполняем действия над определенными типами файлов
  695. $file = downloads::filetype_actions($file);
  696.  
  697. # Изменяем файл в базе
  698. downloads::update_file($this->db, $new_file_id, $file);
  699. $file = '';
  700. }
  701. }
  702. }
  703.  
  704. }
  705.  
  706. ?>