PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Varien/Io/Ftp.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 327 lines | 164 code | 35 blank | 128 comment | 28 complexity | f349b9b833133853c902abb493d4f3bd MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_Io
  23. * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * FTP client
  28. *
  29. * @category Varien
  30. * @package Varien_Io
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Varien_Io_Ftp extends Varien_Io_Abstract
  34. {
  35. const ERROR_EMPTY_HOST = 1;
  36. const ERROR_INVALID_CONNECTION = 2;
  37. const ERROR_INVALID_LOGIN = 3;
  38. const ERROR_INVALID_PATH = 4;
  39. const ERROR_INVALID_MODE = 5;
  40. const ERROR_INVALID_DESTINATION = 6;
  41. const ERROR_INVALID_SOURCE = 7;
  42. /**
  43. * Connection config
  44. *
  45. * @var array
  46. */
  47. protected $_config;
  48. /**
  49. * An FTP connection
  50. *
  51. * @var resource
  52. */
  53. protected $_conn;
  54. /**
  55. * Error code
  56. *
  57. * @var int
  58. */
  59. protected $_error;
  60. protected $_tmpFilename;
  61. /**
  62. * Open a connection
  63. *
  64. * Possible argument keys:
  65. * - host required
  66. * - port default 21
  67. * - timeout default 90
  68. * - user default anonymous
  69. * - password default empty
  70. * - ssl default false
  71. * - passive default false
  72. * - path default empty
  73. * - file_mode default FTP_BINARY
  74. *
  75. * @param array $args
  76. * @return boolean
  77. */
  78. public function open(array $args=array())
  79. {
  80. if (empty($args['host'])) {
  81. $this->_error = self::ERROR_EMPTY_HOST;
  82. throw new Varien_Io_Exception('Empty host specified');
  83. }
  84. if (empty($args['port'])) {
  85. $args['port'] = 21;
  86. }
  87. if (empty($args['user'])) {
  88. $args['user'] = 'anonymous';
  89. $args['password'] = 'anonymous@noserver.com';
  90. }
  91. if (empty($args['password'])) {
  92. $args['password'] = '';
  93. }
  94. if (empty($args['timeout'])) {
  95. $args['timeout'] = 90;
  96. }
  97. if (empty($args['file_mode'])) {
  98. $args['file_mode'] = FTP_BINARY;
  99. }
  100. $this->_config = $args;
  101. if (empty($this->_config['ssl'])) {
  102. $this->_conn = @ftp_connect($this->_config['host'], $this->_config['port'], $this->_config['timeout']);
  103. } else {
  104. $this->_conn = @ftp_ssl_connect($this->_config['host'], $this->_config['port'], $this->_config['timeout']);
  105. }
  106. if (!$this->_conn) {
  107. $this->_error = self::ERROR_INVALID_CONNECTION;
  108. throw new Varien_Io_Exception('Could not establish FTP connection, invalid host or port');
  109. }
  110. if (!@ftp_login($this->_conn, $this->_config['user'], $this->_config['password'])) {
  111. $this->_error = self::ERROR_INVALID_LOGIN;
  112. $this->close();
  113. throw new Varien_Io_Exception('Invalid user name or password');
  114. }
  115. if (!empty($this->_config['path'])) {
  116. if (!@ftp_chdir($this->_conn, $this->_config['path'])) {
  117. $this->_error = self::ERROR_INVALID_PATH;
  118. $this->close();
  119. throw new Varien_Io_Exception('Invalid path');
  120. }
  121. }
  122. if (!empty($this->_config['passive'])) {
  123. if (!@ftp_pasv($this->_conn, true)) {
  124. $this->_error = self::ERROR_INVALID_MODE;
  125. $this->close();
  126. throw new Varien_Io_Exception('Invalid file transfer mode');
  127. }
  128. }
  129. return true;
  130. }
  131. /**
  132. * Close a connection
  133. *
  134. * @return boolean
  135. */
  136. public function close()
  137. {
  138. return @ftp_close($this->_conn);
  139. }
  140. /**
  141. * Create a directory
  142. *
  143. * @todo implement $mode and $recursive
  144. * @param string $dir
  145. * @param int $mode
  146. * @param boolean $recursive
  147. * @return boolean
  148. */
  149. public function mkdir($dir, $mode=0777, $recursive=true)
  150. {
  151. return @ftp_mkdir($this->_conn, $dir);
  152. }
  153. /**
  154. * Delete a directory
  155. *
  156. * @param string $dir
  157. * @return boolean
  158. */
  159. public function rmdir($dir, $recursive=false)
  160. {
  161. return @ftp_rmdir($this->_conn, $dir);
  162. }
  163. /**
  164. * Get current working directory
  165. *
  166. * @return string
  167. */
  168. public function pwd()
  169. {
  170. return @ftp_pwd($this->_conn);
  171. }
  172. /**
  173. * Change current working directory
  174. *
  175. * @param string $dir
  176. * @return boolean
  177. */
  178. public function cd($dir)
  179. {
  180. return @ftp_chdir($this->_conn, $dir);
  181. }
  182. /**
  183. * Read a file to result, file or stream
  184. *
  185. * @param string $filename
  186. * @param string|resource|null $dest destination file name, stream, or if null will return file contents
  187. * @return string
  188. */
  189. public function read($filename, $dest=null)
  190. {
  191. if (is_string($dest)) {
  192. $result = ftp_get($this->_conn, $dest, $filename, $this->_config['file_mode']);
  193. } else {
  194. if (is_resource($dest)) {
  195. $stream = $dest;
  196. } elseif (is_null($dest)) {
  197. $stream = tmpfile();
  198. } else {
  199. $this->_error = self::ERROR_INVALID_DESTINATION;
  200. return false;
  201. }
  202. $result = ftp_fget($this->_conn, $stream, $filename, $this->_config['file_mode']);
  203. if (is_null($dest)) {
  204. fseek($stream, 0);
  205. $result = '';
  206. for ($result = ''; $s = fread($stream, 4096); $result .= $s);
  207. fclose($stream);
  208. }
  209. }
  210. return $result;
  211. }
  212. /**
  213. * Write a file from string, file or stream
  214. *
  215. * @param string $filename
  216. * @param string|resource $src filename, string data or source stream
  217. * @return int|boolean
  218. */
  219. public function write($filename, $src, $mode=null)
  220. {
  221. if (is_string($src) && is_readable($src)) {
  222. return @ftp_put($this->_conn, $filename, $src, $this->_config['file_mode']);
  223. } else {
  224. if (is_string($src)) {
  225. $stream = tmpfile();
  226. fputs($stream, $src);
  227. fseek($stream, 0);
  228. } elseif (is_resource($src)) {
  229. $stream = $src;
  230. } else {
  231. $this->_error = self::ERROR_INVALID_SOURCE;
  232. return false;
  233. }
  234. $result = ftp_fput($this->_conn, $filename, $stream, $this->_config['file_mode']);
  235. if (is_string($src)) {
  236. fclose($stream);
  237. }
  238. return $result;
  239. }
  240. }
  241. /**
  242. * Delete a file
  243. *
  244. * @param string $filename
  245. * @return boolean
  246. */
  247. public function rm($filename)
  248. {
  249. return @ftp_delete($this->_conn, $filename);
  250. }
  251. /**
  252. * Rename or move a directory or a file
  253. *
  254. * @param string $src
  255. * @param string $dest
  256. * @return boolean
  257. */
  258. public function mv($src, $dest)
  259. {
  260. return @ftp_rename($this->_conn, $src, $dest);
  261. }
  262. /**
  263. * Change mode of a directory or a file
  264. *
  265. * @param string $filename
  266. * @param int $mode
  267. * @return boolean
  268. */
  269. public function chmod($filename, $mode)
  270. {
  271. return @ftp_chmod($this->_conn, $mode, $filename);
  272. }
  273. public function ls($grep=null)
  274. {
  275. $ls = @ftp_nlist($this->_conn, '.');
  276. $list = array();
  277. foreach ($ls as $file) {
  278. $list[] = array(
  279. 'text'=>$file,
  280. 'id'=>$this->pwd().'/'.$file,
  281. );
  282. }
  283. return $list;
  284. }
  285. protected function _tmpFilename($new=false)
  286. {
  287. if ($new || !$this->_tmpFilename) {
  288. $this->_tmpFilename = tempnam( md5(uniqid(rand(), TRUE)), '' );
  289. }
  290. return $this->_tmpFilename;
  291. }
  292. }