PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/controller/common/filemanager.php

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