Util.php
5.19 KB
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
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: Util.php 31728 2012-09-25 09:03:42Z zhouxiaobo $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class Cloud_Service_Util {
protected static $_instance;
public static function getInstance() {
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __construct() {
}
public function generateSiteSignUrl($params = array(), $isCamelCase = false, $isReturnArray = false) {
global $_G;
$ts = TIMESTAMP;
$sId = $_G['setting']['my_siteid'];
$sKey = $_G['setting']['my_sitekey'];
$uid = $_G['uid'];
if(!is_array($params)) {
$params = array();
}
unset($params['sig'], $params['ts']);
if ($isCamelCase) {
$params['sId'] = $sId;
$params['sSiteUid'] = $uid;
} else {
$params['s_id'] = $sId;
$params['s_site_uid'] = $uid;
}
ksort($params);
$str = $this->httpBuildQuery($params, '', '&');
$sig = md5(sprintf('%s|%s|%s', $str, $sKey, $ts));
$params['ts'] = $ts;
$params['sig'] = $sig;
if(!$isReturnArray) {
$params = $this->httpBuildQuery($params, '', '&');
}
return $params;
}
public function redirect($url, $code = 302) {
@ob_end_clean();
@ob_start();
$errorChars = array();
for ($i = 0; $i <= 31; $i ++) {
$errorChars[] = chr($i);
}
$url = trim(str_replace($errorChars, '', $url));
if (strpos($url, '/') === 0) {
$url = '/' . ltrim($url, '/');
}
@header('Location: ' . $url, true, $code);
exit;
}
public function generateUniqueId() {
$siteuniqueid = C::t('common_setting')->fetch('siteuniqueid');
if(empty($siteuniqueid) || strlen($siteuniqueid) < 16) {
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$siteuniqueid = 'DX'.$chars[date('y')%60].$chars[date('n')].$chars[date('j')].$chars[date('G')].$chars[date('i')].$chars[date('s')].substr(md5($_G['clientip'].$_G['username'].TIMESTAMP), 0, 4).random(4);
C::t('common_setting')->update('siteuniqueid', $siteuniqueid);
require_once libfile('function/cache');
updatecache('setting');
}
}
public function httpBuildQuery($data, $numeric_prefix='', $arg_separator='', $prefix='') {
$render = array();
if (empty($arg_separator)) {
$arg_separator = @ini_get('arg_separator.output');
empty($arg_separator) && $arg_separator = '&';
}
foreach ((array) $data as $key => $val) {
if (is_array($val) || is_object($val)) {
$_key = empty($prefix) ? "{$key}[%s]" : sprintf($prefix, $key) . "[%s]";
$_render = $this->httpBuildQuery($val, '', $arg_separator, $_key);
if (!empty($_render)) {
$render[] = $_render;
}
} else {
if (is_numeric($key) && empty($prefix)) {
$render[] = urlencode("{$numeric_prefix}{$key}") . "=" . urlencode($val);
} else {
if (!empty($prefix)) {
$_key = sprintf($prefix, $key);
$render[] = urlencode($_key) . "=" . urlencode($val);
} else {
$render[] = urlencode($key) . "=" . urlencode($val);
}
}
}
}
$render = implode($arg_separator, $render);
if (empty($render)) {
$render = '';
}
return $render;
}
public function getApiVersion() {
return '0.6';
}
public function hashHmac($algo, $data, $key, $raw_output = false) {
if (function_exists('hash_hmac')) {
return hash_hmac($algo, $data, $key, $raw_output);
} else {
$algo = strtolower($algo);
$pack = 'H'.strlen(call_user_func($algo, 'test'));
$size = 64;
$opad = str_repeat(chr(0x5C), $size);
$ipad = str_repeat(chr(0x36), $size);
if(strlen($key) > $size) {
$key = str_pad(pack($pack, call_user_func($algo, $key)), $size, chr(0x00));
} else {
$key = str_pad($key, $size, chr(0x00));
}
for ($i = 0; $i < strlen($key) - 1; $i++) {
$opad[$i] = $opad[$i] ^ $key[$i];
$ipad[$i] = $ipad[$i] ^ $key[$i];
}
$output = call_user_func($algo, $opad.pack($pack, call_user_func($algo, $ipad.$data)));
return ($raw_output) ? pack($pack, $output) : $output;
}
}
public function isMobile($status) {
if (getstatus($status, 11) || getstatus($status, 12) || getstatus($status, 13)) {
return true;
}
return false;
}
public function mobileHasSound() {
if (getstatus($status, 13)) {
return true;
}
return false;
}
public function mobileHasPhoto() {
if (getstatus($status, 12) && getstatus($status, 11)) {
return true;
}
return false;
}
public function mobileHasGPS() {
if (getstatus($status, 12)) {
return true;
}
return false;
}
public function isfounder($user) {
global $_G;
$founders = str_replace(' ', '', $_G['config']['admincp']['founder']);
if(!$user['uid'] || $user['groupid'] != 1 || $user['adminid'] != 1) {
return false;
} elseif(empty($founders)) {
return false;
} elseif(strexists(",$founders,", ",$user[uid],")) {
return true;
} elseif(!is_numeric($user['username']) && strexists(",$founders,", ",$user[username],")) {
return true;
} else {
return FALSE;
}
}
}