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

Contents of /contributions/modules/ext_link_page/ext_link_page.module

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


Revision 1.1 - (show annotations) (download) (as text)
Tue Apr 8 22:00:16 2008 UTC (19 months, 2 weeks ago) by binarybill
Branch: MAIN
CVS Tags: DRUPAL-5--0-8, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
File MIME type: text/x-php
Initial commit of External Link Page (ext_link_page) module.  This module provides a custom page for all external links to go to in order to notify the user that they are leaving the site.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * The main module logic for ext_link_page module
7 *
8 * The various hooks and functions that make up this module.
9 */
10
11
12 /**
13 * Display help and module information for ext_link_page
14 * Implementation of hook_help()
15 *
16 * @param $section
17 * which section of the site we're displaying help
18 * @return string
19 * help text for section
20 */
21 function ext_link_page_help($section='') {
22
23 $output = '';
24
25 switch ($section) {
26 case "admin/help#ext_link_page":
27 $output = '<p>'. t("Any external links in a page will go to page that states the user is leaving the site.") .'</p>';
28
29 break;
30 } //end switch
31
32 return $output;
33 } // function ext_link_page_help
34
35
36 /**
37 * Actions to be performed when module is enabled
38 * Implementation of hook_enable()
39 *
40 */
41 function ext_link_page_enable() {
42
43 // get defaults
44 $arr_defaults = _ext_link_page_get_defaults();
45
46 //set default values
47 foreach ($arr_defaults as $key => $value) {
48 variable_set($key, $value);
49 } // end foreach
50
51 //send message to drupal
52 drupal_set_message(t("External Link Page has intialized default values."));
53 } // function ext_link_page_enable
54
55
56 /**
57 * Actions to be performed when module is disabled
58 * Implementation of hook_disable()
59 *
60 */
61 function ext_link_page_disable() {
62
63 // get defaults
64 $arr_defaults = _ext_link_page_get_defaults();
65
66 //set default values
67 foreach ($arr_defaults as $key => $value) {
68 variable_del($key);
69 } // end foreach
70
71 //send message to drupal
72 drupal_set_message(t("External Link Page has removed settings."));
73 } // function ext_link_page_disable
74
75
76
77 /**
78 * Permissions for this module
79 * Implementation of hook_perm()
80 *
81 * @return array
82 * an array of valid permissions for the ext_link_page module
83 */
84 function ext_link_page_perm() {
85
86 return array('administer ext_link_page');
87 } // function ext_link_page_perm
88
89
90
91 /**
92 * Menu settings for this module
93 * Implementation of hook_menu()
94 *
95 * @param $may_cache
96 * boolean for caching
97 * @return array
98 * menu items
99 */
100 function ext_link_page_menu($may_cache) {
101
102 // get defaults
103 $arr_defaults = _ext_link_page_get_defaults();
104
105 // put together menu array
106 $items = array();
107 // admin page
108 if ($may_cache) {
109 $items[] = array(
110 'path' => 'admin/settings/ext_link_page',
111 'title' => t('External link page'),
112 'description' => t('Define the behavoir of External Link Page module'),
113 'callback' => 'drupal_get_form',
114 'callback arguments' => 'ext_link_page_admin',
115 'access' => user_access('administer ext_link_page'),
116 'type' => MENU_NORMAL_ITEM
117 );
118 } // endif
119 // external link page
120 $items[] = array(
121 'path' => variable_get('ext_link_page_link_page', $arr_defaults['ext_link_page_link_page']),
122 'title' => variable_get('ext_link_page_page_title', $arr_defaults['ext_link_page_page_title']),
123 'description' => variable_get('ext_link_page_direct_delay', $arr_defaults['ext_link_page_page_title']),
124 'access' => true,
125 'callback' => 'ext_link_page_build_page',
126 'type' => MENU_CALLBACK
127 );
128
129 return $items;
130 } // function ext_link_page_menu
131
132
133 /**
134 * Implementation of hook_filter()
135 *
136 * @param $op
137 * operation of filter
138 * @param $delta
139 * which filter to use
140 * @param $format
141 * which format the filter is being used for
142 * @param $text
143 * the content to filter
144 * @return
145 * dependent on operation
146 */
147 function ext_link_page_filter($op, $delta = 0, $format = -1, $text = '') {
148
149 switch ($op) {
150 case 'list':
151 return array(0 => t('External links page'));
152
153 case 'description':
154 return t('Directs all External Links to the configured page to notify the user is leaving the site.');
155
156 case 'process':
157 // find all href so we can test if the link is external or not
158 $text = preg_replace_callback('/href="([^">]+)"/', ext_link_page_replace, $text);
159 return $text;
160
161 default:
162 return $text;
163 } // end switch
164 } // function ext_link_page_filter
165
166
167 /**
168 * Admin Settings Page for ext_link_page
169 *
170 * @return
171 * page output
172 */
173 function ext_link_page_admin() {
174
175 // define default values
176 $arr_defaults = _ext_link_page_get_defaults();
177
178 // put together form elements
179 $form['ext_link_page_direct_delay'] = array(
180 '#type' => 'textfield',
181 '#title' => t('Direct Delay'),
182 '#default_value' => variable_get('ext_link_page_direct_delay', $arr_defaults['ext_link_page_direct_delay']),
183 '#maxlength' => 4,
184 '#description' => t("The delay (in seconds) before the user will be redirected to the new site.")
185 );
186 $form['ext_link_page_link_page'] = array(
187 '#type' => 'textfield',
188 '#title' => t('Link Page'),
189 '#default_value' => variable_get('ext_link_page_link_page', $arr_defaults['ext_link_page_link_page']),
190 '#maxlength' => 256,
191 '#description' => t("The page that external links will be directed to. Example: !link",
192 array( '!link' => url(variable_get('ext_link_page_link_page', $arr_defaults['link_page']), 'http://example.com')))
193 );
194 $form['ext_link_page_page_title'] = array(
195 '#type' => 'textfield',
196 '#title' => t('Page Title'),
197 '#maxlength' => 256,
198 '#default_value' => variable_get('ext_link_page_page_title', $arr_defaults['ext_link_page_page_title']),
199 '#description' => t("The title of the external link page.")
200 );
201 $form['ext_link_page_page_message'] = array(
202 '#type' => 'textarea',
203 '#title' => t('Page Message'),
204 '#default_value' => variable_get('ext_link_page_page_message', $arr_defaults['ext_link_page_page_message']),
205 '#description' => t("The message that will be displayed on the Link Page. You may use the following values variable: [site_name], [url], [delay], [link].")
206 );
207
208 return system_settings_form($form);
209 } //function ext_link_page_admin
210
211
212 /**
213 * Link Page
214 *
215 * @return
216 * page output
217 */
218 function ext_link_page_build_page() {
219
220 // sanitize url query
221 $str_url = $_GET['url'];
222 $str_url_regex = "^(https?|ftp)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$";
223 if (eregi($str_url_regex, $str_url)) {
224 $str_url = $str_url;
225 }
226 else {
227 $str_url = url(null, null, null, true);
228 } // endif
229
230 // get variables
231 $arr_defaults = _ext_link_page_get_defaults();
232 $str_delay = variable_get('ext_link_page_direct_delay', $arr_defaults['ext_link_page_direct_delay']);
233 $str_page_message = variable_get('ext_link_page_page_message', $arr_defaults['ext_link_page_page_message']);
234 $str_site_name = '<span class="ext_link_page-site_name">'. variable_get('site_name', 'site_name') .'</span>';
235 $str_link = l($str_url, $str_url, $attributes = array('class' => 'ext_link_page-link'));
236
237 // make redirect and add to header
238 $str_redirect = '<meta http-equiv="refresh" content="'. $str_delay .'; url='. $str_url .'">';
239 drupal_set_html_head($str_redirect);
240
241 // put together variables
242 $str_page_message = str_replace('[link]', $str_link, $str_page_message);
243 $str_page_message = str_replace('[url]', $str_url, $str_page_message);
244 $str_page_message = str_replace('[delay]', $str_delay, $str_page_message);
245 $str_page_message = str_replace('[site_name]', $str_site_name, $str_page_message);
246
247 // call theme function
248 return theme('ext_link_page', $str_page_message);
249 } // function ext_link_page_build_page
250
251
252 /**
253 * Themeable function foru External Link Page output
254 *
255 * @param $message
256 * message for page
257 * @return
258 * page output
259 */
260 function theme_ext_link_page($message) {
261
262 $output = '';
263
264 // create output
265 $output = "<div class=\"ext_link_page-container\">". $message ."</div>\n";
266
267 return $output;
268 } // function theme_ext_link_page
269
270
271 /**
272 * Replace Logic
273 *
274 * @return
275 * page output
276 */
277 function ext_link_page_replace($match) {
278
279 $arr_defaults = _ext_link_page_get_defaults();
280 $url = $match[1];
281 $base_url = url(null, null, null, true);
282
283 // if the link is external (starts with http) and it's not an absolute link to the current site
284 if (strpos($match[1], 'http') === 0 && strpos($match[1], $base_url) === false) {
285 $url = url(variable_get('ext_link_page_link_page', $arr_defaults['ext_link_page_link_page']), 'url='. $match[1]);
286 } //endif
287
288 return 'href="'. $url .'"';
289 } // function ext_link_page_replace
290
291
292 /**
293 * Get Default array
294 *
295 * @return array
296 * page output
297 */
298 function _ext_link_page_get_defaults() {
299
300 $arr_defaults = array();
301 $arr_defaults = array(
302 'ext_link_page_direct_delay' => '5',
303 'ext_link_page_link_page' => 'ext_link',
304 'ext_link_page_page_title' => 'Leaving this Site',
305 'ext_link_page_page_message' => 'You are about to leave [site_name]. If you are not taken there in [delay] seconds, follow this [link].'
306 );
307
308 return $arr_defaults;
309 } // function _ext_link_page_get_defaults

  ViewVC Help
Powered by ViewVC 1.1.2