extlink.module 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. function extlink_menu() {
  3. $items = array();
  4. $items['admin/config/user-interface/extlink'] = array(
  5. 'title' => 'External links',
  6. 'description' => 'Alter the display of external links on the site.',
  7. 'page callback' => 'drupal_get_form',
  8. 'page arguments' => array('extlink_admin_settings'),
  9. 'access callback' => 'user_access',
  10. 'access arguments' => array('administer site configuration'),
  11. );
  12. return $items;
  13. }
  14. /**
  15. * Implementation of hook_init().
  16. */
  17. function extlink_page_build() {
  18. $path = drupal_get_path('module', 'extlink');
  19. drupal_add_js($path .'/extlink.js', array('every_page' => TRUE));
  20. drupal_add_js(array('extlink' => array(
  21. 'extTarget' => variable_get('extlink_target', 0),
  22. 'extClass' => variable_get('extlink_class', 'ext'),
  23. 'extSubdomains' => variable_get('extlink_subdomains', 1),
  24. 'extExclude' => variable_get('extlink_exclude', ''),
  25. 'extInclude' => variable_get('extlink_include', ''),
  26. 'extCssExclude' => variable_get('extlink_css_exclude', ''),
  27. 'extCssExplicit' => variable_get('extlink_css_explicit', ''),
  28. 'extAlert' => variable_get('extlink_alert', 0),
  29. 'extAlertText' => variable_get('extlink_alert_text', 'This link will take you to an external web site. We are not responsible for their content.'),
  30. 'mailtoClass' => variable_get('extlink_mailto_class', 'mailto'))), 'setting'
  31. );
  32. }
  33. function extlink_admin_settings() {
  34. $form = array();
  35. $form['extlink_class'] = array(
  36. '#type' => 'checkbox',
  37. '#title' => t('Place an icon next to external links.'),
  38. '#return_value' => 'ext',
  39. '#default_value' => variable_get('extlink_class', 'ext'),
  40. '#description' => t('Places an !icon icon next to external links.', array('!icon' => theme('image', array('path' => drupal_get_path('module', 'extlink') . '/extlink.png', 'alt' => t('External Links icon'))))),
  41. );
  42. $form['extlink_mailto_class'] = array(
  43. '#type' => 'checkbox',
  44. '#title' => t('Place an icon next to mailto links'),
  45. '#return_value' => 'mailto',
  46. '#default_value' => variable_get('extlink_mailto_class', 'mailto'),
  47. '#description' => t('Places an !icon icon next to mailto links.', array('!icon' => theme('image',array('path' => drupal_get_path('module', 'extlink') . '/mailto.png', 'alt' => t('Email links icon'))))),
  48. );
  49. $form['extlink_subdomains'] = array(
  50. '#type' => 'checkbox',
  51. '#title' => t('Exclude links with the same primary domain.'),
  52. '#default_value' => variable_get('extlink_subdomains', 1),
  53. '#description' => t("For example, a link from 'www.example.com' to the subdomain of 'my.example.com' would be excluded."),
  54. );
  55. $form['extlink_target'] = array(
  56. '#type' => 'checkbox',
  57. '#title' => t('Open external links in a new window'),
  58. '#return_value' => '_blank',
  59. '#default_value' => variable_get('extlink_target', 0),
  60. );
  61. $form['extlink_alert'] = array(
  62. '#type' => 'checkbox',
  63. '#title' => t('Display a pop-up warning when any external link is clicked.'),
  64. '#return_value' => '_blank',
  65. '#default_value' => variable_get('extlink_alert', 0),
  66. );
  67. $form['extlink_alert_text'] = array(
  68. '#type' => 'textarea',
  69. '#title' => t('Text to display in the pop-up warning box.'),
  70. '#rows' => 3,
  71. '#default_value' => variable_get('extlink_alert_text', 'This link will take you to an external web site.'),
  72. '#wysiwyg' => FALSE,
  73. '#states' => array(
  74. // Only show this field when user opts to display a pop-up warning.
  75. 'visible' => array(
  76. ':input[name="extlink_alert"]' => array('checked' => TRUE),
  77. ),
  78. ),
  79. );
  80. $patterns = array(
  81. '<em>(example\.com)</em> ' . t('Matches example.com.'),
  82. '<em>(example\.com)|(example\.net)</em> ' . t('Multiple patterns can be strung together by using a pipe. Matches example.com OR example.net.'),
  83. '<em>(links/goto/[0-9]+/[0-9]+)</em> ' . t('Matches links that go through the <a href="http://drupal.org/project/links">Links module</a> redirect.'),
  84. );
  85. $wildcards = array(
  86. '<em>.</em> ' . t('Matches any character.'),
  87. '<em>?</em> ' . t('The previous character or set is optional.'),
  88. '<em>\d</em> ' . t('Matches any digit (0-9).'),
  89. '<em>[a-z]</em> ' . t('Brackets may be used to match a custom set of characters. This matches any alphabetic letter.'),
  90. );
  91. $form['patterns'] = array(
  92. '#tree' => FALSE,
  93. '#type' => 'fieldset',
  94. '#title' => t('Pattern matching'),
  95. '#collapsible' => TRUE,
  96. '#collapsed' => TRUE,
  97. '#description' =>
  98. '<p>' . t('External links uses patterns (regular expressions) to match the "href" property of links.') . '</p>' .
  99. t('Here are some common patterns.') .
  100. theme('item_list', array('items' => $patterns)) .
  101. t('Common special characters:') .
  102. theme('item_list', array('items' => $wildcards)) .
  103. '<p>' . t('All special characters (<em>^ $ . ? ( ) | * +</em>) must also be escaped with backslashes. Patterns are not case-sensitive. Any <a href="http://www.javascriptkit.com/javatutors/redev2.shtml">pattern supported by JavaScript</a> may be used.') . '</p>',
  104. );
  105. $form['patterns']['extlink_exclude'] = array(
  106. '#type' => 'textfield',
  107. '#title' => t('Exclude links matching the pattern'),
  108. '#maxlength' => NULL,
  109. '#default_value' => variable_get('extlink_exclude', ''),
  110. '#description' => t('Enter a regular expression for links that you wish to exclude from being considered external.'),
  111. );
  112. $form['patterns']['extlink_include'] = array(
  113. '#type' => 'textfield',
  114. '#title' => t('Include links matching the pattern'),
  115. '#maxlength' => NULL,
  116. '#default_value' => variable_get('extlink_include', ''),
  117. '#description' => t('Enter a regular expression for internal links that you wish to be considered external.'),
  118. );
  119. $form['css_matching'] = array(
  120. '#tree' => FALSE,
  121. '#type' => 'fieldset',
  122. '#title' => t('CSS Matching'),
  123. '#collapsible' => TRUE,
  124. '#collapsed' => TRUE,
  125. '#description' =>
  126. '<p>' . t('Use CSS selectors to exclude entirely or only look inside explicitly specified classes and IDs for external links. These will be passed straight to jQuery for matching.') . '</p>',
  127. );
  128. $form['css_matching']['extlink_css_exclude'] = array(
  129. '#type' => 'textarea',
  130. '#title' => t('Exclude links inside these CSS selectors'),
  131. '#maxlength' => NULL,
  132. '#default_value' => variable_get('extlink_css_exclude', ''),
  133. '#description' => t('Enter a comma-separated list of CSS selectors (ie "#block-block-2 .content, ul.menu")'),
  134. );
  135. $form['css_matching']['extlink_css_explicit'] = array(
  136. '#type' => 'textarea',
  137. '#title' => t('Only look for links inside these CSS selectors'),
  138. '#maxlength' => NULL,
  139. '#default_value' => variable_get('extlink_css_explicit', ''),
  140. '#description' => t('Enter a comma-separated list of CSS selectors (ie "#block-block-2 .content, ul.menu")'),
  141. );
  142. return system_settings_form($form);
  143. }
  144. function extlink_admin_settings_validate($form, &$form_state) {
  145. // Check if the exclude pattern is a valid regular expression
  146. if ($exclude = $form_state['values']['extlink_exclude']) {
  147. // Testing the regex via replace
  148. $regexeval = @preg_replace('/' . $exclude . '/', '', 'Lorem ipsum');
  149. // If the regex returns NULL, then throw an error and reset the variable
  150. if ($regexeval === NULL) {
  151. form_set_error('extlink_exclude', t('Invalid regular expression.'));
  152. variable_set('extlink_exclude', '');
  153. }
  154. }
  155. // Check if the include pattern is a valid regular expression
  156. if ($include = $form_state['values']['extlink_include']) {
  157. // Testing the regex via replace
  158. $regexeval = @preg_replace('/' . $include . '/', '', 'Lorem ipsum');
  159. // If the regex returns NULL, then throw an error and reset the variable
  160. if ($regexeval === NULL) {
  161. form_set_error('extlink_include', t('Invalid regular expression.'));
  162. variable_set('extlink_include', '');
  163. }
  164. }
  165. }