locations.inc 13 KB

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