예제 #1
0
 /**
  * Copy a remote or local file to the local campaign storage
  * @param string $url
  * @param string $filename The name of the resulting file
  * @return mixed Null if fail or a string containing the path of the resulting image.
  */
 private function copyFileToStorage($url, $filename = null)
 {
     $dest = $this->module->getPreviewFolder();
     $dest .= $this->campaign_id . DIRECTORY_SEPARATOR;
     if (!Tools::file_exists_no_cache($dest)) {
         mkdir($dest, 0777, true);
         Tools::copy(_PS_MODULE_DIR_ . 'expressmailing/index.php', $dest . 'index.php');
     }
     if ($filename) {
         $dest .= (string) $filename;
     } else {
         $dest .= basename((string) $url);
     }
     $dest = urldecode($dest);
     $dest = str_replace(' ', '_', $dest);
     $dest = EMTools::removeAccents($dest);
     if (($pos = strpos($dest, '?')) !== false) {
         $dest = Tools::substr($dest, 0, $pos);
     }
     if (function_exists('curl_version')) {
         $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0';
         $ch = curl_init((string) $url);
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
         curl_setopt($ch, CURLOPT_CRLF, 1);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
         $raw = curl_exec($ch);
         curl_close($ch);
         if (file_exists($dest)) {
             unlink($dest);
         }
         $fp = fopen($dest, 'x');
         fwrite($fp, $raw);
         fclose($fp);
         if (file_exists($dest)) {
             return $dest;
         }
     } else {
         if (Tools::copy((string) $url, $dest)) {
             return $dest;
         }
     }
     return null;
 }
 public function postProcess()
 {
     // On construit un login pour le compte
     // ------------------------------------
     // Si PS_SHOP_EMAIL = info@axalone.com
     // Alors login      = ps-info-axalone
     //   1/ On ajoute 'ps-' devant l'email
     //   2/ On retire l'extention .com à la fin
     //   3/ On remplace toutes les lettres accentuées par leurs équivalents sans accent
     //   4/ On remplace tous les sigles par des tirets
     //   5/ Enfin on remplace les doubles/triples tirets par des simples
     // --------------------------------------------------------------------------------
     $company_login = '******' . Configuration::get('PS_SHOP_EMAIL');
     $company_login = Tools::substr($company_login, 0, strrpos($company_login, '.'));
     $company_login = EMTools::removeAccents($company_login);
     $company_login = Tools::strtolower($company_login);
     $company_login = preg_replace('/[^a-z0-9-]/', '-', $company_login);
     $company_login = preg_replace('/-{2,}/', '-', $company_login);
     $cart_product = (string) Tools::getValue('product', '');
     // Initialisation de l'API
     // -----------------------
     if (Tools::isSubmit('submitInscription')) {
         // On prépare l'ouverture du compte
         // --------------------------------
         $company_name = (string) Tools::getValue('company_name');
         $company_email = (string) Tools::getValue('company_email');
         $company_phone = (string) Tools::getValue('company_phone');
         $company_address1 = (string) Tools::getValue('company_address1');
         $company_address2 = (string) Tools::getValue('company_address2');
         $company_zipcode = (string) Tools::getValue('company_zipcode');
         $company_city = (string) Tools::getValue('company_city');
         $country_id = (int) Tools::getValue('country_id');
         $country = new Country($country_id);
         if (!is_object($country) || empty($country->id)) {
             $this->errors[] = Tools::displayError('Country is invalid');
         } else {
             $company_country = Country::getNameById($this->context->language->id, $country_id);
         }
         if (!Validate::isGenericName($company_name)) {
             $this->errors[] = sprintf(Tools::displayError('The %s field is required.'), '« ' . Translate::getAdminTranslation('Shop name', 'AdminStores') . ' »');
         }
         if (!Validate::isEmail($company_email)) {
             $this->errors[] = sprintf(Tools::displayError('The %s field is required.'), '« ' . Translate::getAdminTranslation('Shop email', 'AdminStores') . ' »');
         }
         if (!Validate::isPhoneNumber($company_phone)) {
             $this->errors[] = sprintf(Tools::displayError('The %s field is required.'), '« ' . Translate::getAdminTranslation('Phone', 'AdminStores') . ' »');
         }
         if (!Validate::isAddress($company_address1)) {
             $this->errors[] = sprintf(Tools::displayError('The %s field is required.'), '« ' . Translate::getAdminTranslation('Shop address line 1', 'AdminStores') . ' »');
         }
         if ($country->zip_code_format && !$country->checkZipCode($company_zipcode)) {
             $this->errors[] = Tools::displayError('Your Zip/postal code is incorrect.') . '<br />' . Tools::displayError('It must be entered as follows:') . ' ' . str_replace('C', $country->iso_code, str_replace('N', '0', str_replace('L', 'A', $country->zip_code_format)));
         } elseif (empty($company_zipcode) && $country->need_zip_code) {
             $this->errors[] = Tools::displayError('A Zip/postal code is required.');
         } elseif ($company_zipcode && !Validate::isPostCode($company_zipcode)) {
             $this->errors[] = Tools::displayError('The Zip/postal code is invalid.');
         }
         if (!Validate::isGenericName($company_city)) {
             $this->errors[] = sprintf(Tools::displayError('The %s field is required.'), '« ' . Translate::getAdminTranslation('City', 'AdminStores') . ' »');
         }
         // We save these informations in the database
         // ------------------------------------------
         Db::getInstance()->insert('expressmailing_order_address', array('id_address' => 1, 'company_name' => pSQL($company_name), 'company_email' => pSQL($company_email), 'company_address1' => pSQL($company_address1), 'company_address2' => pSQL($company_address2), 'company_zipcode' => pSQL($company_zipcode), 'company_city' => pSQL($company_city), 'country_id' => $country_id, 'company_country' => pSQL($company_country), 'company_phone' => pSQL($company_phone), 'product' => pSQL($cart_product)), false, false, Db::REPLACE);
         // If form contains 1 or more errors, we stop the process
         // ------------------------------------------------------
         if (is_array($this->errors) && count($this->errors)) {
             return false;
         }
         // Open a session on Express-Mailing API
         // -------------------------------------
         if ($this->session_api->openSession()) {
             // We create the account
             // ---------------------
             $response_array = array();
             $base_url = Configuration::get('PS_SSL_ENABLED') == 0 ? Tools::getShopDomain(true, true) : Tools::getShopDomainSsl(true, true);
             $module_dir = Tools::str_replace_once(_PS_ROOT_DIR_, '', _PS_MODULE_DIR_);
             $parameters = array('login' => $company_login, 'info_company' => $company_name, 'info_email' => $company_email, 'info_phone' => $company_phone, 'info_address' => $company_address1 . "\r\n" . $company_address2, 'info_country' => $company_country, 'info_zipcode' => $company_zipcode, 'info_city' => $company_city, 'info_phone' => $company_phone, 'info_contact_firstname' => $this->context->employee->firstname, 'info_contact_lastname' => $this->context->employee->lastname, 'email_report' => $this->context->employee->email, 'gift_code' => 'prestashop_' . Translate::getModuleTranslation('expressmailing', '3320', 'session_api'), 'INFO_WWW' => $base_url . $module_dir . $this->module->name . '/campaigns/index.php');
             if ($this->session_api->createAccount($parameters, $response_array)) {
                 // If the form include the buying process (field 'product')
                 // We initiate a new cart with the product selected
                 // --------------------------------------------------------
                 if ($cart_product) {
                     Tools::redirectAdmin('index.php?controller=AdminMarketingBuy&submitCheckout&campaign_id=' . $this->campaign_id . '&media=' . $this->next_controller . '&product=' . $cart_product . '&token=' . Tools::getAdminTokenLite('AdminMarketingBuy'));
                     exit;
                 }
                 // Else we back to the mailing process
                 // -----------------------------------
                 Tools::redirectAdmin($this->next_action);
                 exit;
             }
             if ($this->session_api->error == 11) {
                 // Account already existe, we print the rescue form (with password input)
                 // ----------------------------------------------------------------------
                 $response_array = array();
                 $parameters = array('login' => $company_login);
                 $this->session_api->resendPassword($parameters, $response_array);
                 $this->generateRescueForm();
                 return;
             } else {
                 // Other error
                 // -----------
                 $this->errors[] = sprintf($this->module->l('Unable to create an account : %s', 'adminmarketinginscription'), $this->session_api->getError());
                 return false;
             }
         } else {
             $this->errors[] = sprintf($this->module->l('Error during communication with Express-Mailing API : %s', 'adminmarketinginscription'), $this->session_api->getError());
             return false;
         }
     } elseif (Tools::isSubmit('submitRescue')) {
         // Rescue form : ask for existing password
         // ---------------------------------------
         if ($this->session_api->openSession()) {
             $response_array = array();
             $password = trim((string) Tools::getValue('api_password'));
             $parameters = array('login' => $company_login, 'password' => $password);
             if ($this->session_api->connectUser($parameters, $response_array)) {
                 Db::getInstance()->insert('expressmailing', array('api_login' => pSQL($company_login), 'api_password' => pSQL($password)), false, false, Db::REPLACE);
                 // If the form include the buying process (field 'product')
                 // We initiate a new cart with the product selected
                 // --------------------------------------------------------
                 if ($cart_product) {
                     Tools::redirectAdmin('index.php?controller=AdminMarketingBuy&submitCheckout&campaign_id=' . $this->campaign_id . '&media=' . $this->next_controller . '&product=' . $cart_product . '&token=' . Tools::getAdminTokenLite('AdminMarketingBuy'));
                     exit;
                 }
                 // Else we back to the mailing process
                 // -----------------------------------
                 Tools::redirectAdmin($this->next_action);
                 exit;
             }
         }
         $this->errors[] = sprintf($this->module->l('Error during communication with Express-Mailing API : %s', 'adminmarketinginscription'), $this->session_api->getError());
         return false;
     }
 }