Repository navigation
Expand file tree
/
Copy pathHttpBaseController.php
More file actions
262 lines (234 loc) · 8.76 KB
/
Copy pathHttpBaseController.php
File metadata and controls
262 lines (234 loc) · 8.76 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
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
262
<?php
/**
* Created by PhpStorm.
* User: childeYin<尹少爷>
* Date: 13/07/2018
* Time: 6:32 PM
*/
use Google\Protobuf\Any;
use Zaly\Proto\Core\TransportData;
use Zaly\Proto\Core\TransportDataHeaderKey;
use Google\Protobuf\Internal\Message;
abstract class HttpBaseController extends \Wpf_Controller
{
protected $logger;
protected $userId;
protected $userInfo;
protected $sessionId;
public $defaultErrorCode = "success";
public $errorCode = "fail";
protected $sessionIdTimeOut = 3600000; //1个小时的毫秒
public $defaultFilePath = "files";
public $whiteAction = [
"page.login",
"page.js",
"page.siteConfig",
"page.passport.login",
"page.jump"
];
private $groupType = "g";
private $u2Type = "u";
private $jumpRoomType = "";
private $jumpRoomId = "";
private $jumpRelation = "";
public $siteCookieName = "zaly_site_user";
public $language = "";
protected $ctx;
public function __construct(BaseCtx $context)
{
if (!$this->checkDBIsExist()) {
$initUrl = ZalyConfig::getConfig("apiPageSiteInit");
header("Location:" . $initUrl);
exit();
}
$this->logger = $context->getLogger();
$this->ctx = $context;
}
/**
* 处理方法, 根据bodyFormatType, 获取transData
* @return string|void
*/
public function doIndex()
{
$tag = __CLASS__ . "-" . __FUNCTION__;
try {
parent::doIndex();
$preSessionId = isset($_GET['preSessionId']) ? $_GET['preSessionId'] : "";
$action = isset($_GET['action']) ? $_GET['action'] : "";
$this->getAndSetClientLang();
if ($preSessionId) {
$this->handlePreSessionId();
}
if (!in_array($action, $this->whiteAction)) {
$flag = $this->ctx->Site_Config->getConfigValue(SiteConfig::SITE_OPEN_WEB_EDITION);
if ($flag != 1) {
echo ZalyText::getText("text.open.web", $this->language);
die();
}
$this->getUserIdByCookie();
}
$this->getJumpUrl();
$this->index();
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error msg =" . $ex->getMessage());
$this->setLogout();
}
}
protected function getAndSetClientLang()
{
$headLang = isset($_GET['lang']) ? $_GET['lang'] : "";
if (isset($headLang) && $headLang == Zaly\Proto\Core\UserClientLangType::UserClientLangZH) {
$this->language = Zaly\Proto\Core\UserClientLangType::UserClientLangZH;
} else {
$this->language = Zaly\Proto\Core\UserClientLangType::UserClientLangEN;
}
}
public function handlePreSessionId()
{
try {
$preSessionId = isset($_GET['preSessionId']) ? $_GET['preSessionId'] : "";
if ($preSessionId) {
$preSessionId = isset($_GET['preSessionId']) ? $_GET['preSessionId'] : "";
if ($preSessionId) {
$clientType = Zaly\Proto\Core\UserClientType::UserClientWeb;
$userProfile = $this->ctx->Site_Login->checkPreSessionIdFromPlatform($preSessionId, "", $clientType);
$this->setCookie($userProfile["sessionId"], $this->siteCookieName);
}
}
$x = isset($_GET['x']) ? $_GET['x'] : "";
$apiPageIndex = ZalyConfig::getApiIndexUrl();
if ($x) {
if (strpos($apiPageIndex, "?")) {
$apiPageIndex . "&x=" . $x;
} else {
$apiPageIndex . "?x=" . $x;
}
}
$successCallBack = $_GET['success_callback'] ? $_GET['success_callback'] : "";
echo "$successCallBack('" . $apiPageIndex . "');";
die();
} catch (Exception $ex) {
$errorInfo = $ex->getMessage();
$failCallBack = $_GET['fail_callback'] ? $_GET['fail_callback'] : "";
echo "$failCallBack('" . $errorInfo . "');";
die();
}
}
public function getJumpUrl()
{
$tag = __CLASS__ . "-" . __FUNCTION__;
try {
$x = isset($_GET['x']) ? $_GET['x'] : "";
if (!$x) {
return;
}
list($type, $id) = explode("-", $x);
if ($id == $this->userId) {
return;
}
if ($type == $this->groupType) {
$this->jumpRoomType = "MessageRoomGroup";
$isInGroupFlag = $this->ctx->SiteGroupTable->getGroupProfile($id, $this->userId);
$this->jumpRelation = $isInGroupFlag != false ? 1 : 0;
} elseif ($type == $this->u2Type) {
$this->jumpRoomType = "MessageRoomU2";
$isFriendFlag = $this->ctx->SiteUserFriendTable->isFollow($this->userId, $id);
$this->jumpRelation = $isFriendFlag > 0 ? 1 : 0;
}
$this->jumpRoomId = $id;
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error msg =" . $ex->getMessage());
}
}
public function getUserIdByCookie()
{
$tag = __CLASS__ . "-" . __FUNCTION__;
try {
$this->checkUserCookie();
} catch (Exception $ex) {
$this->ctx->Wpf_Logger->error($tag, "error msg =" . $ex->getMessage());
$this->setLogout();
}
}
public function checkUserCookie()
{
$tag = __CLASS__ . "-" . __FUNCTION__;
$this->sessionId = isset($_COOKIE[$this->siteCookieName]) ? $_COOKIE[$this->siteCookieName] : "";
if (!$this->sessionId) {
throw new Exception("cookie is not found");
}
$this->sessionInfo = $this->ctx->SiteSessionTable->getSessionInfoBySessionId($this->sessionId);
if (!$this->sessionInfo) {
throw new Exception("session is not ok");
}
$timeActive = $this->sessionInfo['timeActive'];
$nowTime = $this->ctx->ZalyHelper->getMsectime();
if (($nowTime - $timeActive) > $this->sessionIdTimeOut * 24 * 365) {
throw new Exception("session expired");
}
$this->userInfo = $this->ctx->SiteUserTable->getUserByUserId($this->sessionInfo['userId']);
if (!$this->userInfo) {
throw new Exception("user is not ok");
}
$this->sessionId = $this->sessionInfo['sessionId'];
$this->userId = $this->userInfo['userId'];
}
public function setLogout()
{
$x = isset($_GET['x']) ? $_GET['x'] : "";
setcookie($this->siteCookieName, "", time() - 3600, "/", "", false, true);
$apiPageLogin = ZalyConfig::getConfig("apiPageLogin");
if ($x) {
if (strpos($apiPageLogin, "?")) {
header("Location:" . $apiPageLogin . "&x=" . $x);
} else {
header("Location:" . $apiPageLogin . "?x=" . $x);
}
} else {
header("Location:" . $apiPageLogin);
}
exit();
}
public function setTransDataHeaders($key, $val)
{
$key = "_{$key}";
$this->headers[$key] = $val;
}
public function setHeader()
{
$this->setTransDataHeaders(TransportDataHeaderKey::HeaderSessionid, $this->sessionId);
$this->setTransDataHeaders(TransportDataHeaderKey::HeaderUserAgent, $_SERVER['HTTP_USER_AGENT']);
}
public function display($viewName, $params = [])
{
// 自己实现实现一下这个方法,加载view目录下的文件
$params['session_id'] = $this->sessionId;
$params['user_id'] = $this->userId;
$params['nickname'] = $this->userInfo['nickname'] ? $this->userInfo['nickname'] : "匿名";
$params['loginName'] = ZalyHelper::hideMobile($this->userInfo['loginName']);
$params['avatar'] = $this->userInfo['avatar'];
$params['jumpPage'] = ZalyConfig::getApiPageJumpUrl();
if (!isset($params['login'])) {
$params['login'] = '';
}
$params['jumpRoomId'] = $this->jumpRoomId;
$params['jumpRoomType'] = $this->jumpRoomType;
$params['jumpRelation'] = $this->jumpRelation;
$params['versionCode'] = ZalyConfig::getConfig("siteVersionCode");
return parent::display($viewName, $params);
}
/**
* 查库操作
*/
public function getSiteConfigFromDB($columns)
{
try {
$results = $this->ctx->SiteConfigTable->selectSiteConfig($columns);
return $results;
} catch (Exception $e) {
$tag = __CLASS__ . "-" . __FUNCTION__;
$this->ctx->Wpf_Logger->error($tag, " errorMsg = " . $e->getMessage());
return [];
}
}
}