PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

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