From 381f08364a0e6a7efeb7acfb9f38dde74a37c680 Mon Sep 17 00:00:00 2001 From: gggeek Date: Thu, 12 Jan 2023 17:50:45 +0000 Subject: [PATCH] allow more injection of custom loggers --- src/Helper/Charset.php | 21 ++++++++++++++++++++- src/Helper/Http.php | 35 +++++++++++++++++++++++++++-------- src/Helper/XMLParser.php | 34 ++++++++++++++++++++++++++-------- 3 files changed, 73 insertions(+), 17 deletions(-) diff --git a/src/Helper/Charset.php b/src/Helper/Charset.php index 0e2196e2..43edff8d 100644 --- a/src/Helper/Charset.php +++ b/src/Helper/Charset.php @@ -9,6 +9,8 @@ use PhpXmlRpc\PhpXmlRpc; */ class Charset { + protected static $logger; + // tables used for transcoding different charsets into us-ascii xml protected $xml_iso88591_Entities = array("in" => array(), "out" => array()); @@ -41,6 +43,23 @@ class Charset return self::$instance; } + public function getLogger() + { + if (self::$logger === null) { + self::$logger = Logger::instance(); + } + return self::$logger; + } + + /** + * @param $logger + * @return void + */ + public static function setLogger($logger) + { + self::$logger = $logger; + } + /** * Force usage as singleton. */ @@ -273,7 +292,7 @@ class Charset default: $escapedData = ''; /// @todo allow usage of a custom Logger via the DIC(ish) pattern we use in other classes - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ": Converting from $srcEncoding to $destEncoding: not supported..."); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ": Converting from $srcEncoding to $destEncoding: not supported..."); } return $escapedData; diff --git a/src/Helper/Http.php b/src/Helper/Http.php index ca0f4ae9..eb024cf3 100644 --- a/src/Helper/Http.php +++ b/src/Helper/Http.php @@ -11,6 +11,25 @@ use PhpXmlRpc\PhpXmlRpc; */ class Http { + protected static $logger; + + public function getLogger() + { + if (self::$logger === null) { + self::$logger = Logger::instance(); + } + return self::$logger; + } + + /** + * @param $logger + * @return void + */ + public static function setLogger($logger) + { + self::$logger = $logger; + } + /** * Decode a string that is encoded with "chunked" transfer encoding as defined in rfc2068 par. 19.4.6. * Code shamelessly stolen from nusoap library by Dietrich Ayala. @@ -99,7 +118,7 @@ class Http // this filters out all http headers from proxy. maybe we could take them into account, too? $data = substr($data, $bd); } else { - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': HTTPS via proxy error, tunnel connection possibly failed'); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': HTTPS via proxy error, tunnel connection possibly failed'); throw new HttpException(PhpXmlRpc::$xmlrpcstr['http_error'] . ' (HTTPS via proxy error, tunnel connection possibly failed)', PhpXmlRpc::$xmlrpcerr['http_error']); } } @@ -134,7 +153,7 @@ class Http if ($httpResponse['status_code'] !== '200') { $errstr = substr($data, 0, strpos($data, "\n") - 1); - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': HTTP error, got response: ' . $errstr); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': HTTP error, got response: ' . $errstr); throw new HttpException(PhpXmlRpc::$xmlrpcstr['http_error'] . ' (' . $errstr . ')', PhpXmlRpc::$xmlrpcerr['http_error'], null, $httpResponse['status_code']); } @@ -219,7 +238,7 @@ class Http foreach ($httpResponse['cookies'] as $header => $value) { $msg .= "COOKIE: $header={$value['value']}\n"; } - Logger::instance()->debugMessage($msg); + $this->getLogger()->debugMessage($msg); } // if CURL was used for the call, http headers have been processed, and dechunking + reinflating have been carried out @@ -228,7 +247,7 @@ class Http // Decode chunked encoding sent by http 1.1 servers if (isset($httpResponse['headers']['transfer-encoding']) && $httpResponse['headers']['transfer-encoding'] == 'chunked') { if (!$data = static::decodeChunked($data)) { - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to rebuild the chunked data received from server'); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to rebuild the chunked data received from server'); throw new HttpException(PhpXmlRpc::$xmlrpcstr['dechunk_fail'], PhpXmlRpc::$xmlrpcerr['dechunk_fail'], null, $httpResponse['status_code']); } } @@ -243,19 +262,19 @@ class Http if ($httpResponse['headers']['content-encoding'] == 'deflate' && $degzdata = @gzuncompress($data)) { $data = $degzdata; if ($debug) { - Logger::instance()->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); + $this->getLogger()->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); } } elseif ($httpResponse['headers']['content-encoding'] == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) { $data = $degzdata; if ($debug) { - Logger::instance()->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); + $this->getLogger()->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); } } else { - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to decode the deflated data received from server'); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to decode the deflated data received from server'); throw new HttpException(PhpXmlRpc::$xmlrpcstr['decompress_fail'], PhpXmlRpc::$xmlrpcerr['decompress_fail'], null, $httpResponse['status_code']); } } else { - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': the server sent deflated data. Your php install must have the Zlib extension compiled in to support this.'); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': the server sent deflated data. Your php install must have the Zlib extension compiled in to support this.'); throw new HttpException(PhpXmlRpc::$xmlrpcstr['cannot_decompress'], PhpXmlRpc::$xmlrpcerr['cannot_decompress'], null, $httpResponse['status_code']); } } diff --git a/src/Helper/XMLParser.php b/src/Helper/XMLParser.php index 375424cf..6da8ef9a 100644 --- a/src/Helper/XMLParser.php +++ b/src/Helper/XMLParser.php @@ -27,6 +27,8 @@ class XMLParser const ACCEPT_VALUE = 4; const ACCEPT_FAULT = 8; + protected static $logger; + // Used to store state during parsing and to pass parsing results to callers. // Quick explanation of components: // private: @@ -86,6 +88,23 @@ class XMLParser /** @var int $maxChunkLength 4 MB by default. Any value below 10MB should be good */ protected $maxChunkLength = 4194304; + public function getLogger() + { + if (self::$logger === null) { + self::$logger = Logger::instance(); + } + return self::$logger; + } + + /** + * @param $logger + * @return void + */ + public static function setLogger($logger) + { + self::$logger = $logger; + } + /** * @param array $options passed to the xml parser */ @@ -432,7 +451,7 @@ class XMLParser $this->_xh['value'] = $this->_xh['ac']; } elseif ($name == 'DATETIME.ISO8601') { if (!preg_match('/^[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $this->_xh['ac'])) { - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid value received in DATETIME: ' . $this->_xh['ac']); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid value received in DATETIME: ' . $this->_xh['ac']); } $this->_xh['vt'] = Value::$xmlrpcDateTime; $this->_xh['value'] = $this->_xh['ac']; @@ -451,7 +470,7 @@ class XMLParser } else { // log if receiving something strange, even though we set the value to false anyway if ($this->_xh['ac'] != '0' && strcasecmp($this->_xh['ac'], 'false') != 0) { - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid value received in BOOLEAN: ' . $this->_xh['ac']); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': invalid value received in BOOLEAN: ' . $this->_xh['ac']); } $this->_xh['value'] = false; } @@ -461,7 +480,7 @@ class XMLParser // NOTE: regexp could be much stricter than this... if (!preg_match('/^[+-eE0123456789 \t.]+$/', $this->_xh['ac'])) { /// @todo: find a better way of throwing an error than this! - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': non numeric value received in DOUBLE: ' . $this->_xh['ac']); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': non numeric value received in DOUBLE: ' . $this->_xh['ac']); $this->_xh['value'] = 'ERROR_NON_NUMERIC_FOUND'; } else { // it's ok, add it on @@ -472,7 +491,7 @@ class XMLParser // we must check that only 0123456789- are characters here if (!preg_match('/^[+-]?[0123456789 \t]+$/', $this->_xh['ac'])) { /// @todo find a better way of throwing an error than this! - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': non numeric value received in INT: ' . $this->_xh['ac']); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': non numeric value received in INT: ' . $this->_xh['ac']); $this->_xh['value'] = 'ERROR_NON_NUMERIC_FOUND'; } else { // it's ok, add it on @@ -491,7 +510,7 @@ class XMLParser $vscount = count($this->_xh['valuestack']); $this->_xh['valuestack'][$vscount - 1]['values'][$this->_xh['valuestack'][$vscount - 1]['name']] = $this->_xh['value']; } else { - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': missing VALUE inside STRUCT in received xml'); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': missing VALUE inside STRUCT in received xml'); } break; case 'DATA': @@ -514,7 +533,7 @@ class XMLParser $this->_xh['params'][] = $this->_xh['value']; $this->_xh['pt'][] = $this->_xh['vt']; } else { - Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ': missing VALUE inside PARAM in received xml'); + $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': missing VALUE inside PARAM in received xml'); } break; case 'METHODNAME': @@ -681,8 +700,7 @@ class XMLParser } // 4 - if mbstring is available, let it do the guesswork - /// @todo replace with function_exists - if (extension_loaded('mbstring')) { + if (function_exists('mb_detect_encoding')) { if ($encodingPrefs == null && PhpXmlRpc::$xmlrpc_detectencodings != null) { $encodingPrefs = PhpXmlRpc::$xmlrpc_detectencodings; } -- 2.47.0