5ubliminal@twitter

A basic PHP Class Script to access cPanel : 5ubliminal's TellinYa

<a href="http://www.tellinya.com/art2/8/">A basic PHP Class Script to access cPanel : 5ubliminal's TellinYa</a>
5ubliminal's YAMS
Accessing cPanel - Why and How

Why would someone need access directly to cPanel?
It's easy: You can do many things like, manage multiple hosting accounts in one, keep track of your addon or parked domains, retrieveing raw log files and using them with your own analyzer scripts.

This class is not for the unexperienced PHP programmer and you need intermediate skills to advanced to make use of it to its fullest.

cPanel is probably the most popular hosting management console and still some (like me) want more. I needed access to the raw access logs, and after contacting support realized the only way to get the raw access logs of my cPanel hosting hosted website was by donwloading manually from cPanel. But I code every job, no manual work for me! Got to work and about 3 hours later had this script done and functional.

So the reason I wrote this script is te retrieve my raw access logs and analyze them but posibilities are endless. You can use it to access any page in cPanel and even do posts. You can create a centralized domain management console for all your cPanel php hosting put into one.

The actual code of the class

<?
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function gzdecode($data){
    
mt_srand((float)microtime()*1000000);
     
$eh="php-" md5(mt_rand(0,mt_getrandmax())) . ".gz"
    
$fd=fopen($eh,"w");  fwrite($fd$data);  fclose($fd); 
    
$fo fopen($eh"rb"); fseek($fo, -4SEEK_END);
    
$buf fread($fo4); $gzsize end(unpack("V"$buf)); fclose($fo);
    
$zd gzopen($eh,"r");  $contents gzread($zd$gzsize); gzclose($zd); 
    
unlink($eh); return $contents;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
class cPanel{
    var 
$cPanelUser            ="";
    var 
$cPanelPass            ="";
    var 
$cPanelDomain        ="";
    var 
$cPanelPort            =0;
    var 
$cPanelRel            ="";
    var 
$cPanelRoot            ="";
    function 
cPanel($cPanelDomain$cPanelPort$authUser$authPass){
        
$this->cPanelDomain    =$cPanelDomain;
        
$this->cPanelPort    =$cPanelPort;
        
$this->cPanelUser    =$authUser;
        
$this->cPanelPass    =$authPass;
        
//Root path of cPanel to load pages begining with /
        
$this->cPanelRoot    ="http://".$this->cPanelDomain.":".$this->cPanelPort."/";
        
//Relative path of cPanel to load pages not begining with /
        
$this->cPanelRel    =$this->cPanelRoot."frontend/x/";
    }
    function 
fetchPage($cPanelPage){
        
$curl curl_init();
        
$loginf sprintf("%s:%s",$this->cPanelUser,$this->cPanelPass);
        if(
$cPanelPage[0]=='/'){
            
//Build the path - if begins with / we go and paste at root
            
$url=$this->cPanelRoot.substr($cPanelPage,1);
        }else{
            
//Build the path - if begins with / we go and paste relative
            
$url=$this->cPanelRel.$cPanelPage;
        }
        
curl_setopt ($curlCURLOPT_URL$url);
        
curl_setopt ($curlCURLOPT_TIMEOUT30);
        
curl_setopt ($curlCURLOPT_USERAGENTsprintf("Mozilla/%d.0",rand(4,5)));
        
curl_setopt ($curlCURLOPT_HEADER0);
        
curl_setopt ($curlCURLOPT_RETURNTRANSFER1);
        
curl_setopt ($curlCURLOPT_SSL_VERIFYPEER0);
        
curl_setopt ($curlCURLOPT_HTTPAUTHCURLAUTH_BASIC); 
        
curl_setopt ($curlCURLOPT_USERPWD$loginf);
        
$html curl_exec ($curl);
        
curl_close ($curl);
        return 
$html;
    }
    
//Sample functions - expand as required based on the upper functions
    
function fetchRawLogs(){
        
//Fetch a list of domains that have logs available for them
        
$html=$this->fetchPage("raw/index.html");
        if(!
preg_match_all("/"/getaccesslog/accesslog_([^_]+)_[^.]+.gz"/i",$html,$pcs))
            return 
false;
        return 
$pcs[1];
    }
    function 
fetchRawLog($domain,$tm){
        
//Download the log for a goven domain as returned in function above and a date (current)
        
if(!isset($tm)) $tm=time();
        
$gzlnk="/getaccesslog/accesslog_".$domain."_".
            
str_replace("_0","_",strftime("_%m_%d_%Y",$tm)).".gz";
        
$gz=$this->fetchPage($gzlnk);
        
$logtxt=gzdecode($gz);
        return 
$logtxt;
    }
};
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
?>

Sample usage
//Include the Class
require("cPanel.php");

//Initialize the Class
//My cPanel homepage address looks like this :
http://domain:port/frontend/x/index.html
//Analyze my url and easily understand the root and relative paths used in script
$cp=new cPanel($domain,$port,$user,$pass);

//Fetch a list of available subdomains
$sd=$cp->fetchRawLogs();

//Fetch log for a chosen domain
$log=$cp->fetchRawLog($sd[0]);

//Display the log
each nl2br($log);
?>
Last but not least

If you find new and creative usages for this please share. I'd really like to know how this class has been used. And if you find it useful, link to it and help spread the word!

7 Comments Posted By Readers :

Add your comment
#1 luis from Peru web
Posted on Thursday, 21 February, 2008
hi,good script , i'm a web designer an i like to know who can i do a web php with iframe that open automatically my cpanel into the iframe and i use it. Is it posibly whit curl?, i'm traying using this script but it not work only show me the html but the cpanel not work.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_URL, "http://$mydomain:2082/frontend/rvblue/index.html");
curl_setopt($ch, CURLOPT_USERPWD,$user.":".$pass);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
$data = curl_exec($ch);
curl_close($ch);

Could you bring me same help or sample code how can i cpanel autolog in a page into a iframe?
#2 5ubliminal web
Posted on Thursday, 21 February, 2008
These scripts are made to programatically access cPanel functions. But you can not login to cPanel inside an IFRAME as you can not feed the access info to an IFRAME.
You could use cURL to access cPanel by using a script to get cPanel HTML using your credentials but I will not show you how to do this as any mistake, no matter how small, can put your entire hosting account at risk.

You are on the right path with your code but consider the fact that you need to rewrite the entire URL structure of cPanel. Getting the HTML is one thing but you need to reroute all the internal links for the trick to work on other pages except index.
You need something like loadCpanel?url=#UR# where #URL# is each URL you find in the returned HTML.

Still, consider security before you do this.
#3 Lance from United States
Posted on Wednesday, 02 April, 2008
Hi, this script is great. I have been trying to modify it a little but I guess I'm not that great of a programmer.

I was hoping you could help me as this is the closest script I could find that would help my little project. What I wanted to do was to create a custom login form for cPanel webmail. I know there are forms out there for this, but they all have one flaw. If they enter an incorrect user/password they get taken to the default cPanel wemail login(domain.com:2095/login). I am trying to get it to return to the original custom form again.

Someone posted in another forum that I might have to use CURL and parse what the login form returned to see if it was a failure or not and redirect from their.

Any help would be awesome. I have been going a this for a week. I have some programming experience in java but this is all new to me.
#4 5ubliminal web
Posted on Wednesday, 02 April, 2008
You will have to use cURL and intermediate the requests.
You will need to check out if the login sequence was successfull yourself and just download the inner pages with cURL.
You can't just write the login sequence yourself and then move over to webmail as there are cookies involved.
… I don't know if it's all worth the trouble especially if you're not a 'great' programmer:)
#5 Tom Servo from United States web
Posted on Tuesday, 01 July, 2008
This is just what I needed! I'm going to try to use it to let clients set up autoresponders without giving them cpanel access. I wonder if I can make it work somehow for viewing webstats as well, using cURL like you described to Lance...

I'll post back with some code if I get it to do what I want.
#6 5ubliminal web
Posted on Tuesday, 01 July, 2008
If you know a bit of RegExp you can do all that. Study your cPanel skin and parse the HTML with regexp and post back the new data after you see how the forms work.
It's really easy and getting the rawaccesslogs is also.
#7 Tom Servo from United States web
Posted on Monday, 14 July, 2008
It worked great! Here's the methods I added to your class:

(Note: I have another class that validates data from post forms. I make no promises that this code is secure as-is. It may be a little bit unfinished as well, but it works.)


function setAutoreponder($postdata){
if (!is_array($postdata)){
return FALSE;
}

$post_array = $_POST; // Needs to be validated!!

$html = $this->fetchPage("mail/doaddars.html", $post_array);

if(1){ // Would be a good idea to finish this
return TRUE;
}
else {
// Not successful
return FALSE;
}
}

public function unsetAutoresponder($email){
$html=$this->fetchPage("mail/dodelautores.html?email=$email@{$this->cPanelDomain}&redirectdomain=");
$success = 'The auto-responder '.$email.'@'.$this->cPanelDomain.' was deleted.';
if (strpos($html, $success, 10591)!==FALSE){
return TRUE;
} else {
return FALSE;
}
}

public function getAutoresponder($email){ // to get the details for editing
$html=$this->fetchPage("mail/addars.html?email=$email@{$this->cPanelDomain}&redirectdomain=");

$html = preg_replace('/
/', '', $html);
preg_match('/selected>([^]*).*rows=8>([^
5ubliminal's TellinYa.com SEM & SEO Blog © 2007 - All rights reserved unless mentioned otherwise .
Rendered On : [Friday, 03 July, 2009 - 21:53:54 GMT]   No Ajax / Flash Used Here
" A basic PHP Class Script to access cPanel : 5ubliminal's TellinYa "
Close
Tellinya.com is relocating to blog.5ubliminal.com. This blog is no longer maintained and comments are no longer accepted / answered.