PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

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