This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MantisTouchRedirect.php
167 lines (137 loc) · 5.26 KB
/
MantisTouchRedirect.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
<?php
# Copyright (c) 2014 Victor Boctor @ MantisHub.com
# Licensed under the MIT license
class MantisTouchRedirectPlugin extends MantisPlugin {
function register() {
$this->name = plugin_lang_get( 'title' );
$this->description = plugin_lang_get( 'description' );
$this->version = '1.3.0';
$this->requires = array( 'MantisCore' => '1.3.0' );
$this->author = 'Victor Boctor';
$this->contact = 'https://github.com/mantishub/MantisTouchRedirect/';
$this->url = 'http://www.mantishub.com';
$this->page = 'config';
}
function config() {
return array(
'mantistouch_url' => '',
);
}
function hooks() {
return array(
'EVENT_CORE_READY' => 'redirect',
);
}
function redirect() {
# Direct only if user is using a mobile browser.
if ( !$this->is_mobile_browser() ) {
return;
}
# Don't do fancy checks if plugin is loaded as part of API call, otherwise,
# some of the functionality may fail.
if ( strstr( $_SERVER['SCRIPT_NAME'], '/api/' ) !== false ) {
return;
}
if ( !isset( $_SERVER['SCRIPT_NAME'] ) ) {
return;
}
if ( strstr( $_SERVER['SCRIPT_NAME'], 'view.php' ) !== false ||
strstr( $_SERVER['SCRIPT_NAME'], 'bug_report_page.php' ) !== false ||
strstr( $_SERVER['SCRIPT_NAME'], 'bug_report_advanced_page.php' ) !== false ||
strstr( $_SERVER['SCRIPT_NAME'], 'bug_view_page.php' ) !== false ||
strstr( $_SERVER['SCRIPT_NAME'], 'bug_view_advanced_page.php' ) !== false ||
strstr( $_SERVER['SCRIPT_NAME'], 'login_page.php' ) !== false ||
strstr( $_SERVER['SCRIPT_NAME'], 'view_all_bug_page.php' ) !== false ||
strstr( $_SERVER['SCRIPT_NAME'], 'my_view_page.php' ) !== false ) {
$t_mantistouch_url = plugin_config_get( 'mantistouch_url', '' );
if ( empty( $t_mantistouch_url ) ) {
$t_root = dirname( dirname( dirname( __FILE__ ) ) ) . '/';
if ( file_exists( $t_root . 'm' ) ) {
$t_mantistouch_url = $GLOBALS['g_path'] . 'm/';
} else {
$t_mantistouch_url = 'http://mantisbt.mobi/?url=%s';
}
}
$t_url = sprintf( $t_mantistouch_url, $GLOBALS['g_path'] );
$t_issue_id = '';
if ( strstr( $_SERVER['SCRIPT_NAME'], 'view.php' ) !== false ) {
$t_issue_id = (int)$_GET['id'];
}
else if ( strstr( $_SERVER['SCRIPT_NAME'], 'bug_view_page.php' ) !== false || strstr( $_SERVER['SCRIPT_NAME'], 'bug_view_advanced_page.php' ) !== false ) {
$t_issue_id = (int)$_GET['bug_id'];
}
if ( !empty( $t_issue_id ) ) {
if ( strstr( $t_url, 'url=' ) !== false ) {
$t_url .= '&issue_id=' . $t_issue_id;
} else {
$t_url .= '?issue_id=' . $t_issue_id;
}
}
$t_use_iis = isset( $GLOBALS['use_iis'] ) && $GLOBALS['use_iis'] == ON;
if ( !$t_use_iis ) {
header( 'Status: 302' );
}
header( 'Content-Type: text/html' );
if ( $t_use_iis ) {
header( "Refresh: 0;$t_url" );
} else {
header( "Location: $t_url" );
}
exit; # additional output can cause problems so let's just stop output here
}
}
/**
* Detects if it's mobile browser
* Source: http://www.dannyherran.com/2011/02/detect-mobile-browseruser-agent-with-php-ipad-iphone-blackberry-and-others/
* @return boolean<p>True if Mobile Browser, False on PC Browser</p>
*/
function is_mobile_browser() {
# When the plugin is loaded from a php-cli this is not defined. A sample scenario is a cronjob calling
# the sendmails script.
if ( !isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
return false;
}
$_SERVER['ALL_HTTP'] = isset( $_SERVER['ALL_HTTP'] ) ? $_SERVER['ALL_HTTP'] : '';
$t_mobile_browser = false;
$t_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
if ( preg_match( '/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipod|android|xoom)/i', $t_agent ) ) {
$t_mobile_browser = true;
}
if ( ( isset( $_SERVER['HTTP_ACCEPT'] ) ) && ( strpos( strtolower( $_SERVER['HTTP_ACCEPT'] ), 'application/vnd.wap.xhtml+xml' ) !== false ) ) {
$t_mobile_browser = true;
}
if ( isset( $_SERVER['HTTP_X_WAP_PROFILE'] ) ) {
$t_mobile_browser = true;
}
if ( isset( $_SERVER['HTTP_PROFILE'] ) ) {
$t_mobile_browser = true;
}
$t_mobile_ua = substr( $t_agent, 0, 4 );
$t_mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','xda','xda-'
);
if ( in_array( $t_mobile_ua, $t_mobile_agents ) ) {
$t_mobile_browser = true;
}
if ( strpos( strtolower( $_SERVER['ALL_HTTP'] ), 'operamini' ) !== false ) {
$t_mobile_browser = true;
}
// Pre-final check to reset everything if the user is on Windows
if ( strpos( $t_agent, 'windows' ) !== false ) {
$t_mobile_browser = false;
}
// But WP7 is also Windows, with a slightly different characteristic
if ( strpos( $t_agent, 'windows phone' ) !== false ) {
$t_mobile_browser = true;
}
return $t_mobile_browser;
}
}