destinations.inc 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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. function create_info_file($file) {
  605. $info = new backup_file(array('filename' => $file->filename() .'.info'));
  606. $data = _backup_migrate_array_to_ini($file->file_info);
  607. $info->put_contents($data);
  608. return $info;
  609. }
  610. /**
  611. * Cache the file list.
  612. */
  613. function file_cache_set($files) {
  614. cache_set('backup_migrate_file_list:'. $this->get_id(), $files, 'cache', time() + $this->cache_expire);
  615. }
  616. /**
  617. * Retrieve the file list.
  618. */
  619. function file_cache_get() {
  620. backup_migrate_include('files');
  621. $cache = cache_get('backup_migrate_file_list:'. $this->get_id());
  622. if (!empty($cache->data) && $cache->created > (time() - $this->cache_expire)) {
  623. $this->fetch_time = $cache->created;
  624. return $cache->data;
  625. }
  626. $this->fetch_time = 0;
  627. return NULL;
  628. }
  629. /**
  630. * Retrieve the file list.
  631. */
  632. function file_cache_clear() {
  633. if ($this->cache_files) {
  634. $this->file_cache_set(NULL);
  635. }
  636. }
  637. /**
  638. * Delete the file with the given destination specific id.
  639. */
  640. function delete_file($file_id) {
  641. $this->file_cache_clear();
  642. $this->_delete_file($file_id);
  643. }
  644. /**
  645. * Delete the file with the given destination specific id.
  646. */
  647. function _delete_file($file_id) {
  648. // This must be overriden.
  649. }
  650. /**
  651. * Get the edit form for the item.
  652. */
  653. function edit_form() {
  654. if (get_class($this) !== 'backup_migrate_destination') {
  655. $form = parent::edit_form();
  656. $form['name'] = array(
  657. "#type" => "textfield",
  658. "#title" => t("Destination name"),
  659. "#default_value" => $this->get_name(),
  660. "#required" => TRUE,
  661. );
  662. $form['type'] = array(
  663. "#type" => "value",
  664. "#default_value" => $this->destination_type,
  665. );
  666. }
  667. else {
  668. $types = backup_migrate_get_destination_types();
  669. $items = array();
  670. // If no (valid) node type has been provided, display a node type overview.
  671. foreach ($types as $key => $type) {
  672. if (@$type['can_create']) {
  673. $type_url_str = str_replace('_', '-', $key);
  674. $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>';
  675. $out .= '<dd>'. filter_xss_admin($type['description']) .'</dd>';
  676. $items[] = $out;
  677. }
  678. }
  679. if (count($items)) {
  680. $output = t('Choose the type of destination you would like to create:') .'<dl>'. implode('', $items) .'</dl>';
  681. }
  682. else {
  683. $output = t('No destination types available.');
  684. }
  685. $form['select_type'] = array(
  686. '#type' => 'markup',
  687. '#markup' => $output,
  688. );
  689. }
  690. return $form;
  691. }
  692. /**
  693. * Get the message to send to the user when confirming the deletion of the item.
  694. */
  695. function delete_confirm_message() {
  696. 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()));
  697. }
  698. /**
  699. * Get the columns needed to list the type.
  700. */
  701. function get_list_column_info() {
  702. $out = parent::get_list_column_info();
  703. $out = array(
  704. 'name' => array('title' => t('Name')),
  705. 'destination_type_name' => array('title' => t('Type')),
  706. 'display_location' => array('title' => t('Location')),
  707. ) + $out;
  708. return $out;
  709. }
  710. /**
  711. * Get a row of data to be used in a list of items of this type.
  712. */
  713. function get_list_row() {
  714. $out = parent::get_list_row();
  715. // Supress destinations with no actions as there's no value in showing them (and they may confuse new users).
  716. if (empty($out['actions'])) {
  717. return NULL;
  718. }
  719. return $out;
  720. }
  721. /**
  722. * Get the action links for a destination.
  723. */
  724. function get_action_links() {
  725. $out = parent::get_action_links();
  726. $item_id = $this->get_id();
  727. // Don't display the download/delete/restore ops if they are not available for this destination.
  728. if ($this->op('list files') && user_access("access backup files")) {
  729. $out = array('list files' => l(t("list files"), BACKUP_MIGRATE_MENU_PATH . "/$this->type_name/list/files/". $item_id)) + $out;
  730. }
  731. if (!$this->op('configure') || !user_access('administer backup and migrate')) {
  732. unset($out['edit']);
  733. }
  734. return $out;
  735. }
  736. /**
  737. * Get the action links for a file on a given destination.
  738. */
  739. function get_file_links($file_id) {
  740. $out = array('download' => '', 'restore' => '', 'delete' => '');
  741. // Don't display the download/delete/restore ops if they are not available for this destination.
  742. $can_read = $this->can_read_file($file_id);
  743. $can_delete = $this->can_delete_file($file_id);
  744. $destination_id = $this->get_id();
  745. if ($can_read && user_access("access backup files")) {
  746. $out['download'] = l(t("download"), BACKUP_MIGRATE_MENU_PATH . "/destination/downloadfile/". $destination_id .'/'. $file_id);
  747. }
  748. if ($can_read && user_access("restore from backup")) {
  749. $out['restore'] = l(t("restore"), BACKUP_MIGRATE_MENU_PATH . "/destination/restorefile/". $destination_id .'/'. $file_id);
  750. }
  751. if ($can_delete && user_access("delete backup files")) {
  752. $out['delete'] = l(t("delete"), BACKUP_MIGRATE_MENU_PATH . "/destination/deletefile/". $destination_id .'/'. $file_id);
  753. }
  754. return $out;
  755. }
  756. /**
  757. * Determine if we can read the given file.
  758. */
  759. function can_read_file($file_id) {
  760. return $this->op('restore');
  761. }
  762. /**
  763. * Determine if we can read the given file.
  764. */
  765. function can_delete_file($file_id) {
  766. return $this->op('delete');
  767. }
  768. /**
  769. * Get the form for the settings for this destination type.
  770. */
  771. function settings_default() {
  772. return array();
  773. }
  774. /**
  775. * Get the form for the settings for this destination.
  776. */
  777. function settings_form($form) {
  778. return $form;
  779. }
  780. /**
  781. * Validate the form for the settings for this destination.
  782. */
  783. function settings_form_validate($form_values) {
  784. }
  785. /**
  786. * Submit the settings form. Any values returned will be saved.
  787. */
  788. function settings_form_submit($form_values) {
  789. return $form_values;
  790. }
  791. /**
  792. * Create a new destination of the correct type.
  793. */
  794. function create($params = array()) {
  795. $out = NULL;
  796. $types = backup_migrate_get_destination_types();
  797. // Get the type passed in in the params, or if none, check the url for a valid type name.
  798. // This is to allow new destination type to be specified in the path.
  799. $destination_type = !empty($params['type']) ? $params['type'] : arg(BACKUP_MIGRATE_MENU_DEPTH + 3);
  800. if ($destination_type && ($type = @$types[$destination_type])) {
  801. // Include the necessary file if specified by the type.
  802. if (!empty($type['file'])) {
  803. require_once './'. $type['file'];
  804. }
  805. $out = new $type['class']($params + array('destination_type' => $destination_type));
  806. }
  807. if (empty($out)) {
  808. $out = new backup_migrate_destination();
  809. }
  810. return $out;
  811. }
  812. /**
  813. * Add the menu items specific to the destination type.
  814. */
  815. function get_menu_items() {
  816. $items = parent::get_menu_items();
  817. $items[BACKUP_MIGRATE_MENU_PATH . '/destination/list/files'] = array(
  818. 'title' => 'Destination Files',
  819. 'page callback' => 'backup_migrate_menu_callback',
  820. 'page arguments' => array('destinations', 'backup_migrate_ui_destination_display_files', TRUE),
  821. 'access arguments' => array('access backup files'),
  822. 'type' => MENU_LOCAL_TASK,
  823. );
  824. $items[BACKUP_MIGRATE_MENU_PATH . '/destination/deletefile'] = array(
  825. 'title' => 'Delete File',
  826. 'description' => 'Delete a backup file',
  827. 'page callback' => 'backup_migrate_menu_callback',
  828. 'page arguments' => array('destinations', 'backup_migrate_ui_destination_delete_file', TRUE),
  829. 'access arguments' => array('delete backup files'),
  830. 'type' => MENU_CALLBACK,
  831. );
  832. $items[BACKUP_MIGRATE_MENU_PATH . '/destination/restorefile'] = array(
  833. 'title' => 'Restore from backup',
  834. 'description' => 'Restore database from a backup file on the server',
  835. 'page callback' => 'backup_migrate_menu_callback',
  836. 'page arguments' => array('destinations', 'backup_migrate_ui_destination_restore_file', TRUE),
  837. 'access arguments' => array('restore from backup'),
  838. 'type' => MENU_CALLBACK,
  839. );
  840. $items[BACKUP_MIGRATE_MENU_PATH . '/destination/downloadfile'] = array(
  841. 'title' => 'Download File',
  842. 'description' => 'Download a backup file',
  843. 'page callback' => 'backup_migrate_menu_callback',
  844. 'page arguments' => array('destinations', 'backup_migrate_ui_destination_download_file', TRUE),
  845. 'access arguments' => array('access backup files'),
  846. 'type' => MENU_CALLBACK,
  847. );
  848. return $items;
  849. }
  850. /**
  851. * Get the form for the settings for this filter.
  852. */
  853. function backup_settings_default() {
  854. return array();
  855. }
  856. /**
  857. * Get the form for the settings for this filter.
  858. */
  859. function backup_settings_form($settings) {
  860. return array();
  861. }
  862. /**
  863. * Get the form for the settings for this filter.
  864. */
  865. function backup_settings_form_validate($form, &$form_state) {
  866. }
  867. /**
  868. * Submit the settings form. Any values returned will be saved.
  869. */
  870. function backup_settings_form_submit($form, &$form_state) {
  871. }
  872. /**
  873. * Get the form for the settings for this filter.
  874. */
  875. function restore_settings_default() {
  876. return array();
  877. }
  878. /**
  879. * Get the form for the settings for this filter.
  880. */
  881. function restore_settings_form($settings) {
  882. return array();
  883. }
  884. /**
  885. * Get the form for the settings for this filter.
  886. */
  887. function restore_settings_form_validate($form_values) {
  888. }
  889. /**
  890. * Submit the settings form. Any values returned will be saved.
  891. */
  892. function restore_settings_form_submit($form_values) {
  893. return $form_values;
  894. }
  895. }
  896. /**
  897. * A base class for creating destinations.
  898. */
  899. class backup_migrate_destination_remote extends backup_migrate_destination {
  900. /**
  901. * The location is a URI so parse it and store the parts.
  902. */
  903. function get_location() {
  904. return $this->url(FALSE);
  905. }
  906. /**
  907. * The location to display is the url without the password.
  908. */
  909. function get_display_location() {
  910. return $this->url(TRUE);
  911. }
  912. /**
  913. * Return the location with the password.
  914. */
  915. function set_location($location) {
  916. $this->location = $location;
  917. $this->set_url($location);
  918. }
  919. /**
  920. * Get a url from the parts.
  921. */
  922. function url($hide_password = TRUE) {
  923. return $this->glue_url($this->dest_url, $hide_password);
  924. }
  925. /**
  926. * Glue a URLs component parts back into a URL.
  927. */
  928. function glue_url($parts, $hide_password = TRUE) {
  929. // Obscure the password if we need to.
  930. $parts['pass'] = $hide_password ? "" : $parts['pass'];
  931. // Assemble the URL.
  932. $out = "";
  933. $out .= $parts['scheme'] .'://';
  934. $out .= $parts['user'] ? urlencode($parts['user']) : '';
  935. $out .= ($parts['user'] && $parts['pass']) ? ":". urlencode($parts['pass']) : '';
  936. $out .= ($parts['user'] || $parts['pass']) ? "@" : "";
  937. $out .= $parts['host'];
  938. $out .= !empty($parts['port']) ? ':'. $parts['port'] : '';
  939. $out .= "/". $parts['path'];
  940. return $out;
  941. }
  942. /**
  943. * Break a URL into it's component parts.
  944. */
  945. function set_url($url) {
  946. $parts = (array)parse_url($url);
  947. $parts['user'] = urldecode(@$parts['user']);
  948. $parts['pass'] = urldecode(@$parts['pass']);
  949. $parts['path'] = urldecode(@$parts['path']);
  950. $parts['path'] = ltrim(@$parts['path'], "/");
  951. $this->dest_url = $parts;
  952. }
  953. /**
  954. * Destination configuration callback.
  955. */
  956. function edit_form() {
  957. $form = parent::edit_form();
  958. $form['scheme'] = array(
  959. "#type" => "value",
  960. "#title" => t("Scheme"),
  961. "#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : 'mysql',
  962. "#required" => TRUE,
  963. // "#options" => array($GLOBALS['db_type'] => $GLOBALS['db_type']),
  964. "#weight" => 0,
  965. );
  966. $form['host'] = array(
  967. "#type" => "textfield",
  968. "#title" => t("Host"),
  969. "#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost',
  970. "#required" => TRUE,
  971. "#weight" => 10,
  972. );
  973. $form['path'] = array(
  974. "#type" => "textfield",
  975. "#title" => t("Path"),
  976. "#default_value" => @$this->dest_url['path'],
  977. "#required" => TRUE,
  978. "#weight" => 20,
  979. );
  980. $form['user'] = array(
  981. "#type" => "textfield",
  982. "#title" => t("Username"),
  983. "#default_value" => @$this->dest_url['user'],
  984. "#required" => TRUE,
  985. "#weight" => 30,
  986. );
  987. $form['pass'] = array(
  988. "#type" => "password",
  989. "#title" => t("Password"),
  990. "#default_value" => @$this->dest_url['pass'],
  991. '#description' => '',
  992. "#weight" => 40,
  993. );
  994. if (@$this->dest_url['pass']) {
  995. $form['old_password'] = array(
  996. "#type" => "value",
  997. "#value" => @$this->dest_url['pass'],
  998. );
  999. $form['pass']["#description"] .= t(' You do not need to enter a password unless you wish to change the currently saved password.');
  1000. }
  1001. return $form;
  1002. }
  1003. /**
  1004. * Submit the configuration form. Glue the url together and add the old password back if a new one was not specified.
  1005. */
  1006. function edit_form_submit($form, &$form_state) {
  1007. $form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password'];
  1008. $form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE);
  1009. parent::edit_form_submit($form, $form_state);
  1010. }
  1011. }