Shabat Closer

Sunday, February 10, 2013

PHP: filezilla ftp server - auto create user

html + php script to create new user in filezilla ftp server


<?php
define("FTP_CONF_FILE",'C:/Program Files/FileZilla Server/FileZilla Server.xml');
define("FTP_EXE_FILE","C:\\Program Files\\FileZilla Server\\FileZilla server.exe");

function create_user($user_name,$password,$ftpUserFolder,$createBackup=true,$reloadFTP=true){ 
 $xmlfile = FTP_CONF_FILE;
 $xmlfolder=dirname($xmlfilename);
 
 $xmlbackupfile = $xmlfolder . @date("Y-m-d-H-i-s") . '_FileZilla_Server.xml';
 
 $xml = simplexml_load_file($xmlfile);
 // Copy Config for backup before each change, too.
 

 $isvalid = isUserID($user_name);
 if (!$isvalid){
  echo  "Username $user_name is invalid\n";
  return false;
 }
 
 
 if(check_user_exists($xml,$user_name)){
  echo "Username $user_name already exists...\n";
  return false;
 }
 if ($createBackup){
  if (!copy($xmlfile,$xmlbackupfile)){
   echo "Problem creating xml backup file";
   return false;
  }
 }


 $user = $xml->Users->addChild('User');
 $user->addAttribute('Name', $user_name);
 
 add_option($user,'Pass',md5($password));
 add_option($user,'Group',null);
 add_option($user,'Bypass server userlimit','0');
 add_option($user,'User Limit','0');
 add_option($user,'IP Limit','0');
 add_option($user,'Enabled','1');
 add_option($user,'Comments','none');
 add_option($user,'ForceSsl','0');

 $filter = $user->addChild('IpFilter');
 $filter->addChild('Disallowed');
 $filter->addChild('Allowed');

 $permissions = $user->addChild('Permissions');
 $permission = $permissions->addChild('Permission');

 $permission->addAttribute('Dir', str_replace("/","\\",$ftpUserFolder));

 add_option($permission,'FileRead','1');
 add_option($permission,'FileWrite','1');
 add_option($permission,'FileDelete','1');
 add_option($permission,'FileAppend','1');
 add_option($permission,'DirCreate','1');
 add_option($permission,'DirDelete','1');
 add_option($permission,'DirList','1');
 add_option($permission,'DirSubdirs','1');
 add_option($permission,'IsHome','1');
 add_option($permission,'AutoCreate','1');

 $speed = $user->addChild('SpeedLimits');
 $speed->addAttribute('DlType', '1');
 $speed->addAttribute('DlLimit', '10');
 $speed->addAttribute('ServerDlLimitBypass', '0');
 $speed->addAttribute('UlType', '1');
 $speed->addAttribute('UlLimit', '10');
 $speed->addAttribute('ServerUlLimitBypass', '0');
 $speed->addChild('Download');
 $speed->addChild('Upload');

 if(!$rv = $xml->asXML($xmlfile)){
  echo ('SimpleXML could not write file');
  return false;
 }


 //Change file encoding from UTF8 to ISO-8859-1
 $dom = new DOMDocument("1.0","ISO-8859-1");
 $dom->preserveWhiteSpace = false;
 $dom->formatOutput = true;
 if(!$dom->load($xmlfile) || !$dom->save($xmlfile)){
  echo ('DOMDocument could not change file enconding from UTF8 to ISO-8859-1');
  return false;
 }
 
 
 if ($reloadFTP){
  $ftpExecutable = '"'.FTP_EXE_FILE.'" /reload-config';
  $command = $ftpExecutable;
  system($command, $retval);
 }
 
 return true;
 
}
function isUserID($username){return preg_match('/^[A-Za-z0-9][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/', $username);}
function isValid($str){return !preg_match('/[^A-Za-z0-9\_\-]/', $str);}
function add_option($xmlobj,$name,$value){
 $option = $xmlobj->addChild('Option', $value);
 $option->addAttribute('Name', $name);
}

function check_user_exists($xml,$username){
 $children=$xml->Users->children();
 foreach($children as $child){
  if ($child->getName()=='User'){
   foreach($child->attributes() as $attributes ){
    if(trim($attributes) == trim($username)){
     echo "Username $username already exits... \n";
     return true;
    }
   }
  }
 }
 return false;
}







if ($_POST){
 create_user($_POST["user_name"],$_POST["password"],$_POST["ftp_folder"]);
}

?><html><body>
Create FTP User in filzilla server.<br/><br/><form method="POST">
 <table>
  <tr>
   <td>User name : </td>
   <td><input typr="text" name="user_name"/></td>
  </tr>
  <tr>
   <td>Password : </td>
   <td><input typr="text" name="password" /></td>
  </tr>
  <tr>
   <td>FTP root folder : </td>
   <td><input typr="text" name="ftp_folder" /></td>
  </tr>
 </table>
 <input type="submit"></form></body></html>

Enjoy!

4 comments:

  1. Hi there,

    How are you?

    Is it possible to use this script to make cpanel ftp account.

    ReplyDelete
  2. Hello Moshe,

    thank you for sharing your script :)

    I implemented your function in a backend system, but find that after I create a new FZ user using the script, I have a hard time logging in using FZ client. Did you experience anything similar ?

    Best regards,
    Mark

    ReplyDelete
  3. same as mark, your code works but then i cannot connect to the server using filezilla client

    ReplyDelete
  4. Hello Moshe i m using same on local FTP server it's working fine. but on windows 10 live server i m getting this error: SimpleXML could not write file on server. please help me to resolved this issue.

    Thanks.

    ReplyDelete