sources.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * @file
  4. * All of the source handling code needed for Backup and Migrate.
  5. */
  6. require_once dirname(__FILE__) . '/crud.inc';
  7. require_once dirname(__FILE__) . '/destinations.inc';
  8. require_once dirname(__FILE__) . '/locations.inc';
  9. /**
  10. * Get all the available backup sources.
  11. */
  12. function backup_migrate_get_sources() {
  13. return backup_migrate_crud_get_items('source');
  14. }
  15. /**
  16. * Get the available source types.
  17. */
  18. function backup_migrate_get_source_subtypes() {
  19. return backup_migrate_crud_subtypes('source');
  20. }
  21. /**
  22. * Get the destination of the given id.
  23. */
  24. function backup_migrate_get_source($id) {
  25. $sources = backup_migrate_get_sources();
  26. return empty($sources[$id]) ? NULL : $sources[$id];
  27. }
  28. /**
  29. * Create a source object of the given type with the given params.
  30. */
  31. function backup_migrate_create_source($subtype, $params = array()) {
  32. $params['subtype'] = $subtype;
  33. return backup_migrate_crud_create_item('source', $params);
  34. }
  35. /**
  36. * Implements hook_backup_migrate_source_subtypes().
  37. *
  38. * Get the built in Backup and Migrate source types.
  39. */
  40. function backup_migrate_backup_migrate_source_subtypes() {
  41. $out = array();
  42. $out += array(
  43. 'db' => array(
  44. 'type_name' => t('Database'),
  45. 'description' => t('Import the backup directly into another database. Database sources can also be used as a source to backup from.'),
  46. 'file' => drupal_get_path('module', 'backup_migrate') . '/includes/sources.db.inc',
  47. 'class' => 'backup_migrate_source_db',
  48. 'can_create' => FALSE,
  49. ),
  50. 'mysql' => array(
  51. 'type_name' => t('MySQL Database'),
  52. 'description' => t('Import the backup directly into another MySQL database. Database sources can also be used as a source to backup from.'),
  53. 'file' => drupal_get_path('module', 'backup_migrate') . '/includes/sources.db.mysql.inc',
  54. 'class' => 'backup_migrate_source_db_mysql',
  55. 'can_create' => TRUE,
  56. ),
  57. 'filesource' => array(
  58. 'description' => t('A files directory which can be backed up from.'),
  59. 'file' => drupal_get_path('module', 'backup_migrate') . '/includes/sources.filesource.inc',
  60. 'class' => 'backup_migrate_destination_filesource',
  61. 'type_name' => t('File Directory'),
  62. 'can_create' => TRUE,
  63. ),
  64. 'archive' => array(
  65. 'description' => t('Create an archive of your entire site.'),
  66. 'file' => drupal_get_path('module', 'backup_migrate') . '/includes/sources.archivesource.inc',
  67. 'class' => 'backup_migrate_files_destination_archivesource',
  68. 'type_name' => t('Site Archive'),
  69. 'can_create' => FALSE,
  70. ),
  71. );
  72. return $out;
  73. }
  74. /**
  75. * Implements hook_backup_migrate_sources().
  76. *
  77. * Get the built in backup sources and those in the db.
  78. */
  79. function backup_migrate_backup_migrate_sources() {
  80. require_once dirname(__FILE__) . '/filters.inc';
  81. // Expose the configured databases as sources.
  82. $out = backup_migrate_filters_invoke_all('sources');
  83. return $out;
  84. }
  85. /**
  86. * Get the source options as a form element.
  87. */
  88. function _backup_migrate_get_source_form($source_id = 'db') {
  89. require_once dirname(__FILE__) . '/destinations.inc';
  90. $form = array();
  91. $sources = _backup_migrate_get_source_pulldown($source_id);
  92. if (count($sources['#options']) > 1) {
  93. $form['source'] = array(
  94. "#type" => "fieldset",
  95. "#title" => t("Backup Source"),
  96. "#collapsible" => TRUE,
  97. "#collapsed" => FALSE,
  98. "#tree" => FALSE,
  99. );
  100. $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.");
  101. $form['source']['source_id'] = $sources;
  102. }
  103. else {
  104. $form = array();
  105. $form['source']['source_id'] = array(
  106. "#type" => "value",
  107. "#value" => $source_id,
  108. );
  109. }
  110. return $form;
  111. }
  112. /**
  113. * Get pulldown to select existing source options.
  114. */
  115. function _backup_migrate_get_source_pulldown($source_id = NULL) {
  116. $sources = _backup_migrate_get_source_form_item_options();
  117. $form = array(
  118. "#type" => "select",
  119. "#title" => t("Backup Source"),
  120. "#options" => _backup_migrate_get_source_form_item_options(),
  121. "#default_value" => $source_id,
  122. );
  123. return $form;
  124. }
  125. /**
  126. * Get the location options as an options array for a form item.
  127. */
  128. function _backup_migrate_get_source_form_item_options() {
  129. $out = array();
  130. foreach (backup_migrate_get_sources() as $key => $location) {
  131. $out[$key] = $location->get_name();
  132. }
  133. return $out;
  134. }
  135. /**
  136. * A base class for creating sources.
  137. */
  138. class backup_migrate_source extends backup_migrate_location {
  139. public $db_table = "backup_migrate_sources";
  140. public $type_name = 'source';
  141. public $singular = 'source';
  142. public $plural = 'sources';
  143. public $title_plural = 'Sources';
  144. public $title_singular = 'Source';
  145. /**
  146. * This function is not supposed to be called.
  147. *
  148. * It is just here to help out the po extractor.
  149. */
  150. public function strings() {
  151. // Help the pot extractor find these strings.
  152. t('source');
  153. t('sources');
  154. t('Sources');
  155. t('Source');
  156. }
  157. /**
  158. * Get the available location types.
  159. */
  160. public function location_types() {
  161. return backup_migrate_get_source_subtypes();
  162. }
  163. }
  164. /**
  165. * A base class for creating sources.
  166. */
  167. class backup_migrate_source_remote extends backup_migrate_source {
  168. /**
  169. * The location is a URI so parse it and store the parts.
  170. */
  171. public function get_location() {
  172. return $this->url(FALSE);
  173. }
  174. /**
  175. * The location to display is the url without the password.
  176. */
  177. public function get_display_location() {
  178. return $this->url(TRUE);
  179. }
  180. /**
  181. * Returns the location with the password.
  182. */
  183. public function set_location($location) {
  184. $this->location = $location;
  185. $this->set_url($location);
  186. }
  187. /**
  188. * Source configuration callback.
  189. */
  190. public function edit_form() {
  191. $form = parent::edit_form();
  192. $form['scheme'] = array(
  193. "#type" => "textfield",
  194. "#title" => t("Scheme"),
  195. "#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : '',
  196. "#required" => TRUE,
  197. "#weight" => 0,
  198. );
  199. $form['host'] = array(
  200. "#type" => "textfield",
  201. "#title" => t("Host"),
  202. "#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost',
  203. "#required" => TRUE,
  204. "#weight" => 10,
  205. );
  206. $form['path'] = array(
  207. "#type" => "textfield",
  208. "#title" => t("Path"),
  209. "#default_value" => @$this->dest_url['path'],
  210. "#required" => TRUE,
  211. "#weight" => 20,
  212. );
  213. $form['user'] = array(
  214. "#type" => "textfield",
  215. "#title" => t("Username"),
  216. "#default_value" => @$this->dest_url['user'],
  217. "#required" => TRUE,
  218. "#weight" => 30,
  219. );
  220. $form['pass'] = array(
  221. "#type" => "password",
  222. "#title" => t("Password"),
  223. "#default_value" => @$this->dest_url['pass'],
  224. '#description' => '',
  225. "#weight" => 40,
  226. );
  227. if (@$this->dest_url['pass']) {
  228. $form['old_password'] = array(
  229. "#type" => "value",
  230. "#value" => @$this->dest_url['pass'],
  231. );
  232. $form['pass']["#description"] .= t('You do not need to enter a password unless you wish to change the currently saved password.');
  233. }
  234. return $form;
  235. }
  236. /**
  237. * Submits the configuration form.
  238. *
  239. * Glue the url together and add the old password back if a new one was not
  240. * specified.
  241. */
  242. public function edit_form_submit($form, &$form_state) {
  243. $form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password'];
  244. $form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE);
  245. parent::edit_form_submit($form, $form_state);
  246. }
  247. }