extlink.module 8.5 KB

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