/[drupal]/contributions/modules/accessibility/accessibility.php
ViewVC logotype

Contents of /contributions/modules/accessibility/accessibility.php

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


Revision 1.2 - (show annotations) (download) (as text)
Tue Mar 4 10:09:27 2008 UTC (20 months, 3 weeks ago) by jaburns
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
Changes since 1.1: +19 -10 lines
File MIME type: text/x-php
Fixed PPC error again, and added failsafe for function.
1 <?php
2
3 // $Id: accessibility.php, v2.1 2008/03/04 15:15:00 jaburns Exp $
4
5 /**
6 * @file
7 * Part of accessibility.module
8 * Can be used as stand alone script
9 * Depends on 'browser.inc' -> list of know handheld devices
10 *
11 * @functions
12 * is_mobi() returns TRUE or FALSE dependant on the device browsing the site
13 * content_type() returns integer that should be linked to $mime['types']
14 *
15 * @author Jamie Burns <http://www.skiffie.com/>
16 * @link http://www.skiffie.com/
17 */
18
19 //~ For PHP < 5
20 if (!function_exists('stripos')) {
21 function stripos($haystack, $needle, $offset=0) {
22 return strpos(strtolower($haystack), strtolower($needle), $offset);
23 }
24 }
25
26 //~ $mime['VARS'] must be in order
27 global $mime;
28 $mime['types'] = array (
29 'none',
30 'xhtml',
31 'html',
32 'mhtml',
33 'wml',
34 'chtml'
35 );
36
37 $mime['xml'] = array (1,3,4);
38 $mime['mobis'] = array (3,4,5);
39 $mime['mhtml'] = 3;
40 $mime['wml'] = 4;
41 $mime['chtml'] = 5;
42 $mime['nodes'] = 1;
43
44 $mime['theme'] = array(
45 0 => 'web2',
46 1 => 'web2',
47 2 => 'web2',
48 3 => 'mobi',
49 4 => 'wap',
50 5 => 'mobi'
51 );
52
53
54 $mime['desc'] = array (
55 'No Override',
56 'XHTML',
57 'HTML',
58 'XHTML MP',
59 'WML',
60 'HTML BASIC'
61 );
62
63 $mime['content'] = array (
64 'text/html',
65 'application/xhtml+xml',
66 'text/html',
67 'application/vnd.wap.xhtml+xml',
68 'text/vnd.wap.wml',
69 'text/html'
70 );
71
72 $mime['dtds'] = array (
73 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
74 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n",
75 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
76 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n",
77 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
78 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n",
79 '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN"
80 "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile11.dtd"> '."\n",
81 '<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">',
82 '<!DOCTYPE html PUBLIC "-//W3C//DTD Compact HTML 1.0 Draft//EN">'."\n"
83 );
84
85 //~ Some extra user_agent strings to check for
86 global $imode;
87 $imode = array ('doco', 'j-pho', 'up.b', 'ddip', 'port');
88 global $extra;
89 $extra = array ('MIDP', 'symbian', 'phone', 'palm', 'ipaq', 'Googlebot-Mobile');
90
91 global $xml;
92 $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
93 global $dir;
94 $dir = dirname(__FILE__) .'/browser.inc';
95 global $fix;
96 $fix = (isset($fix)) ? $fix : FALSE;
97 //~ Extra function to catch additional mobi parameters: returns > 0 if TRUE
98 function extra(&$extra) {
99 $count = 0;
100
101 if (is_array($extra)) {
102 foreach($extra as $findme) {
103 if ( @stripos(@$_SERVER['HTTP_USER_AGENT'], $findme) !== FALSE ) {
104 $count++;
105 }
106 }
107 } else {
108 if ( @stripos(@$_SERVER['HTTP_USER_AGENT'], $extra) !== FALSE ) {
109 $count++;
110 }
111 }
112 return $count;
113 }
114
115 //~ Is the device a mobile ?
116 function is_mobi($override = FALSE) {
117 global $imode, $extra, $dir;
118
119 $browsers = file( $dir );
120 foreach ($browsers as $dev) {
121 $browserlist [] = trim($dev);
122 }
123 if ($override == TRUE) {
124 return $override;
125 } elseif (
126 @in_array(strtolower(substr(trim(@$_SERVER['HTTP_USER_AGENT']),0,4)), $browserlist) OR
127 @in_array(strtolower(substr(trim(@$_SERVER['HTTP_USER_AGENT']),0,4)), $imode) OR
128 extra($extra) > 0
129 ) {
130 return TRUE;
131 } else {
132 return FALSE;
133 }
134 }
135
136 //~ Establish best suited content type
137 function content_type () {
138 global $mime, $accessibility;
139 $is_mobi = (isset($accessibility['is_mobi'])) ? $accessibility['is_mobi'] : FALSE;
140
141 $accept = array();
142 foreach ($mime['types'] as $lang => $val){
143 $accept[$val] = $mime['content'][$lang];
144 }
145
146 //~ remove unwanted elements
147 $unused = array_shift($accept);
148 $unused = array_pop($accept);
149
150 $c = array();
151
152 if ( !isset($_SERVER["HTTP_ACCEPT"]) OR empty($_SERVER["HTTP_ACCEPT"]) ) {
153 $c['html'] = 1;
154 } else {
155
156 foreach ($accept as $mime_lang => $mime_type) {
157 $esc_type = '/'. str_replace( array ('/','.','+'), array('\/','\.','\+'), $mime_type) .";q=0(\.[1-9]+)/i";
158 $c[$mime_lang] = 1;
159 if ( @stristr(@$_SERVER["HTTP_ACCEPT"], $mime_type) ) {
160 $c[$mime_lang] = $c[$mime_lang] + 1;
161 if (preg_match($esc_type, @$_SERVER["HTTP_ACCEPT"], $matches)) {
162 $c[$mime_lang] = $c[$mime_lang] - (float)$matches[1];
163 } // end if pregmatch
164 } // end if stristr
165 } // end foreach accept
166
167 }
168
169 arsort ($c, SORT_NUMERIC);
170
171 if ( array_sum($c) == count($c) ){
172 unset ( $c );
173 $c['html'] = 1;
174 } // end array_sum
175
176 $max = max($c);
177 foreach ($c as $type => $val) {
178 if ($val != $max) { unset ( $c[$type] ); }
179 } // end foreach
180
181 if ( array_key_exists('xhtml', $c) ) { unset ( $c ); $c['xhtml'] = 1; }
182 if ( array_key_exists('html', $c) ) { unset ( $c ); $c['html'] = 1;}
183 if ( array_key_exists('html', $c) AND $is_mobi === TRUE ) { unset ( $c ); $c['chtml'] = 1;}
184 if ( array_key_exists('wml', $c) ) { unset ( $c ); $c['wml'] = 1; }
185 if ( array_key_exists('mhtml', $c) ) { unset ( $c ); $c['mhtml'] = 1; }
186 if ( stristr(@$_SERVER["HTTP_USER_AGENT"], 'W3C_Validator') ) { unset ( $c ); $c['xhtml'] = 1; }
187
188 return array_search(key($c), $mime['types']);
189
190 }
191
192 //~ Make code WML compatible
193 function wmlfix($buffer) {
194 global $fix;
195 $buffer = str_replace('Submitted by', '<p>Submitted by', $buffer);
196 $buffer = str_replace('<!--np-->', '<p>', $buffer);
197 $buffer = preg_replace('/<a href="\/taxonomy(.*?)class="feed-icon">(.*?)<\/a>/ims', '', $buffer);
198 $buffer = preg_replace('/<a id="comment">(.*?)<\/a>/ims', '', $buffer);
199 $buffer = preg_replace('/<span class="terms">(.*?)<\/span>/ims', '$1<br /><br />', $buffer);
200 $buffer = preg_replace('/<div class="service-label">(.*?)<\/div>/ims', '', $buffer);
201 $buffer = preg_replace('/<div class="service-links">(.*?)<\/div>/ims', '', $buffer);
202 $buffer = str_replace('<div class="pager">', '<p>', $buffer);
203 $buffer = str_replace('</a><', '</a> <', $buffer);
204 $buffer = preg_replace('#<div(.*?)>#', '', $buffer);
205 $buffer = str_replace('</div>', ' ', $buffer);
206 $buffer = str_replace('<hr />', '', $buffer);
207 $buffer = preg_replace('# class="(.*?)"#', '', $buffer);
208 $buffer = preg_replace('# rel="(.*?)"#', '', $buffer);
209 $buffer = preg_replace('#p id="(.*?)"#', 'p', $buffer);
210 $buffer = preg_replace('#p class="(.*?)"#', 'p', $buffer);
211 $buffer = preg_replace('# style="(.*?)"#', '', $buffer);
212 $buffer = preg_replace('# title="(.*?)"#', '', $buffer);
213 $buffer = preg_replace('/<a href="\/comment(.*?)<\/a>/ims', '', $buffer);
214 $buffer = preg_replace('/<a href="?q=comment(.*?)<\/a>/ims', '', $buffer);
215 $buffer = preg_replace('#<h(.*?)>#', '<p><big>', $buffer);
216 $buffer = preg_replace('#</h(.*?)>#', "</big></p>\n", $buffer);
217 $buffer = preg_replace('#<img (.*?) />#', '', $buffer);
218 $buffer = preg_replace('#<ul(.*?)>#', '<p>', $buffer);
219 $buffer = preg_replace('#<ol(.*?)>#', '<p>', $buffer);
220 $buffer = preg_replace('#<dl(.*?)>#', '<p>', $buffer);
221 $buffer = preg_replace('/<form(.*?)\/form>/ims', '<p>Please make comments through website!</p>', $buffer);
222 $buffer = str_replace('<dt>', '<strong>', $buffer);
223 $buffer = str_replace('</dt>', '</strong><br />', $buffer);
224 $buffer = str_replace('<dd>', '', $buffer);
225 $buffer = str_replace('</dd>', '<br />', $buffer);
226 $buffer = str_replace('<li>', '', $buffer);
227 $buffer = str_replace('</li>', '<br />', $buffer);
228 $buffer = str_replace('</dl>', '</p>', $buffer);
229 $buffer = str_replace('</ul>', '</p>', $buffer);
230 $buffer = str_replace('</ol>', '</p>', $buffer);
231 $buffer = str_replace('</p></p>', '</p>', $buffer);
232 $buffer = str_replace('<span>', '', $buffer);
233 $buffer = str_replace('</span>', '', $buffer);
234 $buffer = str_replace('<acronym>', '(', $buffer);
235 $buffer = str_replace('</acronym>', ')', $buffer);
236 $buffer = str_replace('<code>', '<em>', $buffer);
237 $buffer = str_replace('</code>', '</em>', $buffer);
238 $buffer = str_replace('<legend>', '<big>', $buffer);
239 $buffer = str_replace('</legend>', '</big><br />', $buffer);
240 $buffer = str_replace('cardid', 'id', $buffer);
241 $buffer = str_replace('cardtitle', 'title', $buffer);
242 $buffer = str_replace('cardname', 'name', $buffer);
243 $buffer = str_replace('cardtype', 'type', $buffer);
244 $buffer = str_replace('cardlabel', 'label', $buffer);
245 $buffer = str_replace('<p><big></big></p>', '', $buffer);
246 $buffer = str_replace('<p></p>', '', $buffer);
247 $buffer = str_replace('</strong><a', '</strong> <a', $buffer);
248 $buffer = str_replace('&lt;', '&#60;', $buffer);
249 $buffer = str_replace('Dev News', 'News', $buffer);
250 $buffer = str_replace('Site Updates', 'Updates', $buffer);
251 $buffer = str_replace('>[7] Prev', ' accesskey="7">[7] Prev', $buffer);
252 $buffer = str_replace('>Next [9]', ' accesskey="9">Next [9]', $buffer);
253
254 return fix($buffer, $fix);
255
256 }
257
258 //~ Make code mobile compatible, strip out some extras
259 function mobifix($buffer) {
260 global $fix;
261 $buffer = preg_replace('/<a href="\/comment(.*?)<\/a>/ims', '', $buffer);
262 $buffer = preg_replace('/<a href="?q=comment(.*?)<\/a>/ims', '', $buffer);
263 $buffer = str_replace('&lt;', '&#60;', $buffer);
264 $buffer = str_replace('&nbsp;', '&#160;', $buffer);
265 $buffer = preg_replace('/<div class="service-label">(.*?)<\/div>/ims', '', $buffer);
266 $buffer = preg_replace('/<div class="service-links">(.*?)<\/div>/ims', '', $buffer);
267 $buffer = preg_replace('/<a id="comment">(.*?)<\/a>/ims', '', $buffer);
268 $buffer = preg_replace('/<a href="\/taxonomy(.*?)class="feed-icon">(.*?)<\/a>/ims', '', $buffer);
269 $buffer = preg_replace('/<span title="comment">(.*?)<\/span>/ims', 'Visit the website to post comments', $buffer);
270 $buffer = str_replace('</a><', '</a> <', $buffer);
271 $buffer = str_replace('</strong><a', '</strong> <a', $buffer);
272 $buffer = str_replace('Dev News', 'News', $buffer);
273 $buffer = str_replace('Site Updates', 'Updates', $buffer);
274 $buffer = str_replace('>[7] Prev', ' accesskey="7">[7] Prev', $buffer);
275 $buffer = str_replace('>Next [9]', ' accesskey="9">Next [9]', $buffer);
276 return fix($buffer, $fix);
277 }
278
279 //~ Make code HTML compatible
280 function htmlfix($buffer) {
281 $buffer = preg_replace("!\s*/>!", ">", $buffer);
282 return mobifix($buffer);
283 }
284
285 //~ Make code XHTML compatible
286 function fix($buffer, $fix = FALSE) {
287
288 if ($fix == FALSE) {
289 return $buffer;
290 } else {
291
292 /**
293 * This segment is based on htmlcorrector module was made by Steven Wittens <unconed@drop.org>
294 */
295
296 $nonesting = array('li', 'p');
297 // Single use tags in HTML4
298 $singleuse = array('base', 'meta', 'link', 'hr', 'br', 'param', 'img', 'area', 'input', 'col', 'frame', 'go');
299 // Properly entify angles
300 $text = preg_replace('!<([^a-zA-Z/])!', '&lt;\1', $buffer);
301 // Splits tags from text
302 $split = preg_split('/<([^>]+?)>/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
303 // Note: PHP ensures the array consists of alternating delimiters and literals
304 // and begins and ends with a literal (inserting $null as required).
305 $tag = false; // Odd/even counter. Tag or no tag.
306 $stack = array();
307 $output = '';
308 foreach ($split as $value) {
309 // HTML tag
310 if ($tag) {
311 list($tagname) = explode(' ', strtolower($value), 2);
312 // Closing tag
313 if ($tagname{0} == '/') {
314 $tagname = substr($tagname, 1);
315 if (!in_array($tagname, $singleuse)) {
316 // See if we have other tags lingering first, and close them
317 while (($stack[0] != $tagname) && count($stack)) {
318 $output .= '</'. array_shift($stack) .'>';
319 }
320 // If the tag was not found, just leave it out;
321 if (count($stack)) {
322 $output .= '</'. array_shift($stack) .'>';
323 }
324 }
325 }
326 // Opening tag
327 else {
328 // See if we have an identical tag already open and close it if desired.
329 if (count($stack) && ($stack[0] == $tagname) && in_array($stack[0], $nonesting)) {
330 $output .= '</'. array_shift($stack) .'>';
331 }
332 // Push non-single-use tags onto the stack
333 if (!in_array($tagname, $singleuse)) {
334 array_unshift($stack, $tagname);
335 }
336 // Add trailing slash to single-use tags as per X(HT)ML.
337 else {
338 $value = rtrim($value, ' /') . ' /';
339 }
340 $output .= '<'. $value .'>';
341 }
342 }
343 else {
344 // Passthrough
345 $output .= $value;
346 }
347 $tag = !$tag;
348 }
349 // Close remaining tags
350 while (count($stack) > 0) {
351 $output .= '</'. array_shift($stack) .'>';
352 }
353 return str_replace (array('&lt;','<p></p>'), array('<',''), $output );
354 }
355 }

  ViewVC Help
Powered by ViewVC 1.1.2