bebeboutik-scripts/export_webdav.php
2017-12-18 17:14:31 +01:00

313 lines
9.0 KiB
PHP

<?php
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
$_SERVER['SERVER_NAME'] = 'www.bebeboutik.com';
$_SERVER['HTTP_PORT'] = 80;
include('www/config/config.inc.php');
ini_set('memory_limit', '6G');
class WebDAV {
var $host;
var $user;
var $pass;
var $connected;
var $fp;
function WebDAV($host,$user,$pass) {
$this->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
<?xml version="1.0" encoding="utf-8" ?>
<D:propfind xmlns:D="DAV:"><D:allprop/></D:propfind>
';
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.'[^>]*>(.*?)</'.$tag.'>|is',$xml,$matches)) {
if ($stripcdata == true) {
foreach ($matches[1] as $match) {
if (strpos($match,'&lt;![CDATA[')!==false) {
$match = preg_replace('/\s*&lt;!\[CDATA\[/','',$match);
$match = preg_replace('/\]\]&gt;\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
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`
FROM `ps_customer` c
LEFT JOIN `ps_customer_version` v ON v.`id_customer` = c.`id_customer`
ORDER BY `id_customer` ASC
') as $row) {
$customers_to_import[(int)$row['id_customer']] = array(
'id_customer' => $row['id_customer'],
'firstname' => $row['firstname'],
'lastname' => $row['lastname'],
'email' => $row['email'],
'ip_registration_newsletter' => $row['ip_registration_newsletter'],
'newsletter' => (int)$row['newsletter'],
'version' => $row['version'],
'date_first_order' => "",
'date_last_order' => "",
'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'), ';', '"');
for($i=0, $l=count($id_customers); $i < $l; $i+=5000) {
// 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
LEFT JOIN `'._DB_PREFIX_.'order_history` oh ON o.`id_order` = oh.`id_order`
WHERE 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)
AND o.`id_customer` IN ('.implode(', ', array_slice($id_customers, $i, 5000)).')
GROUP BY o.`id_customer`
ORDER BY o.`id_order` DESC
') as $row) {
$customers_to_import[(int)$row['id_customer']]['date_first_order'] = $row['date_first_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
LEFT JOIN `'._DB_PREFIX_.'orders` o ON h.`id_order` = o.`id_order`
WHERE h.`id_order_state` NOT IN (1,14,15,18,6,8,10,11)
AND o.`id_customer` IN ('.implode(', ', array_slice($id_customers, $i, 5000)).')
GROUP BY o.`id_customer`
ORDER BY h.`id_order` DESC
') as $row) {
$customers_to_import[(int)$row['id_customer']]['date_last_order'] = $row['date_last_order'];
}
// Discount
foreach (Db::getInstance()->ExecuteS('
SELECT dh.`id_customer`, dh.`code` as `discount`
FROM `'._DB_PREFIX_.'ant_discount_history` dh
WHERE dh.`id_customer` IN ('.implode(', ', array_slice($id_customers, $i, 5000)).')
AND dh.`used` = 0
GROUP BY dh.`id_customer`
ORDER BY dh.`date_add` DESC
') as $row) {
$customers_to_import[(int)$row['id_customer']]['discount'] = $row['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');
*/
$ftp = ftp_connect('ftp.emarsys.fr');
ftp_login($ftp, 'emarsys-bbb', 'XuCVuK64');
ftp_put($ftp, $filename, 'extracts/webdav/'.$filename, FTP_BINARY);
ftp_close($ftp);