PDA

View Full Version : PHP - this's my "Cpanel Analyzer ver 0.1", check it :)



d-j
April 23rd, 2007, 20:25
How to use 'Cpanel Analyzer ver 0.1':
- Copy and Paste this code to notepad (or your text editor)
- change username, password, domain, and theme in configuration section
- Save as => xxxx.php
- Test in your hosting :)


<?php

/*
- ------------------------
- Cpanel Analyzer ver 0.1
- by dj
- ------------------------
- You can modify this code as you want, but let my credit stay in your code ^_^
- I hope this script can be useful for you, guys...
- Let me know if you find bug(s), find me in FreeWebSpace.net/forum (username: d-j)
- ------------------------
- (work on Cpanel x and x2 theme)
- Special thx to:
- - Brandon <brandon@x10hosting.com> - inspiration code
- - FreeWebSpace.net - I find the new world :p
- - You - Have fun...
*/

// Configurations
$username = "your username"; // cPanel Username
$password = "your password"; // cPanel Password
$domain = "blablabla.com"; // cPanel Domain
$theme = "x2"; // cPanel Theme, x or x2

//GAI = General account information:
$gai[] = "Hosting package";
$gai[] = "Shared Ip Address";
//$gai[] = "Subdomains"; // Let's skip this section, because Cpanel have a fail html code in here
$gai[] = "Parked Domains";
$gai[] = "Addon Domains";
$gai[] = "MySQL Databases";
$gai[] = "Disk Space Usage";
$gai[] = "MySQL Disk Space";
$gai[] = "Disk space available";
$gai[] = "Bandwidth (this month)";
$gai[] = "Email Accounts";
$gai[] = "Email Forwarders";
$gai[] = "Auto-responders";
$gai[] = "Email Filters";
$gai[] = "Ftp Accounts";

//GSI = General server information:
$gsi[] = "Operating system";
$gsi[] = "Kernel version";
$gsi[] = "Machine Type";
$gsi[] = "Apache version";
$gsi[] = "PERL version";
$gsi[] = "Path to PERL";
$gsi[] = "Path to sendmail";
$gsi[] = "PHP version";
$gsi[] = "MySQL version";
$gsi[] = "cPanel Build";
$gsi[] = "Theme";
$gsi[] = "cPanel Pro";


// Core of the Cpanel Analyzer
function Analyze ($key, $file, $sp = " - "){
for ($i=0; $i<count($key) ;$i++){
$temp = explode(strtolower($key[$i])."</td>",$file); //Convert to array
$file = $temp[1]; //Never waste your resource we will use it for next section :)
$temp = str_replace(chr(34),"",$temp[1]); //We dont need " (quote)
$temp = trim(str_replace("<td class=index2>","",$temp)); //Remove <td class=index2>
$pos = strpos($temp, "</td>"); //Find the position of </td>

$result .= $key[$i].$sp.substr($temp,0,$pos)."<br>"; //Bingo... we get it that we want ^_^
}
return $result;
}

// Main Program
ini_set("display_errors", "0");
$file = file_get_contents("http://$username:$password@$domain:2082/frontend/$theme/index.html") or die("<b>Critical Error, Please check your configuration</b>");
$file = strtolower($file);

// Do what do you want... ^_^
echo Analyze($gai,$file);
echo "<hr>";
echo Analyze($gsi,$file," => ");

?>

This's the output of 'Cpanel Analyzer ver 0.1':

Hosting package - xxxx
Shared Ip Address - xxxx
Parked Domains - 0 / 0
Addon Domains - 1 / 2
MySQL Databases - 6 / unlimited
Disk Space Usage - 66.67 megabytes
MySQL Disk Space - 3.45 megabytes
Disk space available - 433.33 megabytes
Bandwidth (this month) - 109.31 megabytes
Email Accounts - 3 / unlimited
Email Forwarders - 0
Auto-responders - 0
Email Filters - 0
Ftp Accounts - 2 / unlimited

--------------------------------------------------------------------------------
Operating system => linux
Kernel version => 2.6.9-42.0.3.elsmp
Machine Type => i686
Apache version => 1.3.37 (unix)
PERL version => 5.8.7
Path to PERL => /usr/bin/perl
Path to sendmail => /usr/sbin/sendmail
PHP version => 4.4.4
MySQL version => 4.1.21-standard
cPanel Build => 10.9.0-current 75
Theme => cpanel x v2.6.0
cPanel Pro => 1.0 (rc1)

BrandonTheG
April 23rd, 2007, 22:15
Very nice I was thinking of doing something like this but you beat me to it:-)

d-j
April 23rd, 2007, 22:59
Very nice I was thinking of doing something like this but you beat me to it:-)

thx bud.. :angel:

your code:

ini_set("display_errors", "0");
$file = file_get_contents("http://$username:$password@$domain:2082/frontend/$theme/index.html") or die("<b>Critical Error, Please check your configuration</b>");
is very useful, i cant do nothing without that :P

krakjoe
April 24th, 2007, 03:58
Now, I hope you fellas don't think I'm being rude, but I have something to say.

Theres nothing wrong with using file_get_contents to retrieve data from cpanel, I have done in the past, however that doesn't make it a good idea, and you really shouldn't release code that uses such practices, cpanel comes equipped with proper http authentication and you should use it, always.

I'm not just opening my mouth and letting my belly rumble, I intend to help you;



<?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( );
?>


again, I hope you don't think I'm being rude :angel:

BrandonTheG
April 24th, 2007, 05:48
Now, I hope you fellas don't think I'm being rude, but I have something to say.

Theres nothing wrong with using file_get_contents to retrieve data from cpanel, I have done in the past, however that doesn't make it a good idea, and you really shouldn't release code that uses such practices, cpanel comes equipped with proper http authentication and you should use it, always.

I'm not just opening my mouth and letting my belly rumble, I intend to help you;
again, I hope you don't think I'm being rude :angel:

And just for people, that will output something like this.


General account information:
Hosting package StaticPaid
Shared Ip Address **.**.**.***
Subdomains 6 / 20
Parked Domains 0 / 15
Addon Domains 3 / 15
MySQL Databases 7 / 10
Disk Space Usage 179.09 Megabytes
MySQL Disk Space 17.30 Megabytes
Disk space available 2892.91
Megabytes Bandwidth (this month) 1217.77 Megabytes
Email Accounts 3 / unlimited
Email Forwarders 0
Auto-responders 0
Mailing Lists 0 / unlimited
Email Filters 0
Ftp Accounts 4 / 20

General server information:
Operating system Linux
Service Status Click to View
Kernel version **********
Machine Type i686
Apache version 1.3.37 (Unix)
PERL version 5.8.7
Path to PERL /usr/bin/perl
Path to sendmail /usr/sbin/sendmail
Installed Perl Modules Click to View
PHP version 5.2.0
MySQL version 4.1.21-standard
cPanel Build 10.9.0-CURRENT 138
Theme cPanel X v2.6.0
Documentation Click to View
cPanel Pro 1.0 (RC36)

d-j
April 25th, 2007, 01:56
@krakjoe = Your cpanel class is cool... (especially in cpanel function) ^_^ :evilb:

i cant try your script now because my connection not allow 2082. But if i read your code, you grab table in the left. i hope cpanel not change their layout frequently :P

I hope someone can improve that script (espesially in account_tables function).
e.g.:
- We use another skin (not x or x2, rsvskin or something like that)
- If we want get only apart of Account information, like bandwith, subdomain, etc. :classic2:

krakjoe
April 25th, 2007, 10:26
It's not a complete script, I was just showing you how you should communicate with cpanel, the idea is you write your own methods with your own regexp and use the make_request() method to retrieve the data securely, bascially, I won't be "working" on that script it's just a scrap of code :wave:

BrandonTheG
April 25th, 2007, 15:01
It's not a complete script, I was just showing you how you should communicate with cpanel, the idea is you write your own methods with your own regexp and use the make_request() method to retrieve the data securely, bascially, I won't be "working" on that script it's just a scrap of code :wave:

Now, what I forgot to ask is, if the server host has HTTP authentication shut off, would this still work?

krakjoe
April 25th, 2007, 16:57
I don't think either way would, the file_get_contents() function used in the intial code uses http authentication also, you would need to modify the consructor to handle that eventuality by posting data to /login instead of using http authentication

BrandonTheG
April 25th, 2007, 22:37
I don't think either way would, the file_get_contents() function used in the intial code uses http authentication also, you would need to modify the consructor to handle that eventuality by posting data to /login instead of using http authentication

oh ya thanks for that head up, ill work on doing that later.

d-j
April 27th, 2007, 10:37
It's not a complete script, I was just showing you how you should communicate with cpanel, the idea is you write your own methods with your own regexp and use the make_request() method to retrieve the data securely, bascially, I won't be "working" on that script it's just a scrap of code :wave:

hehe.. k..

actually this's my first time work with cpanel. it's because i read a thread by blong http://www.freewebspace.net/forums/showthread.php?t=2186662

blong only got a bandwidth with him script, so i want to try how to make it easier to get more information that we want :P .

btw thx for ur script bud.. (i got a lot of information how to communicate with cpanel) :angel: