PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/admin/controller/common/filemanager.php

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