Welcome Guest, Not a member yet? Register   Sign In
[Updated!] Cpanel library (Supports email & Cron)
#1

[eluser]PromaneX[/eluser]
I needed to be able to create e-mail accounts on my cpanel server from within my application so I created a class to do that. I am sharing it here in case anyone else needs this functionality too.

I may extend this to include other cpanel functions such as forwarders, auto-replies, sub-domains and such, if anyone needs something, let me know and i will add it for you.
cpanel.php - place in system/application/libraries/

UPDATE
Quote:I was asked to add support for getting and setting Cron - That feature has now been added.
for get_cron you can call the function with no arguments to return all lines of the cron or you can pass a string and the function will search inside the commands for that string and only return lines that contain it. for set_cron you can pass min,hour,day,month,dayofweek,command and it will add a new line, if you also pass an ID it will update that line of the cron and not add a new one.

Please let me know about any bugs you find or any improvments you come up with Smile
Updated code is in a later post, i reached the max length in this one!!, just replace existing cpanel.php with the new one.
#2

[eluser]marinaccio[/eluser]
Awesome, I definitely could see this being very useful. Thank you for sharing.
#3

[eluser]PromaneX[/eluser]
No worries, let me know if you need anything else adding to it and i will do it for you.
#4

[eluser]marinaccio[/eluser]
Thank you, that sounds great. If I extend it, I'll be sure to post back as well.
#5

[eluser]PromaneX[/eluser]
New code for the lib - wouldnt fit in first post!

Code:
<?php
if (!defined('BASEPATH'))
{
    exit('No direct script access allowed');
}
/**
* Code Igniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package        CodeIgniter
* @author        Rick Ellis
* @copyright    Copyright (c) 2006, pMachine, Inc.
* @license        http://www.codeignitor.com/user_guide/license.html
* @link        http://www.codeigniter.com
* @since        Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
class Cpanel
{
    function Cpanel()
    {
        $this->CI =& get_instance();
        log_message('debug', 'Cpanel Class Initialized');//set up cpanel access data
        $this->CI->config->load('cpanel', TRUE);
        $this->cpanel_config = $this->CI->config->item('cpanel');
    }

    function create_email($user, $pass, $quota=20)
    {
        $request='http://'.$this->cpanel_config['user'];
        $request.=':';
        $request.=$this->cpanel_config['pass'];
        $request.='@';
        $request.=$this->cpanel_config['host'];
        $request.=':2082/frontend/';
        $request.=$this->cpanel_config['skin'];
        $request.='/mail/doaddpop.html?quota=';
        $request.=$quota;
        $request.='&email;=';
        $request.=$user;
        $request.='&domain;=';
        $request.=$this->cpanel_config['host'];
        $request.='&password;=';
        $request.=$pass;
        if ($repsonse = file_get_contents($request))
        {
            if (!stristr($repsonse, 'already exists!') === FALSE)
            {
                return FALSE;
            }
            if (!stristr($repsonse, 'was successfully created') === FALSE)
            {
                return TRUE;
            }
        }
        else
        {
            return FALSE;
        }
    }

    function get_cron($search = null)
    {
        $request = 'http://' . $this->cpanel_config['user'];
        $request .= ':';
        $request .= $this->cpanel_config['pass'];
        $request .= '@';
        $request .= $this->cpanel_config['host'];
        $request .= ':2082/frontend/';
        $request .= $this->cpanel_config['skin'];
        $request .= '/cron/advcron.html';
        if ($repsonse = file_get_contents($request))
        {
            $dom = new domdocument;
            $dom->loadHTML($repsonse);
            $xpath = new domxpath($dom);
            $xNodes = $xpath->query('//input');
            foreach ($xNodes as $item)
            {
                $_n = explode('-', $item->getAttribute('name'));
                if (!stristr($item->getAttribute('name'), '-') === false)
                {
                    if ($search !== null)
                    {
                        if (!stristr($item->getAttribute('value'), $search) === false)
                        {
                            $data[$_n[0]]['accept'] = true;
                        }
                        else
                        {
                            $data[$_n[0]]['accept'] = false;
                        }
                    }
                    $data[$_n[0]][$_n[1]] = $item->getAttribute('value');
                    $data[$_n[0]]['id'] = $_n[0];
                }
            }
            unset($data[0]);
            foreach ($data as $row)
            {
                if ($search !== null)
                {
                    if (!$row['accept'])
                    {
                        unset($data[$row['id']]);
                    }
                }
                unset($data[$row['id']]['id']);
            }
            return $data;
        }
        else
        {
            return false;
        }
    }

    function set_cron($min, $hour, $day, $month, $weekday, $command, $id=0)
    {
        $url = 'http://' . $this->cpanel_config['user'].':'.$this->cpanel_config['pass'].'@'.$this->cpanel_config['host'].':2082/frontend/'.$this->cpanel_config['skin']."/cron/editcron.html?";
        $request = $id.'-minute=';
        $request .= $min;
        $request .= '&'.$id.'-hour=';
        $request .= $hour;
        $request .= '&'.$id.'-day=';
        $request .= $day;
        $request .= '&'.$id.'-month=';
        $request .= $month;
        $request .= '&'.$id.'-weekday=';
        $request .= $weekday;
        $request .= '&'.$id.'-command=';
        $request .= urlencode($command);
        $oldcron = $this->CI->cpanel->get_cron();
        foreach ($oldcron as $key=>$line)
        {
            if ($key==$id)
            {
                unset($oldcron[$key]);
            }
            else
            {
                $request .= '&'.$key.'-minute=';
                $request .= $line['minute'];
                $request .= '&'.$key.'-hour=';
                $request .= $line['hour'];
                $request .= '&'.$key.'-day=';
                $request .= $line['day'];
                $request .= '&'.$key.'-month=';
                $request .= $line['month'];
                $request .= '&'.$key.'-weekday=';
                $request .= $line['weekday'];
                $request .= '&'.$key.'-command=';
                $request .= urlencode($line['command']);
            }
        }
        if ($repsonse = file_get_contents($url.$request))
        {
            return true;
        }
    }
}// END Cpanel class
/* End of file cpanel.php */
/* Location: ./system/application/libraries/cpanel.php */
#6

[eluser]PromaneX[/eluser]
new code pt 2

cpanel.php - config file - place in system/application/config/
Code:
<?
$config['host'] = "mycpaneldomain.com";
$config['user'] = "mycapanelusername";
$config['pass'] = "maycpanelpassword";
$config['skin'] = "x3";  //the name of the skin you use in cpanel, usually x3.


/* End of file cpanel.php */
/* Location: ./system/application/config/cpanel.php */

Next on my list is to add support for getting email account information for existing accounts. I will then slowly work through all of the usable features of cpanel and add support for them. If anyone wants me to give priority to something please let me know!
#7

[eluser]renownedmedia[/eluser]
Creating sub domains would be kick ass!
#8

[eluser]PromaneX[/eluser]
[quote author="Thomas Hunter" date="1259575672"]Creating sub domains would be kick ass![/quote]

I have written the code for that now, would you like it to be able to list the existing sub domains also?
#9

[eluser]Phil Sturgeon[/eluser]
As useful as this is, I am concerned about my server sending out the cPanel in plain text. You should make a note on there that the host should only ever be set to localhost, as if you use this to admin other servers you are prone to attack by packet sniffers.

Btw, the function http_build_query() will help make your code a little cleaner.

And $this->CI->cpanel->get_cron() can just be $this->get_cron(); :-)

For people interested in what could be added to this library, take a look here.
#10

[eluser]PromaneX[/eluser]
[quote author="Phil Sturgeon" date="1259605175"]As useful as this is, I am concerned about my server sending out the cPanel in plain text. You should make a note on there that the host should only ever be set to localhost, as if you use this to admin other servers you are prone to attack by packet sniffers.

Btw, the function http_build_query() will help make your code a little cleaner.

And $this->CI->cpanel->get_cron() can just be $this->get_cron(); :-)

For people interested in what could be added to this library, take a look here.[/quote]

Thank you for the pointers. I could update it to use curl and ssl - that should solve the security issues for those that have curl available to them. I will clean up the code too, i know its a bit of a mess right now Sad

EDIT: I'm going to totally re-write the whole thing to use the xml interface - thank you for pointing that out to me!




Theme © iAndrew 2016 - Forum software by © MyBB