forked from xoopscube/legacy
/
XupdateUtils.class.php
184 lines (172 loc) · 4.22 KB
/
XupdateUtils.class.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
<?php
/**
* @file
* @package xupdate
* @version $Id$
**/
if(!defined('XOOPS_ROOT_PATH'))
{
exit;
}
/**
* Xupdate_Utils
**/
class Xupdate_Utils
{
/**
* getModuleConfig
*
* @param string $name
* @param bool $optional
*
* @return XoopsObjectHandler
**/
public static function getModuleConfig(/*** string ***/ $dirname, /*** mixed ***/ $key)
{
$handler = self::getXoopsHandler('config');
$conf = $handler->getConfigsByDirname($dirname);
return (isset($conf[$key])) ? $conf[$key] : null;
}
/**
* &getXoopsHandler
*
* @param string $name
* @param bool $optional
*
* @return XoopsObjectHandler
**/
public static function &getXoopsHandler(/*** string ***/ $name,/*** bool ***/ $optional = false)
{
// TODO will be emulated xoops_gethandler
return xoops_gethandler($name,$optional);
}
/**
* &getModuleHandler
*
* @param string $name
* @param string $dirname
*
* @return XoopsObjectHandleer
**/
public static function &getModuleHandler(/*** string ***/ $name,/*** string ***/ $dirname)
{
// TODO will be emulated xoops_getmodulehandler
return xoops_getmodulehandler($name,$dirname);
}
/**
* &getXupdateHandler
*
* @param string $name
* @param string $dirname
*
* @return XoopsObjectHandleer
**/
public static function &getXupdateHandler(/*** string ***/ $name,/*** string ***/ $dirname)
{
$asset = null;
XCube_DelegateUtils::call(
'Module.xupdate.Global.Event.GetAssetManager',
new XCube_Ref($asset),
$dirname
);
if(is_object($asset) && is_a($asset, 'Xupdate_AssetManager'))
{
return $asset->getObject('handler',$name);
}
}
/**
* getEnv
*
* @param string $key
*
* @return string
**/
public static function getEnv(/*** string ***/ $key)
{
return getenv($key);
}
/**
* Text sanitizer for toShow
* @param string $str
* @return string
*/
public static function toShow($str) {
return htmlspecialchars(htmlspecialchars_decode($str));
}
/**
* Get redirect URL
* @param string $url
* @param int $limit
* @return string
*/
public static function getRedirectUrl($url, $redirect = 10) {
if ($headers = @ get_headers($url, 1)) {
$location = isset($headers['Location'])? $headers['Location'] : (isset($headers['location'])? $headers['location'] : '');
if ($location) {
if (is_array($location)) {
$url = array_pop($location);
} else {
$url = $location;
}
}
} else {
$url = self::curlGetRedirectUrl($url);
}
return $url;
}
/**
* Get redirect URL with cURL
*
* @param $url
* @param $ch
* @param $max_redirect
* @param $redirects
* @return string
*/
public static function curlGetRedirectUrl($url, $ch = null, $max_redirect = 10, $redirects = 0) {
if ($max_redirect < $redirects) {
return $url;
}
if (! $ch) {
$ch = curl_init($url);
}
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
list($header) = explode("\r\n\r\n", $data, 2);
if (preg_match('/(?:Location:|URI:)(.*?)\n/i', $header, $matches)) {
$url = trim($matches[1]);
curl_setopt($ch, CURLOPT_URL, $url);
$redirects++;
return self::curlGetRedirectUrl($url, $ch, $max_redirect, $redirects);
}
}
curl_close($ch);
return $url;
}
/**
* Check, Is directory writable
*
* @param string $dir
* @return boolean
*/
public static function checkDirWritable($dir) {
$ret = false;
$dir = rtrim($dir, '/\\');
if (!empty($dir) && is_dir($dir)) {
$test = $dir . DIRECTORY_SEPARATOR . 'writable.check';
if (@ touch($test)) {
$ret = true;
unlink($test);
} else {
$ret = false;
}
}
return $ret;
}
}
?>