Net_FTP
[ class tree: Net_FTP ] [ index: Net_FTP ] [ all elements ]

Source for file FTP.php

Documentation is available at FTP.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | Net_FTP Version 1.3                                                  |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 2001-2004 Tobias Schlitt                               |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 3.0 of the PHP license,       |
  8. // | that is available at through the world-wide-web at                   |
  9. // | http://www.php.net/license/3_0.txt.                                  |
  10. // | If you did not receive a copy of the PHP license and are unable to   |
  11. // | obtain it through the world-wide-web, please send a note to          |
  12. // | license@php.net so we can mail you a copy immediately.               |
  13. // +----------------------------------------------------------------------+
  14. // | Authors:       Tobias Schlitt <toby@php.net>                         |
  15. // +----------------------------------------------------------------------+
  16. //
  17. // $Id: FTP.php,v 1.26 2004/06/14 22:26:48 toby Exp $
  18.  
  19. require_once 'PEAR.php';
  20.  
  21.  
  22. /**
  23.  * Option to let the ls() method return only files.
  24.  *
  25.  * @since 1.3
  26.  * @name NET_FTP_FILES_ONLY
  27.  * @see Net_FTP::ls()
  28.  */
  29. define('NET_FTP_FILES_ONLY'0true);
  30.  
  31. /**
  32.  * Option to let the ls() method return only directories.
  33.  *
  34.  * @since 1.3
  35.  * @name NET_FTP_DIRS_ONLY
  36.  * @see Net_FTP::ls()
  37.  */
  38. define('NET_FTP_DIRS_ONLY'1true);
  39.  
  40. /**
  41.  * Option to let the ls() method return directories and files (default).
  42.  *
  43.  * @since 1.3
  44.  * @name NET_FTP_DIRS_FILES
  45.  * @see Net_FTP::ls()
  46.  */
  47. define('NET_FTP_DIRS_FILES'2true);
  48.  
  49. /**
  50.  * Option to let the ls() method return the raw directory listing from ftp_rawlist().
  51.  *
  52.  * @since 1.3
  53.  * @name NET_FTP_RAWLIST
  54.  * @see Net_FTP::ls()
  55.  */
  56. define('NET_FTP_RAWLIST'3true);
  57.  
  58.  
  59. /**
  60.  * Error code to indicate a failed connection
  61.  * This error code indicates, that the connection you tryed to set up
  62.  * could not be established. Check your connection settings (host & port)!
  63.  *
  64.  * @since 1.3
  65.  * @name NET_FTP_ERR_CONNECT_FAILED
  66.  * @see Net_FTP::connect()
  67.  */
  68. define('NET_FTP_ERR_CONNECT_FAILED'-1);
  69.  
  70. /**
  71.  * Error code to indicate a failed login
  72.  * This error code indicates, that the login to the FTP server failed. Check
  73.  * your user data (username & password).
  74.  *
  75.  * @since 1.3
  76.  * @name NET_FTP_ERR_LOGIN_FAILED
  77.  * @see Net_FTP::login()
  78.  */
  79. define('NET_FTP_ERR_LOGIN_FAILED'-2);
  80.  
  81. /**
  82.  * Error code to indicate a failed directory change
  83.  * The cd() method failed. Ensure that the directory you wanted to access exists.
  84.  *
  85.  * @since 1.3
  86.  * @name NET_FTP_ERR_DIRCHANGE_FAILED
  87.  * @see Net_FTP::cd()
  88.  */
  89. define('NET_FTP_ERR_DIRCHANGE_FAILED'2)// Compatibillity reasons!
  90.  
  91. /**
  92.  * Error code to indicate that Net_FTP could not determine the current path
  93.  * The cwd() method failed and could not determine the path you currently reside
  94.  * in on the FTP server.
  95.  *
  96.  * @since 1.3
  97.  * @name NET_FTP_ERR_DETERMINEPATH_FAILED
  98.  * @see Net_FTP::pwd()
  99.  */
  100. define('NET_FTP_ERR_DETERMINEPATH_FAILED'4)// Compatibillity reasons!
  101.  
  102. /**
  103.  * Error code to indicate that the creation of a directory failed
  104.  * The directory you tryed to create could not be created. Check the
  105.  * access rights on the parent directory!
  106.  *
  107.  * @since 1.3
  108.  * @name NET_FTP_ERR_CREATEDIR_FAILED
  109.  * @see Net_FTP::mkdir()
  110.  */
  111. define('NET_FTP_ERR_CREATEDIR_FAILED'-4);
  112.  
  113. /**
  114.  * Error code to indicate that the EXEC execution failed.
  115.  * The execution of a command using EXEC failed. Ensure, that your
  116.  * FTP server supports the EXEC command.
  117.  *
  118.  * @since 1.3
  119.  * @name NET_FTP_ERR_EXEC_FAILED
  120.  * @see Net_FTP::execute()
  121.  */
  122. define('NET_FTP_ERR_EXEC_FAILED'-5);
  123.  
  124. /**
  125.  * Error code to indicate that the SITE command failed.
  126.  * The execution of a command using SITE failed. Ensure, that your
  127.  * FTP server supports the SITE command.
  128.  *
  129.  * @since 1.3
  130.  * @name NET_FTP_ERR_SITE_FAILED
  131.  * @see Net_FTP::site()
  132.  */
  133. define('NET_FTP_ERR_SITE_FAILED'-6);
  134.  
  135. /**
  136.  * Error code to indicate that the CHMOD command failed.
  137.  * The execution of CHMOD failed. Ensure, that your
  138.  * FTP server supports the CHMOD command and that you have the appropriate
  139.  * access rights to use CHMOD.
  140.  *
  141.  * @since 1.3
  142.  * @name NET_FTP_ERR_CHMOD_FAILED
  143.  * @see Net_FTP::chmod()
  144.  */
  145. define('NET_FTP_ERR_CHMOD_FAILED'-7);
  146.  
  147. /**
  148.  * Error code to indicate that a file rename failed
  149.  * The renaming of a file on the server failed. Ensure that you have the
  150.  * appropriate access rights to rename the file.
  151.  *
  152.  * @since 1.3
  153.  * @name NET_FTP_ERR_RENAME_FAILED
  154.  * @see Net_FTP::rename()
  155.  */
  156. define('NET_FTP_ERR_RENAME_FAILED'-8);
  157.  
  158. /**
  159.  * Error code to indicate that the MDTM command failed
  160.  * The MDTM command is not supported for directories. Ensure that you gave
  161.  * a file path to the mdtm() method, not a directory path.
  162.  *
  163.  * @since 1.3
  164.  * @name NET_FTP_ERR_MDTMDIR_UNSUPPORTED
  165.  * @see Net_FTP::mdtm()
  166.  */
  167. define('NET_FTP_ERR_MDTMDIR_UNSUPPORTED'-9);
  168.  
  169. /**
  170.  * Error code to indicate that the MDTM command failed
  171.  * The MDTM command failed. Ensure that your server supports the MDTM command.
  172.  *
  173.  * @since 1.3
  174.  * @name NET_FTP_ERR_MDTM_FAILED
  175.  * @see Net_FTP::mdtm()
  176.  */
  177. define('NET_FTP_ERR_MDTM_FAILED'-10);
  178.  
  179. /**
  180.  * Error code to indicate that a date returned by the server was misformated
  181.  * A date string returned by your server seems to be missformated and could not be
  182.  * parsed. Check that the server is configured correctly. If you're sure, please
  183.  * send an email to the auhtor with a dumped output of $ftp->ls('./', NET_FTP_RAWLIST);
  184.  * to get the date format supported.
  185.  *
  186.  * @since 1.3
  187.  * @name NET_FTP_ERR_DATEFORMAT_FAILED
  188.  * @see Net_FTP::mdtm(), Net_FTP::ls()
  189.  */
  190. define('NET_FTP_ERR_DATEFORMAT_FAILED'-11);
  191.  
  192. /**
  193.  * Error code to indicate that the SIZE command failed
  194.  * The determination of the filesize of a file failed. Ensure that your server supports the
  195.  * SIZE command.
  196.  *
  197.  * @since 1.3
  198.  * @name NET_FTP_ERR_SIZE_FAILED
  199.  * @see Net_FTP::size()
  200.  */
  201. define('NET_FTP_ERR_SIZE_FAILED'-12);
  202.  
  203. /**
  204.  * Error code to indicate that a local file could not be overwritten
  205.  * You specified not to overwrite files. Therefore the local file has not been
  206.  * overwriten. If you want to get the file overwriten, please set the option to
  207.  * do so.
  208.  *
  209.  * @since 1.3
  210.  * @name NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN
  211.  * @see Net_FTP::get(), Net_FTP::getRecursive()
  212.  */
  213. define('NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN'-13);
  214.  
  215. /**
  216.  * Error code to indicate that a local file could not be overwritten
  217.  * Also you specified to overwrite the local file you want to download to,
  218.  * it has not been possible to do so. Check that you have the appropriate access
  219.  * rights on the local file to overwrite it.
  220.  *
  221.  * @since 1.3
  222.  * @name NET_FTP_ERR_OVERWRITELOCALFILE_FAILED
  223.  * @see Net_FTP::get(), Net_FTP::getRecursive()
  224.  */
  225. define('NET_FTP_ERR_OVERWRITELOCALFILE_FAILED'-14);
  226.  
  227. /**
  228.  * Error code to indicate that the file you wanted to upload does not exist
  229.  * The file you tried to upload does not exist. Ensure that it exists.
  230.  *
  231.  * @since 1.3
  232.  * @name NET_FTP_ERR_LOCALFILENOTEXIST
  233.  * @see Net_FTP::put(), Net_FTP::putRecursive()
  234.  */
  235. define('NET_FTP_ERR_LOCALFILENOTEXIST'-15);
  236.  
  237. /**
  238.  * Error code to indicate that a remote file could not be overwritten
  239.  * You specified not to overwrite files. Therefore the remote file has not been
  240.  * overwriten. If you want to get the file overwriten, please set the option to
  241.  * do so.
  242.  *
  243.  * @since 1.3
  244.  * @name NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN
  245.  * @see Net_FTP::put(), Net_FTP::putRecursive()
  246.  */
  247. define('NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN'-16);
  248.  
  249. /**
  250.  * Error code to indicate that the upload of a file failed
  251.  * The upload you tried failed. Ensure that you have appropriate access rights
  252.  * to upload the desired file.
  253.  *
  254.  * @since 1.3
  255.  * @name NET_FTP_ERR_UPLOADFILE_FAILED
  256.  * @see Net_FTP::put(), Net_FTP::putRecursive()
  257.  */
  258. define('NET_FTP_ERR_UPLOADFILE_FAILED'-17);
  259.  
  260. /**
  261.  * Error code to indicate that you specified an incorrect directory path
  262.  * The remote path you specified seems not to be a directory. Ensure that
  263.  * the path you specify is a directory and that the path string ends with
  264.  * a /.
  265.  *
  266.  * @since 1.3
  267.  * @name NET_FTP_ERR_REMOTEPATHNODIR
  268.  * @see Net_FTP::putRecursive(), Net_FTP::getRecursive()
  269.  */
  270. define('NET_FTP_ERR_REMOTEPATHNODIR'-18);
  271.  
  272. /**
  273.  * Error code to indicate that you specified an incorrect directory path
  274.  * The local path you specified seems not to be a directory. Ensure that
  275.  * the path you specify is a directory and that the path string ends with
  276.  * a /.
  277.  *
  278.  * @since 1.3
  279.  * @name NET_FTP_ERR_LOCALPATHNODIR
  280.  * @see Net_FTP::putRecursive(), Net_FTP::getRecursive()
  281.  */
  282. define('NET_FTP_ERR_LOCALPATHNODIR'-19);
  283.  
  284. /**
  285.  * Error code to indicate that a local directory failed to be created
  286.  * You tried to create a local directory through getRecursive() method,
  287.  * which has failed. Ensure that you have the appropriate access rights
  288.  * to create it.
  289.  *
  290.  * @since 1.3
  291.  * @name NET_FTP_ERR_CREATELOCALDIR_FAILED
  292.  * @see Net_FTP::getRecursive()
  293.  */
  294. define('NET_FTP_ERR_CREATELOCALDIR_FAILED'-20);
  295.  
  296. /**
  297.  * Error code to indicate that the provided hostname was incorrect
  298.  * The hostname you provided was invalid. Ensure to provide either a
  299.  * full qualified domain name or an IP address.
  300.  *
  301.  * @since 1.3
  302.  * @name NET_FTP_ERR_HOSTNAMENOSTRING
  303.  * @see Net_FTP::setHostname()
  304.  */
  305. define('NET_FTP_ERR_HOSTNAMENOSTRING'-21);
  306.  
  307. /**
  308.  * Error code to indicate that the provided port was incorrect
  309.  * The port number you provided was invalid. Ensure to provide either a
  310.  * a numeric port number greater zero.
  311.  *
  312.  * @since 1.3
  313.  * @name NET_FTP_ERR_PORTLESSZERO
  314.  * @see Net_FTP::setPort()
  315.  */
  316. define('NET_FTP_ERR_PORTLESSZERO'-22);
  317.  
  318. /**
  319.  * Error code to indicate that you provided an invalid mode constant
  320.  * The mode constant you provided was invalid. You may only provide
  321.  * FTP_ASCII or FTP_BINARY.
  322.  *
  323.  * @since 1.3
  324.  * @name NET_FTP_ERR_NOMODECONST
  325.  * @see Net_FTP::setMode()
  326.  */
  327. define('NET_FTP_ERR_NOMODECONST'-23);
  328.  
  329. /**
  330.  * Error code to indicate that you provided an invalid timeout
  331.  * The timeout you provided was invalid. You have to provide a timeout greater
  332.  * or equal to zero.
  333.  *
  334.  * @since 1.3
  335.  * @name NET_FTP_ERR_TIMEOUTLESSZERO
  336.  * @see Net_FTP::Net_FTP(), Net_FTP::setTimeout()
  337.  */
  338. define('NET_FTP_ERR_TIMEOUTLESSZERO'-24);
  339.  
  340. /**
  341.  * Error code to indicate that you provided an invalid timeout
  342.  * An error occured while setting the timeout. Ensure that you provide a
  343.  * valid integer for the timeount and that your PHP installation works
  344.  * correctly.
  345.  *
  346.  * @since 1.3
  347.  * @name NET_FTP_ERR_SETTIMEOUT_FAILED
  348.  * @see Net_FTP::Net_FTP(), Net_FTP::setTimeout()
  349.  */
  350. define('NET_FTP_ERR_SETTIMEOUT_FAILED'-25);
  351.  
  352. /**
  353.  * Error code to indicate that the provided extension file doesn't exist
  354.  * The provided extension file does not exist. Ensure to provided an
  355.  * existant extension file.
  356.  *
  357.  * @since 1.3
  358.  * @name NET_FTP_ERR_EXTFILENOTEXIST
  359.  * @see Net_FTP::getExtensionFile()
  360.  */
  361. define('NET_FTP_ERR_EXTFILENOTEXIST'-26);
  362.  
  363. /**
  364.  * Error code to indicate that the provided extension file is not readable
  365.  * The provided extension file is not readable. Ensure to have sufficient
  366.  * access rights for it.
  367.  *
  368.  * @since 1.3
  369.  * @name NET_FTP_ERR_EXTFILEREAD_FAILED
  370.  * @see Net_FTP::getExtensionFile()
  371.  */
  372. define('NET_FTP_ERR_EXTFILEREAD_FAILED'-27);
  373.  
  374. /**
  375.  * Error code to indicate that the deletion of a file failed
  376.  * The specified file could not be deleted. Ensure to have sufficient
  377.  * access rights to delete the file.
  378.  *
  379.  * @since 1.3
  380.  * @name NET_FTP_ERR_EXTFILEREAD_FAILED
  381.  * @see Net_FTP::rm()
  382.  */
  383. define('NET_FTP_ERR_DELETEFILE_FAILED'-28);
  384.  
  385. /**
  386.  * Error code to indicate that the deletion of a directory faild
  387.  * The specified file could not be deleted. Ensure to have sufficient
  388.  * access rights to delete the file.
  389.  *
  390.  * @since 1.3
  391.  * @name NET_FTP_ERR_EXTFILEREAD_FAILED
  392.  * @see Net_FTP::rm()
  393.  */
  394. define('NET_FTP_ERR_DELETEDIR_FAILED'-29);
  395.  
  396. /**
  397.  * Error code to indicate that the directory listing failed
  398.  * PHP could not list the directory contents on the server. Ensure
  399.  * that your server is configured appropriate.
  400.  *
  401.  * @since 1.3
  402.  * @name NET_FTP_ERR_RAWDIRLIST_FAILED
  403.  * @see Net_FTP::ls()
  404.  */
  405. define('NET_FTP_ERR_RAWDIRLIST_FAILED'-30);
  406.  
  407. /**
  408.  * Error code to indicate that the directory listing failed
  409.  * The directory listing format your server uses seems not to
  410.  * be supported by Net_FTP. Please send the output of the
  411.  * call ls('./', NET_FTP_RAWLIST); to the author of this
  412.  * class to get it supported.
  413.  *
  414.  * @since 1.3
  415.  * @name NET_FTP_ERR_DIRLIST_UNSUPPORTED
  416.  * @see Net_FTP::ls()
  417.  */
  418. define('NET_FTP_ERR_DIRLIST_UNSUPPORTED'-31);
  419.  
  420. /**
  421.  * Error code to indicate failed disconnecting
  422.  * This error code indicates, that disconnection was not possible.
  423.  *
  424.  * @since 1.3
  425.  * @name NET_FTP_ERR_DISCONNECT_FAILED
  426.  * @see Net_FTP::disconnect()
  427.  */
  428. define('NET_FTP_ERR_DISCONNECT_FAILED'-32);
  429.  
  430. /**
  431.  * Error code to indicate that the username you provided was invalid.
  432.  * Check that you provided a non-empty string as the username.
  433.  *
  434.  * @since 1.3
  435.  * @name NET_FTP_ERR_USERNAMENOSTRING
  436.  * @see Net_FTP::setUsername()
  437.  */
  438. define('NET_FTP_ERR_USERNAMENOSTRING'-33);
  439.  
  440. /**
  441.  * Error code to indicate that the username you provided was invalid.
  442.  * Check that you provided a non-empty string as the username.
  443.  *
  444.  * @since 1.3
  445.  * @name NET_FTP_ERR_PASSWORDNOSTRING
  446.  * @see Net_FTP::setPassword()
  447.  */
  448. define('NET_FTP_ERR_PASSWORDNOSTRING'-33);
  449.  
  450.  
  451. /**
  452.  * Class for comfortable FTP-communication
  453.  *
  454.  * This class provides comfortable communication with FTP-servers. You may do everything
  455.  * enabled by the PHP-FTP-extension and further functionalities, like recursive-deletion,
  456.  * -up- and -download. Another feature is to create directories recursively.
  457.  *
  458.  * @since     PHP 4.2.3
  459.  * @author    Tobias Schlitt <toby@php.net>
  460.  * @see       http://www.schlitt.info
  461.  * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
  462.  */
  463.  
  464. class Net_FTP extends PEAR 
  465. {
  466.     /**
  467.      * The host to connect to
  468.      *
  469.      * @access  private
  470.      * @var     string 
  471.      */
  472.     
  473.     var $_hostname;
  474.  
  475.     /**
  476.      * The port for ftp-connection (standard is 21)
  477.      *
  478.      * @access  private
  479.      * @var     int 
  480.      */
  481.     
  482.     var $_port = 21;
  483.  
  484.     /**
  485.      * The username for login
  486.      *
  487.      * @access  private
  488.      * @var     string 
  489.      */
  490.     
  491.     var $_username;
  492.  
  493.     /**
  494.      * The password for login
  495.      *
  496.      * @access  private
  497.      * @var     string 
  498.      */
  499.     
  500.     var $_password;
  501.  
  502.     /**
  503.      * Determine whether to use passive-mode (true) or active-mode (false)
  504.      *
  505.      * @access  private
  506.      * @var     bool 
  507.      */
  508.     
  509.     var $_passv;
  510.  
  511.     /**
  512.      * The standard mode for ftp-transfer
  513.      *
  514.      * @access  private
  515.      * @var     int 
  516.      */
  517.     
  518.     var $_mode = FTP_BINARY;
  519.  
  520.     /**
  521.      * This holds the handle for the ftp-connection
  522.      *
  523.      * @access  private
  524.      * @var     resource 
  525.      */
  526.     
  527.     var $_handle;
  528.  
  529.     /**
  530.      * Contains the timeout for FTP operations
  531.      *
  532.      * @access  private
  533.      * @var     int 
  534.      * @since   1.3
  535.      */
  536.     
  537.     var $_timeout = 90;
  538.         
  539.     /**
  540.      * Saves file-extensions for ascii- and binary-mode
  541.      *
  542.      * The array contains 2 sub-arrays ("ascii" and "binary"), which both contain
  543.      * file-extensions without the "." (".php" = "php").
  544.      *
  545.      * @access  private
  546.      * @var     array 
  547.      */
  548.     
  549.     var $_file_extensions;
  550.  
  551.     /**
  552.      * ls match
  553.      * Matches the ls entries against a regex and maps the resulting array to speaking names
  554.      *
  555.      * @access  private
  556.      * @var     array 
  557.      * @since   1.3
  558.      */
  559.     
  560.     var $_ls_match = array(
  561.         'unix'    => array(
  562.             'pattern' => '/(?:(d)|.)([rwxt-]+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\S+\s+\S+\s+\S+)\s+(.+)/',
  563.             'map'     => array('name'=>8,'size'=>6,'rights'=>2,'user'=>4,'group'=>5,
  564.                               'files_inside'=>3,'date'=>7,'is_dir'=>1)
  565.         ),
  566.         'windows' => array(
  567.             'pattern' => '/(.+)\s+(.+)\s+((<DIR>)|[0-9]+)\s+(.+)/',
  568.             'map'     => array('name'=>5,'date'=>1,'is_dir'=>3)
  569.         )
  570.     );
  571.     
  572.     /**
  573.      * matcher
  574.      * Stores the matcher for the current connection
  575.      *
  576.      * @access  private
  577.      * @var     array 
  578.      * @since   1.3
  579.      */
  580.     var $_matcher = null;
  581.     
  582.     /**
  583.      * Holds all Net_FTP_Observer objects
  584.      * that wish to be notified of new messages.
  585.      *
  586.      * @var     array 
  587.      * @access  private
  588.      * @since   1.3
  589.      */
  590.     
  591.     var $_listeners = array();
  592.  
  593.     /**
  594.      * This generates a new FTP-Object. The FTP-connection will not be established, yet.
  595.      * You can leave $host and $port blank, if you want. The $host will not be set
  596.      * and the $port will be left at 21. You have to set the $host manualy before
  597.      * trying to connect or with the connect() method.
  598.      *
  599.      * @access  public
  600.      * @param   string $host    (optional) The hostname
  601.      * @param   int    $port    (optional) The port
  602.      * @param   int    $timeout (optional) Sets the standard timeout
  603.      * @return  void 
  604.      * @see     Net_FTP::setHostname(), Net_FTP::setPort(), Net_FTP::connect()
  605.      */
  606.     
  607.     function Net_FTP($host = null$port = null$timeout = 90)
  608.     {
  609.         $this->PEAR();
  610.         if (isset($host)) {
  611.             $this->setHostname($host);
  612.         }
  613.         if (isset($port)) {
  614.             $this->setPort($port);
  615.         }
  616.         $this->_timeout $timeout;
  617.         $this->_file_extensions[FTP_ASCII= array();
  618.         $this->_file_extensions[FTP_BINARY= array();
  619.     }
  620.  
  621.     /**
  622.      * This function generates the FTP-connection. You can optionally define a
  623.      * hostname and/or a port. If you do so, this data is stored inside the object.
  624.      *
  625.      * @access  public
  626.      * @param   string $host    (optional) The Hostname
  627.      * @param   int    $port    (optional) The Port
  628.      * @return  mixed           True on success, otherwise PEAR::Error
  629.      * @see     NET_FTP_ERR_CONNECT_FAILED
  630.      */
  631.     
  632.     function connect($host = null$port = null)
  633.     {
  634.         $this->_matcher = null;
  635.         if (isset($host)) {
  636.             $this->setHostname($host);
  637.         }
  638.         if (isset($port)) {
  639.             $this->setPort($port);
  640.         }
  641.         $handle @ftp_connect($this->getHostname()$this->getPort()$this->_timeout);
  642.         if (!$handle{
  643.             return $this->raiseError("Connection to host failed"NET_FTP_ERR_CONNECT_FAILED);
  644.         else {
  645.             $this->_handle =$handle;
  646.             return true;
  647.         }
  648.     }
  649.  
  650.     /**
  651.      * This function close the FTP-connection
  652.      *
  653.      * @access  public
  654.      * @return  bool|PEAR_ErrorReturns true on success, PEAR_Error on failure
  655.      */
  656.     
  657.     function disconnect()
  658.     {
  659.         $res @ftp_close($this->_handle);
  660.         if (!$res{
  661.             return PEAR::raiseError('Disconnect failed.'NET_FTP_ERR_DISCONNECT_FAILED);
  662.         }
  663.         return true;
  664.     }
  665.  
  666.     /**
  667.      * This logges you into the ftp-server. You are free to specify username and password
  668.      * in this method. If you specify it, the values will be taken into the corresponding
  669.      * attributes, if do not specify, the attributes are taken.
  670.      *
  671.      * @access  public
  672.      * @param   string $username  (optional) The username to use
  673.      * @param   string $password  (optional) The password to use
  674.      * @return  mixed             True on success, otherwise PEAR::Error
  675.      * @see     NET_FTP_ERR_LOGIN_FAILED
  676.      */
  677.     
  678.     function login($username = null$password = null)
  679.     {
  680.         if (!isset($username)) {
  681.             $username $this->getUsername();
  682.         else {
  683.             $this->setUsername($username);
  684.         }
  685.  
  686.         if (!isset($password)) {
  687.             $password $this->getPassword();
  688.         else {
  689.             $this->setPassword($password);
  690.         }
  691.  
  692.         $res @ftp_login($this->_handle$username$password);
  693.  
  694.         if (!$res{
  695.             return $this->raiseError("Unable to login"NET_FTP_ERR_LOGIN_FAILED);
  696.         else {
  697.             return true;
  698.         }
  699.     }
  700.  
  701.     /**
  702.      * This changes the currently used directory. You can use either an absolute
  703.      * directory-path (e.g. "/home/blah") or a relative one (e.g. "../test").
  704.      *
  705.      * @access  public
  706.      * @param   string $dir  The directory to go to.
  707.      * @return  mixed        True on success, otherwise PEAR::Error
  708.      * @see     NET_FTP_ERR_DIRCHANGE_FAILED
  709.      */
  710.     
  711.     function cd($dir)
  712.     {
  713.         $erg @ftp_chdir($this->_handle$dir);
  714.         if (!$erg{
  715.             return $this->raiseError("Directory change failed"NET_FTP_ERR_DIRCHANGE_FAILED);
  716.         else {
  717.             return true;
  718.         }
  719.     }
  720.  
  721.     /**
  722.      * Show's you the actual path on the server
  723.      * This function questions the ftp-handle for the actual selected path and returns it.
  724.      *
  725.      * @access  public
  726.      * @return  mixed        The actual path or PEAR::Error
  727.      * @see     NET_FTP_ERR_DETERMINEPATH_FAILED
  728.      */
  729.     
  730.     function pwd()
  731.     {
  732.         $res @ftp_pwd($this->_handle);
  733.         if (!$res{
  734.             return $this->raiseError("Could not determine the actual path."NET_FTP_ERR_DETERMINEPATH_FAILED);
  735.         else {
  736.             return $res;
  737.         }
  738.     }
  739.  
  740.     /**
  741.      * This works similar to the mkdir-command on your local machine. You can either give
  742.      * it an absolute or relative path. The relative path will be completed with the actual
  743.      * selected server-path. (see: pwd())
  744.      *
  745.      * @access  public
  746.      * @param   string $dir       Absolute or relative dir-path
  747.      * @param   bool   $recursive (optional) Create all needed directories
  748.      * @return  mixed             True on success, otherwise PEAR::Error
  749.      * @see     NET_FTP_ERR_CREATEDIR_FAILED
  750.      */
  751.     
  752.     function mkdir($dir$recursive = false)
  753.     {
  754.         $dir $this->_construct_path($dir);
  755.         if ($recursive === false){
  756.             $res @ftp_mkdir($this->_handle$dir);
  757.             if (!$res{
  758.                 return $this->raiseError("Creation of '$dir' failed"NET_FTP_ERR_CREATEDIR_FAILED);
  759.             else {
  760.                 return true;
  761.             }
  762.         else {
  763.             $pos = 0;
  764.             if(strpos($dir'/'=== false{
  765.                 return $this->mkdir($dir,false);
  766.             }
  767.             $elements = array();
  768.             while (false !== ($pos strpos($dir'/'$pos + 1))){
  769.                 $elements[substr($dir0$pos);
  770.             }
  771.             foreach ($elements as $element){
  772.                 $res $this->mkdir($elementfalse);
  773.                 if($res !== true{
  774.                     return $res;
  775.                 }
  776.             }
  777.             return true;
  778.         }
  779.     }
  780.  
  781.     /**
  782.      * This method tries executing a command on the ftp, using SITE EXEC.
  783.      *
  784.      * @access  public
  785.      * @param   string $command The command to execute
  786.      * @return  mixed           The result of the command (if successfull), otherwise PEAR::Error
  787.      * @see     NET_FTP_ERR_EXEC_FAILED
  788.      */
  789.     
  790.     function execute($command)
  791.     {
  792.         $res @ftp_exec($this->_handle$command);
  793.         if (!$res{
  794.             return $this->raiseError("Execution of command '$command' failed."NET_FTP_ERR_EXEC_FAILED);
  795.         else {
  796.             return $res;
  797.         }
  798.     }
  799.  
  800.     /**
  801.      * Execute a SITE command on the server
  802.      * This method tries to execute a SITE command on the ftp server.
  803.      *
  804.      * @access  public
  805.      * @param   string $command   The command with parameters to execute
  806.      * @return  mixed             True if successful, otherwise PEAR::Error
  807.      * @see     NET_FTP_ERR_SITE_FAILED
  808.      */
  809.     
  810.     function site($command)
  811.     {
  812.         $res @ftp_site($this->_handle$command);
  813.         if (!$res{
  814.             return $this->raiseError("Execution of SITE command '$command' failed."NET_FTP_ERR_SITE_FAILED);
  815.         else {
  816.             return $res;
  817.         }
  818.     }
  819.  
  820.     /**
  821.      * This method will try to chmod the file specified on the server
  822.      * Currently, you must give a number as the the permission argument (777 or
  823.      * similar). The file can be either a relative or absolute path.
  824.      * NOTE: Some servers do not support this feature. In that case, you will
  825.      * get a PEAR error object returned. If successful, the method returns true
  826.      *
  827.      * @access  public
  828.      * @param   mixed   $target        The file or array of files to set permissions for
  829.      * @param   integer $permissions   The mode to set the file permissions to
  830.      * @return  mixed                  True if successful, otherwise PEAR::Error
  831.      * @see     NET_FTP_ERR_CHMOD_FAILED
  832.      */
  833.     
  834.     function chmod($target$permissions)
  835.     {
  836.         // If $target is an array: Loop through it.
  837.         if (is_array($target)) {
  838.  
  839.             for ($i = 0; $i count($target)$i++{
  840.                 $res $this->chmod($target[$i]$permissions);
  841.                 if (PEAR::isError($res)) {
  842.                     return $res;
  843.                 // end if isError
  844.             // end for i < count($target)
  845.  
  846.         else {
  847.  
  848.             $res $this->site("CHMOD " $permissions " " $target);
  849.             if (!$res{
  850.                 return PEAR::raiseError("CHMOD " $permissions " " $target " failed"NET_FTP_ERR_CHMOD_FAILED);
  851.             else {
  852.                 return $res;
  853.             }
  854.  
  855.         // end if is_array
  856.  
  857.     // end method chmod
  858.  
  859.     /**
  860.      * This method will try to chmod a folder and all of its contents
  861.      * on the server. The target argument must be a folder or an array of folders
  862.      * and the permissions argument have to be an integer (i.e. 777).
  863.      * The file can be either a relative or absolute path.
  864.      * NOTE: Some servers do not support this feature. In that case, you
  865.      * will get a PEAR error object returned. If successful, the method
  866.      * returns true
  867.      *
  868.      * @access  public
  869.      * @param   mixed   $target        The folder or array of folders to
  870.      *                                  set permissions for
  871.      * @param   integer $permissions   The mode to set the folder
  872.      *                                  and file permissions to
  873.      * @return  mixed                  True if successful, otherwise PEAR::Error
  874.      * @see     NET_FTP_ERR_CHMOD_FAILED, NET_FTP_ERR_DETERMINEPATH_FAILED, NET_FTP_ERR_RAWDIRLIST_FAILED, NET_FTP_ERR_DIRLIST_UNSUPPORTED
  875.      */
  876.     
  877.     function chmodRecursive($target$permissions)
  878.     {
  879.         static $dir_permissions;
  880.  
  881.         if(!isset($dir_permissions))// Making directory specific permissions
  882.             $dir_permissions $this->_makeDirPermissions($permissions);
  883.         }
  884.  
  885.         // If $target is an array: Loop through it
  886.         if (is_array($target)) {
  887.  
  888.             for ($i = 0; $i count($target)$i++{
  889.                 $res $this->chmodRecursive($target[$i]$permissions);
  890.                 if (PEAR::isError($res)) {
  891.                     return $res;
  892.                 // end if isError
  893.             // end for i < count($target)
  894.  
  895.         else {
  896.  
  897.             $remote_path $this->_construct_path($target);
  898.  
  899.             // Chmod the directory itself
  900.             $result $this->chmod($remote_path$dir_permissions);
  901.  
  902.             if (PEAR::isError($result)) {
  903.                 return $result;
  904.             }
  905.  
  906.             // If $remote_path last character is not a slash, add one
  907.             if (substr($remote_pathstrlen($remote_path)-1!= "/"{
  908.  
  909.                 $remote_path .= "/";
  910.             }
  911.  
  912.             $dir_list = array();
  913.             $mode NET_FTP_DIRS_ONLY;
  914.             $dir_list $this->ls($remote_path$mode);
  915.             foreach ($dir_list as $dir_entry{
  916.  
  917.                 $remote_path_new $remote_path.$dir_entry["name"]."/";
  918.  
  919.                 // Chmod the directory we're about to enter
  920.                 $result $this->chmod($remote_path_new$dir_permissions);
  921.  
  922.                 if (PEAR::isError($result)) {
  923.                     return $result;
  924.                 }
  925.  
  926.                 $result $this->chmodRecursive($remote_path_new$permissions);
  927.  
  928.                 if (PEAR::isError($result)) {
  929.                     return $result;
  930.                 }
  931.  
  932.             // end foreach dir_list as dir_entry
  933.  
  934.             $file_list = array();
  935.             $mode NET_FTP_FILES_ONLY;
  936.             $file_list $this->ls($remote_path$mode);
  937.  
  938.             foreach ($file_list as $file_entry{
  939.  
  940.                 $remote_file $remote_path.$file_entry["name"];
  941.  
  942.                 $result $this->chmod($remote_file$permissions);
  943.  
  944.                 if (PEAR::isError($result)) {
  945.                     return $result;
  946.                 }
  947.  
  948.             // end foreach $file_list
  949.  
  950.         // end if is_array
  951.  
  952.         return true; // No errors
  953.  
  954.     // end method chmodRecursive
  955.  
  956.     /**
  957.      * Rename or move a file or a directory from the ftp-server
  958.      *
  959.      * @access  public
  960.      * @param   string $remote_from The remote file or directory original to rename or move
  961.      * @param   string $remote_to The remote file or directory final to rename or move
  962.      * @return  bool $res True on success, otherwise PEAR::Error
  963.      * @see     NET_FTP_ERR_RENAME_FAILED
  964.      */
  965.  
  966.     function rename ($remote_from$remote_to
  967.     {
  968.         $res @ftp_rename($this->_handle$remote_from$remote_to);
  969.         if(!$res{
  970.             return $this->raiseError("Could not rename ".$remote_from." to ".$remote_to." !"NET_FTP_ERR_RENAME_FAILED);
  971.         }
  972.         return true;
  973.     }
  974.  
  975.     /**
  976.      * This will return logical permissions mask for directory.
  977.      * if directory have to be writeable it have also be executable
  978.      *
  979.      * @access  private
  980.      * @param   string $permissions    File permissions in digits for file (i.e. 666)
  981.      * @return  string                 File permissions in digits for directory (i.e. 777)
  982.      */
  983.  
  984.     function _makeDirPermissions($permissions){
  985.         $permissions = (string)$permissions;
  986.  
  987.         for($i = 0; $i strlen($permissions)$i++)// going through (user, group, world)
  988.             if((int)$permissions{$i4 and !((int)$permissions{$i1))// Read permission is set
  989.                                                                             // but execute not yet
  990.                 (int)$permissions{$i= (int)$permissions{$i+ 1; // Adding execute flag
  991.             }
  992.         }
  993.  
  994.         return (string)$permissions;
  995.     }
  996.  
  997.     /**
  998.      * This will return the last modification-time of a file. You can either give this
  999.      * function a relative or an absolute path to the file to check.
  1000.      * NOTE: Some servers will not support this feature and the function works
  1001.      * only on files, not directories! When successful,
  1002.      * it will return the last modification-time as a unix-timestamp or, when $format is
  1003.      * specified, a preformated timestring.
  1004.      *
  1005.      * @access  public
  1006.      * @param   string $file    The file to check
  1007.      * @param   string $format  (optional) The format to give the date back
  1008.      *                           if not set, it will return a Unix timestamp
  1009.      * @return  mixed           Unix timestamp, a preformated date-string or PEAR::Error
  1010.      * @see     NET_FTP_ERR_MDTMDIR_UNSUPPORTED, NET_FTP_ERR_MDTM_FAILED, NET_FTP_ERR_DATEFORMAT_FAILED
  1011.      */
  1012.     
  1013.     function mdtm($file$format = null)
  1014.     {
  1015.         $file $this->_construct_path($file);
  1016.         if ($this->_check_dir($file)) {
  1017.             return $this->raiseError("Filename '$file' seems to be a directory."NET_FTP_ERR_MDTMDIR_UNSUPPORTED);
  1018.         }
  1019.         $res @ftp_mdtm($this->_handle$file);
  1020.         if ($res == -1{
  1021.             return $this->raiseError("Could not get last-modification-date of '$file'."NET_FTP_ERR_MDTM_FAILED);
  1022.         }
  1023.         if (isset($format)) {
  1024.             $res date($format$res);
  1025.             if (!$res{
  1026.                 return $this->raiseError("Date-format failed on timestamp '$res'."NET_FTP_ERR_DATEFORMAT_FAILED);
  1027.             }
  1028.         }
  1029.         return $res;
  1030.     }
  1031.  
  1032.     /**
  1033.      * This will return the size of a given file in bytes. You can either give this function
  1034.      * a relative or an absolute file-path. NOTE: Some servers do not support this feature!
  1035.      *
  1036.      * @access  public
  1037.      * @param   string $file   The file to check
  1038.      * @return  mixed          Size in bytes or PEAR::Error
  1039.      * @see     NET_FTP_ERR_SIZE_FAILED
  1040.      */
  1041.     
  1042.     function size($file)
  1043.     {
  1044.         $file $this->_construct_path($file);
  1045.         $res @ftp_size($this->_handle$file);
  1046.         if ($res == -1{
  1047.             return $this->raiseError("Could not determine filesize of '$file'."NET_FTP_ERR_SIZE_FAILED);
  1048.         else {
  1049.             return $res;
  1050.         }
  1051.     }
  1052.  
  1053.     /**
  1054.      * This method returns a directory-list of the current directory or given one.
  1055.      * To display the current selected directory, simply set the first parameter to null
  1056.      * or leave it blank, if you do not want to use any other parameters.
  1057.      * <BR><BR>
  1058.      * There are 4 different modes of listing directories. Either to list only
  1059.      * the files (using NET_FTP_FILES_ONLY), to list only directories (using
  1060.      * NET_FTP_DIRS_ONLY) or to show both (using NET_FTP_DIRS_FILES, which is default).
  1061.      * <BR><BR>
  1062.      * The 4th one is the NET_FTP_RAWLIST, which returns just the array created by the
  1063.      * ftp_rawlist()-function build into PHP.
  1064.      * <BR><BR>
  1065.      * The other function-modes will return an array containing the requested data.
  1066.      * The files and dirs are listed in human-sorted order, but if you select
  1067.      * NET_FTP_DIRS_FILES the directories will be added above the files,
  1068.      * but although both sorted.
  1069.      * <BR><BR>
  1070.      * All elements in the arrays are associative arrays themselves. The have the following
  1071.      * structure:
  1072.      * <BR><BR>
  1073.      * Dirs:<BR>
  1074.      *           ["name"]        =>  string The name of the directory<BR>
  1075.      *           ["rights"]      =>  string The rights of the directory (in style "rwxr-xr-x")<BR>
  1076.      *           ["user"]        =>  string The owner of the directory<BR>
  1077.      *           ["group"]       =>  string The group-owner of the directory<BR>
  1078.      *           ["files_inside"]=>  string The number of files/dirs inside the directory
  1079.      *                                      excluding "." and ".."<BR>
  1080.      *           ["date"]        =>  int The creation-date as Unix timestamp<BR>
  1081.      *           ["is_dir"]      =>  bool true, cause this is a dir<BR>
  1082.      * <BR><BR>
  1083.      * Files:<BR>
  1084.      *           ["name"]        =>  string The name of the file<BR>
  1085.      *           ["size"]        =>  int Size in bytes<BR>
  1086.      *           ["rights"]      =>  string The rights of the file (in style "rwxr-xr-x")<BR>
  1087.      *           ["user"]        =>  string The owner of the file<BR>
  1088.      *           ["group"]       =>  string The group-owner of the file<BR>
  1089.      *           ["date"]        =>  int The creation-date as Unix timestamp<BR>
  1090.      *           ["is_dir"]      =>  bool false, cause this is a file<BR>
  1091.      *
  1092.      * @access  public
  1093.      * @param   string $dir   (optional) The directory to list or null, when listing the current directory.
  1094.      * @param   int    $mode  (optional) The mode which types to list (files, directories or both).
  1095.      * @return  mixed         The directory list as described above or PEAR::Error on failure.
  1096.      * @see     NET_FTP_DIRS_FILES, NET_FTP_DIRS_ONLY, NET_FTP_FILES_ONLY, NET_FTP_RAWLIST, NET_FTP_ERR_DETERMINEPATH_FAILED, NET_FTP_ERR_RAWDIRLIST_FAILED, NET_FTP_ERR_DIRLIST_UNSUPPORTED
  1097.      */
  1098.      
  1099.     function ls($dir = null$mode = NET_FTP_DIRS_FILES)
  1100.     {
  1101.         if (!isset($dir)) {
  1102.             $dir @ftp_pwd($this->_handle);
  1103.             if (!$dir{
  1104.                 return $this->raiseError("Could not retrieve current directory"NET_FTP_ERR_DETERMINEPATH_FAILED);
  1105.             }
  1106.         }
  1107.         if (($mode != NET_FTP_FILES_ONLY&& ($mode != NET_FTP_DIRS_ONLY&& ($mode != NET_FTP_RAWLIST)) {
  1108.             $mode NET_FTP_DIRS_FILES;
  1109.         }
  1110.  
  1111.         switch ($mode{
  1112.             case NET_FTP_DIRS_FILES:    $res $this->_ls_both $dir );
  1113.                                         break;
  1114.             case NET_FTP_DIRS_ONLY:     $res $this->_ls_dirs $dir );
  1115.                                         break;
  1116.             case NET_FTP_FILES_ONLY:    $res $this->_ls_files $dir );
  1117.                                         break;
  1118.             case NET_FTP_RAWLIST:       $res @ftp_rawlist($this->_handle$dir);
  1119.                                         break;
  1120.         }
  1121.  
  1122.         return $res;
  1123.     }
  1124.  
  1125.     /**
  1126.      * This method will delete the given file or directory ($path) from the server
  1127.      * (maybe recursive).
  1128.      *
  1129.      * Whether the given string is a file or directory is only determined by the last
  1130.      * sign inside the string ("/" or not).
  1131.      *
  1132.      * If you specify a directory, you can optionally specify $recursive as true,
  1133.      * to let the directory be deleted recursive (with all sub-directories and files
  1134.      * inherited).
  1135.      *
  1136.      * You can either give a absolute or relative path for the file / dir. If you choose to
  1137.      * use the relative path, it will be automatically completed with the actual
  1138.      * selected directory.
  1139.      *
  1140.      * @access  public
  1141.      * @param   string $path      The absolute or relative path to the file / directory.
  1142.      * @param   bool   $recursive (optional)
  1143.      * @return  mixed             True on success, otherwise PEAR::Error
  1144.      * @see     NET_FTP_ERR_DELETEFILE_FAILED, NET_FTP_ERR_DELETEDIR_FAILED, NET_FTP_ERR_REMOTEPATHNODIR
  1145.      */
  1146.     
  1147.     function rm($path$recursive = false)
  1148.     {
  1149.         $path $this->_construct_path($path);
  1150.  
  1151.         if ($this->_check_dir($path)) {
  1152.             if ($recursive{
  1153.                 return $this->_rm_dir_recursive($path);
  1154.             else {
  1155.                 return $this->_rm_dir($path);
  1156.             }
  1157.         else {
  1158.             return $this->_rm_file($path);
  1159.         }
  1160.     }
  1161.  
  1162.     /**
  1163.      * This function will download a file from the ftp-server. You can either spcify a absolute
  1164.      * path to the file (beginning with "/") or a relative one, which will be completed
  1165.      * with the actual directory you selected on the server. You can specify
  1166.      * the path to which the file will be downloaded on the local
  1167.      * maschine, if the file should be overwritten if it exists (optionally, default is
  1168.      * no overwriting) and in which mode (FTP_ASCII or FTP_BINARY) the file should be
  1169.      * downloaded (if you do not specify this, the method tries to determine it automatically
  1170.      * from the mode-directory or uses the default-mode, set by you). If you give a relative
  1171.      * path to the local-file, the script-path is used as basepath.
  1172.      *
  1173.      * @access  public
  1174.      * @param   string $remote_file The absolute or relative path to the file to download
  1175.      * @param   string $local_file  The local file to put the downloaded in
  1176.      * @param   bool   $overwrite   (optional) Whether to overwrite existing file
  1177.      * @param   int    $mode        (optional) Either FTP_ASCII or FTP_BINARY
  1178.      * @return  mixed               True on success, otherwise PEAR::Error
  1179.      * @see     NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED
  1180.      */
  1181.     
  1182.     function get($remote_file$local_file$overwrite = false$mode = null)
  1183.     {
  1184.         if (!isset($mode)) {
  1185.             $mode $this->checkFileExtension($remote_file);
  1186.         }
  1187.  
  1188.         $remote_file $this->_construct_path($remote_file);
  1189.  
  1190.         if (@file_exists($local_file&& !$overwrite{
  1191.             return $this->raiseError("Local file '$local_file' exists and may not be overwriten."NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN);
  1192.         }
  1193.         if (@file_exists($local_file&& !@is_writeable($local_file&& $overwrite{
  1194.             return $this->raiseError("Local file '$local_file' is not writeable. Can not overwrite."NET_FTP_ERR_OVERWRITELOCALFILE_FAILED);
  1195.         }
  1196.  
  1197.         if (@function_exists('ftp_nb_get')){
  1198.             $res @ftp_nb_get($this->_handle$local_file$remote_file$mode);
  1199.             while ($res == FTP_MOREDATA{
  1200.                 $this->_announce('nb_get');
  1201.                 $res @ftp_nb_continue ($this->_handle);
  1202.             }
  1203.         else {
  1204.             $res @ftp_get($this->_handle$local_file$remote_file$mode);
  1205.         }
  1206.         if (!$res{
  1207.             return $this->raiseError("File '$remote_file' could not be downloaded to '$local_file'."NET_FTP_ERR_OVERWRITELOCALFILE_FAILED);
  1208.         else {
  1209.             return true;
  1210.         }
  1211.     }
  1212.  
  1213.     /**
  1214.      * This function will upload a file to the ftp-server. You can either specify a absolute
  1215.      * path to the remote-file (beginning with "/") or a relative one, which will be completed
  1216.      * with the actual directory you selected on the server. You can specify
  1217.      * the path from which the file will be uploaded on the local
  1218.      * maschine, if the file should be overwritten if it exists (optionally, default is
  1219.      * no overwriting) and in which mode (FTP_ASCII or FTP_BINARY) the file should be
  1220.      * downloaded (if you do not specify this, the method tries to determine it automatically
  1221.      * from the mode-directory or uses the default-mode, set by you). If you give a relative
  1222.      * path to the local-file, the script-path is used as basepath.
  1223.      *
  1224.      * @access  public
  1225.      * @param   string $local_file  The local file to upload
  1226.      * @param   string $remote_file The absolute or relative path to the file to upload to
  1227.      * @param   bool   $overwrite   (optional) Whether to overwrite existing file
  1228.      * @param   int    $mode        (optional) Either FTP_ASCII or FTP_BINARY
  1229.      * @return  mixed               True on success, otherwise PEAR::Error
  1230.      * @see     NET_FTP_ERR_LOCALFILENOTEXIST, NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN, NET_FTP_ERR_UPLOADFILE_FAILED
  1231.      */
  1232.     
  1233.     function put($local_file$remote_file$overwrite = false$mode = null)
  1234.     {
  1235.         if (!isset($mode)) {
  1236.             $mode $this->checkFileExtension($local_file);
  1237.         }
  1238.         $remote_file $this->_construct_path($remote_file);
  1239.  
  1240.         if (!@file_exists($local_file)) {
  1241.             return $this->raiseError("Local file '$local_file' does not exist."NET_FTP_ERR_LOCALFILENOTEXIST);
  1242.         }
  1243.         if ((@ftp_size($this->_handle$remote_file!= -1&& !$overwrite{
  1244.             return $this->raiseError("Remote file '$remote_file' exists and may not be overwriten."NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN);
  1245.         }
  1246.  
  1247.         if (function_exists('ftp_nb_put')){
  1248.             $res @ftp_nb_put($this->_handle$remote_file$local_file$mode);
  1249.             while ($res == FTP_MOREDATA{
  1250.                 $this->_announce('nb_put');
  1251.                 $res @ftp_nb_continue($this->_handle);
  1252.             }
  1253.  
  1254.         else {
  1255.             $res @ftp_put($this->_handle$remote_file$local_file$mode);
  1256.         }
  1257.         if (!$res{
  1258.             return $this->raiseError("File '$local_file' could not be uploaded to '$remote_file'."NET_FTP_ERR_UPLOADFILE_FAILED);
  1259.         else {
  1260.             return true;
  1261.         }
  1262.     }
  1263.  
  1264.     /**
  1265.      * This functionality allows you to transfer a whole directory-structure from the
  1266.      * remote-ftp to your local host. You have to give a remote-directory (ending with
  1267.      * '/') and the local directory (ending with '/') where to put the files you download.
  1268.      * The remote path is automatically completed with the current-remote-dir, if you give
  1269.      * a relative path to this function. You can give a relative path for the $local_path,
  1270.      * too. Then the script-basedir will be used for comletion of the path.
  1271.      * The parameter $overwrite will determine, whether to overwrite existing files or not.
  1272.      * Standard for this is false. Fourth you can explicitly set a mode for all transfer-
  1273.      * actions done. If you do not set this, the method tries to determine the transfer-
  1274.      * mode by checking your mode-directory for the file-extension. If the extension is not
  1275.      * inside the mode-directory, it will get your default-mode.
  1276.      *
  1277.      * @access  public
  1278.      * @param   string $remote_path The path to download
  1279.      * @param   string $local_path  The path to download to
  1280.      * @param   bool   $overwrite   (optional) Whether to overwrite existing files (true) or not (false, standard).
  1281.      * @param   int    $mode        (optional) The transfermode (either FTP_ASCII or FTP_BINARY).
  1282.      * @return  mixed               True on succes, otherwise PEAR::Error
  1283.      * @see     NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED, NET_FTP_ERR_REMOTEPATHNODIR, NET_FTP_ERR_LOCALPATHNODIR,NET_FTP_ERR_CREATELOCALDIR_FAILED
  1284.      */
  1285.     
  1286.     function getRecursive($remote_path$local_path$overwrite = false$mode = null)
  1287.     {
  1288.         $remote_path $this->_construct_path($remote_path);
  1289.         if (!$this->_check_dir($remote_path)) {
  1290.             return $this->raiseError("Given remote-path '$remote_path' seems not to be a directory."NET_FTP_ERR_REMOTEPATHNODIR);
  1291.         }
  1292.         if (!$this->_check_dir($local_path)) {
  1293.             return $this->raiseError("Given local-path '$local_path' seems not to be a directory."NET_FTP_ERR_LOCALPATHNODIR);
  1294.         }
  1295.  
  1296.         if (!@is_dir($local_path)) {
  1297.             $res @mkdir($local_path);
  1298.             if (!$res{
  1299.                 return $this->raiseError("Could not create dir '$local_path'"NET_FTP_ERR_CREATELOCALDIR_FAILED);
  1300.             }
  1301.         }
  1302.         $dir_list = array();
  1303.         $dir_list $this->ls($remote_pathNET_FTP_DIRS_ONLY);
  1304.         foreach ($dir_list as $dir_entry{
  1305.             $remote_path_new $remote_path.$dir_entry["name"]."/";
  1306.             $local_path_new $local_path.$dir_entry["name"]."/";
  1307.             $result $this->getRecursive($remote_path_new$local_path_new$overwrite$mode);
  1308.             if ($this->isError($result)) {
  1309.                 return $result;
  1310.             }
  1311.         }
  1312.         $file_list = array();
  1313.         $file_list $this->ls($remote_pathNET_FTP_FILES_ONLY);
  1314.         foreach ($file_list as $file_entry{
  1315.             $remote_file $remote_path.$file_entry["name"];
  1316.             $local_file $local_path.$file_entry["name"];
  1317.             $result $this->get($remote_file$local_file$overwrite$mode);
  1318.             if ($this->isError($result)) {
  1319.                 return $result;
  1320.             }
  1321.         }
  1322.         return true;
  1323.     }
  1324.  
  1325.     /**
  1326.      * This functionality allows you to transfer a whole directory-structure from your
  1327.      * local host to the remote-ftp. You have to give a remote-directory (ending with
  1328.      * '/') and the local directory (ending with '/') where to put the files you download.
  1329.      * The remote path is automatically completed with the current-remote-dir, if you give
  1330.      * a relative path to this function. You can give a relative path for the $local_path,
  1331.      * too. Then the script-basedir will be used for comletion of the path.
  1332.      * The parameter $overwrite will determine, whether to overwrite existing files or not.
  1333.      * Standard for this is false. Fourth you can explicitly set a mode for all transfer-
  1334.      * actions done. If you do not set this, the method tries to determine the transfer-
  1335.      * mode by checking your mode-directory for the file-extension. If the extension is not
  1336.      * inside the mode-directory, it will get your default-mode.
  1337.      *
  1338.      * @access  public
  1339.      * @param   string $remote_path The path to download
  1340.      * @param   string $local_path  The path to download to
  1341.      * @param   bool   $overwrite   (optional) Whether to overwrite existing files (true) or not (false, standard).
  1342.      * @param   int    $mode        (optional) The transfermode (either FTP_ASCII or FTP_BINARY).
  1343.      * @return  mixed               True on succes, otherwise PEAR::Error
  1344.      * @see     NET_FTP_ERR_LOCALFILENOTEXIST, NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN, NET_FTP_ERR_UPLOADFILE_FAILED, NET_FTP_ERR_LOCALPATHNODIR, NET_FTP_ERR_REMOTEPATHNODIR
  1345.      */
  1346.     
  1347.     function putRecursive($local_path$remote_path$overwrite = false$mode = null)
  1348.     {
  1349.         $remote_path $this->_construct_path($remote_path);
  1350.         if (!$this->_check_dir($local_path|| !is_dir($local_path)) {
  1351.             return $this->raiseError("Given local-path '$local_path' seems not to be a directory."NET_FTP_ERR_LOCALPATHNODIR);
  1352.         }
  1353.         if (!$this->_check_dir($remote_path)) {
  1354.             return $this->raiseError("Given remote-path '$remote_path' seems not to be a directory."NET_FTP_ERR_REMOTEPATHNODIR);
  1355.         }
  1356.         $old_path $this->pwd();
  1357.         if ($this->isError($this->cd($remote_path))) {
  1358.             $res $this->mkdir($remote_path);
  1359.             if ($this->isError($res)) {
  1360.                 return $res;
  1361.             }
  1362.         }
  1363.         $this->cd($old_path);
  1364.         $dir_list $this->_ls_local($local_path);
  1365.         foreach ($dir_list["dirs"as $dir_entry{
  1366.             $remote_path_new $remote_path.$dir_entry."/";
  1367.             $local_path_new $local_path.$dir_entry."/";
  1368.             $result $this->putRecursive($local_path_new$remote_path_new$overwrite$mode);
  1369.             if ($this->isError($result)) {
  1370.                 return $result;
  1371.             }
  1372.         }
  1373.  
  1374.         foreach ($dir_list["files"as $file_entry{
  1375.             $remote_file $remote_path.$file_entry;
  1376.             $local_file $local_path.$file_entry;
  1377.             $result $this->put($local_file$remote_file$overwrite$mode);
  1378.             if ($this->isError($result)) {
  1379.                 return $result;
  1380.             }
  1381.         }
  1382.         return true;
  1383.     }
  1384.  
  1385.     /**
  1386.      * This checks, whether a file should be transfered in ascii- or binary-mode
  1387.      * by it's file-extension. If the file-extension is not set or
  1388.      * the extension is not inside one of the extension-dirs, the actual set
  1389.      * transfer-mode is returned.
  1390.      *
  1391.      * @access  public
  1392.      * @param   string $filename  The filename to be checked
  1393.      * @return  int               Either FTP_ASCII or FTP_BINARY
  1394.      */
  1395.     
  1396.     function checkFileExtension($filename)
  1397.     {
  1398.         $pattern "/\.(.*)$/";
  1399.         $has_extension preg_match($pattern$filename$eregs);
  1400.         if (!$has_extension{
  1401.             return $this->_mode;
  1402.         else {
  1403.             $ext $eregs[1];
  1404.         }
  1405.  
  1406.         if (!empty($this->_file_extensions[$ext])) {
  1407.             return $this->_file_extensions[$ext];
  1408.         }
  1409.  
  1410.         return $this->_mode;
  1411.     }
  1412.  
  1413.     /**
  1414.      * Set the Hostname
  1415.      *
  1416.      * @access  public
  1417.      * @param   string $host The Hostname to set
  1418.      * @return  bool True on success, otherwise PEAR::Error
  1419.      * @see     NET_FTP_ERR_HOSTNAMENOSTRING
  1420.      */
  1421.     
  1422.     function setHostname($host)
  1423.     {
  1424.         if (!is_string($host)) {
  1425.             return PEAR::raiseError("Hostname must be a string."NET_FTP_ERR_HOSTNAMENOSTRING);
  1426.         }
  1427.         $this->_hostname $host;
  1428.         return true;
  1429.     }
  1430.  
  1431.     /**
  1432.      * Set the Port
  1433.      *
  1434.      * @access  public
  1435.      * @param   int $port    The Port to set
  1436.      * @return  bool True on success, otherwise PEAR::Error
  1437.      * @see     NET_FTP_ERR_PORTLESSZERO
  1438.      */
  1439.     
  1440.     function setPort($port)
  1441.     {
  1442.         if (!is_int($port|| ($port < 0)) {
  1443.             PEAR::raiseError("Invalid port. Has to be integer >= 0"NET_FTP_ERR_PORTLESSZERO);
  1444.         }
  1445.         $this->_port $port;
  1446.         return true;
  1447.     }
  1448.  
  1449.     /**
  1450.      * Set the Username
  1451.      *
  1452.      * @access  public
  1453.      * @param   string $user The Username to set
  1454.      * @return  mixed True on success, otherwise PEAR::Error
  1455.      * @see     NET_FTP_ERR_USERNAMENOSTRING
  1456.      */
  1457.     
  1458.     function setUsername($user)
  1459.     {
  1460.         if (empty($user|| !is_string($user)) {
  1461.             return PEAR::raiseError('Username $user invalid.'NET_FTP_ERR_USERNAMENOSTRING);
  1462.         }
  1463.         $this->_username $user;
  1464.     }
  1465.  
  1466.     /**
  1467.      * Set the Password
  1468.      *
  1469.      * @access  private
  1470.      * @param   string $password  The Password to set
  1471.      * @return  void 
  1472.      * @see     NET_FTP_ERR_PASSWORDNOSTRING
  1473.      */
  1474.     
  1475.     function setPassword($password)
  1476.     {
  1477.         if (empty($password|| !is_string($password)) {
  1478.             return PEAR::raiseError('Password xxx invalid.'NET_FTP_ERR_PASSWORDNOSTRING);
  1479.         }
  1480.         $this->_password $password;
  1481.     }
  1482.  
  1483.     /**
  1484.      * Set the transfer-mode. You can use the predefined constants
  1485.      * FTP_ASCII or FTP_BINARY. The mode will be stored for any further transfers.
  1486.      *
  1487.      * @access  public
  1488.      * @param   int    $mode  The mode to set
  1489.      * @return  mixed         True on success, otherwise PEAR::Error
  1490.      * @see     NET_FTP_ERR_NOMODECONST
  1491.      */
  1492.     
  1493.     function setMode($mode)
  1494.     {
  1495.         if (($mode == FTP_ASCII|| ($mode == FTP_BINARY)) {
  1496.             $this->_mode $mode;
  1497.             return true;
  1498.         else {
  1499.             return $this->raiseError('FTP-Mode has either to be FTP_ASCII or FTP_BINARY'NET_FTP_ERR_NOMODECONST);
  1500.         }
  1501.     }
  1502.  
  1503.     /**
  1504.      * Set the transfer-method to passive mode
  1505.      *
  1506.      * @access  public
  1507.      * @return  void 
  1508.      */
  1509.     
  1510.     function setPassive()
  1511.     {
  1512.         $this->_passv = true;
  1513.         @ftp_pasv($this->_handletrue);
  1514.     }
  1515.  
  1516.     /**
  1517.      * Set the transfer-method to active mode
  1518.      *
  1519.      * @access  public
  1520.      * @return  void 
  1521.      */
  1522.     
  1523.     function setActive()
  1524.     {
  1525.         $this->_passv = false;
  1526.         @ftp_pasv($this->_handlefalse);
  1527.     }
  1528.  
  1529.     /**
  1530.      * Set the timeout for FTP operations
  1531.      * Use this method to set a timeout for FTP operation. Timeout has to be an integer.
  1532.      *
  1533.      * @acess   public
  1534.      * @param   int $timeout the timeout to use
  1535.      * @return  bool True on success, otherwise PEAR::Error
  1536.      * @see     NET_FTP_ERR_TIMEOUTLESSZERO, NET_FTP_ERR_SETTIMEOUT_FAILED
  1537.      */
  1538.      
  1539.     function setTimeout $timeout = 0 
  1540.     {
  1541.         if (!is_int($timeout|| ($timeout < 0)) {
  1542.             return PEAR::raiseError("Timeout $timeout is invalid, has to be an integer >= 0"NET_FTP_ERR_TIMEOUTLESSZERO);
  1543.         }
  1544.         $this->_timeout $timeout;
  1545.         if (isset($this->_handle&& is_resource($this->_handle)) {
  1546.             $res @ftp_set_option($this->_handleFTP_TIMEOUT_SEC$timeout);
  1547.         else {
  1548.             $res = true;
  1549.         }
  1550.         if (!$res{
  1551.             return PEAR::raiseError("Set timeout failed."NET_FTP_ERR_SETTIMEOUT_FAILED);
  1552.         }
  1553.         return true;
  1554.     }        
  1555.     
  1556.     /**
  1557.      * Adds an extension to a mode-directory
  1558.      * The mode-directory saves file-extensions coresponding to filetypes
  1559.      * (ascii e.g.: 'php', 'txt', 'htm',...; binary e.g.: 'jpg', 'gif', 'exe',...).
  1560.      * The extensions have to be saved without the '.'. And
  1561.      * can be predefined in an external file (see: getExtensionsFile()).
  1562.      *
  1563.      * The array is build like this: 'php' => FTP_ASCII, 'png' => FTP_BINARY
  1564.      *
  1565.      * To change the mode of an extension, just add it again with the new mode!
  1566.      *
  1567.      * @access  public
  1568.      * @param   int    $mode  Either FTP_ASCII or FTP_BINARY
  1569.      * @param   string $ext   Extension
  1570.      * @return  void 
  1571.      */
  1572.     
  1573.     function addExtension($mode$ext)
  1574.     {
  1575.         $this->_file_extensions[$ext$mode;
  1576.     }
  1577.  
  1578.     /**
  1579.      * This function removes an extension from the mode-directories
  1580.      * (described above).
  1581.      *
  1582.      * @access  public
  1583.      * @param   string $ext  The extension to remove
  1584.      * @return  void 
  1585.      */
  1586.     
  1587.     function removeExtension($ext)
  1588.     {
  1589.         unset($this->_file_extensions[$ext]);
  1590.     }
  1591.  
  1592.     /**
  1593.      * This get's both (ascii- and binary-mode-directories) from the given file.
  1594.      * Beware, if you read a file into the mode-directory, all former set values
  1595.      * will be unset!
  1596.      *
  1597.      * @access  public
  1598.      * @param   string $filename  The file to get from
  1599.      * @return  mixed             True on success, otherwise PEAR::Error
  1600.      * @see     NET_FTP_ERR_EXTFILENOTEXIST, NET_FTP_ERR_EXTFILEREAD_FAILED
  1601.      */
  1602.     
  1603.     function getExtensionsFile($filename)
  1604.     {
  1605.         if (!file_exists($filename)) {
  1606.             return $this->raiseError("Extensions-file '$filename' does not exist"NET_FTP_ERR_EXTFILENOTEXIST);
  1607.         }
  1608.  
  1609.         if (!is_readable($filename)) {
  1610.             return $this->raiseError("Extensions-file '$filename' is not readable"NET_FTP_ERR_EXTFILEREAD_FAILED);
  1611.         }
  1612.  
  1613.         $this->_file_extension @parse_ini_file($filename);
  1614.         return true;
  1615.     }
  1616.  
  1617.     /**
  1618.      * Returns the Hostname
  1619.      *
  1620.      * @access  public
  1621.      * @return  string  The Hostname
  1622.      */
  1623.     
  1624.     function getHostname()
  1625.     {
  1626.         return $this->_hostname;
  1627.     }
  1628.  
  1629.     /**
  1630.      * Returns the Port
  1631.      *
  1632.      * @access  public
  1633.      * @return  int     The Port
  1634.      */
  1635.     
  1636.     function getPort()
  1637.     {
  1638.         return $this->_port;
  1639.     }
  1640.  
  1641.     /**
  1642.      * Returns the Username
  1643.      *
  1644.      * @access  public
  1645.      * @return  string  The Username
  1646.      */
  1647.     
  1648.     function getUsername()
  1649.     {
  1650.         return $this->_username;
  1651.     }
  1652.  
  1653.     /**
  1654.      * Returns the Password
  1655.      *
  1656.      * @access  public
  1657.      * @return  string  The Password
  1658.      */
  1659.     
  1660.     function getPassword()
  1661.     {
  1662.         return $this->_password;
  1663.     }
  1664.  
  1665.     /**
  1666.      * Returns the Transfermode
  1667.      *
  1668.      * @access  public
  1669.      * @return  int     The transfermode, either FTP_ASCII or FTP_BINARY.
  1670.      */
  1671.     
  1672.     function getMode()
  1673.     {
  1674.         return $this->_mode;
  1675.     }
  1676.  
  1677.     /**
  1678.      * Returns, whether the connection is set to passive mode or not
  1679.      *
  1680.      * @access  public
  1681.      * @return  bool    True if passive-, false if active-mode
  1682.      */
  1683.     
  1684.     function isPassive()
  1685.     {
  1686.         return $this->_passv;
  1687.     }
  1688.  
  1689.     /**
  1690.      * Returns the mode set for a file-extension
  1691.      *
  1692.      * @access  public
  1693.      * @param   string   $ext    The extension you wanna ask for
  1694.      * @return  int              Either FTP_ASCII, FTP_BINARY or NULL (if not set a mode for it)
  1695.      */
  1696.     
  1697.     function getExtensionMode($ext)
  1698.     {
  1699.         return @$this->_file_extensions[$ext];
  1700.     }
  1701.  
  1702.     /**
  1703.      * Get the currently set timeout.
  1704.      * Returns the actual timeout set.
  1705.      *
  1706.      * @access public
  1707.      * @return int The actual timeout
  1708.      */
  1709.     
  1710.     function getTimeout )
  1711.     {
  1712.         return ftp_get_option($this->_handleFTP_TIMEOUT_SEC);
  1713.     }    
  1714.  
  1715.     /**
  1716.      * Adds a Net_FTP_Observer instance to the list of observers
  1717.      * that are listening for messages emitted by this Net_FTP instance.
  1718.      *
  1719.      * @param   object   $observer     The Net_FTP_Observer instance to attach
  1720.      *                                  as a listener.
  1721.      * @return  boolean                True if the observer is successfully attached.
  1722.      * @access  public
  1723.      * @since   1.3
  1724.      */
  1725.     
  1726.     function attach(&$observer)
  1727.     {
  1728.         if (!is_a($observer'Net_FTP_Observer')) {
  1729.             return false;
  1730.         }
  1731.  
  1732.         $this->_listeners[$observer->getId()&$observer;
  1733.         return true;
  1734.     }
  1735.  
  1736.     /**
  1737.      * Removes a Net_FTP_Observer instance from the list of observers.
  1738.      *
  1739.      * @param   object   $observer     The Net_FTP_Observer instance to detach
  1740.      *                                  from the list of listeners.
  1741.      * @return  boolean                True if the observer is successfully detached.
  1742.      * @access  public
  1743.      * @since   1.3
  1744.      */
  1745.     
  1746.     function detach($observer)
  1747.     {
  1748.         if (!is_a($observer'Net_FTP_Observer'||
  1749.             !isset($this->_listeners[$observer->getId()])) {
  1750.             return false;
  1751.         }
  1752.  
  1753.         unset($this->_listeners[$observer->getId()]);
  1754.         return true;
  1755.     }
  1756.  
  1757.     /**
  1758.      * Informs each registered observer instance that a new message has been
  1759.      * sent.
  1760.      *                                                                      
  1761.      * @param   mixed     $event       A hash describing the net event.
  1762.      * @access  private
  1763.      * @since   1.3
  1764.      */                                                                     
  1765.     
  1766.     function _announce($event)
  1767.     {
  1768.         foreach ($this->_listeners as $id => $listener{
  1769.             $this->_listeners[$id]->notify($event);
  1770.         }
  1771.     }
  1772.     
  1773.         /**
  1774.      * Rebuild the path, if given relative
  1775.      *
  1776.      * @access  private
  1777.      * @param   string $path   The path to check and construct
  1778.      * @return  string         The build path
  1779.      */
  1780.     
  1781.     function _construct_path($path)
  1782.     {
  1783.         if ((substr($path01!= "/"&& (substr($path02!= "./")) {
  1784.             $actual_dir @ftp_pwd($this->_handle);
  1785.             if (substr($actual_dir(strlen($actual_dir- 2)1!= "/"{
  1786.                 $actual_dir .= "/";
  1787.             }
  1788.             $path $actual_dir.$path;
  1789.         }
  1790.         return $path;
  1791.     }
  1792.  
  1793.     /**
  1794.      * Checks, whether a given string is a directory-path (ends with "/") or not.
  1795.      *
  1796.      * @access  private
  1797.      * @param   string $path  Path to check
  1798.      * @return  bool          True if $path is a directory, otherwise false
  1799.      */
  1800.     
  1801.     function _check_dir($path)
  1802.     {
  1803.         if (substr($path(strlen($path- 1)1== "/"{
  1804.             return true;
  1805.         else {
  1806.             return false;
  1807.         }
  1808.     }
  1809.  
  1810.     /**
  1811.      * This will remove a file
  1812.      *
  1813.      * @access  private
  1814.      * @param   string $file   The file to delete
  1815.      * @return  mixed          True on success, otherwise PEAR::Error
  1816.      * @see     NET_FTP_ERR_DELETEFILE_FAILED
  1817.      */
  1818.     
  1819.     function _rm_file($file)
  1820.     {
  1821.         if (substr($file01== "/"{
  1822.             $res @ftp_delete($this->_handle$file);
  1823.         else {
  1824.             $actual_dir @ftp_pwd($this->_handle);
  1825.             if (substr($actual_dir(strlen($actual_dir- 2)1!= "/"{
  1826.                 $actual_dir .= "/";
  1827.             }
  1828.             $file $actual_dir.$file;
  1829.             $res @ftp_delete($this->_handle$file);
  1830.         }
  1831.  
  1832.         if (!$res{
  1833.             return $this->raiseError("Could not delete file '$file'."NET_FTP_ERR_DELETEFILE_FAILED);
  1834.         else {
  1835.             return true;
  1836.         }
  1837.     }
  1838.  
  1839.     /**
  1840.      * This will remove a dir
  1841.      *
  1842.      * @access  private
  1843.      * @param   string $dir  The dir to delete
  1844.      * @return  mixed        True on success, otherwise PEAR::Error
  1845.      * @see     NET_FTP_ERR_REMOTEPATHNODIR, NET_FTP_ERR_DELETEDIR_FAILED
  1846.      */
  1847.     
  1848.     function _rm_dir($dir)
  1849.     {
  1850.         if (substr($dir(strlen($dir- 1)1!= "/"{
  1851.             return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'"NET_FTP_ERR_REMOTEPATHNODIR);
  1852.         }
  1853.         $res @ftp_rmdir($this->_handle$dir);
  1854.         if (!$res{
  1855.             return $this->raiseError("Could not delete directory '$dir'."NET_FTP_ERR_DELETEDIR_FAILED);
  1856.         else {
  1857.             return true;
  1858.         }
  1859.     }
  1860.  
  1861.     /**
  1862.      * This will remove a dir and all subdirs and -files
  1863.      *
  1864.      * @access  private
  1865.      * @param   string $file  The dir to delete recursively
  1866.      * @return  mixed         True on success, otherwise PEAR::Error
  1867.      * @see     NET_FTP_ERR_REMOTEPATHNODIR, NET_FTP_ERR_DELETEDIR_FAILED
  1868.      */
  1869.     
  1870.     function _rm_dir_recursive($dir)
  1871.     {
  1872.         if (substr($dir(strlen($dir- 1)1!= "/"{
  1873.             return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'"NET_FTP_ERR_REMOTEPATHNODIR);
  1874.         }
  1875.         $file_list $this->_ls_files($dir);
  1876.         foreach ($file_list as $file{
  1877.             $file $dir.$file["name"];
  1878.             $res $this->rm($file);
  1879.             if ($this->isError($res)) {
  1880.                 return $res;
  1881.             }
  1882.         }
  1883.         $dir_list $this->_ls_dirs($dir);
  1884.         foreach ($dir_list as $new_dir{
  1885.             $new_dir $dir.$new_dir["name"]."/";
  1886.             $res $this->_rm_dir_recursive($new_dir);
  1887.             if ($this->isError($res)) {
  1888.                 return $res;
  1889.             }
  1890.         }
  1891.         $res $this->_rm_dir($dir);
  1892.         if (!$res{
  1893.             return $res;
  1894.         else {
  1895.             return true;
  1896.         }
  1897.     }
  1898.  
  1899.     /**
  1900.      * Lists up files and directories
  1901.      *
  1902.      * @access  private
  1903.      * @param   string $dir  The directory to list up
  1904.      * @return  array        An array of dirs and files
  1905.      */
  1906.     
  1907.     function _ls_both($dir)
  1908.     {
  1909.         $list_splitted $this->_list_and_parse($dir);
  1910.         if (!is_array($list_splitted["files"])) {
  1911.             $list_splitted["files"= array();
  1912.         }
  1913.         if (!is_array($list_splitted["dirs"])) {
  1914.             $list_splitted["dirs"= array();
  1915.         }
  1916.         $res = array();
  1917.         @array_splice($res00$list_splitted["files"]);
  1918.         @array_splice($res00$list_splitted["dirs"]);
  1919.         return $res;
  1920.     }
  1921.  
  1922.     /**
  1923.      * Lists up directories
  1924.      *
  1925.      * @access  private
  1926.      * @param   string $dir  The directory to list up
  1927.      * @return  array        An array of dirs
  1928.      */
  1929.     
  1930.     function _ls_dirs($dir)
  1931.     {
  1932.         $list["dirs"= array();
  1933.         $list $this->_list_and_parse($dir);
  1934.         return $list["dirs"];
  1935.     }
  1936.  
  1937.     /**
  1938.      * Lists up files
  1939.      *
  1940.      * @access  private
  1941.      * @param   string $dir  The directory to list up
  1942.      * @return  array        An array of files
  1943.      */
  1944.     
  1945.     function _ls_files($dir)
  1946.     {
  1947.         $list $this->_list_and_parse($dir);
  1948.         if (!is_array($list["files"])) {
  1949.             $list["files"= array();
  1950.         }
  1951.         return $list["files"];
  1952.     }
  1953.  
  1954.     /**
  1955.      * This lists up the directory-content and parses the items into well-formated arrays
  1956.      * The results of this array are sorted (dirs on top, sorted by name;
  1957.      * files below, sorted by name).
  1958.      *
  1959.      * @access  private
  1960.      * @param   string $dir  The directory to parse
  1961.      * @return  array        Lists of dirs and files
  1962.      * @see     NET_FTP_ERR_RAWDIRLIST_FAILED
  1963.      */
  1964.     
  1965.     function _list_and_parse($dir)
  1966.     {
  1967.         $dirs_list = array();
  1968.         $files_list = array();
  1969.         $dir_list @ftp_rawlist($this->_handle$dir);
  1970.         if ($dir_list === false{
  1971.             return PEAR::raiseError('Could not get raw directory listing.'NET_FTP_ERR_RAWDIRLIST_FAILED);
  1972.         }
  1973.         if (!isset($this->_matcher)) {
  1974.             $this->_matcher $this->_determine_os_match($dir_list);
  1975.             if (PEAR::isError($this->_matcher)) {
  1976.                 return $this->_matcher;
  1977.             }
  1978.         }
  1979.         foreach ($dir_list as $entry{
  1980.             if (!preg_match($this->_matcher['pattern']$entry$m)) {
  1981.                 continue;
  1982.             }
  1983.             $entry = array();
  1984.             foreach ($this->_matcher['map'as $key=>$val{
  1985.                 $entry[$key$m[$val];
  1986.             }
  1987.             $entry['stamp'$this->_parse_Date($entry['date']);
  1988.  
  1989.             if ($entry['is_dir']{
  1990.                 $dirs_list[$entry;
  1991.             else {
  1992.                 $files_list[$entry;
  1993.             }
  1994.         }
  1995.         @usort($dirs_listarray("Net_FTP""_nat_sort"));
  1996.         @usort($files_listarray("Net_FTP""_nat_sort"));
  1997.         $res["dirs"$dirs_list;
  1998.         $res["files"$files_list;
  1999.         return $res;
  2000.     }
  2001.     
  2002.     /**
  2003.      * Determine server OS
  2004.      * This determines the server OS and returns a valid regex to parse
  2005.      * ls() output.
  2006.      *
  2007.      * @access  private
  2008.      * @param   array $dir_list The raw dir list to parse
  2009.      * @return  mixed An array of 'pattern' and 'map' on success, otherwise PEAR::Error
  2010.      * @see     NET_FTP_ERR_DIRLIST_UNSUPPORTED
  2011.      */
  2012.     
  2013.     function _determine_os_match(&$dir_list{
  2014.     foreach ($dir_list as $entry{
  2015.         foreach ($this->_ls_match as $os => $match{
  2016.             if (preg_match($match['pattern']$entry)) {
  2017.                     return $match;
  2018.                 }
  2019.             }
  2020.     }
  2021.         $error 'The list style of your server seems not to be supported. Please email a "$ftp->ls(NET_FTP_RAWLIST);" output plus info on the server to the maintainer of this package to get it supported! Thanks for your help!';
  2022.         return PEAR::raiseError($errorNET_FTP_ERR_DIRLIST_UNSUPPORTED);
  2023.     }
  2024.     /**
  2025.      * Lists a local directory
  2026.      *
  2027.      * @access  private
  2028.      * @param   string $dir_path  The dir to list
  2029.      * @return  array             The list of dirs and files
  2030.      */
  2031.     
  2032.     function _ls_local($dir_path)
  2033.     {
  2034.         $dir dir($dir_path);
  2035.         $dir_list = array();
  2036.         $file_list = array();
  2037.         while (false !== ($entry $dir->read())) {
  2038.             if (($entry != '.'&& ($entry != '..')) {
  2039.                 if (is_dir($dir_path.$entry)) {
  2040.                     $dir_list[$entry;
  2041.                 else {
  2042.                     $file_list[$entry;
  2043.                 }
  2044.             }
  2045.         }
  2046.         $dir->close();
  2047.         $res['dirs'$dir_list;
  2048.         $res['files'$file_list;
  2049.         return $res;
  2050.     }
  2051.  
  2052.     /**
  2053.      * Function for use with usort().
  2054.      * Compares the list-array-elements by name.
  2055.      *
  2056.      * @access  private
  2057.      */
  2058.     
  2059.     function _nat_sort($item_1$item_2)
  2060.     {
  2061.         return strnatcmp($item_1['name']$item_2['name']);
  2062.     }
  2063.  
  2064.     /**
  2065.      * Parse dates to timestamps
  2066.      *
  2067.      * @access  private
  2068.      * @param   string $date  Date
  2069.      * @return  int           Timestamp
  2070.      * @see     NET_FTP_ERR_DATEFORMAT_FAILED
  2071.      */
  2072.     
  2073.     function _parse_Date($date)
  2074.     {
  2075.         // Sep 10 22:06 => Sep 10, <year> 22:06
  2076.         if (preg_match('/([A-Za-z]+)[ ]+([0-9]+)[ ]+([0-9]+):([0-9]+)/'$date$res)) {
  2077.             $year date('Y');
  2078.             $month $res[1];
  2079.             $day $res[2];
  2080.             $hour $res[3];
  2081.             $minute $res[4];
  2082.             $date = "$month $day$year $hour:$minute";
  2083.             $tmpDate strtotime($date);
  2084.             if ($tmpDate time()) {
  2085.                 $year--;
  2086.                 $date = "$month $day$year $hour:$minute";
  2087.             }
  2088.         }
  2089.         // 09-10-04 => 09/10/04
  2090.         elseif (preg_match('/^\d\d-\d\d-\d\d/',$date)) {
  2091.             $date str_replace('-','/',$date);
  2092.         }
  2093.         $res strtotime($date);
  2094.         if (!$res{
  2095.             return $this->raiseError('Dateconversion failed.'NET_FTP_ERR_DATEFORMAT_FAILED);
  2096.         }
  2097.         return $res;
  2098.     }
  2099. }
  2100. ?>

Documentation generated on Mon, 11 Mar 2019 13:52:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.