PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/admin/controller/common/filemanager.php

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