PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

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