Ejemplo n.º 1
0
    /**
     * Start forms process
     * @see FrontController::postProcess()
     */
    public function postProcess()
    {
        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'];
            }
            $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 e-mail address');
            } else {
                if (!$message) {
                    $this->errors[] = Tools::displayError('Message cannot be blank');
                } else {
                    if (!Validate::isCleanHtml($message)) {
                        $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.');
                        } else {
                            if (!empty($_FILES['fileUpload']['name']) && $_FILES['fileUpload']['error'] != 0) {
                                $this->errors[] = Tools::displayError('An error occurred during the file upload');
                            } else {
                                if (!empty($_FILES['fileUpload']['name']) && !in_array(substr($_FILES['fileUpload']['name'], -4), $extension) && !in_array(substr($_FILES['fileUpload']['name'], -5), $extension)) {
                                    $this->errors[] = Tools::displayError('Bad file extension');
                                } else {
                                    $customer = $this->context->customer;
                                    if (!$customer->id) {
                                        $customer->getByEmail($from);
                                    }
                                    $contact = new Contact($id_contact, $this->context->language->id);
                                    if (!(($id_customer_thread = (int) Tools::getValue('id_customer_thread')) && (int) Db::getInstance()->getValue('
						SELECT cm.id_customer_thread FROM ' . _DB_PREFIX_ . 'customer_thread cm
						WHERE cm.id_customer_thread = ' . (int) $id_customer_thread . ' AND cm.id_shop = ' . (int) $this->context->shop->id . ' AND token = \'' . pSQL(Tools::getValue('token')) . '\'') || ($id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($from, (int) Tools::getValue('id_order'))))) {
                                        $fields = Db::getInstance()->executeS('
					SELECT cm.id_customer_thread, cm.id_contact, cm.id_customer, cm.id_order, cm.id_product, cm.email
					FROM ' . _DB_PREFIX_ . 'customer_thread cm
					WHERE email = \'' . pSQL($from) . '\' AND cm.id_shop = ' . (int) $this->context->shop->id . ' AND (' . ($customer->id ? 'id_customer = ' . (int) $customer->id . ' OR ' : '') . '
						id_order = ' . (int) Tools::getValue('id_order') . ')');
                                        $score = 0;
                                        foreach ($fields as $key => $row) {
                                            $tmp = 0;
                                            if ((int) $row['id_customer'] && $row['id_customer'] != $customer->id && $row['email'] != $from) {
                                                continue;
                                            }
                                            if ($row['id_order'] != 0 && Tools::getValue('id_order') != $row['id_order']) {
                                                continue;
                                            }
                                            if ($row['email'] == $from) {
                                                $tmp += 4;
                                            }
                                            if ($row['id_contact'] == $id_contact) {
                                                $tmp++;
                                            }
                                            if (Tools::getValue('id_product') != 0 && $row['id_product'] == Tools::getValue('id_product')) {
                                                $tmp += 2;
                                            }
                                            if ($tmp >= 5 && $tmp >= $score) {
                                                $score = $tmp;
                                                $id_customer_thread = $row['id_customer_thread'];
                                            }
                                        }
                                    }
                                    $old_message = Db::getInstance()->getValue('
					SELECT cm.message FROM ' . _DB_PREFIX_ . 'customer_message cm
					LEFT JOIN ' . _DB_PREFIX_ . 'customer_thread cc on (cm.id_customer_thread = cc.id_customer_thread)
					WHERE cc.id_customer_thread = ' . (int) $id_customer_thread . ' AND cc.id_shop = ' . (int) $this->context->shop->id . '
					ORDER BY cm.date_add DESC');
                                    if ($old_message == $message) {
                                        $this->context->smarty->assign('alreadySent', 1);
                                        $contact->email = '';
                                        $contact->customer_service = 0;
                                    }
                                    if (!empty($contact->email)) {
                                        $id_order = (int) Tools::getValue('id_order', 0);
                                        $order = new Order($id_order);
                                        $mail_var_list = array('{email}' => $from, '{message}' => Tools::nl2br(stripslashes($message)), '{id_order}' => $id_order, '{order_name}' => $order->getUniqReference(), '{attached_file}' => isset($_FILES['fileUpload'], $_FILES['fileUpload']['name']) ? $_FILES['fileUpload']['name'] : '');
                                        if (Mail::Send($this->context->language->id, 'contact', Mail::l('Message from contact form'), $mail_var_list, $contact->email, $contact->name, $from, $customer->id ? $customer->firstname . ' ' . $customer->lastname : '', $fileAttachment) && Mail::Send($this->context->language->id, 'contact_form', Mail::l('Your message has been correctly sent'), $mail_var_list, $from)) {
                                            $this->context->smarty->assign('confirmation', 1);
                                        } else {
                                            $this->errors[] = Tools::displayError('An error occurred while sending message.');
                                        }
                                    }
                                    if ($contact->customer_service) {
                                        if ((int) $id_customer_thread) {
                                            $ct = new CustomerThread($id_customer_thread);
                                            $ct->status = 'open';
                                            $ct->id_lang = (int) $this->context->language->id;
                                            $ct->id_contact = (int) $id_contact;
                                            if ($id_order = (int) Tools::getValue('id_order')) {
                                                $ct->id_order = $id_order;
                                            }
                                            if ($id_product = (int) Tools::getValue('id_product')) {
                                                $ct->id_product = $id_product;
                                            }
                                            $ct->update();
                                        } else {
                                            $ct = new CustomerThread();
                                            if (isset($customer->id)) {
                                                $ct->id_customer = (int) $customer->id;
                                            }
                                            $ct->id_shop = (int) $this->context->shop->id;
                                            if ($id_order = (int) Tools::getValue('id_order')) {
                                                $ct->id_order = $id_order;
                                            }
                                            if ($id_product = (int) Tools::getValue('id_product')) {
                                                $ct->id_product = $id_product;
                                            }
                                            $ct->id_contact = (int) $id_contact;
                                            $ct->id_lang = (int) $this->context->language->id;
                                            $ct->email = $from;
                                            $ct->status = 'open';
                                            $ct->token = Tools::passwdGen(12);
                                            $ct->add();
                                        }
                                        if ($ct->id) {
                                            $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;
                                            }
                                            $cm->ip_address = ip2long($_SERVER['REMOTE_ADDR']);
                                            $cm->user_agent = $_SERVER['HTTP_USER_AGENT'];
                                            if ($cm->add()) {
                                                if (empty($contact->email)) {
                                                    $var_list = array('{order_name}' => '-', '{attached_file}' => '-', '{message}' => stripslashes($message));
                                                    if ($ct->id_order) {
                                                        $order = new Order($ct->id_order);
                                                        $var_list['{order_name}'] = $order->reference;
                                                    }
                                                    if (isset($filename)) {
                                                        $var_list['{attached_file}'] = $_FILES['fileUpload']['name'];
                                                    }
                                                    Mail::Send($this->context->language->id, 'contact_form', Mail::l('Your message has been correctly sent'), $var_list, $from);
                                                }
                                                $this->context->smarty->assign('confirmation', 1);
                                            } else {
                                                $this->errors[] = Tools::displayError('An error occurred while sending message.');
                                            }
                                        } else {
                                            $this->errors[] = Tools::displayError('An error occurred while sending message.');
                                        }
                                    }
                                    if (count($this->errors) > 1) {
                                        array_unique($this->errors);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    public function postProcess()
    {
        if ($id_customer_thread = (int) Tools::getValue('id_customer_thread')) {
            if ($id_contact = (int) Tools::getValue('id_contact')) {
                Db::getInstance()->execute('
					UPDATE ' . _DB_PREFIX_ . 'customer_thread
					SET id_contact = ' . (int) $id_contact . '
					WHERE id_customer_thread = ' . (int) $id_customer_thread);
            }
            if ($id_status = (int) Tools::getValue('setstatus')) {
                $status_array = array(1 => 'open', 2 => 'closed', 3 => 'pending1', 4 => 'pending2');
                Db::getInstance()->execute('
					UPDATE ' . _DB_PREFIX_ . 'customer_thread
					SET status = "' . $status_array[$id_status] . '"
					WHERE id_customer_thread = ' . (int) $id_customer_thread . ' LIMIT 1
				');
            }
            if (isset($_POST['id_employee_forward'])) {
                $messages = Db::getInstance()->getRow('
					SELECT ct.*, cm.*, cl.name subject, CONCAT(e.firstname, \' \', e.lastname) employee_name,
						CONCAT(c.firstname, \' \', c.lastname) customer_name, c.firstname
					FROM ' . _DB_PREFIX_ . 'customer_thread ct
					LEFT JOIN ' . _DB_PREFIX_ . 'customer_message cm
						ON (ct.id_customer_thread = cm.id_customer_thread)
					LEFT JOIN ' . _DB_PREFIX_ . 'contact_lang cl
						ON (cl.id_contact = ct.id_contact AND cl.id_lang = ' . (int) $this->context->language->id . ')
					LEFT OUTER JOIN ' . _DB_PREFIX_ . 'employee e
						ON e.id_employee = cm.id_employee
					LEFT OUTER JOIN ' . _DB_PREFIX_ . 'customer c
						ON (c.email = ct.email)
					WHERE ct.id_customer_thread = ' . (int) Tools::getValue('id_customer_thread') . '
					ORDER BY cm.date_add DESC
				');
                $output = $this->displayMessage($messages, true, (int) Tools::getValue('id_employee_forward'));
                $cm = new CustomerMessage();
                $cm->id_employee = (int) $this->context->employee->id;
                $cm->id_customer_thread = (int) Tools::getValue('id_customer_thread');
                $cm->ip_address = (int) ip2long(Tools::getRemoteAddr());
                $current_employee = $this->context->employee;
                $id_employee = (int) Tools::getValue('id_employee_forward');
                $employee = new Employee($id_employee);
                $email = Tools::getValue('email');
                $message = Tools::getValue('message_forward');
                if (($error = $cm->validateField('message', $message, null, array(), true)) !== true) {
                    $this->errors[] = $error;
                } elseif ($id_employee && $employee && Validate::isLoadedObject($employee)) {
                    $params = array('{messages}' => stripslashes($output), '{employee}' => $current_employee->firstname . ' ' . $current_employee->lastname, '{comment}' => stripslashes(Tools::nl2br($_POST['message_forward'])), '{firstname}' => $employee->firstname, '{lastname}' => $employee->lastname);
                    if (Mail::Send($this->context->language->id, 'forward_msg', Mail::l('Fwd: Customer message', $this->context->language->id), $params, $employee->email, $employee->firstname . ' ' . $employee->lastname, $current_employee->email, $current_employee->firstname . ' ' . $current_employee->lastname, null, null, _PS_MAIL_DIR_, true)) {
                        $cm->private = 1;
                        $cm->message = $this->l('Message forwarded to') . ' ' . $employee->firstname . ' ' . $employee->lastname . "\n" . $this->l('Comment:') . ' ' . $message;
                        $cm->add();
                    }
                } elseif ($email && Validate::isEmail($email)) {
                    $params = array('{messages}' => Tools::nl2br(stripslashes($output)), '{employee}' => $current_employee->firstname . ' ' . $current_employee->lastname, '{comment}' => stripslashes($_POST['message_forward']));
                    if (Mail::Send($this->context->language->id, 'forward_msg', Mail::l('Fwd: Customer message', $this->context->language->id), $params, $email, null, $current_employee->email, $current_employee->firstname . ' ' . $current_employee->lastname, null, null, _PS_MAIL_DIR_, true)) {
                        $cm->message = $this->l('Message forwarded to') . ' ' . $email . "\n" . $this->l('Comment:') . ' ' . $message;
                        $cm->add();
                    }
                } else {
                    $this->errors[] = '<div class="alert error">' . Tools::displayError('The email address is invalid.') . '</div>';
                }
            }
            if (Tools::isSubmit('submitReply')) {
                $ct = new CustomerThread($id_customer_thread);
                ShopUrl::cacheMainDomainForShop((int) $ct->id_shop);
                $cm = new CustomerMessage();
                $cm->id_employee = (int) $this->context->employee->id;
                $cm->id_customer_thread = $ct->id;
                $cm->ip_address = (int) ip2long(Tools::getRemoteAddr());
                $cm->message = Tools::getValue('reply_message');
                if (($error = $cm->validateField('message', $cm->message, null, array(), true)) !== true) {
                    $this->errors[] = $error;
                } elseif (isset($_FILES) && !empty($_FILES['joinFile']['name']) && $_FILES['joinFile']['error'] != 0) {
                    $this->errors[] = Tools::displayError('An error occurred during the file upload process.');
                } elseif ($cm->add()) {
                    $file_attachment = null;
                    if (!empty($_FILES['joinFile']['name'])) {
                        $file_attachment['content'] = file_get_contents($_FILES['joinFile']['tmp_name']);
                        $file_attachment['name'] = $_FILES['joinFile']['name'];
                        $file_attachment['mime'] = $_FILES['joinFile']['type'];
                    }
                    $customer = new Customer($ct->id_customer);
                    $params = array('{reply}' => Tools::nl2br(Tools::getValue('reply_message')), '{link}' => Tools::url($this->context->link->getPageLink('contact', true), 'id_customer_thread=' . (int) $ct->id . '&token=' . $ct->token), '{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname);
                    //#ct == id_customer_thread    #tc == token of thread   <== used in the synchronization imap
                    $contact = new Contact((int) $ct->id_contact, (int) $ct->id_lang);
                    if (Validate::isLoadedObject($contact)) {
                        $from_name = $contact->name;
                        $from_email = $contact->email;
                    } else {
                        $from_name = null;
                        $from_email = null;
                    }
                    if (Mail::Send((int) $ct->id_lang, 'reply_msg', sprintf(Mail::l('An answer to your message is available #ct%1$s #tc%2$s', $ct->id_lang), $ct->id, $ct->token), $params, Tools::getValue('msg_email'), null, $from_email, $from_name, $file_attachment, null, _PS_MAIL_DIR_, true)) {
                        $ct->status = 'closed';
                        $ct->update();
                    }
                    Tools::redirectAdmin(self::$currentIndex . '&id_customer_thread=' . (int) $id_customer_thread . '&viewcustomer_thread&token=' . Tools::getValue('token'));
                } else {
                    $this->errors[] = Tools::displayError('An error occurred. Your message was not sent. Please contact your system administrator.');
                }
            }
        }
        return parent::postProcess();
    }
    public function preProcess()
    {
        parent::preProcess();
        if (self::$cookie->isLogged()) {
            self::$smarty->assign('isLogged', 1);
            $customer = new Customer((int) self::$cookie->id_customer);
            if (!Validate::isLoadedObject($customer)) {
                die(Tools::displayError('Customer not found'));
            }
            $products = array();
            $orders = array();
            $getOrders = Db::getInstance()->ExecuteS('
				SELECT id_order
				FROM ' . _DB_PREFIX_ . 'orders
				WHERE id_customer = ' . (int) $customer->id . ' ORDER BY date_add');
            foreach ($getOrders as $row) {
                $order = new Order($row['id_order']);
                $date = explode(' ', $order->date_add);
                $orders[$row['id_order']] = Tools::displayDate($date[0], self::$cookie->id_lang);
                $tmp = $order->getProducts();
                foreach ($tmp as $key => $val) {
                    $products[$val['product_id']] = $val['product_name'];
                }
            }
            $orderList = '';
            foreach ($orders as $key => $val) {
                $orderList .= '<option value="' . $key . '" ' . ((int) Tools::getValue('id_order') == $key ? 'selected' : '') . ' >' . $key . ' -- ' . $val . '</option>';
            }
            $orderedProductList = '';
            foreach ($products as $key => $val) {
                $orderedProductList .= '<option value="' . $key . '" ' . ((int) Tools::getValue('id_product') == $key ? 'selected' : '') . ' >' . $val . '</option>';
            }
            self::$smarty->assign('orderList', $orderList);
            self::$smarty->assign('orderedProductList', $orderedProductList);
        }
        if (Tools::isSubmit('submitMessage')) {
            $fileAttachment = NULL;
            if (isset($_FILES['fileUpload']['name']) and !empty($_FILES['fileUpload']['name']) and !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'];
            }
            $message = Tools::htmlentitiesUTF8(Tools::getValue('message'));
            if (!($from = trim(Tools::getValue('from'))) or !Validate::isEmail($from)) {
                $this->errors[] = Tools::displayError('Invalid e-mail address');
            } elseif (!($message = nl2br2($message))) {
                $this->errors[] = Tools::displayError('Message cannot be blank');
            } elseif (!Validate::isCleanHtml($message)) {
                $this->errors[] = Tools::displayError('Invalid message');
            } elseif (!($id_contact = (int) Tools::getValue('id_contact')) or !Validate::isLoadedObject($contact = new Contact((int) $id_contact, (int) self::$cookie->id_lang))) {
                $this->errors[] = Tools::displayError('Please select a subject on the list.');
            } elseif (!empty($_FILES['fileUpload']['name']) and $_FILES['fileUpload']['error'] != 0) {
                $this->errors[] = Tools::displayError('An error occurred during the file upload');
            } elseif (!empty($_FILES['fileUpload']['name']) and !in_array(substr($_FILES['fileUpload']['name'], -4), $extension) and !in_array(substr($_FILES['fileUpload']['name'], -5), $extension)) {
                $this->errors[] = Tools::displayError('Bad file extension');
            } else {
                if ((int) self::$cookie->id_customer) {
                    $customer = new Customer((int) self::$cookie->id_customer);
                } else {
                    $customer = new Customer();
                    $customer->getByEmail($from);
                }
                $contact = new Contact($id_contact, self::$cookie->id_lang);
                if (!($id_customer_thread = (int) Tools::getValue('id_customer_thread') and (int) Db::getInstance()->getValue('
						SELECT cm.id_customer_thread FROM ' . _DB_PREFIX_ . 'customer_thread cm
						WHERE cm.id_customer_thread = ' . (int) $id_customer_thread . ' AND token = \'' . pSQL(Tools::getValue('token')) . '\'') or $id_customer_thread = (int) Db::getInstance()->getValue('
						SELECT cm.id_customer_thread FROM ' . _DB_PREFIX_ . 'customer_thread cm
						WHERE cm.email = \'' . pSQL($from) . '\' AND cm.id_order = ' . (int) Tools::getValue('id_order') . ''))) {
                    $fields = Db::getInstance()->ExecuteS('
					SELECT cm.id_customer_thread, cm.id_contact, cm.id_customer, cm.id_order, cm.id_product, cm.email
					FROM ' . _DB_PREFIX_ . 'customer_thread cm
					WHERE email = \'' . pSQL($from) . '\' AND (' . ($customer->id ? 'id_customer = ' . (int) $customer->id . ' OR ' : '') . '
						id_order = ' . (int) Tools::getValue('id_order') . ')');
                    $score = 0;
                    foreach ($fields as $key => $row) {
                        $tmp = 0;
                        if ((int) $row['id_customer'] and $row['id_customer'] != $customer->id and $row['email'] != $from) {
                            continue;
                        }
                        if ($row['id_order'] != 0 and Tools::getValue('id_order') != $row['id_order']) {
                            continue;
                        }
                        if ($row['email'] == $from) {
                            $tmp += 4;
                        }
                        if ($row['id_contact'] == $id_contact) {
                            $tmp++;
                        }
                        if (Tools::getValue('id_product') != 0 and $row['id_product'] == Tools::getValue('id_product')) {
                            $tmp += 2;
                        }
                        if ($tmp >= 5 and $tmp >= $score) {
                            $score = $tmp;
                            $id_customer_thread = $row['id_customer_thread'];
                        }
                    }
                }
                $old_message = Db::getInstance()->getValue('
					SELECT cm.message FROM ' . _DB_PREFIX_ . 'customer_message cm
					WHERE cm.id_customer_thread = ' . (int) $id_customer_thread . '
					ORDER BY date_add DESC');
                if ($old_message == htmlentities($message, ENT_COMPAT, 'UTF-8')) {
                    self::$smarty->assign('alreadySent', 1);
                    $contact->email = '';
                    $contact->customer_service = 0;
                }
                if (!empty($contact->email)) {
                    if (Mail::Send((int) self::$cookie->id_lang, 'contact', Mail::l('Message from contact form'), array('{email}' => $from, '{message}' => stripslashes($message)), $contact->email, $contact->name, $from, (int) self::$cookie->id_customer ? $customer->firstname . ' ' . $customer->lastname : '', $fileAttachment) and Mail::Send((int) self::$cookie->id_lang, 'contact_form', Mail::l('Your message has been correctly sent'), array('{message}' => stripslashes($message)), $from)) {
                        self::$smarty->assign('confirmation', 1);
                    } else {
                        $this->errors[] = Tools::displayError('An error occurred while sending message.');
                    }
                }
                if ($contact->customer_service) {
                    if ((int) $id_customer_thread) {
                        $ct = new CustomerThread($id_customer_thread);
                        $ct->status = 'open';
                        $ct->id_lang = (int) self::$cookie->id_lang;
                        $ct->id_contact = (int) $id_contact;
                        if ($id_order = (int) Tools::getValue('id_order')) {
                            $ct->id_order = $id_order;
                        }
                        if ($id_product = (int) Tools::getValue('id_product')) {
                            $ct->id_product = $id_product;
                        }
                        $ct->update();
                    } else {
                        $ct = new CustomerThread();
                        if (isset($customer->id)) {
                            $ct->id_customer = (int) $customer->id;
                        }
                        if ($id_order = (int) Tools::getValue('id_order')) {
                            $ct->id_order = $id_order;
                        }
                        if ($id_product = (int) Tools::getValue('id_product')) {
                            $ct->id_product = $id_product;
                        }
                        $ct->id_contact = (int) $id_contact;
                        $ct->id_lang = (int) self::$cookie->id_lang;
                        $ct->email = $from;
                        $ct->status = 'open';
                        $ct->token = Tools::passwdGen(12);
                        $ct->add();
                    }
                    if ($ct->id) {
                        $cm = new CustomerMessage();
                        $cm->id_customer_thread = $ct->id;
                        $cm->message = htmlentities($message, ENT_COMPAT, 'UTF-8');
                        if (isset($filename) and rename($_FILES['fileUpload']['tmp_name'], _PS_MODULE_DIR_ . '../upload/' . $filename)) {
                            $cm->file_name = $filename;
                        }
                        $cm->ip_address = ip2long($_SERVER['REMOTE_ADDR']);
                        $cm->user_agent = $_SERVER['HTTP_USER_AGENT'];
                        if ($cm->add()) {
                            if (empty($contact->email)) {
                                Mail::Send((int) self::$cookie->id_lang, 'contact_form', Mail::l('Your message has been correctly sent'), array('{message}' => stripslashes($message)), $from);
                            }
                            self::$smarty->assign('confirmation', 1);
                        } else {
                            $this->errors[] = Tools::displayError('An error occurred while sending message.');
                        }
                    } else {
                        $this->errors[] = Tools::displayError('An error occurred while sending message.');
                    }
                }
                if (count($this->errors) > 1) {
                    array_unique($this->errors);
                }
            }
        }
    }
Ejemplo n.º 4
0
    /**
     * Start forms process
     * @see FrontController::postProcess()
     */
    public function postProcess()
    {
        if (Tools::isSubmit('submitMessage')) {
            $extension = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.zip', '.png', '.jpeg', '.gif', '.jpg');
            $file_attachment = 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.');
            } elseif (!$message) {
                $this->errors[] = Tools::displayError('The message cannot be blank.');
            } elseif (!Validate::isCleanHtml($message)) {
                $this->errors[] = Tools::displayError('Invalid message');
            } elseif (!($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. ');
            } elseif (!empty($file_attachment['name']) && $file_attachment['error'] != 0) {
                $this->errors[] = Tools::displayError('An error occurred during the file-upload process.');
            } 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)) {
                $this->errors[] = Tools::displayError('Bad file extension');
            } else {
                $customer = $this->context->customer;
                if (!$customer->id) {
                    $customer->getByEmail($from);
                }
                $id_order = (int) $this->getOrder();
                if (!(($id_customer_thread = (int) Tools::getValue('id_customer_thread')) && (int) Db::getInstance()->getValue('
						SELECT cm.id_customer_thread FROM ' . _DB_PREFIX_ . 'customer_thread cm
						WHERE cm.id_customer_thread = ' . (int) $id_customer_thread . ' AND cm.id_shop = ' . (int) $this->context->shop->id . ' AND token = \'' . pSQL(Tools::getValue('token')) . '\'') || ($id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($from, $id_order)))) {
                    $fields = Db::getInstance()->executeS('
					SELECT cm.id_customer_thread, cm.id_contact, cm.id_customer, cm.id_order, cm.id_product, cm.email
					FROM ' . _DB_PREFIX_ . 'customer_thread cm
					WHERE email = \'' . pSQL($from) . '\' AND cm.id_shop = ' . (int) $this->context->shop->id . ' AND (' . ($customer->id ? 'id_customer = ' . (int) $customer->id . ' OR ' : '') . '
						id_order = ' . (int) $id_order . ')');
                    $score = 0;
                    foreach ($fields as $key => $row) {
                        $tmp = 0;
                        if ((int) $row['id_customer'] && $row['id_customer'] != $customer->id && $row['email'] != $from) {
                            continue;
                        }
                        if ($row['id_order'] != 0 && $id_order != $row['id_order']) {
                            continue;
                        }
                        if ($row['email'] == $from) {
                            $tmp += 4;
                        }
                        if ($row['id_contact'] == $id_contact) {
                            $tmp++;
                        }
                        if (Tools::getValue('id_product') != 0 && $row['id_product'] == Tools::getValue('id_product')) {
                            $tmp += 2;
                        }
                        if ($tmp >= 5 && $tmp >= $score) {
                            $score = $tmp;
                            $id_customer_thread = $row['id_customer_thread'];
                        }
                    }
                }
                $old_message = Db::getInstance()->getValue('
					SELECT cm.message FROM ' . _DB_PREFIX_ . 'customer_message cm
					LEFT JOIN ' . _DB_PREFIX_ . 'customer_thread cc on (cm.id_customer_thread = cc.id_customer_thread)
					WHERE cc.id_customer_thread = ' . (int) $id_customer_thread . ' AND cc.id_shop = ' . (int) $this->context->shop->id . '
					ORDER BY cm.date_add DESC');
                if ($old_message == $message) {
                    $this->context->smarty->assign('alreadySent', 1);
                    $contact->email = '';
                    $contact->customer_service = 0;
                }
                if ($contact->customer_service) {
                    if ((int) $id_customer_thread) {
                        $ct = new CustomerThread($id_customer_thread);
                        $ct->status = 'open';
                        $ct->id_lang = (int) $this->context->language->id;
                        $ct->id_contact = (int) $id_contact;
                        $ct->id_order = (int) $id_order;
                        if ($id_product = (int) Tools::getValue('id_product')) {
                            $ct->id_product = $id_product;
                        }
                        $ct->update();
                    } else {
                        $ct = new CustomerThread();
                        if (isset($customer->id)) {
                            $ct->id_customer = (int) $customer->id;
                        }
                        $ct->id_shop = (int) $this->context->shop->id;
                        $ct->id_order = (int) $id_order;
                        if ($id_product = (int) Tools::getValue('id_product')) {
                            $ct->id_product = $id_product;
                        }
                        $ct->id_contact = (int) $id_contact;
                        $ct->id_lang = (int) $this->context->language->id;
                        $ct->email = $from;
                        $ct->status = 'open';
                        $ct->token = Tools::passwdGen(12);
                        $ct->add();
                    }
                    if ($ct->id) {
                        $cm = new CustomerMessage();
                        $cm->id_customer_thread = $ct->id;
                        $cm->message = $message;
                        if (isset($file_attachment['rename']) && !empty($file_attachment['rename']) && rename($file_attachment['tmp_name'], _PS_UPLOAD_DIR_ . basename($file_attachment['rename']))) {
                            $cm->file_name = $file_attachment['rename'];
                            @chmod(_PS_UPLOAD_DIR_ . basename($file_attachment['rename']), 0664);
                        }
                        $cm->ip_address = (int) ip2long(Tools::getRemoteAddr());
                        $cm->user_agent = $_SERVER['HTTP_USER_AGENT'];
                        if (!$cm->add()) {
                            $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                        }
                    } else {
                        $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                    }
                }
                if (!count($this->errors)) {
                    $var_list = array('{order_name}' => '-', '{attached_file}' => '-', '{message}' => Tools::nl2br(stripslashes($message)), '{email}' => $from, '{product_name}' => '');
                    if (isset($file_attachment['name'])) {
                        $var_list['{attached_file}'] = $file_attachment['name'];
                    }
                    $id_product = (int) Tools::getValue('id_product');
                    if (isset($ct) && Validate::isLoadedObject($ct) && $ct->id_order) {
                        $order = new Order((int) $ct->id_order);
                        $var_list['{order_name}'] = $order->getUniqReference();
                        $var_list['{id_order}'] = (int) $order->id;
                    }
                    if ($id_product) {
                        $product = new Product((int) $id_product);
                        if (Validate::isLoadedObject($product) && isset($product->name[Context::getContext()->language->id])) {
                            $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, $file_attachment);
                    } else {
                        if (!Mail::Send($this->context->language->id, 'contact', Mail::l('Message from contact form') . ' [no_sync]', $var_list, $contact->email, $contact->name, null, null, $file_attachment, null, _PS_MAIL_DIR_, false, null, null, $from) || !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)) {
                            $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                        }
                    }
                }
                if (count($this->errors) > 1) {
                    array_unique($this->errors);
                } elseif (!count($this->errors)) {
                    $this->context->smarty->assign('confirmation', 1);
                }
            }
        }
    }
Ejemplo n.º 5
0
    public function postProcess()
    {
        global $currentIndex, $cookie, $link;
        if ($id_customer_thread = (int) Tools::getValue('id_customer_thread')) {
            if ($id_contact = (int) Tools::getValue('id_contact')) {
                Db::getInstance()->Execute('UPDATE ' . _DB_PREFIX_ . 'customer_thread SET id_contact = ' . (int) $id_contact . ' WHERE id_customer_thread = ' . (int) $id_customer_thread);
            }
            if ($id_status = (int) Tools::getValue('setstatus')) {
                $statusArray = array(1 => 'open', 2 => 'closed', 3 => 'pending1', 4 => 'pending2');
                Db::getInstance()->Execute('UPDATE ' . _DB_PREFIX_ . 'customer_thread SET status = "' . $statusArray[$id_status] . '" WHERE id_customer_thread = ' . (int) $id_customer_thread . ' LIMIT 1');
            }
            if (isset($_POST['id_employee_forward'])) {
                // Todo: need to avoid doubles
                $messages = Db::getInstance()->ExecuteS('
				SELECT ct.*, cm.*, cl.name subject, CONCAT(e.firstname, \' \', e.lastname) employee_name, CONCAT(c.firstname, \' \', c.lastname) customer_name, c.firstname
				FROM ' . _DB_PREFIX_ . 'customer_thread ct
				LEFT JOIN ' . _DB_PREFIX_ . 'customer_message cm ON (ct.id_customer_thread = cm.id_customer_thread)
				LEFT JOIN ' . _DB_PREFIX_ . 'contact_lang cl ON (cl.id_contact = ct.id_contact AND cl.id_lang = ' . (int) $cookie->id_lang . ')
				LEFT OUTER JOIN ' . _DB_PREFIX_ . 'employee e ON e.id_employee = cm.id_employee
				LEFT OUTER JOIN ' . _DB_PREFIX_ . 'customer c ON (c.email = ct.email)
				WHERE ct.id_customer_thread = ' . (int) Tools::getValue('id_customer_thread') . '
				ORDER BY cm.date_add DESC');
                $output = '';
                foreach ($messages as $message) {
                    $output .= $this->displayMsg($message, true, (int) Tools::getValue('id_employee_forward'));
                }
                $cm = new CustomerMessage();
                $cm->id_employee = (int) $cookie->id_employee;
                $cm->id_customer_thread = (int) Tools::getValue('id_customer_thread');
                $cm->ip_address = ip2long($_SERVER['REMOTE_ADDR']);
                $currentEmployee = new Employee($cookie->id_employee);
                if ($id_employee = (int) Tools::getValue('id_employee_forward') and $employee = new Employee($id_employee) and Validate::isLoadedObject($employee)) {
                    $params = array('{messages}' => $output, '{employee}' => $currentEmployee->firstname . ' ' . $currentEmployee->lastname, '{comment}' => stripslashes($_POST['message_forward']));
                    if (Mail::Send((int) $cookie->id_lang, 'forward_msg', Mail::l('Fwd: Customer message', (int) $cookie->id_lang), $params, $employee->email, $employee->firstname . ' ' . $employee->lastname, $currentEmployee->email, $currentEmployee->firstname . ' ' . $currentEmployee->lastname, NULL, NULL, _PS_MAIL_DIR_, true)) {
                        $cm->message = $this->l('Message forwarded to') . ' ' . $employee->firstname . ' ' . $employee->lastname . "\n" . $this->l('Comment:') . ' ' . $_POST['message_forward'];
                        $cm->add();
                    }
                } elseif ($email = Tools::getValue('email') and Validate::isEmail($email)) {
                    $params = array('{messages}' => $output, '{employee}' => $currentEmployee->firstname . ' ' . $currentEmployee->lastname, '{comment}' => stripslashes($_POST['message_forward']));
                    if (Mail::Send((int) $cookie->id_lang, 'forward_msg', Mail::l('Fwd: Customer message', (int) $cookie->id_lang), $params, $email, NULL, $currentEmployee->email, $currentEmployee->firstname . ' ' . $currentEmployee->lastname, NULL, NULL, _PS_MAIL_DIR_, true)) {
                        $cm->message = $this->l('Message forwarded to') . ' ' . $email . "\n" . $this->l('Comment:') . ' ' . $_POST['message_forward'];
                        $cm->add();
                    }
                } else {
                    echo '<div class="alert error">' . Tools::displayError('Email invalid.') . '</div>';
                }
            }
            if (Tools::isSubmit('submitReply')) {
                $ct = new CustomerThread($id_customer_thread);
                $cm = new CustomerMessage();
                $cm->id_employee = (int) $cookie->id_employee;
                $cm->id_customer_thread = $ct->id;
                $cm->message = Tools::htmlentitiesutf8(nl2br2(Tools::getValue('reply_message')));
                $cm->ip_address = ip2long($_SERVER['REMOTE_ADDR']);
                if (isset($_FILES) and !empty($_FILES['joinFile']['name']) and $_FILES['joinFile']['error'] != 0) {
                    $this->_errors[] = Tools::displayError('An error occurred with the file upload.');
                } elseif ($cm->add()) {
                    $fileAttachment = NULL;
                    if (!empty($_FILES['joinFile']['name'])) {
                        $fileAttachment['content'] = file_get_contents($_FILES['joinFile']['tmp_name']);
                        $fileAttachment['name'] = $_FILES['joinFile']['name'];
                        $fileAttachment['mime'] = $_FILES['joinFile']['type'];
                    }
                    $params = array('{reply}' => nl2br2(Tools::getValue('reply_message')), '{link}' => $link->getPageLink('contact-form.php', true) . '?id_customer_thread=' . (int) $ct->id . '&token=' . $ct->token);
                    if (Mail::Send((int) $ct->id_lang, 'reply_msg', Mail::l('An answer to your message is available', (int) $ct->id_lang), $params, Tools::getValue('msg_email'), NULL, NULL, NULL, $fileAttachment, NULL, _PS_MAIL_DIR_, true)) {
                        $ct->status = 'closed';
                        $ct->update();
                    }
                    Tools::redirectAdmin($currentIndex . '&id_customer_thread=' . (int) $id_customer_thread . '&viewcustomer_thread&token=' . Tools::getValue('token'));
                } else {
                    $this->_errors[] = Tools::displayError('An error occurred, your message was not sent. Please contact your system administrator.');
                }
            }
        }
        return parent::postProcess();
    }
Ejemplo n.º 6
0
 /**
  * Start forms process
  * @see FrontController::postProcess()
  */
 public function postProcess()
 {
     if (Tools::isSubmit('submitMessage')) {
         $idOrder = (int) Tools::getValue('id_order');
         $msgText = Tools::getValue('msgText');
         if (!$idOrder || !Validate::isUnsignedId($idOrder)) {
             $this->errors[] = Tools::displayError('The order is no longer valid.');
         } elseif (empty($msgText)) {
             $this->errors[] = Tools::displayError('The message cannot be blank.');
         } elseif (!Validate::isMessage($msgText)) {
             $this->errors[] = Tools::displayError('This message is invalid (HTML is not allowed).');
         }
         if (!count($this->errors)) {
             $order = new Order($idOrder);
             if (Validate::isLoadedObject($order) && $order->id_customer == $this->context->customer->id) {
                 //check if a thread already exist
                 $id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($this->context->customer->email, $order->id);
                 $id_product = (int) Tools::getValue('id_product');
                 $cm = new CustomerMessage();
                 if (!$id_customer_thread) {
                     $ct = new CustomerThread();
                     $ct->id_contact = 0;
                     $ct->id_customer = (int) $order->id_customer;
                     $ct->id_shop = (int) $this->context->shop->id;
                     if ($id_product && $order->orderContainProduct($id_product)) {
                         $ct->id_product = $id_product;
                     }
                     $ct->id_order = (int) $order->id;
                     $ct->id_lang = (int) $this->context->language->id;
                     $ct->email = $this->context->customer->email;
                     $ct->status = 'open';
                     $ct->token = Tools::passwdGen(12);
                     $ct->add();
                 } else {
                     $ct = new CustomerThread((int) $id_customer_thread);
                     $ct->status = 'open';
                     $ct->update();
                 }
                 $cm->id_customer_thread = $ct->id;
                 $cm->message = $msgText;
                 $cm->ip_address = (int) ip2long($_SERVER['REMOTE_ADDR']);
                 $cm->add();
                 if (!Configuration::get('PS_MAIL_EMAIL_MESSAGE')) {
                     $to = strval(Configuration::get('PS_SHOP_EMAIL'));
                 } else {
                     $to = new Contact((int) Configuration::get('PS_MAIL_EMAIL_MESSAGE'));
                     $to = strval($to->email);
                 }
                 $toName = strval(Configuration::get('PS_SHOP_NAME'));
                 $customer = $this->context->customer;
                 $product = new Product($id_product);
                 $product_name = '';
                 if (Validate::isLoadedObject($product) && isset($product->name[(int) $this->context->language->id])) {
                     $product_name = $product->name[(int) $this->context->language->id];
                 }
                 if (Validate::isLoadedObject($customer)) {
                     Mail::Send($this->context->language->id, 'order_customer_comment', Mail::l('Message from a customer'), array('{lastname}' => $customer->lastname, '{firstname}' => $customer->firstname, '{email}' => $customer->email, '{id_order}' => (int) $order->id, '{order_name}' => $order->getUniqReference(), '{message}' => Tools::nl2br($msgText), '{product_name}' => $product_name), $to, $toName, $customer->email, $customer->firstname . ' ' . $customer->lastname);
                 }
                 if (Tools::getValue('ajax') != 'true') {
                     Tools::redirect('index.php?controller=order-detail&id_order=' . (int) $idOrder);
                 }
                 $this->context->smarty->assign('message_confirmation', true);
             } else {
                 $this->errors[] = Tools::displayError('Order not found');
             }
         }
     }
 }
Ejemplo n.º 7
0
    public function saveContact()
    {
        $message = Tools::getValue('message');
        // Html entities is not usefull, iscleanHtml check there is no bad html tags.
        if (!($from = trim(Tools::getValue('email'))) || !Validate::isEmail($from)) {
            $this->errors[] = Tools::displayError('Invalid email address.');
        } elseif (!$message) {
            $this->errors[] = Tools::displayError('The message cannot be blank.');
        } elseif (!Validate::isCleanHtml($message)) {
            $this->errors[] = Tools::displayError('Invalid message');
        } elseif (!($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 {
            $customer = $this->context->customer;
            if (!$customer->id) {
                $customer->getByEmail($from);
            }
            $id_order = (int) $this->getOrder();
            if (!(($id_customer_thread = (int) Tools::getValue('id_customer_thread')) && (int) Db::getInstance()->getValue('
						SELECT cm.id_customer_thread FROM ' . _DB_PREFIX_ . 'customer_thread cm
						WHERE cm.id_customer_thread = ' . (int) $id_customer_thread . ' AND cm.id_shop = ' . (int) $this->context->shop->id . ' AND token = \'' . pSQL(Tools::getValue('token')) . '\'') || ($id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($from, $id_order)))) {
                $fields = Db::getInstance()->executeS('
					SELECT cm.id_customer_thread, cm.id_contact, cm.id_customer, cm.id_order, cm.id_product, cm.email
					FROM ' . _DB_PREFIX_ . 'customer_thread cm
					WHERE email = \'' . pSQL($from) . '\' AND cm.id_shop = ' . (int) $this->context->shop->id . ' AND (' . ($customer->id ? 'id_customer = ' . (int) $customer->id . ' OR ' : '') . '
						id_order = ' . (int) $id_order . ')');
                $score = 0;
                foreach ($fields as $key => $row) {
                    $tmp = 0;
                    if ((int) $row['id_customer'] && $row['id_customer'] != $customer->id && $row['email'] != $from) {
                        continue;
                    }
                    if ($row['id_order'] != 0 && $id_order != $row['id_order']) {
                        continue;
                    }
                    if ($row['email'] == $from) {
                        $tmp += 4;
                    }
                    if ($row['id_contact'] == $id_contact) {
                        $tmp++;
                    }
                    if (Tools::getValue('id_product') != 0 && $row['id_product'] == Tools::getValue('id_product')) {
                        $tmp += 2;
                    }
                    if ($tmp >= 5 && $tmp >= $score) {
                        $score = $tmp;
                        $id_customer_thread = $row['id_customer_thread'];
                    }
                }
            }
            $old_message = Db::getInstance()->getValue('
					SELECT cm.message FROM ' . _DB_PREFIX_ . 'customer_message cm
					LEFT JOIN ' . _DB_PREFIX_ . 'customer_thread cc on (cm.id_customer_thread = cc.id_customer_thread)
					WHERE cc.id_customer_thread = ' . (int) $id_customer_thread . ' AND cc.id_shop = ' . (int) $this->context->shop->id . '
					ORDER BY cm.date_add DESC');
            if ($old_message == $message) {
                $this->context->smarty->assign('alreadySent', 1);
                $contact->email = '';
                $contact->customer_service = 0;
            }
            if ($contact->customer_service) {
                if ((int) $id_customer_thread) {
                    $ct = new CustomerThread($id_customer_thread);
                    $ct->status = 'open';
                    $ct->id_lang = (int) $this->context->language->id;
                    $ct->id_contact = (int) $id_contact;
                    $ct->id_order = (int) $id_order;
                    if ($id_product = (int) Tools::getValue('id_product')) {
                        $ct->id_product = $id_product;
                    }
                    $ct->update();
                } else {
                    $ct = new CustomerThread();
                    if (isset($customer->id)) {
                        $ct->id_customer = (int) $customer->id;
                    }
                    $ct->id_shop = (int) $this->context->shop->id;
                    $ct->id_order = (int) $id_order;
                    if ($id_product = (int) Tools::getValue('id_product')) {
                        $ct->id_product = $id_product;
                    }
                    $ct->id_contact = (int) $id_contact;
                    $ct->id_lang = (int) $this->context->language->id;
                    $ct->email = $from;
                    $ct->status = 'open';
                    $ct->token = Tools::passwdGen(12);
                    $ct->add();
                }
                if ($ct->id) {
                    $cm = new CustomerMessage();
                    $cm->id_customer_thread = $ct->id;
                    $cm->message = $message;
                    $cm->ip_address = (int) ip2long(Tools::getRemoteAddr());
                    $cm->user_agent = $_SERVER['HTTP_USER_AGENT'];
                    if (!$cm->add()) {
                        $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                    }
                } else {
                    $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                }
            }
            if (!count($this->errors)) {
                $var_list = array('{order_name}' => '-', '{attached_file}' => '-', '{message}' => Tools::nl2br(stripslashes($message)), '{email}' => $from, '{product_name}' => '');
                if (isset($file_attachment['name'])) {
                    $var_list['{attached_file}'] = $file_attachment['name'];
                }
                $id_product = (int) Tools::getValue('id_product');
                if (isset($ct) && Validate::isLoadedObject($ct) && $ct->id_order) {
                    $order = new Order((int) $ct->id_order);
                    $var_list['{order_name}'] = $order->getUniqReference();
                    $var_list['{id_order}'] = (int) $order->id;
                }
                if ($id_product) {
                    $product = new Product((int) $id_product);
                    if (Validate::isLoadedObject($product) && isset($product->name[Context::getContext()->language->id])) {
                        $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);
                } else {
                    if (!Mail::Send($this->context->language->id, 'contact', Mail::l('Message from contact form') . ' [no_sync]', $var_list, $contact->email, $contact->name, $from, $customer->id ? $customer->firstname . ' ' . $customer->lastname : '') || !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, $contact->email, $contact->name)) {
                        $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                    }
                }
            }
            $response = new stdClass();
            if ($this->errors) {
                $response->status = '0';
                $response->msg = implode('<br />', $this->errors);
            } else {
                $response->status = 1;
                $response->msg = "Send message success!";
            }
            die(Tools::jsonEncode($response));
            /*
            if (count($this->errors) > 1)
            	array_unique($this->errors);
            elseif (!count($this->errors))
            	$this->context->smarty->assign('confirmation', 1);
            * 
            */
        }
    }
Ejemplo n.º 8
0
 public function sendMessage()
 {
     $extension = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.zip', '.png', '.jpeg', '.gif', '.jpg');
     $file_attachment = Tools::fileAttachment('fileUpload');
     $message = Tools::getValue('message');
     if (!($from = trim(Tools::getValue('from'))) || !Validate::isEmail($from)) {
         $this->context->controller->errors[] = $this->l('Invalid email address.');
     } elseif (!$message) {
         $this->context->controller->errors[] = $this->l('The message cannot be blank.');
     } elseif (!Validate::isCleanHtml($message)) {
         $this->context->controller->errors[] = $this->l('Invalid message');
     } elseif (!($id_contact = (int) Tools::getValue('id_contact')) || !Validate::isLoadedObject($contact = new Contact($id_contact, $this->context->language->id))) {
         $this->context->controller->errors[] = $this->l('Please select a subject from the list provided. ');
     } elseif (!empty($file_attachment['name']) && $file_attachment['error'] != 0) {
         $this->context->controller->errors[] = $this->l('An error occurred during the file-upload process.');
     } 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)) {
         $this->context->controller->errors[] = $this->l('Bad file extension');
     } else {
         $customer = $this->context->customer;
         if (!$customer->id) {
             $customer->getByEmail($from);
         }
         $id_order = (int) Tools::getValue('id_order');
         $id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($from, $id_order);
         if ($contact->customer_service) {
             if ((int) $id_customer_thread) {
                 $ct = new CustomerThread($id_customer_thread);
                 $ct->status = 'open';
                 $ct->id_lang = (int) $this->context->language->id;
                 $ct->id_contact = (int) $id_contact;
                 $ct->id_order = (int) $id_order;
                 if ($id_product = (int) Tools::getValue('id_product')) {
                     $ct->id_product = $id_product;
                 }
                 $ct->update();
             } else {
                 $ct = new CustomerThread();
                 if (isset($customer->id)) {
                     $ct->id_customer = (int) $customer->id;
                 }
                 $ct->id_shop = (int) $this->context->shop->id;
                 $ct->id_order = (int) $id_order;
                 if ($id_product = (int) Tools::getValue('id_product')) {
                     $ct->id_product = $id_product;
                 }
                 $ct->id_contact = (int) $id_contact;
                 $ct->id_lang = (int) $this->context->language->id;
                 $ct->email = $from;
                 $ct->status = 'open';
                 $ct->token = Tools::passwdGen(12);
                 $ct->add();
             }
             if ($ct->id) {
                 $lastMessage = CustomerMessage::getLastMessageForCustomerThread($ct->id);
                 $testFileUpload = isset($file_attachment['rename']) && !empty($file_attachment['rename']);
                 // if last message is the same as new message (and no file upload), do not consider this contact
                 if ($lastMessage != $message || $testFileUpload) {
                     $cm = new CustomerMessage();
                     $cm->id_customer_thread = $ct->id;
                     $cm->message = $message;
                     if ($testFileUpload && rename($file_attachment['tmp_name'], _PS_UPLOAD_DIR_ . basename($file_attachment['rename']))) {
                         $cm->file_name = $file_attachment['rename'];
                         @chmod(_PS_UPLOAD_DIR_ . basename($file_attachment['rename']), 0664);
                     }
                     $cm->ip_address = (int) ip2long(Tools::getRemoteAddr());
                     $cm->user_agent = $_SERVER['HTTP_USER_AGENT'];
                     if (!$cm->add()) {
                         $this->context->controller->errors[] = $this->l('An error occurred while sending the message.');
                     }
                 } else {
                     $mailAlreadySend = true;
                 }
             } else {
                 $this->context->controller->errors[] = $this->l('An error occurred while sending the message.');
             }
         }
         if (!count($this->context->controller->errors) && empty($mailAlreadySend)) {
             $var_list = ['{order_name}' => '-', '{attached_file}' => '-', '{message}' => Tools::nl2br(stripslashes($message)), '{email}' => $from, '{product_name}' => ''];
             if (isset($file_attachment['name'])) {
                 $var_list['{attached_file}'] = $file_attachment['name'];
             }
             $id_product = (int) Tools::getValue('id_product');
             if (isset($ct) && Validate::isLoadedObject($ct) && $ct->id_order) {
                 $order = new Order((int) $ct->id_order);
                 $var_list['{order_name}'] = $order->getUniqReference();
                 $var_list['{id_order}'] = (int) $order->id;
             }
             if ($id_product) {
                 $product = new Product((int) $id_product);
                 if (Validate::isLoadedObject($product) && isset($product->name[Context::getContext()->language->id])) {
                     $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) ? $this->trans('Your message has been correctly sent #ct%thread_id% #tc%thread_token%', array('%thread_id%' => $ct->id, '%thread_token%' => $ct->token), 'Emails.Subject') : $this->trans('Your message has been correctly sent', array(), 'Emails.Subject'), $var_list, $from, null, null, null, $file_attachment);
             } else {
                 if (!Mail::Send($this->context->language->id, 'contact', $this->trans('Message from contact form', array(), 'Emails.Subject') . ' [no_sync]', $var_list, $contact->email, $contact->name, null, null, $file_attachment, null, _PS_MAIL_DIR_, false, null, null, $from) || !Mail::Send($this->context->language->id, 'contact_form', isset($ct) && Validate::isLoadedObject($ct) ? $this->trans('Your message has been correctly sent #ct%thread_id% #tc%thread_token%', array('%thread_id%' => $ct->id, '%thread_token%' => $ct->token), 'Emails.Subject') : $this->trans('Your message has been correctly sent', array(), 'Emails.Subject'), $var_list, $from, null, null, null, $file_attachment, null, _PS_MAIL_DIR_, false, null, null, $contact->email)) {
                     $this->context->controller->errors[] = $this->l('An error occurred while sending the message.');
                 }
             }
         }
         if (!count($this->context->controller->errors)) {
             $this->context->controller->success[] = $this->l('Your message has been successfully sent to our team.');
         }
     }
 }
Ejemplo n.º 9
0
 /**
  * Start forms process
  * @see FrontController::postProcess()
  */
 public function postProcess()
 {
     if (Tools::isSubmit('submitMessage')) {
         $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.');
         } elseif (!$message) {
             $this->errors[] = Tools::displayError('The message cannot be blank.');
         } elseif (!Validate::isCleanHtml($message)) {
             $this->errors[] = Tools::displayError('Invalid message');
         } elseif (!empty($file_attachment['name']) && $file_attachment['error'] != 0) {
             $this->errors[] = Tools::displayError('An error occurred during the file-upload process.');
         } 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)) {
             $this->errors[] = Tools::displayError('Bad file extension');
         } else {
             $customer = $this->context->customer;
             if (!$customer->id) {
                 $customer->getByEmail($from);
             }
             $id_contact = (int) Configuration::get('ASKFORPRICE_CONTACT');
             $contact = new Contact($id_contact);
             if ($contact->customer_service) {
                 if ((int) $id_customer_thread) {
                     $ct = new CustomerThread($id_customer_thread);
                     $ct->status = 'open';
                     $ct->id_lang = (int) $this->context->language->id;
                     $ct->id_contact = (int) $id_contact;
                     $ct->id_order = (int) $id_order;
                     if ($id_product = (int) Tools::getValue('id_product')) {
                         $ct->id_product = $id_product;
                     }
                     $ct->update();
                 } else {
                     $ct = new CustomerThread();
                     if (isset($customer->id)) {
                         $ct->id_customer = (int) $customer->id;
                     }
                     $ct->id_shop = (int) $this->context->shop->id;
                     $ct->id_order = (int) $id_order;
                     if ($id_product = (int) Tools::getValue('id_product')) {
                         $ct->id_product = $id_product;
                     }
                     $ct->id_contact = (int) $id_contact;
                     $ct->id_lang = (int) $this->context->language->id;
                     $ct->email = $from;
                     $ct->status = 'open';
                     $ct->token = Tools::passwdGen(12);
                     $ct->add();
                 }
                 if ($ct->id) {
                     $cm = new CustomerMessage();
                     $cm->id_customer_thread = $ct->id;
                     $cm->message = $message;
                     if (isset($file_attachment['rename']) && !empty($file_attachment['rename']) && rename($file_attachment['tmp_name'], _PS_UPLOAD_DIR_ . basename($file_attachment['rename']))) {
                         $cm->file_name = $file_attachment['rename'];
                         @chmod(_PS_UPLOAD_DIR_ . basename($file_attachment['rename']), 0664);
                     }
                     $cm->ip_address = (int) ip2long(Tools::getRemoteAddr());
                     $cm->user_agent = $_SERVER['HTTP_USER_AGENT'];
                     if (!$cm->add()) {
                         $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                     }
                 } else {
                     $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                 }
             }
             if (!count($this->errors)) {
                 $mailpaht = _PS_MODULE_DIR_ . 'askforprice/mails/';
                 $message = Tools::nl2br(stripslashes($message));
                 $var_list = array('{message}' => $message, '{email}' => $from, '{id_product}' => (int) Tools::getValue('id_product'), '{product_name}' => '', '{product_qty}' => (int) Tools::getValue('qty'), '{suggested_price}' => (double) Tools::getValue('price'));
                 $id_product = (int) Tools::getValue('id_product');
                 if (isset($ct) && Validate::isLoadedObject($ct) && $ct->id_order) {
                     $order = new Order((int) $ct->id_order);
                     $var_list['{order_name}'] = $order->getUniqReference();
                     $var_list['{id_order}'] = (int) $order->id;
                 }
                 if ($id_product) {
                     $product = new Product((int) $id_product);
                     if (Validate::isLoadedObject($product) && isset($product->name[Context::getContext()->language->id])) {
                         $var_list['{product_name}'] = $product->name[Context::getContext()->language->id];
                     }
                 }
                 if (empty($contact->email)) {
                     Mail::Send($this->context->language->id, 'askforprice_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, $mailpaht);
                 } else {
                     if (!Mail::Send($this->context->language->id, 'askforprice', Mail::l('Message from contact form') . ' [no_sync]', $var_list, $contact->email, $contact->name, $from, $customer->id ? $customer->firstname . ' ' . $customer->lastname : '', null, null, $mailpaht) || !Mail::Send($this->context->language->id, 'askforprice_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, $contact->email, $contact->name, null, null, $mailpaht)) {
                         $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                     }
                 }
             }
             if (count($this->errors) > 1) {
                 array_unique($this->errors);
             } elseif (!count($this->errors)) {
                 $this->context->smarty->assign('confirmation', 1);
             }
         }
     }
 }