<?php
// Add a user to filezilla ftp server
// Created 10/10/2007
// Author: Alex Duchesne

if (create_ftpuser('username''password''C:\\path'$droit='239')) echo 'OK';

function 
create_ftpuser($username$password$path$permissions='239') {
    if (
$socket fsockopen('127.0.0.1''14147'$errno$errstr10)) {
        
$ConnectionState=1;
        while (
$readString .= fread($socket,8192)) {
            switch (
$ConnectionState) {
                case 
1:
                    if (
substr($readString,0,3) == 'FZS') {
                        
$ConnectionState 2;
                        
fputs($socketbytes2string(array(8,0,0,0,0,12,1,0,0,0,0)));
                    }
                    
$readString='';
                    break;

                case 
2:
                    switch(
ord($readString[0])) {
                        case 
33:
                            
fputs($socketbytes2string(array(32,0,0,0,0)));
                            break;
                            
                        case 
9:
                            
fputs($socketbytes2string(array(24,0,0,0,0)));
                            
$ConnectionState=3;
                            break;
                    }
                    
$readString='';
                    break;
                    
                case 
3:
                    switch(
ord($readString[0])) {
                        case 
25:
                            if (
strlen($readString) == ((ord(substr($readString,2,1)) * 256) + ord(substr($readString,1,1)) + 5)) {
                                
$passwordmd5($password);
                                
$readString substr($readString,4);
                                
$datass='';
                                echo 
'<b>Nombre de comptes sur le serveur FTP: '.ord(substr($readString,4,1)).'</b><br>';
                                
$datass bytes2string(array(0,0,0,ord(substr($readString,4,1))+1)).substr($readString,5);
                                
$datass.= bytes2string(array(0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,1,0,strlen($chemin))).$chemin;
                                
$datass.= bytes2string(array(0,0,1,$droit,0,0,10,0,0,0,0,10,0,0,0,0,0,0,strlen($pseudo)));
                                
$datass.= $pseudo.bytes2string(array(0strlen($password))).$password;
                                
fputs($socket,create_request($datass,'24'));
                                
$ConnectionState=4;
                                echo 
strlen($readString);
                                
$readString='';
                            }
                            break;
                    }
                    break;
                    
                case 
4:
                    switch(
ord($readString[0])) {
                        case 
25:
                            
fclose($socket);
                            return 
true;
                            break;
                    }
                    
$readString='';
                    break;
            }
        }
    }
    
fclose($socket);
    return 
false;
}

function 
bytes2string($data){
    
$retour '';
    if (
is_array($data)) {
        foreach (
$data as $char) {
            
$retour .= chr($char);
        }
    }
    return 
$retour;
}

function 
create_request($data,$commande) {
    
$req chr($commande);
    if (
strlen($data)>255) {
        
$var1 = (strlen($data)/256);
        
$var1 substr($var1,0,strpos($var1,'.') );
        
$var2 = (strlen($data)-($var1*256));
        
$req .=chr($var2).chr($var1);
        
$req .=chr(0).chr(0);
    } else {
        
$var1 = (strlen($data)-($var1*256));
        
$req .=chr($var1).chr(0);
        
$req .=chr(0).chr(0);
    }
    
$req .= $data;
    return 
$req;
}
?>