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

Contents of /contributions/modules/urlicon/urlicon.module

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


Revision 1.25 - (show annotations) (download) (as text)
Mon Oct 6 21:12:36 2008 UTC (13 months, 3 weeks ago) by sanduhrs
Branch: MAIN
CVS Tags: HEAD
Changes since 1.24: +5 -5 lines
File MIME type: text/x-php
#299748 by nirvanajyothi: Fixed missing default favicon
1 <?php
2 // $Id: urlicon.module,v 1.24 2008/07/28 21:07:54 sanduhrs Exp $
3
4 /**
5 * URL Icon
6 *
7 * @file
8 * Filter all external links in nodes and comments.
9 *
10 * @author
11 * Stefan Auditor <stefan.auditor@erdfisch.de>
12 * for erdfisch http://erdfisch.de
13 */
14
15 // Path to directory where favicons are stored
16 define('UI_FILE_PATH', file_directory_path() .'/urlicon');
17 define('UI_FORMAT_FAVICON', 0);
18 define('UI_FORMAT_ICON', 1);
19 define('UI_FORMAT_CLASS', 2);
20
21 /**
22 * Implementation of hook_help().
23 */
24 function urlicon_help($path, $arg) {
25 switch ($path) {
26 case 'admin/help#urlicon':
27 $output = '<p>'. t('The URLIcon module automatically adds a CSS class to &lt;a&gt;-elements reflecting their target and fetches the favicon from the target site.') .'</p>';
28 $output .= '<p>'. t('Use Input Formats to enable the URL filter') .'</p>';
29 $output .= '<p>'. t('<ol><li>Select an existing Input Format or add a new one</li><li>Configure the Input Format</li><li>Enable URL class filter and Save configuration</li><li>Rearrange the weight of the URL filter depending on what filters exist in the format</li></ol>') .'</p>';
30 $output .= '<p>'. t('You can enable the urlfilter for an input format from <a href="%admin-filters">administer &gt;&gt; Site Configuration &gt;&gt; Input Filter</a>.', array('%admin-filters' => url('admin/settings/filters'))) .'</p>';
31 return $output;
32 case 'admin/modules#description':
33 return t('Automatically fetch favicons for URLs.');
34 }
35 }
36
37 /**
38 * Implementation of hook_init().
39 */
40 function urlicon_init() {
41 drupal_add_css(drupal_get_path('module', 'urlicon') .'/urlicon.css');
42 }
43
44 /**
45 * Implementation of hook_filter().
46 */
47 function urlicon_filter($op, $delta = 0, $format = -1, $text = '') {
48 switch ($op) {
49 case 'list':
50 return array(0 => t('URL Icon filter'));
51
52 case 'description':
53 return t('Adds favicons to URLs.');
54 break;
55 case 'process':
56 // check for directory
57 $dir = UI_FILE_PATH;
58 file_check_directory($dir, 1);
59
60 $reg_exp = '/<a.+?href=\"((http|https|ftp|telnet|news|mms):\/\/.+?)\"[^>]*>(.+?)<\/a>/i';
61
62 $ui_format = variable_get("filter_urlicon_$format", UI_FORMAT_FAVICON);
63 switch ($ui_format) {
64 case UI_FORMAT_FAVICON:
65 $text = preg_replace_callback($reg_exp, '_urlicon_format_favicon', $text);
66 break;
67 case UI_FORMAT_ICON:
68 $text = preg_replace_callback($reg_exp, '_urlicon_format_icon', $text);
69 break;
70 case UI_FORMAT_CLASS:
71 $text = preg_replace_callback($reg_exp, '_urlicon_format_class', $text);
72 break;
73 }
74
75 return $text;
76 break;
77 case 'settings':
78 return urlicon_settings($format);
79 break;
80 default:
81 return $text;
82 break;
83 }
84 }
85
86 /**
87 * Callback for filter
88 */
89 function _urlicon_format_class($match) {
90 $url = parse_url($match[1]);
91 $domain = explode('.', $url['host']);
92 $domain = $domain[(count($domain)-2)];
93
94 if (stristr($match[0], 'class')) $match[0] = str_replace('class="', 'class="uc-'. check_plain($domain) .' ', $match[0]);
95 else $match[0] = str_replace('">', '" class="urlicon urlicon-'. check_plain($domain) .'">', $match[0]);
96
97 return $match[0];
98 }
99
100 /**
101 * Callback for filter
102 */
103 function _urlicon_format_icon($match) {
104 $dir = UI_FILE_PATH;
105
106 $url = @parse_url($match[1]);
107 $domain = explode('.', $url['host']);
108 $domain = check_url(str_replace('.', '_', $url['host']));
109
110 // check for favicon availability
111 $favicon = base_path() . drupal_get_path('module', 'urlicon') .'/Icon_External_Link.png';
112
113 $link = theme('urlicon', $match[3], $favicon, $match[1], array('alt' => '', 'title' => t('favicon'), 'class' => 'urlicon urlicon-'. check_plain($domain)));
114 return $link;
115 }
116
117 /**
118 * Callback for filter
119 */
120 function _urlicon_format_favicon($match) {
121 // Define acceptable Content-Types
122 // see http://www.iana.org/assignments/media-types/image/vnd.microsoft.icon
123 // Additional Content-Types suggested by W3C
124 // see http://www.w3.org/2005/10/howto-favicon
125 $ui_ctype = array(
126 // Suggested by IANA
127 'application/ico',
128 'application/octet-stream',
129 'image/vnd.microsoft.icon',
130 'image/ico',
131 'image/icon',
132 'image/x-icon',
133 'text/ico',
134 'text/plain',
135 // Suggested by W3C
136 'image/gif',
137 'image/png',
138 );
139 $dir = UI_FILE_PATH;
140
141 $url = @parse_url($match[1]);
142 $domain = explode('.', $url['host']);
143 $domain = check_url(str_replace('.', '_', $url['host']));
144
145 //check if favicon exists locally
146 if ($url['host'] AND !file_exists($dir .'/'. $domain .'.ico')) {
147
148 //check for favicon in metatags
149 $data = drupal_http_request(check_url($match[1]));
150
151 if (preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $data->data, $icons)) {
152
153 if (strpos($icons[1], '://')) {
154 // absolute path
155 $data = drupal_http_request(check_url($icons[1]));
156 }
157 else if (substr($icons[1], 0, 3) == '../') {
158 // relative path
159 $path = '';
160 $elements = explode('/', $url['path']);
161 $i = 0;
162 while (!strpos($elements[$i], '.') AND $i <= count($elements)) {
163 $path .= $elements[$i] .'/';
164 $i++;
165 }
166
167 $data = drupal_http_request(check_url($url['scheme'] .'://'. $url['host'] . $path . $icons[1]));
168 }
169 else if (substr($icons[1], 0, 1) == '/') {
170 // relative path
171 $data = drupal_http_request(check_url($url['scheme'] .'://'. $url['host'] . $icons[1]));
172 }
173 else {
174 // get favicon from webroot
175 $data = drupal_http_request(check_url('http://'. $url['host'] .'/favicon.ico'));
176 watchdog('urlicon', t('Could not find favicon for URL %url with shortcut url %shortcut, trying webroot.', array('%url' => $match[1], '%shortcut' => $icons[1])), WATCHDOG_ERROR);
177 }
178
179 }
180 else {
181 // get favicon from webroot
182 $data = drupal_http_request(check_url('http://'. $url['host'] .'/favicon.ico'));
183 watchdog('urlicon', t('Could not find favicon for URL %url in metatags, trying webroot.', array('%url' => $match[1])));
184 }
185
186 // Verify if the favicon was returned
187 if (($data->code == '200' OR $data->redirect_code == '200') AND ($data->headers['Content-Length'] > 0 OR $data->headers['Content-length'] > 0)) {
188 //check for acceptable Content-Type
189 //TODO: refactor code
190 $content_type_1 = explode(';', $data->headers['Content-Type']);
191 $content_type_2 = explode(';', $data->headers['Content-type']);
192
193 if (in_array($content_type_1[0], $ui_ctype) OR in_array($content_type_2[0], $ui_ctype)) {
194 //save favicon to file
195 file_save_data($data->data, $dir .'/'. $domain .'.ico', FILE_EXISTS_REPLACE);
196 }
197 }
198 }
199
200 // check for favicon availability
201 $favicon = file_exists($dir .'/'. $domain .'.ico') ? (file_create_url($dir .'/'. $domain .'.ico')) : (base_path().drupal_get_path('module', 'urlicon') .'/Icon_External_Link.png');
202
203 $link = theme('urlicon', $match[3], $favicon, $match[1], array('alt' => '', 'title' => t('favicon'), 'class' => 'urlicon urlicon-'. check_plain($domain)));
204 return $link;
205 }
206
207 /**
208 * Implementation of hook_file_download().
209 */
210 function urlicon_file_download($filepath) {
211 // Check if the file is controlled by the current module.
212 if (strpos($filepath, 'urlicon') !== FALSE) {
213 if (user_access('access content')) {
214 // This is an assumption
215 return array('Content-type: image/ico');
216 }
217 }
218 else {
219 return -1;
220 }
221 }
222
223 /**
224 * Settings form
225 */
226 function urlicon_settings($format) {
227 $form['urlicon'] = array(
228 '#type' => 'fieldset',
229 '#title' => t('URL Icon'),
230 '#collapsible' => TRUE,
231 );
232 $form['urlicon']["filter_urlicon_$format"] = array(
233 '#type' => 'radios',
234 '#title' => t('Filter URLs'),
235 '#default_value' => variable_get("filter_urlicon_$format", UI_FORMAT_FAVICON),
236 '#options' => array(
237 UI_FORMAT_FAVICON => t('Find all external URLs and append the according favicon (if available)'),
238 UI_FORMAT_ICON => t('Find all external URLs and append an <em>external link icon</em>'),
239 UI_FORMAT_CLASS => t('Find all external URLs and add a CSS class only (theme it as you like)'),
240 ),
241 '#description' => t('Choose what to add to a link in the markup.'),
242 );
243 $form['urlicon']["filter_urlicon_$format"] = array(
244 '#type' => 'radios',
245 '#title' => t('Filter URLs'),
246 '#default_value' => variable_get("filter_urlicon_$format", UI_FORMAT_FAVICON),
247 '#options' => array(
248 UI_FORMAT_FAVICON => t('Find all external URLs and append the according favicon (if available)'),
249 UI_FORMAT_ICON => t('Find all external URLs and append an <em>external link icon</em>'),
250 UI_FORMAT_CLASS => t('Find all external URLs and add a CSS class only (theme it as you like)'),
251 ),
252 '#description' => t('Choose what to add to a link in the markup.'),
253 );
254
255 return $form;
256 }
257
258 /**
259 * Implementation of hook_theme().
260 */
261 function urlicon_theme($existing, $type, $theme, $path) {
262 return array(
263 'urlicon' => array(
264 'arguments' => array(
265 'text' => NULL,
266 'favicon' => NULL,
267 'path' => NULL,
268 'attributes' => array(),
269 ),
270 ),
271 );
272 }
273
274 /**
275 * Return a themed link with a favicon.
276 */
277 function theme_urlicon($text, $favicon, $path, $attributes = array()) {
278 $favicon = '<img src="'. $favicon .'" '. drupal_attributes($attributes) .' />';
279 return l($text .' '. $favicon, $path, array('absolute' => TRUE, 'html' => TRUE));
280 }

  ViewVC Help
Powered by ViewVC 1.1.2