destinations.inc 37 KB

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