PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/smart-service.com.ua/www/admin/controller/common/filemanager.php

https://bitbucket.org/spivovartsev/smart-service.com.ua
PHP | 503 lines | 376 code | 127 blank | 0 comment | 108 complexity | 2ee379835f005413e9e64488d8272b9b MD5 | raw file
  1. <?php
  2. class ControllerCommonFileManager extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('common/filemanager');
  6. $this->data['title'] = $this->language->get('heading_title');
  7. if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
  8. $this->data['base'] = HTTPS_SERVER;
  9. } else {
  10. $this->data['base'] = HTTP_SERVER;
  11. }
  12. $this->data['entry_folder'] = $this->language->get('entry_folder');
  13. $this->data['entry_move'] = $this->language->get('entry_move');
  14. $this->data['entry_copy'] = $this->language->get('entry_copy');
  15. $this->data['entry_rename'] = $this->language->get('entry_rename');
  16. $this->data['button_folder'] = $this->language->get('button_folder');
  17. $this->data['button_delete'] = $this->language->get('button_delete');
  18. $this->data['button_move'] = $this->language->get('button_move');
  19. $this->data['button_copy'] = $this->language->get('button_copy');
  20. $this->data['button_rename'] = $this->language->get('button_rename');
  21. $this->data['button_upload'] = $this->language->get('button_upload');
  22. $this->data['button_refresh'] = $this->language->get('button_refresh');
  23. $this->data['error_select'] = $this->language->get('error_select');
  24. $this->data['error_directory'] = $this->language->get('error_directory');
  25. $this->data['token'] = $this->session->data['token'];
  26. $this->data['directory'] = HTTP_IMAGE . 'data/';
  27. if (isset($this->request->get['field'])) {
  28. $this->data['field'] = $this->request->get['field'];
  29. } else {
  30. $this->data['field'] = '';
  31. }
  32. if (isset($this->request->get['CKEditorFuncNum'])) {
  33. $this->data['fckeditor'] = TRUE;
  34. } else {
  35. $this->data['fckeditor'] = FALSE;
  36. }
  37. $this->template = 'common/filemanager.tpl';
  38. $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
  39. }
  40. public function image() {
  41. $this->load->model('tool/image');
  42. if (isset($this->request->post['image'])) {
  43. $this->response->setOutput($this->model_tool_image->resize($this->request->post['image'], 100, 100));
  44. }
  45. }
  46. public function directory() {
  47. $json = array();
  48. if (isset($this->request->post['directory'])) {
  49. $directories = glob(rtrim(DIR_IMAGE . 'data/' . str_replace('../', '', $this->request->post['directory']), '/') . '/*', GLOB_ONLYDIR);
  50. if ($directories) {
  51. $i = 0;
  52. foreach ($directories as $directory) {
  53. $json[$i]['data'] = basename($directory);
  54. $json[$i]['attributes']['directory'] = substr($directory, strlen(DIR_IMAGE . 'data/'));
  55. $children = glob(rtrim($directory, '/') . '/*', GLOB_ONLYDIR);
  56. if ($children) {
  57. $json[$i]['children'] = ' ';
  58. }
  59. $i++;
  60. }
  61. }
  62. }
  63. $this->load->library('json');
  64. $this->response->setOutput(Json::encode($json));
  65. }
  66. public function files() {
  67. $json = array();
  68. $this->load->model('tool/image');
  69. if (isset($this->request->post['directory']) && $this->request->post['directory']) {
  70. $directory = DIR_IMAGE . 'data/' . str_replace('../', '', $this->request->post['directory']);
  71. } else {
  72. $directory = DIR_IMAGE . 'data/';
  73. }
  74. $allowed = array(
  75. '.jpg',
  76. '.jpeg',
  77. '.png',
  78. '.gif'
  79. );
  80. $files = glob(rtrim($directory, '/') . '/*');
  81. foreach ($files as $file) {
  82. if (is_file($file)) {
  83. $ext = strrchr($file, '.');
  84. } else {
  85. $ext = '';
  86. }
  87. if (in_array(strtolower($ext), $allowed)) {
  88. $size = filesize($file);
  89. $i = 0;
  90. $suffix = array(
  91. 'B',
  92. 'KB',
  93. 'MB',
  94. 'GB',
  95. 'TB',
  96. 'PB',
  97. 'EB',
  98. 'ZB',
  99. 'YB'
  100. );
  101. while (($size / 1024) > 1) {
  102. $size = $size / 1024;
  103. $i++;
  104. }
  105. $json[] = array(
  106. 'file' => substr($file, strlen(DIR_IMAGE . 'data/')),
  107. 'filename' => basename($file),
  108. 'size' => round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
  109. 'thumb' => $this->model_tool_image->resize(substr($file, strlen(DIR_IMAGE)), 100, 100)
  110. );
  111. }
  112. }
  113. $this->load->library('json');
  114. $this->response->setOutput(Json::encode($json));
  115. }
  116. public function create() {
  117. $this->load->language('common/filemanager');
  118. $json = array();
  119. if (isset($this->request->post['directory'])) {
  120. if (isset($this->request->post['name']) || $this->request->post['name']) {
  121. $directory = rtrim(DIR_IMAGE . 'data/' . str_replace('../', '', $this->request->post['directory']), '/');
  122. if (!is_dir($directory)) {
  123. $json['error'] = $this->language->get('error_directory');
  124. }
  125. if (file_exists($directory . '/' . str_replace('../', '', $this->request->post['name']))) {
  126. $json['error'] = $this->language->get('error_exists');
  127. }
  128. } else {
  129. $json['error'] = $this->language->get('error_name');
  130. }
  131. } else {
  132. $json['error'] = $this->language->get('error_directory');
  133. }
  134. if (!$this->user->hasPermission('modify', 'common/filemanager')) {
  135. $json['error'] = $this->language->get('error_permission');
  136. }
  137. if (!isset($json['error'])) {
  138. mkdir($directory . '/' . str_replace('../', '', $this->request->post['name']), 0777);
  139. $json['success'] = $this->language->get('text_create');
  140. }
  141. $this->load->library('json');
  142. $this->response->setOutput(Json::encode($json));
  143. }
  144. public function delete() {
  145. $this->load->language('common/filemanager');
  146. $json = array();
  147. if (isset($this->request->post['path'])) {
  148. $path = rtrim(DIR_IMAGE . 'data/' . str_replace('../', '', $this->request->post['path']), '/');
  149. if (!file_exists($path)) {
  150. $json['error'] = $this->language->get('error_select');
  151. }
  152. if ($path == rtrim(DIR_IMAGE . 'data/', '/')) {
  153. $json['error'] = $this->language->get('error_delete');
  154. }
  155. } else {
  156. $json['error'] = $this->language->get('error_select');
  157. }
  158. if (!$this->user->hasPermission('modify', 'common/filemanager')) {
  159. $json['error'] = $this->language->get('error_permission');
  160. }
  161. if (!isset($json['error'])) {
  162. if (is_file($path)) {
  163. unlink($path);
  164. } elseif (is_dir($path)) {
  165. $this->recursiveDelete($path);
  166. }
  167. $json['success'] = $this->language->get('text_delete');
  168. }
  169. $this->load->library('json');
  170. $this->response->setOutput(Json::encode($json));
  171. }
  172. protected function recursiveDelete($directory) {
  173. if (is_dir($directory)) {
  174. $handle = opendir($directory);
  175. }
  176. if (!$handle) {
  177. return FALSE;
  178. }
  179. while (false !== ($file = readdir($handle))) {
  180. if ($file != '.' && $file != '..') {
  181. if (!is_dir($directory . '/' . $file)) {
  182. unlink($directory . '/' . $file);
  183. } else {
  184. $this->recursiveDelete($directory . '/' . $file);
  185. }
  186. }
  187. }
  188. closedir($handle);
  189. rmdir($directory);
  190. return TRUE;
  191. }
  192. public function move() {
  193. $this->load->language('common/filemanager');
  194. $json = array();
  195. if (isset($this->request->post['from']) && isset($this->request->post['to'])) {
  196. $from = rtrim(DIR_IMAGE . 'data/' . str_replace('../', '', $this->request->post['from']), '/');
  197. if (!file_exists($from)) {
  198. $json['error'] = $this->language->get('error_missing');
  199. }
  200. if ($from == DIR_IMAGE . 'data') {
  201. $json['error'] = $this->language->get('error_default');
  202. }
  203. $to = rtrim(DIR_IMAGE . 'data/' . str_replace('../', '', $this->request->post['to']), '/');
  204. if (!file_exists($to)) {
  205. $json['error'] = $this->language->get('error_move');
  206. }
  207. if (file_exists($to . '/' . basename($from))) {
  208. $json['error'] = $this->language->get('error_exists');
  209. }
  210. } else {
  211. $json['error'] = $this->language->get('error_directory');
  212. }
  213. if (!$this->user->hasPermission('modify', 'common/filemanager')) {
  214. $json['error'] = $this->language->get('error_permission');
  215. }
  216. if (!isset($json['error'])) {
  217. rename($from, $to . '/' . basename($from));
  218. $json['success'] = $this->language->get('text_move');
  219. }
  220. $this->load->library('json');
  221. $this->response->setOutput(Json::encode($json));
  222. }
  223. public function copy() {
  224. $this->load->language('common/filemanager');
  225. $json = array();
  226. if (isset($this->request->post['path']) && isset($this->request->post['name'])) {
  227. if ((strlen(utf8_decode($this->request->post['name'])) < 3) || (strlen(utf8_decode($this->request->post['name'])) > 255)) {
  228. $json['error'] = $this->language->get('error_filename');
  229. }
  230. $old_name = rtrim(DIR_IMAGE . 'data/' . str_replace('../', '', $this->request->post['path']), '/');
  231. if (!file_exists($old_name) || $old_name == DIR_IMAGE . 'data') {
  232. $json['error'] = $this->language->get('error_copy');
  233. }
  234. if (is_file($old_name)) {
  235. $ext = strrchr($old_name, '.');
  236. } else {
  237. $ext = '';
  238. }
  239. $new_name = dirname($old_name) . '/' . str_replace('../', '', $this->request->post['name'] . $ext);
  240. if (file_exists($new_name)) {
  241. $json['error'] = $this->language->get('error_exists');
  242. }
  243. } else {
  244. $json['error'] = $this->language->get('error_select');
  245. }
  246. if (!$this->user->hasPermission('modify', 'common/filemanager')) {
  247. $json['error'] = $this->language->get('error_permission');
  248. }
  249. if (!isset($json['error'])) {
  250. if (is_file($old_name)) {
  251. copy($old_name, $new_name);
  252. } else {
  253. $this->recursiveCopy($old_name, $new_name);
  254. }
  255. $json['success'] = $this->language->get('text_copy');
  256. }
  257. $this->load->library('json');
  258. $this->response->setOutput(Json::encode($json));
  259. }
  260. function recursiveCopy($source, $destination) {
  261. $directory = opendir($source);
  262. @mkdir($destination);
  263. while (false !== ($file = readdir($handle))) {
  264. if (($file != '.') && ($file != '..')) {
  265. if (is_dir($source . '/' . $file)) {
  266. $this->recursiveCopy($source . '/' . $file, $destination . '/' . $file);
  267. } else {
  268. copy($source . '/' . $file, $destination . '/' . $file);
  269. }
  270. }
  271. }
  272. closedir($directory);
  273. }
  274. public function folders() {
  275. $this->response->setOutput($this->recursiveFolders(DIR_IMAGE . 'data/'));
  276. }
  277. protected function recursiveFolders($directory) {
  278. $output = '';
  279. $output .= '<option value="' . substr($directory, strlen(DIR_IMAGE . 'data/')) . '">' . substr($directory, strlen(DIR_IMAGE . 'data/')) . '</option>';
  280. $directories = glob(rtrim(str_replace('../', '', $directory), '/') . '/*', GLOB_ONLYDIR);
  281. foreach ($directories as $directory) {
  282. $output .= $this->recursiveFolders($directory);
  283. }
  284. return $output;
  285. }
  286. public function rename() {
  287. $this->load->language('common/filemanager');
  288. $json = array();
  289. if (isset($this->request->post['path']) && isset($this->request->post['name'])) {
  290. if ((strlen(utf8_decode($this->request->post['name'])) < 3) || (strlen(utf8_decode($this->request->post['name'])) > 255)) {
  291. $json['error'] = $this->language->get('error_filename');
  292. }
  293. $old_name = rtrim(DIR_IMAGE . 'data/' . str_replace('../', '', $this->request->post['path']), '/');
  294. if (!file_exists($old_name) || $old_name == DIR_IMAGE . 'data') {
  295. $json['error'] = $this->language->get('error_rename');
  296. }
  297. if (is_file($old_name)) {
  298. $ext = strrchr($old_name, '.');
  299. } else {
  300. $ext = '';
  301. }
  302. $new_name = dirname($old_name) . '/' . str_replace('../', '', $this->request->post['name'] . $ext);
  303. if (file_exists($new_name)) {
  304. $json['error'] = $this->language->get('error_exists');
  305. }
  306. }
  307. if (!$this->user->hasPermission('modify', 'common/filemanager')) {
  308. $json['error'] = $this->language->get('error_permission');
  309. }
  310. if (!isset($json['error'])) {
  311. rename($old_name, $new_name);
  312. $json['success'] = $this->language->get('text_rename');
  313. }
  314. $this->load->library('json');
  315. $this->response->setOutput(Json::encode($json));
  316. }
  317. public function upload() {
  318. $this->load->language('common/filemanager');
  319. $json = array();
  320. if (isset($this->request->post['directory'])) {
  321. if (isset($this->request->files['image']) && $this->request->files['image']['tmp_name']) {
  322. if ((strlen(utf8_decode($this->request->files['image']['name'])) < 3) || (strlen(utf8_decode($this->request->files['image']['name'])) > 255)) {
  323. $json['error'] = $this->language->get('error_filename');
  324. }
  325. $directory = rtrim(DIR_IMAGE . 'data/' . str_replace('../', '', $this->request->post['directory']), '/');
  326. if (!is_dir($directory)) {
  327. $json['error'] = $this->language->get('error_directory');
  328. }
  329. if ($this->request->files['image']['size'] > 300000) {
  330. $json['error'] = $this->language->get('error_file_size');
  331. }
  332. $allowed = array(
  333. 'image/jpeg',
  334. 'image/pjpeg',
  335. 'image/png',
  336. 'image/x-png',
  337. 'image/gif',
  338. 'application/x-shockwave-flash'
  339. );
  340. if (!in_array($this->request->files['image']['type'], $allowed)) {
  341. $json['error'] = $this->language->get('error_file_type');
  342. }
  343. $allowed = array(
  344. '.jpg',
  345. '.jpeg',
  346. '.gif',
  347. '.png',
  348. '.flv'
  349. );
  350. if (!in_array(strtolower(strrchr($this->request->files['image']['name'], '.')), $allowed)) {
  351. $json['error'] = $this->language->get('error_file_type');
  352. }
  353. if ($this->request->files['image']['error'] != UPLOAD_ERR_OK) {
  354. $json['error'] = 'error_upload_' . $this->request->files['image']['error'];
  355. }
  356. } else {
  357. $json['error'] = $this->language->get('error_file');
  358. }
  359. } else {
  360. $json['error'] = $this->language->get('error_directory');
  361. }
  362. if (!$this->user->hasPermission('modify', 'common/filemanager')) {
  363. $json['error'] = $this->language->get('error_permission');
  364. }
  365. if (!isset($json['error'])) {
  366. if (@move_uploaded_file($this->request->files['image']['tmp_name'], $directory . '/' . basename($this->request->files['image']['name']))) {
  367. $json['success'] = $this->language->get('text_uploaded');
  368. } else {
  369. $json['error'] = $this->language->get('error_uploaded');
  370. }
  371. }
  372. $this->load->library('json');
  373. $this->response->setOutput(Json::encode($json));
  374. }
  375. }
  376. ?>