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

Размер файла: 13.09Kb
  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 downloads {
  19.  
  20. /**
  21. * @desc получение реального пути к папке
  22. * @param $int
  23. */
  24. public static function get_path($directory_id, $db, $directory_path = array(), $i = 0) {
  25. $parent = $db->get_row("SELECT * FROM #__downloads_directories WHERE
  26. directory_id = (SELECT parent_id FROM #__downloads_directories WHERE directory_id = '" . intval($directory_id) . "')
  27. ");
  28. $i++;
  29. if ($parent['directory_id'] != 0) {
  30. $directory_path[$i]['directory_id'] = $parent['directory_id'];
  31. $directory_path[$i]['name'] = $parent['name'];
  32. $directory_path = self::get_path($parent['directory_id'], $db, $directory_path, $i);
  33. }
  34. return $directory_path;
  35. }
  36.  
  37. /**
  38. * @desc получение полного пути к папке
  39. * @param $array
  40. */
  41. public static function get_realpath($directories_array) {
  42. if (empty($directories_array))
  43. return;
  44.  
  45. foreach ($directories_array AS $directory) {
  46. $directory_path[] = $directory['directory_id'];
  47. }
  48. if (count($directory_path) > 1) {
  49. $realpath = array_reverse($directory_path);
  50. $realpath = implode('/', $realpath);
  51. } else {
  52. $realpath = $directory_path[0];
  53. }
  54.  
  55. return $realpath;
  56. }
  57.  
  58. /**
  59. * @desc получение полного пути к папке
  60. * @param $array
  61. */
  62. public static function get_namepath($directories_array, $delim = '/', $admin = FALSE) {
  63. if (empty($directories_array)) {
  64. return;
  65. }
  66.  
  67. $segment = $admin ? 'downloads/admin' : 'downloads';
  68.  
  69. foreach ($directories_array AS $directory) {
  70. $directory_path[] = '<a href="' . a_url($segment, 'directory_id=' . $directory['directory_id']) . '">' . $directory['name'] . '</a>';
  71. }
  72. if (count($directory_path) > 1) {
  73. $realpath = array_reverse($directory_path);
  74. $realpath = implode($delim, $realpath);
  75. } else {
  76. $realpath = $directory_path[0];
  77. }
  78.  
  79. return $realpath;
  80. }
  81.  
  82. /**
  83. * @desc скачка файла
  84. */
  85. public static function force_download($filename = '', $data = '', $prefix = '', $attachment = TRUE) {
  86. if ($filename == '' OR $data == '') {
  87. return FALSE;
  88. }
  89.  
  90. // Try to determine if the filename includes a file extension.
  91. // We need it in order to set the MIME type
  92. if (FALSE === strpos($filename, '.')) {
  93. return FALSE;
  94. }
  95.  
  96. // Grab the file extension
  97. $x = explode('.', $filename);
  98. $extension = end($x);
  99.  
  100. // Load the mime types
  101. @include(ROOT . 'utils/mimes.php');
  102.  
  103. // Set a default mime if we can't find it
  104. if (!isset($mimes[$extension])) {
  105. $mime = 'application/octet-stream';
  106. } else {
  107. $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  108. }
  109.  
  110. if (!$attachment) {
  111. //exit($mime);
  112. header('Content-Type: ' . $mime);
  113. header("Content-Length: " . strlen($data));
  114. } else {
  115. // Generate the server headers
  116. if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
  117. header('Content-Type: ' . $mime);
  118. header('Content-Disposition: attachment; filename=' . $prefix . $filename);
  119. header('Expires: 0');
  120. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  121. header("Content-Transfer-Encoding: binary");
  122. header('Pragma: public');
  123. header("Content-Length: " . strlen($data));
  124. } else {
  125. header('Content-Type: ' . $mime);
  126. header('Content-Disposition: attachment; filename=' . $prefix . $filename);
  127. header("Content-Transfer-Encoding: binary");
  128. header('Expires: 0');
  129. header('Pragma: no-cache');
  130. header("Content-Length: " . strlen($data));
  131. }
  132. }
  133.  
  134. exit($data);
  135. }
  136.  
  137. /**
  138. * @desc Рекурсивный подсчет файлов в папке
  139. * @param $string
  140. */
  141. public static function count_files($directory_id) {
  142. $db = Registry::get('db');
  143.  
  144. $files = $db->get_one("SELECT COUNT(*) FROM #__downloads_files WHERE directory_id = '" . intval($directory_id) . "' AND real_name != ''");
  145. $result = $db->query("SELECT * FROM #__downloads_directories WHERE parent_id = '" . intval($directory_id) . "'");
  146. while ($cild_directory = $db->fetch_array($result)) {
  147. $files += self::count_files($cild_directory['directory_id']);
  148. }
  149. return $files;
  150. }
  151.  
  152. /**
  153. * @desc определение имени файла
  154. * @param $string
  155. */
  156. public static function get_filename($string) {
  157. $headers = get_headers($string);
  158.  
  159. foreach ($headers AS $header) {
  160. if (stripos($header, 'Location:') === 0) {
  161. $url = trim(str_ireplace('Location: ', '', $header));
  162. return basename($url);
  163. }
  164. }
  165.  
  166. return false;
  167. }
  168.  
  169. /**
  170. * Действия над файлом
  171. */
  172. public static function filetype_actions($file) {
  173. $config = Registry::get('config');
  174.  
  175. $file_path = ROOT . $file['path_to_file'] . '/' . $file['real_name'];
  176.  
  177. if (class_exists('ffmpeg_movie')) {
  178. # Работаем с видео файлами
  179. if ($file['file_ext'] == '3gp' || $file['file_ext'] == 'mp4' || $file['file_ext'] == 'avi') {
  180. $ff = new ffmpeg_movie($file_path);
  181.  
  182. # Продолжительность
  183. $duration = $ff->getDuration();
  184. $min = floor($duration / 60);
  185. $sec = floor($duration % 60);
  186. $file['file_info'] = 'Продолжительность: ' . ($min > 0 ? $min . ' мин. ' : '') . $sec . ' сек.' . PHP_EOL;
  187.  
  188. # Разрешение экрана
  189. $w = $ff->getFrameWidth();
  190. $h = $ff->getFrameHeight();
  191. $file['file_info'] .= 'Разрешение: ' . $w . 'x' . $h . PHP_EOL;
  192.  
  193. # Создаем скрины для видео
  194. if ($config['downloads']['make_screens_from_video']) {
  195. $frame_count = $ff->getFrameCount();
  196.  
  197. $frame_1 = floor($frame_count / 3);
  198. $frame_2 = $frame_1 * 2;
  199. $frame_3 = $frame_1 * 3;
  200.  
  201. $frames_arr = array(
  202. 1 => $frame_1,
  203. 2 => $frame_2,
  204. 3 => $frame_3
  205. );
  206.  
  207. foreach ($frames_arr AS $key => $frame_num) {
  208. if ($frame_num < 1)
  209. continue;
  210. $screen_name = ROOT . $file['path_to_file'] . '/screen' . $key . '.jpg';
  211. if (!$frame = $ff->getFrame($frame_num))
  212. continue;
  213. $im = $frame->toGDimage();
  214. $im1 = imagecreatetruecolor(120, 100);
  215. imagecopyresampled($im1, $im, 0, 0, 0, 0, 120, 100, imagesx($im), imagesy($im));
  216. imagejpeg($im1, $screen_name);
  217. imagedestroy($im1);
  218. imagedestroy($im);
  219.  
  220. $file['screen' . $key] = 'screen' . $key . '.jpg';
  221. }
  222. }
  223. }
  224.  
  225. # Работаем с мп3
  226. if ($file['file_ext'] == 'mp3') {
  227. $ff = new ffmpeg_movie($file_path);
  228.  
  229. # Продолжительность
  230. $duration = $ff->getDuration();
  231. $min = floor($duration / 60);
  232. $sec = floor($duration % 60);
  233. $file['file_info'] = 'Продолжительность: ' . ($min > 0 ? $min . ' мин. ' : '') . $sec . ' сек.' . PHP_EOL;
  234.  
  235. # ID3 информация
  236. $artist = $ff->getArtist();
  237. $genre = $ff->getGenre();
  238. if (!empty($artist))
  239. $file['file_info'] .= 'Исполнитель: ' . $artist . PHP_EOL;
  240. if (!empty($genre))
  241. $file['file_info'] .= 'Жанр: ' . $genre . PHP_EOL;
  242.  
  243. # Битрейт и тп
  244. $file['file_info'] .= 'Аудио: ' . round($ff->getBitRate() / 1000) . ' Kbps, ' . ($ff->getAudioChannels() == 1 ? 'Mono' : 'Stereo') . PHP_EOL;
  245. }
  246. }
  247.  
  248. # Создаем превьюшки для картинок, анимаций и файлов со скринами
  249. if ($file['screen1'] != '' || $file['file_ext'] == 'png' || $file['file_ext'] == 'gif' || $file['file_ext'] == 'jpg' || $file['file_ext'] == 'jpeg') {
  250. if ($file['screen1'] != '')
  251. $src_file = ROOT . $file['path_to_file'] . '/' . $file['screen1'];
  252. else
  253. $src_file = ROOT . $file['path_to_file'] . '/' . $file['real_name'];
  254.  
  255. # 20x20
  256. main::image_resize($src_file, ROOT . $file['path_to_file'] . '/preview_20.jpg', 20, 20);
  257. # 60x60
  258. main::image_resize($src_file, ROOT . $file['path_to_file'] . '/preview_60.jpg', 60, 60);
  259. # 100x100
  260. main::image_resize($src_file, ROOT . $file['path_to_file'] . '/preview_100.jpg', 100, 100);
  261.  
  262. $file['previews'] = 'yes';
  263. }
  264. return $file;
  265. }
  266.  
  267. /**
  268. * Изменение данных файла в базе
  269. */
  270. public static function update_file($db, $file_id, $file, $new_file = true) {
  271. $db->query("UPDATE #__downloads_files SET
  272. directory_id = '" . intval($file['directory_id']) . "',
  273. user_id = '" . (!empty($file['user_id']) ? $file['user_id'] : USER_ID) . "',
  274. name = '" . ($file['name'] ? a_safe($file['name']) : a_safe(str_replace('.' . $file['file_ext'], '', $file['real_name']))) . "',
  275. " . ($new_file ? "time = UNIX_TIMESTAMP()," : "") . "
  276. real_name = '" . a_safe($file['real_name']) . "',
  277. path_to_file = '" . $file['path_to_file'] . "',
  278. about = '" . a_safe($file['about']) . "',
  279. filesize = '" . $file['filesize'] . "',
  280. file_ext = '" . strtolower($file['file_ext']) . "',
  281. screen1 = '" . a_safe($file['screen1']) . "',
  282. screen2 = '" . a_safe($file['screen2']) . "',
  283. screen3 = '" . a_safe($file['screen3']) . "',
  284. add_file_real_name_1 = '" . (!empty($file['add_file_real_name_1']) ? a_safe($file['add_file_real_name_1']) : '') . "',
  285. add_file_real_name_2 = '" . (!empty($file['add_file_real_name_2']) ? a_safe($file['add_file_real_name_2']) : '') . "',
  286. add_file_real_name_3 = '" . (!empty($file['add_file_real_name_3']) ? a_safe($file['add_file_real_name_3']) : '') . "',
  287. add_file_real_name_4 = '" . (!empty($file['add_file_real_name_4']) ? a_safe($file['add_file_real_name_4']) : '') . "',
  288. add_file_real_name_5 = '" . (!empty($file['add_file_real_name_5']) ? a_safe($file['add_file_real_name_5']) : '') . "',
  289. previews = '" . $file['previews'] . "',
  290. status = '" . a_safe($file['status']) . "',
  291. file_info = '" . a_safe($file['file_info']) . "'
  292. WHERE
  293. file_id = '" . intval($file_id) . "'
  294. ");
  295. }
  296.  
  297. /**
  298. * Получение размера удаленного файла
  299. */
  300. public static function get_filesize($file_path) {
  301. $headers = get_headers($file_path, 1);
  302. if ((!array_key_exists('Content-Length', $headers))) {
  303. return false;
  304. }
  305. return $headers['Content-Length'];
  306. }
  307.  
  308. /**
  309. * Получение настоящего адреса файла
  310. * @return string
  311. */
  312. public static function get_real_file_path($file_path) {
  313. $headers = get_headers($file_path, 1);
  314. if (!array_key_exists('Location', $headers)) {
  315. return $file_path;
  316. }
  317. if (!strstr($headers['Location'], 'http://')) {
  318. $url_data = parse_url($file_path);
  319. $location = 'http://' . $url_data['host'] . '/' . $headers['Location'];
  320. } else
  321. $location = $headers['Location'];
  322. return self::get_real_file_path($location);
  323. }
  324.  
  325. /**
  326. * Получение настоящего адреса файла
  327. * @param integer $directory_id
  328. */
  329. public static function delete_directories($directory_id) {
  330. $db = Registry::get('db');
  331.  
  332. $result = $db->query("SELECT * FROM #__downloads_directories WHERE parent_id = $directory_id");
  333. while ($dir = $db->fetch_array($result)) {
  334. $db->query("DELETE FROM #__downloads_directories WHERE directory_id = " . $dir['directory_id']);
  335. self::delete_directories($dir['directory_id']);
  336. }
  337. }
  338.  
  339. }
  340.  
  341. ?>