Skip to content

Instantly share code, notes, and snippets.

@md-riaz
Created May 17, 2023 11:43
Show Gist options
  • Save md-riaz/4a316678173baafb305caf849f4cad8d to your computer and use it in GitHub Desktop.
Save md-riaz/4a316678173baafb305caf849f4cad8d to your computer and use it in GitHub Desktop.
<?php
error_reporting(E_ALL & ~E_NOTICE);
class cPanelLicensing {
function __construct ($user, $pass, $key = NULL) {
if (!function_exists('curl_init')) {
die("cPanelLicensing requires that curl+ssl support is compiled into the PHP interpreter\n");
}
$this->set_format("simplexml");
$this->curl = curl_init();
$this->user = $user;
$this->pass = $pass;
$this->key = $key;
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_HEADER, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, 'cPanel Licensing Agent (php) 3.16' );
if (isset($_SERVER["PHP_CURL_SSL_VERIFY_HOSTNAME"]) && !$_SERVER["PHP_CURL_SSL_VERIFY_HOSTNAME"]) {
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0);
}
}
function __destruct () {
curl_close($this->curl);
}
public function setCredentials ($user, $pass) {
curl_setopt($this->curl, CURLOPT_USERPWD, $user . ':' . $pass);
}
public function set_format ($format) {
$valid_formats = [
"simplexml" => "xml",
"xml" => "xml",
"json" => "json",
"yaml" => "yaml",
];
if ($valid_formats[$format]) {
$this->format = $format;
$this->output = $valid_formats[$format];
return $this->format;
}
else {
die("set_format requires that the format is xml, json, yaml or simplexml\n");
}
}
public function parseHTTP ($response) {
# parse the response into a header array and content variables
$pos = 0;
if (preg_match("/(\S+) (\d+)\s*([^\r\n]*)\r?\n/", $response, $matches, 0, $pos)) {
$status_code = $matches[2];
$status_mesg = $matches[3];
$pos += strlen($matches[0]);
}
$headers = [];
while (1) {
if (!isset($response[$pos])) {
break;
}
elseif (preg_match("/\G([^:\s]+):\s*([^\r\n]*)\r?\n/", $response, $matches, 0, $pos)) {
$headers[$matches[1]] = $matches[2];
$pos += strlen($matches[0]);
}
elseif (preg_match("/\G\r?\n/", $response, $matches, 0, $pos)) {
$pos += strlen($matches[0]);
break;
}
else {
break;
}
}
$content = substr($response, $pos);
$href = [
"status_code" => $status_code,
"status_mesg" => $status_mesg,
"headers" => $headers,
"content" => $content,
];
return $href;
}
private function api_build_query ($query_data) {
$query = [];
foreach ($query_data as $name => $value) {
if (is_array($value)) {
foreach ($value as $subvalue) {
$query[] = urlencode($name) . "=" . urlencode($subvalue);
}
} else {
$query[] = urlencode($name) . "=" . urlencode($value);
}
}
return implode($query, '&');
}
private function request ($function, $args = []) {
if (!$function) {
die("request requires that a function is defined\n");
}
$manage2_host = $_SERVER['MANAGE2_HOST'] ?: 'manage2.cpanel.net';
$url = "https://$manage2_host/$function";
$args["output"] = $this->output;
if ($this->key) {
$args["apikey"] = $this->key;
}
elseif ($this->user && $this->pass) {
$this->setCredentials($this->user, $this->pass);
}
$query = $this->api_build_query($args);
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $query);
$response = curl_exec($this->curl);
if ($response == false) {
die('request failed: ' . curl_error($this->curl) . "\n" );
}
$href = $this->parseHTTP($response);
if ($href["status_code"] != 200) {
die("request failed: $href[status_code] $href[status_mesg]\n");
}
if ( $this->format == 'simplexml' || $this->format == 'xml' ) {
$ref = simplexml_load_string($href["content"]);
# convert xml objects into regular php array
$ref = json_decode(json_encode($ref), TRUE);
return $ref;
}
elseif ( $this->format == 'json' ) {
return json_decode($href["content"]);
}
elseif ( $this->format == 'yaml' ) {
return yaml_parse($href["content"]);
}
else {
return $href["content"];
}
}
public function reactivateLicense ($args) {
return $this->request("XMLlicenseReActivate.cgi", $args);
}
public function expireLicense ($args) {
return $this->request("XMLlicenseExpire.cgi", $args);
}
public function extendOnetimeUpdates ($args) {
return $this->request("XMLonetimeext.cgi", $args);
}
public function changeip ($args) {
return $this->request("XMLtransfer.cgi", $args);
}
public function requestTransfer ($args) {
return $this->request("XMLtransferRequest.cgi", $args);
}
public function activateLicense ($args) {
$args['legal'] = 1;
return $this->request("XMLlicenseAdd.cgi", $args);
}
public function addPickupPass ($args) {
return $this->request("XMLaddPickupPass.cgi", $args);
}
public function registerAuth ($args) {
$response = $this->request("XMLregisterAuth.cgi", $args);
if ($this->format == "simplexml") {
$this->setCredentials($args["user"], $response["key"]);
}
return $response;
}
public function fetchGroups () {
return $this->request("XMLgroupInfo.cgi");
}
public function fetchLicenseRiskData ($args) {
return $this->request("XMLsecverify.cgi", $args);
}
public function fetchLicenseRaw ($args) {
$args = array_merge(["all" => 1], $args);
return $this->request("XMLRawlookup.cgi", $args);
}
public function fetchLicenseId ($args) {
$args = array_merge(["all" => 1], $args);
return $this->request("XMLlookup.cgi", $args);
}
public function fetchPackages () {
return $this->request("XMLpackageInfo.cgi");
}
public function fetchLicenses ($args) {
return $this->request("XMLlicenseInfo.cgi", $args);
}
public function fetchExpiredLicenses () {
return $this->request("XMLlicenseInfo.cgi", ["expired" => '1']);
}
public function findKey ($search, $ref) {
if (array_key_exists("packages", $ref)) {
$type = "packages";
}
elseif (array_key_exists("groups", $ref)) {
$type = "groups";
}
else {
die("findKey with an object that is not groups or packages\n");
}
foreach ($ref[$type] as $ref2) {
foreach ($ref2 as $key => $value) {
if ($value == $search) {
if (preg_match("/^[a-z](\d+)/i", $key, $matches)) {
$key = $matches[1];
}
return $key;
}
}
}
die("Could not find $type that matches $search\n");
}
public function updateGroup ($args) {
return $this->request("XMLgroupUpdate.cgi", $args);
}
public function updatePackage ($args) {
return $this->request("XMLpackageUpdate.cgi", $args);
}
public function addServiceCredit ($args) {
return $this->request("XMLserviceCredit.cgi", $args);
}
public function updateActiveLicenseMetadata ($args) {
return $this->request("XMLupdateActiveLicenseMetadata.cgi", $args);
}
}
// example usage
$USER = 'user@example.com';
$PASS = 'password';
$IP = '1.3.5.7';
$cpl = new cPanelLicensing($USER, $PASS);
# For more information regarding these options, please see the following:
# https://go.cpanel.net/Manage2-API-List-License-Information
$ref = $cpl->fetchLicenses();
print_r($ref);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment