PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/catalog/controller/product/product.php

https://github.com/kstep/opencart
PHP | 701 lines | 534 code | 163 blank | 4 comment | 118 complexity | 0a071e2a048a12a51f8b25c90280b05b MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. class ControllerProductProduct extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->language->load('product/product');
  6. $this->data['breadcrumbs'] = array();
  7. $this->data['breadcrumbs'][] = array(
  8. 'text' => $this->language->get('text_home'),
  9. 'href' => $this->url->link('common/home'),
  10. 'separator' => false
  11. );
  12. $this->load->model('catalog/category');
  13. if (isset($this->request->get['path'])) {
  14. $path = '';
  15. $parts = explode('_', (string)$this->request->get['path']);
  16. $category_id = (int)array_pop($parts);
  17. foreach ($parts as $path_id) {
  18. if (!$path) {
  19. $path = $path_id;
  20. } else {
  21. $path .= '_' . $path_id;
  22. }
  23. $category_info = $this->model_catalog_category->getCategory($path_id);
  24. if ($category_info) {
  25. $this->data['breadcrumbs'][] = array(
  26. 'text' => $category_info['name'],
  27. 'href' => $this->url->link('product/category', 'path=' . $path),
  28. 'separator' => $this->language->get('text_separator')
  29. );
  30. }
  31. }
  32. // Set the last category breadcrumb
  33. $category_info = $this->model_catalog_category->getCategory($category_id);
  34. if ($category_info) {
  35. $url = '';
  36. if (isset($this->request->get['sort'])) {
  37. $url .= '&sort=' . $this->request->get['sort'];
  38. }
  39. if (isset($this->request->get['order'])) {
  40. $url .= '&order=' . $this->request->get['order'];
  41. }
  42. if (isset($this->request->get['page'])) {
  43. $url .= '&page=' . $this->request->get['page'];
  44. }
  45. if (isset($this->request->get['limit'])) {
  46. $url .= '&limit=' . $this->request->get['limit'];
  47. }
  48. $this->data['breadcrumbs'][] = array(
  49. 'text' => $category_info['name'],
  50. 'href' => $this->url->link('product/category', 'path=' . $this->request->get['path']),
  51. 'separator' => $this->language->get('text_separator')
  52. );
  53. }
  54. }
  55. $this->load->model('catalog/manufacturer');
  56. if (isset($this->request->get['manufacturer_id'])) {
  57. $this->data['breadcrumbs'][] = array(
  58. 'text' => $this->language->get('text_brand'),
  59. 'href' => $this->url->link('product/manufacturer'),
  60. 'separator' => $this->language->get('text_separator')
  61. );
  62. $url = '';
  63. if (isset($this->request->get['sort'])) {
  64. $url .= '&sort=' . $this->request->get['sort'];
  65. }
  66. if (isset($this->request->get['order'])) {
  67. $url .= '&order=' . $this->request->get['order'];
  68. }
  69. if (isset($this->request->get['page'])) {
  70. $url .= '&page=' . $this->request->get['page'];
  71. }
  72. if (isset($this->request->get['limit'])) {
  73. $url .= '&limit=' . $this->request->get['limit'];
  74. }
  75. $manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($this->request->get['manufacturer_id']);
  76. if ($manufacturer_info) {
  77. $this->data['breadcrumbs'][] = array(
  78. 'text' => $manufacturer_info['name'],
  79. 'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . $url),
  80. 'separator' => $this->language->get('text_separator')
  81. );
  82. }
  83. }
  84. if (isset($this->request->get['search']) || isset($this->request->get['tag'])) {
  85. $url = '';
  86. if (isset($this->request->get['search'])) {
  87. $url .= '&search=' . $this->request->get['search'];
  88. }
  89. if (isset($this->request->get['tag'])) {
  90. $url .= '&tag=' . $this->request->get['tag'];
  91. }
  92. if (isset($this->request->get['description'])) {
  93. $url .= '&description=' . $this->request->get['description'];
  94. }
  95. if (isset($this->request->get['category_id'])) {
  96. $url .= '&category_id=' . $this->request->get['category_id'];
  97. }
  98. if (isset($this->request->get['sub_category'])) {
  99. $url .= '&sub_category=' . $this->request->get['sub_category'];
  100. }
  101. if (isset($this->request->get['sort'])) {
  102. $url .= '&sort=' . $this->request->get['sort'];
  103. }
  104. if (isset($this->request->get['order'])) {
  105. $url .= '&order=' . $this->request->get['order'];
  106. }
  107. if (isset($this->request->get['page'])) {
  108. $url .= '&page=' . $this->request->get['page'];
  109. }
  110. if (isset($this->request->get['limit'])) {
  111. $url .= '&limit=' . $this->request->get['limit'];
  112. }
  113. $this->data['breadcrumbs'][] = array(
  114. 'text' => $this->language->get('text_search'),
  115. 'href' => $this->url->link('product/search', $url),
  116. 'separator' => $this->language->get('text_separator')
  117. );
  118. }
  119. if (isset($this->request->get['product_id'])) {
  120. $product_id = (int)$this->request->get['product_id'];
  121. } else {
  122. $product_id = 0;
  123. }
  124. $this->load->model('catalog/product');
  125. $product_info = $this->model_catalog_product->getProduct($product_id);
  126. if ($product_info) {
  127. $url = '';
  128. if (isset($this->request->get['path'])) {
  129. $url .= '&path=' . $this->request->get['path'];
  130. }
  131. if (isset($this->request->get['filter'])) {
  132. $url .= '&filter=' . $this->request->get['filter'];
  133. }
  134. if (isset($this->request->get['manufacturer_id'])) {
  135. $url .= '&manufacturer_id=' . $this->request->get['manufacturer_id'];
  136. }
  137. if (isset($this->request->get['search'])) {
  138. $url .= '&search=' . $this->request->get['search'];
  139. }
  140. if (isset($this->request->get['tag'])) {
  141. $url .= '&tag=' . $this->request->get['tag'];
  142. }
  143. if (isset($this->request->get['description'])) {
  144. $url .= '&description=' . $this->request->get['description'];
  145. }
  146. if (isset($this->request->get['category_id'])) {
  147. $url .= '&category_id=' . $this->request->get['category_id'];
  148. }
  149. if (isset($this->request->get['sub_category'])) {
  150. $url .= '&sub_category=' . $this->request->get['sub_category'];
  151. }
  152. if (isset($this->request->get['sort'])) {
  153. $url .= '&sort=' . $this->request->get['sort'];
  154. }
  155. if (isset($this->request->get['order'])) {
  156. $url .= '&order=' . $this->request->get['order'];
  157. }
  158. if (isset($this->request->get['page'])) {
  159. $url .= '&page=' . $this->request->get['page'];
  160. }
  161. if (isset($this->request->get['limit'])) {
  162. $url .= '&limit=' . $this->request->get['limit'];
  163. }
  164. $this->data['breadcrumbs'][] = array(
  165. 'text' => $product_info['name'],
  166. 'href' => $this->url->link('product/product', $url . '&product_id=' . $this->request->get['product_id']),
  167. 'separator' => $this->language->get('text_separator')
  168. );
  169. $this->document->setTitle($product_info['name']);
  170. $this->document->setDescription($product_info['meta_description']);
  171. $this->document->setKeywords($product_info['meta_keyword']);
  172. $this->document->addLink($this->url->link('product/product', 'product_id=' . $this->request->get['product_id']), 'canonical');
  173. $this->document->addScript('catalog/view/javascript/jquery/tabs.js');
  174. $this->document->addScript('catalog/view/javascript/jquery/colorbox/jquery.colorbox-min.js');
  175. $this->document->addStyle('catalog/view/javascript/jquery/colorbox/colorbox.css');
  176. $this->data['heading_title'] = $product_info['name'];
  177. $this->data['text_select'] = $this->language->get('text_select');
  178. $this->data['text_manufacturer'] = $this->language->get('text_manufacturer');
  179. $this->data['text_model'] = $this->language->get('text_model');
  180. $this->data['text_reward'] = $this->language->get('text_reward');
  181. $this->data['text_points'] = $this->language->get('text_points');
  182. $this->data['text_discount'] = $this->language->get('text_discount');
  183. $this->data['text_stock'] = $this->language->get('text_stock');
  184. $this->data['text_price'] = $this->language->get('text_price');
  185. $this->data['text_tax'] = $this->language->get('text_tax');
  186. $this->data['text_discount'] = $this->language->get('text_discount');
  187. $this->data['text_option'] = $this->language->get('text_option');
  188. $this->data['text_qty'] = $this->language->get('text_qty');
  189. $this->data['text_minimum'] = sprintf($this->language->get('text_minimum'), $product_info['minimum']);
  190. $this->data['text_or'] = $this->language->get('text_or');
  191. $this->data['text_write'] = $this->language->get('text_write');
  192. $this->data['text_note'] = $this->language->get('text_note');
  193. $this->data['text_share'] = $this->language->get('text_share');
  194. $this->data['text_wait'] = $this->language->get('text_wait');
  195. $this->data['text_tags'] = $this->language->get('text_tags');
  196. $this->data['entry_name'] = $this->language->get('entry_name');
  197. $this->data['entry_review'] = $this->language->get('entry_review');
  198. $this->data['entry_rating'] = $this->language->get('entry_rating');
  199. $this->data['entry_good'] = $this->language->get('entry_good');
  200. $this->data['entry_bad'] = $this->language->get('entry_bad');
  201. $this->data['entry_captcha'] = $this->language->get('entry_captcha');
  202. $this->data['button_cart'] = $this->language->get('button_cart');
  203. $this->data['button_wishlist'] = $this->language->get('button_wishlist');
  204. $this->data['button_compare'] = $this->language->get('button_compare');
  205. $this->data['button_upload'] = $this->language->get('button_upload');
  206. $this->data['button_continue'] = $this->language->get('button_continue');
  207. $this->load->model('catalog/review');
  208. $this->data['tab_description'] = $this->language->get('tab_description');
  209. $this->data['tab_attribute'] = $this->language->get('tab_attribute');
  210. $this->data['tab_review'] = sprintf($this->language->get('tab_review'), $product_info['reviews']);
  211. $this->data['tab_related'] = $this->language->get('tab_related');
  212. $this->data['product_id'] = $this->request->get['product_id'];
  213. $this->data['manufacturer'] = $product_info['manufacturer'];
  214. $this->data['manufacturers'] = $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $product_info['manufacturer_id']);
  215. $this->data['model'] = $product_info['model'];
  216. $this->data['reward'] = $product_info['reward'];
  217. $this->data['points'] = $product_info['points'];
  218. if ($product_info['quantity'] <= 0) {
  219. $this->data['stock'] = $product_info['stock_status'];
  220. } elseif ($this->config->get('config_stock_display')) {
  221. $this->data['stock'] = $product_info['quantity'];
  222. } else {
  223. $this->data['stock'] = $this->language->get('text_instock');
  224. }
  225. $this->load->model('tool/image');
  226. if ($product_info['image']) {
  227. $this->data['popup'] = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));
  228. } else {
  229. $this->data['popup'] = '';
  230. }
  231. if ($product_info['image']) {
  232. $this->data['thumb'] = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height'));
  233. } else {
  234. $this->data['thumb'] = '';
  235. }
  236. $this->data['images'] = array();
  237. $results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
  238. foreach ($results as $result) {
  239. $this->data['images'][] = array(
  240. 'popup' => $this->model_tool_image->resize($result['image'], $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height')),
  241. 'thumb' => $this->model_tool_image->resize($result['image'], $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
  242. );
  243. }
  244. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  245. $this->data['price'] = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
  246. } else {
  247. $this->data['price'] = false;
  248. }
  249. if ((float)$product_info['special']) {
  250. $this->data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
  251. } else {
  252. $this->data['special'] = false;
  253. }
  254. if ($this->config->get('config_tax')) {
  255. $this->data['tax'] = $this->currency->format((float)$product_info['special'] ? $product_info['special'] : $product_info['price']);
  256. } else {
  257. $this->data['tax'] = false;
  258. }
  259. $discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
  260. $this->data['discounts'] = array();
  261. foreach ($discounts as $discount) {
  262. $this->data['discounts'][] = array(
  263. 'quantity' => $discount['quantity'],
  264. 'price' => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')))
  265. );
  266. }
  267. $this->data['options'] = array();
  268. foreach ($this->model_catalog_product->getProductOptions($this->request->get['product_id']) as $option) {
  269. $product_option_value_data = array();
  270. foreach ($option['product_option_value'] as $option_value) {
  271. if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
  272. if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
  273. $price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax') ? 'P' : false));
  274. } else {
  275. $price = false;
  276. }
  277. $product_option_value_data[] = array(
  278. 'product_option_value_id' => $option_value['product_option_value_id'],
  279. 'option_value_id' => $option_value['option_value_id'],
  280. 'name' => $option_value['name'],
  281. 'image' => $this->model_tool_image->resize($option_value['image'], 50, 50),
  282. 'price' => $price,
  283. 'price_prefix' => $option_value['price_prefix']
  284. );
  285. }
  286. }
  287. $this->data['options'][] = array(
  288. 'product_option_id' => $option['product_option_id'],
  289. 'product_option_value' => $product_option_value_data,
  290. 'option_id' => $option['option_id'],
  291. 'name' => $option['name'],
  292. 'type' => $option['type'],
  293. 'value' => $option['value'],
  294. 'required' => $option['required']
  295. );
  296. }
  297. if ($product_info['minimum']) {
  298. $this->data['minimum'] = $product_info['minimum'];
  299. } else {
  300. $this->data['minimum'] = 1;
  301. }
  302. $this->data['review_status'] = $this->config->get('config_review_status');
  303. $this->data['reviews'] = sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']);
  304. $this->data['rating'] = (int)$product_info['rating'];
  305. $this->data['description'] = html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8');
  306. $this->data['attribute_groups'] = $this->model_catalog_product->getProductAttributes($this->request->get['product_id']);
  307. $this->data['products'] = array();
  308. $results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);
  309. foreach ($results as $result) {
  310. if ($result['image']) {
  311. $image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
  312. } else {
  313. $image = false;
  314. }
  315. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  316. $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
  317. } else {
  318. $price = false;
  319. }
  320. if ((float)$result['special']) {
  321. $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
  322. } else {
  323. $special = false;
  324. }
  325. if ($this->config->get('config_review_status')) {
  326. $rating = (int)$result['rating'];
  327. } else {
  328. $rating = false;
  329. }
  330. $this->data['products'][] = array(
  331. 'product_id' => $result['product_id'],
  332. 'thumb' => $image,
  333. 'name' => $result['name'],
  334. 'price' => $price,
  335. 'special' => $special,
  336. 'rating' => $rating,
  337. 'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
  338. 'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'])
  339. );
  340. }
  341. $this->data['tags'] = array();
  342. if ($product_info['tag']) {
  343. $tags = explode(',', $product_info['tag']);
  344. foreach ($tags as $tag) {
  345. $this->data['tags'][] = array(
  346. 'tag' => trim($tag),
  347. 'href' => $this->url->link('product/search', 'tag=' . trim($tag))
  348. );
  349. }
  350. }
  351. $this->model_catalog_product->updateViewed($this->request->get['product_id']);
  352. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product.tpl')) {
  353. $this->template = $this->config->get('config_template') . '/template/product/product.tpl';
  354. } else {
  355. $this->template = 'default/template/product/product.tpl';
  356. }
  357. $this->children = array(
  358. 'common/column_left',
  359. 'common/column_right',
  360. 'common/content_top',
  361. 'common/content_bottom',
  362. 'common/footer',
  363. 'common/header'
  364. );
  365. $this->response->setOutput($this->render());
  366. } else {
  367. $url = '';
  368. if (isset($this->request->get['path'])) {
  369. $url .= '&path=' . $this->request->get['path'];
  370. }
  371. if (isset($this->request->get['filter'])) {
  372. $url .= '&filter=' . $this->request->get['filter'];
  373. }
  374. if (isset($this->request->get['manufacturer_id'])) {
  375. $url .= '&manufacturer_id=' . $this->request->get['manufacturer_id'];
  376. }
  377. if (isset($this->request->get['search'])) {
  378. $url .= '&search=' . $this->request->get['search'];
  379. }
  380. if (isset($this->request->get['tag'])) {
  381. $url .= '&tag=' . $this->request->get['tag'];
  382. }
  383. if (isset($this->request->get['description'])) {
  384. $url .= '&description=' . $this->request->get['description'];
  385. }
  386. if (isset($this->request->get['category_id'])) {
  387. $url .= '&category_id=' . $this->request->get['category_id'];
  388. }
  389. if (isset($this->request->get['sub_category'])) {
  390. $url .= '&sub_category=' . $this->request->get['sub_category'];
  391. }
  392. if (isset($this->request->get['sort'])) {
  393. $url .= '&sort=' . $this->request->get['sort'];
  394. }
  395. if (isset($this->request->get['order'])) {
  396. $url .= '&order=' . $this->request->get['order'];
  397. }
  398. if (isset($this->request->get['page'])) {
  399. $url .= '&page=' . $this->request->get['page'];
  400. }
  401. if (isset($this->request->get['limit'])) {
  402. $url .= '&limit=' . $this->request->get['limit'];
  403. }
  404. $this->data['breadcrumbs'][] = array(
  405. 'text' => $this->language->get('text_error'),
  406. 'href' => $this->url->link('product/product', $url . '&product_id=' . $product_id),
  407. 'separator' => $this->language->get('text_separator')
  408. );
  409. $this->document->setTitle($this->language->get('text_error'));
  410. $this->data['heading_title'] = $this->language->get('text_error');
  411. $this->data['text_error'] = $this->language->get('text_error');
  412. $this->data['button_continue'] = $this->language->get('button_continue');
  413. $this->data['continue'] = $this->url->link('common/home');
  414. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
  415. $this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
  416. } else {
  417. $this->template = 'default/template/error/not_found.tpl';
  418. }
  419. $this->children = array(
  420. 'common/column_left',
  421. 'common/column_right',
  422. 'common/content_top',
  423. 'common/content_bottom',
  424. 'common/footer',
  425. 'common/header'
  426. );
  427. $this->response->setOutput($this->render());
  428. }
  429. }
  430. public function review() {
  431. $this->language->load('product/product');
  432. $this->load->model('catalog/review');
  433. $this->data['text_on'] = $this->language->get('text_on');
  434. $this->data['text_no_reviews'] = $this->language->get('text_no_reviews');
  435. if (isset($this->request->get['page'])) {
  436. $page = $this->request->get['page'];
  437. } else {
  438. $page = 1;
  439. }
  440. $this->data['reviews'] = array();
  441. $review_total = $this->model_catalog_review->getTotalReviewsByProductId($this->request->get['product_id']);
  442. $results = $this->model_catalog_review->getReviewsByProductId($this->request->get['product_id'], ($page - 1) * 5, 5);
  443. foreach ($results as $result) {
  444. $this->data['reviews'][] = array(
  445. 'author' => $result['author'],
  446. 'text' => $result['text'],
  447. 'rating' => (int)$result['rating'],
  448. 'reviews' => sprintf($this->language->get('text_reviews'), (int)$review_total),
  449. 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
  450. );
  451. }
  452. $pagination = new Pagination();
  453. $pagination->total = $review_total;
  454. $pagination->page = $page;
  455. $pagination->limit = 5;
  456. $pagination->text = $this->language->get('text_pagination');
  457. $pagination->url = $this->url->link('product/product/review', 'product_id=' . $this->request->get['product_id'] . '&page={page}');
  458. $this->data['pagination'] = $pagination->render();
  459. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/review.tpl')) {
  460. $this->template = $this->config->get('config_template') . '/template/product/review.tpl';
  461. } else {
  462. $this->template = 'default/template/product/review.tpl';
  463. }
  464. $this->response->setOutput($this->render());
  465. }
  466. public function write() {
  467. $this->language->load('product/product');
  468. $this->load->model('catalog/review');
  469. $json = array();
  470. if ($this->request->server['REQUEST_METHOD'] == 'POST') {
  471. if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 25)) {
  472. $json['error'] = $this->language->get('error_name');
  473. }
  474. if ((utf8_strlen($this->request->post['text']) < 25) || (utf8_strlen($this->request->post['text']) > 1000)) {
  475. $json['error'] = $this->language->get('error_text');
  476. }
  477. if (empty($this->request->post['rating'])) {
  478. $json['error'] = $this->language->get('error_rating');
  479. }
  480. if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
  481. $json['error'] = $this->language->get('error_captcha');
  482. }
  483. if (!isset($json['error'])) {
  484. $this->model_catalog_review->addReview($this->request->get['product_id'], $this->request->post);
  485. $json['success'] = $this->language->get('text_success');
  486. }
  487. }
  488. $this->response->setOutput(json_encode($json));
  489. }
  490. public function captcha() {
  491. $this->load->library('captcha');
  492. $captcha = new Captcha();
  493. $this->session->data['captcha'] = $captcha->getCode();
  494. $captcha->showImage();
  495. }
  496. public function upload() {
  497. $this->language->load('product/product');
  498. $json = array();
  499. if (!empty($this->request->files['file']['name'])) {
  500. $filename = basename(preg_replace('/[^a-zA-Z0-9\.\-\s+]/', '', html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8')));
  501. if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 64)) {
  502. $json['error'] = $this->language->get('error_filename');
  503. }
  504. // Allowed file extension types
  505. $allowed = array();
  506. $filetypes = explode("\n", $this->config->get('config_file_extension_allowed'));
  507. foreach ($filetypes as $filetype) {
  508. $allowed[] = trim($filetype);
  509. }
  510. if (!in_array(substr(strrchr($filename, '.'), 1), $allowed)) {
  511. $json['error'] = $this->language->get('error_filetype');
  512. }
  513. // Allowed file mime types
  514. $allowed = array();
  515. $filetypes = explode("\n", $this->config->get('config_file_mime_allowed'));
  516. foreach ($filetypes as $filetype) {
  517. $allowed[] = trim($filetype);
  518. }
  519. if (!in_array($this->request->files['file']['type'], $allowed)) {
  520. $json['error'] = $this->language->get('error_filetype');
  521. }
  522. if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
  523. $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
  524. }
  525. } else {
  526. $json['error'] = $this->language->get('error_upload');
  527. }
  528. if (!$json && is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) {
  529. $file = basename($filename) . '.' . md5(mt_rand());
  530. // Hide the uploaded file name so people can not link to it directly.
  531. $json['file'] = $this->encryption->encrypt($file);
  532. move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $file);
  533. $json['success'] = $this->language->get('text_upload');
  534. }
  535. $this->response->setOutput(json_encode($json));
  536. }
  537. }
  538. ?>