PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/dzakiafif/cokelatklasik
PHP | 398 lines | 225 code | 72 blank | 101 comment | 24 complexity | 3f3657ba174da4ab854006bdaf4cb35b 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', 'systemType',
  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 (empty($root) === false && ! 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. $stream = fopen('php://temp', 'w+b');
  133. fwrite($stream, $contents);
  134. rewind($stream);
  135. $result = $this->writeStream($path, $stream, $config);
  136. fclose($stream);
  137. if ($result === false) {
  138. return false;
  139. }
  140. $result['contents'] = $contents;
  141. $result['mimetype'] = Util::guessMimeType($path, $contents);
  142. return $result;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function writeStream($path, $resource, Config $config)
  148. {
  149. $this->ensureDirectory(Util::dirname($path));
  150. if (! ftp_fput($this->getConnection(), $path, $resource, $this->transferMode)) {
  151. return false;
  152. }
  153. if ($visibility = $config->get('visibility')) {
  154. $this->setVisibility($path, $visibility);
  155. }
  156. return compact('path', 'visibility');
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function update($path, $contents, Config $config)
  162. {
  163. return $this->write($path, $contents, $config);
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function updateStream($path, $resource, Config $config)
  169. {
  170. return $this->writeStream($path, $resource, $config);
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function rename($path, $newpath)
  176. {
  177. return ftp_rename($this->getConnection(), $path, $newpath);
  178. }
  179. /**
  180. * {@inheritdoc}
  181. */
  182. public function delete($path)
  183. {
  184. return ftp_delete($this->getConnection(), $path);
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function deleteDir($dirname)
  190. {
  191. $connection = $this->getConnection();
  192. $contents = array_reverse($this->listDirectoryContents($dirname));
  193. foreach ($contents as $object) {
  194. if ($object['type'] === 'file') {
  195. if (! ftp_delete($connection, $object['path'])) {
  196. return false;
  197. }
  198. } elseif (! ftp_rmdir($connection, $object['path'])) {
  199. return false;
  200. }
  201. }
  202. return ftp_rmdir($connection, $dirname);
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function createDir($dirname, Config $config)
  208. {
  209. $result = false;
  210. $connection = $this->getConnection();
  211. $directories = explode('/', $dirname);
  212. foreach ($directories as $directory) {
  213. $result = $this->createActualDirectory($directory, $connection);
  214. if (! $result) {
  215. break;
  216. }
  217. ftp_chdir($connection, $directory);
  218. }
  219. $this->setConnectionRoot();
  220. if (! $result) {
  221. return false;
  222. }
  223. return ['path' => $dirname];
  224. }
  225. /**
  226. * Create a directory.
  227. *
  228. * @param string $directory
  229. * @param resource $connection
  230. *
  231. * @return bool
  232. */
  233. protected function createActualDirectory($directory, $connection)
  234. {
  235. // List the current directory
  236. $listing = ftp_nlist($connection, '.');
  237. foreach ($listing as $key => $item) {
  238. if (preg_match('~^\./.*~', $item)) {
  239. $listing[$key] = substr($item, 2);
  240. }
  241. }
  242. if (in_array($directory, $listing)) {
  243. return true;
  244. }
  245. return (boolean) ftp_mkdir($connection, $directory);
  246. }
  247. /**
  248. * {@inheritdoc}
  249. */
  250. public function getMetadata($path)
  251. {
  252. $connection = $this->getConnection();
  253. if ($path === '') {
  254. return ['type' => 'dir', 'path' => ''];
  255. }
  256. if (@ftp_chdir($connection, $path) === true) {
  257. $this->setConnectionRoot();
  258. return ['type' => 'dir', 'path' => $path];
  259. }
  260. $listing = ftp_rawlist($connection, $path);
  261. if (empty($listing)) {
  262. return false;
  263. }
  264. $metadata = $this->normalizeObject($listing[0], '');
  265. return $metadata;
  266. }
  267. /**
  268. * {@inheritdoc}
  269. */
  270. public function getMimetype($path)
  271. {
  272. if (! $metadata = $this->read($path)) {
  273. return false;
  274. }
  275. $metadata['mimetype'] = Util::guessMimeType($path, $metadata['contents']);
  276. return $metadata;
  277. }
  278. /**
  279. * {@inheritdoc}
  280. */
  281. public function read($path)
  282. {
  283. if (! $object = $this->readStream($path)) {
  284. return false;
  285. }
  286. $object['contents'] = stream_get_contents($object['stream']);
  287. fclose($object['stream']);
  288. unset($object['stream']);
  289. return $object;
  290. }
  291. /**
  292. * {@inheritdoc}
  293. */
  294. public function readStream($path)
  295. {
  296. $stream = fopen('php://temp', 'w+');
  297. $result = ftp_fget($this->getConnection(), $stream, $path, $this->transferMode);
  298. rewind($stream);
  299. if (! $result) {
  300. fclose($stream);
  301. return false;
  302. }
  303. return compact('stream');
  304. }
  305. /**
  306. * {@inheritdoc}
  307. */
  308. public function setVisibility($path, $visibility)
  309. {
  310. $mode = $visibility === AdapterInterface::VISIBILITY_PUBLIC ? $this->getPermPublic() : $this->getPermPrivate();
  311. if (! ftp_chmod($this->getConnection(), $mode, $path)) {
  312. return false;
  313. }
  314. return compact('visibility');
  315. }
  316. /**
  317. * {@inheritdoc}
  318. *
  319. * @param string $directory
  320. */
  321. protected function listDirectoryContents($directory, $recursive = true)
  322. {
  323. $listing = ftp_rawlist($this->getConnection(), '-lna '.$directory, $recursive);
  324. return $listing ? $this->normalizeListing($listing, $directory) : [];
  325. }
  326. }