Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated PHP SDK for cPanel API Tokens #16

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.phpunit.result.cache
**/error_log
19 changes: 12 additions & 7 deletions Cpanel/Core/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cpanel_Core_Object
*
* Copyright (c) 2011, cPanel, Inc.
* Copyright (c) 2011, cPanel, L.L.C.
* All rights reserved.
* http://cpanel.net
*
Expand All @@ -13,7 +13,7 @@
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of cPanel, Inc. nor the
* * Neither the name of cPanel, L.L.C. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
Expand All @@ -31,9 +31,9 @@
* @category Cpanel
* @package Cpanel_Core
* @author David Neimeyer <[email protected]>
* @copyright Copyright (c) 2011, cPanel, Inc., All rights Reserved. (http://cpanel.net)
* @copyright Copyright (c) 2011, cPanel, L.L.C., All rights Reserved. (http://cpanel.net)
* @license http://sdk.cpanel.net/license/bsd.html BSD License
* @version 0.1.0
* @version 0.2.0
* @link http://sdk.cpanel.net
* @since 0.1.0
*/
Expand All @@ -48,13 +48,13 @@
* @category Cpanel
* @package Cpanel_Core
* @author David Neimeyer <[email protected]>
* @copyright Copyright (c) 2011, cPanel, Inc., All rights Reserved. (http://cpanel.net)
* @copyright Copyright (c) 2011, cPanel, L.L.C., All rights Reserved. (http://cpanel.net)
* @license http://sdk.cpanel.net/license/bsd.html BSD License
* @version 0.1.0
* @version 0.2.0
* @link http://sdk.cpanel.net
* @since 0.1.0
*/
class Cpanel_Core_Object implements IteratorAggregate
class Cpanel_Core_Object implements IteratorAggregate, Countable
{
/**
* internal storage location
Expand Down Expand Up @@ -83,6 +83,11 @@ public function getIterator()
{
return new ArrayIterator($this->dataContainer);
}

public function count() {
return $this->getIterator()->count();
}

/**
* Accessor for storing data internally
*
Expand Down
12 changes: 6 additions & 6 deletions Cpanel/Core/PriorityQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cpanel_Core_PriorityQueue
*
* Copyright (c) 2011, cPanel, Inc.
* Copyright (c) 2011, cPanel, L.L.C.
* All rights reserved.
* http://cpanel.net
*
Expand All @@ -13,7 +13,7 @@
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of cPanel, Inc. nor the
* * Neither the name of cPanel, L.L.C. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
Expand All @@ -31,9 +31,9 @@
* @category Cpanel
* @package Cpanel_Core
* @author David Neimeyer <[email protected]>
* @copyright Copyright (c) 2011, cPanel, Inc., All rights Reserved. (http://cpanel.net)
* @copyright Copyright (c) 2011, cPanel, L.L.C., All rights Reserved. (http://cpanel.net)
* @license http://sdk.cpanel.net/license/bsd.html BSD License
* @version 0.1.0
* @version 0.2.0
* @link http://sdk.cpanel.net
* @since 0.1.0
*/
Expand All @@ -48,9 +48,9 @@
* @category Cpanel
* @package Cpanel_Core
* @author David Neimeyer <[email protected]>
* @copyright Copyright (c) 2011, cPanel, Inc., All rights Reserved. (http://cpanel.net)
* @copyright Copyright (c) 2011, cPanel, L.L.C., All rights Reserved. (http://cpanel.net)
* @license http://sdk.cpanel.net/license/bsd.html BSD License
* @version 0.1.0
* @version 0.2.0
* @link http://sdk.cpanel.net
* @since 0.1.0
*/
Expand Down
8 changes: 4 additions & 4 deletions Cpanel/Examples/Accessing_Response_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Example usage of iterating over a response (Cpanel_Query_Object)
*
* Copyright (c) 2011, cPanel, Inc.
* Copyright (c) 2011, cPanel, L.L.C.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t this say 2019?

* All rights reserved.
* http://cpanel.net
*
Expand All @@ -13,7 +13,7 @@
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of cPanel, Inc. nor the
* * Neither the name of cPanel, L.L.C. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
Expand All @@ -31,9 +31,9 @@
* @category Cpanel
* @package Cpanel_PublicAPI
* @author David Neimeyer <[email protected]>
* @copyright Copyright (c) 2011, cPanel, Inc., All rights Reserved. (http://cpanel.net)
* @copyright Copyright (c) 2011, cPanel, L.L.C., All rights Reserved. (http://cpanel.net)
* @license http://sdk.cpanel.net/license/bsd.html BSD License
* @version 0.1.0
* @version 0.2.0
* @link http://sdk.cpanel.net
* @since 0.1.0
*/
Expand Down
8 changes: 4 additions & 4 deletions Cpanel/Examples/Basic_WHM_Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Example usage of WHM Service
*
* Copyright (c) 2011, cPanel, Inc.
* Copyright (c) 2011, cPanel, L.L.C.
* All rights reserved.
* http://cpanel.net
*
Expand All @@ -13,7 +13,7 @@
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of cPanel, Inc. nor the
* * Neither the name of cPanel, L.L.C. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
Expand All @@ -31,9 +31,9 @@
* @category Cpanel
* @package Cpanel_PublicAPI
* @author David Neimeyer <[email protected]>
* @copyright Copyright (c) 2011, cPanel, Inc., All rights Reserved. (http://cpanel.net)
* @copyright Copyright (c) 2011, cPanel, L.L.C., All rights Reserved. (http://cpanel.net)
* @license http://sdk.cpanel.net/license/bsd.html BSD License
* @version 0.1.0
* @version 0.2.0
* @link http://sdk.cpanel.net
* @since 0.1.0
*/
Expand Down
8 changes: 4 additions & 4 deletions Cpanel/Examples/Basic_cPanel_Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Example usage of cPanel Service
*
* Copyright (c) 2011, cPanel, Inc.
* Copyright (c) 2011, cPanel, L.L.C.
* All rights reserved.
* http://cpanel.net
*
Expand All @@ -13,7 +13,7 @@
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of cPanel, Inc. nor the
* * Neither the name of cPanel, L.L.C. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
Expand All @@ -31,9 +31,9 @@
* @category Cpanel
* @package Cpanel_PublicAPI
* @author David Neimeyer <[email protected]>
* @copyright Copyright (c) 2011, cPanel, Inc., All rights Reserved. (http://cpanel.net)
* @copyright Copyright (c) 2011, cPanel, L.L.C., All rights Reserved. (http://cpanel.net)
* @license http://sdk.cpanel.net/license/bsd.html BSD License
* @version 0.1.0
* @version 0.2.0
* @link http://sdk.cpanel.net
* @since 0.1.0
*/
Expand Down
8 changes: 4 additions & 4 deletions Cpanel/Examples/Introduction_to_PublicAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Introduction to PublicAPI and the cPanel PHP library
*
* Copyright (c) 2011, cPanel, Inc.
* Copyright (c) 2011, cPanel, L.L.C.
* All rights reserved.
* http://cpanel.net
*
Expand All @@ -13,7 +13,7 @@
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of cPanel, Inc. nor the
* * Neither the name of cPanel, L.L.C. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
Expand All @@ -31,9 +31,9 @@
* @category Cpanel
* @package Cpanel_PublicAPI
* @author David Neimeyer <[email protected]>
* @copyright Copyright (c) 2011, cPanel, Inc., All rights Reserved. (http://cpanel.net)
* @copyright Copyright (c) 2011, cPanel, L.L.C., All rights Reserved. (http://cpanel.net)
* @license http://sdk.cpanel.net/license/bsd.html BSD License
* @version 0.1.0
* @version 0.2.0
* @link http://sdk.cpanel.net
* @since 0.1.0
*/
Expand Down
8 changes: 4 additions & 4 deletions Cpanel/Examples/PublicAPI_DirectURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Example usage of Direct URL query via PublicAPI client
*
* Copyright (c) 2011, cPanel, Inc.
* Copyright (c) 2011, cPanel, L.L.C.
* All rights reserved.
* http://cpanel.net
*
Expand All @@ -13,7 +13,7 @@
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of cPanel, Inc. nor the
* * Neither the name of cPanel, L.L.C. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
Expand All @@ -31,9 +31,9 @@
* @category Cpanel
* @package Cpanel_PublicAPI
* @author David Neimeyer <[email protected]>
* @copyright Copyright (c) 2011, cPanel, Inc., All rights Reserved. (http://cpanel.net)
* @copyright Copyright (c) 2011, cPanel, L.L.C., All rights Reserved. (http://cpanel.net)
* @license http://sdk.cpanel.net/license/bsd.html BSD License
* @version 0.1.0
* @version 0.2.0
* @link http://sdk.cpanel.net
* @since 0.1.0
*/
Expand Down
8 changes: 4 additions & 4 deletions Cpanel/Examples/PublicAPI_and_Service_Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Example usage of configuration options for the PublicAPI client and cPanel
* PHP library.
*
* Copyright (c) 2011, cPanel, Inc.
* Copyright (c) 2011, cPanel, L.L.C.
* All rights reserved.
* http://cpanel.net
*
Expand All @@ -14,7 +14,7 @@
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of cPanel, Inc. nor the
* * Neither the name of cPanel, L.L.C. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
Expand All @@ -32,9 +32,9 @@
* @category Cpanel
* @package Cpanel_PublicAPI
* @author David Neimeyer <[email protected]>
* @copyright Copyright (c) 2011, cPanel, Inc., All rights Reserved. (http://cpanel.net)
* @copyright Copyright (c) 2011, cPanel, L.L.C., All rights Reserved. (http://cpanel.net)
* @license http://sdk.cpanel.net/license/bsd.html BSD License
* @version 0.1.0
* @version 0.2.0
* @link http://sdk.cpanel.net
* @since 0.1.0
*/
Expand Down
90 changes: 90 additions & 0 deletions Cpanel/Examples/Token_Access_cPanel_Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Example usage of cPanel Service with an API Token
*
* Copyright (c) 2019, cPanel, L.L.C.
* All rights reserved.
* http://cpanel.net
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of cPanel, L.L.C. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category Cpanel
* @package Cpanel_PublicAPI
* @author Dustin Scherer <[email protected]>
* @copyright Copyright (c) 2019, cPanel, L.L.C., All rights Reserved. (http://cpanel.net)
* @license http://sdk.cpanel.net/license/bsd.html BSD License
* @version 0.2.0
* @link http://sdk.cpanel.net
* @since 0.2.0
*/

/**
* Please look at the Introduction_to_PublicAPI.php for more details.
*/
TRY {
require_once realpath( dirname(__FILE__) . '/../Util/Autoload.php');

# Replace these values with real values from your server
# You can generate a cPanel API Token in the Cpanel > API Tokens Interface
$HOST_NAME = '10.1.32.118';
$HOST_USER = 'green';
$TOKEN = '33JK2FL611YNDACZX1EO4M1N7O37HHPE';

/**
* Getting a cPanel Service object and invoking the api2_request method on
* the cPanel Service object itself. The preferred method is to use the
* cpanel_api{n}_request() method available in the PublicAPI interface, but
* this is here for demonstration purposes of the Service available in the
* cPanel library.
*
* We use the cPanel Service's accessor methods to set initialization
* variables. Authenticating as the user with a token
*/
$cpanel = Cpanel_PublicAPI::factory('cPanel');
$cpanel->setUser($HOST_USER)
->setToken($TOKEN)
->setHost($HOST_NAME);

$service = 'cpanel';
$queryMF = array(
'module' => 'Email',
'function' => 'listforwards',
'user' => $HOST_USER,
);
$response = $cpanel->api2_request($service, $queryMF);

echo "API2 response for {$queryMF['module']}::{$queryMF['function']} '\n";

foreach ($response->cpanelresult->data as $dataset) {
foreach ($dataset as $key => $value) {
echo "\t$key: $value\n";
}
}

echo "\n";
}
CATCH(Exception $e) {
echo $e->getMessage() . "\n" . $e->getTraceAsString() . "\n";
}
echo "EOF: You've successfully ended the " . basename(__FILE__) . " script.\n";
?>
Loading