-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobify-wordpress-plugin.php
316 lines (266 loc) · 10.4 KB
/
mobify-wordpress-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/*
Plugin Name: Mobify Wordpress Plugin
Plugin URI: http://github.com/b1tr0t/mobify-wordpress-plugin/tree/master
Description: Detect mobile device requests and redirect to mobify, setting cookies as applicable
Version: .9
Author: Peter McLachlan, derived from work by Robin Jewsbury and Mike Rowehl
Author URI: http://mobify.me
*/
/* Copyright 2009 (email : admin@mobify.me)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
plugin based on: Mobilize by Mippin Wordpress Plugin. Copyright for mippin components :
Copyright 2008 (email : info@Mippin.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
$MOBIFY_REDIRECT_DEFAULT='##########';
$MOBIFY_PARAM_NAME='mobify';
$MOBIFY_COOKIE_NAME='mobify';
$MOBIFY_COOKIE_EXPIRE=time() + 60 * 60 * 24 * 14; # default 2 week expiry
function is_mobile_device() {
$isMobile = false;
$op = strtolower($_SERVER['HTTP_X_OPERAMINI_PHONE']);
$no = strtolower($_SERVER['HTTP_X_MOBILE_GATEWAY']);
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$ac = strtolower($_SERVER['HTTP_ACCEPT']);
$ip = $_SERVER['REMOTE_ADDR'];
$isMobile = strpos($ac, 'application/vnd.wap.xhtml+xml') !== false
|| $op != ''
|| $no != ''
|| strpos($ua, 'alcatel') !== false
|| strpos($ua, 'audiovox') !== false
|| strpos($ua, 'au-mic') !== false
|| strpos($ua, 'avantgo') !== false
|| strpos($ua, 'bolt') !== false
|| strpos($ua, 'blackberry') !== false
|| strpos($ua, 'blazer') !== false
|| strpos($ua, 'cldc-') !== false
|| strpos($ua, 'danger') !== false
|| strpos($ua, 'dopod') !== false
|| strpos($ua, 'epoc') !== false
|| strpos($ua, 'ericsson') !== false
|| strpos($ua, 'Google Wireless Transcoder') !== false
|| strpos($ua, 'htc') !== false
|| strpos($ua, 'huawei') !== false
|| strpos($ua, 'iemobile') !== false
|| strpos($ua, 'ipaq') !== false
|| strpos($ua, 'iphone') !== false
|| strpos($ua, 'ipod') !== false
|| strpos($ua, 'j2me') !== false
|| strpos($ua, 'lg') !== false
|| strpos($ua, 'midp') !== false
|| strpos($ua, 'mobile') !== false
|| strpos($ua, 'mot') !== false
|| strpos($ua, 'moto') !== false
|| strpos($ua, 'motorola') !== false
|| strpos($ua, 'nec-') !== false
|| strpos($ua, 'netfront') !== false
|| strpos($ua, 'netfront') !== false
|| strpos($ua, 'nitro') !== false
|| strpos($ua, 'nokia') !== false
|| strpos($ua, 'novarra-vision') !== false
|| strpos($ua, 'opera mini') !== false
|| strpos($ua, 'palm') !== false
|| strpos($ua, 'palmsource') !== false
|| strpos($ua, 'panasonic') !== false
|| strpos($ua, 'philips') !== false
|| strpos($ua, 'pocketpc') !== false
|| strpos($ua, 'portalmmm') !== false
|| strpos($ua, 'rover') !== false
|| strpos($ua, 'sagem') !== false
|| strpos($ua, 'samsung') !== false
|| strpos($ua, 'sanyo') !== false
|| strpos($ua, 'sec') !== false
|| strpos($ua, 'series60') !== false
|| strpos($ua, 'sharp') !== false
|| strpos($ua, 'sie-') !== false
|| strpos($ua, 'smartphone') !== false
|| strpos($ua, 'sony') !== false
|| strpos($ua, 'symbian') !== false
|| strpos($ua, 't-mobile') !== false
|| strpos($ua, 'untrusted') !== false
|| strpos($ua, 'up.browser') !== false
|| strpos($ua, 'up.link') !== false
|| strpos($ua, 'vodafone/') !== false
|| strpos($ua, 'wap1.') !== false
|| strpos($ua, 'wap2.') !== false
|| strpos($ua, 'windows ce') !== false;
if (strpos($ua, 'Intel Mac OS X') !== false
|| strpos($ua, 'PPC Mac OS X') !== false
|| strpos($ua, 'Mac_powerPC') !== false
|| strpos($ua, 'SunOS') !== false
|| strpos($ua, 'Windows NT') !== false
|| strpos($ua, 'Windows 98') !== false
|| strpos($ua, 'WinNT') !== false )
$isMobile = false;
return $isMobile;
}
function mobify_url() {
# strip out mobify_param_name
global $MOBIFY_PARAM_NAME;
$url = 'http://' . get_option('mobify_redirect_base') . $_SERVER['REQUEST_URI'];
$url = str_replace($MOBIFY_PARAM_NAME . '=1', '', $url);
$url = str_replace($MOBIFY_PARAM_NAME . '=0', '', $url);
return $url;
}
$LOGFILENAME='/tmp/mobify_wp.log';
$DEBUG=0;
function log_to_file($msg)
{
global $DEBUG;
global $LOGFILENAME;
if ($DEBUG < 1) {
return;
}
// open file
$fd = fopen($LOGFILENAME, "a");
// write string
fwrite($fd, $msg . "\n");
// close file
fclose($fd);
}
function mobify_cookie_value() {
global $MOBIFY_COOKIE_NAME;
if (isset($_COOKIE[$MOBIFY_COOKIE_NAME])) {
if ($_COOKIE[$MOBIFY_COOKIE_NAME] == '1') {
return 1;
} else {
return 0;
}
} else {
return -1;
}
}
function mobify_param_value() {
global $MOBIFY_PARAM_NAME;
if (isset($_GET[$MOBIFY_PARAM_NAME])) {
if ($_GET[$MOBIFY_PARAM_NAME] == '1') {
return 1;
} else {
return 0;
}
} else {
return -1;
}
}
function mobify_redirect() {
global $MOBIFY_COOKIE_NAME;
global $MOBIFY_COOKIE_EXPIRE;
$groups = null;
preg_match("/(?P<domain>[\w+]+\.[\w]+)$/", get_option('mobify_redirect_base'), $groups);
$cookie_domain = "." . $groups[0];
// get parameter has highest priority
if (mobify_param_value() == 1) {
go_mobile();
} elseif (mobify_param_value() == 0) {
go_desktop();
return;
}
// mobify_param is not set, check out cookie setting
if (mobify_cookie_value() == 1 ) {
go_mobile();
} elseif (mobify_cookie_value() == 0) {
go_desktop();
return;
}
// nothing is set, do autodetection
if (is_mobile_device()) {
go_mobile();
}
go_desktop();
return;
}
function get_cookie_domain() {
$groups = null;
preg_match("/(?P<domain>[\w+]+\.[\w]+)$/", get_option('mobify_redirect_base'), $groups);
return "." . $groups[0];
}
function go_mobile() {
global $MOBIFY_COOKIE_NAME;
global $MOBIFY_COOKIE_EXPIRE;
$cookie_domain = get_cookie_domain();
// set cookie, then redirect
log_to_file('set mobify cookie for domain' . $cookie_domain . " cookie name: " . $MOBIFY_COOKIE_NAME . ' to value 1');
setcookie($MOBIFY_COOKIE_NAME, '1', $MOBIFY_COOKIE_EXPIRE, '/', $cookie_domain);
header('Location: ' . mobify_url());
exit();
}
function go_desktop() {
global $MOBIFY_COOKIE_NAME;
global $MOBIFY_COOKIE_EXPIRE;
$cookie_domain = get_cookie_domain();
log_to_file('set mobify cookie for domain ' . $cookie_domain . " cookie name: " . $MOBIFY_COOKIE_NAME . ' to value 0');
setcookie($MOBIFY_COOKIE_NAME, '0', $MOBIFY_COOKIE_EXPIRE, '/', $cookie_domain);
}
function mobify_admin() {
if (function_exists('add_submenu_page')) {
add_options_page('Mobify Setup', 'Mobify', 10, basename(__FILE__), 'mobify_admin_page');
}
}
function mobify_admin_page() {
if (isset($_POST['mobify_options_submit'])) {
update_option('mobify_redirect_base', $_POST['mobify_redirect_base']);
echo '<div id="message" class="updated fade"><p><strong>';
_e('Options saved.');
echo '</strong></p></div>';
}
?>
<div class="wrap">
<h2>Mobify Options Page</h2>
<form name="mobify_options_form" action="<?php echo $_SERVER['PHP_SELF'] . '?page=' . basename(__FILE__); ?>" method="post">
The Mobify wordpress plugin requires that you set the mobile domain name (CNAME) that you configured with Mobify. This is typically 'm.yoursite.com'.
<ul style="width:75%">
<li><strong>CNAME</strong>: <input type="text" name="mobify_redirect_base" value="<?php echo get_option('mobify_redirect_base');?>" /></li>
</ul>
<div class="submit" style="float:right">
<input type="submit" name="mobify_options_submit" value="<?php _e('Update Options »') ?>"/>
</div>
</form>
<?php
}
function mobify_widget_init() {
global $MOBIFY_REDIRECT_DEFAULT;
if ( !function_exists('register_sidebar_widget') ) {
return;
}
$mobify_redirect_base = get_option('mobify_redirect_base');
if ($mobify_redirect_base == false or empty($mobify_redirect_base) or $mobify_redirect_base == '' ) {
add_option('mobify_redirect_base', $value = $MOBIFY_REDIRECT_DEFAULT, $deprecated = '', $autoload = 'yes');
update_option('mobify_redirect_base', $MOBIFY_REDIRECT_DEFAULT);
}
function mobify_widget($args) {
extract($args);
echo $before_widget;
echo $before_title.'Mobile Version'.$after_title;
echo "<a href='" . mobify_url() . "'>Switch to mobile view:</a>";
echo "<a href='" . mobify_url() . "'><img style='border:none;' src='http://www.mippin.com/app/images/blogger_button.gif' /></a><br />";
echo $after_widget;
}
register_sidebar_widget('Mobify Widget', 'mobify_widget');
}
add_action('template_redirect', 'mobify_redirect');
add_action('admin_menu', 'mobify_admin');
add_action('plugins_loaded', 'mobify_widget_init');
?>