Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.64 KB | None | 0 0
  1. <?php
  2. /*
  3. * 2007-2016 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2016 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26.  
  27. class ContactControllerCore extends FrontController
  28. {
  29. public $php_self = 'contact';
  30. public $ssl = true;
  31.  
  32. /**
  33. * Start forms process
  34. * @see FrontController::postProcess()
  35. */
  36. public function postProcess()
  37. {
  38. if (Tools::isSubmit('submitMessage')) {
  39. $saveContactKey = $this->context->cookie->contactFormKey;
  40. $extension = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.zip', '.png', '.jpeg', '.gif', '.jpg');
  41. $file_attachment = Tools::fileAttachment('fileUpload');
  42. $message = Tools::getValue('message'); // Html entities is not usefull, iscleanHtml check there is no bad html tags.
  43. $url = Tools::getValue('url');
  44. if (!($from = trim(Tools::getValue('from'))) || !Validate::isEmail($from)) {
  45. $this->errors[] = Tools::displayError('Invalid email address.');
  46. } elseif (!$message) {
  47. $this->errors[] = Tools::displayError('The message cannot be blank.');
  48. } elseif (!Validate::isCleanHtml($message)) {
  49. $this->errors[] = Tools::displayError('Invalid message');
  50. } elseif (!($id_contact = (int)Tools::getValue('id_contact')) || !(Validate::isLoadedObject($contact = new Contact($id_contact, $this->context->language->id)))) {
  51. $this->errors[] = Tools::displayError('Please select a subject from the list provided. ');
  52. } elseif (!empty($file_attachment['name']) && $file_attachment['error'] != 0) {
  53. $this->errors[] = Tools::displayError('An error occurred during the file-upload process.');
  54. } elseif (!empty($file_attachment['name']) && !in_array(Tools::strtolower(substr($file_attachment['name'], -4)), $extension) && !in_array(Tools::strtolower(substr($file_attachment['name'], -5)), $extension)) {
  55. $this->errors[] = Tools::displayError('Bad file extension');
  56. } elseif ($url === false || !empty($url) || $saveContactKey != (Tools::getValue('contactKey'))) {
  57. $this->errors[] = Tools::displayError('An error occurred while sending the message.');
  58. } else {
  59. $customer = $this->context->customer;
  60. if (!$customer->id) {
  61. $customer->getByEmail($from);
  62. }
  63.  
  64. $id_order = (int)$this->getOrder();
  65.  
  66. if (!((
  67. ($id_customer_thread = (int)Tools::getValue('id_customer_thread'))
  68. && (int)Db::getInstance()->getValue('
  69. SELECT cm.id_customer_thread FROM '._DB_PREFIX_.'customer_thread cm
  70. WHERE cm.id_customer_thread = '.(int)$id_customer_thread.' AND cm.id_shop = '.(int)$this->context->shop->id.' AND token = \''.pSQL(Tools::getValue('token')).'\'')
  71. ) || (
  72. $id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($from, $id_order)
  73. ))) {
  74. $fields = Db::getInstance()->executeS('
  75. SELECT cm.id_customer_thread, cm.id_contact, cm.id_customer, cm.id_order, cm.id_product, cm.email
  76. FROM '._DB_PREFIX_.'customer_thread cm
  77. WHERE email = \''.pSQL($from).'\' AND cm.id_shop = '.(int)$this->context->shop->id.' AND ('.
  78. ($customer->id ? 'id_customer = '.(int)$customer->id.' OR ' : '').'
  79. id_order = '.(int)$id_order.')');
  80. $score = 0;
  81. foreach ($fields as $key => $row) {
  82. $tmp = 0;
  83. if ((int)$row['id_customer'] && $row['id_customer'] != $customer->id && $row['email'] != $from) {
  84. continue;
  85. }
  86. if ($row['id_order'] != 0 && $id_order != $row['id_order']) {
  87. continue;
  88. }
  89. if ($row['email'] == $from) {
  90. $tmp += 4;
  91. }
  92. if ($row['id_contact'] == $id_contact) {
  93. $tmp++;
  94. }
  95. if (Tools::getValue('id_product') != 0 && $row['id_product'] == Tools::getValue('id_product')) {
  96. $tmp += 2;
  97. }
  98. if ($tmp >= 5 && $tmp >= $score) {
  99. $score = $tmp;
  100. $id_customer_thread = $row['id_customer_thread'];
  101. }
  102. }
  103. }
  104. $old_message = Db::getInstance()->getValue('
  105. SELECT cm.message FROM '._DB_PREFIX_.'customer_message cm
  106. LEFT JOIN '._DB_PREFIX_.'customer_thread cc on (cm.id_customer_thread = cc.id_customer_thread)
  107. WHERE cc.id_customer_thread = '.(int)$id_customer_thread.' AND cc.id_shop = '.(int)$this->context->shop->id.'
  108. ORDER BY cm.date_add DESC');
  109. if ($old_message == $message) {
  110. $this->context->smarty->assign('alreadySent', 1);
  111. $contact->email = '';
  112. $contact->customer_service = 0;
  113. }
  114.  
  115. if ($contact->customer_service) {
  116. if ((int)$id_customer_thread) {
  117. $ct = new CustomerThread($id_customer_thread);
  118. $ct->status = 'open';
  119. $ct->id_lang = (int)$this->context->language->id;
  120. $ct->id_contact = (int)$id_contact;
  121. $ct->id_order = (int)$id_order;
  122. if ($id_product = (int)Tools::getValue('id_product')) {
  123. $ct->id_product = $id_product;
  124. }
  125. $ct->update();
  126. } else {
  127. $ct = new CustomerThread();
  128. if (isset($customer->id)) {
  129. $ct->id_customer = (int)$customer->id;
  130. }
  131. $ct->id_shop = (int)$this->context->shop->id;
  132. $ct->id_order = (int)$id_order;
  133. if ($id_product = (int)Tools::getValue('id_product')) {
  134. $ct->id_product = $id_product;
  135. }
  136. $ct->id_contact = (int)$id_contact;
  137. $ct->id_lang = (int)$this->context->language->id;
  138. $ct->email = $from;
  139. $ct->status = 'open';
  140. $ct->token = Tools::passwdGen(12);
  141. $ct->add();
  142. }
  143.  
  144. if ($ct->id) {
  145. $cm = new CustomerMessage();
  146. $cm->id_customer_thread = $ct->id;
  147. $cm->message = $message;
  148. if (isset($file_attachment['rename']) && !empty($file_attachment['rename']) && rename($file_attachment['tmp_name'], _PS_UPLOAD_DIR_.basename($file_attachment['rename']))) {
  149. $cm->file_name = $file_attachment['rename'];
  150. @chmod(_PS_UPLOAD_DIR_.basename($file_attachment['rename']), 0664);
  151. }
  152. $cm->ip_address = (int)ip2long(Tools::getRemoteAddr());
  153. $cm->user_agent = $_SERVER['HTTP_USER_AGENT'];
  154. if (!$cm->add()) {
  155. $this->errors[] = Tools::displayError('An error occurred while sending the message.');
  156. }
  157. } else {
  158. $this->errors[] = Tools::displayError('An error occurred while sending the message.');
  159. }
  160. }
  161.  
  162. if (!count($this->errors)) {
  163. $var_list = array(
  164. '{order_name}' => '-',
  165. '{attached_file}' => '-',
  166. '{message}' => Tools::nl2br(stripslashes($message)),
  167. '{email}' => $from,
  168. '{product_name}' => '',
  169. );
  170.  
  171. if (isset($file_attachment['name'])) {
  172. $var_list['{attached_file}'] = $file_attachment['name'];
  173. }
  174.  
  175. $id_product = (int)Tools::getValue('id_product');
  176.  
  177. if (isset($ct) && Validate::isLoadedObject($ct) && $ct->id_order) {
  178. $order = new Order((int)$ct->id_order);
  179. $var_list['{order_name}'] = $order->getUniqReference();
  180. $var_list['{id_order}'] = (int)$order->id;
  181. }
  182.  
  183. if ($id_product) {
  184. $product = new Product((int)$id_product);
  185. if (Validate::isLoadedObject($product) && isset($product->name[Context::getContext()->language->id])) {
  186. $var_list['{product_name}'] = $product->name[Context::getContext()->language->id];
  187. }
  188. }
  189.  
  190. if (empty($contact->email)) {
  191. Mail::Send($this->context->language->id, 'contact_form', ((isset($ct) && Validate::isLoadedObject($ct)) ? sprintf(Mail::l('Your message has been correctly sent #ct%1$s #tc%2$s'), $ct->id, $ct->token) : Mail::l('Your message has been correctly sent')), $var_list, $from, null, null, null, $file_attachment);
  192. } else {
  193. if (!Mail::Send($this->context->language->id, 'contact', Mail::l('Message from contact form').' [no_sync]',
  194. $var_list, $contact->email, $contact->name, null, null,
  195. $file_attachment, null, _PS_MAIL_DIR_, false, null, null, $from) ||
  196. !Mail::Send($this->context->language->id, 'contact_form', ((isset($ct) && Validate::isLoadedObject($ct)) ? sprintf(Mail::l('Your message has been correctly sent #ct%1$s #tc%2$s'), $ct->id, $ct->token) : Mail::l('Your message has been correctly sent')), $var_list, $from, null, null, null, $file_attachment, null, _PS_MAIL_DIR_, false, null, null, $contact->email)) {
  197. $this->errors[] = Tools::displayError('An error occurred while sending the message.');
  198. }
  199. }
  200. }
  201.  
  202. if (count($this->errors) > 1) {
  203. array_unique($this->errors);
  204. } elseif (!count($this->errors)) {
  205. $this->context->smarty->assign('confirmation', 1);
  206. }
  207. }
  208. }
  209. }
  210.  
  211. public function setMedia()
  212. {
  213. parent::setMedia();
  214. $this->addCSS(_THEME_CSS_DIR_.'contact-form.css');
  215. $this->addJS(_THEME_JS_DIR_.'contact-form.js');
  216. $this->addJS(_PS_JS_DIR_.'validate.js');
  217. }
  218.  
  219. /**
  220. * Assign template vars related to page content
  221. * @see FrontController::initContent()
  222. */
  223. public function initContent()
  224. {
  225. parent::initContent();
  226.  
  227. $this->assignOrderList();
  228.  
  229. $email = Tools::safeOutput(Tools::getValue('from',
  230. ((isset($this->context->cookie) && isset($this->context->cookie->email) && Validate::isEmail($this->context->cookie->email)) ? $this->context->cookie->email : '')));
  231. $this->context->smarty->assign(array(
  232. 'errors' => $this->errors,
  233. 'email' => $email,
  234. 'fileupload' => Configuration::get('PS_CUSTOMER_SERVICE_FILE_UPLOAD'),
  235. 'max_upload_size' => (int)Tools::getMaxUploadSize()
  236. ));
  237.  
  238. if (($id_customer_thread = (int)Tools::getValue('id_customer_thread')) && $token = Tools::getValue('token')) {
  239. $customer_thread = Db::getInstance()->getRow('
  240. SELECT cm.*
  241. FROM '._DB_PREFIX_.'customer_thread cm
  242. WHERE cm.id_customer_thread = '.(int)$id_customer_thread.'
  243. AND cm.id_shop = '.(int)$this->context->shop->id.'
  244. AND token = \''.pSQL($token).'\'
  245. ');
  246.  
  247. $order = new Order((int)$customer_thread['id_order']);
  248. if (Validate::isLoadedObject($order)) {
  249. $customer_thread['reference'] = $order->getUniqReference();
  250. }
  251. $this->context->smarty->assign('customerThread', $customer_thread);
  252. }
  253.  
  254. $contactKey = md5(uniqid(microtime(), true));
  255. $this->context->cookie->__set('contactFormKey', $contactKey);
  256.  
  257. $this->context->smarty->assign(array(
  258. 'contacts' => Contact::getContacts($this->context->language->id),
  259. 'message' => html_entity_decode(Tools::getValue('message')),
  260. 'contactKey' => $contactKey,
  261. ));
  262.  
  263. $this->setTemplate(_PS_THEME_DIR_.'contact-form.tpl');
  264. }
  265.  
  266. /**
  267. * Assign template vars related to order list and product list ordered by the customer
  268. */
  269. protected function assignOrderList()
  270. {
  271. if ($this->context->customer->isLogged()) {
  272. $this->context->smarty->assign('isLogged', 1);
  273.  
  274. $products = array();
  275. $result = Db::getInstance()->executeS('
  276. SELECT id_order
  277. FROM '._DB_PREFIX_.'orders
  278. WHERE id_customer = '.(int)$this->context->customer->id.Shop::addSqlRestriction(Shop::SHARE_ORDER).' ORDER BY date_add');
  279.  
  280. $orders = array();
  281.  
  282. foreach ($result as $row) {
  283. $order = new Order($row['id_order']);
  284. $date = explode(' ', $order->date_add);
  285. $tmp = $order->getProducts();
  286. foreach ($tmp as $key => $val) {
  287. $products[$row['id_order']][$val['product_id']] = array('value' => $val['product_id'], 'label' => $val['product_name']);
  288. }
  289.  
  290. $orders[] = array('value' => $order->id, 'label' => $order->getUniqReference().' - '.Tools::displayDate($date[0], null) , 'selected' => (int)$this->getOrder() == $order->id);
  291. }
  292.  
  293. $this->context->smarty->assign('orderList', $orders);
  294. $this->context->smarty->assign('orderedProductList', $products);
  295. }
  296. }
  297.  
  298. protected function getOrder()
  299. {
  300. $id_order = false;
  301. if (!is_numeric($reference = Tools::getValue('id_order'))) {
  302. $reference = ltrim($reference, '#');
  303. $orders = Order::getByReference($reference);
  304. if ($orders) {
  305. foreach ($orders as $order) {
  306. $id_order = (int)$order->id;
  307. break;
  308. }
  309. }
  310. } elseif (Order::getCartIdStatic((int)Tools::getValue('id_order'))) {
  311. $id_order = (int)Tools::getValue('id_order');
  312. }
  313. return (int)$id_order;
  314. }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement