PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/controller/common/filemanager.php

https://gitlab.com/reclamare/mao
PHP | 413 lines | 313 code | 54 blank | 46 comment | 38 complexity | 6ea2c11a9ddb435f10c4d93946aad17a MD5 | raw file
  1. <?php
  2. class ControllerCommonFileManager extends Controller {
  3. public function index() {
  4. $this->load->language('common/filemanager');
  5. if (isset($this->request->get['filter_name'])) {
  6. $filter_name = rtrim(str_replace(array('../', '..\\', '..', '*'), '', $this->request->get['filter_name']), '/');
  7. } else {
  8. $filter_name = null;
  9. }
  10. // Make sure we have the correct directory
  11. if (isset($this->request->get['directory'])) {
  12. $directory = rtrim(DIR_IMAGE . 'catalog/' . str_replace(array('../', '..\\', '..'), '', $this->request->get['directory']), '/');
  13. } else {
  14. $directory = DIR_IMAGE . 'catalog';
  15. }
  16. if (isset($this->request->get['page'])) {
  17. $page = $this->request->get['page'];
  18. } else {
  19. $page = 1;
  20. }
  21. $data['images'] = array();
  22. $this->load->model('tool/image');
  23. // Get directories
  24. $directories = glob($directory . '/' . $filter_name . '*', GLOB_ONLYDIR);
  25. if (!$directories) {
  26. $directories = array();
  27. }
  28. // Get files
  29. $files = glob($directory . '/' . $filter_name . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
  30. if (!$files) {
  31. $files = array();
  32. }
  33. // Merge directories and files
  34. $images = array_merge($directories, $files);
  35. // Get total number of files and directories
  36. $image_total = count($images);
  37. // Split the array based on current page number and max number of items per page of 10
  38. $images = array_splice($images, ($page - 1) * 16, 16);
  39. foreach ($images as $image) {
  40. $name = str_split(basename($image), 14);
  41. if (is_dir($image)) {
  42. $url = '';
  43. if (isset($this->request->get['target'])) {
  44. $url .= '&target=' . $this->request->get['target'];
  45. }
  46. if (isset($this->request->get['thumb'])) {
  47. $url .= '&thumb=' . $this->request->get['thumb'];
  48. }
  49. $data['images'][] = array(
  50. 'thumb' => '',
  51. 'name' => implode(' ', $name),
  52. 'type' => 'directory',
  53. 'path' => utf8_substr($image, utf8_strlen(DIR_IMAGE)),
  54. 'href' => $this->url->link('common/filemanager', 'token=' . $this->session->data['token'] . '&directory=' . urlencode(utf8_substr($image, utf8_strlen(DIR_IMAGE . 'catalog/'))) . $url, 'SSL')
  55. );
  56. } elseif (is_file($image)) {
  57. // Find which protocol to use to pass the full image link back
  58. if ($this->request->server['HTTPS']) {
  59. $server = HTTPS_CATALOG;
  60. } else {
  61. $server = HTTP_CATALOG;
  62. }
  63. $data['images'][] = array(
  64. 'thumb' => $this->model_tool_image->resize(utf8_substr($image, utf8_strlen(DIR_IMAGE)), 100, 100),
  65. 'name' => implode(' ', $name),
  66. 'type' => 'image',
  67. 'path' => utf8_substr($image, utf8_strlen(DIR_IMAGE)),
  68. 'href' => $server . 'image/' . utf8_substr($image, utf8_strlen(DIR_IMAGE))
  69. );
  70. }
  71. }
  72. $data['heading_title'] = $this->language->get('heading_title');
  73. $data['text_no_results'] = $this->language->get('text_no_results');
  74. $data['text_confirm'] = $this->language->get('text_confirm');
  75. $data['entry_search'] = $this->language->get('entry_search');
  76. $data['entry_folder'] = $this->language->get('entry_folder');
  77. $data['button_parent'] = $this->language->get('button_parent');
  78. $data['button_refresh'] = $this->language->get('button_refresh');
  79. $data['button_upload'] = $this->language->get('button_upload');
  80. $data['button_folder'] = $this->language->get('button_folder');
  81. $data['button_delete'] = $this->language->get('button_delete');
  82. $data['button_search'] = $this->language->get('button_search');
  83. $data['token'] = $this->session->data['token'];
  84. if (isset($this->request->get['directory'])) {
  85. $data['directory'] = urlencode($this->request->get['directory']);
  86. } else {
  87. $data['directory'] = '';
  88. }
  89. if (isset($this->request->get['filter_name'])) {
  90. $data['filter_name'] = $this->request->get['filter_name'];
  91. } else {
  92. $data['filter_name'] = '';
  93. }
  94. // Return the target ID for the file manager to set the value
  95. if (isset($this->request->get['target'])) {
  96. $data['target'] = $this->request->get['target'];
  97. } else {
  98. $data['target'] = '';
  99. }
  100. // Return the thumbnail for the file manager to show a thumbnail
  101. if (isset($this->request->get['thumb'])) {
  102. $data['thumb'] = $this->request->get['thumb'];
  103. } else {
  104. $data['thumb'] = '';
  105. }
  106. // Parent
  107. $url = '';
  108. if (isset($this->request->get['directory'])) {
  109. $pos = strrpos($this->request->get['directory'], '/');
  110. if ($pos) {
  111. $url .= '&directory=' . urlencode(substr($this->request->get['directory'], 0, $pos));
  112. }
  113. }
  114. if (isset($this->request->get['target'])) {
  115. $url .= '&target=' . $this->request->get['target'];
  116. }
  117. if (isset($this->request->get['thumb'])) {
  118. $url .= '&thumb=' . $this->request->get['thumb'];
  119. }
  120. $data['parent'] = $this->url->link('common/filemanager', 'token=' . $this->session->data['token'] . $url, 'SSL');
  121. // Refresh
  122. $url = '';
  123. if (isset($this->request->get['directory'])) {
  124. $url .= '&directory=' . urlencode($this->request->get['directory']);
  125. }
  126. if (isset($this->request->get['target'])) {
  127. $url .= '&target=' . $this->request->get['target'];
  128. }
  129. if (isset($this->request->get['thumb'])) {
  130. $url .= '&thumb=' . $this->request->get['thumb'];
  131. }
  132. $data['refresh'] = $this->url->link('common/filemanager', 'token=' . $this->session->data['token'] . $url, 'SSL');
  133. $url = '';
  134. if (isset($this->request->get['directory'])) {
  135. $url .= '&directory=' . urlencode(html_entity_decode($this->request->get['directory'], ENT_QUOTES, 'UTF-8'));
  136. }
  137. if (isset($this->request->get['filter_name'])) {
  138. $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
  139. }
  140. if (isset($this->request->get['target'])) {
  141. $url .= '&target=' . $this->request->get['target'];
  142. }
  143. if (isset($this->request->get['thumb'])) {
  144. $url .= '&thumb=' . $this->request->get['thumb'];
  145. }
  146. $pagination = new Pagination();
  147. $pagination->total = $image_total;
  148. $pagination->page = $page;
  149. $pagination->limit = 16;
  150. $pagination->url = $this->url->link('common/filemanager', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
  151. $data['pagination'] = $pagination->render();
  152. $this->response->setOutput($this->load->view('common/filemanager.tpl', $data));
  153. }
  154. public function upload() {
  155. $this->load->language('common/filemanager');
  156. $json = array();
  157. // Check user has permission
  158. if (!$this->user->hasPermission('modify', 'common/filemanager')) {
  159. $json['error'] = $this->language->get('error_permission');
  160. }
  161. // Make sure we have the correct directory
  162. if (isset($this->request->get['directory'])) {
  163. $directory = rtrim(DIR_IMAGE . 'catalog/' . str_replace(array('../', '..\\', '..'), '', $this->request->get['directory']), '/');
  164. } else {
  165. $directory = DIR_IMAGE . 'catalog';
  166. }
  167. // Check its a directory
  168. if (!is_dir($directory)) {
  169. $json['error'] = $this->language->get('error_directory');
  170. }
  171. if (!$json) {
  172. if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
  173. // Sanitize the filename
  174. $filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));
  175. // Validate the filename length
  176. if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 255)) {
  177. $json['error'] = $this->language->get('error_filename');
  178. }
  179. // Allowed file extension types
  180. $allowed = array(
  181. 'jpg',
  182. 'jpeg',
  183. 'gif',
  184. 'png'
  185. );
  186. if (!in_array(utf8_strtolower(utf8_substr(strrchr($filename, '.'), 1)), $allowed)) {
  187. $json['error'] = $this->language->get('error_filetype');
  188. }
  189. // Allowed file mime types
  190. $allowed = array(
  191. 'image/jpeg',
  192. 'image/pjpeg',
  193. 'image/png',
  194. 'image/x-png',
  195. 'image/gif'
  196. );
  197. if (!in_array($this->request->files['file']['type'], $allowed)) {
  198. $json['error'] = $this->language->get('error_filetype');
  199. }
  200. // Check to see if any PHP files are trying to be uploaded
  201. $content = file_get_contents($this->request->files['file']['tmp_name']);
  202. if (preg_match('/\<\?php/i', $content)) {
  203. $json['error'] = $this->language->get('error_filetype');
  204. }
  205. // Return any upload error
  206. if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
  207. $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
  208. }
  209. } else {
  210. $json['error'] = $this->language->get('error_upload');
  211. }
  212. }
  213. if (!$json) {
  214. move_uploaded_file($this->request->files['file']['tmp_name'], $directory . '/' . $filename);
  215. $json['success'] = $this->language->get('text_uploaded');
  216. }
  217. $this->response->addHeader('Content-Type: application/json');
  218. $this->response->setOutput(json_encode($json));
  219. }
  220. public function folder() {
  221. $this->load->language('common/filemanager');
  222. $json = array();
  223. // Check user has permission
  224. if (!$this->user->hasPermission('modify', 'common/filemanager')) {
  225. $json['error'] = $this->language->get('error_permission');
  226. }
  227. // Make sure we have the correct directory
  228. if (isset($this->request->get['directory'])) {
  229. $directory = rtrim(DIR_IMAGE . 'catalog/' . str_replace(array('../', '..\\', '..'), '', $this->request->get['directory']), '/');
  230. } else {
  231. $directory = DIR_IMAGE . 'catalog';
  232. }
  233. // Check its a directory
  234. if (!is_dir($directory)) {
  235. $json['error'] = $this->language->get('error_directory');
  236. }
  237. if (!$json) {
  238. // Sanitize the folder name
  239. $folder = str_replace(array('../', '..\\', '..'), '', basename(html_entity_decode($this->request->post['folder'], ENT_QUOTES, 'UTF-8')));
  240. // Validate the filename length
  241. if ((utf8_strlen($folder) < 3) || (utf8_strlen($folder) > 128)) {
  242. $json['error'] = $this->language->get('error_folder');
  243. }
  244. // Check if directory already exists or not
  245. if (is_dir($directory . '/' . $folder)) {
  246. $json['error'] = $this->language->get('error_exists');
  247. }
  248. }
  249. if (!$json) {
  250. mkdir($directory . '/' . $folder, 0777);
  251. chmod($directory . '/' . $folder, 0777);
  252. $json['success'] = $this->language->get('text_directory');
  253. }
  254. $this->response->addHeader('Content-Type: application/json');
  255. $this->response->setOutput(json_encode($json));
  256. }
  257. public function delete() {
  258. $this->load->language('common/filemanager');
  259. $json = array();
  260. // Check user has permission
  261. if (!$this->user->hasPermission('modify', 'common/filemanager')) {
  262. $json['error'] = $this->language->get('error_permission');
  263. }
  264. if (isset($this->request->post['path'])) {
  265. $paths = $this->request->post['path'];
  266. } else {
  267. $paths = array();
  268. }
  269. // Loop through each path to run validations
  270. foreach ($paths as $path) {
  271. $path = rtrim(DIR_IMAGE . str_replace(array('../', '..\\', '..'), '', $path), '/');
  272. // Check path exsists
  273. if ($path == DIR_IMAGE . 'catalog') {
  274. $json['error'] = $this->language->get('error_delete');
  275. break;
  276. }
  277. }
  278. if (!$json) {
  279. // Loop through each path
  280. foreach ($paths as $path) {
  281. $path = rtrim(DIR_IMAGE . str_replace(array('../', '..\\', '..'), '', $path), '/');
  282. // If path is just a file delete it
  283. if (is_file($path)) {
  284. unlink($path);
  285. // If path is a directory beging deleting each file and sub folder
  286. } elseif (is_dir($path)) {
  287. $files = array();
  288. // Make path into an array
  289. $path = array($path . '*');
  290. // While the path array is still populated keep looping through
  291. while (count($path) != 0) {
  292. $next = array_shift($path);
  293. foreach (glob($next) as $file) {
  294. // If directory add to path array
  295. if (is_dir($file)) {
  296. $path[] = $file . '/*';
  297. }
  298. // Add the file to the files to be deleted array
  299. $files[] = $file;
  300. }
  301. }
  302. // Reverse sort the file array
  303. rsort($files);
  304. foreach ($files as $file) {
  305. // If file just delete
  306. if (is_file($file)) {
  307. unlink($file);
  308. // If directory use the remove directory function
  309. } elseif (is_dir($file)) {
  310. rmdir($file);
  311. }
  312. }
  313. }
  314. }
  315. $json['success'] = $this->language->get('text_delete');
  316. }
  317. $this->response->addHeader('Content-Type: application/json');
  318. $this->response->setOutput(json_encode($json));
  319. }
  320. }