forked from bmsterling/WURFL-Capabilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWURFLCap.php
222 lines (177 loc) · 7.85 KB
/
WURFLCap.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
<?php
define('WURFL_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WURFL_BASE_DIR', WP_CONTENT_DIR . '/wurfl');
define('WURFL_RESOURCES_DIR', WURFL_BASE_DIR . '/resources');
define('WURFL_PERSISTENCE_DIR', WURFL_RESOURCES_DIR . '/storage/persistence');
define('WURFL_CACHE_DIR', WURFL_RESOURCES_DIR . '/storage/cache');
define('WURFL_DIR', WURFL_PLUGIN_DIR . '/WURFL');
include_once('WURFL/FileUtils.php');
class WURFLCap {
var $base;
var $requestingDevice;
function __construct ($base) {
$this->base = $base;
// Register hooks that are fired when the plugin is activated, deactivated, and uninstalled, respectively.
register_activation_hook( $this->base, array( $this, 'activate' ) );
register_deactivation_hook( $this->base, array( $this, 'deactivate' ) );
register_uninstall_hook( $this->base, array( $this, 'uninstall' ) );
add_action( 'setup_theme', array( $this, 'action_setup_theme') );
} // end constructor
/**
* Fired when the plugin is activated.
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog
*/
public function activate( $network_wide ) {
$new_file_path = WURFL_BASE_DIR;
if (!file_exists(WURFL_PLUGIN_DIR.'/wurfl.xml')){
wp_die("wurfl.xml file not found, please read the readme file for installation instructions.");
}
if (!WURFL_FileUtils::safe_mkdir($new_file_path, 0755, true)){
wp_die("Permission denied, make sure you have write permission to wp-content folder.");
// return array(
// 'error' => 'Permission denied, make sure you have write permission to wp-content folder.'
// );
}
$persistence = WURFL_PERSISTENCE_DIR;
$cache = WURFL_CACHE_DIR;
if (!WURFL_FileUtils::safe_mkdir($persistence, 0755, true)) {
wp_die('Permission denied, make sure you have write permission to '.$new_file_path.' folder.');
// return array(
// 'error' => 'Permission denied, make sure you have write permission to '.$new_file_path.' folder.'
// );
}
if (!WURFL_FileUtils::safe_mkdir($cache, 0755, true)) {
wp_die('Permission denied, make sure you have write permission to '.$new_file_path.' folder.');
// return array(
// 'error' => 'Permission denied, make sure you have write permission to '.$new_file_path.' folder.'
// );
}
} // end activate
/**
* Fired when the plugin is deactivated.
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog
*/
public function deactivate( $network_wide ) {
} // end deactivate
/**
* Fired when the plugin is uninstalled.
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog
*/
public function uninstall( $network_wide ) {
$new_file_path = WURFL_BASE_DIR;
$this->recursive_remove_directory($new_file_path);
} // end uninstall
// code borrowed from http://lixlpixel.org/recursive_function/php/recursive_directory_delete/
public function recursive_remove_directory($directory, $empty=FALSE) {
// if the path has a slash at the end we remove it here
if (substr($directory,-1) == '/') {
$directory = substr($directory,0,-1);
}
// if the path is not valid or is not a directory ...
if (!file_exists($directory) || !is_dir($directory)) {
// ... we return false and exit the function
return FALSE;
// ... if the path is not readable
} elseif(!is_readable($directory)) {
// ... we return false and exit the function
return FALSE;
// ... else if the path is readable
}
else {
// we open the directory
$handle = opendir($directory);
// and scan through the items inside
while (FALSE !== ($item = readdir($handle))) {
// if the filepointer is not the current directory
// or the parent directory
if ($item != '.' && $item != '..') {
// we build the new path to delete
$path = $directory.'/'.$item;
// if the new path is a directory
if(is_dir($path)) {
// we call this function with the new path
$this->recursive_remove_directory($path);
// if the new path is a file
}
else{
// we remove the file
unlink($path);
}
}
}
// close the directory
closedir($handle);
// if the option to empty is not set to true
if ($empty == FALSE) {
// try to delete the now empty directory
if (!rmdir($directory)) {
// return false if not possible
return FALSE;
}
}
// return success
return TRUE;
}
}
public function action_setup_theme () {
require_once WURFL_DIR .'/Application.php';
$persistenceDir = WURFL_PERSISTENCE_DIR;
$cacheDir = WURFL_CACHE_DIR;
// Create WURFL Configuration
$wurflConfig = new WURFL_Configuration_InMemoryConfig();
// Set location of the WURFL File
$wurflConfig->wurflFile(WURFL_PLUGIN_DIR.'/wurfl.xml');
// Set the match mode for the API ('performance' or 'accuracy')
$wurflConfig->matchMode('performance');
// Automatically reload the WURFL data if it changes
$wurflConfig->allowReload(true);
// Setup WURFL Persistence
$wurflConfig->persistence('file', array('dir' => $persistenceDir));
// Setup Caching
$wurflConfig->cache('file', array('dir' => $cacheDir, 'expiration' => 36000));
// Create a WURFL Manager Factory from the WURFL Configuration
$wurflManagerFactory = new WURFL_WURFLManagerFactory($wurflConfig);
// Create a WURFL Manager
/* @var $wurflManager WURFL_WURFLManager */
$wurflManager = $wurflManagerFactory->create();
$this->requestingDevice = $wurflManager->getDeviceForHttpRequest($_SERVER);
}
public function getCapability ($cap = false) {
return $this->requestingDevice->getCapability($cap);
}
public function is_wireless_device () {
return $this->getCapability('is_wireless_device');
}
public function is_tablet () {
return $this->getCapability('is_tablet');
}
public function is_touch () {
return $this->pointing_method() == "touchscreen" ? "true" : "false";
}
public function supports_borderradius () {
return $this->getCapability('css_rounded_corners') == "css3";
}
public function supports_gradient () {
return $this->getCapability('css_gradient') == "css3";
}
public function pointing_method () {
return $this->getCapability('pointing_method');
}
public function is_smartphone () {
$is_wireless = $this->is_wireless_device() == 'true';
$is_tablet = $this->is_tablet() == 'true';
$has_web_support = $this->getCapability('device_claims_web_support') == 'true';
$is_phone = $this->getCapability('can_assign_phone_number') == 'true';
if($is_tablet) {
$ret = false;
} else if ( $is_wireless && $has_web_support && $is_phone ) {
$ret = true;
} else {
$ret = false;
}
return ($ret == 'true');
}
}