destinations.inc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. <?php
  2. /**
  3. * @file
  4. * All of the destination handling code needed for Backup and Migrate.
  5. */
  6. require_once dirname(__FILE__) . '/crud.inc';
  7. require_once dirname(__FILE__) . '/locations.inc';
  8. /**
  9. * Get the available destination types.
  10. */
  11. function backup_migrate_get_destination_subtypes() {
  12. return backup_migrate_crud_subtypes('destination');
  13. }
  14. /**
  15. * Implements hook_backup_migrate_destination_subtypes().
  16. */
  17. function backup_migrate_backup_migrate_destination_subtypes() {
  18. // Get the built-in Backup and 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 this 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. 'local' => TRUE,
  28. 'can_create' => TRUE,
  29. ),
  30. 'file_manual' => array(
  31. 'file' => drupal_get_path('module', 'backup_migrate') . '/includes/destinations.file.inc',
  32. 'type_name' => t('Server Directory'),
  33. 'class' => 'backup_migrate_destination_files_manual',
  34. ),
  35. 'file_scheduled' => array(
  36. 'file' => drupal_get_path('module', 'backup_migrate') . '/includes/destinations.file.inc',
  37. 'type_name' => t('Server Directory'),
  38. 'class' => 'backup_migrate_destination_files_scheduled',
  39. ),
  40. );
  41. }
  42. $out += array(
  43. 'browser_download' => array(
  44. 'file' => drupal_get_path('module', 'backup_migrate') . '/includes/destinations.browser.inc',
  45. 'class' => 'backup_migrate_destination_browser_download',
  46. ),
  47. 'browser_upload' => array(
  48. 'file' => drupal_get_path('module', 'backup_migrate') . '/includes/destinations.browser.inc',
  49. 'class' => 'backup_migrate_destination_browser_upload',
  50. ),
  51. 'ftp' => array(
  52. 'description' => t('Save the backup files to any a directory on an FTP server.'),
  53. 'file' => drupal_get_path('module', 'backup_migrate') . '/includes/destinations.ftp.inc',
  54. 'class' => 'backup_migrate_destination_ftp',
  55. 'type_name' => t('FTP Directory'),
  56. 'can_create' => TRUE,
  57. 'remote' => TRUE,
  58. ),
  59. 's3' => array(
  60. 'description' => t('Save the backup files to a bucket on your <a href="@link" target="_blank">Amazon S3 account</a>.', array('@link' => url('http://aws.amazon.com/s3/'))),
  61. 'file' => drupal_get_path('module', 'backup_migrate') . '/includes/destinations.s3.inc',
  62. 'class' => 'backup_migrate_destination_s3',
  63. 'type_name' => t('Amazon S3 Bucket'),
  64. 'can_create' => TRUE,
  65. 'remote' => TRUE,
  66. ),
  67. 'email' => array(
  68. 'type_name' => t('Email'),
  69. 'description' => t('Send the backup as an email attachment to the specified email address.'),
  70. 'file' => drupal_get_path('module', 'backup_migrate') . '/includes/destinations.email.inc',
  71. 'class' => 'backup_migrate_destination_email',
  72. 'can_create' => TRUE,
  73. 'remote' => TRUE,
  74. ),
  75. );
  76. return $out;
  77. }
  78. /**
  79. * Implements hook_backup_migrate_destinations().
  80. */
  81. function backup_migrate_backup_migrate_destinations() {
  82. require_once dirname(__FILE__) . '/filters.inc';
  83. // Get the built in backup destinations and those in the db.
  84. $out = array();
  85. // Add the default, out of the box destinations for new users.
  86. if (variable_get('backup_migrate_allow_backup_to_file', TRUE)) {
  87. if (variable_get('file_private_path', FALSE)) {
  88. $out['manual'] = backup_migrate_create_destination('file_manual', array('machine_name' => 'manual'));
  89. $out['scheduled'] = backup_migrate_create_destination('file_scheduled', array('machine_name' => 'scheduled'));
  90. }
  91. else {
  92. _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');
  93. }
  94. }
  95. // Add the browser destination for downloading to the desktop.
  96. if (variable_get('backup_migrate_allow_backup_to_download', TRUE)) {
  97. $out['download'] = backup_migrate_create_destination('browser_download');
  98. }
  99. $out['upload'] = backup_migrate_create_destination('browser_upload');
  100. // Expose the configured databases as sources.
  101. $out += backup_migrate_filters_invoke_all('destinations');
  102. return $out;
  103. }
  104. /**
  105. * Get all the available backup destination.
  106. *
  107. * @param $op
  108. * The operation which will be performed on the destination. Hooks can use this
  109. * to return only those destinations appropriate for the given op.
  110. * Options include:
  111. * 'manual backup' - destinations available for manual backup
  112. * 'scheduled backup' - destinations available for schedules backup
  113. * 'list files' - destinations whose backup files can be listed
  114. * 'restore' - destinations whose files can be restored from
  115. * 'all' - all available destinations should be returned
  116. */
  117. function backup_migrate_get_destinations($op = 'all') {
  118. $destinations = &drupal_static('backup_migrate_get_destinations', NULL);
  119. // Get the list of destinations and cache them locally.
  120. if ($destinations === NULL) {
  121. $destinations = backup_migrate_crud_get_items('destination');
  122. }
  123. // Return all if that's what was asked for.
  124. if ($op == 'all') {
  125. return $destinations;
  126. }
  127. // Return only those destinations which support the given op.
  128. $out = array();
  129. foreach ($destinations as $key => $destination) {
  130. if ($destination->op($op)) {
  131. $out[$key] = $destination;
  132. }
  133. }
  134. return $out;
  135. }
  136. /**
  137. * Get the destination of the given id.
  138. */
  139. function backup_migrate_get_destination($id) {
  140. $destinations = backup_migrate_get_destinations('all');
  141. return empty($destinations[$id]) ? NULL : $destinations[$id];
  142. }
  143. /**
  144. * Create a destination object of the given type with the given params.
  145. */
  146. function backup_migrate_create_destination($destination_type, $params = array()) {
  147. $params['subtype'] = $destination_type;
  148. return backup_migrate_crud_create_item('destination', $params);
  149. }
  150. /**
  151. * Load a file from a destination and return the file info.
  152. */
  153. function backup_migrate_destination_get_file($destination_id, $file_id) {
  154. if ($destination = backup_migrate_get_destination($destination_id)) {
  155. return $destination->load_file($file_id);
  156. }
  157. return NULL;
  158. }
  159. /**
  160. * Load a file from a destination and return the file info.
  161. */
  162. function backup_migrate_destination_get_latest_file($destination_id) {
  163. $out = NULL;
  164. if ($destination = backup_migrate_get_destination($destination_id)) {
  165. $files = $destination->list_files();
  166. $max = 0;
  167. foreach ((array) $files as $file) {
  168. $info = $file->info();
  169. // If there's a datestamp, it should override the filetime as it's probably more reliable.
  170. $time = !empty($info['datestamp']) ? $info['datestamp'] : $info['filetime'];
  171. if ($time > $max) {
  172. $max = $time;
  173. $out = $file;
  174. }
  175. }
  176. }
  177. return $out;
  178. }
  179. /**
  180. * Check if a file exists in the given destination.
  181. */
  182. function backup_migrate_destination_file_exists($destination_id, $file_id) {
  183. if ($destination = backup_migrate_get_destination($destination_id)) {
  184. return $destination->file_exists($file_id);
  185. }
  186. return NULL;
  187. }
  188. /**
  189. * Send a file to the destination specified by the settings array.
  190. */
  191. function backup_migrate_destination_confirm_destination(&$settings) {
  192. if ($destinations = $settings->get_destinations()) {
  193. foreach ($destinations as $key => $destination) {
  194. // Check that the destination is ready to go.
  195. if (!$destination->confirm_destination()) {
  196. unset($destinations[$key]);
  197. }
  198. }
  199. }
  200. $settings->destinations = $destinations;
  201. return count($destinations);
  202. }
  203. /**
  204. * Send a file to the destination specified by the settings array.
  205. */
  206. function backup_migrate_destination_save_file($file, &$settings) {
  207. $saved_to = array();
  208. if ($destinations = $settings->get_destinations()) {
  209. foreach ($destinations as $destination) {
  210. // Make sure the file only gets saved to each destination once.
  211. $id = $destination->get('id');
  212. if (!in_array($id, $saved_to)) {
  213. if ($destination->save_file($file, $settings)) {
  214. $saved_to[] = $id;
  215. }
  216. }
  217. }
  218. }
  219. return $saved_to ? $file : FALSE;
  220. }
  221. /**
  222. * Delete a file in the given destination.
  223. */
  224. function backup_migrate_destination_delete_file($destination_id, $file_id) {
  225. if ($destination = backup_migrate_get_destination($destination_id)) {
  226. return $destination->delete_file($file_id);
  227. }
  228. }
  229. /**
  230. * Get the action links for a file on a given destination.
  231. */
  232. function _backup_migrate_destination_get_file_links($destination_id, $file_id) {
  233. $out = array();
  234. if ($destination = backup_migrate_get_destination($destination_id)) {
  235. $out = $destination->get_file_links($file_id);
  236. }
  237. return $out;
  238. }
  239. /* UI Menu Callbacks */
  240. /**
  241. * List the backup files in the given destination.
  242. */
  243. function backup_migrate_ui_destination_display_files($destination_id = NULL) {
  244. drupal_add_css(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.css');
  245. $rows = $sort = array();
  246. if ($destination = backup_migrate_get_destination($destination_id)) {
  247. drupal_set_title(t('@title Files', array('@title' => $destination->get_name())));
  248. return _backup_migrate_ui_destination_display_files($destination, 20, TRUE);
  249. }
  250. drupal_goto(BACKUP_MIGRATE_MENU_PATH . "/destination");
  251. }
  252. /**
  253. * List the backup files in the given destination.
  254. */
  255. function _backup_migrate_ui_destination_display_files($destination = NULL, $limit = NULL, $show_pager = FALSE) {
  256. if ($destination) {
  257. // Refresh the file listing cache if requested.
  258. if (isset($_GET['refresh'])) {
  259. $destination->file_cache_clear();
  260. drupal_goto($_GET['q']);
  261. }
  262. $files = $destination->list_files();
  263. $fetch = $out = '';
  264. // Get the fetch link.
  265. if ($destination->cache_files && $destination->fetch_time) {
  266. $fetch = '<div class="description">' . 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>';
  267. }
  268. $out .= $fetch;
  269. $out .= _backup_migrate_ui_destination_display_file_list($files, array('limit' => 20, 'pager' => TRUE));
  270. $out .= $fetch;
  271. return $out;
  272. }
  273. }
  274. /**
  275. * List the backup files in the given destination.
  276. */
  277. function _backup_migrate_ui_destination_display_file_list($files, $options = array()) {
  278. drupal_add_css(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.css');
  279. // Set some default options.
  280. $options += array(
  281. 'pager' => TRUE,
  282. 'more' => FALSE,
  283. 'operations' => TRUE,
  284. 'limit' => NULL,
  285. 'form_elements' => NULL,
  286. );
  287. $rows = $sort = array();
  288. if ($files) {
  289. $headers = array(
  290. array('data' => 'Filename', 'field' => 'filename'),
  291. array('data' => 'Created', 'field' => 'filetime', 'sort' => 'desc'),
  292. array('data' => 'Size', 'field' => 'filesize'),
  293. );
  294. if ($options['operations']) {
  295. $headers[] = array('data' => 'Operations');
  296. }
  297. if ($options['form_elements']) {
  298. array_unshift($headers, '');
  299. }
  300. $sort_order = tablesort_get_order($headers);
  301. $sort_key = $sort_order['sql'] ? $sort_order['sql'] : 'filetime';
  302. $sort_dir = tablesort_get_sort($headers) == 'desc' ? SORT_DESC : SORT_ASC;
  303. $i = 0;
  304. foreach ((array) $files as $id => $file) {
  305. $info = $file->info();
  306. // If there's a datestamp, it should override the filetime as it's probably more reliable.
  307. $info['filetime'] = !empty($info['datestamp']) ? $info['datestamp'] : $info['filetime'];
  308. $description = '';
  309. // Add the description as a new row.
  310. if (!empty($info['description'])) {
  311. $description .= ' <div title="' . check_plain($info['description']) . '" class="backup-migrate-description">' . check_plain($info['description']) . '</div>';
  312. }
  313. // Add the backup source.
  314. if (!empty($info['bam_sourcename'])) {
  315. $description .= ' <div title="' . check_plain($info['bam_sourcename']) . '" class="backup-migrate-tags"><span class="backup-migrate-label">' . t('Source:') . ' </span>' . check_plain($info['bam_sourcename']) . '</div>';
  316. }
  317. // Add the tags as a new row.
  318. if (!empty($info['tags'])) {
  319. $tags = check_plain(implode(', ', (array) $info['tags']));
  320. $description .= ' <div title="' . $tags . '" class="backup-migrate-tags"><span class="backup-migrate-label">' . t('Tags:') . ' </span>' . $tags . '</div>';
  321. }
  322. // Add the other info.
  323. if (!empty($info['bam_other_safe'])) {
  324. foreach ($info['bam_other_safe'] as $label => $data) {
  325. $description .= ' <div class="backup-migrate-tags"><span class="backup-migrate-label">' . $label . ' </span>' . $data . '</div>';
  326. }
  327. }
  328. // Show only files that can be restored from.
  329. $sort[] = $info[$sort_key];
  330. $row = array(
  331. check_plain($info['filename']) . $description,
  332. t('!time ago', array(
  333. '!time' => format_interval(time() - $info['filetime'], 2),
  334. ))
  335. . '<div class="backup-migrate-date">'
  336. . format_date($info['filetime'], 'small')
  337. . '</div>',
  338. format_size($info['filesize']),
  339. );
  340. if ($options['operations']) {
  341. $row[] = array(
  342. 'data' => implode(' &nbsp; ', $file->destination->get_file_links($file->file_id())),
  343. 'class' => 'backup-migrate-actions',
  344. );
  345. }
  346. if (isset($options['form_elements'][$id])) {
  347. array_unshift($row, $options['form_elements'][$id]);
  348. }
  349. $rows[] = $row;
  350. }
  351. array_multisort($sort, $sort_dir, $rows);
  352. // Show only some of them if it's limited.
  353. $showing = $pager = $more = '';
  354. if ($options['limit']) {
  355. $limit = $options['limit'];
  356. $total = count($rows);
  357. $end = $limit;
  358. $start = 0;
  359. if ($options['pager']) {
  360. $page = isset($_GET['page']) ? intval($_GET['page']) : 0;
  361. $start = $page * $limit;
  362. $element = 0;
  363. $GLOBALS['pager_total'][$element] = ceil($total / $limit);
  364. $GLOBALS['pager_page_array'][$element] = $page;
  365. $tags = array(
  366. t('« newest'),
  367. t('« newer'),
  368. '',
  369. t('older »'),
  370. t('oldest »'),
  371. );
  372. $pager = theme('pager', $tags, $limit, $element, array(), ceil($total / $limit));
  373. $end = min($total, $start + $limit);
  374. }
  375. if ($total > $limit && $options['more']) {
  376. $more = ' ' . l(t('view all'), $options['more']);
  377. }
  378. $showing = t('Showing @start to @end of @total files.', array(
  379. '@start' => $start + 1,
  380. '@end' => $end,
  381. '@total' => $total,
  382. ));
  383. // Limit the number of rows shown.
  384. $rows = array_slice($rows, $start, $limit, TRUE);
  385. }
  386. $out = theme('table', array(
  387. 'header' => $headers,
  388. 'rows' => $rows,
  389. 'class' => 'backup-migrate-listing backup-migrate-listing-files',
  390. ));
  391. $out .= $showing;
  392. $out .= $pager;
  393. $out .= $more;
  394. }
  395. else {
  396. $out = t('There are no backup files to display.');
  397. }
  398. return $out;
  399. }
  400. /**
  401. * List the backup files in the given destination.
  402. */
  403. function _backup_migrate_ui_destination_display_file_list_options($files, $limit = NULL) {
  404. drupal_add_css(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.css');
  405. $rows = $sort = array();
  406. if ($files) {
  407. $headers = array(
  408. '',
  409. array('data' => 'Filename', 'field' => 'filename'),
  410. array('data' => 'Created', 'field' => 'filetime', 'sort' => 'desc'),
  411. array('data' => 'Size', 'field' => 'filesize'),
  412. );
  413. $sort_order = tablesort_get_order($headers);
  414. $sort_key = $sort_order['sql'] ? $sort_order['sql'] : 'filetime';
  415. $sort_dir = tablesort_get_sort($headers) == 'desc' ? SORT_DESC : SORT_ASC;
  416. $i = 0;
  417. foreach ((array) $files as $file) {
  418. $info = $file->info();
  419. // If there's a datestamp, it should override the filetime as it's
  420. // probably more reliable.
  421. $info['filetime'] = !empty($info['datestamp']) ? $info['datestamp'] : $info['filetime'];
  422. $description = '';
  423. // Add the description as a new row.
  424. if (!empty($info['description'])) {
  425. $description .= ' <div title="' . check_plain($info['description']) . '" class="backup-migrate-description">' . check_plain($info['description']) . '</div>';
  426. }
  427. // Add the backup source.
  428. if (!empty($info['bam_sourcename'])) {
  429. $description .= ' <div title="' . check_plain($info['bam_sourcename']) . '" class="backup-migrate-tags"><span class="backup-migrate-label">' . t('Source:') . ' </span>' . check_plain($info['bam_sourcename']) . '</div>';
  430. }
  431. // Add the tags as a new row.
  432. if (!empty($info['tags'])) {
  433. $tags = check_plain(implode(', ', (array) $info['tags']));
  434. $description .= ' <div title="' . $tags . '" class="backup-migrate-tags"><span class="backup-migrate-label">' . t('Tags:') . ' </span>' . $tags . '</div>';
  435. }
  436. // Add the other info.
  437. if (!empty($info['bam_other_safe'])) {
  438. foreach ($info['bam_other_safe'] as $label => $data) {
  439. $description .= ' <div class="backup-migrate-tags"><span class="backup-migrate-label">' . $label . ' </span>' . $data . '</div>';
  440. }
  441. }
  442. // Show only files that can be restored from.
  443. $sort[] = $info[$sort_key];
  444. $rows[] = array(
  445. 'radio',
  446. check_plain($info['filename']) . $description,
  447. t('!time ago', array(
  448. '!time' => format_interval(time() - $info['filetime'], 2),
  449. ))
  450. . '<div class="backup-migrate-date">'
  451. . format_date($info['filetime'], 'small')
  452. . '</div>',
  453. format_size($info['filesize']),
  454. );
  455. }
  456. array_multisort($sort, $sort_dir, $rows);
  457. // Show only some of them if it's limited.
  458. $showing = $pager = '';
  459. if ($limit) {
  460. $total = count($rows);
  461. $end = $limit;
  462. $start = 0;
  463. $showing = t('Showing @start to @end of @total files.', array(
  464. '@start' => $start + 1,
  465. '@end' => $end + 1,
  466. '@total' => $total,
  467. ));
  468. // Limit the number of rows shown.
  469. $rows = array_slice($rows, $start, $limit, TRUE);
  470. }
  471. $out = theme('table', array(
  472. 'header' => $headers,
  473. 'rows' => $rows,
  474. 'class' => 'backup-migrate-listing backup-migrate-listing-files',
  475. ));
  476. $out .= $showing;
  477. }
  478. else {
  479. $out = t('There are no backup files to display.');
  480. }
  481. return $out;
  482. }
  483. /**
  484. * Download a file to the browser.
  485. */
  486. function backup_migrate_ui_destination_download_file($destination_id = NULL, $file_id = NULL) {
  487. if ($file = backup_migrate_destination_get_file($destination_id, $file_id)) {
  488. require_once dirname(__FILE__) . '/files.inc';
  489. $file->transfer();
  490. }
  491. drupal_goto(BACKUP_MIGRATE_MENU_PATH);
  492. }
  493. /**
  494. * Restore a backup file from a destination.
  495. */
  496. function backup_migrate_ui_destination_restore_file($destination_id = NULL, $file_id = NULL) {
  497. if (backup_migrate_destination_file_exists($destination_id, $file_id)) {
  498. return drupal_get_form('backup_migrate_ui_destination_restore_file_confirm', $destination_id, $file_id);
  499. }
  500. _backup_migrate_message('Cannot restore from the the file: %file_id because it does not exist.', array('%file_id' => $file_id), 'error');
  501. if ($destination_id && user_access('access backup files')) {
  502. drupal_goto(BACKUP_MIGRATE_MENU_PATH . '/destination/list/files/' . $destination_id);
  503. }
  504. drupal_goto(BACKUP_MIGRATE_MENU_PATH);
  505. }
  506. /**
  507. * Ask confirmation for file restore.
  508. */
  509. function backup_migrate_ui_destination_restore_file_confirm($form, &$form_state, $destination_id, $file_id) {
  510. $sources = _backup_migrate_get_source_form_item_options();
  511. if (count($sources) > 1) {
  512. $form['source_id'] = array(
  513. "#type" => "select",
  514. "#title" => t("Restore to"),
  515. "#options" => $sources,
  516. "#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."),
  517. "#default_value" => 'db',
  518. );
  519. }
  520. else {
  521. $form['source_id'] = array(
  522. "#type" => "value",
  523. "#value" => 'db',
  524. );
  525. }
  526. $form['destination_id'] = array('#type' => 'value', '#value' => $destination_id);
  527. $form['file_id'] = array('#type' => 'value', '#value' => $file_id);
  528. $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'));
  529. 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);
  530. $form = array_merge_recursive($form, backup_migrate_filters_settings_form(backup_migrate_filters_settings_default('restore'), 'restore'));
  531. $form['actions']['#weight'] = 100;
  532. // Add the advanced fieldset if there are any fields in it.
  533. if (@$form['advanced']) {
  534. $form['advanced']['#type'] = 'fieldset';
  535. $form['advanced']['#title'] = t('Advanced Options');
  536. $form['advanced']['#collapsed'] = TRUE;
  537. $form['advanced']['#collapsible'] = TRUE;
  538. }
  539. return $form;
  540. }
  541. /**
  542. * Do the file restore.
  543. */
  544. function backup_migrate_ui_destination_restore_file_confirm_submit($form, &$form_state) {
  545. $destination_id = $form_state['values']['destination_id'];
  546. $file_id = $form_state['values']['file_id'];
  547. if ($destination_id && $file_id) {
  548. backup_migrate_perform_restore($destination_id, $file_id, $form_state['values']);
  549. }
  550. $redir = user_access('access backup files') ? BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/" . $destination_id : BACKUP_MIGRATE_MENU_PATH;
  551. $form_state['redirect'] = $redir;
  552. }
  553. /**
  554. * Menu callback to delete a file from a destination.
  555. */
  556. function backup_migrate_ui_destination_delete_file($destination_id = NULL, $file_id = NULL) {
  557. if (backup_migrate_destination_file_exists($destination_id, $file_id)) {
  558. return drupal_get_form('backup_migrate_ui_destination_delete_file_confirm', $destination_id, $file_id);
  559. }
  560. _backup_migrate_message('Cannot delete the file: %file_id because it does not exist.', array('%file_id' => $file_id), 'error');
  561. if ($destination_id && user_access('access backup files')) {
  562. drupal_goto(BACKUP_MIGRATE_MENU_PATH . '/destination/list/files/' . $destination_id);
  563. }
  564. drupal_goto(BACKUP_MIGRATE_MENU_PATH);
  565. }
  566. /**
  567. * Ask confirmation for file deletion.
  568. */
  569. function backup_migrate_ui_destination_delete_file_confirm($form, &$form_state, $destination_id, $file_id) {
  570. $form['destination_id'] = array('#type' => 'value', '#value' => $destination_id);
  571. $form['file_id'] = array('#type' => 'value', '#value' => $file_id);
  572. 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'));
  573. }
  574. /**
  575. * Delete confirmed, perform the delete.
  576. */
  577. function backup_migrate_ui_destination_delete_file_confirm_submit($form, &$form_state) {
  578. if (user_access('delete backup files')) {
  579. $destination_id = $form_state['values']['destination_id'];
  580. $file_id = $form_state['values']['file_id'];
  581. backup_migrate_destination_delete_file($destination_id, $file_id);
  582. _backup_migrate_message('Database backup file deleted: %file_id', array('%file_id' => $file_id));
  583. }
  584. $form_state['redirect'] = user_access('access backup files') ? BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/" . $destination_id : BACKUP_MIGRATE_MENU_PATH;
  585. }
  586. /* Utilities */
  587. /**
  588. * Get pulldown to select existing source options.
  589. */
  590. function _backup_migrate_get_destination_pulldown($op, $destination_id = NULL, $copy_destination_id = NULL) {
  591. drupal_add_js(drupal_get_path('module', 'backup_migrate') . '/backup_migrate.js');
  592. $destinations = _backup_migrate_get_destination_form_item_options($op);
  593. $form = array(
  594. '#element_validate' => array('_backup_migrate_destination_pulldown_validate'),
  595. '#after_build' => array('_backup_migrate_process_destination_pulldown'),
  596. );
  597. $form['destination_id'] = array(
  598. '#type' => 'select',
  599. '#title' => t('Backup Destination'),
  600. '#options' => $destinations,
  601. '#default_value' => $destination_id,
  602. // '#process' => array('_backup_migrate_process_destination_pulldown'),
  603. );
  604. if (user_access('administer backup and migrate')) {
  605. $form['destination_id']['#description'] = l(t('Create new destination'), BACKUP_MIGRATE_MENU_PATH . '/settings/destination/add');
  606. }
  607. $form['copy'] = array(
  608. '#type' => 'checkbox',
  609. '#title' => '<span class="backup-migrate-destination-copy-label">' . t('Save a copy to a second destination') . '</span>',
  610. '#default_value' => !empty($copy_destination_id),
  611. );
  612. $form['copy_destination'] = array(
  613. '#type' => 'backup_migrate_dependent',
  614. '#dependencies' => array(
  615. 'copy' => TRUE,
  616. ),
  617. );
  618. $form['copy_destination']['copy_destination_id'] = array(
  619. '#type' => 'select',
  620. '#title' => t('Second Backup Destination'),
  621. '#options' => $destinations,
  622. '#default_value' => $copy_destination_id,
  623. );
  624. return $form;
  625. }
  626. /**
  627. * Return the destination pulldown with an option to save a second copy.
  628. */
  629. function _backup_migrate_process_destination_pulldown($element) {
  630. $id = $element['destination_id']['#id'];
  631. $settings = array(
  632. 'backup_migrate' => array(
  633. 'destination_selectors' => array(
  634. $id => array(
  635. 'destination_selector' => $id,
  636. 'copy_destination_selector' => $element['copy_destination']['copy_destination_id']['#id'],
  637. 'copy' => $element['copy']['#id'],
  638. 'labels' => array(
  639. t('Local') => t('Save an offsite copy to'),
  640. t('Offsite') => t('Save a local copy to'),
  641. ),
  642. ),
  643. ),
  644. ),
  645. );
  646. drupal_add_js($settings, 'setting');
  647. return $element;
  648. }
  649. /**
  650. * Validate the destination and second destination widget.
  651. */
  652. function _backup_migrate_destination_pulldown_validate($element, &$form_state, $form) {
  653. if (empty($element['copy']['#value']) || $element['copy_destination']['copy_destination_id']['#value'] == $element['destination_id']['#value']) {
  654. _backup_migrate_destination_pulldown_set_value($form_state['values'], $element['copy_destination']['copy_destination_id']['#parents'], '');
  655. }
  656. }
  657. /**
  658. * Set the value of a form field given it's parent array.
  659. */
  660. function _backup_migrate_destination_pulldown_set_value(&$values, $parents, $value) {
  661. $key = array_shift($parents);
  662. if (empty($parents)) {
  663. $values[$key] = $value;
  664. }
  665. else {
  666. _backup_migrate_destination_pulldown_set_value($values[$key], $parents, $value);
  667. }
  668. }
  669. /**
  670. * Get the destination options as an options array for a form item.
  671. */
  672. function _backup_migrate_get_destination_form_item_options($op) {
  673. $remote = $local = array();
  674. foreach (backup_migrate_get_destinations($op) as $key => $destination) {
  675. // Only work with destinations that are currently configured and will work.
  676. if ($destination->confirm_destination()) {
  677. if ($destination->op('remote backup')) {
  678. $remote[$key] = $destination->get_name();
  679. }
  680. else {
  681. $local[$key] = $destination->get_name();
  682. }
  683. }
  684. }
  685. $out = array();
  686. if ($remote && $local) {
  687. $out = array(
  688. t('Local') => $local,
  689. t('Offsite') => $remote,
  690. );
  691. }
  692. else {
  693. $out = $remote + $local;
  694. }
  695. return $out;
  696. }
  697. /**
  698. * A base class for creating destinations.
  699. */
  700. class backup_migrate_destination extends backup_migrate_location {
  701. public $db_table = 'backup_migrate_destinations';
  702. public $type_name = 'destination';
  703. public $default_values = array('settings' => array());
  704. public $singular = 'destination';
  705. public $plural = 'destinations';
  706. public $title_plural = 'Destinations';
  707. public $title_singular = 'Destination';
  708. public $cache_files = FALSE;
  709. public $fetch_time = NULL;
  710. public $weight = 0;
  711. public $destination_type = '';
  712. public $supported_ops = array();
  713. /**
  714. * 24 hours, i.e. 24 * 60 * 60 seconds.
  715. *
  716. * @var int.
  717. */
  718. public $cache_expire = 86400;
  719. /**
  720. * This function is not supposed to be called.
  721. *
  722. * It is just here to help the po extractor out.
  723. */
  724. public function strings() {
  725. // Help the pot extractor find these strings.
  726. t('Destination');
  727. t('Destinations');
  728. t('destinations');
  729. t('destination');
  730. }
  731. /**
  732. * Save the given file to the destination.
  733. */
  734. public function save_file($file, $settings) {
  735. $this->file_cache_clear();
  736. // Save the file metadata if the destination supports it.
  737. $this->save_file_info($file, $settings);
  738. return $this->_save_file($file, $settings);
  739. }
  740. /**
  741. * Save the given file to the destination.
  742. */
  743. public function _save_file($file, $settings) {
  744. // This must be overriden.
  745. return $file;
  746. }
  747. /**
  748. * Save the file metadata.
  749. *
  750. * @return object
  751. * The file object after it has been saved.
  752. */
  753. public function save_file_info($file, $settings) {
  754. $info = $this->create_info_file($file);
  755. // Save the info file and the actual file.
  756. return $this->_save_file($info, $settings);
  757. }
  758. /**
  759. * Load the file with the given destination specific ID.
  760. *
  761. * @return \backup_file
  762. * A backup file.
  763. */
  764. public function load_file($file_id) {
  765. // This must be overriden.
  766. return NULL;
  767. }
  768. /**
  769. * Check if the file exists in the list of available files.
  770. *
  771. * Actual destination types may have more efficient ways of doing this.
  772. */
  773. public function file_exists($file_id) {
  774. $files = $this->list_files();
  775. return isset($files[$file_id]);
  776. }
  777. /**
  778. * List all the available files in the given destination.
  779. *
  780. * Includes their destination specific id.
  781. */
  782. public function list_files() {
  783. $files = NULL;
  784. if ($this->cache_files) {
  785. $files = $this->file_cache_get();
  786. }
  787. if ($files === NULL) {
  788. require_once dirname(__FILE__) . '/files.inc';
  789. $files = $this->_list_files();
  790. $files = $this->load_files_info($files);
  791. if ($this->cache_files) {
  792. $this->file_cache_set($files);
  793. }
  794. // Clean up any previous abandoned tmp files before going further.
  795. _backup_migrate_temp_files_delete();
  796. }
  797. $out = array();
  798. if (is_array($files)) {
  799. foreach ($files as $id => $file) {
  800. if ($file->is_recognized_type()) {
  801. $out[$id] = $file;
  802. $out[$id]->destination = &$this;
  803. }
  804. }
  805. }
  806. return $out;
  807. }
  808. /**
  809. * Count all the available files in the given destination.
  810. */
  811. public function count_files() {
  812. return count($this->list_files());
  813. }
  814. /**
  815. * List all the available files in the given destination.
  816. *
  817. * Includes their destination specific id.
  818. */
  819. public function _list_files() {
  820. return array();
  821. }
  822. /**
  823. * Load up the file's metadata from the accompanying .info file if applicable.
  824. */
  825. public function load_files_info($files) {
  826. foreach ($files as $key => $file) {
  827. // See if there is an info file with the same name as the backup.
  828. if (isset($files[$key . '.info'])) {
  829. $info_file = $this->load_file($files[$key . '.info']->file_id());
  830. $info = drupal_parse_info_file($info_file->filepath());
  831. // Allow the stored metadata to override the detected metadata.
  832. unset($info['filename']);
  833. $file->file_info = $info + $file->file_info;
  834. // Remove the metadata file from the list.
  835. unset($files[$key . '.info']);
  836. }
  837. // Add destination specific info.
  838. $file->info_set('destination_id', $this->get('id'));
  839. $file->info_set('remote', $this->get('remote'));
  840. }
  841. return $files;
  842. }
  843. /**
  844. * Create an ini file and write the meta data.
  845. */
  846. public function create_info_file($file) {
  847. $info = $this->_file_info_file($file);
  848. $data = _backup_migrate_array_to_ini($file->file_info);
  849. $info->put_contents($data);
  850. return $info;
  851. }
  852. /**
  853. * Create the info file object.
  854. */
  855. public function _file_info_file($file) {
  856. $info = new backup_file(array('filename' => $this->_file_info_filename($file->file_id())));
  857. return $info;
  858. }
  859. /**
  860. * Determine the file name of the info file for a file.
  861. */
  862. public function _file_info_filename($file_id) {
  863. return $file_id . '.info';
  864. }
  865. /**
  866. * Cache the file list.
  867. */
  868. public function file_cache_set($files) {
  869. cache_set('backup_migrate_file_list:' . $this->get_id(), $files, 'cache', time() + $this->cache_expire);
  870. }
  871. /**
  872. * Retrieve the file list.
  873. */
  874. public function file_cache_get() {
  875. require_once dirname(__FILE__) . '/files.inc';
  876. $cache = cache_get('backup_migrate_file_list:' . $this->get_id());
  877. if (!empty($cache->data) && $cache->created > (time() - $this->cache_expire)) {
  878. $this->fetch_time = $cache->created;
  879. return $cache->data;
  880. }
  881. $this->fetch_time = 0;
  882. return NULL;
  883. }
  884. /**
  885. * Retrieve the file list.
  886. */
  887. public function file_cache_clear() {
  888. if ($this->cache_files) {
  889. $this->file_cache_set(NULL);
  890. }
  891. }
  892. /**
  893. * Delete the file with the given destination specific id.
  894. */
  895. public function delete_file($file_id) {
  896. $this->file_cache_clear();
  897. $this->_delete_file($file_id);
  898. $this->_delete_file($this->_file_info_filename($file_id));
  899. }
  900. /**
  901. * Delete the file with the given destination specific id.
  902. */
  903. public function _delete_file($file_id) {
  904. // This must be overriden.
  905. }
  906. /**
  907. * Get the edit form for the item.
  908. */
  909. public function edit_form() {
  910. if (get_class($this) !== 'backup_migrate_destination') {
  911. $form = parent::edit_form();
  912. $form['subtype'] = array(
  913. "#type" => "value",
  914. "#default_value" => $this->get('subtype'),
  915. );
  916. }
  917. else {
  918. $types = backup_migrate_get_destination_subtypes();
  919. $items = array(
  920. 'remote' => array(
  921. 'title' => t('Offsite Destinations'),
  922. 'description' => t('For the highest level of protection, set up an offsite backup destination in a location separate from your website.'),
  923. 'items' => array(),
  924. ),
  925. 'local' => array(
  926. 'title' => t('Local Destinations'),
  927. 'description' => t('Local backups are quick and convenient but do not provide the additional safety of offsite backups.'),
  928. 'items' => array(),
  929. ),
  930. 'other' => array(
  931. 'title' => t('Other Destinations'),
  932. 'description' => t('These destinations have not been classified because they were created for Backup and Migrate version 2. They may not work correctly with this version.'),
  933. 'items' => array(),
  934. ),
  935. );
  936. // If no (valid) node type has been provided, display a node type
  937. // overview.
  938. $path = $this->get_settings_path();
  939. foreach ($types as $key => $type) {
  940. if (@$type['can_create']) {
  941. $type_url_str = str_replace('_', '-', $key);
  942. $out = '<dt>' . l($type['type_name'], $path . "/add/$type_url_str", array('attributes' => array('title' => t('Add a new @s destination.', array('@s' => $type['type_name']))))) . '</dt>';
  943. $out .= '<dd>' . filter_xss_admin($type['description']) . '</dd>';
  944. if (!empty($type['local'])) {
  945. $items['local']['items'][] = $out;
  946. }
  947. if (!empty($type['remote'])) {
  948. $items['remote']['items'][] = $out;
  949. }
  950. if (empty($type['local']) && empty($type['remote'])) {
  951. $items['other']['items'][] = $out;
  952. }
  953. }
  954. }
  955. if (count($items['local']['items']) || count($items['remote']['items']) || count($items['other']['items'])) {
  956. $output = '<p>' . t('Choose the type of destination you would like to create:') . '</p>';
  957. foreach ($items as $group) {
  958. if (count($group['items'])) {
  959. $group['body'] = '<dl>' . implode('', $group['items']) . '</dl>';
  960. $output .= theme('backup_migrate_group', $group);
  961. }
  962. }
  963. }
  964. else {
  965. $output = t('No destination types available.');
  966. }
  967. $form['select_type'] = array(
  968. '#type' => 'markup',
  969. '#markup' => $output,
  970. );
  971. }
  972. return $form;
  973. }
  974. /**
  975. * Get a message to send to the user when confirming the deletion of the item.
  976. */
  977. public function delete_confirm_message() {
  978. 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()));
  979. }
  980. /**
  981. * Get a boolean representing if the destination is remote or local.
  982. */
  983. public function get_remote() {
  984. return $this->op('remote backup');
  985. }
  986. /**
  987. * Get the action links for a destination.
  988. */
  989. public function get_action_links() {
  990. $out = parent::get_action_links();
  991. $item_id = $this->get_id();
  992. // Don't display the download/delete/restore ops if they are not available
  993. // for this destination.
  994. if ($this->op('list files') && user_access("access backup files")) {
  995. $out = array('list files' => l(t("list files"), $this->get_settings_path() . '/list/files/' . $item_id)) + $out;
  996. }
  997. if (!$this->op('configure') || !user_access('administer backup and migrate')) {
  998. unset($out['edit']);
  999. }
  1000. return $out;
  1001. }
  1002. /**
  1003. * Get the action links for a file on a given destination.
  1004. */
  1005. public function get_file_links($file_id) {
  1006. $out = array();
  1007. // Don't display the download/delete/restore ops if they are not available
  1008. // for this destination.
  1009. $can_read = $this->can_read_file($file_id);
  1010. $can_delete = $this->can_delete_file($file_id);
  1011. $path = $this->get_settings_path();
  1012. $destination_id = $this->get_id();
  1013. if ($can_read && user_access("access backup files")) {
  1014. $out[] = l(t("download"), $path . '/downloadfile/' . $destination_id . '/' . $file_id);
  1015. }
  1016. if ($can_read && user_access("restore from backup")) {
  1017. $out[] = l(t("restore"), $path . '/list/restorefile/' . $destination_id . '/' . $file_id);
  1018. }
  1019. if ($can_delete && user_access("delete backup files")) {
  1020. $out[] = l(t("delete"), $path . '/list/deletefile/' . $destination_id . '/' . $file_id);
  1021. }
  1022. return $out;
  1023. }
  1024. /**
  1025. * Determine if we can read the given file.
  1026. */
  1027. public function can_read_file($file_id) {
  1028. return $this->op('restore');
  1029. }
  1030. /**
  1031. * Determine if we can read the given file.
  1032. */
  1033. public function can_delete_file($file_id) {
  1034. return $this->op('delete');
  1035. }
  1036. /**
  1037. * Get the form for the settings for this destination type.
  1038. */
  1039. public function settings_default() {
  1040. return array();
  1041. }
  1042. /**
  1043. * Get the form for the settings for this destination.
  1044. */
  1045. public function settings_form($form) {
  1046. return $form;
  1047. }
  1048. /**
  1049. * Validate the form for the settings for this destination.
  1050. */
  1051. public function settings_form_validate($form_values) {
  1052. }
  1053. /**
  1054. * Submit the settings form. Any values returned will be saved.
  1055. */
  1056. public function settings_form_submit($form_values) {
  1057. return $form_values;
  1058. }
  1059. /**
  1060. * Check that a destination is valid.
  1061. */
  1062. public function confirm_destination() {
  1063. return TRUE;
  1064. }
  1065. /**
  1066. * Add the menu items specific to the destination type.
  1067. */
  1068. public function get_menu_items() {
  1069. $items = parent::get_menu_items();
  1070. $path = $this->get_settings_path();
  1071. $items[$path . '/list/files'] = array(
  1072. 'title' => 'Destination Files',
  1073. 'page callback' => 'backup_migrate_menu_callback',
  1074. 'page arguments' => array(
  1075. 'destinations',
  1076. 'backup_migrate_ui_destination_display_files',
  1077. TRUE,
  1078. ),
  1079. 'access arguments' => array('access backup files'),
  1080. 'type' => MENU_LOCAL_TASK,
  1081. );
  1082. $items[$path . '/list/deletefile'] = array(
  1083. 'title' => 'Delete File',
  1084. 'description' => 'Delete a backup file',
  1085. 'page callback' => 'backup_migrate_menu_callback',
  1086. 'page arguments' => array(
  1087. 'destinations',
  1088. 'backup_migrate_ui_destination_delete_file',
  1089. TRUE,
  1090. ),
  1091. 'access arguments' => array('delete backup files'),
  1092. 'type' => MENU_LOCAL_TASK,
  1093. );
  1094. $items[$path . '/list/restorefile'] = array(
  1095. 'title' => 'Restore from backup',
  1096. 'description' => 'Restore database from a backup file on the server',
  1097. 'page callback' => 'backup_migrate_menu_callback',
  1098. 'page arguments' => array(
  1099. 'destinations',
  1100. 'backup_migrate_ui_destination_restore_file',
  1101. TRUE,
  1102. ),
  1103. 'access arguments' => array('restore from backup'),
  1104. 'type' => MENU_LOCAL_TASK,
  1105. );
  1106. $items[$path . '/downloadfile'] = array(
  1107. 'title' => 'Download File',
  1108. 'description' => 'Download a backup file',
  1109. 'page callback' => 'backup_migrate_menu_callback',
  1110. 'page arguments' => array(
  1111. 'destinations',
  1112. 'backup_migrate_ui_destination_download_file',
  1113. TRUE,
  1114. ),
  1115. 'access arguments' => array('access backup files'),
  1116. 'type' => MENU_CALLBACK,
  1117. );
  1118. return $items;
  1119. }
  1120. }
  1121. /**
  1122. * A base class for creating destinations.
  1123. */
  1124. class backup_migrate_destination_remote extends backup_migrate_destination {
  1125. /**
  1126. * The location is a URI so parse it and store the parts.
  1127. */
  1128. public function get_location() {
  1129. return $this->url(FALSE);
  1130. }
  1131. /**
  1132. * The location to display is the url without the password.
  1133. */
  1134. public function get_display_location() {
  1135. return $this->url(TRUE);
  1136. }
  1137. /**
  1138. * Return the location with the password.
  1139. */
  1140. public function set_location($location) {
  1141. $this->location = $location;
  1142. $this->set_url($location);
  1143. }
  1144. /**
  1145. * Destination configuration callback.
  1146. */
  1147. public function edit_form() {
  1148. $form = parent::edit_form();
  1149. $form['scheme'] = array(
  1150. "#type" => "textfield",
  1151. "#title" => t("Scheme"),
  1152. "#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : '',
  1153. "#required" => TRUE,
  1154. "#weight" => 0,
  1155. );
  1156. $form['host'] = array(
  1157. "#type" => "textfield",
  1158. "#title" => t("Host"),
  1159. "#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost',
  1160. "#required" => TRUE,
  1161. "#weight" => 10,
  1162. );
  1163. $form['path'] = array(
  1164. "#type" => "textfield",
  1165. "#title" => t("Path"),
  1166. "#default_value" => @$this->dest_url['path'],
  1167. "#required" => TRUE,
  1168. "#weight" => 20,
  1169. );
  1170. $form['user'] = array(
  1171. "#type" => "textfield",
  1172. "#title" => t("Username"),
  1173. "#default_value" => @$this->dest_url['user'],
  1174. "#required" => TRUE,
  1175. "#weight" => 30,
  1176. );
  1177. $form['pass'] = array(
  1178. "#type" => "password",
  1179. "#title" => t("Password"),
  1180. "#default_value" => @$this->dest_url['pass'],
  1181. '#description' => '',
  1182. "#weight" => 40,
  1183. );
  1184. if (@$this->dest_url['pass']) {
  1185. $form['old_password'] = array(
  1186. "#type" => "value",
  1187. "#value" => @$this->dest_url['pass'],
  1188. );
  1189. $form['pass']["#description"] .= ' ' . t('You do not need to enter a password unless you wish to change the currently saved password.');
  1190. }
  1191. return $form;
  1192. }
  1193. /**
  1194. * Submit the configuration form.
  1195. *
  1196. * Glue the url together and add the old password back if a new one was not
  1197. * specified.
  1198. */
  1199. public function edit_form_submit($form, &$form_state) {
  1200. $form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password'];
  1201. $form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE);
  1202. parent::edit_form_submit($form, $form_state);
  1203. }
  1204. }