List Cpanel Account Infomation

Started on the old forum in April 2009 · view the original in the archive · old links redirect here

1 reply · 868 views · started 17 years ago · 1 min read

synergyx2009 Developer · 86 posts #1

I am trying to get a cleaner output and i cant seem to get the coding right.

firstly you have your main whm.php file with all php classes:

<?php

class Whm
{
	var $controller = true;
	var $host = localhost;
	var $user=username;
	var $accessHash = remote access key;
	var $errors=array();
	var $fp=null;

	/*
	 * initialization
	 */
	function startup(&$controller)
	{
		$this->controller =& $controller;
	}

	/*
	 * initialization
	 */
	function init($host,$user,$accessHash)
	{
		$this->host=$host;
		$this->user=$user;
		$accessHash = str_replace(array("\r", "\n"),"",$accessHash);
		$this->accessHash=$accessHash;
	}

	/*
	 * connect to the xml api
	 * Output: true on success, false on fail
	 */
	function connect($api_path)
	{
		/*
		 *  Open a socket for HTTPS
		 */
		$this->fp = fsockopen("ssl://" . $this->host, 2087, $errno, $errstr, 30);

		/*
		 *  Uncomment to use unsecure HTTP instead
		 */
		//$this->fp = fsockopen("ssl://" . $this->host, 2087, $errno, $errstr, 30);

		/*
		 * Die on error initializing socket
		 */
		if ($errno == 0 && $this->fp == false)
		{
			$this->errors[]="Socket Error: Could not initialize socket.";
			return false;
		}
		elseif ($this->fp == false)
		{
			$this->errors[]="Socket Error #" . $errno . ": " . $errstr;
			return false;
		}

		/*
		 *  Assemble the header to send
		 */
		$header = "";
		$header .= "GET " . $api_path . " HTTP/1.0\r\n";
		$header .= "Host: " . $this->host . "\r\n";
		$header .= "Connection: Close\r\n";
		$header .= "Authorization: WHM " . $this->user . ":" . $this->accessHash . "\r\n";
		// Comment above line and uncomment below line to use password authentication in place of hash authentication
		//$header .= "Authorization: Basic " . base64_encode($user . ":" . $pass) . "\r\n";
		$header .= "\r\n";

		/*
		 * Send the Header
		 */
		if(!@fputs($this->fp, $header))
		{
			$this->errors[]='Unable to send header.';
			return false;
		}
	}

	/*
	 * Close the socket
	 */
	function disconnect()
	{
		fclose($this->fp);
	}

	/*
	 * Get the raw output from the server
	 * Output: string
	 */
	function getOutput()
	{
		$rawResult = "";
		while (!feof($this->fp))
		{
			$rawResult .= @fgets($this->fp, 128); // Suppress errors with @
		}

		/*
		 * Ignore headers
		 */
		$rawResultParts = explode("\r\n\r\n",$rawResult);
		$result = $rawResultParts[1];

		/*
		 * Output XML
		 */
		return $result;
	}

	/*
	 * This function lists the verison of cPanel and WHM installed on the server.
	 * Output: string
	 */
	function version()
	{
		//connect using prpoer xml api address
		$this->connect('/xml-api/version');
		//get the output
		$xmlstr=$this->getOutput();
		if($xmlstr=='')
		{
			$this->errors[]='No output.';
			return false;
		}
		//disconnect
		$this->disconnect();

		//get the output xml as an array using simple xml
		$xml = new SimpleXMLElement($xmlstr);

		return $xml->version;
	}

function accountsummary($accUser)
	{
		//connect using prpoer xml api address
		$this->connect("/xml-api/accountsummary?user=$accUser");
		//get the output
		$xmlstr=$this->getOutput();
		if($xmlstr=='')
		{
			$this->errors[]='No output.';
			return false;
		}
		//disconnect
		$this->disconnect();

		//get the output xml as an array using simple xml
		$xml = new SimpleXMLElement($xmlstr);

		if($xml->status==1)
		{
			$result['disklimit']=$xml->acct->disklimit;
			$result['diskused']=$xml->acct->diskused;
			$result['bandwidthlimit']=$xml->acct->bandwidthlimit;
			$result['domain']=$xml->acct->domain;
			$result['email']=$xml->acct->email;
			$result['ip']=$xml->acct->ip;
			$result['owner']=$xml->acct->owner;
			$result['partition']=$xml->acct->partition;
			$result['plan']=$xml->acct->plan;
			$result['startdate']=$xml->acct->startdate;
			$result['theme']=$xml->acct->theme;
			$result['unix_startdate']=$xml->acct->unix_startdate;
			$result['user']=$xml->acct->user;
			return $result;
		}
		else
		{
			$this->errors[]=$xml->statusmsg;
			return false;
		}
	}

|Dont forget to say thanx|

synergyx2009 Developer · 86 posts #2

The second file used to call the functions, example: test.php

<?php
//include the whm class file.
require_once('whm.php');

// create a new instance of whm class
$test= new whm;

//initilize the whm object 
//you can use you hostname or an IP below 
//you can find you whm hash when yopu login into your whm account clickong on "Setup Remote Access Key" link.
$test->init('localhost','username','remote access key');

//This will output the cpanel/whm version.  
$version= $test->version();
echo "Cpanel/whm version is: $version <br>";

//This way you can list an account. 
//This function will return a result set as an array in success and will return false on fail.
$accUser = "account user";
$result= $test->accountsummary($accUser);

//check if list account summary was successfull or not.
if($result)
{
	//print the result set
	print_r($result);
}
else
{
	//You can get the errors like this.
	print_r($test->errors);
}

?>

now when you have added your host, username and remote access key to both whm.php and test.php, the output code works 100% but it is displayed using simple xml, and i would like to display in a more cleaner way.

Can anybody do this as i am not too clued up on xml

|Dont forget to say thanx|

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close