/[drupal]/contributions/modules/elf/elf.module
ViewVC logotype

Contents of /contributions/modules/elf/elf.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Sun Jan 27 02:13:40 2008 UTC (22 months ago) by m3avrck
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
new external links filter (elf) module, replaces old olf module
1 <?php
2 // $Id: elf.module,v 1.3 2006/10/10 14:51:18 m3avrck Exp $
3
4 /**
5 * Implementation of hook_menu().
6 */
7 function elf_menu($may_cache) {
8 $items = array();
9
10 if ($may_cache) {
11 $items[] = array(
12 'path' => 'admin/settings/elf',
13 'title' => t('External links filter'),
14 'description' => t('Configure external links filter'),
15 'callback' => 'drupal_get_form',
16 'callback arguments' => array('elf_admin_settings'),
17 'access' => user_access('administer site configuration')
18 );
19 }
20 else {
21 if (variable_get('elf_css', TRUE)) {
22 $path = drupal_get_path('module', 'elf');
23 drupal_add_css($path .'/elf.css');
24 }
25 }
26
27 return $items;
28 }
29
30 function elf_admin_settings() {
31 $form = array();
32
33 $form['elf_css'] = array(
34 '#type' => 'checkbox',
35 '#default_value' => variable_get('elf_css', TRUE),
36 '#title' => t('Add CSS to place image next to all external and mailto links'),
37 '#description' => t('When enabled, this will include a CSS file on each page that adds an !icon next to each external and mailto link with the class "external-link" or "mailto-link".', array('!icon' => theme_image(drupal_get_path('module', 'elf') .'/elf.png'))),
38 );
39
40 return system_settings_form($form);
41 }
42
43 /**
44 * Implementation of hook_filter().
45 */
46 function elf_filter($op, $delta = 0, $format = -1, $text = '') {
47 switch ($op) {
48 case 'list':
49 return array(0 => t('External links filter'));
50
51 case 'description':
52 return t('Adds a CSS class to all external and mailto links.');
53
54 case 'process':
55 // find all <a> tags and match their href so we can test if the link is external or not
56 $text = preg_replace_callback('!<a.*?href="([^"]+)".*?>!', elf_replace, $text);
57 return $text;
58
59 default:
60 return $text;
61 }
62 }
63
64 function elf_replace($match) {
65 $link = $match[0];
66 $site_url = url(NULL, NULL, NULL, TRUE);
67
68 // if the link is external (e.g., it starts with http) and it's not an absolute link to the current site
69 if (strpos($match[1], 'http') === 0 && strpos($match[1], $site_url) === FALSE) {
70 // if there is no class
71 if (strpos($match[0], 'class="') === FALSE) {
72 // it is faster to use PHP's string functions then complex regexes
73 // in this case we strip off the last > in the <a> and then append our class and close the tag
74 $link = substr($match[0], 0, -1);
75 $link .= ' class="external-link">';
76 }
77 else {
78 // append external class if this <a> already has a class
79 $link = preg_replace('!class="([^"]+)"!', 'class="${1} external-link"', $match[0]);
80 }
81 }
82 // mailto link
83 else if (strpos($match[1], 'mailto:') == 0) {
84 // if there is no class
85 if (strpos($match[0], 'class="') === FALSE) {
86 // it is faster to use PHP's string functions then complex regexes
87 // in this case we strip off the last > in the <a> and then append our class and close the tag
88 $link = substr($match[0], 0, -1);
89 $link .= ' class="mailto-link">';
90 }
91 else {
92 // append external class if this <a> already has a class
93 $link = preg_replace('!class="([^"]+)"!', 'class="${1} mailto-link"', $match[0]);
94 }
95 }
96
97 return $link;
98 }

  ViewVC Help
Powered by ViewVC 1.1.2