HiboxPublicContactController.php
16.5 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
Yii::import('application.components.HiboxUpdateCache');
require_once realpath(Yii::app()->basePath . '/../../util/MQ/UserOnline/YMQUserOnlineGetData.class.php');
require_once realpath(Yii::app()->basePath . '/../../util/MQ/UserOnline/YMQUserOnlineSetData.class.php');
require_once realpath(Yii::app()->basePath . '/../../util/MQProcess/YUMMQProcess.class.php');
/**
* 公共联系人类控制器
*/
class HiboxPublicContactController extends HiboxUpdateCache
{
/**
* @var string the default layout for the views. Defaults to '//layouts/column1', meaning
* using two-column layout. See 'protected/views/layouts/column1.php'.
*/
public $layout = '//layouts/column1';
/**
* @var CActiveRecord the currently loaded data model instance.
*/
private $_model;
/**
* @return array action filters
*/
public function filters()
{
return array(
//'accessControl',
);
}
/**
* 显示公共联系人的Wave列表
*/
public function actionIndex()
{
// 默认只显示两人聊天Wave
$arg = Yii::app()->request->getQuery('q', 2);
// 获取公共联系人的Wave列表
$model = $this->_getWaveList($arg);
$this->render('index', array('model' => $model));
}
/**
* 创建公共联系人
*
* 原通过后台管理方式动态生成, 已不再使用. (old)
*/
public function actionCreate()
{
$model = new HiboxPublicContactForm;
$this->performAjaxValidation($model);
if (isset($_POST['HiboxPublicContactForm']))
{
$model->attributes = $_POST['HiboxPublicContactForm'];
if ($model->validate())
{
// 解析配置文件
$iniArray = $this->parseIniFileToArray();
// 获取前端文件所在的目录地址
$frontDirectory = $this->getCurrentFrontDirectory();
// 源文件存放路径
$sourceFile = $frontDirectory.$iniArray["HIBOX_PUBLIC_CONTACT_SET"]["path"].DIRECTORY_SEPARATOR.$iniArray["HIBOX_PUBLIC_CONTACT_SET"]["file"];
// 获取目标缓存文件存放的目录
$destDirectory = $this->getBackCacheDirectory();
$destFile = $destDirectory.DIRECTORY_SEPARATOR.$iniArray["HIBOX_PUBLIC_CONTACT_SET"]["file"];
// 如果目录不存在新建一个可写目录
if (!file_exists($destDirectory))
{
mkdir($destDirectory, 0777);
}
// 如果目标目录不可写, 改变文件模式
if (!is_writable($destDirectory))
{
chmod($destFile, 0777);
}
// 判断是否存在缓存文件, 没有新建
if (!file_exists($sourceFile))
{
$handle = @fopen($destFile, 'w');
$content = '<?php '.PHP_EOL. 'global $publicContactID;'.PHP_EOL.'$publicContactID = '.$model->contactID.';'.PHP_EOL.' ?>';
fwrite($handle, $content);
fclose($handle);
// 改变文件权限为: 可读可执行
chmod($destFile, 0755);
Yii::app()->user->setFlash('success', '已成功创建公共联系人!');
}
else
{
Yii::app()->user->setFlash('error', '已创建过公共联系人!');
$this->refresh();
Yii::app()->end();
}
$model->onlineState = Yii::app()->request->getPost('onlineState', 1);
// 加载需要的常量定义文件
include $frontDirectory. $iniArray["HIBOX_PUBLIC_CONTACT_SET"]["path"].DIRECTORY_SEPARATOR.$iniArray["HIBOX_PUBLIC_CONTACT_SET"]["const"];
// 设置公共联系人当前在线信息
$this->_setUserOnlineInfo($model->contactID, $model->onlineState);
}
}
$this->render('create', array('model' => $model));
}
/**
* 修改公共联系人
*/
public function actionUpdate()
{
$model = new HiboxPublicContactForm;
$this->performAjaxValidation($model);
// include realpath(Yii::app()->basePath.'/extensions/MQ/UserOnline/YMQUserOnlineGetData.class.php');
// include realpath(Yii::app()->basePath.'/extensions/MQ/UserOnline/YMQUserOnlineSetData.class.php');
// include realpath(Yii::app()->basePath.'/extensions/MQ/MQProcess/YUMMQProcess.class.php');
// 解析配置文件
$iniArray = $this->parseIniFileToArray();
// 获取前端文件所在的目录地址
$frontDirectory = $this->getCurrentFrontDirectory();
// 源文件存放路径
$sourceFile = $frontDirectory.$iniArray["HIBOX_PUBLIC_CONTACT_SET"]["path"].DIRECTORY_SEPARATOR.$iniArray["HIBOX_PUBLIC_CONTACT_SET"]["file"];
// 获取目标缓存文件存放的目录
$destDirectory = $this->getBackCacheDirectory();
$destFile = $destDirectory.DIRECTORY_SEPARATOR.$iniArray["HIBOX_PUBLIC_CONTACT_SET"]["file"];
// 检测是否创建过公共联系人
if(!file_exists($destFile) && !file_exists($sourceFile))
{
throw new CHttpException(400, '您尚未创建公共联系人, 不能执行此操作!');
}
// 判断是否存在缓存文件
if(!file_exists($destFile) && file_exists($sourceFile))
{
copy($sourceFile, $destFile);
}
// 加载公共联系人配置文件
include $destFile;
global $publicContactID;
// 获取公共联系人的当前在线状态
$onlineState = YMQUserOnlineGetData::getAll($publicContactID);
if (!is_array($onlineState))
{
$onlineState = array('onlineState' => 0);
}
if (isset($_POST['HiboxPublicContactForm']))
{
$model->attributes = $_POST['HiboxPublicContactForm'];
$newOnlineState = Yii::app()->request->getPost('onlineState', 1);
// 用于检查是否有修改操作
$isUpdate = null;
// 修改公共联系人ID号
if ($model->contactID != $publicContactID)
{
$isUpdate = true;
$content = file_get_contents($destFile);
$content = str_replace("$publicContactID", "$model->contactID", $content);
file_put_contents($destFile, $content);
unset($content);
}
// 修改公共联系人的当前在线状态
if ($newOnlineState != $onlineState['onlineState'])
{
$isUpdate = true;
$model->onlineState = $newOnlineState;
$onlineState['onlineState'] = $newOnlineState;
// 设置公共联系人的在线状态信息
YMQUserOnlineSetData::set($publicContactID, json_encode($onlineState));
// 设置公共联系人永久在线
YMQUserOnlineSetData::setTimeout($publicContactID, 0);
// // 发送即时消息到服务器
// $data = array('state' => $newOnlineState);
// $this->_changeStateToMQ($userID, $data);
$this->_setUserOnlineInfo($publicContactID, $newOnlineState);
}
// 未进行修改操作
if ($isUpdate === null)
{
Yii::app()->user->setFlash('error', '尚未进行任何修改!');
}
else
{
Yii::app()->user->setFlash('success', '已成功修改公共联系人!');
}
}
else
{
$model->contactID = $publicContactID;
$model->onlineState = $onlineState['onlineState'];
}
$this->render('update', array('model' => $model));
}
/**
* 更新缓存文件
*/
public function actionUpdateCacheFile()
{
$this->updateCacheFile();
}
/**
* 初始化消息
*/
public function actionAjaxInitMessage()
{
if (Yii::app()->request->isPostRequest)
{
$waveID = Yii::app()->request->getPost('waveID', null);
if (isset($waveID) && is_numeric($waveID))
{
$model = Wave::model()->with('messages')->find('', array(':waveID' => $waveID));
$this->renderPartial('_initMessage', array('model' => $model));
}
}
}
/**
* 发送消息
*/
public function actionSendMessage()
{
if (Yii::app()->request->isPostRequest)
{
$userID = $this->_getPublicContactID();
$waveID = Yii::app()->request->getPost('waveID', null);
$message = Yii::app()->request->getPost('message', null);
if (!isset($waveID, $message))
{
throw new CHttpException(400, '消息内容不能为空!');
}
// 目前只处理100K以下的信息内容, 100K以上的暂时不支持, 转为大文本格式
if (mb_strlen($message, 'utf8') >= (10*1024))
{
$hugeContentID = $this->_addHugeContent($message);
$message = '<span class = "hibox_msg"><span class = "hibox_bigContent">'.mb_substr(trim(strip_tags($message)), 0, 512, 'utf8');
$message.= ' ... <a herf = "#" class = "opt_more_info" onclick = "IYOHO_opt.WaveTalkContent.moreMsg(this,'.$hugeContentID.')">[点击查看全文]</a></span></span>';
}
// 消息内容进行字符串格式转义
$message = htmlspecialchars(addslashes($message));
$connection = Yii::app()->db;
$transaction = $connection->beginTransaction();
try
{
$sqlStatement = "CALL p_im_userMsgs_i(:waveID, :userID, :message, 10)";
$command = $connection->createCommand($sqlStatement);
$command->bindValue(':waveID', $waveID);
$command->bindValue(':userID', $userID);
$command->bindValue(':message', "$message");
$messageID = $command->queryScalar();
// 返回上一页
$this->redirect(Yii::app()->request->urlReferrer);
$transaction->commit();
}
catch (Exception $e)
{
echo $e->getMessage();
$transaction->rollback();
}
}
}
/**
* Ajax 验证表单
*/
protected function performAjaxValidation($model)
{
if (isset($_POST['ajax']) && $_POST['ajax'] === 'hibox-public-contact-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
/**
* 获取公共联系人ID号
*/
private function _getPublicContactID()
{
static $contactID = null;
if (null === $contactID)
{
// 解析配置文件
$iniArray = $this->parseIniFileToArray();
// 获取前端文件所在的目录地址
$frontDirectory = $this->getCurrentFrontDirectory();
// 源文件存放路径
$sourceFile = $frontDirectory.$iniArray["HIBOX_PUBLIC_CONTACT_SET"]["path"].DIRECTORY_SEPARATOR.$iniArray["HIBOX_PUBLIC_CONTACT_SET"]["file"];
if (!file_exists($sourceFile))
{
throw new CHttpException(400, '您尚未设置公共联系人, 不能执行此操作!');
}
include $sourceFile;
global $publicContactID;
$contactID = $publicContactID;
unset($iniArray);
}
return $contactID;
}
/**
* 显示公共联系人的Wave列表
*/
private function _getWaveList()
{
$model = array();
$classID = func_get_arg(0);
$classID = (int) $classID;
$userID = $this->_getPublicContactID();
if (is_numeric($userID) && is_int($classID))
{
// 查询公共联系人的Wave列表
$waveList = Wave::model()->with('send')->findAll('', array(':userID' => $userID, ':classID' => $classID));
// 构建Wave列表格式
foreach ($waveList as $wave)
{
$title = $wave->title.' ('.$wave->send->noReads. '条更新'.')';
$content = '<div id = "message'. $wave->id. '">'. CHtml::link('点击查看', '', array('class' => 'btn', 'onclick' => 'initMessage('. $wave->id. ')')). '</div>';
$model[$title] = $content;
}
}
return $model;
}
/**
* 添加大文本消息
*/
private function _addHugeContent($message)
{
if (is_string($message))
{
// 压缩后存储, 由于要加引号, 所以这里加上转义符号
$message = addslashes(gzcompress($message));
$connection = Yii::app()->db;
$sqlStatement = "CALL p_im_userMsgs_big_i(:message);";
$command = $connection->createCommand($sqlStatement);
$command->bindValue(':message', "$message");
// 向大文件表中添加内容数据库操作,返回ID
$hugeContentID = $command->queryScalar();
return $hugeContentID;
}
}
/**
* 发送在线状态到MQ中
*
* @param int $userID
* @param array $data
*/
private function _changeStateToMQ($userID, $data)
{
// 构造待处理MQ中的创建wave数据实体
$protocolData = array(
'user' => 0,
'type' => 0,
'data' => array(),
);
$protocolData['user'] = $userID;
$protocolData['type'] = UM_CHANGESTATE;
$protocolData['data'] = $data;
$mMQProcess = new YUMMQProcess();
// 处理MQ对象
return $mMQProcess->deal($protocolData);
}
/**
* 将登录信息发送到MQ函数
*
* MQ中需要的登录数据协议格式
* {
* user:userID
* type:数据协议键值
* data
* {
* state :onlineState
* }
* }
*
* @param int $userID
* @param int $onlineState
*/
private function _loginToMQ($userID, $onlineState)
{
$loginProtocol = array(
'user' => $userID,
'type' => UM_LOGIN,
'data' => array('state' => $onlineState),
);
$MQProcess = new YUMMQProcess();
$MQProcess->deal($loginProtocol);
}
/**
* 设置公共联系人登录状态信息
*
* @param int $userID
* @param int $onlineState
*/
private function _setUserOnlineInfo($userID, $onlineState)
{
// include realpath(Yii::app()->basePath.'/extensions/MQ/UserOnline/YMQUserOnlineSetData.class.php');
// include realpath(Yii::app()->basePath.'/extensions/MQ/MQProcess/YUMMQProcess.class.php');
// 获取IP地址
$ipObj = new IP();
$ip = $ipObj->getip();
// 生成随机码,随机码为8位
$randomKey = substr(md5(uniqid(rand(),1)), 24, 8);
// 计算key值
$keyValue = $userID + time() + $ip;
// 得到md5的key值
$key = md5($keyValue.uniqid(rand(), 1));
$userStateInfo = new stdClass();
$userStateInfo->onlineState = $onlineState;
//根据用户ID奇偶截取key的位置和长度
if($userID % 2)
{
// 奇数,截取
$userStateInfo->key = substr($key, 2, 22);
}
else
{ // 偶数,截取
$userStateInfo->key = substr($key, 4, 22);
}
// 随机码赋值
$userStateInfo->randomKey = $randomKey;
// 设置状态服务器
YMQUserOnlineSetData::set($userID, json_encode($userStateInfo));
// 发送到MQ
$this->_loginToMQ($userID, $onlineState);
}
}
?>