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.
<?
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
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, -4, SEEK_END);
$buf = fread($fo, 4); $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 ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_TIMEOUT, 30);
curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt ($curl, CURLOPT_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;
}
};
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
?>
//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);
?>
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!