diff --git a/cron_emarsys_unsubscribe.php b/cron_emarsys_unsubscribe.php index deb84fc..6cc84be 100644 --- a/cron_emarsys_unsubscribe.php +++ b/cron_emarsys_unsubscribe.php @@ -1,4 +1,9 @@ getRow(' + if(in_array((int) $line[1], array(0, 1, 2)) + && !empty($line[2]) && in_array(strtolower($line[3]), array('vrai', 'faux')) + && Validate::isEmail($line[2])) { + + if($customer = $db->getRow(' SELECT `id_customer` FROM `'._DB_PREFIX_.'customer` WHERE `email` = "'.pSQL($line[2]).'" @@ -33,5 +44,7 @@ foreach(glob(dirname(__FILE__).'/emarsys/*.csv') as $filename) { } } fclose($f); + + // Move file as done rename($filename, str_replace('/emarsys/', '/emarsys_done/', $filename)); } \ No newline at end of file diff --git a/export_webdav.php b/export_webdav.php index eb7fd4d..07c23d2 100644 --- a/export_webdav.php +++ b/export_webdav.php @@ -1,4 +1,9 @@ host = $host; - $this->user = $user; - $this->pass = $pass; - $this->connected = false; - } - - - function connect() { - $this->fp = fsockopen($this->host, 80, $errno, $errstr, 5); - if(!$this->fp) { - $this->connected = false; - return false; - } else { - $this->connected = true; - return true; - } - } - - /** - * check if the connection was established - * returns true if connected otherwise false - */ - function isConnected() { - return $this->connected; - } - - /* - * get the file listing from webdav - */ - function getListing($directory='/') { - $listing = array(); - if ($this->isConnected() == false) { - $this->connect(); - } - if ($this->isConnected() == false) { - return $listing; - } else { - $data = 'PROPFIND '.$directory.' HTTP/1.0 -Host: '.$this->host.' -Connection: close -Authorization: Basic '.base64_encode($this->user.':'.$this->pass).' -Depth: 1 -Content-Type: text/xml; charset="utf-8" -Content-Length: 93 - - - -'; - - fwrite($this->fp,$data); - - $xml = ''; - while (!feof($this->fp)) { - $xml .= fgets($this->fp, 8192); - } - - $responses = $this->getContentInTag('d:response',$xml,true); - foreach ($responses as $response) { - $hrefs = $this->getContentInTag('d:href',$response,true); - $rtype = $this->getContentInTag('d:resourcetype',$response,true); - if (trim($rtype[0]) == '') { - $listing[$hrefs[0]] = 'file'; - } else { - $listing[$hrefs[0]] = 'dir'; - } - } - - $this->disconnect(); - return $listing; - } - } - - - - - function storeFile($filename, $destFile) { - if (!file_exists($filename)) { - print "No such file for upload: $filename\n"; - return false; - } - - if ($this->isConnected() == false) { - $this->connect(); - } - if ($this->isConnected() == false) { - return false; - } - - $data = 'PUT '.$destFile.' HTTP/1.0 -Host: '.$this->host.' -Connection: close -Authorization: Basic '.base64_encode($this->user.':'.$this->pass).' -Content-Type: application/octet-stream -Content-Length: '.filesize($filename).' - -'; - fwrite($this->fp,$data); - // print $data; - - $fh = fopen($filename,'rb'); - while (!feof($fh)) { - $data = fread($fh,8192); - fwrite($this->fp,$data); - // print $data; - } - fclose($fh); - $this->disconnect(); - } - - - function disconnect() { - if ($this->isConnected() == true) { - fclose($this->fp); - $this->connected = false; - } - } - - - function getContentInTag($tag,&$xml,$stripcdata=false) { - $ret = array(); - if ($xml == '') { - print "No XML for tag ".$tag."\n"; - } - $matches = array(); - if (preg_match_all('|<'.$tag.'[^>]*>(.*?)|is',$xml,$matches)) { - if ($stripcdata == true) { - foreach ($matches[1] as $match) { - if (strpos($match,'<![CDATA[')!==false) { - $match = preg_replace('/\s*<!\[CDATA\[/','',$match); - $match = preg_replace('/\]\]>\s*/','',$match); - $ret[] = $match; - } else { - $ret[] = $match; - } - } - } else { - $ret = array(); - foreach ($matches[1] as $match) { - $ret[] = html_entity_decode ($match); - } - return $ret; - } - } - return $ret; - } - - -} - $customers_to_import = array(); // Customer informations @@ -182,7 +28,7 @@ foreach (Db::getInstance()->ExecuteS(' ORDER BY `id_customer` ASC ') as $row) { $customers_to_import[(int)$row['id_customer']] = array( - 'id_customer' => $row['id_customer'], + 'id_customer' => $row['id_customer'], 'firstname' => $row['firstname'], 'lastname' => $row['lastname'], 'email' => $row['email'], @@ -194,16 +40,13 @@ foreach (Db::getInstance()->ExecuteS(' 'discount' => "" ); } + $id_customers = array_keys($customers_to_import); -$filename = date('Y-m-d', mktime()).'.csv'; - -$f = fopen('extracts/webdav/'.$filename, 'w'); - -fputcsv($f, array('id_customer', 'firstname', 'lastname', 'email', 'ip_registration_newsletter', 'newsletter', 'langue', 'date de premier achat', 'Date de dernier achat', 'discount'), ';', '"'); +//@todo : newsletter for($i=0, $l=count($id_customers); $i < $l; $i+=5000) { - // first order + // First order foreach (Db::getInstance()->ExecuteS(' SELECT o.`id_customer`, DATE_FORMAT(o.`date_add`,\'%d/%m/%Y\') AS `date_first_order` FROM `'._DB_PREFIX_.'orders` o @@ -217,7 +60,7 @@ for($i=0, $l=count($id_customers); $i < $l; $i+=5000) { $customers_to_import[(int)$row['id_customer']]['date_first_order'] = $row['date_first_order']; } - // last order + // Last order foreach (Db::getInstance()->ExecuteS(' SELECT o.`id_customer`, DATE_FORMAT(MAX(h.`date_add`), \'%d/%m/%Y\') AS `date_last_order` FROM `'._DB_PREFIX_.'order_history` h @@ -242,70 +85,16 @@ for($i=0, $l=count($id_customers); $i < $l; $i+=5000) { $customers_to_import[(int)$row['id_customer']]['discount'] = $row['discount']; } } + +$filename = date('Y-m-d', mktime()).'.csv'; +$f = fopen('extracts/webdav/'.$filename, 'w'); +fputcsv($f, array('id_customer', 'firstname', 'lastname', 'email', 'ip_registration_newsletter', 'newsletter', 'langue', 'date de premier achat', 'Date de dernier achat', 'discount'), ';', '"'); foreach ($customers_to_import as $customer) { fwrite($f, implode(';', array_values($customer))."\n"); } fclose($f); -// Ancienne requete trop longue -// foreach(Db::getInstance()->ExecuteS(' -// SELECT -// c.`id_customer`, -// c.`firstname`, -// c.`lastname`, -// c.`email`, -// c.`ip_registration_newsletter`, -// IF(c.`deleted` = 1, 0, IF(c.`active` = 0, 0, c.`newsletter`)) AS `newsletter`, -// IFNULL(v.`version`, "fr") AS `version`, -// IFNULL( -// ( -// SELECT DATE_FORMAT(o.`date_add`,\'%m/%d/%Y\') -// FROM `'._DB_PREFIX_.'orders` o -// LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON (oh.`id_order` = o.`id_order`) -// WHERE o.`id_customer` = c.`id_customer` -// AND oh.`id_order_history` = (SELECT MAX(`id_order_history`) FROM `'._DB_PREFIX_.'order_history` moh WHERE moh.`id_order` = o.`id_order` GROUP BY moh.`id_order`) -// AND oh.id_order_state NOT IN (1,14,15,18,6,8,10,11) -// ORDER BY o.`date_add` ASC LIMIT 1 -// ) -// ,"") AS `date_first_order`, -// IFNULL( -// ( -// SELECT DATE_FORMAT(o.`date_add`,\'%m/%d/%Y\') -// FROM `'._DB_PREFIX_.'orders` o -// LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON (oh.`id_order` = o.`id_order`) -// WHERE o.`id_customer` = c.`id_customer` -// AND oh.`id_order_history` = (SELECT MAX(`id_order_history`) FROM `'._DB_PREFIX_.'order_history` moh WHERE moh.`id_order` = o.`id_order` GROUP BY moh.`id_order`) -// AND oh.id_order_state NOT IN (1,14,15,18,6,8,10,11) -// ORDER BY o.`date_add` DESC LIMIT 1 -// ) -// ,"") AS `date_last_order`, -// IFNULL( -// ( -// SELECT dh.`code` -// FROM `'._DB_PREFIX_.'ant_discount_history` dh -// WHERE dh.`id_customer` = c.`id_customer` -// AND dh.`used` = 0 -// ORDER BY dh.`date_add` DESC LIMIT 1 -// ) -// ,"") AS `discount` -// FROM `'._DB_PREFIX_.'customer` c -// LEFT JOIN `'._DB_PREFIX_.'customer_version` v ON v.`id_customer` = c.`id_customer` -// ORDER BY `id_customer` ASC LIMIT '.$i.','.($i+10000).' -// ') as $customer) { -// fputcsv($f, $customer, ';', '"'); -// } - - -/* -$user = 'admin'; -$pw = 'v6S385Vf'; -$account = 'bebeboutik'; -$host = 'suite.emarsys.net'; - -$w = new WebDAV($host, $user, $pw); -$w->connect(); -$w->storeFile('extracts/webdav/'.$filename, '/storage/'.$account.'/newsletter.csv'); -*/ +// Send to FTP $ftp = ftp_connect('ftp.emarsys.fr'); ftp_login($ftp, 'emarsys-bbb', 'XuCVuK64'); ftp_put($ftp, $filename, 'extracts/webdav/'.$filename, FTP_BINARY);