110 lines
3.2 KiB
PHP
110 lines
3.2 KiB
PHP
<?php
|
|
/**
|
|
* 2007-2017 Decanet
|
|
*
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This source file is subject to the Academic Free License (AFL 3.0)
|
|
* that is bundled with this package in the file LICENSE.txt.
|
|
* It is also available through the world-wide-web at this URL:
|
|
* http://opensource.org/licenses/afl-3.0.php
|
|
* If you did not receive a copy of the license and are unable to
|
|
* obtain it through the world-wide-web, please send an email
|
|
* to license@prestashop.com so we can send you a copy immediately.
|
|
*
|
|
* DISCLAIMER
|
|
*
|
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
|
* versions in the future. If you wish to customize PrestaShop for your
|
|
* needs please refer to http://www.decanet.fr for more information.
|
|
*
|
|
* @author Decanet SA <contact@decanet.fr>
|
|
* @copyright 2007-2017 Decanet SA
|
|
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
|
* International Registered Trademark & Property of PrestaShop SA
|
|
*/
|
|
|
|
class DecanetApi
|
|
{
|
|
public $LOGIN = false;
|
|
public $PASS = false;
|
|
public $ROOT = 'https://api.decanet.fr';
|
|
|
|
public $timeDrift = 0;
|
|
|
|
public function __construct($_login = false, $_pass = false, $_root = false)
|
|
{
|
|
if ($_login) {
|
|
$this->LOGIN = $_login;
|
|
}
|
|
if ($_pass) {
|
|
$this->PASS = $_pass;
|
|
}
|
|
if ($_root) {
|
|
$this->ROOT = $_root;
|
|
}
|
|
|
|
// Compute time drift
|
|
$srvTime = Tools::jsonDecode(Tools::file_get_contents($this->ROOT . '/auth/time'));
|
|
if ($srvTime !== false) {
|
|
$this->timeDrift = time() - (int)$srvTime;
|
|
}
|
|
}
|
|
|
|
private function call($method, $url, $body = null)
|
|
{
|
|
$url = $this->ROOT . $url;
|
|
if ($body) {
|
|
$bodystring = '';
|
|
foreach ($body as $key => $value) {
|
|
$bodystring .= $key.'='.urlencode($value).'&';
|
|
}
|
|
$bodystring = rtrim($bodystring, '&');
|
|
}
|
|
|
|
// Compute signature
|
|
$time = time() - $this->timeDrift;
|
|
$toSign = $this->LOGIN.'+'.$this->PASS.'+'.$method.'+'.$time;
|
|
$signature = '$1$' . sha1($toSign);
|
|
|
|
// Call
|
|
$curl = curl_init($url);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
|
|
'X-Consumer:' . $this->LOGIN,
|
|
'X-Signature:' . $signature,
|
|
'X-Timestamp:' . $time,
|
|
));
|
|
if ($body) {
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $bodystring);
|
|
}
|
|
$result = curl_exec($curl);
|
|
if ($result === false) {
|
|
echo curl_error($curl);
|
|
return null;
|
|
}
|
|
|
|
return Tools::jsonDecode($result);
|
|
}
|
|
|
|
public function get($url)
|
|
{
|
|
return $this->call("GET", $url);
|
|
}
|
|
public function put($url, $body)
|
|
{
|
|
return $this->call("PUT", $url, $body);
|
|
}
|
|
public function post($url, $body)
|
|
{
|
|
return $this->call("POST", $url, $body);
|
|
}
|
|
public function delete($url, $body = false)
|
|
{
|
|
return $this->call("DELETE", $url, $body);
|
|
}
|
|
}
|