destinations.inc 43 KB

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