files.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <?php
  2. /**
  3. * @file
  4. * General file handling code for Backup and Migrate.
  5. */
  6. define('BACKUP_MIGRATE_FILENAME_MAXLENGTH', 255);
  7. /**
  8. * Add a file to the temporary files list for deletion when we're done.
  9. */
  10. function backup_migrate_temp_files_add($filepath = NULL) {
  11. static $files = array();
  12. if (!$filepath) {
  13. return $files;
  14. }
  15. else {
  16. $files[] = $filepath;
  17. }
  18. }
  19. /**
  20. * Delete all temporary files.
  21. */
  22. function _backup_migrate_temp_files_delete() {
  23. if (variable_get('backup_migrate_cleanup_temp_files', TRUE)) {
  24. // Delete the temp files created during this run.
  25. foreach (backup_migrate_temp_files_add() as $file) {
  26. if (file_exists($file) && is_writable($file)) {
  27. _backup_migrate_temp_files_delete_file($file);
  28. }
  29. }
  30. // Delete temp files abandoned for 6 or more hours.
  31. $dir = file_directory_temp();
  32. $expire = time() - variable_get('backup_migrate_cleanup_time', 21600);
  33. if (file_exists($dir) && is_dir($dir) && is_readable($dir) && $handle = opendir($dir)) {
  34. while (FALSE !== ($file = @readdir($handle))) {
  35. // Delete 'backup_migrate_' files in the temp directory that are older than the expire time.
  36. // We should only attempt to delete writable files to prevent errors in shared environments.
  37. // This could still cause issues in shared environments with poorly configured file permissions.
  38. if (strpos($file, 'backup_migrate_') === 0 && is_writable("$dir/$file") && @filectime("$dir/$file") < $expire) {
  39. _backup_migrate_temp_files_delete_file("$dir/$file");
  40. }
  41. }
  42. closedir($handle);
  43. }
  44. }
  45. }
  46. /**
  47. * Delete a temporary file or folder
  48. */
  49. function _backup_migrate_temp_files_delete_file($file) {
  50. if (file_exists($file) && (is_writable($file) || is_link($file))) {
  51. if (!is_link($file) && is_dir($file) && is_readable($file) && $handle = opendir($file)) {
  52. $dir = $file;
  53. while (FALSE !== ($file = @readdir($handle))) {
  54. if ($file != '..' && $file != '.') {
  55. _backup_migrate_temp_files_delete_file("$dir/$file");
  56. }
  57. }
  58. rmdir($dir);
  59. }
  60. else {
  61. unlink($file);
  62. }
  63. }
  64. }
  65. /**
  66. * Move files recursively.
  67. */
  68. function _backup_migrate_move_files($from, $to) {
  69. if (is_readable($from)) {
  70. if (is_dir($from) && is_readable($from) && $handle = opendir($from)) {
  71. if (!file_exists($to)) {
  72. mkdir($to);
  73. }
  74. while (FALSE !== ($file = @readdir($handle))) {
  75. if ($file != '..' && $file != '.') {
  76. _backup_migrate_move_files("$from/$file", "$to/$file");
  77. }
  78. }
  79. }
  80. else {
  81. rename($from, $to);
  82. }
  83. }
  84. return FALSE;
  85. }
  86. /**
  87. * Create a temporary directory.
  88. */
  89. function backup_migrate_temp_directory() {
  90. $tmp = realpath(file_directory_temp());
  91. // Check the writability of the temp directory.
  92. if (!is_writable(realpath(file_directory_temp()))) {
  93. _backup_migrate_message('Your temporary directory %tmp is not writable. Backup and migrate needs to be able to create temporary files.', array('%tmp' => $tmp), 'error');
  94. }
  95. // Use a full path so that the files can be deleted during the shutdown function if needed.
  96. $file = $tmp .'/'. uniqid('backup_migrate_');
  97. mkdir($file);
  98. backup_migrate_temp_files_add($file);
  99. return $file;
  100. }
  101. /**
  102. * Return a list of backup filetypes.
  103. */
  104. function _backup_migrate_filetypes() {
  105. backup_migrate_include('filters');
  106. $out = backup_migrate_filters_file_types();
  107. foreach ($out as $key => $info) {
  108. $out[$key]['id'] = empty($info['id']) ? $key : $info['id'];
  109. }
  110. return $out;
  111. }
  112. /**
  113. * Adjust the length of a filename to allow for a string to be appended,
  114. * staying within the maximum filename limit.
  115. */
  116. function _backup_migrate_filename_append_prepare($filename, $append_str) {
  117. $max_name_len = BACKUP_MIGRATE_FILENAME_MAXLENGTH - drupal_strlen($append_str);
  118. if (drupal_strlen($filename) > $max_name_len) {
  119. $filename = drupal_substr($filename, 0, $max_name_len);
  120. }
  121. return $filename;
  122. }
  123. /**
  124. * Construct a filename using token and some cleaning.
  125. */
  126. function _backup_migrate_construct_filename($settings) {
  127. // Get the raw filename from the settings.
  128. $filename = $settings->filename;
  129. // Replace any tokens
  130. if (module_exists('token') && function_exists('token_replace')) {
  131. $filename = token_replace($filename);
  132. }
  133. // Remove illegal characters
  134. $filename = _backup_migrate_clean_filename($filename);
  135. // Generate a timestamp if needed.
  136. $timestamp = '';
  137. if ($settings->append_timestamp && $settings->timestamp_format) {
  138. $timestamp = format_date(time(), 'custom', $settings->timestamp_format);
  139. }
  140. // Trim to length if needed to allow the timestamp to fit into the max filename.
  141. $filename = _backup_migrate_filename_append_prepare($filename, $timestamp);
  142. $filename .= '-' . $timestamp;
  143. $filename = trim($filename, '-');
  144. // If there is no filename, then just call it untitled.
  145. if (drupal_strlen($filename) == 0) {
  146. $filename = 'untitled';
  147. }
  148. return $filename;
  149. }
  150. /**
  151. * Construct a default filename using the site's name.
  152. */
  153. function _backup_migrate_default_filename() {
  154. if (module_exists('token')) {
  155. return '[site:name]';
  156. }
  157. else {
  158. // Cleaning the string isn't strictly necessary but it looks better in the settings field.
  159. return _backup_migrate_clean_filename(variable_get('site_name', "backup_migrate"));
  160. }
  161. }
  162. /**
  163. * Clean up a filename to remove unsafe characters.
  164. */
  165. function _backup_migrate_clean_filename($filename) {
  166. $filename = preg_replace("/[^a-zA-Z0-9\.\-_]/", "", $filename);
  167. return $filename;
  168. }
  169. /**
  170. * An output buffer callback which simply throws away the buffer instead of sending it to the browser.
  171. */
  172. function _backup_migrate_file_dispose_buffer($buffer) {
  173. return "";
  174. }
  175. /**
  176. * A backup file which allows for saving to and reading from the server.
  177. */
  178. class backup_file {
  179. var $file_info = array();
  180. var $type = array();
  181. var $ext = array();
  182. var $path = "";
  183. var $name = "";
  184. var $handle = NULL;
  185. /**
  186. * Construct a file object given a file path, or create a temp file for writing.
  187. */
  188. function backup_file($params = array()) {
  189. if (isset($params['filepath']) && file_exists($params['filepath'])) {
  190. $this->set_filepath($params['filepath']);
  191. }
  192. else {
  193. $this->set_file_info($params);
  194. $this->temporary_file();
  195. }
  196. }
  197. /**
  198. * Get the file_id if the file has been saved to a destination.
  199. */
  200. function file_id() {
  201. // The default file_id is the filename. Destinations can override the file_id if needed.
  202. return isset($this->file_info['file_id']) ? $this->file_info['file_id'] : $this->filename();
  203. }
  204. /**
  205. * Get the current filepath.
  206. */
  207. function filepath() {
  208. return drupal_realpath($this->path);
  209. }
  210. /**
  211. * Get the final filename.
  212. */
  213. function filename($name = NULL) {
  214. if ($name) {
  215. $this->name = $name;
  216. }
  217. $extension_str = '.' . $this->extension();
  218. $this->name = _backup_migrate_filename_append_prepare($this->name, $extension_str);
  219. return $this->name . $extension_str;
  220. }
  221. /**
  222. * Set the current filepath.
  223. */
  224. function set_filepath($path) {
  225. $this->path = $path;
  226. $params = array(
  227. 'filename' => basename($path),
  228. 'file_id' => basename($path)
  229. );
  230. if (file_exists($path)) {
  231. $params['filesize'] = filesize($path);
  232. $params['filetime'] = filectime($path);
  233. }
  234. $this->set_file_info($params);
  235. }
  236. /**
  237. * Get one or all pieces of info for the file.
  238. */
  239. function info($key = NULL) {
  240. if ($key) {
  241. return @$this->file_info[$key];
  242. }
  243. return $this->file_info;
  244. }
  245. /**
  246. * Get one or all pieces of info for the file.
  247. */
  248. function info_set($key, $value) {
  249. $this->file_info[$key] = $value;
  250. }
  251. /**
  252. * Get the file extension.
  253. */
  254. function extension() {
  255. return implode(".", $this->ext);
  256. }
  257. /**
  258. * Get the file type.
  259. */
  260. function type() {
  261. return $this->type;
  262. }
  263. /**
  264. * Get the file mimetype.
  265. */
  266. function mimetype() {
  267. return @$this->type['filemime'] ? $this->type['filemime'] : 'application/octet-stream';
  268. }
  269. /**
  270. * Get the file mimetype.
  271. */
  272. function type_id() {
  273. return @$this->type['id'];
  274. }
  275. function filesize() {
  276. if (empty($this->file_info['filesize'])) {
  277. $this->calculate_filesize();
  278. }
  279. return $this->file_info['filesize'];
  280. }
  281. function calculate_filesize() {
  282. $this->file_info['filesize'] = '';
  283. if (!empty($this->path) && file_exists($this->path)) {
  284. $this->file_info['filesize'] = filesize($this->path);
  285. }
  286. }
  287. /**
  288. * Can this file be used to backup to.
  289. */
  290. function can_backup() {
  291. return @$this->type['backup'];
  292. }
  293. /**
  294. * Can this file be used to restore to.
  295. */
  296. function can_restore() {
  297. return @$this->type['restore'];
  298. }
  299. /**
  300. * Can this file be used to restore to.
  301. */
  302. function is_recognized_type() {
  303. return @$this->type['restore'] || @$this->type['backup'];
  304. }
  305. /**
  306. * Open a file for reading or writing.
  307. */
  308. function open($write = FALSE, $binary = FALSE) {
  309. if (!$this->handle) {
  310. $path = $this->filepath();
  311. // Check if the file can be read/written.
  312. if ($write && ((file_exists($path) && !is_writable($path)) || !is_writable(dirname($path)))) {
  313. _backup_migrate_message('The file %path cannot be written to.', array('%path' => $path), 'error');
  314. return FALSE;
  315. }
  316. if (!$write && !is_readable($path)) {
  317. _backup_migrate_message('The file %path cannot be read.', array('%path' => $path), 'error');
  318. return FALSE;
  319. }
  320. // Open the file.
  321. $mode = ($write ? "w" : "r") . ($binary ? "b" : "");
  322. $this->handle = fopen($path, $mode);
  323. return $this->handle;
  324. }
  325. return NULL;
  326. }
  327. /**
  328. * Close a file when we're done reading/writing.
  329. */
  330. function close() {
  331. fclose($this->handle);
  332. $this->handle = NULL;
  333. }
  334. /**
  335. * Write a line to the file.
  336. */
  337. function write($data) {
  338. if (!$this->handle) {
  339. $this->handle = $this->open(TRUE);
  340. }
  341. if ($this->handle) {
  342. fwrite($this->handle, $data);
  343. }
  344. }
  345. /**
  346. * Read a line from the file.
  347. */
  348. function read($size = NULL) {
  349. if (!$this->handle) {
  350. $this->handle = $this->open();
  351. }
  352. if ($this->handle && !feof($this->handle)) {
  353. return $size ? fread($this->handle, $size) : fgets($this->handle);
  354. }
  355. return NULL;
  356. }
  357. /**
  358. * Write data to the file.
  359. */
  360. function put_contents($data) {
  361. file_put_contents($this->filepath(), $data);
  362. }
  363. /**
  364. * Read data from the file.
  365. */
  366. function get_contents() {
  367. return file_get_contents($this->filepath());
  368. }
  369. /**
  370. * Transfer file using http to client. Similar to the built in file_transfer,
  371. * but it calls module_invoke_all('exit') so that temp files can be deleted.
  372. */
  373. function transfer() {
  374. $headers = array(
  375. array('key' => 'Content-Type', 'value' => $this->mimetype()),
  376. array('key' => 'Content-Disposition', 'value' => 'attachment; filename="'. $this->filename() .'"'),
  377. );
  378. // In some circumstances, web-servers will double compress gzipped files.
  379. // This may help aleviate that issue by disabling mod-deflate.
  380. if ($this->mimetype() == 'application/x-gzip') {
  381. $headers[] = array('key' => 'Content-Encoding', 'value' => 'gzip');
  382. }
  383. if ($size = $this->info('filesize')) {
  384. $headers[] = array('key' => 'Content-Length', 'value' => $size);
  385. }
  386. // Suppress the warning you get when the buffer is empty.
  387. @ob_end_clean();
  388. if ($this->open(FALSE, TRUE)) {
  389. foreach ($headers as $header) {
  390. // To prevent HTTP header injection, we delete new lines that are
  391. // not followed by a space or a tab.
  392. // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
  393. $header['value'] = preg_replace('/\r?\n(?!\t| )/', '', $header['value']);
  394. drupal_add_http_header($header['key'], $header['value']);
  395. }
  396. // Transfer file in 1024 byte chunks to save memory usage.
  397. while ($data = $this->read(1024)) {
  398. print $data;
  399. }
  400. $this->close();
  401. // Ask devel.module not to print it's footer.
  402. $GLOBALS['devel_shutdown'] = FALSE;
  403. }
  404. else {
  405. drupal_not_found();
  406. }
  407. // Start buffering and throw away the results so that errors don't get appended to the file.
  408. ob_start('_backup_migrate_file_dispose_buffer');
  409. backup_migrate_cleanup();
  410. module_invoke_all('exit');
  411. exit();
  412. }
  413. /**
  414. * Push a file extension onto the file and return the previous file path.
  415. */
  416. function push_type($extension) {
  417. $types = _backup_migrate_filetypes();
  418. if ($type = @$types[$extension]) {
  419. $this->push_filetype($type);
  420. }
  421. $out = $this->filepath();
  422. $this->temporary_file();
  423. return $out;
  424. }
  425. /**
  426. * Push a file extension onto the file and return the previous file path.
  427. */
  428. function pop_type() {
  429. $out = new backup_file(array('filepath' => $this->filepath()));
  430. $this->pop_filetype();
  431. $this->temporary_file();
  432. return $out;
  433. }
  434. /**
  435. * Set the current file type.
  436. */
  437. function set_filetype($type) {
  438. $this->type = $type;
  439. $this->ext = array($type['extension']);
  440. }
  441. /**
  442. * Set the current file type.
  443. */
  444. function push_filetype($type) {
  445. $this->ext[] = $type['extension'];
  446. $this->type = $type;
  447. }
  448. /**
  449. * Pop the current file type.
  450. */
  451. function pop_filetype() {
  452. array_pop($this->ext);
  453. $this->detect_filetype_from_extension();
  454. }
  455. /**
  456. * Set the file info.
  457. */
  458. function set_file_info($file_info) {
  459. $this->file_info = $file_info;
  460. $this->ext = explode('.', @$this->file_info['filename']);
  461. // Remove the underscores added to file extensions by Drupal's upload security.
  462. foreach ($this->ext as $key => $val) {
  463. $this->ext[$key] = trim($val, '_');
  464. }
  465. $this->filename(array_shift($this->ext));
  466. $this->detect_filetype_from_extension();
  467. }
  468. /**
  469. * Get the filetype info of the given file, or false if the file is not a valid type.
  470. */
  471. function detect_filetype_from_extension() {
  472. $ext = end($this->ext);
  473. $this->type = array();
  474. $types = _backup_migrate_filetypes();
  475. foreach ($types as $key => $type) {
  476. if (trim($ext, "_0123456789") === $type['extension']) {
  477. $this->type = $type;
  478. $this->type['id'] = $key;
  479. }
  480. }
  481. }
  482. /**
  483. * Get a temporary file name with path.
  484. */
  485. function temporary_file() {
  486. $file = drupal_tempnam('temporary://', 'backup_migrate_');
  487. // Add the version without the extension. The tempnam function creates this for us.
  488. backup_migrate_temp_files_add($file);
  489. if ($this->extension()) {
  490. $file .= '.'. $this->extension();
  491. // Add the version with the extension. This is the one we will actually use.
  492. backup_migrate_temp_files_add($file);
  493. }
  494. $this->path = $file;
  495. }
  496. }