PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/controller/common/filemanager.php

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