PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/league/flysystem/src/Adapter/Ftp.php

https://gitlab.com/pthapa81/MeroSaaman-1.0
PHP | 396 lines | 227 code | 68 blank | 101 comment | 26 complexity | 98994ece7d2b21e872abd37b1b2a4e04 MD5 | raw file
  1. <?php
  2. namespace League\Flysystem\Adapter;
  3. use League\Flysystem\Adapter\Polyfill\StreamedCopyTrait;
  4. use League\Flysystem\AdapterInterface;
  5. use League\Flysystem\Config;
  6. use League\Flysystem\Util;
  7. use RuntimeException;
  8. class Ftp extends AbstractFtpAdapter
  9. {
  10. use StreamedCopyTrait;
  11. /**
  12. * @var int
  13. */
  14. protected $transferMode = FTP_BINARY;
  15. /**
  16. * @var array
  17. */
  18. protected $configurable = [
  19. 'host', 'port', 'username',
  20. 'password', 'ssl', 'timeout',
  21. 'root', 'permPrivate',
  22. 'permPublic', 'passive',
  23. 'transferMode',
  24. ];
  25. /**
  26. * Set the transfer mode.
  27. *
  28. * @param int $mode
  29. *
  30. * @return $this
  31. */
  32. public function setTransferMode($mode)
  33. {
  34. $this->transferMode = $mode;
  35. return $this;
  36. }
  37. /**
  38. * Set if Ssl is enabled.
  39. *
  40. * @param bool $ssl
  41. *
  42. * @return $this
  43. */
  44. public function setSsl($ssl)
  45. {
  46. $this->ssl = (bool) $ssl;
  47. return $this;
  48. }
  49. /**
  50. * Set if passive mode should be used.
  51. *
  52. * @param bool $passive
  53. */
  54. public function setPassive($passive = true)
  55. {
  56. $this->passive = $passive;
  57. }
  58. /**
  59. * Connect to the FTP server.
  60. */
  61. public function connect()
  62. {
  63. if ($this->ssl) {
  64. $this->connection = ftp_ssl_connect($this->getHost(), $this->getPort(), $this->getTimeout());
  65. } else {
  66. $this->connection = ftp_connect($this->getHost(), $this->getPort(), $this->getTimeout());
  67. }
  68. if (! $this->connection) {
  69. throw new RuntimeException('Could not connect to host: '.$this->getHost().', port:'.$this->getPort());
  70. }
  71. $this->login();
  72. $this->setConnectionPassiveMode();
  73. $this->setConnectionRoot();
  74. }
  75. /**
  76. * Set the connections to passive mode.
  77. *
  78. * @throws RuntimeException
  79. */
  80. protected function setConnectionPassiveMode()
  81. {
  82. if (! ftp_pasv($this->getConnection(), $this->passive)) {
  83. throw new RuntimeException('Could not set passive mode for connection: '.$this->getHost().'::'.$this->getPort());
  84. }
  85. }
  86. /**
  87. * Set the connection root.
  88. */
  89. protected function setConnectionRoot()
  90. {
  91. $root = $this->getRoot();
  92. $connection = $this->getConnection();
  93. if ($root && ! ftp_chdir($connection, $root)) {
  94. throw new RuntimeException('Root is invalid or does not exist: '.$this->getRoot());
  95. }
  96. // Store absolute path for further reference.
  97. // This is needed when creating directories and
  98. // initial root was a relative path, else the root
  99. // would be relative to the chdir'd path.
  100. $this->root = ftp_pwd($connection);
  101. }
  102. /**
  103. * Login.
  104. *
  105. * @throws RuntimeException
  106. */
  107. protected function login()
  108. {
  109. set_error_handler(function () {});
  110. $isLoggedIn = ftp_login($this->getConnection(), $this->getUsername(), $this->getPassword());
  111. restore_error_handler();
  112. if (! $isLoggedIn) {
  113. $this->disconnect();
  114. throw new RuntimeException('Could not login with connection: '.$this->getHost().'::'.$this->getPort().', username: '.$this->getUsername());
  115. }
  116. }
  117. /**
  118. * Disconnect from the FTP server.
  119. */
  120. public function disconnect()
  121. {
  122. if ($this->connection) {
  123. ftp_close($this->connection);
  124. }
  125. $this->connection = null;
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function write($path, $contents, Config $config)
  131. {
  132. $mimetype = Util::guessMimeType($path, $contents);
  133. $config = Util::ensureConfig($config);
  134. $stream = tmpfile();
  135. fwrite($stream, $contents);
  136. rewind($stream);
  137. $result = $this->writeStream($path, $stream, $config);
  138. $result = fclose($stream) && $result;
  139. if ($result === false) {
  140. return false;
  141. }
  142. if ($visibility = $config->get('visibility')) {
  143. $this->setVisibility($path, $visibility);
  144. }
  145. return compact('path', 'contents', 'mimetype', 'visibility');
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function writeStream($path, $resource, Config $config)
  151. {
  152. $this->ensureDirectory(Util::dirname($path));
  153. $config = Util::ensureConfig($config);
  154. if (! ftp_fput($this->getConnection(), $path, $resource, $this->transferMode)) {
  155. return false;
  156. }
  157. if ($visibility = $config->get('visibility')) {
  158. $this->setVisibility($path, $visibility);
  159. }
  160. return compact('path', 'visibility');
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function update($path, $contents, Config $config)
  166. {
  167. return $this->write($path, $contents, $config);
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function updateStream($path, $resource, Config $config)
  173. {
  174. return $this->writeStream($path, $resource, $config);
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function rename($path, $newpath)
  180. {
  181. return ftp_rename($this->getConnection(), $path, $newpath);
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function delete($path)
  187. {
  188. return ftp_delete($this->getConnection(), $path);
  189. }
  190. /**
  191. * {@inheritdoc}
  192. */
  193. public function deleteDir($dirname)
  194. {
  195. $connection = $this->getConnection();
  196. $contents = array_reverse($this->listDirectoryContents($dirname));
  197. foreach ($contents as $object) {
  198. if ($object['type'] === 'file') {
  199. if (! ftp_delete($connection, $object['path'])) {
  200. return false;
  201. }
  202. } elseif (! ftp_rmdir($connection, $object['path'])) {
  203. return false;
  204. }
  205. }
  206. return ftp_rmdir($connection, $dirname);
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. public function createDir($dirname, Config $config)
  212. {
  213. $result = false;
  214. $connection = $this->getConnection();
  215. $directories = explode('/', $dirname);
  216. foreach ($directories as $directory) {
  217. $result = $this->createActualDirectory($directory, $connection);
  218. if (! $result) {
  219. break;
  220. }
  221. ftp_chdir($connection, $directory);
  222. }
  223. $this->setConnectionRoot();
  224. if (! $result) {
  225. return false;
  226. }
  227. return ['path' => $dirname];
  228. }
  229. /**
  230. * Create a directory.
  231. *
  232. * @param string $directory
  233. * @param resource $connection
  234. *
  235. * @return bool
  236. */
  237. protected function createActualDirectory($directory, $connection)
  238. {
  239. // List the current directory
  240. $listing = ftp_nlist($connection, '.');
  241. foreach ($listing as $key => $item) {
  242. if (preg_match('~^\./.*~', $item)) {
  243. $listing[$key] = substr($item, 2);
  244. }
  245. }
  246. if (in_array($directory, $listing)) {
  247. return true;
  248. }
  249. return (boolean) ftp_mkdir($connection, $directory);
  250. }
  251. /**
  252. * {@inheritdoc}
  253. */
  254. public function getMetadata($path)
  255. {
  256. $listing = ftp_rawlist($this->getConnection(), $path);
  257. if (empty($listing)) {
  258. return false;
  259. }
  260. $metadata = $this->normalizeObject($listing[0], '');
  261. if ($metadata['path'] === '.') {
  262. $metadata['path'] = $path;
  263. }
  264. return $metadata;
  265. }
  266. /**
  267. * {@inheritdoc}
  268. */
  269. public function getMimetype($path)
  270. {
  271. if (! $metadata = $this->read($path)) {
  272. return false;
  273. }
  274. $metadata['mimetype'] = Util::guessMimeType($path, $metadata['contents']);
  275. return $metadata;
  276. }
  277. /**
  278. * {@inheritdoc}
  279. */
  280. public function read($path)
  281. {
  282. if (! $object = $this->readStream($path)) {
  283. return false;
  284. }
  285. $object['contents'] = stream_get_contents($object['stream']);
  286. fclose($object['stream']);
  287. unset($object['stream']);
  288. return $object;
  289. }
  290. /**
  291. * {@inheritdoc}
  292. */
  293. public function readStream($path)
  294. {
  295. $stream = fopen('php://temp', 'w+');
  296. $result = ftp_fget($this->getConnection(), $stream, $path, $this->transferMode);
  297. rewind($stream);
  298. if (! $result) {
  299. fclose($stream);
  300. return false;
  301. }
  302. return compact('stream');
  303. }
  304. /**
  305. * {@inheritdoc}
  306. */
  307. public function setVisibility($path, $visibility)
  308. {
  309. $mode = $visibility === AdapterInterface::VISIBILITY_PUBLIC ? $this->getPermPublic() : $this->getPermPrivate();
  310. if (! ftp_chmod($this->getConnection(), $mode, $path)) {
  311. return false;
  312. }
  313. return compact('visibility');
  314. }
  315. /**
  316. * {@inheritdoc}
  317. *
  318. * @param string $directory
  319. */
  320. protected function listDirectoryContents($directory, $recursive = true)
  321. {
  322. $listing = ftp_rawlist($this->getConnection(), '-lna '.$directory, $recursive);
  323. if ($listing === false) {
  324. return [];
  325. }
  326. return $this->normalizeListing($listing, $directory);
  327. }
  328. }