Are you running or planning on running a community website? If so how would you like to give every user automatically an email address, a sub domain and a ftp account? You might think that you need a special server in order to do this. But you are wrong. If your Hosting Control Panel is CPanel then you can still do this easily.
Before you look at the code and start playing with it you need to remember a few things.
- Never trust the input of your users and always validate their input
- Nothing is 100% secure
- Always test, test, test, test before releasing new functionality to users
In order to set up this automatization you need a few things.
- CPanel username
- CPanel password
- Your domain
- CPanel port
- CPanel theme
You should know your username and password already. If not you need to contact your hosting company. If you log into your CPanel and look at the address bar in your browser you can easily find out the rest of the information.
Sample URL
http://www.yourdomain.com:2082/frontend/x/index.html www.yourdomain.com = Your domain2082 = CPanel portx = CPanel theme
With this information you can now use the php class below. It is very easy. It has the following features
Features
Emails:
- Create Email addresses
- Delete Email addresses
- Change Email address Password
- Change Quota of Email address in box
FTP:
- Create FTP account
- Delete FTP account
- Change FTP account Quota
- Change FTP account Password
Subdomain:
- Create Subdomain
- Delete Subdomain
- Create Redirection for a subdomain
- Delete Redirection of a subdomain
PHP Class
Simply copy the code below into a file and call it anything you want ie cpanel.php
class cpanel {
var $cpanel_username = "user";
var $cpanel_password = "pass";
var $your_domain = "www.domain.com";
var $port = "2082";
var $cpanel_theme ='x';
var $url = '';
function cpanel () {
$this->url = "http://".$this->cpanel_username.':'.$this->cpanel_password. '@'.$this->your_domain.':'.$this->port.'/frontend/'.$this-> cpanel_theme;
}
// email
function CreateEmail($user,$domain,$password,$quota) {
@file($this->url.'/mail/doaddpop.html?email='.$user.'&domain='. $domain.'&password='.$password.'"a='.$quota);
}
function DeleteEmail($user,$domain) {
@file($this->url.'/mail/realdelpop.html?email='.$user.' &domain=' .$domain);
}
function UpdateEmailQuota($user,$domain,$quota) {
@file($this->url.'/mail/doeditquota.html?email='.$user.' &domain= '.$domain.'"a='.$quota);
}
function UpdateEmailPassword($user,$domain,$password) {
@file($this->url.'/mail/dopasswdpop.html?email='.$user.' &domain= '.$domain.'&password='.$password);
}
// ftp
function CreateFTP($login,$password,$homedir,$quota) {
@file($this->url.'/ftp/doaddftp.html?login='.$login.'&password=' .$password.'&homedir='.$homedir.'"a='.$quota);
}
function DeleteFTP($login) {
@file($this->url.'/ftp/realdodelftp.html?login='.$login);
}
function UpdateFTPQuota($login,$quota) {
@file($this->url.'/ftp/doeditquota.html?acct='.$login.'"a='. $quota);
}
function UpdateFTPPassword($login,$password) {
@file($this->url.'/ftp/dopasswdftp.html?acct='.$login.'&password ='.$password);
}
// subdomains
function CreateSubDomain ($domain,$root) {
@file($this->url.'/subdomain/doadddomain.html?domain='.$domain. '&rootdomain='.$root);
}
function DeleteSubDomain ($domain,$root) {
@file($this->url.'/subdomain/dodeldomain.html?domain='.$domain. '_'.$root);
}
function CreateRedirect ($domain,$root,$url) {
@file($this->url.'/subdomain/saveredirect.html?domain='.$domain. '_'.$root.'&url='.urlencode($url));
}
function DeleteRedirect ($domain,$root) {
@file($this->url.'/subdomain/donoredirect.html?domain='.$domain. '_'.$root);
}
}
?>How to use the class
First you must include the class
include('cpanel.php');
Then you must create a new object
$cpanel = new cpanel();
Lets go quickly through the different functions
CreateEmail($user,$domain,$password,$quota)
$user: The username before the @
$domain: After the @ (without the www.)
$password: Password for the email account
$quota: How much space you give the email account (0=unlimited)
Example:
$cpanel->CreateEmail('Mark','mydomain.com','password','10');
DeleteEmail($user,$domain)
$user: The username before the @
$domain: After the @ (without the www.)
Example:
$cpanel->DeleteEmail('Mark','mydomain.com');
UpdateEmailQuota($user,$domain,$quota)
$user: The username before the @
$domain: After the @ (without the www.)
$quota: How much space you give the email account (0=unlimited)
Example:
$cpanel->UpdateEmailQuota('Mark','mydomain.com','0');
UpdateEmailPassword($user,$domain,$password)
$user: The username before the @
$domain: After the @ (without the www.)
$password: Password for the email account
Example:
$cpanel->UpdateEmailPassword('Mark','mydomain.com','newpassword');
CreateFTP($login,$password,$homedir,$quota)
$login: This will be the username. Usually it will get the domain name added ie @yourdomain.com
$password: Password for the email account (always from your rot directory)
$homedir: Which directory he can access. (!!carefull don't give them access to your root directory.)
$quota: How much space you give the ftp account (0=unlimited)
Example:
$cpanel->CreateFTP('Mark','password','/Mark','100');
DeleteFTP($login)
$login: This will be the username without the @yourdomain.com
Example:
$cpanel->DeleteFTP('Mark');
UpdateFTPQuota($login,$quota)
$login: This will be the username without the @yourdomain.com
$quota: How much space you give the ftp account (0=unlimited)
Example:
$cpanel->UpdateFTPQuota('Mark','200');
UpdateFTPPassword($login,$password)
$login: This will be the username without the @yourdomain.com
$quota: How much space you give the ftp account (0=unlimited)
Example:
$cpanel->UpdateFTPPassword('Mark','newpassword');
CreateSubDomain ($domain,$root)
$domain: the subdomain name i.e. Mark
$root: the domain i.e. yourdomain.com
Example:
$cpanel->CreateSubDomain('Mark','mydomain.com',);
DeleteSubDomain ($domain,$root)
$domain: the subdomain name i.e. Mark
$root: the domain i.e. yourdomain.com
Example:
$cpanel->DeleteSubDomain('Mark','mydomain.com');
CreateRedirect ($domain,$root,$url)
$domain: the subdomain name i.e. Mark
$root: the domain i.e. yourdomain.com
$url: where you which to redirect the user to i.e. http://www.yournewdomain.com
Example:
$cpanel->CreateRedirect('Mark','mydomain.com',
'http://www.mynewdomain.com');
DeleteRedirect ($domain,$root)
$domain: the subdomain name i.e. Mark
$root: the domain i.e. yourdomain.com
Example:
$cpanel->DeleteRedirect('Mark','mydomain.com');
So I hope this will help you and please remember that you need to test loads before you can release this functionality to your users. You need to make sure that it is safe which I can guarantee. Just imagine if something goes wrong and you give one of your users access to your root directory.
Enjoy