Source for file Ftp.php

Documentation is available at Ftp.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Web
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Object.php';
  23. 23:  
  24. 24:  
  25. 25:  
  26. 26: /**
  27. 27:  * Access to a FTP server.
  28. 28:  *
  29. 29:  * <code>
  30. 30:  * $ftp = new Ftp;
  31. 31:  * $ftp->connect('ftp.example.com');
  32. 32:  * $ftp->login('anonymous', 'example@example.com');
  33. 33:  * $ftp->get('file.txt', 'README', Ftp::ASCII);
  34. 34:  * </code>
  35. 35:  *
  36. 36:  * @author     David Grudl
  37. 37:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  38. 38:  * @package    Nette\Web
  39. 39:  */
  40. 40: class Ftp extends Object
  41. 41: {
  42. 42:     /**#@+ FTP constant alias */
  43. 43:     const ASCII = FTP_ASCII;
  44. 44:     const TEXT = FTP_TEXT;
  45. 45:     const BINARY = FTP_BINARY;
  46. 46:     const IMAGE = FTP_IMAGE;
  47. 47:     const TIMEOUT_SEC = FTP_TIMEOUT_SEC;
  48. 48:     const AUTOSEEK = FTP_AUTOSEEK;
  49. 49:     const AUTORESUME = FTP_AUTORESUME;
  50. 50:     const FAILED = FTP_FAILED;
  51. 51:     const FINISHED = FTP_FINISHED;
  52. 52:     const MOREDATA = FTP_MOREDATA;
  53. 53:     /**#@-*/
  54. 54:  
  55. 55:     /** @var resource */
  56. 56:     private $resource;
  57. 57:  
  58. 58:     /** @var array */
  59. 59:     private $state;
  60. 60:  
  61. 61:  
  62. 62:  
  63. 63:     /**
  64. 64:      * Magic method (do not call directly).
  65. 65:      * @param  string  method name
  66. 66:      * @param  array   arguments
  67. 67:      * @return mixed 
  68. 68:      * @throws MemberAccessException
  69. 69:      * @throws FtpException
  70. 70:      */
  71. 71:     public function __call($name$args)
  72. 72:     {
  73. 73:         $name strtolower($name);
  74. 74:         $silent strncmp($name'try'3=== 0;
  75. 75:         $func $silent substr($name3$name;
  76. 76:         static $aliases array(
  77. 77:             'sslconnect' => 'ssl_connect',
  78. 78:             'getoption' => 'get_option',
  79. 79:             'setoption' => 'set_option',
  80. 80:             'nbcontinue' => 'nb_continue',
  81. 81:             'nbfget' => 'nb_fget',
  82. 82:             'nbfput' => 'nb_fput',
  83. 83:             'nbget' => 'nb_get',
  84. 84:             'nbput' => 'nb_put',
  85. 85:         );
  86. 86:         $func 'ftp_' (isset($aliases[$func]$aliases[$func$func);
  87. 87:  
  88. 88:         if (!function_exists($func)) {
  89. 89:             return parent::__call($name$args);
  90. 90:         }
  91. 91:  
  92. 92:  
  93. 93:         Tools::tryError();
  94. 94:  
  95. 95:         if ($func === 'ftp_connect' || $func === 'ftp_ssl_connect'{
  96. 96:             $this->state array($name => $args);
  97. 97:             $this->resource call_user_func_array($func$args);
  98. 98:             $res NULL;
  99. 99:  
  100. 100:         elseif (!is_resource($this->resource)) {
  101. 101:             Tools::catchError($msg);
  102. 102:             throw new FtpException("Not connected to FTP server. Call connect() or ssl_connect() first.");
  103. 103:  
  104. 104:         else {
  105. 105:             if ($func === 'ftp_login' || $func === 'ftp_pasv'{
  106. 106:                 $this->state[$name$args;
  107. 107:             }
  108. 108:  
  109. 109:             array_unshift($args$this->resource);
  110. 110:             $res call_user_func_array($func$args);
  111. 111:  
  112. 112:             if ($func === 'ftp_chdir' || $func === 'ftp_cdup'{
  113. 113:                 $this->state['chdir'array(ftp_pwd($this->resource));
  114. 114:             }
  115. 115:         }
  116. 116:  
  117. 117:         if (Tools::catchError($msg&& !$silent{
  118. 118:             throw new FtpException($msg);
  119. 119:         }
  120. 120:  
  121. 121:         return $res;
  122. 122:     }
  123. 123:  
  124. 124:  
  125. 125:  
  126. 126:     /**
  127. 127:      * Reconnects to FTP server.
  128. 128:      * @return void 
  129. 129:      */
  130. 130:     public function reconnect()
  131. 131:     {
  132. 132:         @ftp_close($this->resource)// intentionally @
  133. 133:         foreach ($this->state as $name => $args{
  134. 134:             call_user_func_array(array($this$name)$args);
  135. 135:         }
  136. 136:     }
  137. 137:  
  138. 138:  
  139. 139:  
  140. 140:     /**
  141. 141:      * Checks if file or directory exists.
  142. 142:      * @param  string 
  143. 143:      * @return bool 
  144. 144:      */
  145. 145:     public function fileExists($file)
  146. 146:     {
  147. 147:         return is_array($this->nlist($file));
  148. 148:     }
  149. 149:  
  150. 150:  
  151. 151:  
  152. 152:     /**
  153. 153:      * Checks if directory exists.
  154. 154:      * @param  string 
  155. 155:      * @return bool 
  156. 156:      */
  157. 157:     public function isDir($dir)
  158. 158:     {
  159. 159:         $current $this->pwd();
  160. 160:         try {
  161. 161:             $this->chdir($dir);
  162. 162:         catch (FtpException $e{
  163. 163:         }
  164. 164:         $this->chdir($current);
  165. 165:         return empty($e);
  166. 166:     }
  167. 167:  
  168. 168:  
  169. 169:  
  170. 170:     /**
  171. 171:      * Recursive creates directories.
  172. 172:      * @param  string 
  173. 173:      * @return void 
  174. 174:      */
  175. 175:     public function mkDirRecursive($dir)
  176. 176:     {
  177. 177:         $parts explode('/'$dir);
  178. 178:         $path '';
  179. 179:         while (!empty($parts)) {
  180. 180:             $path .= array_shift($parts);
  181. 181:             try {
  182. 182:                 if ($path !== ''$this->mkdir($path);
  183. 183:             catch (FtpException $e{
  184. 184:                 if (!$this->isDir($path)) {
  185. 185:                     throw new FtpException("Cannot create directory '$path'.");
  186. 186:                 }
  187. 187:             }
  188. 188:             $path .= '/';
  189. 189:         }
  190. 190:     }
  191. 191:  
  192. 193:  
  193. 194:  
  194. 195:  
  195. 196: /**
  196. 197:  * FTP server exception.
  197. 198:  *
  198. 199:  * @package    Nette\Web
  199. 200:  */
  200. 201: class FtpException extends Exception