PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/smart-service.com.ua/www/admin/controller/common/watermanager.php

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