destinations.inc

  1. nittany7 modules/contrib/backup_migrate/includes/destinations.inc
  2. mooc7 modules/contrib/backup_migrate/includes/destinations.inc

All of the destination handling code needed for Backup and Migrate.

Functions

Namesort descending Description
backup_migrate_backup_migrate_destinations Implementation of hook_backup_migrate_destinations().
backup_migrate_backup_migrate_destination_types Implementation of hook_backup_migrate_destination_types().
backup_migrate_create_destination Create a destination object of the given type with the given params.
backup_migrate_destination_delete_file Delete a file in the given destination.
backup_migrate_destination_file_exists Check if a file exists in the given destination.
backup_migrate_destination_get_file Load a file from a destination and return the file info.
backup_migrate_destination_save_file Send a file to the destination specified by the settings array.
backup_migrate_get_destination Get the destination of the given id.
backup_migrate_get_destinations Get all the available backup destination.
backup_migrate_get_destination_types Get the available destination types.
backup_migrate_ui_destination_delete_file Menu callback to delete a file from a destination.
backup_migrate_ui_destination_delete_file_confirm Ask confirmation for file deletion.
backup_migrate_ui_destination_delete_file_confirm_submit Delete confirmed, perform the delete.
backup_migrate_ui_destination_display_files List the backup files in the given destination.
backup_migrate_ui_destination_download_file Download a file to the browser.
backup_migrate_ui_destination_restore_file Restore a backup file from a destination.
backup_migrate_ui_destination_restore_file_confirm Ask confirmation for file restore.
backup_migrate_ui_destination_restore_file_confirm_submit Do the file restore.
_backup_migrate_destination_get_file_links Get the action links for a file on a given destination.
_backup_migrate_get_destination_form_item_options Get the destination options as an options array for a form item.
_backup_migrate_get_source_form Get the source options as a form element.
_backup_migrate_get_source_pulldown Get pulldown to select existing source options.

Classes

Namesort descending Description
backup_migrate_destination A base class for creating destinations.
backup_migrate_destination_remote A base class for creating destinations.

File

modules/contrib/backup_migrate/includes/destinations.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * All of the destination handling code needed for Backup and Migrate.
  5. */
  6. backup_migrate_include('crud');
  7. /**
  8. * Get the available destination types.
  9. */
  10. function backup_migrate_get_destination_types() {
  11. $out = array();
  12. foreach (module_implements('backup_migrate_destination_types') as $module) {
  13. $fn = $module . '_backup_migrate_destination_types';
  14. $out += $fn();
  15. }
  16. return $out;
  17. }
  18. /**
  19. * Implementation of hook_backup_migrate_destination_types().
  20. *
  21. * Get the built in Backup and Migrate destination types.
  22. */
  23. function backup_migrate_backup_migrate_destination_types() {
  24. $out = array();
  25. if (!module_exists('nodesquirrel')) {
  26. $out += array(
  27. 'nodesquirrel' => array(
  28. 'description' => t('!link is the cloud backup service by the maintainers of Backup and Migrate.', array('!link' => l(t('NodeSquirrel'), 'http://nodesquirrel.com'))),
  29. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.nodesquirrel.inc',
  30. 'class' => 'backup_migrate_destination_nodesquirrel',
  31. 'type_name' => t('NodeSquirrel.com'),
  32. 'can_create' => TRUE,
  33. ),
  34. );
  35. }
  36. if (variable_get('backup_migrate_allow_backup_to_file', TRUE)) {
  37. $out += array(
  38. 'file' => array(
  39. 'description' => t('Save the backup files to any local directory the web server can write to.'),
  40. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.file.inc',
  41. 'class' => 'backup_migrate_destination_files',
  42. 'type_name' => t('Server Directory'),
  43. 'can_create' => TRUE,
  44. ),
  45. 'file_manual' => array(
  46. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.file.inc',
  47. 'type_name' => t('Server Directory'),
  48. 'class' => 'backup_migrate_destination_files_manual',
  49. ),
  50. 'file_scheduled' => array(
  51. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.file.inc',
  52. 'type_name' => t('Server Directory'),
  53. 'class' => 'backup_migrate_destination_files_scheduled',
  54. ),
  55. );
  56. }
  57. $out += array(
  58. 'browser_download' => array(
  59. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.browser.inc',
  60. 'class' => 'backup_migrate_destination_browser_download',
  61. ),
  62. 'browser_upload' => array(
  63. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.browser.inc',
  64. 'class' => 'backup_migrate_destination_browser_upload',
  65. ),
  66. 'db' => array(
  67. 'type_name' => t('Database'),
  68. 'description' => t('Import the backup directly into another database. Database destinations can also be used as a source to backup from.'),
  69. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.db.inc',
  70. 'class' => 'backup_migrate_destination_db',
  71. 'can_create' => FALSE,
  72. ),
  73. 'mysql' => array(
  74. 'type_name' => t('MySQL Database'),
  75. 'description' => t('Import the backup directly into another MySQL database. Database destinations can also be used as a source to backup from.'),
  76. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.db.mysql.inc',
  77. 'class' => 'backup_migrate_destination_db_mysql',
  78. 'can_create' => TRUE,
  79. ),
  80. 'ftp' => array(
  81. 'description' => t('Save the backup files to any a directory on an FTP server.'),
  82. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.ftp.inc',
  83. 'class' => 'backup_migrate_destination_ftp',
  84. 'type_name' => t('FTP Directory'),
  85. 'can_create' => TRUE,
  86. ),
  87. 's3' => array(
  88. 'description' => t('Save the backup files to a bucket on your !link.', array('!link' => l(t('Amazon S3 account'), 'http://aws.amazon.com/s3/'))),
  89. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.s3.inc',
  90. 'class' => 'backup_migrate_destination_s3',
  91. 'type_name' => t('Amazon S3 Bucket'),
  92. 'can_create' => TRUE,
  93. ),
  94. 'email' => array(
  95. 'type_name' => t('Email'),
  96. 'description' => t('Send the backup as an email attachment to the specified email address.'),
  97. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.email.inc',
  98. 'class' => 'backup_migrate_destination_email',
  99. 'can_create' => TRUE,
  100. ),
  101. );
  102. return $out;
  103. }
  104. /**
  105. * Implementation of hook_backup_migrate_destinations().
  106. *
  107. * Get the built in backup destinations and those in the db.
  108. */
  109. function backup_migrate_backup_migrate_destinations() {
  110. $out = array();
  111. // Add the default, out of the box destinations for new users.
  112. if (variable_get('backup_migrate_allow_backup_to_file', TRUE)) {
  113. if (variable_get('file_private_path', FALSE)) {
  114. $out['manual'] = backup_migrate_create_destination('file_manual', array('destination_id' => 'manual'));
  115. $out['scheduled'] = backup_migrate_create_destination('file_scheduled', array('destination_id' => 'scheduled'));
  116. }
  117. else {
  118. _backup_migrate_message('You must specify a private file system path in the !settings to backup to the server.', array('!settings' => l(t('file system settings'), 'admin/config/media/file-system')), 'warning');
  119. }
  120. }
  121. // Add the browser destination for downloading to the desktop.
  122. if (variable_get('backup_migrate_allow_backup_to_download', TRUE)) {
  123. $out['download'] = backup_migrate_create_destination('browser_download');
  124. }
  125. $out['upload'] = backup_migrate_create_destination('browser_upload');
  126. if ($secret_key = variable_get('nodesquirrel_secret_key', '')) {
  127. $out['nodesquirrel'] = backup_migrate_create_destination('nodesquirrel', array('destination_id' => 'nodesquirrel'));
  128. $out['nodesquirrel']->settings['secret_key'] = $secret_key;
  129. }
  130. // Expose the configured databases as sources.
  131. backup_migrate_include('filters');
  132. $out += backup_migrate_filters_invoke_all('destinations');
  133. /*
  134. global $db_url;
  135. $urls = is_array($db_url) ? $db_url : array('default' => $db_url);
  136. foreach ((array)$urls as $key => $url) {
  137. // Only mysql is currently supported. If more DBMs's are implemented, theis will need to be abstacted.
  138. if (strpos($url, 'mysql') === 0) {
  139. if ($destination = backup_migrate_create_destination('mysql', array('url' => $url))) {
  140. // Treat the default database differently because it is probably the only one available.
  141. if ($key == 'default') {
  142. $destination->set_id('db');
  143. $destination->set_name(t('Default Database'));
  144. // Dissalow backing up to the default database because that's confusing and potentially dangerous.
  145. unset($destination->supported_ops['scheduled backup']);
  146. unset($destination->supported_ops['manual backup']);
  147. }
  148. else {
  149. $destination->set_id('db:'. $key);
  150. $destination->set_name($key .": ". $destination->get_display_location());
  151. }
  152. $out[$destination->get_id()] = $destination;
  153. }
  154. }
  155. }
  156. */
  157. return $out;
  158. }
  159. /**
  160. * Get all the available backup destination.
  161. *
  162. * @param $op
  163. * The operation which will be performed on the destination. Hooks can use this
  164. * to return only those destinations appropriate for the given op.
  165. * Options include:
  166. * 'manual backup' - destinations available for manual backup
  167. * 'scheduled backup' - destinations available for schedules backup
  168. * 'list files' - destinations whose backup files can be listed
  169. * 'restore' - destinations whose files can be restored from
  170. * 'all' - all available destinations should be returned
  171. */
  172. function backup_migrate_get_destinations($op = 'all') {
  173. static $destinations = NULL;
  174. // Get the list of destinations and cache them locally.
  175. if ($destinations === NULL) {
  176. $destinations = backup_migrate_crud_get_items('destination');
  177. }
  178. // Return all if that's what was asked for.
  179. if ($op == 'all') {
  180. return $destinations;
  181. }
  182. // Return only those destinations which support the given op.
  183. $out = array();
  184. foreach ($destinations as $key => $destination) {
  185. if ($destination->op($op)) {
  186. $out[$key] = $destination;
  187. }
  188. }
  189. return $out;
  190. }
  191. /**
  192. * Get the destination of the given id.
  193. */
  194. function backup_migrate_get_destination($id) {
  195. $destinations = backup_migrate_get_destinations('all');
  196. return empty($destinations[$id]) ? NULL : $destinations[$id];
  197. }
  198. /**
  199. * Create a destination object of the given type with the given params.
  200. */
  201. function backup_migrate_create_destination($destination_type, $params = array()) {
  202. $params['type'] = $destination_type;
  203. // Create a new dummy destination to call the create method on because the base item create is not static.
  204. $destination = new backup_migrate_destination();
  205. return $destination->create($params);
  206. }
  207. /**
  208. * Load a file from a destination and return the file info.
  209. */
  210. function backup_migrate_destination_get_file($destination_id, $file_id) {
  211. if ($destination = backup_migrate_get_destination($destination_id)) {
  212. return $destination->load_file($file_id);
  213. }
  214. return NULL;
  215. }
  216. /**
  217. * Check if a file exists in the given destination.
  218. */
  219. function backup_migrate_destination_file_exists($destination_id, $file_id) {
  220. if ($destination = backup_migrate_get_destination($destination_id)) {
  221. return $destination->file_exists($file_id);
  222. }
  223. return NULL;
  224. }
  225. /**
  226. * Send a file to the destination specified by the settings array.
  227. */
  228. function backup_migrate_destination_save_file($file, &$settings) {
  229. if ($destination = $settings->get_destination()) {
  230. $file = $destination->save_file($file, $settings);
  231. return $file;
  232. }
  233. return NULL;
  234. }
  235. /**
  236. * Delete a file in the given destination.
  237. */
  238. function backup_migrate_destination_delete_file($destination_id, $file_id) {
  239. if ($destination = backup_migrate_get_destination($destination_id)) {
  240. return $destination->delete_file($file_id);
  241. }
  242. }
  243. /**
  244. * Get the action links for a file on a given destination.
  245. */
  246. function _backup_migrate_destination_get_file_links($destination_id, $file_id) {
  247. $out = array();
  248. if ($destination = backup_migrate_get_destination($destination_id)) {
  249. $out = $destination->get_file_links($file_id);
  250. }
  251. return $out;
  252. }
  253. /* UI Menu Callbacks */
  254. /**
  255. * List the backup files in the given destination.
  256. */
  257. function backup_migrate_ui_destination_display_files($destination_id = NULL) {
  258. drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css');
  259. $rows = $sort = array();
  260. if ($destination = backup_migrate_get_destination($destination_id)) {
  261. // Refresh the file listing cache if requested.
  262. if (isset($_GET['refresh'])) {
  263. $destination->file_cache_clear();
  264. drupal_goto($_GET['q']);
  265. }
  266. drupal_set_title(t('@title Files', array('@title' => $destination->get_name())));
  267. $headers = array(
  268. array('data' => t('Filename'), 'field' => 'filename'),
  269. array('data' => t('Date'), 'field' => 'filetime'),
  270. array('data' => t('Age'), 'field' => 'filetime', 'sort' => 'desc'),
  271. array('data' => t('Size'), 'field' => 'filesize'),
  272. );
  273. $sort_order = tablesort_get_order($headers);
  274. $sort_key = $sort_order['sql'] ? $sort_order['sql'] : 'filetime';
  275. $sort_dir = tablesort_get_sort($headers) == 'desc' ? SORT_DESC : SORT_ASC;
  276. $files = $destination->list_files();
  277. $i = 0;
  278. $ops = 0;
  279. foreach ((array)$files as $file) {
  280. $info = $file->info();
  281. $operations = $destination->get_file_links($file->file_id());
  282. $description = '';
  283. // Add the description as a new row.
  284. if (!empty($info['description'])) {
  285. $description = ' <div title="'.check_plain($info['description']).'" class="backup-migrate-description">'. $info['description'] .'</div>';
  286. }
  287. // Show only files that can be restored from.
  288. if ($file->is_recognized_type()) {
  289. $sort[] = $info[$sort_key];
  290. $rows[] = array_merge(array(
  291. check_plain($info['filename']) . $description,
  292. format_date($info['filetime'], 'small'),
  293. format_interval(time() - $info['filetime'], 1),
  294. format_size($info['filesize']),
  295. ), $operations);
  296. }
  297. $ops = max($ops, count($operations));
  298. }
  299. // Add the operations if any
  300. if ($ops) {
  301. $headers[] = array('data' => t('Operations'), 'colspan' => $ops);
  302. }
  303. array_multisort($sort, $sort_dir, $rows);
  304. if ($rows) {
  305. $out = theme('table', array('header' => $headers, 'rows' => $rows));
  306. }
  307. else {
  308. $out = t('There are no backup files to display.');
  309. }
  310. if ($destination->cache_files && $destination->fetch_time) {
  311. drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css');
  312. $out .= '<div class="backup-migrate-cache-time">'. t('This listing was fetched !time ago. !refresh', array('!time' => format_interval(time() - $destination->fetch_time, 1), '!refresh' => l(t('fetch now'), $_GET['q'], array('query' => array('refresh' => 'true'))))) .'</div>';
  313. }
  314. return $out;
  315. }
  316. drupal_goto(BACKUP_MIGRATE_MENU_PATH . "/destination");
  317. }
  318. /**
  319. * Download a file to the browser.
  320. */
  321. function backup_migrate_ui_destination_download_file($destination_id = NULL, $file_id = NULL) {
  322. if ($file = backup_migrate_destination_get_file($destination_id, $file_id)) {
  323. backup_migrate_include('files');
  324. $file->transfer();
  325. }
  326. drupal_goto(BACKUP_MIGRATE_MENU_PATH);
  327. }
  328. /**
  329. * Restore a backup file from a destination.
  330. */
  331. function backup_migrate_ui_destination_restore_file($destination_id = NULL, $file_id = NULL) {
  332. if (backup_migrate_destination_file_exists($destination_id, $file_id)) {
  333. return drupal_get_form('backup_migrate_ui_destination_restore_file_confirm', $destination_id, $file_id);
  334. }
  335. drupal_goto(user_access('access backup files') ? BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/". $destination_id : BACKUP_MIGRATE_MENU_PATH);
  336. }
  337. /**
  338. * Ask confirmation for file restore.
  339. */
  340. function backup_migrate_ui_destination_restore_file_confirm($form, &$form_state, $destination_id, $file_id) {
  341. $sources = _backup_migrate_get_destination_form_item_options('source');
  342. if (count($sources) > 1) {
  343. $form['source_id'] = array(
  344. "#type" => "select",
  345. "#title" => t("Database"),
  346. "#options" => _backup_migrate_get_destination_form_item_options('source'),
  347. "#description" => t("Choose the database to restore to. Any database destinations you have created and any databases specified in your settings.php can be restored to."),
  348. "#default_value" => 'db',
  349. );
  350. }
  351. else {
  352. $form['source_id'] = array(
  353. "#type" => "value",
  354. "#value" => 'db',
  355. );
  356. }
  357. $form['destination_id'] = array('#type' => 'value', '#value' => $destination_id);
  358. $form['file_id'] = array('#type' => 'value', '#value' => $file_id);
  359. $form = confirm_form($form, t('Are you sure you want to restore the database?'), BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/". $destination_id, t('Are you sure you want to restore the database from the backup file %file_id? This will delete some or all of your data and cannot be undone. <strong>Always test your backups on a non-production server!</strong>', array('%file_id' => $file_id)), t('Restore'), t('Cancel'));
  360. drupal_set_message(t('Restoring will delete some or all of your data and cannot be undone. <strong>Always test your backups on a non-production server!</strong>'), 'warning', FALSE);
  361. $form = array_merge_recursive($form, backup_migrate_filters_settings_form(backup_migrate_filters_settings_default('restore'), 'restore'));
  362. $form['actions']['#weight'] = 100;
  363. // Add the advanced fieldset if there are any fields in it.
  364. if (@$form['advanced']) {
  365. $form['advanced']['#type'] = 'fieldset';
  366. $form['advanced']['#title'] = t('Advanced Options');
  367. $form['advanced']['#collapsed'] = true;
  368. $form['advanced']['#collapsible'] = true;
  369. }
  370. return $form;
  371. }
  372. /**
  373. * Do the file restore.
  374. */
  375. function backup_migrate_ui_destination_restore_file_confirm_submit($form, &$form_state) {
  376. $destination_id = $form_state['values']['destination_id'];
  377. $file_id = $form_state['values']['file_id'];
  378. if ($destination_id && $file_id) {
  379. backup_migrate_perform_restore($destination_id, $file_id, $form_state['values']);
  380. }
  381. $redir = user_access('access backup files') ? BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/". $destination_id : BACKUP_MIGRATE_MENU_PATH;
  382. $form_state['redirect'] = $redir;
  383. }
  384. /**
  385. * Menu callback to delete a file from a destination.
  386. */
  387. function backup_migrate_ui_destination_delete_file($destination_id = NULL, $file_id = NULL) {
  388. if (backup_migrate_destination_file_exists($destination_id, $file_id)) {
  389. return drupal_get_form('backup_migrate_ui_destination_delete_file_confirm', $destination_id, $file_id);
  390. }
  391. drupal_goto(BACKUP_MIGRATE_MENU_PATH);
  392. }
  393. /**
  394. * Ask confirmation for file deletion.
  395. */
  396. function backup_migrate_ui_destination_delete_file_confirm($form, &$form_state, $destination_id, $file_id) {
  397. $form['destination_id'] = array('#type' => 'value', '#value' => $destination_id);
  398. $form['file_id'] = array('#type' => 'value', '#value' => $file_id);
  399. return confirm_form($form, t('Are you sure you want to delete the backup file?'), BACKUP_MIGRATE_MENU_PATH . '/destination/list/files/'. $destination_id, t('Are you sure you want to delete the backup file %file_id? <strong>This action cannot be undone.</strong>', array('%file_id' => $file_id)), t('Delete'), t('Cancel'));
  400. }
  401. /**
  402. * Delete confirmed, perform the delete.
  403. */
  404. function backup_migrate_ui_destination_delete_file_confirm_submit($form, &$form_state) {
  405. if (user_access('delete backup files')) {
  406. $destination_id = $form_state['values']['destination_id'];
  407. $file_id = $form_state['values']['file_id'];
  408. backup_migrate_destination_delete_file($destination_id, $file_id);
  409. _backup_migrate_message('Database backup file deleted: %file_id', array('%file_id' => $file_id));
  410. }
  411. $form_state['redirect'] = user_access('access backup files') ? BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/". $destination_id : BACKUP_MIGRATE_MENU_PATH;
  412. }
  413. /* Utilities */
  414. /**
  415. * Get the source options as a form element.
  416. */
  417. function _backup_migrate_get_source_form($source_id = 'db') {
  418. backup_migrate_include('destinations');
  419. $form = array();
  420. $sources = _backup_migrate_get_source_pulldown($source_id);
  421. if (count($sources['#options']) > 1) {
  422. $form['source'] = array(
  423. "#type" => "fieldset",
  424. "#title" => t("Backup Source"),
  425. "#collapsible" => TRUE,
  426. "#collapsed" => FALSE,
  427. "#tree" => FALSE,
  428. );
  429. $sources['#description'] = t("Choose the database to backup. Any database destinations you have created and any databases specified in your settings.php can be backed up.");
  430. $form['source']['source_id'] = $sources;
  431. }
  432. else {
  433. $form = array();
  434. $form['source']['source_id'] = array(
  435. "#type" => "value",
  436. "#value" => $source_id,
  437. );
  438. }
  439. return $form;
  440. }
  441. /**
  442. * Get pulldown to select existing source options.
  443. */
  444. function _backup_migrate_get_source_pulldown($source_id = NULL) {
  445. backup_migrate_include('destinations');
  446. $sources = _backup_migrate_get_destination_form_item_options('source');
  447. $form = array(
  448. "#type" => "select",
  449. "#title" => t("Backup Source"),
  450. "#options" => _backup_migrate_get_destination_form_item_options('source'),
  451. "#default_value" => $source_id,
  452. );
  453. return $form;
  454. }
  455. /**
  456. * Get the destination options as an options array for a form item.
  457. */
  458. function _backup_migrate_get_destination_form_item_options($op) {
  459. $out = array();
  460. foreach (backup_migrate_get_destinations($op) as $key => $destination) {
  461. $out[$key] = $destination->get_name();
  462. }
  463. return $out;
  464. }
  465. /**
  466. * A base class for creating destinations.
  467. */
  468. class backup_migrate_destination extends backup_migrate_item {
  469. var $db_table = "backup_migrate_destinations";
  470. var $type_name = "destination";
  471. var $default_values = array('settings' => array());
  472. var $singular = 'destination';
  473. var $plural = 'destinations';
  474. var $cache_files = FALSE;
  475. var $fetch_time = NULL;
  476. var $cache_expire = 86400; // 24 hours
  477. var $destination_type = "";
  478. var $supported_ops = array();
  479. /**
  480. * This function is not supposed to be called. It is just here to help the po extractor out.
  481. */
  482. function strings() {
  483. // Help the pot extractor find these strings.
  484. t('Destination');
  485. t('Destinations');
  486. t('destinations');
  487. t('destination');
  488. }
  489. function ops() {
  490. return $this->supported_ops;
  491. }
  492. /**
  493. * Does this destination support the given operation.
  494. */
  495. function op($op) {
  496. $ops = (array)$this->ops();
  497. return in_array($op, $ops);
  498. }
  499. /**
  500. * Remove the given op from the support list.
  501. */
  502. function remove_op($op) {
  503. $key = array_search($op, $this->supported_ops);
  504. if ($key !== FALSE) {
  505. unset($this->supported_ops[$key]);
  506. }
  507. }
  508. function get_name() {
  509. return @$this->name;
  510. }
  511. function set_name($name) {
  512. return $this->name = $name;
  513. }
  514. function set_type($type) {
  515. $this->type = $type;
  516. }
  517. function set_location($location) {
  518. $this->location = $location;
  519. }
  520. function get_location() {
  521. return @$this->location;
  522. }
  523. function get_display_location() {
  524. return $this->get_location();
  525. }
  526. function file_types() {
  527. return array();
  528. }
  529. function settings($key = NULL) {
  530. $out = $this->settings;
  531. if ($key) {
  532. $out = isset($out[$key]) ? $out[$key] : NULL;
  533. }
  534. return $out;
  535. }
  536. /**
  537. * Get the type name of this destination for display to the user.
  538. */
  539. function get_destination_type_name() {
  540. if ($type = $this->destination_type) {
  541. $types = backup_migrate_get_destination_types();
  542. return isset($types[$type]['type_name']) ? $types[$type]['type_name'] : $type;
  543. }
  544. }
  545. /**
  546. * Save the given file to the destination.
  547. */
  548. function save_file($file, $settings) {
  549. // This must be overriden.
  550. $this->file_cache_clear();
  551. // Save the file metadata if the destination supports it.
  552. $this->save_file_info($file, $settings);
  553. return $this->_save_file($file, $settings);
  554. }
  555. /**
  556. * Save the given file to the destination.
  557. */
  558. function _save_file($file, $settings) {
  559. // This must be overriden.
  560. return $file;
  561. }
  562. /**
  563. * Save the file metadata
  564. */
  565. function save_file_info($file, $settings) {
  566. $info = $this->create_info_file($file);
  567. // Save the info file and the actual file.
  568. return $this->_save_file($info, $settings);
  569. }
  570. /**
  571. * Load the file with the given destination specific id and return as a backup_file object.
  572. */
  573. function load_file($file_id) {
  574. // This must be overriden.
  575. return NULL;
  576. }
  577. /**
  578. * Check if a file exists in the given destination.
  579. */
  580. function file_exists($file_id) {
  581. // Check if the file exists in the list of available files. Actual destination types may have more efficient ways of doing this.
  582. $files = $this->list_files();
  583. return isset($files[$file_id]);
  584. }
  585. /**
  586. * List all the available files in the given destination with their destination specific id.
  587. */
  588. function list_files() {
  589. $out = NULL;
  590. if ($this->cache_files) {
  591. $out = $this->file_cache_get();
  592. }
  593. if ($out === NULL) {
  594. $out = $this->_list_files();
  595. $out = $this->load_files_info($out);
  596. if ($this->cache_files) {
  597. $this->file_cache_set($out);
  598. }
  599. }
  600. return $out;
  601. }
  602. /**
  603. * List all the available files in the given destination with their destination specific id.
  604. */
  605. function _list_files() {
  606. return array();
  607. }
  608. /**
  609. * Load up the file's metadata from the accompanying .info file if applicable.
  610. */
  611. function load_files_info($files) {
  612. foreach ($files as $key => $file) {
  613. if (isset($files[$key . '.info'])) {
  614. // See if there is an info file with the same name as the backup.
  615. $info = drupal_parse_info_file($files[$key . '.info']->filepath());
  616. // Allow the stored metadata to override the detected metadata.
  617. $file->file_info = $info + $file->file_info;
  618. // Remove the metadata file from the list
  619. unset($files[$key . '.info']);
  620. }
  621. }
  622. return $files;
  623. }
  624. /**
  625. * Create an ini file and write the meta data.
  626. */
  627. function create_info_file($file) {
  628. $info = $this->_file_info_file($file);
  629. $data = _backup_migrate_array_to_ini($file->file_info);
  630. $info->put_contents($data);
  631. return $info;
  632. }
  633. /**
  634. * Create the info file object.
  635. */
  636. function _file_info_file($file) {
  637. $info = new backup_file(array('filename' => $this->_file_info_filename($file->file_id())));
  638. return $info;
  639. }
  640. /**
  641. * Determine the file name of the info file for a file.
  642. */
  643. function _file_info_filename($file_id) {
  644. return $file_id . '.info';
  645. }
  646. /**
  647. * Cache the file list.
  648. */
  649. function file_cache_set($files) {
  650. cache_set('backup_migrate_file_list:'. $this->get_id(), $files, 'cache', time() + $this->cache_expire);
  651. }
  652. /**
  653. * Retrieve the file list.
  654. */
  655. function file_cache_get() {
  656. backup_migrate_include('files');
  657. $cache = cache_get('backup_migrate_file_list:'. $this->get_id());
  658. if (!empty($cache->data) && $cache->created > (time() - $this->cache_expire)) {
  659. $this->fetch_time = $cache->created;
  660. return $cache->data;
  661. }
  662. $this->fetch_time = 0;
  663. return NULL;
  664. }
  665. /**
  666. * Retrieve the file list.
  667. */
  668. function file_cache_clear() {
  669. if ($this->cache_files) {
  670. $this->file_cache_set(NULL);
  671. }
  672. }
  673. /**
  674. * Delete the file with the given destination specific id.
  675. */
  676. function delete_file($file_id) {
  677. $this->file_cache_clear();
  678. $this->_delete_file($file_id);
  679. $this->_delete_file($this->_file_info_filename($file_id));
  680. }
  681. /**
  682. * Delete the file with the given destination specific id.
  683. */
  684. function _delete_file($file_id) {
  685. // This must be overriden.
  686. }
  687. /**
  688. * Get the edit form for the item.
  689. */
  690. function edit_form() {
  691. if (get_class($this) !== 'backup_migrate_destination') {
  692. $form = parent::edit_form();
  693. $form['name'] = array(
  694. "#type" => "textfield",
  695. "#title" => t("Destination name"),
  696. "#default_value" => $this->get_name(),
  697. "#required" => TRUE,
  698. );
  699. $form['type'] = array(
  700. "#type" => "value",
  701. "#default_value" => $this->destination_type,
  702. );
  703. }
  704. else {
  705. $types = backup_migrate_get_destination_types();
  706. $items = array();
  707. // If no (valid) node type has been provided, display a node type overview.
  708. foreach ($types as $key => $type) {
  709. if (@$type['can_create']) {
  710. $type_url_str = str_replace('_', '-', $key);
  711. $out = '<dt>'. l($type['type_name'], BACKUP_MIGRATE_MENU_PATH . "/destination/list/add/$type_url_str", array('attributes' => array('title' => t('Add a new @s destination.', array('@s' => $type['type_name']))))) .'</dt>';
  712. $out .= '<dd>'. filter_xss_admin($type['description']) .'</dd>';
  713. $items[] = $out;
  714. }
  715. }
  716. if (count($items)) {
  717. $output = t('Choose the type of destination you would like to create:') .'<dl>'. implode('', $items) .'</dl>';
  718. }
  719. else {
  720. $output = t('No destination types available.');
  721. }
  722. $form['select_type'] = array(
  723. '#type' => 'markup',
  724. '#markup' => $output,
  725. );
  726. }
  727. return $form;
  728. }
  729. /**
  730. * Get the message to send to the user when confirming the deletion of the item.
  731. */
  732. function delete_confirm_message() {
  733. return t('Are you sure you want to delete the destination %name? Backup files already saved to this destination will not be deleted.', array('%name' => $this->get_name()));
  734. }
  735. /**
  736. * Get the columns needed to list the type.
  737. */
  738. function get_list_column_info() {
  739. $out = parent::get_list_column_info();
  740. $out = array(
  741. 'name' => array('title' => t('Name')),
  742. 'destination_type_name' => array('title' => t('Type')),
  743. 'display_location' => array('title' => t('Location')),
  744. ) + $out;
  745. return $out;
  746. }
  747. /**
  748. * Get a row of data to be used in a list of items of this type.
  749. */
  750. function get_list_row() {
  751. $out = parent::get_list_row();
  752. // Supress destinations with no actions as there's no value in showing them (and they may confuse new users).
  753. if (empty($out['actions'])) {
  754. return NULL;
  755. }
  756. return $out;
  757. }
  758. /**
  759. * Get the action links for a destination.
  760. */
  761. function get_action_links() {
  762. $out = parent::get_action_links();
  763. $item_id = $this->get_id();
  764. // Don't display the download/delete/restore ops if they are not available for this destination.
  765. if ($this->op('list files') && user_access("access backup files")) {
  766. $out = array('list files' => l(t("list files"), BACKUP_MIGRATE_MENU_PATH . "/$this->type_name/list/files/". $item_id)) + $out;
  767. }
  768. if (!$this->op('configure') || !user_access('administer backup and migrate')) {
  769. unset($out['edit']);
  770. }
  771. return $out;
  772. }
  773. /**
  774. * Get the action links for a file on a given destination.
  775. */
  776. function get_file_links($file_id) {
  777. $out = array('download' => '', 'restore' => '', 'delete' => '');
  778. // Don't display the download/delete/restore ops if they are not available for this destination.
  779. $can_read = $this->can_read_file($file_id);
  780. $can_delete = $this->can_delete_file($file_id);
  781. $destination_id = $this->get_id();
  782. if ($can_read && user_access("access backup files")) {
  783. $out['download'] = l(t("download"), BACKUP_MIGRATE_MENU_PATH . "/destination/downloadfile/". $destination_id .'/'. $file_id);
  784. }
  785. if ($can_read && user_access("restore from backup")) {
  786. $out['restore'] = l(t("restore"), BACKUP_MIGRATE_MENU_PATH . "/destination/restorefile/". $destination_id .'/'. $file_id);
  787. }
  788. if ($can_delete && user_access("delete backup files")) {
  789. $out['delete'] = l(t("delete"), BACKUP_MIGRATE_MENU_PATH . "/destination/deletefile/". $destination_id .'/'. $file_id);
  790. }
  791. return $out;
  792. }
  793. /**
  794. * Determine if we can read the given file.
  795. */
  796. function can_read_file($file_id) {
  797. return $this->op('restore');
  798. }
  799. /**
  800. * Determine if we can read the given file.
  801. */
  802. function can_delete_file($file_id) {
  803. return $this->op('delete');
  804. }
  805. /**
  806. * Get the form for the settings for this destination type.
  807. */
  808. function settings_default() {
  809. return array();
  810. }
  811. /**
  812. * Get the form for the settings for this destination.
  813. */
  814. function settings_form($form) {
  815. return $form;
  816. }
  817. /**
  818. * Validate the form for the settings for this destination.
  819. */
  820. function settings_form_validate($form_values) {
  821. }
  822. /**
  823. * Submit the settings form. Any values returned will be saved.
  824. */
  825. function settings_form_submit($form_values) {
  826. return $form_values;
  827. }
  828. /**
  829. * Create a new destination of the correct type.
  830. */
  831. function create($params = array()) {
  832. $out = NULL;
  833. $types = backup_migrate_get_destination_types();
  834. // Get the type passed in in the params, or if none, check the url for a valid type name.
  835. // This is to allow new destination type to be specified in the path.
  836. $destination_type = !empty($params['type']) ? $params['type'] : arg(BACKUP_MIGRATE_MENU_DEPTH + 3);
  837. if ($destination_type && ($type = @$types[$destination_type])) {
  838. // Include the necessary file if specified by the type.
  839. if (!empty($type['file'])) {
  840. require_once './'. $type['file'];
  841. }
  842. $out = new $type['class']($params + array('destination_type' => $destination_type));
  843. }
  844. if (empty($out)) {
  845. $out = new backup_migrate_destination();
  846. }
  847. return $out;
  848. }
  849. /**
  850. * Add the menu items specific to the destination type.
  851. */
  852. function get_menu_items() {
  853. $items = parent::get_menu_items();
  854. $items[BACKUP_MIGRATE_MENU_PATH . '/destination/list/files'] = array(
  855. 'title' => 'Destination Files',
  856. 'page callback' => 'backup_migrate_menu_callback',
  857. 'page arguments' => array('destinations', 'backup_migrate_ui_destination_display_files', TRUE),
  858. 'access arguments' => array('access backup files'),
  859. 'type' => MENU_LOCAL_TASK,
  860. );
  861. $items[BACKUP_MIGRATE_MENU_PATH . '/destination/deletefile'] = array(
  862. 'title' => 'Delete File',
  863. 'description' => 'Delete a backup file',
  864. 'page callback' => 'backup_migrate_menu_callback',
  865. 'page arguments' => array('destinations', 'backup_migrate_ui_destination_delete_file', TRUE),
  866. 'access arguments' => array('delete backup files'),
  867. 'type' => MENU_CALLBACK,
  868. );
  869. $items[BACKUP_MIGRATE_MENU_PATH . '/destination/restorefile'] = array(
  870. 'title' => 'Restore from backup',
  871. 'description' => 'Restore database from a backup file on the server',
  872. 'page callback' => 'backup_migrate_menu_callback',
  873. 'page arguments' => array('destinations', 'backup_migrate_ui_destination_restore_file', TRUE),
  874. 'access arguments' => array('restore from backup'),
  875. 'type' => MENU_CALLBACK,
  876. );
  877. $items[BACKUP_MIGRATE_MENU_PATH . '/destination/downloadfile'] = array(
  878. 'title' => 'Download File',
  879. 'description' => 'Download a backup file',
  880. 'page callback' => 'backup_migrate_menu_callback',
  881. 'page arguments' => array('destinations', 'backup_migrate_ui_destination_download_file', TRUE),
  882. 'access arguments' => array('access backup files'),
  883. 'type' => MENU_CALLBACK,
  884. );
  885. return $items;
  886. }
  887. /**
  888. * Get the form for the settings for this filter.
  889. */
  890. function backup_settings_default() {
  891. return array();
  892. }
  893. /**
  894. * Get the form for the settings for this filter.
  895. */
  896. function backup_settings_form($settings) {
  897. return array();
  898. }
  899. /**
  900. * Get the form for the settings for this filter.
  901. */
  902. function backup_settings_form_validate($form, &$form_state) {
  903. }
  904. /**
  905. * Submit the settings form. Any values returned will be saved.
  906. */
  907. function backup_settings_form_submit($form, &$form_state) {
  908. }
  909. /**
  910. * Get the form for the settings for this filter.
  911. */
  912. function restore_settings_default() {
  913. return array();
  914. }
  915. /**
  916. * Get the form for the settings for this filter.
  917. */
  918. function restore_settings_form($settings) {
  919. return array();
  920. }
  921. /**
  922. * Get the form for the settings for this filter.
  923. */
  924. function restore_settings_form_validate($form_values) {
  925. }
  926. /**
  927. * Submit the settings form. Any values returned will be saved.
  928. */
  929. function restore_settings_form_submit($form_values) {
  930. return $form_values;
  931. }
  932. }
  933. /**
  934. * A base class for creating destinations.
  935. */
  936. class backup_migrate_destination_remote extends backup_migrate_destination {
  937. /**
  938. * The location is a URI so parse it and store the parts.
  939. */
  940. function get_location() {
  941. return $this->url(FALSE);
  942. }
  943. /**
  944. * The location to display is the url without the password.
  945. */
  946. function get_display_location() {
  947. return $this->url(TRUE);
  948. }
  949. /**
  950. * Return the location with the password.
  951. */
  952. function set_location($location) {
  953. $this->location = $location;
  954. $this->set_url($location);
  955. }
  956. /**
  957. * Get a url from the parts.
  958. */
  959. function url($hide_password = TRUE) {
  960. return $this->glue_url($this->dest_url, $hide_password);
  961. }
  962. /**
  963. * Glue a URLs component parts back into a URL.
  964. */
  965. function glue_url($parts, $hide_password = TRUE) {
  966. // Obscure the password if we need to.
  967. $parts['pass'] = $hide_password ? "" : $parts['pass'];
  968. // Assemble the URL.
  969. $out = "";
  970. $out .= $parts['scheme'] .'://';
  971. $out .= $parts['user'] ? urlencode($parts['user']) : '';
  972. $out .= ($parts['user'] && $parts['pass']) ? ":". urlencode($parts['pass']) : '';
  973. $out .= ($parts['user'] || $parts['pass']) ? "@" : "";
  974. $out .= $parts['host'];
  975. $out .= !empty($parts['port']) ? ':'. $parts['port'] : '';
  976. $out .= "/". $parts['path'];
  977. return $out;
  978. }
  979. /**
  980. * Break a URL into it's component parts.
  981. */
  982. function set_url($url) {
  983. $parts = (array)parse_url($url);
  984. $parts['user'] = urldecode(@$parts['user']);
  985. $parts['pass'] = urldecode(@$parts['pass']);
  986. $parts['path'] = urldecode(@$parts['path']);
  987. $parts['path'] = ltrim(@$parts['path'], "/");
  988. $this->dest_url = $parts;
  989. }
  990. /**
  991. * Destination configuration callback.
  992. */
  993. function edit_form() {
  994. $form = parent::edit_form();
  995. $form['scheme'] = array(
  996. "#type" => "value",
  997. "#title" => t("Scheme"),
  998. "#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : 'mysql',
  999. "#required" => TRUE,
  1000. // "#options" => array($GLOBALS['db_type'] => $GLOBALS['db_type']),
  1001. "#weight" => 0,
  1002. );
  1003. $form['host'] = array(
  1004. "#type" => "textfield",
  1005. "#title" => t("Host"),
  1006. "#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost',
  1007. "#required" => TRUE,
  1008. "#weight" => 10,
  1009. );
  1010. $form['path'] = array(
  1011. "#type" => "textfield",
  1012. "#title" => t("Path"),
  1013. "#default_value" => @$this->dest_url['path'],
  1014. "#required" => TRUE,
  1015. "#weight" => 20,
  1016. );
  1017. $form['user'] = array(
  1018. "#type" => "textfield",
  1019. "#title" => t("Username"),
  1020. "#default_value" => @$this->dest_url['user'],
  1021. "#required" => TRUE,
  1022. "#weight" => 30,
  1023. );
  1024. $form['pass'] = array(
  1025. "#type" => "password",
  1026. "#title" => t("Password"),
  1027. "#default_value" => @$this->dest_url['pass'],
  1028. '#description' => '',
  1029. "#weight" => 40,
  1030. );
  1031. if (@$this->dest_url['pass']) {
  1032. $form['old_password'] = array(
  1033. "#type" => "value",
  1034. "#value" => @$this->dest_url['pass'],
  1035. );
  1036. $form['pass']["#description"] .= t(' You do not need to enter a password unless you wish to change the currently saved password.');
  1037. }
  1038. return $form;
  1039. }
  1040. /**
  1041. * Submit the configuration form. Glue the url together and add the old password back if a new one was not specified.
  1042. */
  1043. function edit_form_submit($form, &$form_state) {
  1044. $form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password'];
  1045. $form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE);
  1046. parent::edit_form_submit($form, $form_state);
  1047. }
  1048. }