Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cpanel/Parser/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ public function parse($str)
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)) {
$this->_hasParseError = true;
Expand Down
1 change: 1 addition & 0 deletions Cpanel/PublicAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @version 0.1.0
* @link http://sdk.cpanel.net
* @since 0.1.0
*
*/
class Cpanel_PublicAPI extends Cpanel_Core_Object
{
Expand Down
26 changes: 15 additions & 11 deletions Cpanel/Query/Http/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,10 @@ public function buildAuthStr()
return 'Authorization: WHM '
. $this->user
. ':'
. $this->auth
. "\r\n";
. $this->auth;
} elseif ($this->auth_type == 'pass') {
return 'Authorization: Basic '
. base64_encode($this->user . ':' . $this->auth)
. "\r\n";
. base64_encode($this->user . ':' . $this->auth);
} else {
THROW new Exception('invalid auth_type set');
}
Expand Down Expand Up @@ -748,6 +746,7 @@ public function curlQuery($rObj)
curl_setopt($curl, CURLOPT_BUFFERSIZE, 131072);
// Pass authentication header
$curlHeader = $this->buildCurlHeaders($curl, $rObj);

$url = $rObj->query->url;
// Set the URL
curl_setopt($curl, CURLOPT_URL, $url);
Expand All @@ -760,7 +759,9 @@ public function curlQuery($rObj)
. "\tAUTH: {$rObj->query->authstr}\n"
);
}

$result = $this->curlExec($curl);

curl_close($curl);
return $result;
}
Expand All @@ -782,6 +783,7 @@ protected function curlExec($curl)
throw new Exception('Invalid cURL resource');
}
$result = curl_exec($curl);

if ($result === false) {
$msg = 'curl_exec threw error "' . curl_error($curl) . '"';
$rObj = $this->getResponseObject();
Expand All @@ -801,21 +803,20 @@ protected function curlExec($curl)
* Update cURL resource POST field data and return a header string
*
* @param resource $curl cURL resource to update
* @param string $headerStr Previous built header string
* @param string $postdata URL query parameter string to append to header
* string
*
* @return string Appended header string
* @throws Exception If $curl is not a resource
*/
protected function addCurlPostFields($curl, $headerStr, $postdata)
protected function addCurlPostFields($curl, $postdata)
{
if (!is_resource($curl)) {
throw new Exception('Invalid cURL resource');
}

curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
return $headerStr . "\r\n" . $postdata;;
}
/**
* Build a cURL header array, updating cURL POST field data as necessasry
Expand Down Expand Up @@ -847,20 +848,23 @@ protected function buildCurlHeaders($curl, $rObj)
$customHeaders = $h->getAllDataRecursively();
$headers = $customHeaders + $headers;
}

$curlHeader[0] = $authstr;

foreach ($headers as $key => $value) {
$headerStrs[] = "{$key}: {$value}";
$curlHeader[] = "{$key}: {$value}";
}
$curlHeader[0] = $authstr . implode("\r\n", $headerStrs);

$qt = $queryObj->httpQueryType;
if ($qt && strtoupper($qt) == 'GET') {
$queryObj->url = "{$queryObj->url}?{$postdata}";
} else {
$curlHeader[0] = $this->addCurlPostFields(
$this->addCurlPostFields(
$curl,
$curlHeader[0],
$postdata
);
}

return $curlHeader;
}
/**
Expand Down
3 changes: 2 additions & 1 deletion Cpanel/Service/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ protected function arrayType($arr)
);
}
ksort($arr);
$fkey = array_shift(array_keys($arr));
$arrKeys = array_keys($arr);
$fkey = array_shift($arrKeys);
if (is_int($fkey)) {
return self::API1ARGS;
}
Expand Down
30 changes: 22 additions & 8 deletions Cpanel/Service/Adapter/WHMapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,26 @@ class Cpanel_Service_Adapter_WHMapi extends Cpanel_Service_XmlapiClientClass
'JSON',
'XML'
);

/**
* 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)
Expand All @@ -99,7 +101,19 @@ 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 protocol stream wrapper registered');
}
// @codeCoverageIgnoreEnd

$this->setPort($port);
$RFT = ($RFT) ? $RFT : self::DRFT;
$this->setAdapterResponseFormatType($RFT);
return $this;
Expand Down
1 change: 0 additions & 1 deletion Cpanel/Tests/Cpanel/Core/ObjectTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
require_once 'PHPUnit/Autoload.php';
/**
* Basic test case for the Cpanel_Core_Object class
* @author davidneimeyer
Expand Down
1 change: 1 addition & 0 deletions Cpanel/Tests/Cpanel/Parser/JSONTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
require 'Cpanel/Util/Autoload.php';
/**
* @covers Cpanel_Parser_JSON
* @author davidneimeyer
Expand Down
7 changes: 5 additions & 2 deletions Cpanel/Tests/Cpanel/Parser/XMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ public function testEncodeQueryObjectThrowOnBadEncode()
);
$rObj = $this->getRObj();
$rObj->setResponse($data);
$r = $p->encodeQueryObject($rObj);

$r = $p->encodeQueryObject(serialize($rObj));
}
public function testEncodeQueryObjectCallsGetResponseAndEncodes()
{
Expand All @@ -483,7 +484,9 @@ public function testEncodeQueryObjectCallsGetResponseAndEncodes()
));
$rObj->expects($this->once())->method('getResponse')->with('array')->will($this->returnValue($this->getArrayListPopsWithDisk()));
$expected = $this->getXMLListPopsWithDisk();

$actual = $p->encodeQueryObject($rObj);

$this->assertEquals($expected, $actual);
}
public function testEncodeQueryObjectCallsGetResponseAndEncodesSmall()
Expand Down Expand Up @@ -682,7 +685,7 @@ public function getXMLListPopsWithDisk()
<apiversion>2</apiversion>
<data>
<_diskquota>262144000</_diskquota>
<_diskused></_diskused>
<_diskused/>
<diskquota>250</diskquota>
<txtdiskquota>250</txtdiskquota>
<user>auththis</user>
Expand Down
1 change: 0 additions & 1 deletion Cpanel/Tests/Cpanel/PublicAPITest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
require_once 'PHPUnit/Autoload.php';
/**
* Basic test case for the Cpanel class
* @author davidneimeyer
Expand Down
26 changes: 13 additions & 13 deletions Cpanel/Tests/Cpanel/Query/Http/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ public function testBuildAuthStrReturnsHashAuthStr()
$rprop = new ReflectionProperty($this->cut, 'user');
$rprop->setAccessible(true);
$rprop->setValue($rq, $user);
$expected = "Authorization: WHM ${user}:${auth}\r\n";
$expected = "Authorization: WHM ${user}:${auth}";
$actual = $rq->buildAuthStr();
$this->assertEquals($expected, $actual);
}
Expand All @@ -996,7 +996,7 @@ public function testBuildAuthStrReturnsPasswordAuthStr()
$rprop = new ReflectionProperty($this->cut, 'user');
$rprop->setAccessible(true);
$rprop->setValue($rq, $user);
$expected = "Authorization: Basic " . base64_encode($user . ':' . $auth) . "\r\n";
$expected = "Authorization: Basic " . base64_encode($user . ':' . $auth);
$actual = $rq->buildAuthStr();
$this->assertEquals($expected, $actual);
}
Expand Down Expand Up @@ -1310,12 +1310,12 @@ public function testAddCurlPostFieldsParams()
$params = $rmeth->getParameters();
$expected = array(
0 => 'curl',
1 => 'headerStr',
2 => 'postdata'
1 => 'postdata'
);
foreach ($params as $param) {
$actual[$param->getPosition() ] = $param->getName();
}

$this->assertEquals($expected, $actual);
}
/**
Expand All @@ -1333,8 +1333,9 @@ public function testAddCurlPostFieldsReturnsString()
$rq = $this->getRQ();
$rmeth = new ReflectionMethod($this->cut, 'addCurlPostFields');
$rmeth->setAccessible(true);
$actual = $rmeth->invoke($rq, curl_init(), '', 'what=ever');
$this->assertInternalType('string', $actual);
$actual = $rmeth->invoke($rq, curl_init(), 'what=ever');

$this->assertInternalType('null', $actual);
}
/**
* @expectedException Exception
Expand Down Expand Up @@ -1403,15 +1404,13 @@ public function testBuildCurlHeadersDoesNotAlterURLForPOST()
$actual = $rmeth->invoke($rq, $curl, $rObj);
$this->assertEquals($url, $rObj->query->url);
}
/**
* @depends testAddCurlPostFieldsReturnsString
*/

public function testBuildCurlHeadersCallsAddCurlPostFields()
{
$user = 'foo';
$pass = 'bar';
$postdata = 'what=ever';
$authstr = 'Authentication: Basic ' . base64_encode($user . ':' . $pass) . "\r\n";
$authstr = 'Authentication: Basic ' . base64_encode($user . ':' . $pass);
$customHeaders = array(
'CustomHeader' => 'CustomHeaderValue'
);
Expand All @@ -1421,7 +1420,7 @@ public function testBuildCurlHeadersCallsAddCurlPostFields()
$rq = $this->getRQ(array(
'addCurlPostFields'
));
$rq->expects($this->once())->method('addCurlPostFields')->with($curl, $this->anything(), $postdata)->will($this->returnValue($this->anything()));

$rObj = $this->getRObj();
// $rObj->setQuery($customHeaders);
$rObj->query->authstr = $authstr;
Expand All @@ -1438,7 +1437,7 @@ public function testBuildCurlHeadersAddsCustomHeaders()
$user = 'foo';
$pass = 'bar';
$postdata = 'what=ever';
$authstr = 'Authentication: Basic ' . base64_encode($user . ':' . $pass) . "\r\n";
$authstr = 'Authentication: Basic ' . base64_encode($user . ':' . $pass);
$customHeaders = array(
'CustomHeader' => 'CustomHeaderValue'
);
Expand All @@ -1454,7 +1453,8 @@ public function testBuildCurlHeadersAddsCustomHeaders()
$rmeth = new ReflectionMethod($this->cut, 'buildCurlHeaders');
$rmeth->setAccessible(true);
$actual = $rmeth->invoke($rq, $curl, $rObj);
$containsHeaders = (bool)strpos($actual[0], 'CustomHeader: CustomHeaderValue');

$containsHeaders = ($actual[1] == 'CustomHeader: CustomHeaderValue');
$this->assertTrue($containsHeaders);
}
public function testBuildCurlHeadersAltersURLForGET()
Expand Down
7 changes: 5 additions & 2 deletions Cpanel/Tests/Cpanel/Query/Live/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ public static function startMSS()
if (!file_exists($mockserverscript)) {
self::fail("Mock socket server script '$mockserverscript' does not exist");
}
$cmd = "/usr/bin/php -f $mockserverscript";

$phpPath = exec('which php');
$cmd = "$phpPath -f $mockserverscript"; //original

$arg = "socketfile={$socketfile}";
$full_cmd = "nohup $cmd $arg > /dev/null 2>&1 & echo $!"; // > /dev/null
$PID = exec($full_cmd);
Expand Down Expand Up @@ -369,7 +372,7 @@ public function testOpenCpanelHandleThrowsOnBadSocketfile()
* looks like this test passed due to warn by fsockopen, and not the scripted
* exception throw
* @todo consider reworking or removing; might be untestable
* @expectedException Exception
* @expectedException PHPUnit_Framework_Error_Warning
*/
public function testOpenCpanelHandleThrowsOnBadfsockopen()
{
Expand Down
Loading