Location: PHPKode > scripts > Insert FTP > insert-ftp/insertFTP.php
<?php
/**
Copyright (C) 2007 Jeanneret Internux
 * Table structure :
 *
 * show columns from ftpd
  +-------------+---------------+------+-----+---------+-------+
  | Field       | Type          | Null | Key | Default | Extra |
  +-------------+---------------+------+-----+---------+-------+
  | User        | varchar(16)   |      | PRI |         |       |
  | status      | enum('0','1') |      |     | 0       |       |
  | Password    | varchar(64)   |      |     |         |       |
  | Uid         | varchar(11)   |      |     | -1      |       |
  | Gid         | varchar(11)   |      |     | -1      |       |
  | Dir         | varchar(128)  |      |     |         |       |
  | ULBandwidth | smallint(5)   |      |     | 0       |       |
  | DLBandwidth | smallint(5)   |      |     | 0       |       |
  | comment     | tinytext      |      |     |         |       |
  | ipaccess    | varchar(15)   |      |     | *       |       |
  | QuotaSize   | smallint(5)   |      |     | 0       |       |
  | QuotaFiles  | int(11)       |      |     | 0       |       |
  +-------------+---------------+------+-----+---------+-------+
 *
 * @author C. Jeanneret <hide@address.com>
 */

/*
Copyright (C) 2007 Jeanneret Internux

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

// mysql distant
define('SRVHOSTD','',true);
define('SRVUSERD','',true);
define('SRVPSWDD','',true);
define('SRVBDDD','pureftpd',true);
define('DEBUGSQL',false,true);

/**
 * Manage ftp accounts on a distant serveur through mysql.
 *
 * All vars should be private. But since we're with php4, it can't manage it.
 * All functions are present, so we can use under php5 and change "var" to "private"
 *
 * @var $projectID    : project ID, used as username
 * @var $uid          : user id on distant server. default : 2001
 * @var $gid          : group id on distant server. default : 2001
 * @var $dir          : directory PATH in absolute. Class adds username at the end of this var.
 * @var $ULBandwidth  : max upload quota. default : infinite
 * @var $DLBandwidth  : max download quota. default : infinite
 * @var $comment      : comment about the user. default : FTP User for project '.$projectID;
 * @var $ipaccess     : limits access for this user from only one IP. default : *
 * @var $quotaHDD     : quota for hdd space. default : 100Mo
 * @var $quotaFiles   : quota for files quantity. default : infinite
 * @var $passwd       : password for user. generated.
 * @var $sqlResult    : results of mysql_query. gives all informations about table content for selected user.
 */
class insertFTP {
  var $projectID;
  var $uid;
  var $gid;
  var $dir;
  var $ULBandwidth;
  var $DLBandwidth;
  var $comment;
  var $ipaccess;
  var $quotaHDD;
  var $quotaFile;
  var $passwd;
  var $sqlResult;

  /**
   * creates the object.
   *
   * @param int $id
   * @return insertFTP
   */
  function insertFTP($id,$quota=100,$passSize=6) {
    $this->projectID = $id;
    $this->uid = '2001';
    $this->gid = '2001';
    $this->dir = '/home/ftp/'.$id;
    $this->ULBandwidth = '0'; // no limit
    $this->DLBandwidth = '0'; // no limit
    $this->comment = 'FTP User for project '.$id;
    $this->ipaccess = '*'; // allowed from everywhere
    $this->quotaHDD = $quota;
    $this->quotaFile = '0'; // infinite
    $this->passwd = $this->makePasswd($passSize);
  }

  /**
   * Just does the connection
   * @link sqlConnectDistante()
   * @link closeDistant()
   */
  function sqlConnectDistante() {
    $link = mysql_connect(SRVHOSTD, SRVUSERD, SRVPSWDD);
    if(!link) return ($this->sqlErrorDistant('Connexion'));
    $select = mysql_select_db(SRVBDDD);
    if(!$select) return ($this->sqlErrorDistant('Selection'));
    return true;
  }

  /**
   * Manages mysql errors
   *
   * @param string $sql
   * @link sqlConnectDistante()
   * @link closeDistant()
   */
  function sqlErrorDistant($sql='') {
    if(DEBUGSQL) {
      echo '<p>Fatal error with your query: <br />'.$sql.'<br /> it returns:</p>';
      echo mysql_error();
      echo '<hr />';
      return false;
    } else {
      echo 'Mooomyyyy someone makes me crash !';
      return false;
    }
  }

  /**
   * Builds a password
   *
   * @param int $size
   * @return string
   * @link updatePass()
   */
  function makePasswd($size=6) {
    $chaine = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    srand((double)microtime()*1000000);
    for($i=0; $i<$size; $i++) {
      $passwd .= $chaine{rand()%strlen($chaine)};
    }
    return $passwd;
  }

  /**
   * Closes distant connection
   * @link sqlConnectDistante()
   * @link sqlConnectDistante()
   */
  function closeDistant() {
    mysql_close();
  }

  /**
   * Inserts values in distant table. manage connections as well.
   * Password is hashed with MD5 algorithm.
   *
   * @return true if success.
   * @link insertFTP()
   * @link sqlConnectDistante()
   * @link sqlErrorDistant()
   * @link closeDistant()
   */
  function insert() {
    $sql = 'INSERT INTO ftpd VALUES ("'.$this->projectID.'","1",MD5("'.$this->passwd.'"),"'.$this->uid.'","'.$this->gid.'","'.$this->dir.'","'.$this->ULBandwidth.'","'.$this->DLBandwidth.'","'.$this->comment.'","'.$this->ipaccess.'","'.$this->quotaHDD.'","'.$this->quotaFile.'")';
    $this->sqlConnectDistante() or die();
    mysql_query($sql) or $this->sqlErrorDistant($sql);
    $this->closeDistant();
    return true;
  }

  /**
   * Set user status to 0 (not active)
   *
   * @return true if success.
   * @link sqlConnectDistante()
   * @link sqlErrorDistant()
   * @link closeDistant()
   */
  function blockUser() {
    $sql = 'UPDATE ftpd SET status="0" WHERE User='.$this->projectID.' LIMIT 1';
    $this->sqlConnectDistante() or die('Fatal : cannot connect to database');
    mysql_query($sql) or $this->sqlErrorDistant($sql);
    $this->closeDistant();
    return true;
  }

  /**
   * Set quota for user. default : 100Mo
   *
   * @param int $amount
   */
  function setQuotasHDD($amount) {
    $this->quotaHDD = $amount;
  }

  /**
   * Set file quantity. dfault : no limit
   *
   * @param int $amount
   */
  function setQuotaFile($amount) {
    $this->quotaFile = $amount;
  }

  /**
   * Updates user quotas for project
   *
   * @return true if success.
   * @link sqlConnectDistante()
   * @link sqlErrorDistant()
   * @link closeDistant()
   */
  function updateQuotas() {
    $sql = 'UPDATE ftpd SET QuotaSize="'.$this->quotaHDD.'", QuotaFiles="'.$this->quotaFile.'" WHERE User='.$this->projectID.' LIMIT 1';
    $this->sqlConnectDistante() or die('Fatal : cannot connect to database');
    mysql_query($sql) or $this->sqlErrorDistant($sql);
    $this->closeDistant();
    return true;
  }

  /**
   * Gives us current password. Empty if not created.
   *
   * @return string
   */
  function getPasswd() {
    return $this->passwd;
  }

  /**
   * Grab all datas from distant table for one user
   *
   * @return object sql result
   * @link sqlConnectDistante()
   * @link sqlErrorDistant()
   * @link closeDistant()
   */
  function getContent() {
    $sql = 'SELECT * FROM ftpd WHERE User="'.$this->projectID.'" LIMIT 1';
    $this->sqlConnectDistante() or die('Fatal : cannot connect to database');
    $res = mysql_query($sql) or $this->sqlErrorDistant($sql);
    $this->closeDistant();
    $this->sqlResult = mysql_fetch_assoc($res);
    return $this->sqlResult;
  }

  /**
   * Get FTP usage
   *
   * @return object sql query
   */
  function getStats() {
    $sql = 'SELECT * FROM quotas WHERE User="'.$this->projectID.'" LIMIT 1';
    $this->sqlConnectDistante() or die('Fatal : cannot connect to database');
    $res = mysql_query($sql) or $this->sqlErrorDistant($sql);
    $this->closeDistant();
    $this->sqlResult = mysql_fetch_assoc($res);
    return $this->sqlResult;
  }

  /**
   * Check if our project has an account or not
   *
   * @return true if so.
   * @link sqlConnectDistante()
   * @link sqlErrorDistant()
   * @link closeDistant()
   */
  function checkAccount() {
    $sql = 'SELECT COUNT(User) from ftpd WHERE User="'.$this->projectID.'"';
    $this->sqlConnectDistante();
    $q = mysql_query($sql) or $this->sqlErrorDistant($sql);
    $this->closeDistant();
    return (mysql_result($q,0,0)!=0);
  }

  /**
   * Update user password.
   * @link makePasswd()
   * @link sqlConnectDistante()
   * @link sqlErrorDistant()
   * @link closeDistant()
   */
  function updatePass() {
    $sql = 'UPDATE ftpd SET Password=MD5("'.$this->passwd.'") WHERE User='.$this->projectID.' LIMIT 1';
    $this->sqlConnectDistante();
    mysql_query($sql) or $this->sqlErrorDistant($sql);
    $this->closeDistant();

  }
}
?>
Return current item: Insert FTP