redirect.admin.inc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for the redirect module.
  5. */
  6. function redirect_list_form($form, &$form_state) {
  7. $form['#operations'] = redirect_get_redirect_operations();
  8. if (isset($form_state['values']['operation']) && empty($form_state['values']['confirm'])) {
  9. return redirect_list_form_operations_confirm_form($form, $form_state, $form_state['values']['operation'], array_filter($form_state['values']['rids']));
  10. }
  11. $destination = drupal_get_destination();
  12. $default_status_code = variable_get('redirect_default_status_code', 301);
  13. // Set up the header.
  14. $header = array(
  15. 'source' => array('data' => t('From'), 'field' => 'source', 'sort' => 'asc'),
  16. 'redirect' => array('data' => t('To'), 'field' => 'redirect'),
  17. 'status_code' => array('data' => t('Type'), 'field' => 'status_code'),
  18. 'language' => array('data' => t('Language'), 'field' => 'language'),
  19. 'count' => array('data' => t('Count'), 'field' => 'count'),
  20. 'access' => array('data' => t('Last accessed'), 'field' => 'access'),
  21. 'operations' => array('data' => t('Operations')),
  22. );
  23. // Do not include the language column if locale is disabled.
  24. if (!module_exists('locale')) {
  25. unset($header['language']);
  26. }
  27. // Get filter keys and add the filter form.
  28. $keys = func_get_args();
  29. $keys = array_splice($keys, 2); // Offset the $form and $form_state parameters.
  30. $keys = implode('/', $keys);
  31. $form['redirect_list_filter_form'] = redirect_list_filter_form($keys);
  32. // Build the 'Update options' form.
  33. $form['operations'] = array(
  34. '#type' => 'fieldset',
  35. '#title' => t('Update options'),
  36. '#prefix' => '<div class="container-inline">',
  37. '#suffix' => '</div>',
  38. '#attributes' => array(
  39. 'class' => array('redirect-list-operations'),
  40. ),
  41. );
  42. $operations = array();
  43. foreach ($form['#operations'] as $key => $operation) {
  44. $operations[$key] = $operation['action'];
  45. }
  46. $form['operations']['operation'] = array(
  47. '#type' => 'select',
  48. '#options' => $operations,
  49. '#default_value' => 'delete',
  50. );
  51. $form['operations']['submit'] = array(
  52. '#type' => 'submit',
  53. '#value' => t('Update'),
  54. '#validate' => array('redirect_list_form_operations_validate'),
  55. '#submit' => array('redirect_list_form_operations_submit'),
  56. );
  57. // Building the SQL query and load the redirects.
  58. $query = db_select('redirect', 'r')->extend('TableSort')->extend('PagerDefault');
  59. $query->addField('r', 'rid');
  60. $query->condition('r.type', 'redirect');
  61. $query->orderByHeader($header);
  62. $query->limit(50);
  63. $query->addTag('redirect_list');
  64. $query->addTag('redirect_access');
  65. redirect_build_filter_query($query, array('source', 'redirect'), $keys);
  66. $rids = $query->execute()->fetchCol();
  67. $redirects = redirect_load_multiple($rids);
  68. $rows = array();
  69. foreach ($redirects as $rid => $redirect) {
  70. $row = array();
  71. $redirect->source_options = array_merge($redirect->source_options, array('alias' => TRUE, 'language' => redirect_language_load($redirect->language)));
  72. $source_url = redirect_url($redirect->source, $redirect->source_options);
  73. $redirect_url = redirect_url($redirect->redirect, array_merge($redirect->redirect_options, array('alias' => TRUE)));
  74. drupal_alter('redirect_url', $redirect->source, $redirect->source_options);
  75. drupal_alter('redirect_url', $redirect->redirect, $redirect->redirect_options);
  76. $row['source'] = l($source_url, $redirect->source, $redirect->source_options);
  77. $row['redirect'] = l($redirect_url, $redirect->redirect, $redirect->redirect_options);
  78. $row['status_code'] = $redirect->status_code ? $redirect->status_code : t('Default (@default)', array('@default' => $default_status_code));
  79. $row['language'] = module_invoke('locale', 'language_name', $redirect->language);
  80. $row['count'] = $redirect->count;
  81. if ($redirect->access) {
  82. $row['access'] = array(
  83. 'data' => t('!interval ago', array('!interval' => format_interval(REQUEST_TIME - $redirect->access))),
  84. 'title' => t('Last accessed on @date', array('@date' => format_date($redirect->access))),
  85. );
  86. }
  87. else {
  88. $row['access'] = t('Never');
  89. }
  90. // Mark redirects that override existing paths with a warning in the table.
  91. if (drupal_valid_path($redirect->source)) {
  92. $row['#attributes']['class'][] = 'warning';
  93. $row['#attributes']['title'] = t('This redirect overrides an existing internal path.');
  94. }
  95. $operations = array();
  96. if (redirect_access('update', $redirect)) {
  97. $operations['edit'] = array(
  98. 'title' => t('Edit'),
  99. 'href' => 'admin/config/search/redirect/edit/' . $rid,
  100. 'query' => $destination,
  101. );
  102. }
  103. if (redirect_access('delete', $redirect)) {
  104. $operations['delete'] = array(
  105. 'title' => t('Delete'),
  106. 'href' => 'admin/config/search/redirect/delete/' . $rid,
  107. 'query' => $destination,
  108. );
  109. }
  110. $row['operations'] = array(
  111. 'data' => array(
  112. '#theme' => 'links',
  113. '#links' => $operations,
  114. '#attributes' => array('class' => array('links', 'inline', 'nowrap')),
  115. ),
  116. );
  117. $rows[$rid] = $row;
  118. }
  119. $form['rids'] = array(
  120. '#type' => 'tableselect',
  121. '#header' => $header,
  122. '#options' => $rows,
  123. '#empty' => t('No URL redirects available.'),
  124. '#attributes' => array(
  125. 'class' => array('redirect-list-tableselect'),
  126. ),
  127. '#attached' => array(
  128. 'js' => array(
  129. drupal_get_path('module', 'redirect') . '/redirect.admin.js',
  130. ),
  131. ),
  132. );
  133. if (redirect_access('create', 'redirect')) {
  134. $form['rids']['#empty'] .= ' ' . l(t('Add URL redirect.'), 'admin/config/search/redirect/add', array('query' => $destination));
  135. }
  136. $form['pager'] = array('#theme' => 'pager');
  137. return $form;
  138. }
  139. /**
  140. * Return a form to filter URL redirects.
  141. *
  142. * @see redirect_list_filter_form_submit()
  143. *
  144. * @ingroup forms
  145. */
  146. function redirect_list_filter_form($keys = '') {
  147. $form['#attributes'] = array('class' => array('search-form'));
  148. $form['basic'] = array(
  149. '#type' => 'fieldset',
  150. '#title' => t('Filter redirects'),
  151. '#attributes' => array('class' => array('container-inline')),
  152. );
  153. $form['basic']['filter'] = array(
  154. '#type' => 'textfield',
  155. '#title' => '',
  156. '#default_value' => $keys,
  157. '#maxlength' => 128,
  158. '#size' => 25,
  159. );
  160. $form['basic']['submit'] = array(
  161. '#type' => 'submit',
  162. '#value' => t('Filter'),
  163. '#submit' => array('redirect_list_filter_form_submit'),
  164. );
  165. if ($keys) {
  166. $form['basic']['reset'] = array(
  167. '#type' => 'submit',
  168. '#value' => t('Reset'),
  169. '#submit' => array('redirect_list_filter_form_reset'),
  170. );
  171. }
  172. return $form;
  173. }
  174. /**
  175. * Process filter form submission when the Filter button is pressed.
  176. */
  177. function redirect_list_filter_form_submit($form, &$form_state) {
  178. $form_state['redirect'] = 'admin/config/search/redirect/list/' . trim($form_state['values']['filter']);
  179. }
  180. /**
  181. * Process filter form submission when the Reset button is pressed.
  182. */
  183. function redirect_list_filter_form_reset($form, &$form_state) {
  184. $form_state['redirect'] = 'admin/config/search/redirect';
  185. }
  186. /**
  187. * Extends a query object for URL redirect filters.
  188. *
  189. * @param $query
  190. * Query object that should be filtered.
  191. * @param $keys
  192. * The filter string to use.
  193. */
  194. function redirect_build_filter_query(QueryAlterableInterface $query, array $fields, $keys = '') {
  195. if ($keys && $fields) {
  196. // Replace wildcards with PDO wildcards.
  197. $conditions = db_or();
  198. $wildcard = '%' . trim(preg_replace('!\*+!', '%', db_like($keys)), '%') . '%';
  199. foreach ($fields as $field) {
  200. $conditions->condition($field, $wildcard, 'LIKE');
  201. }
  202. $query->condition($conditions);
  203. }
  204. }
  205. /**
  206. * Validate redirect_list_form form submissions.
  207. *
  208. * Check if any redirects have been selected to perform the chosen
  209. * 'Update option' on.
  210. */
  211. function redirect_list_form_operations_validate($form, &$form_state) {
  212. // Error if there are no redirects selected.
  213. if (!is_array($form_state['values']['rids']) || !count(array_filter($form_state['values']['rids']))) {
  214. form_set_error('', t('No redirects selected.'));
  215. }
  216. }
  217. /**
  218. * Process redirect_list_form form submissions.
  219. *
  220. * Execute the chosen 'Update option' on the selected redirects.
  221. */
  222. function redirect_list_form_operations_submit($form, &$form_state) {
  223. $operations = $form['#operations'];
  224. $operation = $operations[$form_state['values']['operation']];
  225. // Filter out unchecked redirects
  226. $rids = array_filter($form_state['values']['rids']);
  227. if (!empty($operation['confirm']) && empty($form_state['values']['confirm'])) {
  228. // We need to rebuild the form to go to a second step. For example, to
  229. // show the confirmation form for the deletion of redirects.
  230. $form_state['rebuild'] = TRUE;
  231. }
  232. else {
  233. $function = $operation['callback'];
  234. // Add in callback arguments if present.
  235. if (isset($operation['callback arguments'])) {
  236. $args = array_merge(array($rids), $operation['callback arguments']);
  237. }
  238. else {
  239. $args = array($rids);
  240. }
  241. call_user_func_array($function, $args);
  242. $count = count($form_state['values']['rids']);
  243. watchdog('redirect', '@action @count redirects.', array('@action' => $operation['action_past'], '@count' => $count));
  244. drupal_set_message(format_plural(count($rids), '@action @count redirect.', '@action @count redirects.', array('@action' => $operation['action_past'], '@count' => $count)));
  245. }
  246. }
  247. function redirect_list_form_operations_confirm_form($form, &$form_state, $operation, $rids) {
  248. $operations = $form['#operations'];
  249. $operation = $operations[$form_state['values']['operation']];
  250. $form['rids_list'] = array(
  251. '#theme' => 'item_list',
  252. '#items' => array(),
  253. );
  254. $form['rids'] = array(
  255. '#type' => 'value',
  256. '#value' => $rids,
  257. );
  258. $redirects = redirect_load_multiple($rids);
  259. foreach ($redirects as $rid => $redirect) {
  260. $form['rids_list']['#items'][$rid] = check_plain(redirect_url($redirect->source, $redirect->source_options));
  261. }
  262. $form['operation'] = array('#type' => 'hidden', '#value' => $form_state['values']['operation']);
  263. $form['#submit'][] = 'redirect_list_form_operations_submit';
  264. $confirm_question = format_plural(count($rids), 'Are you sure you want to @action this redirect?', 'Are you sure you want to @action these redirects?', array('@action' => drupal_strtolower($operation['action'])));
  265. return confirm_form(
  266. $form,
  267. $confirm_question,
  268. 'admin/config/search/redirect', // @todo This does not redirect back to filtered page.
  269. t('This action cannot be undone.'),
  270. $operation['action'],
  271. t('Cancel')
  272. );
  273. }
  274. /**
  275. * Form builder to add or edit an URL redirect.
  276. *
  277. * @see redirect_element_validate_source()
  278. * @see redirect_element_validate_redirect()
  279. * @see redirect_edit_form_validate()
  280. * @see redirect_edit_form_submit()
  281. *
  282. * @ingroup forms
  283. */
  284. function redirect_edit_form($form, &$form_state, $redirect = NULL) {
  285. if (!isset($redirect)) {
  286. $redirect = new stdClass();
  287. }
  288. // Merge default values.
  289. redirect_object_prepare($redirect, array(
  290. 'source' => isset($_GET['source']) ? urldecode($_GET['source']) : '',
  291. 'source_options' => isset($_GET['source_options']) ? drupal_get_query_array($_GET['source_options']) : array(),
  292. 'redirect' => isset($_GET['redirect']) ? urldecode($_GET['redirect']) : '',
  293. 'redirect_options' => isset($_GET['redirect_options']) ? drupal_get_query_array($_GET['redirect_options']) : array(),
  294. 'language' => isset($_GET['language']) ? urldecode($_GET['language']) : LANGUAGE_NONE,
  295. ));
  296. $form['rid'] = array(
  297. '#type' => 'value',
  298. '#value' => $redirect->rid,
  299. );
  300. $form['type'] = array(
  301. '#type' => 'value',
  302. '#value' => $redirect->type,
  303. );
  304. $form['hash'] = array(
  305. '#type' => 'value',
  306. '#value' => $redirect->hash,
  307. );
  308. $form['source'] = array(
  309. '#type' => 'textfield',
  310. '#title' => t('From'),
  311. '#description' => t("Enter an internal Drupal path or path alias to redirect (e.g. %example1 or %example2). Fragment anchors (e.g. %anchor) are <strong>not</strong> allowed.", array('%example1' => 'node/123', '%example2' => 'taxonomy/term/123', '%anchor' => '#anchor')),
  312. '#maxlength' => 560,
  313. '#default_value' => $redirect->rid || $redirect->source ? redirect_url($redirect->source, $redirect->source_options + array('alter' => FALSE)) : '',
  314. '#required' => TRUE,
  315. '#field_prefix' => $GLOBALS['base_url'] . '/' . (variable_get('clean_url', 0) ? '' : '?q='),
  316. '#element_validate' => array('redirect_element_validate_source'),
  317. );
  318. $form['source_options'] = array(
  319. '#type' => 'value',
  320. '#value' => $redirect->source_options,
  321. '#tree' => TRUE,
  322. );
  323. $form['redirect'] = array(
  324. '#type' => 'textfield',
  325. '#title' => t('To'),
  326. '#maxlength' => 560,
  327. '#default_value' => $redirect->rid || $redirect->redirect ? redirect_url($redirect->redirect, $redirect->redirect_options, TRUE) : '',
  328. '#required' => TRUE,
  329. '#description' => t('Enter an internal Drupal path, path alias, or complete external URL (like http://example.com/) to redirect to. Use %front to redirect to the front page.', array('%front' => '<front>')),
  330. '#element_validate' => array('redirect_element_validate_redirect'),
  331. );
  332. $form['redirect_options'] = array(
  333. '#type' => 'value',
  334. '#value' => $redirect->redirect_options,
  335. '#tree' => TRUE,
  336. );
  337. // This will be a hidden value unless locale module is enabled.
  338. $form['language'] = array(
  339. '#type' => 'value',
  340. '#value' => $redirect->language,
  341. );
  342. $form['advanced'] = array(
  343. '#type' => 'fieldset',
  344. '#title' => t('Advanced options'),
  345. '#collapsible' => TRUE,
  346. '#collapsed' => TRUE,
  347. );
  348. $form['advanced']['status_code'] = array(
  349. '#type' => 'select',
  350. '#title' => t('Redirect status'),
  351. '#description' => t('You can find more information about HTTP redirect status codes at <a href="@status-codes">@status-codes</a>.', array('@status-codes' => 'http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection')),
  352. '#default_value' => $redirect->status_code,
  353. '#options' => array(0 => t('Default (@default)', array('@default' => variable_get('redirect_default_status_code', 301)))) + redirect_status_code_options(),
  354. );
  355. $form['override'] = array(
  356. '#type' => 'checkbox',
  357. '#title' => t('I understand the following warnings and would like to proceed with saving this URL redirect'),
  358. '#default_value' => FALSE,
  359. '#access' => FALSE,
  360. '#required' => FALSE,
  361. '#weight' => -100,
  362. '#prefix' => '<div class="messages warning">',
  363. '#suffix' => '</div>',
  364. );
  365. if (!empty($form_state['storage']['override_messages'])) {
  366. $form['override']['#access'] = TRUE;
  367. //$form['override']['#required'] = TRUE;
  368. $form['override']['#description'] = theme('item_list', array('items' => $form_state['storage']['override_messages']));
  369. // Reset the messages.
  370. $form_state['storage']['override_messages'] = array();
  371. }
  372. $form['actions'] = array('#type' => 'actions');
  373. $form['actions']['submit'] = array(
  374. '#type' => 'submit',
  375. '#value' => t('Save'),
  376. );
  377. $form['actions']['cancel'] = array(
  378. '#type' => 'link',
  379. '#title' => t('Cancel'),
  380. '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/redirect',
  381. );
  382. return $form;
  383. }
  384. /**
  385. * Element validate handler; validate the source of an URL redirect.
  386. *
  387. * @see redirect_edit_form()
  388. */
  389. function redirect_element_validate_source($element, &$form_state) {
  390. $value = &$element['#value'];
  391. // Check that the source contains no URL fragment.
  392. if (strpos($value, '#') !== FALSE) {
  393. form_error($element, t('The source path cannot contain an URL fragment anchor.'));
  394. }
  395. _redirect_extract_url_options($element, $form_state);
  396. // Disallow redirections from the frontpage.
  397. if ($value === '<front>') {
  398. form_error($element, t('The source path cannot be the front page.'));
  399. }
  400. // Cannot create redirects for valid paths.
  401. if (empty($form_state['values']['override'])) {
  402. $menu_item = menu_get_item($value);
  403. if ($menu_item && $menu_item['page_callback'] != 'redirect_redirect' && $value == $menu_item['path']) {
  404. $form_state['storage']['override_messages']['valid-path'] = t('The source path %path is likely a valid path. It is preferred to <a href="@url-alias">create URL aliases</a> for existing paths rather than redirects.', array('%path' => $value, '@url-alias' => url('admin/config/search/path/add')));
  405. $form_state['rebuild'] = TRUE;
  406. }
  407. }
  408. return $element;
  409. }
  410. /**
  411. * Element validate handler; validate the redirect of an URL redirect.
  412. *
  413. * @see redirect_edit_form()
  414. */
  415. function redirect_element_validate_redirect($element, &$form_state) {
  416. $value = &$element['#value'];
  417. _redirect_extract_url_options($element, $form_state);
  418. $value = &$form_state['values']['redirect'];
  419. // Normalize the path.
  420. $value = drupal_get_normal_path($value, $form_state['values']['language']);
  421. if (!valid_url($value) && !valid_url($value, TRUE) && $value != '<front>' && $value != '') {
  422. form_error($element, t('The redirect path %value is not valid.', array('%value' => $value)));
  423. }
  424. return $element;
  425. }
  426. /**
  427. * Extract the query and fragment parts out of an URL field.
  428. */
  429. function _redirect_extract_url_options(&$element, &$form_state) {
  430. $value = &$element['#value'];
  431. $type = $element['#name'];
  432. $options = &$form_state['values']["{$type}_options"];
  433. $parsed = redirect_parse_url($value);
  434. if (isset($parsed['fragment'])) {
  435. $options['fragment'] = $parsed['fragment'];
  436. }
  437. else {
  438. unset($options['fragment']);
  439. }
  440. if (isset($parsed['query'])) {
  441. $options['query'] = $parsed['query'];
  442. }
  443. else {
  444. unset($options['query']);
  445. }
  446. if (isset($parsed['scheme']) && $parsed['scheme'] == 'https') {
  447. $options['https'] = TRUE;
  448. }
  449. else {
  450. unset($options['https']);
  451. }
  452. if (!url_is_external($parsed['url'])) {
  453. $parsed['url'] = drupal_get_normal_path($parsed['url'], $form_state['values']['language']);
  454. }
  455. form_set_value($element, $parsed['url'], $form_state);
  456. return $parsed;
  457. }
  458. /**
  459. * Form validate handler; validate an URL redirect
  460. *
  461. * @see redirect_edit_form()
  462. */
  463. function redirect_edit_form_validate($form, &$form_state) {
  464. $redirect = (object) $form_state['values'];
  465. if (empty($form_state['values']['override'])) {
  466. if ($existing = redirect_load_by_source($redirect->source, $redirect->language)) {
  467. if ($redirect->rid != $existing->rid && $redirect->language == $existing->language) {
  468. // The "from" path should not conflict with another redirect
  469. $form_state['storage']['override_messages']['redirect-conflict'] = t('The base source path %source is already being redirected. Do you want to <a href="@edit-page">edit the existing redirect</a>?', array('%source' => $redirect->source, '@edit-page' => url('admin/config/search/redirect/edit/'. $existing->rid)));
  470. $form_state['rebuild'] = TRUE;
  471. }
  472. }
  473. if ($form['override']['#access']) {
  474. drupal_set_message('Did you read the warnings and click the checkbox?', 'error');
  475. $form_state['rebuild'] = TRUE;
  476. //form_set_error('override', 'CLICK DA BUTTON!');
  477. }
  478. }
  479. redirect_validate($redirect, $form, $form_state);
  480. }
  481. /**
  482. * Form submit handler; insert or update an URL redirect.
  483. *
  484. * @see redirect_edit_form()
  485. */
  486. function redirect_edit_form_submit($form, &$form_state) {
  487. form_state_values_clean($form_state);
  488. $redirect = (object) $form_state['values'];
  489. redirect_save($redirect);
  490. drupal_set_message(t('The redirect has been saved.'));
  491. $form_state['redirect'] = 'admin/config/search/redirect';
  492. }
  493. /**
  494. * Form builder to delete an URL redirect.
  495. *
  496. * @see redirect_delete_form()
  497. * @see confirm_form()
  498. *
  499. * @ingroup forms
  500. */
  501. function redirect_delete_form($form, &$form_state, $redirect) {
  502. $form['rid'] = array(
  503. '#type' => 'value',
  504. '#value' => $redirect->rid,
  505. );
  506. return confirm_form(
  507. $form,
  508. t('Are you sure you want to delete the URL redirect from %source to %redirect?', array('%source' => $redirect->source, '%redirect' => $redirect->redirect)),
  509. 'admin/config/search/redirect'
  510. );
  511. }
  512. /**
  513. * Form submit handler; delete an URL redirect after confirmation.
  514. *
  515. * @see redirect_delete_form()
  516. */
  517. function redirect_delete_form_submit($form, &$form_state) {
  518. redirect_delete($form_state['values']['rid']);
  519. drupal_set_message(t('The redirect has been deleted.'));
  520. $form_state['redirect'] = 'admin/config/search/redirect';
  521. }
  522. /**
  523. * Form builder for redirection settings.
  524. *
  525. * @see system_settings_form()
  526. * @see redirect_settings_form_submit()
  527. *
  528. * @ingroup forms
  529. */
  530. function redirect_settings_form($form, &$form_state) {
  531. $form['redirect_auto_redirect'] = array(
  532. '#type' => 'checkbox',
  533. '#title' => t('Automatically create redirects when URL aliases are changed.'),
  534. '#default_value' => variable_get('redirect_auto_redirect', TRUE),
  535. '#disabled' => !module_exists('path'),
  536. );
  537. $form['redirect_passthrough_querystring'] = array(
  538. '#type' => 'checkbox',
  539. '#title' => t('Retain query string through redirect.'),
  540. '#default_value' => variable_get('redirect_passthrough_querystring', 1),
  541. '#description' => t('For example, given a redirect from %source to %redirect, if a user visits %sourcequery they would be redirected to %redirectquery. The query strings in the redirection will always take precedence over the current query string.', array('%source' => 'source-path', '%redirect' => 'node?a=apples', '%sourcequery' => 'source-path?a=alligators&b=bananas', '%redirectquery' => 'node?a=apples&b=bananas')),
  542. );
  543. $form['redirect_warning'] = array(
  544. '#type' => 'checkbox',
  545. '#title' => t('Display a warning message to users when they are redirected.'),
  546. '#default_value' => variable_get('redirect_warning', FALSE),
  547. '#access' => FALSE,
  548. );
  549. $form['redirect_default_status_code'] = array(
  550. '#type' => 'select',
  551. '#title' => t('Default redirect status'),
  552. '#description' => t('You can find more information about HTTP redirect status codes at <a href="@status-codes">@status-codes</a>.', array('@status-codes' => 'http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection')),
  553. '#options' => redirect_status_code_options(),
  554. '#default_value' => variable_get('redirect_default_status_code', 301),
  555. );
  556. $form['redirect_page_cache'] = array(
  557. '#type' => 'checkbox',
  558. '#title' => t('Allow redirects to be saved into the page cache.'),
  559. '#default_value' => variable_get('redirect_page_cache', 0),
  560. '#description' => t('This feature requires <a href="@performance">Cache pages for anonymous users</a> to be enabled and the %variable variable to be TRUE.', array('@performance' => url('admin/config/development/performance'), '%variable' => "\$conf['page_cache_invoke_hooks']")),
  561. '#disabled' => !variable_get('cache', 0) || !variable_get('page_cache_invoke_hooks', TRUE),
  562. );
  563. $form['redirect_purge_inactive'] = array(
  564. '#type' => 'select',
  565. '#title' => t('Delete redirects that have not been accessed for'),
  566. '#default_value' => variable_get('redirect_purge_inactive', 0),
  567. '#options' => array(0 => t('Never (do not discard)')) + drupal_map_assoc(array(604800, 1209600, 1814400, 2592000, 5184000, 7776000, 10368000, 15552000, 31536000), 'format_interval'),
  568. '#description' => t('Only redirects managaged by the redirect module itself will be deleted. Redirects managed by other modules will be left alone.'),
  569. '#disabled' => variable_get('redirect_page_cache', 0) && !variable_get('page_cache_invoke_hooks', TRUE),
  570. );
  571. $form['globals'] = array(
  572. '#type' => 'fieldset',
  573. '#title' => t('Always enabled redirections'),
  574. '#description' => t('(formerly Global Redirect features)'),
  575. '#access' => FALSE,
  576. );
  577. $form['globals']['redirect_global_home'] = array(
  578. '#type' => 'checkbox',
  579. '#title' => t('Redirect from paths like index.php and /node to the root directory.'),
  580. '#default_value' => variable_get('redirect_global_home', 1),
  581. '#access' => FALSE,
  582. );
  583. $form['globals']['redirect_global_clean'] = array(
  584. '#type' => 'checkbox',
  585. '#title' => t('Redirect from non-clean URLs to clean URLs.'),
  586. '#default_value' => variable_get('redirect_global_clean', 1),
  587. '#disabled' => !variable_get('clean_url', 0),
  588. '#access' => FALSE,
  589. );
  590. $form['globals']['redirect_global_canonical'] = array(
  591. '#type' => 'checkbox',
  592. '#title' => t('Redirect from non-canonical URLs to the canonical URLs.'),
  593. '#default_value' => variable_get('redirect_global_canonical', 1),
  594. );
  595. $form['globals']['redirect_global_deslash'] = array(
  596. '#type' => 'checkbox',
  597. '#title' => t('Remove trailing slashes from paths.'),
  598. '#default_value' => variable_get('redirect_global_deslash', 0),
  599. '#access' => FALSE,
  600. );
  601. $form['globals']['redirect_global_admin_paths'] = array(
  602. '#type' => 'checkbox',
  603. '#title' => t('Allow redirections on admin paths.'),
  604. '#default_value' => variable_get('redirect_global_admin_paths', 0),
  605. );
  606. $form['#submit'][] = 'redirect_settings_form_submit';
  607. return system_settings_form($form);
  608. }
  609. /**
  610. * Form submit handler; clears the page cache.
  611. *
  612. * @see redirect_settings_form()
  613. */
  614. function redirect_settings_form_submit($form, &$form_state) {
  615. redirect_page_cache_clear();
  616. }
  617. function redirect_404_list($form = NULL) {
  618. $destination = drupal_get_destination();
  619. // Get filter keys and add the filter form.
  620. $keys = func_get_args();
  621. //$keys = array_splice($keys, 2); // Offset the $form and $form_state parameters.
  622. $keys = implode('/', $keys);
  623. $build['redirect_list_404_filter_form'] = drupal_get_form('redirect_list_404_filter_form', $keys);
  624. $header = array(
  625. array('data' => t('Page'), 'field' => 'message'),
  626. array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
  627. array('data' => t('Last accessed'), 'field' => 'timestamp'),
  628. array('data' => t('Operations')),
  629. );
  630. $count_query = db_select('watchdog', 'w');
  631. $count_query->addExpression('COUNT(DISTINCT(w.message))');
  632. $count_query->leftJoin('redirect', 'r', 'w.message = r.source');
  633. $count_query->condition('w.type', 'page not found');
  634. $count_query->isNull('r.rid');
  635. redirect_build_filter_query($count_query, array('w.message'), $keys);
  636. $query = db_select('watchdog', 'w')->extend('PagerDefault')->extend('TableSort');
  637. $query->fields('w', array('message'));
  638. $query->addExpression('COUNT(wid)', 'count');
  639. $query->addExpression('MAX(timestamp)', 'timestamp');
  640. $query->leftJoin('redirect', 'r', 'w.message = r.source');
  641. $query->isNull('r.rid');
  642. $query->condition('w.type', 'page not found');
  643. $query->groupBy('w.message');
  644. $query->orderByHeader($header);
  645. $query->limit(25);
  646. redirect_build_filter_query($query, array('w.message'), $keys);
  647. $query->setCountQuery($count_query);
  648. $results = $query->execute();
  649. $rows = array();
  650. foreach ($results as $result) {
  651. $row = array();
  652. $row['source'] = l($result->message, $result->message, array('query' => $destination));
  653. $row['count'] = $result->count;
  654. $row['timestamp'] = format_date($result->timestamp, 'short');
  655. $operations = array();
  656. if (redirect_access('create', 'redirect')) {
  657. $operations['add'] = array(
  658. 'title' => t('Add redirect'),
  659. 'href' => 'admin/config/search/redirect/add/',
  660. 'query' => array('source' => $result->message) + $destination,
  661. );
  662. }
  663. $row['operations'] = array(
  664. 'data' => array(
  665. '#theme' => 'links',
  666. '#links' => $operations,
  667. '#attributes' => array('class' => array('links', 'inline', 'nowrap')),
  668. ),
  669. );
  670. $rows[] = $row;
  671. }
  672. $build['redirect_404_table'] = array(
  673. '#theme' => 'table',
  674. '#header' => $header,
  675. '#rows' => $rows,
  676. '#empty' => t('No 404 pages without redirects found.'),
  677. );
  678. $build['redirect_404_pager'] = array('#theme' => 'pager');
  679. return $build;
  680. }
  681. /**
  682. * Return a form to filter URL redirects.
  683. *
  684. * @see redirect_list_filter_form_submit()
  685. *
  686. * @ingroup forms
  687. */
  688. function redirect_list_404_filter_form($form, &$form_state, $keys = '') {
  689. $form['#attributes'] = array('class' => array('search-form'));
  690. $form['basic'] = array(
  691. '#type' => 'fieldset',
  692. '#title' => t('Filter 404s'),
  693. '#attributes' => array('class' => array('container-inline')),
  694. );
  695. $form['basic']['filter'] = array(
  696. '#type' => 'textfield',
  697. '#title' => '',
  698. '#default_value' => $keys,
  699. '#maxlength' => 128,
  700. '#size' => 25,
  701. );
  702. $form['basic']['submit'] = array(
  703. '#type' => 'submit',
  704. '#value' => t('Filter'),
  705. '#submit' => array('redirect_list_404_filter_form_submit'),
  706. );
  707. if ($keys) {
  708. $form['basic']['reset'] = array(
  709. '#type' => 'submit',
  710. '#value' => t('Reset'),
  711. '#submit' => array('redirect_list_404_filter_form_reset'),
  712. );
  713. }
  714. return $form;
  715. }
  716. /**
  717. * Process filter form submission when the Filter button is pressed.
  718. */
  719. function redirect_list_404_filter_form_submit($form, &$form_state) {
  720. $form_state['redirect'] = 'admin/config/search/redirect/404/' . trim($form_state['values']['filter']);
  721. }
  722. /**
  723. * Process filter form submission when the Reset button is pressed.
  724. */
  725. function redirect_list_404_filter_form_reset($form, &$form_state) {
  726. $form_state['redirect'] = 'admin/config/search/redirect/404';
  727. }
  728. function redirect_list_table($redirects, $header) {
  729. $destination = drupal_get_destination();
  730. $default_status_code = variable_get('redirect_default_status_code', 301);
  731. // Set up the header.
  732. $header = array_combine($header, $header);
  733. $header = array_intersect_key(array(
  734. 'source' => array('data' => t('From'), 'field' => 'source', 'sort' => 'asc'),
  735. 'redirect' => array('data' => t('To'), 'field' => 'redirect'),
  736. 'status_code' => array('data' => t('Type'), 'field' => 'status_code'),
  737. 'language' => array('data' => t('Language'), 'field' => 'language'),
  738. 'count' => array('data' => t('Count'), 'field' => 'count'),
  739. 'access' => array('data' => t('Last accessed'), 'field' => 'access'),
  740. 'operations' => array('data' => t('Operations')),
  741. ), $header);
  742. // Do not include the language column if locale is disabled.
  743. if (!module_exists('locale')) {
  744. unset($header['language']);
  745. }
  746. $rows = array();
  747. foreach ($redirects as $rid => $redirect) {
  748. $row = array();
  749. $redirect->source_options = array_merge($redirect->source_options, array('alias' => TRUE, 'language' => redirect_language_load($redirect->language)));
  750. $source_url = redirect_url($redirect->source, $redirect->source_options);
  751. $redirect_url = redirect_url($redirect->redirect, array_merge($redirect->redirect_options, array('alias' => TRUE)));
  752. $row['data']['source'] = l($source_url, $redirect->source, $redirect->source_options);
  753. $row['data']['redirect'] = l($redirect_url, $redirect->redirect, $redirect->redirect_options);
  754. $row['data']['status_code'] = $redirect->status_code ? $redirect->status_code : t('Default (@default)', array('@default' => $default_status_code));
  755. $row['data']['language'] = module_invoke('locale', 'language_name', $redirect->language);
  756. $row['data']['count'] = $redirect->count;
  757. if ($redirect->access) {
  758. $row['data']['access'] = array(
  759. 'data' => t('!interval ago', array('!interval' => format_interval(REQUEST_TIME - $redirect->access))),
  760. 'title' => t('Last accessed on @date', array('@date' => format_date($redirect->access))),
  761. );
  762. }
  763. else {
  764. $row['data']['access'] = t('Never');
  765. }
  766. // Mark redirects that override existing paths with a warning in the table.
  767. if (drupal_valid_path($redirect->source)) {
  768. $row['class'][] = 'warning';
  769. $row['title'] = t('This redirect overrides an existing internal path.');
  770. }
  771. $operations = array();
  772. if (redirect_access('update', $redirect)) {
  773. $operations['edit'] = array(
  774. 'title' => t('Edit'),
  775. 'href' => 'admin/config/search/redirect/edit/' . $rid,
  776. 'query' => $destination,
  777. );
  778. }
  779. if (redirect_access('delete', $redirect)) {
  780. $operations['delete'] = array(
  781. 'title' => t('Delete'),
  782. 'href' => 'admin/config/search/redirect/delete/' . $rid,
  783. 'query' => $destination,
  784. );
  785. }
  786. $row['data']['operations'] = array(
  787. 'data' => array(
  788. '#theme' => 'links',
  789. '#links' => $operations,
  790. '#attributes' => array('class' => array('links', 'inline', 'nowrap')),
  791. ),
  792. );
  793. $row['data'] = array_intersect_key($row['data'], $header);
  794. $rows[$rid] = $row;
  795. }
  796. $build['list'] = array(
  797. '#theme' => 'table',
  798. '#header' => $header,
  799. '#rows' => $rows,
  800. '#empty' => t('No URL redirects available.'),
  801. '#attributes' => array('class' => array('redirect-list')),
  802. );
  803. return $build;
  804. }