The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Using PHP to access cPanel

Discussion in 'General Discussion' started by jcaruso, Jul 6, 2005.

  1. jcaruso

    jcaruso Registered

    Joined:
    Jul 6, 2005
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Just for some background, I am just a customer of a webhosting company who uses cPanel, so I don't have root access to the server.

    I've looked around the forums, and I haven't found an answer to what I am trying to do... so here it is:

    I am currently trying to find a way to access my current bandwidth usage through a php script. I was wondering if there is a way to access this information through some sort of function call or other script. I read somewhere about the WHM API, but it doesn't sound like something a simple user would have access to. Sounded more like account management.

    If what I asked about above is not possible, I am thinking of just checking the raw server logs. However, I need a way to access these through php.

    Are either of these things possible? I thank you in advance for your help,
    - John
     
  2. lankyb

    lankyb Well-Known Member

    Joined:
    Sep 21, 2004
    Messages:
    99
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Peterborough, UK
    You can do it very much the same why as the custom login scripts. The username and passwords are added to the request string at the start like so:

    Code:
    http://username:password@serverip:2082/whateverscript
    I have included two scripts to collect the webstats off Awstats and Webalizer:

    Awstats:
    PHP:
    <?php

    $user 
    'username';//your cpanel username
    $pass 'password';//your cpanel password
    $domain 'website.com';//do not include 'http://' or 'www.'

    /*
    NO NEED TO TOUCH ANYTHING BELOW HERE
    */

    //retrieves the file, either .pl or .png
    function getFile($fileQuery){
     global 
    $user$pass$domain;
     return 
    file_get_contents("http://$user:$pass@$domain:2082/".$fileQuery,'r');
    }

    //it's a .png file...
    if(strpos($_SERVER['QUERY_STRING'],'.png')!==false) {
     
    $fileQuery $_SERVER['QUERY_STRING'];
    }
    //probably first time to access page...
    elseif(empty($_SERVER['QUERY_STRING'])){
     
    $fileQuery "awstats.pl?config=$domain";
    }
    //otherwise, all other accesses
    else {
     
    $fileQuery 'awstats.pl?'.$_SERVER['QUERY_STRING'];
    }

    //now get the file
    $file getFile($fileQuery);

    //check again to see if it was a .png file
    //if it's not, replace the links
    if(strpos($_SERVER['QUERY_STRING'],'.png')===false) {
     
    $file str_replace('awstats.pl'basename($_SERVER['PHP_SELF']), $file);
     
    $file str_replace('="/images','="'.basename($_SERVER['PHP_SELF']).'?images',$file);
    }
    //if it is a png, output appropriate header
    else {
     
    header("Content-type: image/png");
    }

    //output the file
    echo $file;
    ?>
    Webalizer:
    PHP:
    <?php
    $user 
    'username';
    $pass 'password';
    $url 'yourdomain.com';//do not include 'http:// or www'

    //retrieves the webalizer file, either .html or .png
    function getFile($file){
      global 
    $user$pass$url;
      return 
    file_get_contents("http://$user:$pass@$url:2082/tmp/$user/webalizer/$file");
    }
    //alters links, either .html or .png
    function changeLinks($subject$type) {
      return 
    preg_replace("/($type=\")(?!http)(.*?)\"/is","$1$PHP_SELF?$2\"",$subject);
    }

    if(!empty(
    $_SERVER['QUERY_STRING'])){
     
      
    //get file (whether png or html)
      
    $page getFile($_SERVER['QUERY_STRING']);

      
    //if png, output appropriate header
      
    if(strpos($_SERVER['QUERY_STRING'],'.png')!==false){
        
    header("Content-type: image/png");
      }
      
    //change the .png src(s)
      
    else {
        
    $page changeLinks($page'src');
      }
    }
    else {
      
    //get index
      
    $page getFile('index.html');

      
    //change links
      
    $page changeLinks($page'href');

      
    //change the usage.png src
      
    $page changeLinks($page'src');
    }
    //output it
    echo $page;

    ?>
    You can adapt the scripts to collect whatever you want from cPanel
     
  3. jcaruso

    jcaruso Registered

    Joined:
    Jul 6, 2005
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Thank you very much, you have ended my suffering :)
     
  4. bpmod

    bpmod Well-Known Member

    Joined:
    Sep 23, 2004
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    6
    I have tried this, but it won't work because there is a @ in my password. Is it possible to do this without changing the password?

    Thanks

    Brian
     
  5. lankyb

    lankyb Well-Known Member

    Joined:
    Sep 21, 2004
    Messages:
    99
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Peterborough, UK
    Remember that your going to have to use html symbols in the code.... so replace the '@' sign with '&#64'.... and that should work.

    Here is a website with the full list of html symbols: http://www.primeshop.com/html/jump3b.htm
     
    #5 lankyb, Jul 15, 2005
    Last edited: Jul 15, 2005
Loading...

Share This Page