Cpanel Automatic Subdomain and FTP account creation
Hi everyone,
I have a smal script that allows me to add a subdomain to cpanel using php, however I can not figure out how to add an ftp account.
Ideally I would like a form with 2 boxes one for subdomain/ftp username and one for password
When processed the script would create the subdomain (which it does now) then create the FTP account for this domain using the subdomain as a username and password specified and finally echo the result.
This is what I have so far for the subdomain addition...
Code:
<?php
// cpanel user
define('CPANELUSER','username');
// cpanel password
define('CPANELPASS','password');
// cPanel skin (mainly "x")
// Check http://www.zubrag.com/articles/determine-cpanel-skin.php
// to know it for sure
define('CPANEL_SKIN','x3');
// Default domain (subdomains will be created for this domain)
// Will be used if not passed via parameter and not set in subdomains file
define('DOMAIN','domainname');
/////////////// END OF INITIAL SETTINGS ////////////////////////
////////////////////////////////////////////////////////////////
function getVar($name, $def = '') {
if (isset($_REQUEST[$name]) && ($_REQUEST[$name] != ''))
return $_REQUEST[$name];
else
return $def;
}
$cpaneluser=getVar('cpaneluser', CPANELUSER);
$cpanelpass=getVar('cpanelpass', CPANELPASS);
$cpanel_skin = getVar('cpanelskin', CPANEL_SKIN);
if (isset($_REQUEST["subdomain"])) {
// get parameters passed via URL or form, emulate string from file
$doms = array( getVar('domain', DOMAIN) . ";" . $_REQUEST["subdomain"]);
if (getVar('domain', DOMAIN) == '') die("You must specify domain name");
}
else {
// open file with domains list
$doms = @file(INPUT_FILE);
if (!$doms) {
// file does not exist, show input form
echo "
<form method='post'>
Subdomain:<input name='subdomain'><br>
<input type='submit' value='Create Subdomain' style='border:1px solid black'>
</form>";
die();
}
}
// create subdomain
function subd($host,$port,$ownername,$passw,$request) {
$sock = fsockopen('localhost',2082);
if(!$sock) {
print('Socket error');
exit();
}
$authstr = "$ownername:$passw";
$pass = base64_encode($authstr);
$in = "GET $request\r\n";
$in .= "HTTP/1.0\r\n";
$in .= "Host:$host\r\n";
$in .= "Authorization: Basic $pass\r\n";
$in .= "\r\n";
fputs($sock, $in);
while (!feof($sock)) {
$result .= fgets ($sock,128);
}
fclose( $sock );
return $result;
}
foreach($doms as $dom) {
$lines = explode(';',$dom);
if (count($lines) == 2) {
// domain and subdomain passed
$domain = trim($lines[0]);
$subd = trim($lines[1]);
}
else {
// only subdomain passed
$domain = getVar('domain', DOMAIN);
$subd = trim($lines[0]);
}
// http://[domainhere]:2082/frontend/x/subdomain/doadddomain.html?domain=[subdomain here]&rootdomain=[domain here]
$request = "/frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$domain&domain=$subd";
$result = subd('localhost',2082,$cpaneluser,$cpanelpass,$request);
$show = strip_tags($result);
echo "domain created!";
}
?>
Here is the other script, please can anyone help me or show me how to combine the 2 scripts
Code:
<?php
###############################################################
# cPanel FTP Account Creator 1.0
###############################################################
# Visit http://www.zubrag.com/scripts/ for updates
###############################################################
# Required parameters:
# - domain - create ftp account for this domain
# - fuser - ftp account username
# - fpass - ftp account password
# - fquota - ftp account quota
# - fhomedir - ftp account home directory (home folder)
#
# Sample run: cpanel-add-ftp.php?domain=reseller.com&fuser=ftp555&fpass=ftp12345&fquota=50&fhomedir=/
#
# This script can also be run from another PHP script. This may
# be helpful if you have some user interface already in place and
# want to automatically create FTP accounts from there.
# In this case you have to setup following variables instead of
# passing them via url as parameters:
# - $domain - new account domain
# - $fuser - new ftp account username
# - $fpass - new ftp account password
# - $fquota - account quota
# - $fhomedir - user's home directory
#
# Feel free to post your questions and comments at http://www.zubrag.com/forum/
#
###############################################################
#####################################################################################
############## START OF SETTINGS. YOU MAY EDIT BELOW ######################
#####################################################################################
// Cpanel username and password
$user = "your-cpanel-username-here";
$pass = "your-cpanel-password-here";
// cpanel skin. For more info on what is your skin check
// this url http://www.zubrag.com/articles/determine-cpanel-skin.php
$skin = "x";
#####################################################################################
############## END OF SETTINGS. DO NOT EDIT BELOW #######################
#####################################################################################
function getVar($name, $def = '') {
if (isset($_REQUEST[$name]))
return $_REQUEST[$name];
else
return $def;
}
// ftp account for domain
if (!isset($domain)) {
$domain = getVar('domain');
}
// ftp user
if (!isset($fuser)) {
$fuser = getVar('fuser');
}
// ftp password
if (!isset($fpass)) {
$fpass = getVar('fpass');
}
// ftp quota
if (!isset($fquota)) {
$fquota = getVar('fquota');
}
// ftp homedir
if (!isset($fhomedir)) {
$fhomedir = getVar('fhomedir');
}
if (empty($domain)) {
$frm = <<<EOD
<html>
<head>
<title>cPanel FTP Account Creator</title>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</head>
<body>
<style>
input { border: 1px solid black; }
</style>
<form method="post">
<h3>cPanel FTP Account Creator</h3>
<table border="0">
<tr><td>Domain:</td><td><input name="domain" size="30"></td><td>domain without www part</td></tr>
<tr><td>FTP Username:</td><td><input name="fuser" size="30"></td><td></td></tr>
<tr><td>FTP Password:</td><td><input name="fpass" size="30"></td><td></td></tr>
<tr><td>FTP Home Directory:</td><td><input name="fhomedir" size="30" value="/"></td><td></td></tr>
<tr><td>FTP Quota:</td><td><input name="fquota" size="30" value="0"></td><td>numeric ftp quota, Mb (0 for unlimited)<br>This parameter may not work with early cPanel versions.</td></tr>
<tr><td colspan="3"><br /><input type="submit" value="Create FTP Account"></td></tr>
</table>
</form>
</body>
</html>
EOD;
die($frm);
}
$url = "http://$user:$pass@$domain:2082/frontend/$skin/ftp/doaddftp.html?";
$url = $url . "login=$fuser&password=$fpass&homedir=$fhomedir"a=$fquota";
$result = @file_get_contents($url);
if ($result === FALSE) die("ERROR: FTP Account not created. Please make sure you passed correct parameters.");
echo $result;
?>
OK awesome, thank you I actually have it all working now, do you know how I could check to see if the subdomain already exists and if it does returns an error instead of continuing?
the script is causing my whole shared hosting account to become corrupted, here is the error from the hosting company
Error is in ScriptAlias cgi-bin entry for subdomain in apache config, once added its putting entry as
I have the same FTP script and have no need for the other but I would like to know the following:
1, How do I make the script hide the following so they can not be changed by the user:
(a) Domain?
(b) FTP Home Directory?
(c) FTP Quota?
2, I need the home directory at 1(b) to start at home/MYURL/public_ftp/ and then add the person's username automatically to the end so it makes their ftp folder there. How can this be done? (I have added the public_ftp/ to the value in that line then a statement at the end for the customer to add their username but it looks naff and unprofessional)
3, How can I make it check to see if the account already exists and, if it does, ask the user to try again with a different name?
At the moment, if the username is already being used, it will just update the password if it is different. So if user 1 is username = abc & password = 123 and user 2 is username = abc & password = 789 user 1 can no longer get access and user 2 can see all user 1's files! VERY BAD SECURITY FLAW!
4, When a successful account is created, it shows links to other stuff that are not on my site, nor do I want to actually add files for those links. How do I stop it from creating those links?
5, At the moment my script looks like this:
<tr><td>Domain:</td><td><input name="domain" size="30" value ="MYDOMAINNAME"></td><td>DO NOT CHANGE THIS SETTING!</td></tr>
<tr><td>FTP Username:</td><td><input name="fuser" size="30"></td><td>Please enter a Username</td></tr>
<tr><td>FTP Password:</td><td><input name="fpass" size="30"></td><td>Please enter a Password</td></tr>
<tr><td>FTP Home Directory:</td><td><input name="fhomedir" size="30" value="/public_ftp/"></td><td>Please add your Username to the end of /public_ftp/</td></tr>
<tr><td>FTP Quota:</td><td><input name="fquota" size="30" value="0"></td><td>DO NOT CHANGE THIS SETTING!<br></td></tr>
<tr><td colspan="3"><br /><input type="submit" value="Create FTP Account"></td></tr>
I have tried the following three things so far, all with different results, neither of which acceptable.
(a) Set the value to 0 but the text still shows in the input fields
(b) Deleted the value completely but same effect as 5(a)
(c) Deleted the selected lines completely but that way just keeps it going back to the same page without creating the account nor showing any errors.
6, I have also added the following to the bottom of the script, but before the ?>
$to = "myemail@mysite";
$subject = "FTP Account Created!";
$message = "Someone has created a FTP account via your site.";
$from = "myemail@mysite";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
This sends me an email when someone lands on the page but I need it to only send the email when they click on create account and the account is successful. How would I make it only send an email when the account has been made successfully?
Thanks in advance to anyone who can help with these issues!
Bookmarks