updater.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. /**
  3. * @file
  4. * Classes used for updating various files in the Drupal webroot. These
  5. * classes use a FileTransfer object to actually perform the operations.
  6. * Normally, the FileTransfer is provided when the site owner is redirected to
  7. * authorize.php as part of a multistep process.
  8. */
  9. /**
  10. * Interface for a class which can update a Drupal project.
  11. *
  12. * An Updater currently serves the following purposes:
  13. * - It can take a given directory, and determine if it can operate on it.
  14. * - It can move the contents of that directory into the appropriate place
  15. * on the system using FileTransfer classes.
  16. * - It can return a list of "next steps" after an update or install.
  17. * - In the future, it will most likely perform some of those steps as well.
  18. */
  19. interface DrupalUpdaterInterface {
  20. /**
  21. * Checks if the project is installed.
  22. *
  23. * @return bool
  24. */
  25. public function isInstalled();
  26. /**
  27. * Returns the system name of the project.
  28. *
  29. * @param string $directory
  30. * A directory containing a project.
  31. */
  32. public static function getProjectName($directory);
  33. /**
  34. * @return string
  35. * An absolute path to the default install location.
  36. */
  37. public function getInstallDirectory();
  38. /**
  39. * Determine if the Updater can handle the project provided in $directory.
  40. *
  41. * @todo: Provide something more rational here, like a project spec file.
  42. *
  43. * @param string $directory
  44. *
  45. * @return bool
  46. * TRUE if the project is installed, FALSE if not.
  47. */
  48. public static function canUpdateDirectory($directory);
  49. /**
  50. * Actions to run after an install has occurred.
  51. */
  52. public function postInstall();
  53. /**
  54. * Actions to run after an update has occurred.
  55. */
  56. public function postUpdate();
  57. }
  58. /**
  59. * Base class for Updaters used in Drupal.
  60. */
  61. class Updater {
  62. /**
  63. * @var string $source Directory to install from.
  64. */
  65. public $source;
  66. /**
  67. * @var string $name The name of the project.
  68. */
  69. protected $name;
  70. /**
  71. * @var string $title The title of the project.
  72. */
  73. protected $title;
  74. public function __construct($source) {
  75. $this->source = $source;
  76. $this->name = self::getProjectName($source);
  77. $this->title = self::getProjectTitle($source);
  78. }
  79. /**
  80. * Return an Updater of the appropriate type depending on the source.
  81. *
  82. * If a directory is provided which contains a module, will return a
  83. * ModuleUpdater.
  84. *
  85. * @param string $source
  86. * Directory of a Drupal project.
  87. *
  88. * @return Updater
  89. */
  90. public static function factory($source) {
  91. if (is_dir($source)) {
  92. $updater = self::getUpdaterFromDirectory($source);
  93. }
  94. else {
  95. throw new UpdaterException(t('Unable to determine the type of the source directory.'));
  96. }
  97. return new $updater($source);
  98. }
  99. /**
  100. * Determine which Updater class can operate on the given directory.
  101. *
  102. * @param string $directory
  103. * Extracted Drupal project.
  104. *
  105. * @return string
  106. * The class name which can work with this project type.
  107. */
  108. public static function getUpdaterFromDirectory($directory) {
  109. // Gets a list of possible implementing classes.
  110. $updaters = drupal_get_updaters();
  111. foreach ($updaters as $updater) {
  112. $class = $updater['class'];
  113. if (call_user_func(array($class, 'canUpdateDirectory'), $directory)) {
  114. return $class;
  115. }
  116. }
  117. throw new UpdaterException(t('Cannot determine the type of project.'));
  118. }
  119. /**
  120. * Figure out what the most important (or only) info file is in a directory.
  121. *
  122. * Since there is no enforcement of which info file is the project's "main"
  123. * info file, this will get one with the same name as the directory, or the
  124. * first one it finds. Not ideal, but needs a larger solution.
  125. *
  126. * @param string $directory
  127. * Directory to search in.
  128. *
  129. * @return string
  130. * Path to the info file.
  131. */
  132. public static function findInfoFile($directory) {
  133. $info_files = file_scan_directory($directory, '/.*\.info$/');
  134. if (!$info_files) {
  135. return FALSE;
  136. }
  137. foreach ($info_files as $info_file) {
  138. if (drupal_substr($info_file->filename, 0, -5) == drupal_basename($directory)) {
  139. // Info file Has the same name as the directory, return it.
  140. return $info_file->uri;
  141. }
  142. }
  143. // Otherwise, return the first one.
  144. $info_file = array_shift($info_files);
  145. return $info_file->uri;
  146. }
  147. /**
  148. * Get the name of the project directory (basename).
  149. *
  150. * @todo: It would be nice, if projects contained an info file which could
  151. * provide their canonical name.
  152. *
  153. * @param string $directory
  154. *
  155. * @return string
  156. * The name of the project.
  157. */
  158. public static function getProjectName($directory) {
  159. return drupal_basename($directory);
  160. }
  161. /**
  162. * Return the project name from a Drupal info file.
  163. *
  164. * @param string $directory
  165. * Directory to search for the info file.
  166. *
  167. * @return string
  168. * The title of the project.
  169. */
  170. public static function getProjectTitle($directory) {
  171. $info_file = self::findInfoFile($directory);
  172. $info = drupal_parse_info_file($info_file);
  173. if (empty($info)) {
  174. throw new UpdaterException(t('Unable to parse info file: %info_file.', array('%info_file' => $info_file)));
  175. }
  176. if (empty($info['name'])) {
  177. throw new UpdaterException(t("The info file (%info_file) does not define a 'name' attribute.", array('%info_file' => $info_file)));
  178. }
  179. return $info['name'];
  180. }
  181. /**
  182. * Store the default parameters for the Updater.
  183. *
  184. * @param array $overrides
  185. * An array of overrides for the default parameters.
  186. *
  187. * @return array
  188. * An array of configuration parameters for an update or install operation.
  189. */
  190. protected function getInstallArgs($overrides = array()) {
  191. $args = array(
  192. 'make_backup' => FALSE,
  193. 'install_dir' => $this->getInstallDirectory(),
  194. 'backup_dir' => $this->getBackupDir(),
  195. );
  196. return array_merge($args, $overrides);
  197. }
  198. /**
  199. * Updates a Drupal project, returns a list of next actions.
  200. *
  201. * @param FileTransfer $filetransfer
  202. * Object that is a child of FileTransfer. Used for moving files
  203. * to the server.
  204. * @param array $overrides
  205. * An array of settings to override defaults; see self::getInstallArgs().
  206. *
  207. * @return array
  208. * An array of links which the user may need to complete the update
  209. */
  210. public function update(&$filetransfer, $overrides = array()) {
  211. try {
  212. // Establish arguments with possible overrides.
  213. $args = $this->getInstallArgs($overrides);
  214. // Take a Backup.
  215. if ($args['make_backup']) {
  216. $this->makeBackup($args['install_dir'], $args['backup_dir']);
  217. }
  218. if (!$this->name) {
  219. // This is bad, don't want to delete the install directory.
  220. throw new UpdaterException(t('Fatal error in update, cowardly refusing to wipe out the install directory.'));
  221. }
  222. // Make sure the installation parent directory exists and is writable.
  223. $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
  224. if (is_dir($args['install_dir'] . '/' . $this->name)) {
  225. // Remove the existing installed file.
  226. $filetransfer->removeDirectory($args['install_dir'] . '/' . $this->name);
  227. }
  228. // Copy the directory in place.
  229. $filetransfer->copyDirectory($this->source, $args['install_dir']);
  230. // Make sure what we just installed is readable by the web server.
  231. $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
  232. // Run the updates.
  233. // @TODO: decide if we want to implement this.
  234. $this->postUpdate();
  235. // For now, just return a list of links of things to do.
  236. return $this->postUpdateTasks();
  237. }
  238. catch (FileTransferException $e) {
  239. throw new UpdaterFileTransferException(t('File Transfer failed, reason: !reason', array('!reason' => strtr($e->getMessage(), $e->arguments))));
  240. }
  241. }
  242. /**
  243. * Installs a Drupal project, returns a list of next actions.
  244. *
  245. * @param FileTransfer $filetransfer
  246. * Object that is a child of FileTransfer.
  247. * @param array $overrides
  248. * An array of settings to override defaults; see self::getInstallArgs().
  249. *
  250. * @return array
  251. * An array of links which the user may need to complete the install.
  252. */
  253. public function install(&$filetransfer, $overrides = array()) {
  254. try {
  255. // Establish arguments with possible overrides.
  256. $args = $this->getInstallArgs($overrides);
  257. // Make sure the installation parent directory exists and is writable.
  258. $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
  259. // Copy the directory in place.
  260. $filetransfer->copyDirectory($this->source, $args['install_dir']);
  261. // Make sure what we just installed is readable by the web server.
  262. $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
  263. // Potentially enable something?
  264. // @TODO: decide if we want to implement this.
  265. $this->postInstall();
  266. // For now, just return a list of links of things to do.
  267. return $this->postInstallTasks();
  268. }
  269. catch (FileTransferException $e) {
  270. throw new UpdaterFileTransferException(t('File Transfer failed, reason: !reason', array('!reason' => strtr($e->getMessage(), $e->arguments))));
  271. }
  272. }
  273. /**
  274. * Make sure the installation parent directory exists and is writable.
  275. *
  276. * @param FileTransfer $filetransfer
  277. * Object which is a child of FileTransfer.
  278. * @param string $directory
  279. * The installation directory to prepare.
  280. */
  281. public function prepareInstallDirectory(&$filetransfer, $directory) {
  282. // Make the parent dir writable if need be and create the dir.
  283. if (!is_dir($directory)) {
  284. $parent_dir = dirname($directory);
  285. if (!is_writable($parent_dir)) {
  286. @chmod($parent_dir, 0755);
  287. // It is expected that this will fail if the directory is owned by the
  288. // FTP user. If the FTP user == web server, it will succeed.
  289. try {
  290. $filetransfer->createDirectory($directory);
  291. $this->makeWorldReadable($filetransfer, $directory);
  292. }
  293. catch (FileTransferException $e) {
  294. // Probably still not writable. Try to chmod and do it again.
  295. // @todo: Make a new exception class so we can catch it differently.
  296. try {
  297. $old_perms = substr(sprintf('%o', fileperms($parent_dir)), -4);
  298. $filetransfer->chmod($parent_dir, 0755);
  299. $filetransfer->createDirectory($directory);
  300. $this->makeWorldReadable($filetransfer, $directory);
  301. // Put the permissions back.
  302. $filetransfer->chmod($parent_dir, intval($old_perms, 8));
  303. }
  304. catch (FileTransferException $e) {
  305. $message = t($e->getMessage(), $e->arguments);
  306. $throw_message = t('Unable to create %directory due to the following: %reason', array('%directory' => $directory, '%reason' => $message));
  307. throw new UpdaterException($throw_message);
  308. }
  309. }
  310. // Put the parent directory back.
  311. @chmod($parent_dir, 0555);
  312. }
  313. }
  314. }
  315. /**
  316. * Ensure that a given directory is world readable.
  317. *
  318. * @param FileTransfer $filetransfer
  319. * Object which is a child of FileTransfer.
  320. * @param string $path
  321. * The file path to make world readable.
  322. * @param bool $recursive
  323. * If the chmod should be applied recursively.
  324. */
  325. public function makeWorldReadable(&$filetransfer, $path, $recursive = TRUE) {
  326. if (!is_executable($path)) {
  327. // Set it to read + execute.
  328. $new_perms = substr(sprintf('%o', fileperms($path)), -4, -1) . "5";
  329. $filetransfer->chmod($path, intval($new_perms, 8), $recursive);
  330. }
  331. }
  332. /**
  333. * Perform a backup.
  334. *
  335. * @todo Not implemented.
  336. */
  337. public function makeBackup(&$filetransfer, $from, $to) {
  338. }
  339. /**
  340. * Return the full path to a directory where backups should be written.
  341. */
  342. public function getBackupDir() {
  343. return file_stream_wrapper_get_instance_by_scheme('temporary')->getDirectoryPath();
  344. }
  345. /**
  346. * Perform actions after new code is updated.
  347. */
  348. public function postUpdate() {
  349. }
  350. /**
  351. * Perform actions after installation.
  352. */
  353. public function postInstall() {
  354. }
  355. /**
  356. * Return an array of links to pages that should be visited post operation.
  357. *
  358. * @return array
  359. * Links which provide actions to take after the install is finished.
  360. */
  361. public function postInstallTasks() {
  362. return array();
  363. }
  364. /**
  365. * Return an array of links to pages that should be visited post operation.
  366. *
  367. * @return array
  368. * Links which provide actions to take after the update is finished.
  369. */
  370. public function postUpdateTasks() {
  371. return array();
  372. }
  373. }
  374. /**
  375. * Exception class for the Updater class hierarchy.
  376. *
  377. * This is identical to the base Exception class, we just give it a more
  378. * specific name so that call sites that want to tell the difference can
  379. * specifically catch these exceptions and treat them differently.
  380. */
  381. class UpdaterException extends Exception {
  382. }
  383. /**
  384. * Child class of UpdaterException that indicates a FileTransfer exception.
  385. *
  386. * We have to catch FileTransfer exceptions and wrap those in t(), since
  387. * FileTransfer is so low-level that it doesn't use any Drupal APIs and none
  388. * of the strings are translated.
  389. */
  390. class UpdaterFileTransferException extends UpdaterException {
  391. }