destinations.file.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * @file
  4. * A destination type for saving locally to the server.
  5. */
  6. /**
  7. * A destination type for saving locally to the server.
  8. *
  9. * @ingroup backup_migrate_destinations
  10. */
  11. class backup_migrate_destination_files extends backup_migrate_destination {
  12. public $supported_ops = array('scheduled backup', 'manual backup', 'local backup', 'restore', 'list files', 'configure', 'delete');
  13. /**
  14. *
  15. */
  16. public function type_name() {
  17. return t("Server Directory");
  18. }
  19. /**
  20. * Get the file location.
  21. */
  22. public function get_realpath() {
  23. if ($realpath = drupal_realpath($this->get_location())) {
  24. return $realpath;
  25. }
  26. return $this->get_location();
  27. }
  28. /**
  29. * File save destination callback.
  30. */
  31. public function _save_file($file, $settings) {
  32. if ($this->confirm_destination() && $dir = $this->get_location()) {
  33. $filepath = rtrim($dir, "/") . "/" . $file->filename();
  34. // Allow files to be overwritten by the filesystem.
  35. $replace_method = $settings->append_timestamp == 2 ? FILE_EXISTS_REPLACE : FILE_EXISTS_RENAME;
  36. // Copy the file if there are multiple destinations.
  37. if (count($settings->get_destinations()) > 1) {
  38. file_unmanaged_copy($file->filepath(), $filepath, $replace_method);
  39. }
  40. // Otherwise we can move it and save a delete.
  41. else {
  42. file_unmanaged_move($file->filepath(), $filepath, $replace_method);
  43. }
  44. // chmod, chown and chgrp the file if needed.
  45. if ($chmod = $this->settings('chmod')) {
  46. if (!@drupal_chmod($filepath, octdec($chmod))) {
  47. _backup_migrate_message('Unable to set the file mode for: @file', array('@file' => $filepath), 'error');
  48. }
  49. }
  50. if ($chgrp = $this->settings('chgrp')) {
  51. if (!@chgrp($filepath, $chgrp)) {
  52. _backup_migrate_message('Unable to set the file group for: @file', array('@file' => $filepath), 'error');
  53. }
  54. }
  55. return $file;
  56. }
  57. }
  58. /**
  59. * Determine if we can read the given file.
  60. */
  61. public function can_read_file($file_id) {
  62. return $this->op('restore') && is_readable($this->get_filepath($file_id));
  63. }
  64. /**
  65. * File load destination callback.
  66. */
  67. public function load_file($file_id) {
  68. $filepath = $this->get_filepath($file_id);
  69. if (file_exists($filepath)) {
  70. require_once dirname(__FILE__) . '/files.inc';
  71. return new backup_file(array('filepath' => $filepath));
  72. }
  73. }
  74. /**
  75. * Get the file object for the given file.
  76. */
  77. public function get_file($file_id) {
  78. $files = $this->list_files();
  79. if (isset($files[$file_id])) {
  80. isset($files[$file_id]);
  81. }
  82. return NULL;
  83. }
  84. /**
  85. * File list destination callback.
  86. */
  87. public function _list_files() {
  88. $files = array();
  89. if ($dir = $this->get_realpath()) {
  90. if ($handle = @opendir($dir)) {
  91. require_once dirname(__FILE__) . '/files.inc';
  92. while (FALSE !== ($file = readdir($handle))) {
  93. if (substr($file, 0, 1) !== '.') {
  94. $filepath = $dir . "/" . $file;
  95. $files[$file] = new backup_file(array('filepath' => $filepath));
  96. }
  97. }
  98. }
  99. }
  100. return $files;
  101. }
  102. /**
  103. * File delete destination callback.
  104. */
  105. public function _delete_file($file_id) {
  106. $filepath = $this->get_filepath($file_id);
  107. file_unmanaged_delete($filepath);
  108. }
  109. /**
  110. * Get the filepath from the given file id.
  111. */
  112. public function get_filepath($file_id) {
  113. if ($dir = $this->get_realpath()) {
  114. $filepath = rtrim($dir, '/') . '/' . $file_id;
  115. return $filepath;
  116. }
  117. return FALSE;
  118. }
  119. /**
  120. * Get the form for the settings for the files destination.
  121. */
  122. public function edit_form() {
  123. $form = parent::edit_form();
  124. $form['location'] = array(
  125. "#type" => "textfield",
  126. "#title" => t("Directory path"),
  127. "#default_value" => $this->get_location(),
  128. "#required" => TRUE,
  129. "#description" => t('Enter the path to the directory to save the backups to. Use a relative path to pick a path relative to your Drupal root directory. The web server must be able to write to this path.'),
  130. );
  131. $form['settings'] = array(
  132. '#type' => 'fieldset',
  133. '#title' => t('Advanced Settings'),
  134. '#tree' => TRUE,
  135. '#collapsible' => TRUE,
  136. '#collapsed' => TRUE,
  137. );
  138. if (function_exists('chmod')) {
  139. $form['settings']['chmod'] = array(
  140. '#type' => 'textfield',
  141. '#title' => t('Change file mode (chmod)'),
  142. '#size' => 5,
  143. '#default_value' => $this->settings('chmod'),
  144. '#description' => t('If you enter a value here, backup files will be chmoded with the mode you specify. Specify the mode in octal form (e.g. 644 or 0644) or leave blank to disable this feature.'),
  145. );
  146. }
  147. if (function_exists('chgrp')) {
  148. $form['settings']['chgrp'] = array(
  149. '#type' => 'textfield',
  150. '#title' => t('Change file group (chgrp)'),
  151. '#size' => 5,
  152. '#default_value' => $this->settings('chgrp'),
  153. '#description' => t('If you enter a value here, backup files will be chgrped to the group you specify. Leave blank to disable this feature.'),
  154. );
  155. }
  156. return $form;
  157. }
  158. /**
  159. * Validate the form for the settings for the files destination.
  160. */
  161. public function edit_form_validate($form, &$form_state) {
  162. $values = $form_state['values'];
  163. if (isset($values['settings']['chmod']) && !empty($values['settings']['chmod']) && !preg_match('/0?[0-7]{3}/', $values['settings']['chmod'])) {
  164. form_set_error('chmod', t('You must enter a valid chmod octal value (e.g. 644 or 0644) in the change mode field, or leave it blank.'));
  165. }
  166. parent::edit_form_validate($form, $form_state);
  167. }
  168. /**
  169. * Submit the form for the settings for the files destination.
  170. */
  171. public function edit_form_submit($form, &$form_state) {
  172. // Add a 0 to the start of a 3 digit file mode to make it proper PHP encoded octal.
  173. if (strlen($form_state['values']['settings']['chmod']) == 3) {
  174. $form_state['values']['settings']['chmod'] = '0' . $form_state['values']['settings']['chmod'];
  175. }
  176. parent::edit_form_submit($form, $form_state);
  177. }
  178. /**
  179. * Check that a destination is valid.
  180. */
  181. public function confirm_destination() {
  182. if ($dir = $this->get_location()) {
  183. return $this->check_dir($dir);
  184. }
  185. return FALSE;
  186. }
  187. /**
  188. * Prepare the destination directory for the backups.
  189. */
  190. public function check_dir($directory) {
  191. if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
  192. // Unable to create destination directory.
  193. _backup_migrate_message("Unable to create or write to the save directory '%directory'. Please check the file permissions of that directory and try again.", array('%directory' => $directory), "error");
  194. return FALSE;
  195. }
  196. // If the destination directory is within the webroot, then secure it as best we can.
  197. if ($this->dir_in_webroot($directory)) {
  198. $directory = $this->check_web_dir($directory);
  199. }
  200. return $directory;
  201. }
  202. /**
  203. * Check that a web accessible directory has been properly secured, othewise attempt to secure it.
  204. */
  205. public function check_web_dir($directory) {
  206. // Check if the file has already been tested.
  207. if (is_file($directory . '/tested.txt')) {
  208. return $directory;
  209. }
  210. else {
  211. file_create_htaccess($directory, TRUE);
  212. // Check the user agent to make sure we're not responding to a request from drupal itself.
  213. // That should prevent infinite loops which could be caused by poormanscron in some circumstances.
  214. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Drupal') !== FALSE) {
  215. return FALSE;
  216. }
  217. // Check to see if the destination is publicly accessible.
  218. $test_contents = "this file should not be publicly accessible";
  219. // Create the the text.txt file if it's not already there.
  220. if (!is_file($directory . '/test.txt') || file_get_contents($directory . '/test.txt') != $test_contents) {
  221. if ($fp = fopen($directory . '/test.txt', 'w')) {
  222. @fputs($fp, $test_contents);
  223. fclose($fp);
  224. }
  225. else {
  226. $message = t("Security notice: Backup and Migrate was unable to write a test text file to the destination directory %directory, and is therefore unable to check the security of the backup destination. Backups to the server will be disabled until the destination becomes writable and secure.", array('%directory' => $directory));
  227. drupal_set_message($message, "error");
  228. return FALSE;
  229. }
  230. }
  231. // Attempt to read the test file via http. This may fail for other
  232. // reasons, so it's not a bullet-proof check.
  233. if ($this->test_file_readable_remotely($directory . '/test.txt', $test_contents)) {
  234. $message = t("Security notice: Backup and Migrate will not save backup files to the server because the destination directory is publicly accessible. If you want to save files to the server, please secure the '%directory' directory", array('%directory' => $directory));
  235. drupal_set_message($message, "error");
  236. unlink($directory . '/test.txt');
  237. return FALSE;
  238. }
  239. // Directory tested OK, so we mark it as tested.
  240. if ($fp = fopen($directory . '/tested.txt', 'w')) {
  241. $contents = t('The presence of this file indicates that this directory has been tested as safe to use as a destination for Backup and Migrate. If you change the permissions of this directory or change your web server settings, please delete this file so that the directory can be checked again.');
  242. @fputs($fp, $contents);
  243. fclose($fp);
  244. }
  245. return $directory;
  246. }
  247. }
  248. /**
  249. * Check if the given directory is within the webroot and is therefore web accessible.
  250. */
  251. public function dir_in_webroot($directory) {
  252. $real_dir = drupal_realpath($directory);
  253. $real_root = drupal_realpath(DRUPAL_ROOT);
  254. if ($real_dir == $real_root || strpos($real_dir, $real_root . '/') === 0) {
  255. return TRUE;
  256. }
  257. return FALSE;
  258. }
  259. /**
  260. * Check if a file can be read remotely via http.
  261. */
  262. public function test_file_readable_remotely($directory, $contents) {
  263. $real_dir = drupal_realpath($directory);
  264. $real_root = drupal_realpath(DRUPAL_ROOT);
  265. if ($real_dir && $real_root) {
  266. // Get the root relative path.
  267. $path = substr($real_dir, strlen($real_root));
  268. $url = $GLOBALS['base_url'] . str_replace('\\', '/', $path);
  269. $result = drupal_http_request($url);
  270. if (!empty($result->data) && strpos($result->data, $contents) !== FALSE) {
  271. return TRUE;
  272. }
  273. }
  274. return FALSE;
  275. }
  276. }
  277. /**
  278. * The manual files directory.
  279. */
  280. class backup_migrate_destination_files_manual extends backup_migrate_destination_files {
  281. public $supported_ops = array('manual backup', 'restore', 'list files', 'configure', 'delete');
  282. /**
  283. *
  284. */
  285. public function __construct($params = array()) {
  286. $dir = 'private://backup_migrate/manual';
  287. parent::__construct($params + array('location' => $dir, 'name' => t('Manual Backups Directory')));
  288. }
  289. }
  290. /**
  291. * The scheduled files directory.
  292. */
  293. class backup_migrate_destination_files_scheduled extends backup_migrate_destination_files {
  294. public $supported_ops = array('scheduled backup', 'restore', 'list files', 'configure', 'delete');
  295. /**
  296. *
  297. */
  298. public function __construct($params = array()) {
  299. $dir = 'private://backup_migrate/scheduled';
  300. parent::__construct($params + array('location' => $dir, 'name' => t('Scheduled Backups Directory')));
  301. }
  302. }