locations.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. /**
  3. * @file
  4. * All of the location handling code needed for Backup and Migrate.
  5. */
  6. require_once dirname(__FILE__) . '/crud.inc';
  7. /**
  8. * Get the available location types.
  9. */
  10. function backup_migrate_get_location_subtypes() {
  11. require_once dirname(__FILE__) . '/crud.inc';
  12. require_once dirname(__FILE__) . '/destinations.inc';
  13. require_once dirname(__FILE__) . '/sources.inc';
  14. return backup_migrate_crud_subtypes('destination') + backup_migrate_crud_subtypes('source');
  15. }
  16. /**
  17. * Implements hook_backup_migrate_locations().
  18. *
  19. * Get the built in backup locations and those in the db.
  20. */
  21. function backup_migrate_backup_migrate_locations() {
  22. require_once dirname(__FILE__) . '/filters.inc';
  23. $out = array();
  24. // Allow the filter plugins to declare the default locations.
  25. $out += backup_migrate_filters_invoke_all('locations');
  26. return $out;
  27. }
  28. /**
  29. * Get all the available backup location.
  30. *
  31. * @param $op
  32. * The operation which will be performed on the location. Hooks can use this
  33. * to return only those locations appropriate for the given op.
  34. * Options include:
  35. * 'manual backup' - locations available for manual backup
  36. * 'scheduled backup' - locations available for schedules backup
  37. * 'list files' - locations whose backup files can be listed
  38. * 'restore' - locations whose files can be restored from
  39. * 'all' - all available locations should be returned
  40. */
  41. function backup_migrate_get_locations($op = 'all') {
  42. $locations = &drupal_static('backup_migrate_get_locations', NULL);
  43. // Get the list of locations and cache them locally.
  44. if ($locations === NULL) {
  45. $locations = backup_migrate_crud_get_items('location');
  46. }
  47. // Return all if that's what was asked for.
  48. if ($op == 'all') {
  49. return $locations;
  50. }
  51. // Return only those locations which support the given op.
  52. $out = array();
  53. if ($locations) {
  54. foreach ($locations as $key => $location) {
  55. if ($location->op($op)) {
  56. $out[$key] = $location;
  57. }
  58. }
  59. }
  60. return $out;
  61. }
  62. /**
  63. * Get the location of the given id.
  64. */
  65. function backup_migrate_get_location($id) {
  66. $locations = backup_migrate_get_locations('all');
  67. return empty($locations[$id]) ? NULL : $locations[$id];
  68. }
  69. /**
  70. * A base class for creating locations.
  71. */
  72. class backup_migrate_location extends backup_migrate_item {
  73. public $db_table = "backup_migrate_destinations";
  74. public $type_name = "location";
  75. public $default_values = array('settings' => array());
  76. public $singular = 'location';
  77. public $plural = 'locations';
  78. public $title_plural = 'Locations';
  79. public $title_singular = 'Location';
  80. public $subtype = "";
  81. public $supported_ops = array();
  82. /**
  83. * This function is not supposed to be called. It is just here to help the po extractor out.
  84. */
  85. public function strings() {
  86. // Help the pot extractor find these strings.
  87. t('location');
  88. t('locations');
  89. t('Location');
  90. t('Locations');
  91. }
  92. /**
  93. *
  94. */
  95. public function ops() {
  96. return $this->supported_ops;
  97. }
  98. /**
  99. * Does this location support the given operation.
  100. */
  101. public function op($op) {
  102. $ops = (array) $this->ops();
  103. return in_array($op, $ops);
  104. }
  105. /**
  106. * Remove the given op from the support list.
  107. */
  108. public function remove_op($op) {
  109. $key = array_search($op, $this->supported_ops);
  110. if ($key !== FALSE) {
  111. unset($this->supported_ops[$key]);
  112. }
  113. }
  114. /**
  115. *
  116. */
  117. public function get_name() {
  118. return @$this->name;
  119. }
  120. /**
  121. *
  122. */
  123. public function set_name($name) {
  124. return $this->name = $name;
  125. }
  126. /**
  127. *
  128. */
  129. public function set_location($location) {
  130. $this->location = $location;
  131. }
  132. /**
  133. *
  134. */
  135. public function get_location() {
  136. return @$this->location;
  137. }
  138. /**
  139. *
  140. */
  141. public function get_display_location() {
  142. return $this->get_location();
  143. }
  144. /**
  145. *
  146. */
  147. public function settings($key = NULL) {
  148. $out = $this->settings;
  149. if ($key) {
  150. $out = isset($out[$key]) ? $out[$key] : NULL;
  151. }
  152. return $out;
  153. }
  154. /**
  155. * Get the type name of this location for display to the user.
  156. */
  157. public function get_subtype_name() {
  158. if ($type = $this->get('subtype')) {
  159. $types = $this->location_types();
  160. return isset($types[$type]['type_name']) ? $types[$type]['type_name'] : $type;
  161. }
  162. }
  163. /**
  164. * Get the edit form for the item.
  165. */
  166. public function edit_form() {
  167. if (!empty($this->supported_ops)) {
  168. $form = parent::edit_form();
  169. $form['subtype'] = array(
  170. "#type" => "value",
  171. "#default_value" => $this->get('subtype'),
  172. );
  173. }
  174. else {
  175. $types = $this->location_types();
  176. $items = array();
  177. // If no (valid) node type has been provided, display a node type overview.
  178. foreach ($types as $key => $type) {
  179. if (@$type['can_create']) {
  180. $type_url_str = str_replace('_', '-', $key);
  181. $out = '<dt>' . l($type['type_name'], BACKUP_MIGRATE_MENU_PATH . "/settings/$this->type_name/add/$type_url_str", array('attributes' => array('title' => t('Add a new @s location.', array('@s' => $type['type_name']))))) . '</dt>';
  182. $out .= '<dd>' . filter_xss_admin($type['description']) . '</dd>';
  183. $items[] = $out;
  184. }
  185. }
  186. if (count($items)) {
  187. $output = t('Choose the type of location you would like to create:') . '<dl>' . implode('', $items) . '</dl>';
  188. }
  189. else {
  190. $output = t('No types available.');
  191. }
  192. $form['select_type'] = array(
  193. '#type' => 'markup',
  194. '#markup' => $output,
  195. );
  196. }
  197. return $form;
  198. }
  199. /**
  200. * Get the available location types.
  201. */
  202. public function location_types() {
  203. return backup_migrate_get_location_subtypes();
  204. }
  205. /**
  206. * Get the message to send to the user when confirming the deletion of the item.
  207. */
  208. public function delete_confirm_message() {
  209. return t('Are you sure you want to delete the %name?', array('%name' => $this->get_name()));
  210. }
  211. /**
  212. * Get the columns needed to list the type.
  213. */
  214. public function get_list_column_info() {
  215. $out = parent::get_list_column_info();
  216. $out = array(
  217. 'name' => array('title' => t('Name')),
  218. 'subtype_name' => array('title' => t('Type')),
  219. 'display_location' => array('title' => t('Location')),
  220. ) + $out;
  221. return $out;
  222. }
  223. /**
  224. * Get a row of data to be used in a list of items of this type.
  225. */
  226. public function get_list_row() {
  227. $out = parent::get_list_row();
  228. // Suppress locations with no actions as there's no value in showing them (and they may confuse new users).
  229. if (empty($out['actions'])) {
  230. return NULL;
  231. }
  232. return $out;
  233. }
  234. /**
  235. * Get the action links for a location.
  236. */
  237. public function get_action_links() {
  238. $out = parent::get_action_links();
  239. $item_id = $this->get_id();
  240. // Don't display the download/delete/restore ops if they are not available for this location.
  241. if ($this->op('list files') && user_access("access backup files")) {
  242. $out = array('list files' => l(t("list files"), BACKUP_MIGRATE_MENU_PATH . "/$this->type_name/list/files/" . $item_id)) + $out;
  243. }
  244. if (!$this->op('configure') || !user_access('administer backup and migrate')) {
  245. unset($out['edit']);
  246. }
  247. return $out;
  248. }
  249. /**
  250. * Determine if we can read the given file.
  251. */
  252. public function can_read_file($file_id) {
  253. return $this->op('restore');
  254. }
  255. /**
  256. * Get the form for the settings for this location type.
  257. */
  258. public function settings_default() {
  259. return array();
  260. }
  261. /**
  262. * Get the form for the settings for this location.
  263. */
  264. public function settings_form($form) {
  265. return $form;
  266. }
  267. /**
  268. * Validate the form for the settings for this location.
  269. */
  270. public function settings_form_validate($form_values) {
  271. }
  272. /**
  273. * Submit the settings form. Any values returned will be saved.
  274. */
  275. public function settings_form_submit($form_values) {
  276. return $form_values;
  277. }
  278. /**
  279. * Get the form for the settings for this filter.
  280. */
  281. public function backup_settings_default() {
  282. return array();
  283. }
  284. /**
  285. * Get the form for the settings for this filter.
  286. */
  287. public function backup_settings_form($settings) {
  288. return array();
  289. }
  290. /**
  291. * Get the form for the settings for this filter.
  292. */
  293. public function backup_settings_form_validate($form, &$form_state) {
  294. }
  295. /**
  296. * Submit the settings form. Any values returned will be saved.
  297. */
  298. public function backup_settings_form_submit($form, &$form_state) {
  299. }
  300. /**
  301. * Get the form for the settings for this filter.
  302. */
  303. public function restore_settings_default() {
  304. return array();
  305. }
  306. /**
  307. * Get the form for the settings for this filter.
  308. */
  309. public function restore_settings_form($settings) {
  310. return array();
  311. }
  312. /**
  313. * Get the form for the settings for this filter.
  314. */
  315. public function restore_settings_form_validate($form_values) {
  316. }
  317. /**
  318. * Submit the settings form. Any values returned will be saved.
  319. */
  320. public function restore_settings_form_submit($form_values) {
  321. return $form_values;
  322. }
  323. /**
  324. * Create a new location of the correct type.
  325. */
  326. public function create($params = array()) {
  327. $out = NULL;
  328. $types = backup_migrate_get_location_subtypes();
  329. // Get the type passed in in the params, or if none, check the url for a valid type name.
  330. // This is to allow new location type to be specified in the path.
  331. $location_type = !empty($params['subtype']) ? $params['subtype'] : NULL;
  332. if ($location_type && ($type = @$types[$location_type])) {
  333. // Include the necessary file if specified by the type.
  334. if (!empty($type['file'])) {
  335. require_once './' . $type['file'];
  336. }
  337. $out = new $type['class']($params + array('subtype' => $location_type));
  338. }
  339. if (empty($out)) {
  340. $out = parent::create($params);
  341. }
  342. return $out;
  343. }
  344. /**
  345. * Get a url from the parts.
  346. */
  347. public function url($hide_password = TRUE) {
  348. return $this->glue_url($this->dest_url, $hide_password);
  349. }
  350. /**
  351. * Glue a URLs component parts back into a URL.
  352. */
  353. public function glue_url($parts, $hide_password = TRUE) {
  354. // Obscure the password if we need to.
  355. $parts['pass'] = $hide_password ? "" : $parts['pass'];
  356. // Assemble the URL.
  357. $out = "";
  358. $out .= $parts['scheme'] . '://';
  359. $out .= $parts['user'] ? urlencode($parts['user']) : '';
  360. $out .= ($parts['user'] && $parts['pass']) ? ":" . urlencode($parts['pass']) : '';
  361. $out .= ($parts['user'] || $parts['pass']) ? "@" : "";
  362. $out .= $parts['host'];
  363. $out .= !empty($parts['port']) ? ':' . $parts['port'] : '';
  364. $out .= "/" . $parts['path'];
  365. return $out;
  366. }
  367. /**
  368. * Break a URL into it's component parts.
  369. */
  370. public function set_url($url) {
  371. $parts = (array) parse_url($url);
  372. $parts['user'] = urldecode(@$parts['user']);
  373. $parts['pass'] = urldecode(@$parts['pass']);
  374. $parts['path'] = urldecode(@$parts['path']);
  375. $parts['path'] = ltrim(@$parts['path'], "/");
  376. $this->dest_url = $parts;
  377. }
  378. /**
  379. * Retrieve a list of filetypes supported by this source/destination.
  380. */
  381. public function file_types() {
  382. return array();
  383. }
  384. }
  385. /**
  386. * A base class for creating locations.
  387. */
  388. class backup_migrate_location_remote extends backup_migrate_location {
  389. /**
  390. * The location is a URI so parse it and store the parts.
  391. */
  392. public function get_location() {
  393. return $this->url(FALSE);
  394. }
  395. /**
  396. * The location to display is the url without the password.
  397. */
  398. public function get_display_location() {
  399. return $this->url(TRUE);
  400. }
  401. /**
  402. * Return the location with the password.
  403. */
  404. public function set_location($location) {
  405. $this->location = $location;
  406. $this->set_url($location);
  407. }
  408. /**
  409. * Location configuration callback.
  410. */
  411. public function edit_form() {
  412. $form = parent::edit_form();
  413. $form['scheme'] = array(
  414. "#type" => "select",
  415. "#title" => t("Scheme"),
  416. "#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : 'mysql',
  417. "#required" => TRUE,
  418. "#options" => array($GLOBALS['db_type'] => $GLOBALS['db_type']),
  419. "#weight" => 0,
  420. );
  421. $form['host'] = array(
  422. "#type" => "textfield",
  423. "#title" => t("Host"),
  424. "#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost',
  425. "#required" => TRUE,
  426. "#weight" => 10,
  427. );
  428. $form['path'] = array(
  429. "#type" => "textfield",
  430. "#title" => t("Path"),
  431. "#default_value" => @$this->dest_url['path'],
  432. "#required" => TRUE,
  433. "#weight" => 20,
  434. );
  435. $form['user'] = array(
  436. "#type" => "textfield",
  437. "#title" => t("Username"),
  438. "#default_value" => @$this->dest_url['user'],
  439. "#required" => TRUE,
  440. "#weight" => 30,
  441. );
  442. $form['pass'] = array(
  443. "#type" => "password",
  444. "#title" => t("Password"),
  445. "#default_value" => @$this->dest_url['pass'],
  446. '#description' => '',
  447. "#weight" => 40,
  448. );
  449. if (@$this->dest_url['pass']) {
  450. $form['old_password'] = array(
  451. "#type" => "value",
  452. "#value" => @$this->dest_url['pass'],
  453. );
  454. $form['pass']["#description"] .= t(' You do not need to enter a password unless you wish to change the currently saved password.');
  455. }
  456. return $form;
  457. }
  458. /**
  459. * Submit the configuration form. Glue the url together and add the old password back if a new one was not specified.
  460. */
  461. public function edit_form_submit($form, &$form_state) {
  462. $form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password'];
  463. $form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE);
  464. parent::edit_form_submit($form, $form_state);
  465. }
  466. }