PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/controller/catalog/download.php

https://github.com/zahidiub/yahya
PHP | 559 lines | 413 code | 143 blank | 3 comment | 95 complexity | b8a923ff4930703d3f53eda0ade8c0ef MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class ControllerCatalogDownload extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->language->load('catalog/download');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('catalog/download');
  8. $this->getList();
  9. }
  10. public function insert() {
  11. $this->language->load('catalog/download');
  12. $this->document->setTitle($this->language->get('heading_title'));
  13. $this->load->model('catalog/download');
  14. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  15. $this->model_catalog_download->addDownload($this->request->post);
  16. $this->session->data['success'] = $this->language->get('text_success');
  17. $url = '';
  18. if (isset($this->request->get['sort'])) {
  19. $url .= '&sort=' . $this->request->get['sort'];
  20. }
  21. if (isset($this->request->get['order'])) {
  22. $url .= '&order=' . $this->request->get['order'];
  23. }
  24. if (isset($this->request->get['page'])) {
  25. $url .= '&page=' . $this->request->get['page'];
  26. }
  27. $this->redirect($this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  28. }
  29. $this->getForm();
  30. }
  31. public function update() {
  32. $this->language->load('catalog/download');
  33. $this->document->setTitle($this->language->get('heading_title'));
  34. $this->load->model('catalog/download');
  35. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  36. $this->model_catalog_download->editDownload($this->request->get['download_id'], $this->request->post);
  37. $this->session->data['success'] = $this->language->get('text_success');
  38. $url = '';
  39. if (isset($this->request->get['sort'])) {
  40. $url .= '&sort=' . $this->request->get['sort'];
  41. }
  42. if (isset($this->request->get['order'])) {
  43. $url .= '&order=' . $this->request->get['order'];
  44. }
  45. if (isset($this->request->get['page'])) {
  46. $url .= '&page=' . $this->request->get['page'];
  47. }
  48. $this->redirect($this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->language->load('catalog/download');
  54. $this->document->setTitle($this->language->get('heading_title'));
  55. $this->load->model('catalog/download');
  56. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  57. foreach ($this->request->post['selected'] as $download_id) {
  58. $this->model_catalog_download->deleteDownload($download_id);
  59. }
  60. $this->session->data['success'] = $this->language->get('text_success');
  61. $url = '';
  62. if (isset($this->request->get['sort'])) {
  63. $url .= '&sort=' . $this->request->get['sort'];
  64. }
  65. if (isset($this->request->get['order'])) {
  66. $url .= '&order=' . $this->request->get['order'];
  67. }
  68. if (isset($this->request->get['page'])) {
  69. $url .= '&page=' . $this->request->get['page'];
  70. }
  71. $this->redirect($this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  72. }
  73. $this->getList();
  74. }
  75. protected function getList() {
  76. if (isset($this->request->get['sort'])) {
  77. $sort = $this->request->get['sort'];
  78. } else {
  79. $sort = 'dd.name';
  80. }
  81. if (isset($this->request->get['order'])) {
  82. $order = $this->request->get['order'];
  83. } else {
  84. $order = 'ASC';
  85. }
  86. if (isset($this->request->get['page'])) {
  87. $page = $this->request->get['page'];
  88. } else {
  89. $page = 1;
  90. }
  91. $url = '';
  92. if (isset($this->request->get['sort'])) {
  93. $url .= '&sort=' . $this->request->get['sort'];
  94. }
  95. if (isset($this->request->get['order'])) {
  96. $url .= '&order=' . $this->request->get['order'];
  97. }
  98. if (isset($this->request->get['page'])) {
  99. $url .= '&page=' . $this->request->get['page'];
  100. }
  101. $this->data['breadcrumbs'] = array();
  102. $this->data['breadcrumbs'][] = array(
  103. 'text' => $this->language->get('text_home'),
  104. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  105. 'separator' => false
  106. );
  107. $this->data['breadcrumbs'][] = array(
  108. 'text' => $this->language->get('heading_title'),
  109. 'href' => $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  110. 'separator' => ' :: '
  111. );
  112. $this->data['insert'] = $this->url->link('catalog/download/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  113. $this->data['delete'] = $this->url->link('catalog/download/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
  114. $this->data['downloads'] = array();
  115. $data = array(
  116. 'sort' => $sort,
  117. 'order' => $order,
  118. 'start' => ($page - 1) * $this->config->get('config_admin_limit'),
  119. 'limit' => $this->config->get('config_admin_limit')
  120. );
  121. $download_total = $this->model_catalog_download->getTotalDownloads();
  122. $results = $this->model_catalog_download->getDownloads($data);
  123. foreach ($results as $result) {
  124. $action = array();
  125. $action[] = array(
  126. 'text' => $this->language->get('text_edit'),
  127. 'href' => $this->url->link('catalog/download/update', 'token=' . $this->session->data['token'] . '&download_id=' . $result['download_id'] . $url, 'SSL')
  128. );
  129. $this->data['downloads'][] = array(
  130. 'download_id' => $result['download_id'],
  131. 'name' => $result['name'],
  132. 'remaining' => $result['remaining'],
  133. 'selected' => isset($this->request->post['selected']) && in_array($result['download_id'], $this->request->post['selected']),
  134. 'action' => $action
  135. );
  136. }
  137. $this->data['heading_title'] = $this->language->get('heading_title');
  138. $this->data['text_no_results'] = $this->language->get('text_no_results');
  139. $this->data['column_name'] = $this->language->get('column_name');
  140. $this->data['column_remaining'] = $this->language->get('column_remaining');
  141. $this->data['column_action'] = $this->language->get('column_action');
  142. $this->data['button_insert'] = $this->language->get('button_insert');
  143. $this->data['button_delete'] = $this->language->get('button_delete');
  144. if (isset($this->error['warning'])) {
  145. $this->data['error_warning'] = $this->error['warning'];
  146. } else {
  147. $this->data['error_warning'] = '';
  148. }
  149. if (isset($this->session->data['success'])) {
  150. $this->data['success'] = $this->session->data['success'];
  151. unset($this->session->data['success']);
  152. } else {
  153. $this->data['success'] = '';
  154. }
  155. $url = '';
  156. if ($order == 'ASC') {
  157. $url .= '&order=DESC';
  158. } else {
  159. $url .= '&order=ASC';
  160. }
  161. if (isset($this->request->get['page'])) {
  162. $url .= '&page=' . $this->request->get['page'];
  163. }
  164. $this->data['sort_name'] = $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . '&sort=dd.name' . $url, 'SSL');
  165. $this->data['sort_remaining'] = $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . '&sort=d.remaining' . $url, 'SSL');
  166. $url = '';
  167. if (isset($this->request->get['sort'])) {
  168. $url .= '&sort=' . $this->request->get['sort'];
  169. }
  170. if (isset($this->request->get['order'])) {
  171. $url .= '&order=' . $this->request->get['order'];
  172. }
  173. $pagination = new Pagination();
  174. $pagination->total = $download_total;
  175. $pagination->page = $page;
  176. $pagination->limit = $this->config->get('config_admin_limit');
  177. $pagination->text = $this->language->get('text_pagination');
  178. $pagination->url = $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
  179. $this->data['pagination'] = $pagination->render();
  180. $this->data['sort'] = $sort;
  181. $this->data['order'] = $order;
  182. $this->template = 'catalog/download_list.tpl';
  183. $this->children = array(
  184. 'common/header',
  185. 'common/footer'
  186. );
  187. $this->response->setOutput($this->render());
  188. }
  189. protected function getForm() {
  190. $this->data['heading_title'] = $this->language->get('heading_title');
  191. $this->data['entry_name'] = $this->language->get('entry_name');
  192. $this->data['entry_filename'] = $this->language->get('entry_filename');
  193. $this->data['entry_mask'] = $this->language->get('entry_mask');
  194. $this->data['entry_remaining'] = $this->language->get('entry_remaining');
  195. $this->data['entry_update'] = $this->language->get('entry_update');
  196. $this->data['button_save'] = $this->language->get('button_save');
  197. $this->data['button_cancel'] = $this->language->get('button_cancel');
  198. $this->data['button_upload'] = $this->language->get('button_upload');
  199. if (isset($this->error['warning'])) {
  200. $this->data['error_warning'] = $this->error['warning'];
  201. } else {
  202. $this->data['error_warning'] = '';
  203. }
  204. if (isset($this->error['name'])) {
  205. $this->data['error_name'] = $this->error['name'];
  206. } else {
  207. $this->data['error_name'] = array();
  208. }
  209. if (isset($this->error['filename'])) {
  210. $this->data['error_filename'] = $this->error['filename'];
  211. } else {
  212. $this->data['error_filename'] = '';
  213. }
  214. if (isset($this->error['mask'])) {
  215. $this->data['error_mask'] = $this->error['mask'];
  216. } else {
  217. $this->data['error_mask'] = '';
  218. }
  219. $url = '';
  220. if (isset($this->request->get['sort'])) {
  221. $url .= '&sort=' . $this->request->get['sort'];
  222. }
  223. if (isset($this->request->get['order'])) {
  224. $url .= '&order=' . $this->request->get['order'];
  225. }
  226. if (isset($this->request->get['page'])) {
  227. $url .= '&page=' . $this->request->get['page'];
  228. }
  229. $this->data['breadcrumbs'] = array();
  230. $this->data['breadcrumbs'][] = array(
  231. 'text' => $this->language->get('text_home'),
  232. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  233. 'separator' => false
  234. );
  235. $this->data['breadcrumbs'][] = array(
  236. 'text' => $this->language->get('heading_title'),
  237. 'href' => $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  238. 'separator' => ' :: '
  239. );
  240. if (!isset($this->request->get['download_id'])) {
  241. $this->data['action'] = $this->url->link('catalog/download/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  242. } else {
  243. $this->data['action'] = $this->url->link('catalog/download/update', 'token=' . $this->session->data['token'] . '&download_id=' . $this->request->get['download_id'] . $url, 'SSL');
  244. }
  245. $this->data['cancel'] = $this->url->link('catalog/download', 'token=' . $this->session->data['token'] . $url, 'SSL');
  246. $this->load->model('localisation/language');
  247. $this->data['languages'] = $this->model_localisation_language->getLanguages();
  248. if (isset($this->request->get['download_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  249. $download_info = $this->model_catalog_download->getDownload($this->request->get['download_id']);
  250. }
  251. $this->data['token'] = $this->session->data['token'];
  252. if (isset($this->request->get['download_id'])) {
  253. $this->data['download_id'] = $this->request->get['download_id'];
  254. } else {
  255. $this->data['download_id'] = 0;
  256. }
  257. if (isset($this->request->post['download_description'])) {
  258. $this->data['download_description'] = $this->request->post['download_description'];
  259. } elseif (isset($this->request->get['download_id'])) {
  260. $this->data['download_description'] = $this->model_catalog_download->getDownloadDescriptions($this->request->get['download_id']);
  261. } else {
  262. $this->data['download_description'] = array();
  263. }
  264. if (isset($this->request->post['filename'])) {
  265. $this->data['filename'] = $this->request->post['filename'];
  266. } elseif (!empty($download_info)) {
  267. $this->data['filename'] = $download_info['filename'];
  268. } else {
  269. $this->data['filename'] = '';
  270. }
  271. if (isset($this->request->post['mask'])) {
  272. $this->data['mask'] = $this->request->post['mask'];
  273. } elseif (!empty($download_info)) {
  274. $this->data['mask'] = $download_info['mask'];
  275. } else {
  276. $this->data['mask'] = '';
  277. }
  278. if (isset($this->request->post['remaining'])) {
  279. $this->data['remaining'] = $this->request->post['remaining'];
  280. } elseif (!empty($download_info)) {
  281. $this->data['remaining'] = $download_info['remaining'];
  282. } else {
  283. $this->data['remaining'] = 1;
  284. }
  285. if (isset($this->request->post['update'])) {
  286. $this->data['update'] = $this->request->post['update'];
  287. } else {
  288. $this->data['update'] = false;
  289. }
  290. $this->template = 'catalog/download_form.tpl';
  291. $this->children = array(
  292. 'common/header',
  293. 'common/footer'
  294. );
  295. $this->response->setOutput($this->render());
  296. }
  297. protected function validateForm() {
  298. if (!$this->user->hasPermission('modify', 'catalog/download')) {
  299. $this->error['warning'] = $this->language->get('error_permission');
  300. }
  301. foreach ($this->request->post['download_description'] as $language_id => $value) {
  302. if ((utf8_strlen($value['name']) < 3) || (utf8_strlen($value['name']) > 64)) {
  303. $this->error['name'][$language_id] = $this->language->get('error_name');
  304. }
  305. }
  306. if ((utf8_strlen($this->request->post['filename']) < 3) || (utf8_strlen($this->request->post['filename']) > 128)) {
  307. $this->error['filename'] = $this->language->get('error_filename');
  308. }
  309. if (!file_exists(DIR_DOWNLOAD . $this->request->post['filename']) && !is_file(DIR_DOWNLOAD . $this->request->post['filename'])) {
  310. $this->error['filename'] = $this->language->get('error_exists');
  311. }
  312. if ((utf8_strlen($this->request->post['mask']) < 3) || (utf8_strlen($this->request->post['mask']) > 128)) {
  313. $this->error['mask'] = $this->language->get('error_mask');
  314. }
  315. if (!$this->error) {
  316. return true;
  317. } else {
  318. return false;
  319. }
  320. }
  321. protected function validateDelete() {
  322. if (!$this->user->hasPermission('modify', 'catalog/download')) {
  323. $this->error['warning'] = $this->language->get('error_permission');
  324. }
  325. $this->load->model('catalog/product');
  326. foreach ($this->request->post['selected'] as $download_id) {
  327. $product_total = $this->model_catalog_product->getTotalProductsByDownloadId($download_id);
  328. if ($product_total) {
  329. $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
  330. }
  331. }
  332. if (!$this->error) {
  333. return true;
  334. } else {
  335. return false;
  336. }
  337. }
  338. public function upload() {
  339. $this->language->load('sale/order');
  340. $json = array();
  341. if (!$this->user->hasPermission('modify', 'catalog/download')) {
  342. $json['error'] = $this->language->get('error_permission');
  343. }
  344. if (!isset($json['error'])) {
  345. if (!empty($this->request->files['file']['name'])) {
  346. $filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));
  347. if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 128)) {
  348. $json['error'] = $this->language->get('error_filename');
  349. }
  350. // Allowed file extension types
  351. $allowed = array();
  352. $filetypes = explode("\n", $this->config->get('config_file_extension_allowed'));
  353. foreach ($filetypes as $filetype) {
  354. $allowed[] = trim($filetype);
  355. }
  356. if (!in_array(substr(strrchr($filename, '.'), 1), $allowed)) {
  357. $json['error'] = $this->language->get('error_filetype');
  358. }
  359. // Allowed file mime types
  360. $allowed = array();
  361. $filetypes = explode("\n", $this->config->get('config_file_mime_allowed'));
  362. foreach ($filetypes as $filetype) {
  363. $allowed[] = trim($filetype);
  364. }
  365. if (!in_array($this->request->files['file']['type'], $allowed)) {
  366. $json['error'] = $this->language->get('error_filetype');
  367. }
  368. // Check to see if any PHP files are trying to be uploaded
  369. $content = file_get_contents($this->request->files['file']['tmp_name']);
  370. if (preg_match('/\<\?php/i', $content)) {
  371. $json['error'] = $this->language->get('error_filetype');
  372. }
  373. if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
  374. $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
  375. }
  376. } else {
  377. $json['error'] = $this->language->get('error_upload');
  378. }
  379. }
  380. if (!isset($json['error'])) {
  381. if (is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) {
  382. $ext = md5(mt_rand());
  383. $json['filename'] = $filename . '.' . $ext;
  384. $json['mask'] = $filename;
  385. move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $filename . '.' . $ext);
  386. }
  387. $json['success'] = $this->language->get('text_upload');
  388. }
  389. $this->response->setOutput(json_encode($json));
  390. }
  391. public function autocomplete() {
  392. $json = array();
  393. if (isset($this->request->get['filter_name'])) {
  394. $this->load->model('catalog/download');
  395. $data = array(
  396. 'filter_name' => $this->request->get['filter_name'],
  397. 'start' => 0,
  398. 'limit' => 20
  399. );
  400. $results = $this->model_catalog_download->getDownloads($data);
  401. foreach ($results as $result) {
  402. $json[] = array(
  403. 'download_id' => $result['download_id'],
  404. 'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
  405. );
  406. }
  407. }
  408. $sort_order = array();
  409. foreach ($json as $key => $value) {
  410. $sort_order[$key] = $value['name'];
  411. }
  412. array_multisort($sort_order, SORT_ASC, $json);
  413. $this->response->setOutput(json_encode($json));
  414. }
  415. }
  416. ?>