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