<?php
class cpanel
{
/**
* @var resource contains curl resource for communication with cpanel
**/
var $socket ;
/**
* @param string username your cpanel username
* @param string password your cpanel password
* @param string hostname your cpanel hostname
* @param bool ssl indicate ssl availability
* @param string theme indicate the theme being used
* @return void
*
* This will give you the default settings of using ssl and X as the theme
* <code><?php $cpanel = new cpanel( 'username', 'password', 'hostname' ); ?></code>
*
* This will allow you to disable SSL
* <code><?php $cpanel = new cpanel( 'username', 'password', 'hostname', false ); ?></code>
*
* This will allow you to disable SSL and change the theme
* <code><?php $cpanel = new cpanel( 'username', 'password', 'hostname', false, 'mytheme' ); ?></code>
**/
function cpanel( $username, $password, $hostname, $ssl = true, $theme = 'x' )
{
$this->ssl = $ssl ;
$this->username = $username ;
$this->password = $password ;
$this->hostname = $hostname ;
$this->request = $request ;
$this->theme = $theme ;
if( !( $this->socket = @curl_init( ) ) ): die('FATAL : Cannot initialize curl');
return;
else:
if( $this->ssl ):
curl_setopt( $this->socket, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $this->socket, CURLOPT_SSL_VERIFYHOST, 0 );
endif;
curl_setopt( $this->socket, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $this->socket, CURLOPT_HEADER, 0 );
curl_setopt( $this->socket, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $this->socket, CURLOPT_HTTPHEADER,
array( sprintf( "Authorization: Basic %s", base64_encode( sprintf( '%s:%s', $this->username, $this->password ) ) ) )
);
endif;
if( ereg( 'Login Attempt Failed', $this->make_request( sprintf( '/frontend/%s/', $this->theme ) ) ) )
die("Cannot login to cpanel");
}
/**
* @param string request the page to retrieve from cpanel
* @return string returns raw data
*
* This would retrieve the index page from cpanel
* <code><?php $cpanel->make_request( "/frontend/$cpanel->theme" ); ?>
**/
function make_request( $request )
{
if( $this->ssl )
curl_setopt( $this->socket, CURLOPT_URL, sprintf( "https://%s:2083%s",$this->hostname, $request ) );
else
curl_setopt( $this->socket, CURLOPT_URL, sprintf( "http://%s:2082%s",$this->hostname, $request ) );
return curl_exec( $this->socket );
}
/**
* @return string returns html tables of table from cpanel index
*
* This example method returns all your account information from cpanel index, unformatted
* <code><?php echo $cpanel->account_tables( ); ?></code>
**/
function account_tables( )
{
preg_match_all( '#(<table width="275" border="0" cellspacing="3" cellpadding="4">.*?[^<]</table>)#si',
$this->make_request( sprintf( "/frontend/%s/", $this->theme ) ),
$return
);
return implode( "<br />", $return[0] );
}
/**
* This method closes the curl resource
*
* <code><?php $cpanel->_close( ); ?></code>
**/
function _close( )
{
return @curl_close( $this->socket );
}
}
$cpanel = new cpanel( "username", "password", "hostname" );
echo $cpanel->account_tables( );
$cpanel->_close( );
?>