PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/controller/common/filemanager.php

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