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

/web/core/lib/Drupal/Core/FileTransfer/FileTransfer.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 421 lines | 245 code | 21 blank | 155 comment | 10 complexity | e9a4acf688f1c85ff466fa7e4668ba22 MD5 | raw file
  1. <?php
  2. namespace Drupal\Core\FileTransfer;
  3. /**
  4. * Defines the base FileTransfer class.
  5. *
  6. * Classes extending this class perform file operations on directories not
  7. * writable by the webserver. To achieve this, the class should connect back
  8. * to the server using some backend (for example FTP or SSH). To keep security,
  9. * the password should always be asked from the user and never stored. For
  10. * safety, all methods operate only inside a "jail", by default the Drupal root.
  11. */
  12. abstract class FileTransfer {
  13. /**
  14. * The username for this file transfer.
  15. *
  16. * @var string
  17. */
  18. protected $username;
  19. /**
  20. * The password for this file transfer.
  21. *
  22. * @var string
  23. */
  24. protected $password;
  25. /**
  26. * The hostname for this file transfer.
  27. *
  28. * @var string
  29. */
  30. protected $hostname = 'localhost';
  31. /**
  32. * The port for this file transfer.
  33. *
  34. * @var int
  35. */
  36. protected $port;
  37. /**
  38. * Constructs a Drupal\Core\FileTransfer\FileTransfer object.
  39. *
  40. * @param $jail
  41. * The full path where all file operations performed by this object will
  42. * be restricted to. This prevents the FileTransfer classes from being
  43. * able to touch other parts of the filesystem.
  44. */
  45. public function __construct($jail) {
  46. $this->jail = $jail;
  47. }
  48. /**
  49. * Defines a factory method for this class.
  50. *
  51. * Classes that extend this class must override the factory() static method.
  52. * They should return a new instance of the appropriate FileTransfer subclass.
  53. *
  54. * @param string $jail
  55. * The full path where all file operations performed by this object will
  56. * be restricted to. This prevents the FileTransfer classes from being
  57. * able to touch other parts of the filesystem.
  58. * @param array $settings
  59. * An array of connection settings for the FileTransfer subclass. If the
  60. * getSettingsForm() method uses any nested settings, the same structure
  61. * will be assumed here.
  62. *
  63. * @return object
  64. * New instance of the appropriate FileTransfer subclass.
  65. *
  66. * @throws \Drupal\Core\FileTransfer\FileTransferException
  67. */
  68. public static function factory($jail, $settings) {
  69. throw new FileTransferException('FileTransfer::factory() static method not overridden by FileTransfer subclass.');
  70. }
  71. /**
  72. * Implements the magic __get() method.
  73. *
  74. * If the connection isn't set to anything, this will call the connect()
  75. * method and return the result; afterwards, the connection will be returned
  76. * directly without using this method.
  77. *
  78. * @param string $name
  79. * The name of the variable to return.
  80. *
  81. * @return string|bool
  82. * The variable specified in $name.
  83. */
  84. public function __get($name) {
  85. if ($name == 'connection') {
  86. $this->connect();
  87. return $this->connection;
  88. }
  89. if ($name == 'chroot') {
  90. $this->setChroot();
  91. return $this->chroot;
  92. }
  93. }
  94. /**
  95. * Connects to the server.
  96. */
  97. abstract public function connect();
  98. /**
  99. * Copies a directory.
  100. *
  101. * @param string $source
  102. * The source path.
  103. * @param string $destination
  104. * The destination path.
  105. */
  106. final public function copyDirectory($source, $destination) {
  107. $source = $this->sanitizePath($source);
  108. $destination = $this->fixRemotePath($destination);
  109. $this->checkPath($destination);
  110. $this->copyDirectoryJailed($source, $destination);
  111. }
  112. /**
  113. * Changes the permissions of the specified $path (file or directory).
  114. *
  115. * @param string $path
  116. * The file / directory to change the permissions of.
  117. * @param int $mode
  118. * The new file permission mode to be passed to chmod().
  119. * @param bool $recursive
  120. * Pass TRUE to recursively chmod the entire directory specified in $path.
  121. *
  122. * @throws \Drupal\Core\FileTransfer\FileTransferException
  123. *
  124. * @see http://php.net/chmod
  125. */
  126. final public function chmod($path, $mode, $recursive = FALSE) {
  127. if (!($this instanceof ChmodInterface)) {
  128. throw new FileTransferException('Unable to change file permissions');
  129. }
  130. $path = $this->sanitizePath($path);
  131. $path = $this->fixRemotePath($path);
  132. $this->checkPath($path);
  133. $this->chmodJailed($path, $mode, $recursive);
  134. }
  135. /**
  136. * Creates a directory.
  137. *
  138. * @param string $directory
  139. * The directory to be created.
  140. */
  141. final public function createDirectory($directory) {
  142. $directory = $this->fixRemotePath($directory);
  143. $this->checkPath($directory);
  144. $this->createDirectoryJailed($directory);
  145. }
  146. /**
  147. * Removes a directory.
  148. *
  149. * @param string $directory
  150. * The directory to be removed.
  151. */
  152. final public function removeDirectory($directory) {
  153. $directory = $this->fixRemotePath($directory);
  154. $this->checkPath($directory);
  155. $this->removeDirectoryJailed($directory);
  156. }
  157. /**
  158. * Copies a file.
  159. *
  160. * @param string $source
  161. * The source file.
  162. * @param string $destination
  163. * The destination file.
  164. */
  165. final public function copyFile($source, $destination) {
  166. $source = $this->sanitizePath($source);
  167. $destination = $this->fixRemotePath($destination);
  168. $this->checkPath($destination);
  169. $this->copyFileJailed($source, $destination);
  170. }
  171. /**
  172. * Removes a file.
  173. *
  174. * @param string $destination
  175. * The destination file to be removed.
  176. */
  177. final public function removeFile($destination) {
  178. $destination = $this->fixRemotePath($destination);
  179. $this->checkPath($destination);
  180. $this->removeFileJailed($destination);
  181. }
  182. /**
  183. * Checks that the path is inside the jail and throws an exception if not.
  184. *
  185. * @param string $path
  186. * A path to check against the jail.
  187. *
  188. * @throws \Drupal\Core\FileTransfer\FileTransferException
  189. */
  190. final protected function checkPath($path) {
  191. $full_jail = $this->chroot . $this->jail;
  192. $full_path = \Drupal::service('file_system')
  193. ->realpath(substr($this->chroot . $path, 0, strlen($full_jail)));
  194. $full_path = $this->fixRemotePath($full_path, FALSE);
  195. if ($full_jail !== $full_path) {
  196. throw new FileTransferException('@directory is outside of the @jail', 0, ['@directory' => $path, '@jail' => $this->jail]);
  197. }
  198. }
  199. /**
  200. * Returns a modified path suitable for passing to the server.
  201. *
  202. * If a path is a windows path, makes it POSIX compliant by removing the drive
  203. * letter. If $this->chroot has a value and $strip_chroot is TRUE, it is
  204. * stripped from the path to allow for chroot'd filetransfer systems.
  205. *
  206. * @param string $path
  207. * The path to modify.
  208. * @param bool $strip_chroot
  209. * Whether to remove the path in $this->chroot.
  210. *
  211. * @return string
  212. * The modified path.
  213. */
  214. final protected function fixRemotePath($path, $strip_chroot = TRUE) {
  215. $path = $this->sanitizePath($path);
  216. // Strip out windows driveletter if its there.
  217. $path = preg_replace('|^([a-z]{1}):|i', '', $path);
  218. if ($strip_chroot) {
  219. if ($this->chroot && strpos($path, $this->chroot) === 0) {
  220. $path = ($path == $this->chroot) ? '' : substr($path, strlen($this->chroot));
  221. }
  222. }
  223. return $path;
  224. }
  225. /**
  226. * Changes backslashes to slashes, also removes a trailing slash.
  227. *
  228. * @param string $path
  229. * The path to modify.
  230. *
  231. * @return string
  232. * The modified path.
  233. */
  234. public function sanitizePath($path) {
  235. // Windows path sanitization.
  236. $path = str_replace('\\', '/', $path);
  237. if (substr($path, -1) == '/') {
  238. $path = substr($path, 0, -1);
  239. }
  240. return $path;
  241. }
  242. /**
  243. * Copies a directory.
  244. *
  245. * We need a separate method to make sure the $destination is in the jail.
  246. *
  247. * @param string $source
  248. * The source path.
  249. * @param string $destination
  250. * The destination path.
  251. */
  252. protected function copyDirectoryJailed($source, $destination) {
  253. if ($this->isDirectory($destination)) {
  254. $destination = $destination . '/' . \Drupal::service('file_system')->basename($source);
  255. }
  256. $this->createDirectory($destination);
  257. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) {
  258. $relative_path = substr($filename, strlen($source));
  259. if ($file->isDir()) {
  260. $this->createDirectory($destination . $relative_path);
  261. }
  262. else {
  263. $this->copyFile($file->getPathName(), $destination . $relative_path);
  264. }
  265. }
  266. }
  267. /**
  268. * Creates a directory.
  269. *
  270. * @param string $directory
  271. * The directory to be created.
  272. */
  273. abstract protected function createDirectoryJailed($directory);
  274. /**
  275. * Removes a directory.
  276. *
  277. * @param string $directory
  278. * The directory to be removed.
  279. */
  280. abstract protected function removeDirectoryJailed($directory);
  281. /**
  282. * Copies a file.
  283. *
  284. * @param string $source
  285. * The source file.
  286. * @param string $destination
  287. * The destination file.
  288. */
  289. abstract protected function copyFileJailed($source, $destination);
  290. /**
  291. * Removes a file.
  292. *
  293. * @param string $destination
  294. * The destination file to be removed.
  295. */
  296. abstract protected function removeFileJailed($destination);
  297. /**
  298. * Checks if a particular path is a directory.
  299. *
  300. * @param string $path
  301. * The path to check
  302. *
  303. * @return bool
  304. * TRUE if the specified path is a directory, FALSE otherwise.
  305. */
  306. abstract public function isDirectory($path);
  307. /**
  308. * Checks if a particular path is a file (not a directory).
  309. *
  310. * @param string $path
  311. * The path to check.
  312. *
  313. * @return bool
  314. * TRUE if the specified path is a file, FALSE otherwise.
  315. */
  316. abstract public function isFile($path);
  317. /**
  318. * Returns the chroot property for this connection.
  319. *
  320. * It does this by moving up the tree until it finds itself
  321. *
  322. * @return string|bool
  323. * If successful, the chroot path for this connection, otherwise FALSE.
  324. */
  325. public function findChroot() {
  326. // If the file exists as is, there is no chroot.
  327. $path = __FILE__;
  328. $path = $this->fixRemotePath($path, FALSE);
  329. if ($this->isFile($path)) {
  330. return FALSE;
  331. }
  332. $path = __DIR__;
  333. $path = $this->fixRemotePath($path, FALSE);
  334. $parts = explode('/', $path);
  335. $chroot = '';
  336. while (count($parts)) {
  337. $check = implode('/', $parts);
  338. if ($this->isFile($check . '/' . \Drupal::service('file_system')->basename(__FILE__))) {
  339. // Remove the trailing slash.
  340. return substr($chroot, 0, -1);
  341. }
  342. $chroot .= array_shift($parts) . '/';
  343. }
  344. return FALSE;
  345. }
  346. /**
  347. * Sets the chroot and changes the jail to match the correct path scheme.
  348. */
  349. public function setChroot() {
  350. $this->chroot = $this->findChroot();
  351. $this->jail = $this->fixRemotePath($this->jail);
  352. }
  353. /**
  354. * Returns a form to collect connection settings credentials.
  355. *
  356. * Implementing classes can either extend this form with fields collecting the
  357. * specific information they need, or override it entirely.
  358. *
  359. * @return array
  360. * An array that contains a Form API definition.
  361. */
  362. public function getSettingsForm() {
  363. $form['username'] = [
  364. '#type' => 'textfield',
  365. '#title' => t('Username'),
  366. ];
  367. $form['password'] = [
  368. '#type' => 'password',
  369. '#title' => t('Password'),
  370. '#description' => t('Your password is not saved in the database and is only used to establish a connection.'),
  371. ];
  372. $form['advanced'] = [
  373. '#type' => 'details',
  374. '#title' => t('Advanced settings'),
  375. ];
  376. $form['advanced']['hostname'] = [
  377. '#type' => 'textfield',
  378. '#title' => t('Host'),
  379. '#default_value' => 'localhost',
  380. '#description' => t('The connection will be created between your web server and the machine hosting the web server files. In the vast majority of cases, this will be the same machine, and "localhost" is correct.'),
  381. ];
  382. $form['advanced']['port'] = [
  383. '#type' => 'textfield',
  384. '#title' => t('Port'),
  385. '#default_value' => NULL,
  386. ];
  387. return $form;
  388. }
  389. }