diff --git a/classes/Tools.php b/classes/Tools.php index 558f13c52..573ac1d40 100644 --- a/classes/Tools.php +++ b/classes/Tools.php @@ -2430,7 +2430,7 @@ exit; return $pattern; return preg_replace('/\\\[px]\{[a-z]\}{1,2}|(\/[a-z]*)u([a-z]*)$/i', "$1$2", $pattern); } - + public static function addonsRequest($request, $params = array()) { $addons_url = 'api.addons.prestashop.com'; @@ -2516,6 +2516,21 @@ exit; // No content, return false return false; } + + public static function fileAttachment($input = 'fileUpload') + { + $fileAttachment = null; + if (isset($_FILES[$input]['name']) && !empty($_FILES[$input]['name']) && !empty($_FILES[$input]['tmp_name'])) + { + $fileAttachment['rename'] = uniqid(). self::strtolower(substr($_FILES[$input]['name'], -5)); + $fileAttachment['content'] = file_get_contents($_FILES[$input]['tmp_name']); + $fileAttachment['tmp_name'] = $_FILES[$input]['tmp_name']; + $fileAttachment['name'] = $_FILES[$input]['name']; + $fileAttachment['mime'] = $_FILES[$input]['type']; + $fileAttachment['error'] = $_FILES[$input]['error']; + } + return $fileAttachment; + } } /** diff --git a/controllers/front/ContactController.php b/controllers/front/ContactController.php index d5a136ae3..01941bf4d 100644 --- a/controllers/front/ContactController.php +++ b/controllers/front/ContactController.php @@ -37,15 +37,8 @@ class ContactControllerCore extends FrontController { if (Tools::isSubmit('submitMessage')) { - $fileAttachment = null; - if (isset($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['tmp_name'])) - { - $extension = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.zip', '.png', '.jpeg', '.gif', '.jpg'); - $filename = uniqid().substr($_FILES['fileUpload']['name'], -5); - $fileAttachment['content'] = file_get_contents($_FILES['fileUpload']['tmp_name']); - $fileAttachment['name'] = $_FILES['fileUpload']['name']; - $fileAttachment['mime'] = $_FILES['fileUpload']['type']; - } + $extension = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.zip', '.png', '.jpeg', '.gif', '.jpg'); + $fileAttachment = Tools::fileAttachment('fileUpload'); $message = Tools::getValue('message'); // Html entities is not usefull, iscleanHtml check there is no bad html tags. if (!($from = trim(Tools::getValue('from'))) || !Validate::isEmail($from)) $this->errors[] = Tools::displayError('Invalid email address.'); @@ -55,9 +48,9 @@ class ContactControllerCore extends FrontController $this->errors[] = Tools::displayError('Invalid message'); else if (!($id_contact = (int)(Tools::getValue('id_contact'))) || !(Validate::isLoadedObject($contact = new Contact($id_contact, $this->context->language->id)))) $this->errors[] = Tools::displayError('Please select a subject from the list provided. '); - else if (!empty($_FILES['fileUpload']['name']) && $_FILES['fileUpload']['error'] != 0) + else if (!empty($fileAttachment['name']) && $fileAttachment['error'] != 0) $this->errors[] = Tools::displayError('An error occurred during the file-upload process.'); - else if (!empty($_FILES['fileUpload']['name']) && !in_array(substr(Tools::strtolower($_FILES['fileUpload']['name']), -4), $extension) && !in_array(substr(Tools::strtolower($_FILES['fileUpload']['name']), -5), $extension)) + else if (!empty($fileAttachment['name']) && !in_array( Tools::strtolower(substr($fileAttachment['name'], -4)), $extension) && !in_array( Tools::strtolower(substr($fileAttachment['name'], -5)), $extension)) $this->errors[] = Tools::displayError('Bad file extension'); else { @@ -152,8 +145,8 @@ class ContactControllerCore extends FrontController $cm = new CustomerMessage(); $cm->id_customer_thread = $ct->id; $cm->message = Tools::htmlentitiesUTF8($message); - if (isset($filename) && rename($_FILES['fileUpload']['tmp_name'], _PS_MODULE_DIR_.'../upload/'.$filename)) - $cm->file_name = $filename; + if (isset($fileAttachment['rename']) && !empty($fileAttachment['rename']) && rename($fileAttachment['tmp_name'], _PS_MODULE_DIR_.'../upload/'.basename($fileAttachment['rename']))) + $cm->file_name = $fileAttachment['rename']; $cm->ip_address = ip2long($_SERVER['REMOTE_ADDR']); $cm->user_agent = $_SERVER['HTTP_USER_AGENT']; if (!$cm->add()) @@ -173,8 +166,8 @@ class ContactControllerCore extends FrontController '{product_name}' => '', ); - if (isset($filename)) - $var_list['{attached_file}'] = $_FILES['fileUpload']['name']; + if (isset($fileAttachment['name'])) + $var_list['{attached_file}'] = $fileAttachment['name']; $id_order = (int)Tools::getValue('id_order'); @@ -197,10 +190,6 @@ class ContactControllerCore extends FrontController $var_list['{product_name}'] = $product->name[Context::getContext()->language->id]; } - - - - if (empty($contact->email)) 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, $fileAttachment); else @@ -296,5 +285,4 @@ class ContactControllerCore extends FrontController $this->context->smarty->assign('orderedProductList', $products); } } -} - +} \ No newline at end of file diff --git a/themes/default/contact-form.tpl b/themes/default/contact-form.tpl index dcd5d252f..bc6bef2f4 100644 --- a/themes/default/contact-form.tpl +++ b/themes/default/contact-form.tpl @@ -89,8 +89,8 @@ {/foreach} {elseif !isset($customerThread.id_order) && !isset($isLogged)} - - {elseif $customerThread.id_order > 0} + + {elseif $customerThread.id_order|intval > 0} {/if}