root/wordpress-mobile-edition/trunk/wp-mobile.php

Revision 52882, 6.5 kB (checked in by alexkingorg, 4 days ago)

catch more browsers

Line 
1 <?php
2
3 // WordPress Mobile Edition
4 //
5 // Copyright (c) 2002-2008 Alex King
6 // http://alexking.org/projects/wordpress
7 //
8 // Released under the GPL license
9 // http://www.opensource.org/licenses/gpl-license.php
10 //
11 // **********************************************************************
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 // *****************************************************************
16
17 /*
18 Plugin Name: WordPress Mobile Edition
19 Plugin URI: http://alexking.org/projects/wordpress
20 Description: Show a mobile view of the post/page if the visitor is on a known mobile device. Questions on configuration, etc.? Make sure to read the README.
21 Author: Alex King
22 Author URI: http://alexking.org
23 Version: 2.1a
24 */
25
26 if (!function_exists('is_admin_page')) {
27     function is_admin_page() {
28         if (function_exists('is_admin')) {
29             return is_admin();
30         }
31         if (function_exists('check_admin_referer')) {
32             return true;
33         }
34         else {
35             return false;
36         }
37     }
38 }
39
40 $_SERVER['REQUEST_URI'] = ( isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME'] . (( isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '')));
41
42 function akm_check_mobile() {
43     if (!isset($_SERVER["HTTP_USER_AGENT"]) || (isset($_COOKIE['akm_mobile']) && $_COOKIE['akm_mobile'] == 'false')) {
44         return false;
45     }
46     if (akm_mobile_exclude()) {
47         return false;
48     }
49     if (isset($_COOKIE['akm_mobile']) && $_COOKIE['akm_mobile'] == 'true') {
50         return true;
51     }
52     $whitelist = array(
53         'Stand Alone/QNws'
54     );
55     foreach ($whitelist as $browser) {
56         if (strstr($_SERVER["HTTP_USER_AGENT"], $browser)) {
57             return false;
58         }
59     }
60     $small_browsers = array(
61         '2.0 MMP'
62         ,'240x320'
63         ,'AvantGo'
64         ,'BlackBerry'
65         ,'Blazer'
66         ,'Cellphone'
67         ,'Danger'
68         ,'DoCoMo'
69         ,'Elaine/3.0'
70         ,'EudoraWeb'
71         ,'hiptop'
72         ,'IEMobile'
73         ,'KYOCERA/WX310K'
74         ,'LG/U990'
75         ,'MIDP-2.'
76         ,'MMEF20'
77         ,'MOT-V'
78         ,'NetFront'
79         ,'Newt'
80         ,'Nintendo Wii'
81         ,'Nitro' // Nintendo DS
82         ,'Nokia'
83         ,'Opera Mini'
84         ,'Palm'
85         ,'PlayStation Portable'
86         ,'portalmmm'
87         ,'Proxinet'
88         ,'ProxiNet'
89         ,'SHARP-TQ-GX10'
90         ,'Small'
91         ,'SonyEricsson'
92         ,'Symbian OS'
93         ,'SymbianOS'
94         ,'TS21i-10'
95         ,'UP.Browser'
96         ,'UP.Link'
97         ,'Windows CE'
98         ,'WinWAP'
99     );
100
101     foreach ($small_browsers as $browser) {
102         if (strstr($_SERVER["HTTP_USER_AGENT"], $browser)) {
103             return true;
104         }
105     }
106     return false;
107 }
108
109 function akm_mobile_exclude() {
110     $exclude = false;
111     $pages_to_exclude = array(
112         'wp-admin'
113         ,'wp-comments-post.php'
114         ,'wp-mail.php'
115         ,'wp-login.php'
116     );
117     foreach ($pages_to_exclude as $exclude_page) {
118         if (strstr(strtolower($_SERVER['REQUEST_URI']), $exclude_page)) {
119             $exclude = true;
120         }
121     }
122     return $exclude;
123 }
124
125 function akm_template($theme) {
126     if (akm_installed()) {
127         return apply_filters('akm_template', 'wp-mobile');
128     }
129     else {
130         return $theme;
131     }
132 }
133
134 function akm_installed() {
135     return is_dir(ABSPATH.'/wp-content/themes/wp-mobile');
136 }
137
138 if (is_admin_page() && !akm_installed()) {
139     global $wp_version;
140     if (isset($wp_version) && version_compare($wp_version, '2.5', '>=')) {
141         add_action('admin_notices', create_function( '', "echo '<div class=\"error\">WP Mobile is incorrectly installed. Please check the <a href=\"http://alexking.org/projects/wordpress/readme?project=wordpress-mobile-edition\">README</a>.</div>';" ) );
142     }
143 }
144
145 function akm_mobile_available($content) {
146     return $content.'<p><a href="'.get_bloginfo('wpurl').'/index.php?ak_action=accept_mobile">Return to the Mobile Edition</a>.</p>';
147 }
148
149 function akm_mobile_link() {
150     echo '<a href="'.get_bloginfo('wpurl').'/index.php?ak_action=force_mobile">Mobile Edition</a>';
151 }
152
153 if (!function_exists('ak_recent_posts')) {
154 // this is based almost entirely on:
155 /*
156 Plugin Name: Recent Posts
157 Plugin URI: http://mtdewvirus.com/code/wordpress-plugins/
158 Description: Returns a list of the most recent posts.
159 Version: 1.07
160 Author: Nick Momrik
161 Author URI: http://mtdewvirus.com/
162 */
163     function ak_recent_posts($count = 5, $before = '<li>', $after = '</li>', $hide_pass_post = true, $skip_posts = 0, $show_excerpts = false, $where = '', $join = '', $groupby = '') {
164         global $wpdb;
165         $time_difference = get_settings('gmt_offset');
166         $now = gmdate("Y-m-d H:i:s",time());
167     
168         $join = apply_filters('posts_join', $join);
169         $where = apply_filters('posts_where', $where);
170         $groupby = apply_filters('posts_groupby', $groupby);
171         if (!empty($groupby)) { $groupby = ' GROUP BY '.$groupby; }
172     
173         $request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts $join WHERE post_status = 'publish' AND post_type != 'page' ";
174         if ($hide_pass_post) $request .= "AND post_password ='' ";
175         $request .= "AND post_date_gmt < '$now' $where $groupby ORDER BY post_date DESC LIMIT $skip_posts, $count";
176         $posts = $wpdb->get_results($request);
177         $output = '';
178         if ($posts) {
179             foreach ($posts as $post) {
180                 $post_title = stripslashes($post->post_title);
181                 $permalink = get_permalink($post->ID);
182                 $output .= $before . '<a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . htmlspecialchars($post_title, ENT_COMPAT) . '">' . htmlspecialchars($post_title) . '</a>';
183                 if($show_excerpts) {
184                     $post_excerpt = stripslashes($post->post_excerpt);
185                     $output.= '<br />' . $post_excerpt;
186                 }
187                 $output .= $after;
188             }
189         } else {
190             $output .= $before . "None found" . $after;
191         }
192         echo $output;
193     }
194 }
195
196 function akm_request_handler() {
197     if (isset($_GET['ak_action'])) {
198         $url = parse_url(get_bloginfo('home'));
199         $domain = $url['host'];
200         if (!empty($url['path'])) {
201             $path = $url['path'];
202         }
203         else {
204             $path = '/';
205         }
206         $redirect = false;
207         switch ($_GET['ak_action']) {
208             case 'reject_mobile':
209                 setcookie(
210                     'akm_mobile'
211                     , 'false'
212                     , time() + 300000
213                     , $path
214                     , $domain
215                 );
216                 $redirect = true;
217                 break;
218             case 'force_mobile':
219             case 'accept_mobile':
220                 setcookie(
221                     'akm_mobile'
222                     , 'true'
223                     , time() + 300000
224                     , $path
225                     , $domain
226                 );
227                 $redirect = true;
228                 break;
229         }
230         if ($redirect) {
231             if (!empty($_SERVER['HTTP_REFERER'])) {
232                 $go = $_SERVER['HTTP_REFERER'];
233             }
234             else {
235                 $go = get_bloginfo('home');
236             }
237             header('Location: '.$go);
238             die();
239         }
240     }
241 }
242 add_action('init', 'akm_request_handler');
243
244 if (akm_check_mobile()) {
245     add_action('template', 'akm_template');
246     add_action('option_template', 'akm_template');
247     add_action('option_stylesheet', 'akm_template');
248 }
249
250 if (isset($_COOKIE['akm_mobile']) && $_COOKIE['akm_mobile'] == 'false') {
251     add_action('the_content', 'akm_mobile_available');
252 }
253
254 ?>
Note: See TracBrowser for help on using the browser.