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:      */
  65. 65:     public function __construct()
  66. 66:     {
  67. 67:         if (!extension_loaded('ftp')) {
  68. 68:             throw new Exception("PHP extension FTP is not loaded.");
  69. 69:         }
  70. 70:     }
  71. 71:  
  72. 72:  
  73. 73:  
  74. 74:     /**
  75. 75:      * Magic method (do not call directly).
  76. 76:      * @param  string  method name
  77. 77:      * @param  array   arguments
  78. 78:      * @return mixed 
  79. 79:      * @throws MemberAccessException
  80. 80:      * @throws FtpException
  81. 81:      */
  82. 82:     public function __call($name$args)
  83. 83:     {
  84. 84:         $name strtolower($name);
  85. 85:         $silent strncmp($name'try'3=== 0;
  86. 86:         $func $silent substr($name3$name;
  87. 87:         static $aliases array(
  88. 88:             'sslconnect' => 'ssl_connect',
  89. 89:             'getoption' => 'get_option',
  90. 90:             'setoption' => 'set_option',
  91. 91:             'nbcontinue' => 'nb_continue',
  92. 92:             'nbfget' => 'nb_fget',
  93. 93:             'nbfput' => 'nb_fput',
  94. 94:             'nbget' => 'nb_get',
  95. 95:             'nbput' => 'nb_put',
  96. 96:         );
  97. 97:         $func 'ftp_' (isset($aliases[$func]$aliases[$func$func);
  98. 98:  
  99. 99:         if (!function_exists($func)) {
  100. 100:             return parent::__call($name$args);
  101. 101:         }
  102. 102:  
  103. 103:  
  104. 104:         Tools::tryError();
  105. 105:  
  106. 106:         if ($func === 'ftp_connect' || $func === 'ftp_ssl_connect'{
  107. 107:             $this->state array($name => $args);
  108. 108:             $this->resource call_user_func_array($func$args);
  109. 109:             $res NULL;
  110. 110:  
  111. 111:         elseif (!is_resource($this->resource)) {
  112. 112:             Tools::catchError($msg);
  113. 113:             throw new FtpException("Not connected to FTP server. Call connect() or ssl_connect() first.");
  114. 114:  
  115. 115:         else {
  116. 116:             if ($func === 'ftp_login' || $func === 'ftp_pasv'{
  117. 117:                 $this->state[$name$args;
  118. 118:             }
  119. 119:  
  120. 120:             array_unshift($args$this->resource);
  121. 121:             $res call_user_func_array($func$args);
  122. 122:  
  123. 123:             if ($func === 'ftp_chdir' || $func === 'ftp_cdup'{
  124. 124:                 $this->state['chdir'array(ftp_pwd($this->resource));
  125. 125:             }
  126. 126:         }
  127. 127:  
  128. 128:         if (Tools::catchError($msg&& !$silent{
  129. 129:             throw new FtpException($msg);
  130. 130:         }
  131. 131:  
  132. 132:         return $res;
  133. 133:     }
  134. 134:  
  135. 135:  
  136. 136:  
  137. 137:     /**
  138. 138:      * Reconnects to FTP server.
  139. 139:      * @return void 
  140. 140:      */
  141. 141:     public function reconnect()
  142. 142:     {
  143. 143:         @ftp_close($this->resource)// intentionally @
  144. 144:         foreach ($this->state as $name => $args{
  145. 145:             call_user_func_array(array($this$name)$args);
  146. 146:         }
  147. 147:     }
  148. 148:  
  149. 149:  
  150. 150:  
  151. 151:     /**
  152. 152:      * Checks if file or directory exists.
  153. 153:      * @param  string 
  154. 154:      * @return bool 
  155. 155:      */
  156. 156:     public function fileExists($file)
  157. 157:     {
  158. 158:         return is_array($this->nlist($file));
  159. 159:     }
  160. 160:  
  161. 161:  
  162. 162:  
  163. 163:     /**
  164. 164:      * Checks if directory exists.
  165. 165:      * @param  string 
  166. 166:      * @return bool 
  167. 167:      */
  168. 168:     public function isDir($dir)
  169. 169:     {
  170. 170:         $current $this->pwd();
  171. 171:         try {
  172. 172:             $this->chdir($dir);
  173. 173:         catch (FtpException $e{
  174. 174:         }
  175. 175:         $this->chdir($current);
  176. 176:         return empty($e);
  177. 177:     }
  178. 178:  
  179. 179:  
  180. 180:  
  181. 181:     /**
  182. 182:      * Recursive creates directories.
  183. 183:      * @param  string 
  184. 184:      * @return void 
  185. 185:      */
  186. 186:     public function mkDirRecursive($dir)
  187. 187:     {
  188. 188:         $parts explode('/'$dir);
  189. 189:         $path '';
  190. 190:         while (!empty($parts)) {
  191. 191:             $path .= array_shift($parts);
  192. 192:             try {
  193. 193:                 if ($path !== ''$this->mkdir($path);
  194. 194:             catch (FtpException $e{
  195. 195:                 if (!$this->isDir($path)) {
  196. 196:                     throw new FtpException("Cannot create directory '$path'.");
  197. 197:                 }
  198. 198:             }
  199. 199:             $path .= '/';
  200. 200:         }
  201. 201:     }
  202. 202:  
  203. 204:  
  204. 205:  
  205. 206:  
  206. 207: /**
  207. 208:  * FTP server exception.
  208. 209:  *
  209. 210:  * @package    Nette\Web
  210. 211:  */
  211. 212: class FtpException extends Exception