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.36 2005/01/04 20:23:20 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.         $savedir $this->pwd();
  756.         $this->pushErrorHandling(PEAR_ERROR_RETURN);
  757.         $e $this->cd($dir);
  758.         $this->popErrorHandling();
  759.         if ($e === true{
  760.             $this->cd($savedir);
  761.             return true;
  762.         }
  763.         $this->cd($savedir);
  764.         if ($recursive === false){
  765.             $res @ftp_mkdir($this->_handle$dir);
  766.             if (!$res{
  767.                 return $this->raiseError("Creation of '$dir' failed"NET_FTP_ERR_CREATEDIR_FAILED);
  768.             else {
  769.                 return true;
  770.             }
  771.         else {
  772.             if(strpos($dir'/'=== false{
  773.                 return $this->mkdir($dir,false);
  774.             }
  775.             $pos = 0;
  776.             $res $this->mkdir(dirname($dir)true);
  777.             $res $this->mkdir($dirfalse);
  778.             if ($res !== true{
  779.                 return $res;
  780.             }
  781.             return true;
  782.         }
  783.     }
  784.  
  785.     /**
  786.      * This method tries executing a command on the ftp, using SITE EXEC.
  787.      *
  788.      * @access  public
  789.      * @param   string $command The command to execute
  790.      * @return  mixed           The result of the command (if successfull), otherwise PEAR::Error
  791.      * @see     NET_FTP_ERR_EXEC_FAILED
  792.      */
  793.     
  794.     function execute($command)
  795.     {
  796.         $res @ftp_exec($this->_handle$command);
  797.         if (!$res{
  798.             return $this->raiseError("Execution of command '$command' failed."NET_FTP_ERR_EXEC_FAILED);
  799.         else {
  800.             return $res;
  801.         }
  802.     }
  803.  
  804.     /**
  805.      * Execute a SITE command on the server
  806.      * This method tries to execute a SITE command on the ftp server.
  807.      *
  808.      * @access  public
  809.      * @param   string $command   The command with parameters to execute
  810.      * @return  mixed             True if successful, otherwise PEAR::Error
  811.      * @see     NET_FTP_ERR_SITE_FAILED
  812.      */
  813.     
  814.     function site($command)
  815.     {
  816.         $res @ftp_site($this->_handle$command);
  817.         if (!$res{
  818.             return $this->raiseError("Execution of SITE command '$command' failed."NET_FTP_ERR_SITE_FAILED);
  819.         else {
  820.             return $res;
  821.         }
  822.     }
  823.  
  824.     /**
  825.      * This method will try to chmod the file specified on the server
  826.      * Currently, you must give a number as the the permission argument (777 or
  827.      * similar). The file can be either a relative or absolute path.
  828.      * NOTE: Some servers do not support this feature. In that case, you will
  829.      * get a PEAR error object returned. If successful, the method returns true
  830.      *
  831.      * @access  public
  832.      * @param   mixed   $target        The file or array of files to set permissions for
  833.      * @param   integer $permissions   The mode to set the file permissions to
  834.      * @return  mixed                  True if successful, otherwise PEAR::Error
  835.      * @see     NET_FTP_ERR_CHMOD_FAILED
  836.      */
  837.     
  838.     function chmod($target$permissions)
  839.     {
  840.         // If $target is an array: Loop through it.
  841.         if (is_array($target)) {
  842.  
  843.             for ($i = 0; $i count($target)$i++{
  844.                 $res $this->chmod($target[$i]$permissions);
  845.                 if (PEAR::isError($res)) {
  846.                     return $res;
  847.                 // end if isError
  848.             // end for i < count($target)
  849.  
  850.         else {
  851.  
  852.             $res $this->site("CHMOD " $permissions " " $target);
  853.             if (!$res{
  854.                 return PEAR::raiseError("CHMOD " $permissions " " $target " failed"NET_FTP_ERR_CHMOD_FAILED);
  855.             else {
  856.                 return $res;
  857.             }
  858.  
  859.         // end if is_array
  860.  
  861.     // end method chmod
  862.  
  863.     /**
  864.      * This method will try to chmod a folder and all of its contents
  865.      * on the server. The target argument must be a folder or an array of folders
  866.      * and the permissions argument have to be an integer (i.e. 777).
  867.      * The file can be either a relative or absolute path.
  868.      * NOTE: Some servers do not support this feature. In that case, you
  869.      * will get a PEAR error object returned. If successful, the method
  870.      * returns true
  871.      *
  872.      * @access  public
  873.      * @param   mixed   $target        The folder or array of folders to
  874.      *                                  set permissions for
  875.      * @param   integer $permissions   The mode to set the folder
  876.      *                                  and file permissions to
  877.      * @return  mixed                  True if successful, otherwise PEAR::Error
  878.      * @see     NET_FTP_ERR_CHMOD_FAILED, NET_FTP_ERR_DETERMINEPATH_FAILED, NET_FTP_ERR_RAWDIRLIST_FAILED, NET_FTP_ERR_DIRLIST_UNSUPPORTED
  879.      */
  880.     
  881.     function chmodRecursive($target$permissions)
  882.     {
  883.         static $dir_permissions;
  884.  
  885.         if(!isset($dir_permissions))// Making directory specific permissions
  886.             $dir_permissions $this->_makeDirPermissions($permissions);
  887.         }
  888.  
  889.         // If $target is an array: Loop through it
  890.         if (is_array($target)) {
  891.  
  892.             for ($i = 0; $i count($target)$i++{
  893.                 $res $this->chmodRecursive($target[$i]$permissions);
  894.                 if (PEAR::isError($res)) {
  895.                     return $res;
  896.                 // end if isError
  897.             // end for i < count($target)
  898.  
  899.         else {
  900.  
  901.             $remote_path $this->_construct_path($target);
  902.  
  903.             // Chmod the directory itself
  904.             $result $this->chmod($remote_path$dir_permissions);
  905.  
  906.             if (PEAR::isError($result)) {
  907.                 return $result;
  908.             }
  909.  
  910.             // If $remote_path last character is not a slash, add one
  911.             if (substr($remote_pathstrlen($remote_path)-1!= "/"{
  912.  
  913.                 $remote_path .= "/";
  914.             }
  915.  
  916.             $dir_list = array();
  917.             $mode NET_FTP_DIRS_ONLY;
  918.             $dir_list $this->ls($remote_path$mode);
  919.             foreach ($dir_list as $dir_entry{
  920.  
  921.                 $remote_path_new $remote_path.$dir_entry["name"]."/";
  922.  
  923.                 // Chmod the directory we're about to enter
  924.                 $result $this->chmod($remote_path_new$dir_permissions);
  925.  
  926.                 if (PEAR::isError($result)) {
  927.                     return $result;
  928.                 }
  929.  
  930.                 $result $this->chmodRecursive($remote_path_new$permissions);
  931.  
  932.                 if (PEAR::isError($result)) {
  933.                     return $result;
  934.                 }
  935.  
  936.             // end foreach dir_list as dir_entry
  937.  
  938.             $file_list = array();
  939.             $mode NET_FTP_FILES_ONLY;
  940.             $file_list $this->ls($remote_path$mode);
  941.  
  942.             foreach ($file_list as $file_entry{
  943.  
  944.                 $remote_file $remote_path.$file_entry["name"];
  945.  
  946.                 $result $this->chmod($remote_file$permissions);
  947.  
  948.                 if (PEAR::isError($result)) {
  949.                     return $result;
  950.                 }
  951.  
  952.             // end foreach $file_list
  953.  
  954.         // end if is_array
  955.  
  956.         return true; // No errors
  957.  
  958.     // end method chmodRecursive
  959.  
  960.     /**
  961.      * Rename or move a file or a directory from the ftp-server
  962.      *
  963.      * @access  public
  964.      * @param   string $remote_from The remote file or directory original to rename or move
  965.      * @param   string $remote_to The remote file or directory final to rename or move
  966.      * @return  bool $res True on success, otherwise PEAR::Error
  967.      * @see     NET_FTP_ERR_RENAME_FAILED
  968.      */
  969.  
  970.     function rename ($remote_from$remote_to
  971.     {
  972.         $res @ftp_rename($this->_handle$remote_from$remote_to);
  973.         if(!$res{
  974.             return $this->raiseError("Could not rename ".$remote_from." to ".$remote_to." !"NET_FTP_ERR_RENAME_FAILED);
  975.         }
  976.         return true;
  977.     }
  978.  
  979.     /**
  980.      * This will return logical permissions mask for directory.
  981.      * if directory have to be writeable it have also be executable
  982.      *
  983.      * @access  private
  984.      * @param   string $permissions    File permissions in digits for file (i.e. 666)
  985.      * @return  string                 File permissions in digits for directory (i.e. 777)
  986.      */
  987.  
  988.     function _makeDirPermissions($permissions){
  989.         $permissions = (string)$permissions;
  990.  
  991.         for($i = 0; $i strlen($permissions)$i++)// going through (user, group, world)
  992.             if((int)$permissions{$i4 and !((int)$permissions{$i1))// Read permission is set
  993.                                                                             // but execute not yet
  994.                 (int)$permissions{$i= (int)$permissions{$i+ 1; // Adding execute flag
  995.             }
  996.         }
  997.  
  998.         return (string)$permissions;
  999.     }
  1000.  
  1001.     /**
  1002.      * This will return the last modification-time of a file. You can either give this
  1003.      * function a relative or an absolute path to the file to check.
  1004.      * NOTE: Some servers will not support this feature and the function works
  1005.      * only on files, not directories! When successful,
  1006.      * it will return the last modification-time as a unix-timestamp or, when $format is
  1007.      * specified, a preformated timestring.
  1008.      *
  1009.      * @access  public
  1010.      * @param   string $file    The file to check
  1011.      * @param   string $format  (optional) The format to give the date back
  1012.      *                           if not set, it will return a Unix timestamp
  1013.      * @return  mixed           Unix timestamp, a preformated date-string or PEAR::Error
  1014.      * @see     NET_FTP_ERR_MDTMDIR_UNSUPPORTED, NET_FTP_ERR_MDTM_FAILED, NET_FTP_ERR_DATEFORMAT_FAILED
  1015.      */
  1016.     
  1017.     function mdtm($file$format = null)
  1018.     {
  1019.         $file $this->_construct_path($file);
  1020.         if ($this->_check_dir($file)) {
  1021.             return $this->raiseError("Filename '$file' seems to be a directory."NET_FTP_ERR_MDTMDIR_UNSUPPORTED);
  1022.         }
  1023.         $res @ftp_mdtm($this->_handle$file);
  1024.         if ($res == -1{
  1025.             return $this->raiseError("Could not get last-modification-date of '$file'."NET_FTP_ERR_MDTM_FAILED);
  1026.         }
  1027.         if (isset($format)) {
  1028.             $res date($format$res);
  1029.             if (!$res{
  1030.                 return $this->raiseError("Date-format failed on timestamp '$res'."NET_FTP_ERR_DATEFORMAT_FAILED);
  1031.             }
  1032.         }
  1033.         return $res;
  1034.     }
  1035.  
  1036.     /**
  1037.      * This will return the size of a given file in bytes. You can either give this function
  1038.      * a relative or an absolute file-path. NOTE: Some servers do not support this feature!
  1039.      *
  1040.      * @access  public
  1041.      * @param   string $file   The file to check
  1042.      * @return  mixed          Size in bytes or PEAR::Error
  1043.      * @see     NET_FTP_ERR_SIZE_FAILED
  1044.      */
  1045.     
  1046.     function size($file)
  1047.     {
  1048.         $file $this->_construct_path($file);
  1049.         $res @ftp_size($this->_handle$file);
  1050.         if ($res == -1{
  1051.             return $this->raiseError("Could not determine filesize of '$file'."NET_FTP_ERR_SIZE_FAILED);
  1052.         else {
  1053.             return $res;
  1054.         }
  1055.     }
  1056.  
  1057.     /**
  1058.      * This method returns a directory-list of the current directory or given one.
  1059.      * To display the current selected directory, simply set the first parameter to null
  1060.      * or leave it blank, if you do not want to use any other parameters.
  1061.      * <BR><BR>
  1062.      * There are 4 different modes of listing directories. Either to list only
  1063.      * the files (using NET_FTP_FILES_ONLY), to list only directories (using
  1064.      * NET_FTP_DIRS_ONLY) or to show both (using NET_FTP_DIRS_FILES, which is default).
  1065.      * <BR><BR>
  1066.      * The 4th one is the NET_FTP_RAWLIST, which returns just the array created by the
  1067.      * ftp_rawlist()-function build into PHP.
  1068.      * <BR><BR>
  1069.      * The other function-modes will return an array containing the requested data.
  1070.      * The files and dirs are listed in human-sorted order, but if you select
  1071.      * NET_FTP_DIRS_FILES the directories will be added above the files,
  1072.      * but although both sorted.
  1073.      * <BR><BR>
  1074.      * All elements in the arrays are associative arrays themselves. The have the following
  1075.      * structure:
  1076.      * <BR><BR>
  1077.      * Dirs:<BR>
  1078.      *           ["name"]        =>  string The name of the directory<BR>
  1079.      *           ["rights"]      =>  string The rights of the directory (in style "rwxr-xr-x")<BR>
  1080.      *           ["user"]        =>  string The owner of the directory<BR>
  1081.      *           ["group"]       =>  string The group-owner of the directory<BR>
  1082.      *           ["files_inside"]=>  string The number of files/dirs inside the directory
  1083.      *                                      excluding "." and ".."<BR>
  1084.      *           ["date"]        =>  int The creation-date as Unix timestamp<BR>
  1085.      *           ["is_dir"]      =>  bool true, cause this is a dir<BR>
  1086.      * <BR><BR>
  1087.      * Files:<BR>
  1088.      *           ["name"]        =>  string The name of the file<BR>
  1089.      *           ["size"]        =>  int Size in bytes<BR>
  1090.      *           ["rights"]      =>  string The rights of the file (in style "rwxr-xr-x")<BR>
  1091.      *           ["user"]        =>  string The owner of the file<BR>
  1092.      *           ["group"]       =>  string The group-owner of the file<BR>
  1093.      *           ["date"]        =>  int The creation-date as Unix timestamp<BR>
  1094.      *           ["is_dir"]      =>  bool false, cause this is a file<BR>
  1095.      *
  1096.      * @access  public
  1097.      * @param   string $dir   (optional) The directory to list or null, when listing the current directory.
  1098.      * @param   int    $mode  (optional) The mode which types to list (files, directories or both).
  1099.      * @return  mixed         The directory list as described above or PEAR::Error on failure.
  1100.      * @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
  1101.      */
  1102.      
  1103.     function ls($dir = null$mode = NET_FTP_DIRS_FILES)
  1104.     {
  1105.         if (!isset($dir)) {
  1106.             $dir @ftp_pwd($this->_handle);
  1107.             if (!$dir{
  1108.                 return $this->raiseError("Could not retrieve current directory"NET_FTP_ERR_DETERMINEPATH_FAILED);
  1109.             }
  1110.         }
  1111.         if (($mode != NET_FTP_FILES_ONLY&& ($mode != NET_FTP_DIRS_ONLY&& ($mode != NET_FTP_RAWLIST)) {
  1112.             $mode NET_FTP_DIRS_FILES;
  1113.         }
  1114.  
  1115.         switch ($mode{
  1116.             case NET_FTP_DIRS_FILES:    $res $this->_ls_both $dir );
  1117.                                         break;
  1118.             case NET_FTP_DIRS_ONLY:     $res $this->_ls_dirs $dir );
  1119.                                         break;
  1120.             case NET_FTP_FILES_ONLY:    $res $this->_ls_files $dir );
  1121.                                         break;
  1122.             case NET_FTP_RAWLIST:       $res @ftp_rawlist($this->_handle$dir);
  1123.                                         break;
  1124.         }
  1125.  
  1126.         return $res;
  1127.     }
  1128.  
  1129.     /**
  1130.      * This method will delete the given file or directory ($path) from the server
  1131.      * (maybe recursive).
  1132.      *
  1133.      * Whether the given string is a file or directory is only determined by the last
  1134.      * sign inside the string ("/" or not).
  1135.      *
  1136.      * If you specify a directory, you can optionally specify $recursive as true,
  1137.      * to let the directory be deleted recursive (with all sub-directories and files
  1138.      * inherited).
  1139.      *
  1140.      * You can either give a absolute or relative path for the file / dir. If you choose to
  1141.      * use the relative path, it will be automatically completed with the actual
  1142.      * selected directory.
  1143.      *
  1144.      * @access  public
  1145.      * @param   string $path      The absolute or relative path to the file / directory.
  1146.      * @param   bool   $recursive (optional)
  1147.      * @return  mixed             True on success, otherwise PEAR::Error
  1148.      * @see     NET_FTP_ERR_DELETEFILE_FAILED, NET_FTP_ERR_DELETEDIR_FAILED, NET_FTP_ERR_REMOTEPATHNODIR
  1149.      */
  1150.     
  1151.     function rm($path$recursive = false)
  1152.     {
  1153.         $path $this->_construct_path($path);
  1154.  
  1155.         if ($this->_check_dir($path)) {
  1156.             if ($recursive{
  1157.                 return $this->_rm_dir_recursive($path);
  1158.             else {
  1159.                 return $this->_rm_dir($path);
  1160.             }
  1161.         else {
  1162.             return $this->_rm_file($path);
  1163.         }
  1164.     }
  1165.  
  1166.     /**
  1167.      * This function will download a file from the ftp-server. You can either spcify a absolute
  1168.      * path to the file (beginning with "/") or a relative one, which will be completed
  1169.      * with the actual directory you selected on the server. You can specify
  1170.      * the path to which the file will be downloaded on the local
  1171.      * maschine, if the file should be overwritten if it exists (optionally, default is
  1172.      * no overwriting) and in which mode (FTP_ASCII or FTP_BINARY) the file should be
  1173.      * downloaded (if you do not specify this, the method tries to determine it automatically
  1174.      * from the mode-directory or uses the default-mode, set by you). If you give a relative
  1175.      * path to the local-file, the script-path is used as basepath.
  1176.      *
  1177.      * @access  public
  1178.      * @param   string $remote_file The absolute or relative path to the file to download
  1179.      * @param   string $local_file  The local file to put the downloaded in
  1180.      * @param   bool   $overwrite   (optional) Whether to overwrite existing file
  1181.      * @param   int    $mode        (optional) Either FTP_ASCII or FTP_BINARY
  1182.      * @return  mixed               True on success, otherwise PEAR::Error
  1183.      * @see     NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED
  1184.      */
  1185.     
  1186.     function get($remote_file$local_file$overwrite = false$mode = null)
  1187.     {
  1188.         if (!isset($mode)) {
  1189.             $mode $this->checkFileExtension($remote_file);
  1190.         }
  1191.  
  1192.         $remote_file $this->_construct_path($remote_file);
  1193.  
  1194.         if (@file_exists($local_file&& !$overwrite{
  1195.             return $this->raiseError("Local file '$local_file' exists and may not be overwriten."NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN);
  1196.         }
  1197.         if (@file_exists($local_file&& !@is_writeable($local_file&& $overwrite{
  1198.             return $this->raiseError("Local file '$local_file' is not writeable. Can not overwrite."NET_FTP_ERR_OVERWRITELOCALFILE_FAILED);
  1199.         }
  1200.  
  1201.         if (@function_exists('ftp_nb_get')){
  1202.             $res @ftp_nb_get($this->_handle$local_file$remote_file$mode);
  1203.             while ($res == FTP_MOREDATA{
  1204.                 $this->_announce('nb_get');
  1205.                 $res @ftp_nb_continue ($this->_handle);
  1206.             }
  1207.         else {
  1208.             $res @ftp_get($this->_handle$local_file$remote_file$mode);
  1209.         }
  1210.         if (!$res{
  1211.             return $this->raiseError("File '$remote_file' could not be downloaded to '$local_file'."NET_FTP_ERR_OVERWRITELOCALFILE_FAILED);
  1212.         else {
  1213.             return true;
  1214.         }
  1215.     }
  1216.  
  1217.     /**
  1218.      * This function will upload a file to the ftp-server. You can either specify a absolute
  1219.      * path to the remote-file (beginning with "/") or a relative one, which will be completed
  1220.      * with the actual directory you selected on the server. You can specify
  1221.      * the path from which the file will be uploaded on the local
  1222.      * maschine, if the file should be overwritten if it exists (optionally, default is
  1223.      * no overwriting) and in which mode (FTP_ASCII or FTP_BINARY) the file should be
  1224.      * downloaded (if you do not specify this, the method tries to determine it automatically
  1225.      * from the mode-directory or uses the default-mode, set by you). If you give a relative
  1226.      * path to the local-file, the script-path is used as basepath.
  1227.      *
  1228.      * @access  public
  1229.      * @param   string $local_file  The local file to upload
  1230.      * @param   string $remote_file The absolute or relative path to the file to upload to
  1231.      * @param   bool   $overwrite   (optional) Whether to overwrite existing file
  1232.      * @param   int    $mode        (optional) Either FTP_ASCII or FTP_BINARY
  1233.      * @return  mixed               True on success, otherwise PEAR::Error
  1234.      * @see     NET_FTP_ERR_LOCALFILENOTEXIST, NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN, NET_FTP_ERR_UPLOADFILE_FAILED
  1235.      */
  1236.     
  1237.     function put($local_file$remote_file$overwrite = false$mode = null)
  1238.     {
  1239.         if (!isset($mode)) {
  1240.             $mode $this->checkFileExtension($local_file);
  1241.         }
  1242.         $remote_file $this->_construct_path($remote_file);
  1243.  
  1244.         if (!@file_exists($local_file)) {
  1245.             return $this->raiseError("Local file '$local_file' does not exist."NET_FTP_ERR_LOCALFILENOTEXIST);
  1246.         }
  1247.         if ((@ftp_size($this->_handle$remote_file!= -1&& !$overwrite{
  1248.             return $this->raiseError("Remote file '$remote_file' exists and may not be overwriten."NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN);
  1249.         }
  1250.  
  1251.         if (function_exists('ftp_nb_put')){
  1252.             $res @ftp_nb_put($this->_handle$remote_file$local_file$mode);
  1253.             while ($res == FTP_MOREDATA{
  1254.                 $this->_announce('nb_put');
  1255.                 $res @ftp_nb_continue($this->_handle);
  1256.             }
  1257.  
  1258.         else {
  1259.             $res @ftp_put($this->_handle$remote_file$local_file$mode);
  1260.         }
  1261.         if (!$res{
  1262.             return $this->raiseError("File '$local_file' could not be uploaded to '$remote_file'."NET_FTP_ERR_UPLOADFILE_FAILED);
  1263.         else {
  1264.             return true;
  1265.         }
  1266.     }
  1267.  
  1268.     /**
  1269.      * This functionality allows you to transfer a whole directory-structure from the
  1270.      * remote-ftp to your local host. You have to give a remote-directory (ending with
  1271.      * '/') and the local directory (ending with '/') where to put the files you download.
  1272.      * The remote path is automatically completed with the current-remote-dir, if you give
  1273.      * a relative path to this function. You can give a relative path for the $local_path,
  1274.      * too. Then the script-basedir will be used for comletion of the path.
  1275.      * The parameter $overwrite will determine, whether to overwrite existing files or not.
  1276.      * Standard for this is false. Fourth you can explicitly set a mode for all transfer-
  1277.      * actions done. If you do not set this, the method tries to determine the transfer-
  1278.      * mode by checking your mode-directory for the file-extension. If the extension is not
  1279.      * inside the mode-directory, it will get your default-mode.
  1280.      *
  1281.      * @access  public
  1282.      * @param   string $remote_path The path to download
  1283.      * @param   string $local_path  The path to download to
  1284.      * @param   bool   $overwrite   (optional) Whether to overwrite existing files (true) or not (false, standard).
  1285.      * @param   int    $mode        (optional) The transfermode (either FTP_ASCII or FTP_BINARY).
  1286.      * @return  mixed               True on succes, otherwise PEAR::Error
  1287.      * @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
  1288.      */
  1289.     
  1290.     function getRecursive($remote_path$local_path$overwrite = false$mode = null)
  1291.     {
  1292.         $remote_path $this->_construct_path($remote_path);
  1293.         if (!$this->_check_dir($remote_path)) {
  1294.             return $this->raiseError("Given remote-path '$remote_path' seems not to be a directory."NET_FTP_ERR_REMOTEPATHNODIR);
  1295.         }
  1296.         if (!$this->_check_dir($local_path)) {
  1297.             return $this->raiseError("Given local-path '$local_path' seems not to be a directory."NET_FTP_ERR_LOCALPATHNODIR);
  1298.         }
  1299.  
  1300.         if (!@is_dir($local_path)) {
  1301.             $res @mkdir($local_path);
  1302.             if (!$res{
  1303.                 return $this->raiseError("Could not create dir '$local_path'"NET_FTP_ERR_CREATELOCALDIR_FAILED);
  1304.             }
  1305.         }
  1306.         $dir_list = array();
  1307.         $dir_list $this->ls($remote_pathNET_FTP_DIRS_ONLY);
  1308.         foreach ($dir_list as $dir_entry{
  1309.             if ($dir_entry['name'!= '.' && $dir_entry['name'!= '..'{
  1310.                 $remote_path_new $remote_path.$dir_entry["name"]."/";
  1311.                 $local_path_new $local_path.$dir_entry["name"]."/";
  1312.                 $result $this->getRecursive($remote_path_new$local_path_new$overwrite$mode);
  1313.                 if ($this->isError($result)) {
  1314.                     return $result;
  1315.                 }
  1316.             }
  1317.         }
  1318.         $file_list = array();
  1319.         $file_list $this->ls($remote_pathNET_FTP_FILES_ONLY);
  1320.         foreach ($file_list as $file_entry{
  1321.             $remote_file $remote_path.$file_entry["name"];
  1322.             $local_file $local_path.$file_entry["name"];
  1323.             $result $this->get($remote_file$local_file$overwrite$mode);
  1324.             if ($this->isError($result)) {
  1325.                 return $result;
  1326.             }
  1327.         }
  1328.         return true;
  1329.     }
  1330.  
  1331.     /**
  1332.      * This functionality allows you to transfer a whole directory-structure from your
  1333.      * local host to the remote-ftp. You have to give a remote-directory (ending with
  1334.      * '/') and the local directory (ending with '/') where to put the files you download.
  1335.      * The remote path is automatically completed with the current-remote-dir, if you give
  1336.      * a relative path to this function. You can give a relative path for the $local_path,
  1337.      * too. Then the script-basedir will be used for comletion of the path.
  1338.      * The parameter $overwrite will determine, whether to overwrite existing files or not.
  1339.      * Standard for this is false. Fourth you can explicitly set a mode for all transfer-
  1340.      * actions done. If you do not set this, the method tries to determine the transfer-
  1341.      * mode by checking your mode-directory for the file-extension. If the extension is not
  1342.      * inside the mode-directory, it will get your default-mode.
  1343.      *
  1344.      * @access  public
  1345.      * @param   string $remote_path The path to download
  1346.      * @param   string $local_path  The path to download to
  1347.      * @param   bool   $overwrite   (optional) Whether to overwrite existing files (true) or not (false, standard).
  1348.      * @param   int    $mode        (optional) The transfermode (either FTP_ASCII or FTP_BINARY).
  1349.      * @return  mixed               True on succes, otherwise PEAR::Error
  1350.      * @see     NET_FTP_ERR_LOCALFILENOTEXIST, NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN, NET_FTP_ERR_UPLOADFILE_FAILED, NET_FTP_ERR_LOCALPATHNODIR, NET_FTP_ERR_REMOTEPATHNODIR
  1351.      */
  1352.     
  1353.     function putRecursive($local_path$remote_path$overwrite = false$mode = null)
  1354.     {
  1355.         $remote_path $this->_construct_path($remote_path);
  1356.         if (!$this->_check_dir($local_path|| !is_dir($local_path)) {
  1357.             return $this->raiseError("Given local-path '$local_path' seems not to be a directory."NET_FTP_ERR_LOCALPATHNODIR);
  1358.         }
  1359.         if (!$this->_check_dir($remote_path)) {
  1360.             return $this->raiseError("Given remote-path '$remote_path' seems not to be a directory."NET_FTP_ERR_REMOTEPATHNODIR);
  1361.         }
  1362.         $old_path $this->pwd();
  1363.         if ($this->isError($this->cd($remote_path))) {
  1364.             $res $this->mkdir($remote_path);
  1365.             if ($this->isError($res)) {
  1366.                 return $res;
  1367.             }
  1368.         }
  1369.         $this->cd($old_path);
  1370.         $dir_list $this->_ls_local($local_path);
  1371.         foreach ($dir_list["dirs"as $dir_entry{
  1372.             $remote_path_new $remote_path.$dir_entry."/";
  1373.             $local_path_new $local_path.$dir_entry."/";
  1374.             $result $this->putRecursive($local_path_new$remote_path_new$overwrite$mode);
  1375.             if ($this->isError($result)) {
  1376.                 return $result;
  1377.             }
  1378.         }
  1379.  
  1380.         foreach ($dir_list["files"as $file_entry{
  1381.             $remote_file $remote_path.$file_entry;
  1382.             $local_file $local_path.$file_entry;
  1383.             $result $this->put($local_file$remote_file$overwrite$mode);
  1384.             if ($this->isError($result)) {
  1385.                 return $result;
  1386.             }
  1387.         }
  1388.         return true;
  1389.     }
  1390.  
  1391.     /**
  1392.      * This checks, whether a file should be transfered in ascii- or binary-mode
  1393.      * by it's file-extension. If the file-extension is not set or
  1394.      * the extension is not inside one of the extension-dirs, the actual set
  1395.      * transfer-mode is returned.
  1396.      *
  1397.      * @access  public
  1398.      * @param   string $filename  The filename to be checked
  1399.      * @return  int               Either FTP_ASCII or FTP_BINARY
  1400.      */
  1401.     
  1402.     function checkFileExtension($filename)
  1403.     {
  1404.         $pattern "/\.(.*)$/";
  1405.         $has_extension preg_match($pattern$filename$eregs);
  1406.         if (!$has_extension{
  1407.             return $this->_mode;
  1408.         else {
  1409.             $ext $eregs[1];
  1410.         }
  1411.  
  1412.         if (!empty($this->_file_extensions[$ext])) {
  1413.             return $this->_file_extensions[$ext];
  1414.         }
  1415.  
  1416.         return $this->_mode;
  1417.     }
  1418.  
  1419.     /**
  1420.      * Set the Hostname
  1421.      *
  1422.      * @access  public
  1423.      * @param   string $host The Hostname to set
  1424.      * @return  bool True on success, otherwise PEAR::Error
  1425.      * @see     NET_FTP_ERR_HOSTNAMENOSTRING
  1426.      */
  1427.     
  1428.     function setHostname($host)
  1429.     {
  1430.         if (!is_string($host)) {
  1431.             return PEAR::raiseError("Hostname must be a string."NET_FTP_ERR_HOSTNAMENOSTRING);
  1432.         }
  1433.         $this->_hostname $host;
  1434.         return true;
  1435.     }
  1436.  
  1437.     /**
  1438.      * Set the Port
  1439.      *
  1440.      * @access  public
  1441.      * @param   int $port    The Port to set
  1442.      * @return  bool True on success, otherwise PEAR::Error
  1443.      * @see     NET_FTP_ERR_PORTLESSZERO
  1444.      */
  1445.     
  1446.     function setPort($port)
  1447.     {
  1448.         if (!is_int($port|| ($port < 0)) {
  1449.             PEAR::raiseError("Invalid port. Has to be integer >= 0"NET_FTP_ERR_PORTLESSZERO);
  1450.         }
  1451.         $this->_port $port;
  1452.         return true;
  1453.     }
  1454.  
  1455.     /**
  1456.      * Set the Username
  1457.      *
  1458.      * @access  public
  1459.      * @param   string $user The Username to set
  1460.      * @return  mixed True on success, otherwise PEAR::Error
  1461.      * @see     NET_FTP_ERR_USERNAMENOSTRING
  1462.      */
  1463.     
  1464.     function setUsername($user)
  1465.     {
  1466.         if (empty($user|| !is_string($user)) {
  1467.             return PEAR::raiseError('Username $user invalid.'NET_FTP_ERR_USERNAMENOSTRING);
  1468.         }
  1469.         $this->_username $user;
  1470.     }
  1471.  
  1472.     /**
  1473.      * Set the Password
  1474.      *
  1475.      * @access  private
  1476.      * @param   string $password  The Password to set
  1477.      * @return  void 
  1478.      * @see     NET_FTP_ERR_PASSWORDNOSTRING
  1479.      */
  1480.     
  1481.     function setPassword($password)
  1482.     {
  1483.         if (empty($password|| !is_string($password)) {
  1484.             return PEAR::raiseError('Password xxx invalid.'NET_FTP_ERR_PASSWORDNOSTRING);
  1485.         }
  1486.         $this->_password $password;
  1487.     }
  1488.  
  1489.     /**
  1490.      * Set the transfer-mode. You can use the predefined constants
  1491.      * FTP_ASCII or FTP_BINARY. The mode will be stored for any further transfers.
  1492.      *
  1493.      * @access  public
  1494.      * @param   int    $mode  The mode to set
  1495.      * @return  mixed         True on success, otherwise PEAR::Error
  1496.      * @see     NET_FTP_ERR_NOMODECONST
  1497.      */
  1498.     
  1499.     function setMode($mode)
  1500.     {
  1501.         if (($mode == FTP_ASCII|| ($mode == FTP_BINARY)) {
  1502.             $this->_mode $mode;
  1503.             return true;
  1504.         else {
  1505.             return $this->raiseError('FTP-Mode has either to be FTP_ASCII or FTP_BINARY'NET_FTP_ERR_NOMODECONST);
  1506.         }
  1507.     }
  1508.  
  1509.     /**
  1510.      * Set the transfer-method to passive mode
  1511.      *
  1512.      * @access  public
  1513.      * @return  void 
  1514.      */
  1515.     
  1516.     function setPassive()
  1517.     {
  1518.         $this->_passv = true;
  1519.         @ftp_pasv($this->_handletrue);
  1520.     }
  1521.  
  1522.     /**
  1523.      * Set the transfer-method to active mode
  1524.      *
  1525.      * @access  public
  1526.      * @return  void 
  1527.      */
  1528.     
  1529.     function setActive()
  1530.     {
  1531.         $this->_passv = false;
  1532.         @ftp_pasv($this->_handlefalse);
  1533.     }
  1534.  
  1535.     /**
  1536.      * Set the timeout for FTP operations
  1537.      * Use this method to set a timeout for FTP operation. Timeout has to be an integer.
  1538.      *
  1539.      * @acess   public
  1540.      * @param   int $timeout the timeout to use
  1541.      * @return  bool True on success, otherwise PEAR::Error
  1542.      * @see     NET_FTP_ERR_TIMEOUTLESSZERO, NET_FTP_ERR_SETTIMEOUT_FAILED
  1543.      */
  1544.      
  1545.     function setTimeout $timeout = 0 
  1546.     {
  1547.         if (!is_int($timeout|| ($timeout < 0)) {
  1548.             return PEAR::raiseError("Timeout $timeout is invalid, has to be an integer >= 0"NET_FTP_ERR_TIMEOUTLESSZERO);
  1549.         }
  1550.         $this->_timeout $timeout;
  1551.         if (isset($this->_handle&& is_resource($this->_handle)) {
  1552.             $res @ftp_set_option($this->_handleFTP_TIMEOUT_SEC$timeout);
  1553.         else {
  1554.             $res = true;
  1555.         }
  1556.         if (!$res{
  1557.             return PEAR::raiseError("Set timeout failed."NET_FTP_ERR_SETTIMEOUT_FAILED);
  1558.         }
  1559.         return true;
  1560.     }        
  1561.     
  1562.     /**
  1563.      * Adds an extension to a mode-directory
  1564.      * The mode-directory saves file-extensions coresponding to filetypes
  1565.      * (ascii e.g.: 'php', 'txt', 'htm',...; binary e.g.: 'jpg', 'gif', 'exe',...).
  1566.      * The extensions have to be saved without the '.'. And
  1567.      * can be predefined in an external file (see: getExtensionsFile()).
  1568.      *
  1569.      * The array is build like this: 'php' => FTP_ASCII, 'png' => FTP_BINARY
  1570.      *
  1571.      * To change the mode of an extension, just add it again with the new mode!
  1572.      *
  1573.      * @access  public
  1574.      * @param   int    $mode  Either FTP_ASCII or FTP_BINARY
  1575.      * @param   string $ext   Extension
  1576.      * @return  void 
  1577.      */
  1578.     
  1579.     function addExtension($mode$ext)
  1580.     {
  1581.         $this->_file_extensions[$ext$mode;
  1582.     }
  1583.  
  1584.     /**
  1585.      * This function removes an extension from the mode-directories
  1586.      * (described above).
  1587.      *
  1588.      * @access  public
  1589.      * @param   string $ext  The extension to remove
  1590.      * @return  void 
  1591.      */
  1592.     
  1593.     function removeExtension($ext)
  1594.     {
  1595.         unset($this->_file_extensions[$ext]);
  1596.     }
  1597.  
  1598.     /**
  1599.      * This get's both (ascii- and binary-mode-directories) from the given file.
  1600.      * Beware, if you read a file into the mode-directory, all former set values
  1601.      * will be unset!
  1602.      *
  1603.      * @access  public
  1604.      * @param   string $filename  The file to get from
  1605.      * @return  mixed             True on success, otherwise PEAR::Error
  1606.      * @see     NET_FTP_ERR_EXTFILENOTEXIST, NET_FTP_ERR_EXTFILEREAD_FAILED
  1607.      */
  1608.     
  1609.     function getExtensionsFile($filename)
  1610.     {
  1611.         if (!file_exists($filename)) {
  1612.             return $this->raiseError("Extensions-file '$filename' does not exist"NET_FTP_ERR_EXTFILENOTEXIST);
  1613.         }
  1614.  
  1615.         if (!is_readable($filename)) {
  1616.             return $this->raiseError("Extensions-file '$filename' is not readable"NET_FTP_ERR_EXTFILEREAD_FAILED);
  1617.         }
  1618.  
  1619.         $this->_file_extension @parse_ini_file($filename);
  1620.         return true;
  1621.     }
  1622.  
  1623.     /**
  1624.      * Returns the Hostname
  1625.      *
  1626.      * @access  public
  1627.      * @return  string  The Hostname
  1628.      */
  1629.     
  1630.     function getHostname()
  1631.     {
  1632.         return $this->_hostname;
  1633.     }
  1634.  
  1635.     /**
  1636.      * Returns the Port
  1637.      *
  1638.      * @access  public
  1639.      * @return  int     The Port
  1640.      */
  1641.     
  1642.     function getPort()
  1643.     {
  1644.         return $this->_port;
  1645.     }
  1646.  
  1647.     /**
  1648.      * Returns the Username
  1649.      *
  1650.      * @access  public
  1651.      * @return  string  The Username
  1652.      */
  1653.     
  1654.     function getUsername()
  1655.     {
  1656.         return $this->_username;
  1657.     }
  1658.  
  1659.     /**
  1660.      * Returns the Password
  1661.      *
  1662.      * @access  public
  1663.      * @return  string  The Password
  1664.      */
  1665.     
  1666.     function getPassword()
  1667.     {
  1668.         return $this->_password;
  1669.     }
  1670.  
  1671.     /**
  1672.      * Returns the Transfermode
  1673.      *
  1674.      * @access  public
  1675.      * @return  int     The transfermode, either FTP_ASCII or FTP_BINARY.
  1676.      */
  1677.     
  1678.     function getMode()
  1679.     {
  1680.         return $this->_mode;
  1681.     }
  1682.  
  1683.     /**
  1684.      * Returns, whether the connection is set to passive mode or not
  1685.      *
  1686.      * @access  public
  1687.      * @return  bool    True if passive-, false if active-mode
  1688.      */
  1689.     
  1690.     function isPassive()
  1691.     {
  1692.         return $this->_passv;
  1693.     }
  1694.  
  1695.     /**
  1696.      * Returns the mode set for a file-extension
  1697.      *
  1698.      * @access  public
  1699.      * @param   string   $ext    The extension you wanna ask for
  1700.      * @return  int              Either FTP_ASCII, FTP_BINARY or NULL (if not set a mode for it)
  1701.      */
  1702.     
  1703.     function getExtensionMode($ext)
  1704.     {
  1705.         return @$this->_file_extensions[$ext];
  1706.     }
  1707.  
  1708.     /**
  1709.      * Get the currently set timeout.
  1710.      * Returns the actual timeout set.
  1711.      *
  1712.      * @access public
  1713.      * @return int The actual timeout
  1714.      */
  1715.     
  1716.     function getTimeout )
  1717.     {
  1718.         return ftp_get_option($this->_handleFTP_TIMEOUT_SEC);
  1719.     }    
  1720.  
  1721.     /**
  1722.      * Adds a Net_FTP_Observer instance to the list of observers
  1723.      * that are listening for messages emitted by this Net_FTP instance.
  1724.      *
  1725.      * @param   object   $observer     The Net_FTP_Observer instance to attach
  1726.      *                                  as a listener.
  1727.      * @return  boolean                True if the observer is successfully attached.
  1728.      * @access  public
  1729.      * @since   1.3
  1730.      */
  1731.     
  1732.     function attach(&$observer)
  1733.     {
  1734.         if (!is_a($observer'Net_FTP_Observer')) {
  1735.             return false;
  1736.         }
  1737.  
  1738.         $this->_listeners[$observer->getId()&$observer;
  1739.         return true;
  1740.     }
  1741.  
  1742.     /**
  1743.      * Removes a Net_FTP_Observer instance from the list of observers.
  1744.      *
  1745.      * @param   object   $observer     The Net_FTP_Observer instance to detach
  1746.      *                                  from the list of listeners.
  1747.      * @return  boolean                True if the observer is successfully detached.
  1748.      * @access  public
  1749.      * @since   1.3
  1750.      */
  1751.     
  1752.     function detach($observer)
  1753.     {
  1754.         if (!is_a($observer'Net_FTP_Observer'||
  1755.             !isset($this->_listeners[$observer->getId()])) {
  1756.             return false;
  1757.         }
  1758.  
  1759.         unset($this->_listeners[$observer->getId()]);
  1760.         return true;
  1761.     }
  1762.  
  1763.     /**
  1764.      * Informs each registered observer instance that a new message has been
  1765.      * sent.
  1766.      *                                                                      
  1767.      * @param   mixed     $event       A hash describing the net event.
  1768.      * @access  private
  1769.      * @since   1.3
  1770.      */                                                                     
  1771.     
  1772.     function _announce($event)
  1773.     {
  1774.         foreach ($this->_listeners as $id => $listener{
  1775.             $this->_listeners[$id]->notify($event);
  1776.         }
  1777.     }
  1778.     
  1779.         /**
  1780.      * Rebuild the path, if given relative
  1781.      *
  1782.      * @access  private
  1783.      * @param   string $path   The path to check and construct
  1784.      * @return  string         The build path
  1785.      */
  1786.     
  1787.     function _construct_path($path)
  1788.     {
  1789.         if ((substr($path01!= "/"&& (substr($path02!= "./")) {
  1790.             $actual_dir @ftp_pwd($this->_handle);
  1791.             if (substr($actual_dir(strlen($actual_dir- 2)1!= "/"{
  1792.                 $actual_dir .= "/";
  1793.             }
  1794.             $path $actual_dir.$path;
  1795.         }
  1796.         return $path;
  1797.     }
  1798.  
  1799.     /**
  1800.      * Checks, whether a given string is a directory-path (ends with "/") or not.
  1801.      *
  1802.      * @access  private
  1803.      * @param   string $path  Path to check
  1804.      * @return  bool          True if $path is a directory, otherwise false
  1805.      */
  1806.     
  1807.     function _check_dir($path)
  1808.     {
  1809.         if (substr($path(strlen($path- 1)1== "/"{
  1810.             return true;
  1811.         else {
  1812.             return false;
  1813.         }
  1814.     }
  1815.  
  1816.     /**
  1817.      * This will remove a file
  1818.      *
  1819.      * @access  private
  1820.      * @param   string $file   The file to delete
  1821.      * @return  mixed          True on success, otherwise PEAR::Error
  1822.      * @see     NET_FTP_ERR_DELETEFILE_FAILED
  1823.      */
  1824.     
  1825.     function _rm_file($file)
  1826.     {
  1827.         if (substr($ifile01!= "/"{
  1828.             $actual_dir @ftp_pwd($this->_handle);
  1829.             if (substr($actual_dir(strlen($actual_dir- 2)1!= "/"{
  1830.                 $actual_dir .= "/";
  1831.             }
  1832.             $file $actual_dir.$file;
  1833.         }
  1834.         $res @ftp_delete($this->_handle$file);
  1835.         
  1836.         if (!$res{
  1837.             return $this->raiseError("Could not delete file '$file'."NET_FTP_ERR_DELETEFILE_FAILED);
  1838.         else {
  1839.             return true;
  1840.         }
  1841.     }
  1842.  
  1843.     /**
  1844.      * This will remove a dir
  1845.      *
  1846.      * @access  private
  1847.      * @param   string $dir  The dir to delete
  1848.      * @return  mixed        True on success, otherwise PEAR::Error
  1849.      * @see     NET_FTP_ERR_REMOTEPATHNODIR, NET_FTP_ERR_DELETEDIR_FAILED
  1850.      */
  1851.     
  1852.     function _rm_dir($dir)
  1853.     {
  1854.         if (substr($dir(strlen($dir- 1)1!= "/"{
  1855.             return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'"NET_FTP_ERR_REMOTEPATHNODIR);
  1856.         }
  1857.         $res @ftp_rmdir($this->_handle$dir);
  1858.         if (!$res{
  1859.             return $this->raiseError("Could not delete directory '$dir'."NET_FTP_ERR_DELETEDIR_FAILED);
  1860.         else {
  1861.             return true;
  1862.         }
  1863.     }
  1864.  
  1865.     /**
  1866.      * This will remove a dir and all subdirs and -files
  1867.      *
  1868.      * @access  private
  1869.      * @param   string $file  The dir to delete recursively
  1870.      * @return  mixed         True on success, otherwise PEAR::Error
  1871.      * @see     NET_FTP_ERR_REMOTEPATHNODIR, NET_FTP_ERR_DELETEDIR_FAILED
  1872.      */
  1873.     
  1874.     function _rm_dir_recursive($dir)
  1875.     {
  1876.         if (substr($dir(strlen($dir- 1)1!= "/"{
  1877.             return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'"NET_FTP_ERR_REMOTEPATHNODIR);
  1878.         }
  1879.         $file_list $this->_ls_files($dir);
  1880.         foreach ($file_list as $file{
  1881.             $file $dir.$file["name"];
  1882.             $res $this->rm($file);
  1883.             if ($this->isError($res)) {
  1884.                 return $res;
  1885.             }
  1886.         }
  1887.         $dir_list $this->_ls_dirs($dir);
  1888.         foreach ($dir_list as $new_dir{
  1889.             $new_dir $dir.$new_dir["name"]."/";
  1890.             $res $this->_rm_dir_recursive($new_dir);
  1891.             if ($this->isError($res)) {
  1892.                 return $res;
  1893.             }
  1894.         }
  1895.         $res $this->_rm_dir($dir);
  1896.         if (PEAR::isError($res)) {
  1897.             return $res;
  1898.         else {
  1899.             return true;
  1900.         }
  1901.     }
  1902.  
  1903.     /**
  1904.      * Lists up files and directories
  1905.      *
  1906.      * @access  private
  1907.      * @param   string $dir  The directory to list up
  1908.      * @return  array        An array of dirs and files
  1909.      */
  1910.     
  1911.     function _ls_both($dir)
  1912.     {
  1913.         $list_splitted $this->_list_and_parse($dir);
  1914.         if (!is_array($list_splitted["files"])) {
  1915.             $list_splitted["files"= array();
  1916.         }
  1917.         if (!is_array($list_splitted["dirs"])) {
  1918.             $list_splitted["dirs"= array();
  1919.         }
  1920.         $res = array();
  1921.         @array_splice($res00$list_splitted["files"]);
  1922.         @array_splice($res00$list_splitted["dirs"]);
  1923.         return $res;
  1924.     }
  1925.  
  1926.     /**
  1927.      * Lists up directories
  1928.      *
  1929.      * @access  private
  1930.      * @param   string $dir  The directory to list up
  1931.      * @return  array        An array of dirs
  1932.      */
  1933.     
  1934.     function _ls_dirs($dir)
  1935.     {
  1936.         $list $this->_list_and_parse($dir);
  1937.         if (PEAR::isError($list)) {
  1938.             return $list;
  1939.         }
  1940.         return $list["dirs"];
  1941.     }
  1942.  
  1943.     /**
  1944.      * Lists up files
  1945.      *
  1946.      * @access  private
  1947.      * @param   string $dir  The directory to list up
  1948.      * @return  array        An array of files
  1949.      */
  1950.     
  1951.     function _ls_files($dir)
  1952.     {
  1953.         $list $this->_list_and_parse($dir);
  1954.         if (PEAR::isError($list)) {
  1955.             return $list;
  1956.         }
  1957.         return $list["files"];
  1958.     }
  1959.  
  1960.     /**
  1961.      * This lists up the directory-content and parses the items into well-formated arrays
  1962.      * The results of this array are sorted (dirs on top, sorted by name;
  1963.      * files below, sorted by name).
  1964.      *
  1965.      * @access  private
  1966.      * @param   string $dir  The directory to parse
  1967.      * @return  array        Lists of dirs and files
  1968.      * @see     NET_FTP_ERR_RAWDIRLIST_FAILED
  1969.      */
  1970.     
  1971.     function _list_and_parse($dir)
  1972.     {
  1973.         $dirs_list = array();
  1974.         $files_list = array();
  1975.         $dir_list @ftp_rawlist($this->_handle$dir);
  1976.         if ($dir_list === false{
  1977.             return PEAR::raiseError('Could not get raw directory listing.'NET_FTP_ERR_RAWDIRLIST_FAILED);
  1978.         }
  1979.         if (!isset($this->_matcher|| PEAR::isError($this->_matcher)) {
  1980.             $this->_matcher $this->_determine_os_match($dir_list);
  1981.             if (PEAR::isError($this->_matcher)) {
  1982.                 return $this->_matcher;
  1983.             }
  1984.         }
  1985.         foreach ($dir_list as $entry{
  1986.             if (!preg_match($this->_matcher['pattern']$entry$m)) {
  1987.                 continue;
  1988.             }
  1989.             $entry = array();
  1990.             foreach ($this->_matcher['map'as $key=>$val{
  1991.                 $entry[$key$m[$val];
  1992.             }
  1993.             $entry['stamp'$this->_parse_Date($entry['date']);
  1994.  
  1995.             if ($entry['is_dir']{
  1996.                 $dirs_list[$entry;
  1997.             else {
  1998.                 $files_list[$entry;
  1999.             }
  2000.         }
  2001.         @usort($dirs_listarray("Net_FTP""_nat_sort"));
  2002.         @usort($files_listarray("Net_FTP""_nat_sort"));
  2003.         $res["dirs"(is_array($dirs_list)) $dirs_list : array();
  2004.         $res["files"(is_array($files_list)) $files_list : array();
  2005.         return $res;
  2006.     }
  2007.     
  2008.     /**
  2009.      * Determine server OS
  2010.      * This determines the server OS and returns a valid regex to parse
  2011.      * ls() output.
  2012.      *
  2013.      * @access  private
  2014.      * @param   array $dir_list The raw dir list to parse
  2015.      * @return  mixed An array of 'pattern' and 'map' on success, otherwise PEAR::Error
  2016.      * @see     NET_FTP_ERR_DIRLIST_UNSUPPORTED
  2017.      */
  2018.     
  2019.     function _determine_os_match(&$dir_list{
  2020.     foreach ($dir_list as $entry{
  2021.         foreach ($this->_ls_match as $os => $match{
  2022.             if (preg_match($match['pattern']$entry)) {
  2023.                     return $match;
  2024.                 }
  2025.             }
  2026.     }
  2027.         $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!';
  2028.         return PEAR::raiseError($errorNET_FTP_ERR_DIRLIST_UNSUPPORTED);
  2029.     }
  2030.     /**
  2031.      * Lists a local directory
  2032.      *
  2033.      * @access  private
  2034.      * @param   string $dir_path  The dir to list
  2035.      * @return  array             The list of dirs and files
  2036.      */
  2037.     
  2038.     function _ls_local($dir_path)
  2039.     {
  2040.         $dir dir($dir_path);
  2041.         $dir_list = array();
  2042.         $file_list = array();
  2043.         while (false !== ($entry $dir->read())) {
  2044.             if (($entry != '.'&& ($entry != '..')) {
  2045.                 if (is_dir($dir_path.$entry)) {
  2046.                     $dir_list[$entry;
  2047.                 else {
  2048.                     $file_list[$entry;
  2049.                 }
  2050.             }
  2051.         }
  2052.         $dir->close();
  2053.         $res['dirs'$dir_list;
  2054.         $res['files'$file_list;
  2055.         return $res;
  2056.     }
  2057.  
  2058.     /**
  2059.      * Function for use with usort().
  2060.      * Compares the list-array-elements by name.
  2061.      *
  2062.      * @access  private
  2063.      */
  2064.     
  2065.     function _nat_sort($item_1$item_2)
  2066.     {
  2067.         return strnatcmp($item_1['name']$item_2['name']);
  2068.     }
  2069.  
  2070.     /**
  2071.      * Parse dates to timestamps
  2072.      *
  2073.      * @access  private
  2074.      * @param   string $date  Date
  2075.      * @return  int           Timestamp
  2076.      * @see     NET_FTP_ERR_DATEFORMAT_FAILED
  2077.      */
  2078.     
  2079.     function _parse_Date($date)
  2080.     {
  2081.         // Sep 10 22:06 => Sep 10, <year> 22:06
  2082.         if (preg_match('/([A-Za-z]+)[ ]+([0-9]+)[ ]+([0-9]+):([0-9]+)/'$date$res)) {
  2083.             $year date('Y');
  2084.             $month $res[1];
  2085.             $day $res[2];
  2086.             $hour $res[3];
  2087.             $minute $res[4];
  2088.             $date = "$month $day$year $hour:$minute";
  2089.             $tmpDate strtotime($date);
  2090.             if ($tmpDate time()) {
  2091.                 $year--;
  2092.                 $date = "$month $day$year $hour:$minute";
  2093.             }
  2094.         }
  2095.         // 09-10-04 => 09/10/04
  2096.         elseif (preg_match('/^\d\d-\d\d-\d\d/',$date)) {
  2097.             $date str_replace('-','/',$date);
  2098.         }
  2099.         $res strtotime($date);
  2100.         if (!$res{
  2101.             return $this->raiseError('Dateconversion failed.'NET_FTP_ERR_DATEFORMAT_FAILED);
  2102.         }
  2103.         return $res;
  2104.     }
  2105. }
  2106. ?>

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