ftp.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Storage;
  9. class FTP extends \OC\Files\Storage\StreamWrapper{
  10. private $password;
  11. private $user;
  12. private $host;
  13. private $secure;
  14. private $root;
  15. private static $tempFiles=array();
  16. public function __construct($params) {
  17. if (isset($params['host']) && isset($params['user']) && isset($params['password'])) {
  18. $this->host=$params['host'];
  19. $this->user=$params['user'];
  20. $this->password=$params['password'];
  21. if (isset($params['secure'])) {
  22. if (is_string($params['secure'])) {
  23. $this->secure = ($params['secure'] === 'true');
  24. } else {
  25. $this->secure = (bool)$params['secure'];
  26. }
  27. } else {
  28. $this->secure = false;
  29. }
  30. $this->root=isset($params['root'])?$params['root']:'/';
  31. if ( ! $this->root || $this->root[0]!='/') {
  32. $this->root='/'.$this->root;
  33. }
  34. if (substr($this->root, -1) !== '/') {
  35. $this->root .= '/';
  36. }
  37. } else {
  38. throw new \Exception('Creating \OC\Files\Storage\FTP storage failed');
  39. }
  40. }
  41. public function getId(){
  42. return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root;
  43. }
  44. /**
  45. * construct the ftp url
  46. * @param string $path
  47. * @return string
  48. */
  49. public function constructUrl($path) {
  50. $url='ftp';
  51. if ($this->secure) {
  52. $url.='s';
  53. }
  54. $url.='://'.$this->user.':'.$this->password.'@'.$this->host.$this->root.$path;
  55. return $url;
  56. }
  57. /**
  58. * Unlinks file or directory
  59. * @param string $path
  60. */
  61. public function unlink($path) {
  62. if ($this->is_dir($path)) {
  63. return $this->rmdir($path);
  64. }
  65. else {
  66. $url = $this->constructUrl($path);
  67. $result = unlink($url);
  68. clearstatcache(true, $url);
  69. return $result;
  70. }
  71. }
  72. public function fopen($path,$mode) {
  73. switch($mode) {
  74. case 'r':
  75. case 'rb':
  76. case 'w':
  77. case 'wb':
  78. case 'a':
  79. case 'ab':
  80. //these are supported by the wrapper
  81. $context = stream_context_create(array('ftp' => array('overwrite' => true)));
  82. return fopen($this->constructUrl($path), $mode, false, $context);
  83. case 'r+':
  84. case 'w+':
  85. case 'wb+':
  86. case 'a+':
  87. case 'x':
  88. case 'x+':
  89. case 'c':
  90. case 'c+':
  91. //emulate these
  92. if (strrpos($path, '.')!==false) {
  93. $ext=substr($path, strrpos($path, '.'));
  94. } else {
  95. $ext='';
  96. }
  97. $tmpFile=\OCP\Files::tmpFile($ext);
  98. \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
  99. if ($this->file_exists($path)) {
  100. $this->getFile($path, $tmpFile);
  101. }
  102. self::$tempFiles[$tmpFile]=$path;
  103. return fopen('close://'.$tmpFile, $mode);
  104. }
  105. return false;
  106. }
  107. public function writeBack($tmpFile) {
  108. if (isset(self::$tempFiles[$tmpFile])) {
  109. $this->uploadFile($tmpFile, self::$tempFiles[$tmpFile]);
  110. unlink($tmpFile);
  111. }
  112. }
  113. /**
  114. * check if php-ftp is installed
  115. */
  116. public static function checkDependencies() {
  117. if (function_exists('ftp_login')) {
  118. return(true);
  119. } else {
  120. return array('ftp');
  121. }
  122. }
  123. }