From 8bead1af23ecf03f3d4e33b69c1dd936787ea5fe Mon Sep 17 00:00:00 2001 From: aKorczak Date: Fri, 31 Aug 2012 09:41:22 +0000 Subject: [PATCH] [-] WS : Multiple fix on Web Services --- classes/Autoload.php | 2 +- classes/ObjectModel.php | 12 +++++-- classes/db/Db.php | 5 ++- .../webservice/WebserviceOutputBuilder.php | 7 ++-- classes/webservice/WebserviceRequest.php | 35 ++++++++++++++++--- 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/classes/Autoload.php b/classes/Autoload.php index f95399920..5e1e379a8 100644 --- a/classes/Autoload.php +++ b/classes/Autoload.php @@ -177,7 +177,7 @@ class Autoload $classes = array_merge($classes, $this->getClassesFromDir($path.$file.'/')); else if (substr($file, -4) == '.php') { - $content = file_get_contents($this->root_dir.$path.$file); + $content = file_get_contents($this->root_dir.$path.$file); $pattern = '#\W((abstract\s+)?class|interface)\s+(?P'.basename($file, '.php').'(Core)?)' .'(\s+extends\s+[a-z][a-z0-9_]*)?(\s+implements\s+[a-z][a-z0-9_]*(\s*,\s*[a-z][a-z0-9_]*)*)?\s*\{#i'; if (preg_match($pattern, $content, $m)) diff --git a/classes/ObjectModel.php b/classes/ObjectModel.php index aac95ad72..4a51d5b46 100644 --- a/classes/ObjectModel.php +++ b/classes/ObjectModel.php @@ -1037,7 +1037,10 @@ abstract class ObjectModelCore ON (main.`'.bqSQL($this->def['primary']).'` = `multi_shop_'.bqSQL($this->def['table']).'`.`'.bqSQL($this->def['primary']).'`)'; foreach ($vars['shopIDs'] as $id_shop) $or[] = ' `multi_shop_'.bqSQL($this->def['table']).'`.id_shop = '.(int)$id_shop.' '; - $sql_filter = ' AND ('.implode('OR', $or).') '.$sql_filter; + $prepend = ''; + if (count($or)) + $prepend = 'AND ('.implode('OR', $or).')'; + $sql_filter = $prepend.$sql_filter; $sql_join = $multi_shop_join.' '.$sql_join; } else @@ -1045,7 +1048,11 @@ abstract class ObjectModelCore $vars = get_class_vars($class_name); foreach ($vars['shopIDs'] as $id_shop) $or[] = ' main.id_shop = '.(int)$id_shop.' '; - $sql_filter = ' AND ('.implode('OR', $or).') '.$sql_filter; + + $prepend = ''; + if (count($or)) + $prepend = 'AND ('.implode('OR', $or).')'; + $sql_filter = $prepend.' '.$sql_filter; } } $query = ' @@ -1054,7 +1061,6 @@ abstract class ObjectModelCore WHERE 1 '.$sql_filter.' '.($sql_sort != '' ? $sql_sort : '').' '.($sql_limit != '' ? $sql_limit : ''); - return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); } diff --git a/classes/db/Db.php b/classes/db/Db.php index 49cf11ee9..ba6b659cf 100644 --- a/classes/db/Db.php +++ b/classes/db/Db.php @@ -596,7 +596,10 @@ abstract class DbCore $errno = $this->getNumberError(); if ($webservice_call && $errno) - WebserviceRequest::getInstance()->setError(500, '[SQL Error] '.$this->getMsgError().'. Query was : '.$sql, 97); + { + $dbg = debug_backtrace(); + WebserviceRequest::getInstance()->setError(500, '[SQL Error] '.$this->getMsgError().'. From '.(isset($dbg[3]['class']) ? $dbg[3]['class'] : '').'->'.$dbg[3]['function'].'() Query was : '.$sql, 97); + } else if (_PS_DEBUG_SQL_ && $errno && !defined('PS_INSTALLATION_IN_PROGRESS')) { if ($sql) diff --git a/classes/webservice/WebserviceOutputBuilder.php b/classes/webservice/WebserviceOutputBuilder.php index f149856c2..149ec5cea 100755 --- a/classes/webservice/WebserviceOutputBuilder.php +++ b/classes/webservice/WebserviceOutputBuilder.php @@ -188,6 +188,7 @@ class WebserviceOutputBuilderCore { return $this->status; } + public function getStatusInt() { return $this->statusInt; @@ -283,7 +284,7 @@ class WebserviceOutputBuilderCore if (is_null($this->wsResource)) throw new WebserviceException ('You must set web service resource for get the resources list.', array(82, 500)); $output = ''; - $more_attr = array('shop_name' => Configuration::get('PS_SHOP_NAME')); + $more_attr = array('shop_name' => htmlentities(Configuration::get('PS_SHOP_NAME'))); $output .= $this->objectRender->renderNodeHeader('api', array(), $more_attr); foreach ($this->wsResource as $resourceName => $resource) { @@ -675,7 +676,7 @@ class WebserviceOutputBuilderCore if (!is_null($this->schemaToDisplay)) $field['synopsis_details'] = $this->getSynopsisDetails($field); - + $field['is_association'] = true; $output .= $this->setIndent($depth-1).$this->objectRender->renderField($field); } } @@ -755,10 +756,12 @@ class WebserviceOutputBuilderCore $this->virtualFields[$entity_name][] = array('parameters' => $parameters, 'object' => $object, 'method' => $method, 'type' => gettype($object)); } + public function getVirtualFields() { return $this->virtualFields; } + public function addVirtualFields($entity_name, $entity_object) { $arr_return = array(); diff --git a/classes/webservice/WebserviceRequest.php b/classes/webservice/WebserviceRequest.php index ee122bd0e..e7228f080 100644 --- a/classes/webservice/WebserviceRequest.php +++ b/classes/webservice/WebserviceRequest.php @@ -392,6 +392,10 @@ class WebserviceRequestCore $this->_startTime = microtime(true); $this->objects = array(); + // Error handler + set_error_handler(array($this, 'webserviceErrorHandler')); + ini_set('html_errors', 'off'); + // Two global vars, for compatibility with the PS core... global $webservice_call, $display_errors; $webservice_call = true; @@ -401,9 +405,7 @@ class WebserviceRequestCore // set the output object which manage the content and header structure and informations $this->objOutput = new WebserviceOutputBuilder($this->wsUrl); - // Error handler - set_error_handler(array($this, 'webserviceErrorHandler')); - ini_set('html_errors', 'off'); + $this->_key = trim($key); @@ -545,7 +547,10 @@ class WebserviceRequestCore public function setError($status, $label, $code) { global $display_errors; - $this->objOutput->setStatus($status); + if (!isset($display_errors)) + $display_errors = strtolower(ini_get('display_errors')) != 'off'; + if (isset($this->objOutput)) + $this->objOutput->setStatus($status); $this->errors[] = $display_errors ? array($code, $label) : 'Internal error. To see this error please display the PHP errors.'; } @@ -603,8 +608,28 @@ class WebserviceRequestCore */ public function webserviceErrorHandler($errno, $errstr, $errfile, $errline) { - if (!(error_reporting() & $errno)) + echo 'Error Handler WebserviceRequest'; + $display_errors = strtolower(ini_get('display_errors')) != 'off'; + if (!(error_reporting() & $errno) || $display_errors) return; + + $errortype = array ( + E_ERROR => 'Error', + E_WARNING => 'Warning', + E_PARSE => 'Parse', + E_NOTICE => 'Notice', + E_CORE_ERROR => 'Core Error', + E_CORE_WARNING => 'Core Warning', + E_COMPILE_ERROR => 'Compile Error', + E_COMPILE_WARNING => 'Compile Warning', + E_USER_ERROR => 'Error', + E_USER_WARNING => 'User warning', + E_USER_NOTICE => 'User notice', + E_STRICT => 'Runtime Notice', + E_RECOVERABLE_ERROR => 'Recoverable error' + ); + $type = (isset($errortype[$errno]) ? $errortype[$errno] : 'Unknown error'); + error_log('[PHP '.$type.' #'.$errno.'] '.$errstr.' ('.$errfile.', line '.$errline.')'); switch($errno) {