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