PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/controller/common/filemanager.php

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