spottedhaggis

Registered
Jan 13, 2010
2
0
51
I am a complete newbie at this, and had been looking for something that would allow me to create and delete ftp accounts from a web page rather than through cpanel (for multiple user access)

I stumbled accross a script that someone created to do just this in the cpanel forum, and would like to know how I can go about modifying this script so a form is used to enter the new account details.

The basic idea is that new users who want ftp access fill in a request form which un-benown to them creates said FTP account, however I need this script to get the information from the form, and either email the account details to myself or better still add the information to a database.

Can anyone help me out. and thanks to whoever created this, it does work perfectly but only on the pre-defined information within.

PHP:
<?php
$conn = array ( 'host' => '127.0.0.1'
              , 'port' => '2083'
              , 'user' => 'kbc3ur0'
              , 'pass' => 'will1996'
              ) ;

function cpanel_api ( $conn , $type , $user , $pass , $domain )
{
 if ( $conn [ 'port' ] == '2087' || $conn [ 'port' ] == '2083' || $conn [ 'port' ] == '443' )
 {
 	$site = 'https://' . $conn [ 'host' ] . ':' . $conn [ 'port' ] ;
 } else {
 	$site = 'http://' . $conn [ 'host' ] . ':' . $conn [ 'port' ] ;
 }
 
 switch ( $type )
 {
 	case 'add_ftp' :
 	 $xmlin = '<cpanelaction><module>Ftp</module><func>addftp</func><apiversion>1</apiversion><args>' . $user . '</args><args>' . $pass . '</args><args>/' . $user . '</args><args>20</args></cpanelaction>' ;
 	 break ;
 	 
 	case 'del_ftp' :
 	 $xmlin = '<cpanelaction><module>Ftp</module><func>delftp</func><apiversion>1</apiversion><args>' . $user . '</args><args>1</args></cpanelaction>' ;
 	 break ;
 	 
 	default :
 	 echo 'Type error' ;
 }
 
 if ( $type == 'add_ftp' || $type == 'del_ftp' )
 {
  $query = '/xml-api/cpanel?user=' . $conn [ 'user' ] . '&xmlin=' . $xmlin ;
  
  $curl = curl_init ( ) ;
  curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER , 0 ) ;
  curl_setopt ( $curl, CURLOPT_RETURNTRANSFER , 1 ) ;
  curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST , 0 ) ;
  curl_setopt ( $curl, CURLOPT_USERPWD        , $conn [ 'user' ] . ':' . $conn [ 'pass' ] ) ;
  curl_setopt ( $curl, CURLOPT_HEADER         , 0 ) ;
  curl_setopt ( $curl, CURLOPT_URL            , $site . $query ) ;
  $result = curl_exec ( $curl ) ;
  curl_close ( $curl ) ;
 }
}

/***** ADD Subdomain and FTP Account *****/
cpanel_api ( $conn , 'add_ftp'       , 'test' , '0123456789' , '.domain.com' ) ;

/***** DEL Subdomain and FTP Account *****/
//cpanel_api ( $conn , 'del_ftp'       , 'test' , '0123456789' , '.domain.com' ) ;
?>Don't forget to use in your php.ini :
 

spottedhaggis

Registered
Jan 13, 2010
2
0
51
cPanel with api or similar

I am on a shared host account with cPanel, and am trying to create a page that I can put a form on to that new users can use to create a new FTP account.

The form works exactly as it is, however I am not sure how secure the form is, and it is meant to pull in the skin as well but it fails, so I thought maybe you could advise on a better method for this.

Basically I want to be able to create, delete ftp accounts (including folders) from a single page, php or whatever is needed, rather than give access to multiple users on cPanel which would be a bad idea (they would play and its a company wide hosting)

Here is what I have, cant remember where I got it, but it works, in that it creates the ftp account.

PHP:
<?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 = "xxxxxxx";
$pass = "xxxxxxx";

// cpanel skin. For more info on what is your skin check 
// this url      http://www.zubrag.com/articles/determine-cpanel-skin.php 
$skin = "x3";

#####################################################################################
##############          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>KBC Networks FTP Account Creator</h3>
<table border="0">
<tr><td>Domain:</td><td><input name="domain" value="" size="30"></td><td>Leasve this as it is</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" value="/public_html/ftp/" size="30"></td><td>Add the username and trailing slash to the end of this line</td></tr>
<tr><td>FTP Quota:</td><td><input name="fquota" size="30" value="0"></td><td>Leave the quota as 0 for unlimited</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:[email protected]$domain:2082/frontend/x3/ftp/doaddftp.html?";
$url = $url . "login=$fuser&password=$fpass&homedir=$fhomedir&quota=$fquota";
$result = @file_get_contents($url);
if ($result === FALSE) die("ERROR: FTP Account not created. Please make sure you passed correct parameters.");
echo $result;

?>
Also, if anyonme knows, the Echo $result, I would either like that to appear in a new popup or better still be emailed to me or another admin.