-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelper.php
261 lines (227 loc) · 7.37 KB
/
helper.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
<?php
define('IFLYCHAT_EXTERNAL_HOST', 'http://api.iflychat.com');
define('IFLYCHAT_EXTERNAL_PORT', '80');
define('IFLYCHAT_EXTERNAL_A_HOST', 'https://api.iflychat.com');
define('IFLYCHAT_EXTERNAL_A_PORT', '443');
define('IFLYCHAT_EXTERNAL_CDN_HOST', 'cdn.iflychat.com');
/**
* @package iFlyChat
* @version 1.0.0
* @copyright Copyright (C) 2014 iFlyChat. All rights reserved.
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
* @author iFlyChat Team
* @link https://iflychat.com
*/
class IflychatHelper {
public function iflychat_check_chat_admin() {
if (OW_User::getInstance()->isAdmin()) {
return TRUE;
}
else {
return FALSE;
}
}
public function roleArray() {
$arr = BOL_AuthorizationRoleDao::getInstance()->findAll();
$roleArr = array();
for ($i = 0; $i < sizeof($arr); $i++) {
$roleArr += array($arr[$i]->id => $arr[$i]->name);
}
return $roleArr;
}
public function iflychat_extended_http_request($url, $data_json) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$res_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result = json_decode($result);
if (empty($result)) {
$result = (object) array('code' => $res_code);
}
else {
$result->code = $res_code;
}
curl_close($ch);
return $result;
}
public function generateToken($api_key) {
$role = '';
$chat_role = 'participant';
$uid = OW::getUser()->getId();
$uname = ($uid) ? BOL_UserService::getInstance()
->findUserById($uid)->username : '';
if ($this->iflychat_check_chat_admin() || $this->iflychat_check_chat($uname, $this->params('iflychat_administers'))) {
$role = 'admin';
$chat_role = 'admin';
}
elseif ($this->iflychat_check_chat($uname, $this->params('iflychat_moderators'))) {
$chat_role = 'moderator';
}
if ($uid) {
$data = array(
'user_name' => $uname,
'user_id' => strval($uid),
'app_id' => $this->params('iflychat_app_id'),
'api_key' => $api_key,
'user_roles' => $role,
'version' => 'Oxwall-2.0.0',
'user_list_filter' => 'all',
'user_status' => TRUE,
'chat_role' => $chat_role
);
}
//Add aRole parameter in data array
if ($role == 'admin') {
$data['user_site_roles'] = $this->roleArray();
}
//Get friend's id
if ($this->params('iflychat_enable_friends') == 2 && is_array(FRIENDS_BOL_Service::getInstance()
->findAllActiveFriendships())
) {
$data['user_list_filter'] = 'friend';
$final_list = array();
$final_list['1']['name'] = 'friend';
$final_list['1']['plural'] = 'friends';
$final_list['1']['valid_uids'] = FRIENDS_BOL_Service::getInstance()
->findFriendIdList($uid, 0, 1000);;
$data['user_relationships'] = $final_list;
}
else {
$data['user_list_filter'] = 'all';
}
$data['user_avatar_url'] = $this->iflychat_get_user_pic_url($uid);
$data['user_profile_url'] = $this->iflychat_get_user_profile_url($uid);
//print_r($data);
$uri = IFLYCHAT_EXTERNAL_A_HOST . ':' . IFLYCHAT_EXTERNAL_A_PORT . '/api/1.1/token/generate';
try {
$response = $this->iflychat_extended_http_request($uri, json_encode($data));
if (isset($response->code) && $response->code != 200) {
$object = (Array) ['code' => $response->code];
return json_encode($object);
}
// if (isset($response->app_id)) {
// $config = OW::getConfig();
// $configArr = $config->getValues('iflychat');
// $data = json_decode($configArr['setting_vars'], true);
// $data2 = array(
// 'iflychat_app_id' => $response->app_id
// );
// $config->saveConfig('iflychat', 'setting_vars', json_encode(array_merge($data, $data2)));
// }
return json_encode($response);
} catch (Exception $e) {
$var = array(
'key' => NULL,
'expires_in' => NULL
);
return json_encode($var);
}
}
public function iflychat_get_user_pic_url($uid) {
$url = BOL_AvatarService::getInstance()->getAvatarUrl($uid);
$pos = strpos($url, ':');
if ($pos !== FALSE) {
$url = substr($url, $pos + 1);
}
return $url;
}
public function iflychat_check_chat($uname, $allNames) {
if (!empty($allNames) && ($uname)) {
$a_names = explode(",", $allNames);
foreach ($a_names as $an) {
$aa = trim($an);
if ($aa == $uname) {
return TRUE;
break;
}
}
}
return FALSE;
}
public function iflychat_get_user_profile_url($uid) {
$upl = BOL_UserService::getInstance()->getUserUrl($uid);
$pos = strpos($upl, ':');
if ($pos !== FALSE) {
$upl = substr($upl, $pos + 1);
}
return $upl;
}
private function defaultValue($field) {
$dVal = array(
'iflychat_external_api_key' => '',
'iflychat_app_id' => '',
'iflychat_show_popup_chat' => '1',
'iflychat_enable_friends' => '1',
'iflychat_moderators' => '',
'iflychat_administers' => ''
);
return $dVal["$field"];
}
public function params($field) {
$config = OW::getConfig();
$configArr = $config->getValues('iflychat');
if (!isset($configArr["$field"]) && empty($params["$field"])) {
return $this->defaultValue($field);
}
else {
return $configArr["$field"];
}
}
public function iflychat_path_check() {
$page_match = FALSE;
//Check default path
if ($this->default_path()) {
return FALSE;
}
if (trim($this->params('iflychat_path_pages')) != '') {
if (function_exists('mb_strtolower')) {
$pages = mb_strtolower($this->params('iflychat_path_pages'));
$path = mb_strtolower(OW::getRouter()->getUri());
}
else {
$pages = strtolower($this->params('iflychat_path_pages'));
$path = strtolower(OW::getRouter()->getUri());
}
$page_match = $this->iflychat_match_path($path, $pages);
$page_match = ($this->params('iflychat_path_visibility') == '1') ? (!$page_match) : $page_match;
}
else {
if ($this->params('iflychat_path_visibility') == '1') {
$page_match = TRUE;
}
}
return $page_match;
}
public function iflychat_match_path($path, $patterns) {
$to_replace = array(
'/(\r\n?|\n)/',
'/\\\\\*/',
);
$replacements = array(
'|',
'.*',
);
$patterns_quoted = preg_quote($patterns, '/');
$regexps[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
return (bool) preg_match($regexps[$patterns], $path);
}
// Chat not load on default path
public function default_path() {
$defaultPath = 'base/media-panel';
$path = mb_strtolower(OW::getRouter()->getUri());
$var = explode("/", $path);
if (!empty($var[1])) {
$path = $var[0] . "/" . $var[1];
$page_match = $this->iflychat_match_path($path, $defaultPath);
return $page_match;
}
else {
return NULL;
}
}
}