From 07c330fc94c03d0e2c99a84f4a71062895315213 Mon Sep 17 00:00:00 2001 From: Dmitry Lomakin Date: Tue, 26 Mar 2013 12:40:33 +0400 Subject: [PATCH 1/2] Use correct port for API communications (select the right one using the registered stream wrappers list) --- Cpanel/Service/Adapter/WHMapi.php | 57 +++++++++++++++++++------------ 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/Cpanel/Service/Adapter/WHMapi.php b/Cpanel/Service/Adapter/WHMapi.php index 463131b..1102466 100644 --- a/Cpanel/Service/Adapter/WHMapi.php +++ b/Cpanel/Service/Adapter/WHMapi.php @@ -69,22 +69,23 @@ class Cpanel_Service_Adapter_WHMapi extends Cpanel_Service_XmlapiClientClass ); /** * Constructor - * + * * Prepare the adapter for use by Service. - * + * * If $RFT is not passed the const Cpanel_Service_Adapter_WHMapi::DRFT will be - * used when invoking {@link setAdapterResponseFormatType()} at + * used when invoking {@link setAdapterResponseFormatType()} at * instantiation - * + * * HTTP port is set to '2087' by default. {@link setPort()} - * + * * NOTE: this constructor as support for the legacy PHP XML-API client class - * + * * @param string $host Host address for query call * @param string $user User to authenticate query call * @param string $password Password to authenticate query call * @param string $RFT Response format type - * + * + * @throws Exception in case of no valid stread wrapper is found * @return Cpanel_Service_Adapter_WHMapi */ public function __construct($host = null, $user = null, $password = null, $RFT = null) @@ -99,16 +100,28 @@ public function __construct($host = null, $user = null, $password = null, $RFT = if ($password) { $this->setPassword($password); } - $this->setPort('2087'); + + // @codeCoverageIgnoreStart + $registeredStreams = stream_get_wrappers(); + if (in_array('https', $registeredStreams)) { + $port = 2087; + } elseif (in_array('http', $registeredStreams)) { + $port = 2086; + } else { + throw new Exception('No valid stream wrapper registered'); + } + // @codeCoverageIgnoreEnd + + $this->setPort($port); $RFT = ($RFT) ? $RFT : self::DRFT; $this->setAdapterResponseFormatType($RFT); return $this; } /** * Return the current response format type - * + * * @see Cpanel_Query_Http_Abstract::getAdapterResponseFormatType() - * + * * @return string */ public function getAdapterResponseFormatType() @@ -117,11 +130,11 @@ public function getAdapterResponseFormatType() } /** * Set the response format type - * + * * @param string $type The response format type to set - * + * * @see Cpanel_Query_Http_Abstract::setAdapterResponseFormatType() - * + * * @return Cpanel_Service_Adapter_Cpanelapi * @throws Exception If an invalid RFT */ @@ -135,13 +148,13 @@ public function setAdapterResponseFormatType($type) } /** * Method for querying a native XML-API function - * + * * @param string $function XML-API function to invoke * @param array $args Arguments for $function - * + * * @see Cpanel_Query_Http_Abstract::xmlapi_query() * @link http://docs.cpanel.net/twiki/bin/vief/AllDocumentation/AutomationIntegration/XmlApi#Functions XML-API Funcitons - * + * * @return Cpanel_Query_Object */ public function xmlapi_query($function, $args = array()) @@ -150,16 +163,16 @@ public function xmlapi_query($function, $args = array()) } /** * Method for querying API1 via XML-API - * + * * @param string $user User to query against * @param string $module API1 module to source * @param string $func API1 function of $module to invoke * @param array $args Arguments for $function - * + * * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CallingAPIFunctions Calling API Functions * @link http://docs.cpanel.net/twiki/bin/view/ApiDocs/Api1/WebHome API1 Modules * @see Cpanel_Query_Http_Abstract::api1_query() - * + * * @return Cpanel_Query_Object * @throws Exception If $user, $module, or $func are not defined * @throws Exception If $args is not an array @@ -199,16 +212,16 @@ public function api1_query($user, $module, $func, $args = array()) } /** * Method for querying API2 via XML-API - * + * * @param string $user User to query against * @param string $module API2 module to source * @param string $func API2 function of $module to invoke * @param array $args Arguments for $function - * + * * @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CallingAPIFunctions Calling API Functions * @link http://docs.cpanel.net/twiki/bin/view/ApiDocs/Api2/WebHome API2 Modules * @see Cpanel_Query_Http_Abstract::api2_query() - * + * * @return Cpanel_Query_Object * @throws Exception If $user, $module, or $func are not defined * @throws Exception If $args is not an array From 2da8a6645c7f11e34982699def29c36d2f36d0c0 Mon Sep 17 00:00:00 2001 From: Dmitry Lomakin Date: Wed, 7 Aug 2013 15:46:40 +0400 Subject: [PATCH 2/2] JSON-API responses made safe to avoid JSON_ERROR_UTF8 errors --- Cpanel/Parser/JSON.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Cpanel/Parser/JSON.php b/Cpanel/Parser/JSON.php index a592307..945db4e 100644 --- a/Cpanel/Parser/JSON.php +++ b/Cpanel/Parser/JSON.php @@ -133,6 +133,9 @@ public function parse($str) return $this->getParserInternalErrors( self::ERROR_DECODE, 'Cannot decode empty string.' ); + } + if (function_exists('iconv')) { + $str = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($str)); } $r = json_decode($str, true); if (is_null($r)) {