dolibarr  17.0.4
PSWebServiceLibrary.class.php
1 <?php
2 /*
3 * 2007-2013 PrestaShop
4 *
5 * NOTICE OF LICENSE
6 *
7 * This source file is subject to the Open Software License (OSL 3.0)
8 * that is bundled with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * https://opensource.org/licenses/osl-3.0.php
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@prestashop.com so we can send you a copy immediately.
14 *
15 * DISCLAIMER
16 *
17 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18 * versions in the future. If you wish to customize PrestaShop for your
19 * needs please refer to https://www.prestashop.com for more information.
20 *
21 * @author PrestaShop SA <contact@prestashop.com>
22 * @copyright 2007-2013 PrestaShop SA
23 * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
24 * International Registered Trademark & Property of PrestaShop SA
25 * PrestaShop Webservice Library
26 * @package PrestaShopWebservice
27 */
28 
33 {
34 
36  protected $url;
37 
39  protected $key;
40 
42  protected $debug;
43 
45  protected $version;
46 
48  const PSCOMPATIBLEVERSIONMIN = '1.4.0.0';
49  const PSCOMPATIBLEVERSIONMAX = '1.7.99.99';
50 
51 
72  public function __construct($url, $key, $debug = true)
73  {
74  if (!extension_loaded('curl')) {
75  throw new PrestaShopWebserviceException('Please activate the PHP extension \'curl\' to allow use of PrestaShop webservice library');
76  }
77  $this->url = $url;
78  $this->key = $key;
79  $this->debug = $debug;
80  $this->version = 'unknown';
81  }
82 
89  protected function checkStatusCode($status_code)
90  {
91  $error_label = 'This call to PrestaShop Web Services failed and returned an HTTP status of %d. That means: %s.';
92  switch ($status_code) {
93  case 200:
94  case 201:
95  break;
96  case 204:
97  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'No content'));
98  case 400:
99  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Bad Request'));
100  case 401:
101  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Unauthorized'));
102  case 404:
103  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Not Found'));
104  case 405:
105  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Method Not Allowed'));
106  case 500:
107  throw new PrestaShopWebserviceException(sprintf($error_label, $status_code, 'Internal Server Error'));
108  default:
109  throw new PrestaShopWebserviceException('This call to PrestaShop Web Services returned an unexpected HTTP status of:'.$status_code);
110  }
111  }
112 
120  public function executeRequest($url, $curl_params = array())
121  {
122  $defaultParams = array(
123  CURLOPT_HEADER => true,
124  CURLOPT_RETURNTRANSFER => true,
125  CURLINFO_HEADER_OUT => true,
126  CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
127  CURLOPT_USERPWD => $this->key.':',
128  CURLOPT_HTTPHEADER => array('Expect:')
129  );
130 
131  dol_syslog("curl_init url=".$url);
132  $session = curl_init($url);
133 
134  $curl_options = array();
135  foreach ($defaultParams as $defkey => $defval) {
136  if (isset($curl_params[$defkey])) {
137  $curl_options[$defkey] = $curl_params[$defkey];
138  } else {
139  $curl_options[$defkey] = $defaultParams[$defkey];
140  }
141  }
142  foreach ($curl_params as $defkey => $defval) {
143  if (!isset($curl_options[$defkey])) {
144  $curl_options[$defkey] = $curl_params[$defkey];
145  }
146  }
147 
148  dol_syslog("curl curl_options = ".var_export($curl_options, true));
149  curl_setopt_array($session, $curl_options);
150  $response = curl_exec($session);
151 
152  $index = strpos($response, "\r\n\r\n");
153  if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') {
154  throw new PrestaShopWebserviceException('Bad HTTP response');
155  }
156 
157  $header = substr($response, 0, $index);
158  $body = substr($response, $index + 4);
159 
160  $headerArrayTmp = explode("\n", $header);
161 
162  $headerArray = array();
163  foreach ($headerArrayTmp as &$headerItem) {
164  $tmp = explode(':', $headerItem);
165  $tmp = array_map('trim', $tmp);
166  if (count($tmp) == 2) {
167  $headerArray[$tmp[0]] = $tmp[1];
168  }
169  }
170 
171  if (array_key_exists('PSWS-Version', $headerArray)) {
172  $this->version = $headerArray['PSWS-Version'];
173  if (version_compare(PrestaShopWebservice::PSCOMPATIBLEVERSIONMIN, $headerArray['PSWS-Version']) == 1 ||
174  version_compare(PrestaShopWebservice::PSCOMPATIBLEVERSIONMAX, $headerArray['PSWS-Version']) == -1
175  ) {
176  throw new PrestaShopWebserviceException('This library is not compatible with this version of PrestaShop. Please upgrade/downgrade this library');
177  }
178  }
179 
180  if ($this->debug) {
181  $this->printDebug('HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT));
182  $this->printDebug('HTTP RESPONSE HEADER', $header);
183  }
184  $status_code = curl_getinfo($session, CURLINFO_HTTP_CODE);
185  if ($status_code === 0) {
186  throw new PrestaShopWebserviceException('CURL Error: '.curl_error($session));
187  }
188  curl_close($session);
189  if ($this->debug) {
190  if ($curl_params[CURLOPT_CUSTOMREQUEST] == 'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] == 'POST') {
191  $this->printDebug('XML SENT', urldecode($curl_params[CURLOPT_POSTFIELDS]));
192  }
193  if ($curl_params[CURLOPT_CUSTOMREQUEST] != 'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD') {
194  $this->printDebug('RETURN HTTP BODY', $body);
195  }
196  }
197  return array('status_code' => $status_code, 'response' => $body, 'header' => $header);
198  }
199 
207  public function printDebug($title, $content)
208  {
209  echo '<div style="display:table;background:#CCC;font-size:8pt;padding:7px"><h6 style="font-size:9pt;margin:0">'.dol_escape_htmltag($title).'</h6><pre>'.dol_escape_htmltag($content).'</pre></div>';
210  }
211 
217  public function getVersion()
218  {
219  return $this->version;
220  }
221 
230  protected function parseXML($response)
231  {
232  if ($response != '') {
233  libxml_clear_errors();
234  libxml_use_internal_errors(true);
235  if (!function_exists('simplexml_load_string')) {
236  throw new PrestaShopWebserviceException('Method simplexml_load_string not available. Your PHP does not support xml.');
237  }
238  $xml = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA|LIBXML_NONET);
239  if (libxml_get_errors()) {
240  $msg = var_export(libxml_get_errors(), true);
241  libxml_clear_errors();
242  throw new PrestaShopWebserviceException('HTTP XML response is not parsable: '.$msg);
243  }
244  return $xml;
245  } else {
246  throw new PrestaShopWebserviceException('HTTP response is empty');
247  }
248  }
249 
262  public function add($options)
263  {
264  $xml = '';
265  $url = '';
266 
267  if (isset($options['resource'], $options['postXml']) || isset($options['url'], $options['postXml'])) {
268  $url = (isset($options['resource']) ? $this->url.'/api/'.$options['resource'] : $options['url']);
269  $xml = $options['postXml'];
270  if (isset($options['id_shop'])) {
271  $url .= '&id_shop='.$options['id_shop'];
272  }
273  if (isset($options['id_group_shop'])) {
274  $url .= '&id_group_shop='.$options['id_group_shop'];
275  }
276  } else {
277  throw new PrestaShopWebserviceException('Bad parameters given');
278  }
279  $request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $xml));
280 
281  $this->checkStatusCode($request['status_code']);
282  return $this->parseXML($request['response']);
283  }
284 
315  public function get($options)
316  {
317  if (isset($options['url'])) {
318  $url = $options['url'];
319  } elseif (isset($options['resource'])) {
320  $url = $this->url.'/api/'.$options['resource'];
321  $url_params = array();
322  if (isset($options['id'])) {
323  $url .= '/'.$options['id'];
324  }
325 
326  // @CHANGE LDR
327  //$params = array('filter', 'display', 'sort', 'limit', 'id_shop', 'id_group_shop');
328  $params = array('filter', 'display', 'sort', 'limit', 'id_shop', 'id_group_shop', 'date');
329  foreach ($params as $p) {
330  foreach ($options as $k => $o) {
331  if (strpos($k, $p) !== false) {
332  $url_params[$k] = $options[$k];
333  }
334  }
335  }
336  if (count($url_params) > 0) {
337  $url .= '?'.http_build_query($url_params);
338  }
339  } else {
340  throw new PrestaShopWebserviceException('Bad parameters given ');
341  }
342 
343  $request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'GET'));
344  $this->checkStatusCode($request['status_code']); // check the response validity
345  return $this->parseXML($request['response']);
346  }
347 
356  public function head($options)
357  {
358  if (isset($options['url'])) {
359  $url = $options['url'];
360  } elseif (isset($options['resource'])) {
361  $url = $this->url.'/api/'.$options['resource'];
362  $url_params = array();
363  if (isset($options['id'])) {
364  $url .= '/'.$options['id'];
365  }
366 
367  $params = array('filter', 'display', 'sort', 'limit');
368  foreach ($params as $p) {
369  foreach ($options as $k => $o) {
370  if (strpos($k, $p) !== false) {
371  $url_params[$k] = $options[$k];
372  }
373  }
374  }
375  if (count($url_params) > 0) {
376  $url .= '?'.http_build_query($url_params);
377  }
378  } else {
379  throw new PrestaShopWebserviceException('Bad parameters given');
380  }
381  $request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'HEAD', CURLOPT_NOBODY => true));
382  $this->checkStatusCode($request['status_code']); // check the response validity
383  return $request['header'];
384  }
398  public function edit($options)
399  {
400  $xml = '';
401  if (isset($options['url'])) {
402  $url = $options['url'];
403  } elseif ((isset($options['resource'], $options['id']) || isset($options['url'])) && $options['putXml']) {
404  $url = (isset($options['url']) ? $options['url'] : $this->url.'/api/'.$options['resource'].'/'.$options['id']);
405  $xml = $options['putXml'];
406  if (isset($options['id_shop'])) {
407  $url .= '&id_shop='.$options['id_shop'];
408  }
409  if (isset($options['id_group_shop'])) {
410  $url .= '&id_group_shop='.$options['id_group_shop'];
411  }
412  } else {
413  throw new PrestaShopWebserviceException('Bad parameters given');
414  }
415 
416  $request = $this->executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_POSTFIELDS => $xml));
417  $this->checkStatusCode($request['status_code']); // check the response validity
418  return $this->parseXML($request['response']);
419  }
420 }
421 
426 {
427 
428 }
add($options)
Add (POST) a resource.
printDebug($title, $content)
Output debug info.
edit($options)
Edit (PUT) a resource.
parseXML($response)
Load XML from string.
head($options)
Head method (HEAD) a resource.
__construct($url, $key, $debug=true)
PrestaShopWebservice constructor.
checkStatusCode($status_code)
Take the status code and throw an exception if the server didn't return 200 or 201 code.
executeRequest($url, $curl_params=array())
Handles a CURL request to PrestaShop Webservice.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.