PageRenderTime 128ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/firstrend/src/core/service/message.service.php

http://ownerpress.googlecode.com/
PHP | 576 lines | 434 code | 82 blank | 60 comment | 76 complexity | 915de2ed817e08b8aa6fb4471bd2fbc6 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ?????????? (Build on ThinkPHP)
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. /**
  8. * message.service.php
  9. *
  10. * ?????
  11. *
  12. * @package service
  13. * @author awfigq <awfigq@qq.com>
  14. */
  15. //???????
  16. define('MSG_SEND_NONE_ERROR','-10');
  17. define('MSG_MLIST_NONE_ERROR','-20');
  18. define('MSG_PRIVILEGE_NONE_ERROR','-30');
  19. class MessageService
  20. {
  21. /**
  22. * ?????????
  23. * @return void
  24. */
  25. public function sysMsgInit($uid,$gid)
  26. {
  27. $count = 0;
  28. $res = FDB::query('SELECT m.mid
  29. FROM '.FDB::table('sys_msg').' AS m
  30. LEFT JOIN '.FDB::table('sys_msg_member').' AS mm ON mm.mid = m.mid AND mm.uid = '.$uid.'
  31. WHERE mm.mid IS NULL AND (m.end_time = 0 OR m.end_time >= '.TIME_UTC.')');
  32. while($data = FDB::fetch($res))
  33. {
  34. $mid = $data['mid'];
  35. $is_no = FDB::resultFirst('SELECT COUNT(mid)
  36. FROM '.FDB::table('sys_msg_user_no')."
  37. WHERE mid = '$mid' AND uid = '$uid'");
  38. if($is_no > 0)
  39. continue;
  40. $is_send = false;
  41. $is_yes = FDB::resultFirst('SELECT COUNT(mid)
  42. FROM '.FDB::table('sys_msg_user_yes')."
  43. WHERE mid = '$mid' AND uid = '$uid'");
  44. if($is_yes > 0)
  45. $is_send = true;
  46. else
  47. {
  48. $is_yes = FDB::resultFirst('SELECT COUNT(mid)
  49. FROM '.FDB::table('sys_msg_user_group')."
  50. WHERE mid = '$mid' AND gid = '$gid'");
  51. if($is_yes > 0)
  52. $is_send = true;
  53. }
  54. if($is_send)
  55. {
  56. $count++;
  57. $msg = array();
  58. $msg['mid'] = $mid;
  59. $msg['uid'] = $uid;
  60. $msg['dateline'] = TIME_UTC;
  61. FDB::insert('sys_msg_member',$msg);
  62. }
  63. }
  64. if($count > 0)
  65. {
  66. $result = FDB::query("INSERT INTO ".FDB::table('user_notice')."(uid, type, num, create_time) VALUES('$uid',5,'$count','".TIME_UTC."')", 'SILENT');
  67. if(!$result)
  68. FDB::query("UPDATE ".FDB::table('user_notice')." SET num = num + '$count', create_time='".TIME_UTC."' WHERE uid='$uid' AND type=5");
  69. }
  70. }
  71. /**
  72. * ????????
  73. * @return void
  74. */
  75. public function getSysMsgs($uid)
  76. {
  77. $list = FDB::fetchAll('SELECT *
  78. FROM '.FDB::table('sys_msg_member').' AS mm
  79. LEFT JOIN '.FDB::table('sys_msg').' AS m ON m.mid = mm.mid
  80. WHERE mm.status < 2 AND mm.uid = '.$uid.'
  81. ORDER BY m.mid DESC');
  82. FDB::query("UPDATE ".FDB::table('sys_msg_member')." SET status = 1 WHERE uid='$uid' AND status = 0");
  83. return $list;
  84. }
  85. /**
  86. * ??????
  87. * @return void
  88. */
  89. public function getSysMsgByMid($uid,$mid)
  90. {
  91. return FDB::fetchFirst('SELECT *
  92. FROM '.FDB::table('sys_msg_member').' AS mm
  93. LEFT JOIN '.FDB::table('sys_msg').' AS m ON m.mid = mm.mid
  94. WHERE mm.status < 2 AND mm.mid = '.$mid.' AND mm.uid = '.$uid);
  95. }
  96. /**
  97. * ??????
  98. * @return void
  99. */
  100. public function deleteSysMsg($uid,$mid)
  101. {
  102. FDB::query("UPDATE ".FDB::table('sys_msg_member')." SET status = 2 WHERE uid='$uid' AND mid='$mid'");
  103. return 1;
  104. }
  105. /**
  106. * ????
  107. * @param int $fuid ??????
  108. * @param string $fusername ??????
  109. * @param array $tuids ????????
  110. * @param string $subject ????
  111. * @param string $message ????
  112. * @param int $type ????
  113. * @return int
  114. */
  115. public function sendMsg($fuid, $fusername, $tuids, $subject, $message, $type = 0)
  116. {
  117. if(empty($fuid) || empty($fusername) || empty($tuids) || empty($message))
  118. return 0;
  119. //????????
  120. $tuids = array_unique($tuids);
  121. $relations = $mlids = array();
  122. $tmp_tuids = $tuids;
  123. foreach($tmp_tuids as $key => $uid)
  124. {
  125. if($fuid == $uid || empty($uid))
  126. {
  127. unset($tuids[$key]);
  128. continue;
  129. }
  130. $relations[$uid] = MessageService::getRelation($fuid, $uid);
  131. }
  132. if(empty($tuids))
  133. return MSG_SEND_NONE_ERROR;
  134. if(!$subject)
  135. {
  136. $subject = htmlspecialchars(cutStr(clearExpress(trim($message)), 80));
  137. }
  138. else
  139. {
  140. $subject = htmlspecialchars($subject);
  141. }
  142. $last_msg = htmlspecialchars(cutStr(clearExpress(trim($message)), 150));
  143. $type = 0;
  144. if($type == 0)
  145. {
  146. $res = FDB::query('SELECT mlid,min_max FROM '.FDB::table('user_msg_list')." WHERE min_max".FDB::createIN($relations));
  147. while($data = FDB::fetch($res))
  148. {
  149. $mlids[$data['min_max']] = $data['mlid'];
  150. }
  151. $msg_config = array('last_uid' => $fuid, 'last_user_name' => $fusername, 'last_msg' => $last_msg);
  152. $msg_config = addslashes(serialize($msg_config));
  153. foreach($relations as $key => $value)
  154. {
  155. if(!isset($mlids[$value]))
  156. {
  157. FDB::query("INSERT INTO ".FDB::table('user_msg_list')."(uid,type,subject,members,min_max,dateline,msg_config) VALUES('$fuid', '1', '$subject', 2, '$value', '".TIME_UTC."', '$msg_config')");
  158. $mlid = FDB::insertId();
  159. FDB::query("INSERT INTO ".FDB::table('user_msg_index')."(mlid) VALUES('$mlid')");
  160. $miid = FDB::insertId();
  161. FDB::query("INSERT INTO ".MessageService::getTablaName($mlid)."(miid,mlid,uid,message,dateline,status) VALUES('$miid', '$mlid', '$fuid', '$message', '".TIME_UTC."', 0)");
  162. FDB::query("INSERT INTO ".FDB::table('user_msg_member')."(mlid, uid, is_new, num, last_update, last_dateline) VALUES('$mlid', '$key', '1', '1', '0', '".TIME_UTC."')");
  163. FDB::query("INSERT INTO ".FDB::table('user_msg_member')."(mlid, uid, is_new, num, last_update, last_dateline) VALUES('$mlid', '$fuid', '0', '1', '".TIME_UTC."', '".TIME_UTC."')");
  164. }
  165. else
  166. {
  167. $mlid = $mlids[$value];
  168. FDB::query("INSERT INTO ".FDB::table('user_msg_index')."(mlid) VALUES('$mlid')");
  169. $miid = FDB::insertId();
  170. FDB::query("INSERT INTO ".MessageService::getTablaName($mlid)."(miid,mlid,uid,message,dateline,status) VALUES('$miid', '$mlid', '$fuid', '$message', '".TIME_UTC."', 0)");
  171. $result = FDB::query("INSERT INTO ".FDB::table('user_msg_member')."(mlid, uid, is_new, num, last_update, last_dateline) VALUES('$mlid', '$key', '1', '1', '0', '".TIME_UTC."')", 'SILENT');
  172. if(!$result)
  173. FDB::query("UPDATE ".FDB::table('user_msg_member')." SET is_new = 1, num = num + 1, last_dateline='".TIME_UTC."' WHERE mlid='$mlid' AND uid='$key'");
  174. $result = FDB::query("INSERT INTO ".FDB::table('user_msg_member')."(mlid, uid, is_new, num, last_update, last_dateline) VALUES('$mlid', '$fuid', '0', '1', '".TIME_UTC."', '".TIME_UTC."')", 'SILENT');
  175. if(!$result)
  176. FDB::query("UPDATE ".FDB::table('user_msg_member')." SET is_new = 1, num = num + 1, last_update='".TIME_UTC."', last_dateline='".TIME_UTC."' WHERE mlid='$mlid' AND uid='$fuid'");
  177. FDB::query("UPDATE ".FDB::table('user_msg_list')." SET msg_config='$msg_config' WHERE mlid='$mlid'");
  178. }
  179. }
  180. }
  181. else
  182. {
  183. }
  184. foreach($tuids as $uid)
  185. {
  186. $result = FDB::query("INSERT INTO ".FDB::table('user_notice')."(uid, type, num, create_time) VALUES('$uid',5,1,'".TIME_UTC."')", 'SILENT');
  187. if(!$result)
  188. FDB::query("UPDATE ".FDB::table('user_notice')." SET num = num + 1, create_time='".TIME_UTC."' WHERE uid='$uid' AND type=5");
  189. }
  190. return $miid;
  191. }
  192. /**
  193. * ????
  194. * @param int $mlid ?????
  195. * @param int $fuid ??????
  196. * @param string $fusername ??????
  197. * @param string $message ????
  198. * @return int
  199. */
  200. public function replyMsg($mlid, $fuid, $fusername, $message)
  201. {
  202. if(empty($mlid) || empty($fuid) || empty($fusername) || empty($message))
  203. return 0;
  204. $mlist = FDB::fetchFirst("SELECT * FROM ".FDB::table('user_msg_list')." WHERE mlid='$mlid'");
  205. if(empty($mlist))
  206. return MSG_MLIST_NONE_ERROR;
  207. if($mlist['type'] == 1)
  208. {
  209. $users = explode('_', $mlist['min_max']);
  210. if($users[0] == $fuid)
  211. $tuid = $users[1];
  212. elseif($users[1] == $fuid)
  213. $tuid = $users[0];
  214. else
  215. return MSG_PRIVILEGE_NONE_ERROR;
  216. }
  217. $members = array();
  218. $query = FDB::query("SELECT * FROM ".FDB::table('user_msg_member')." WHERE mlid='$mlid'");
  219. while($member = FDB::fetch($query))
  220. {
  221. $members[$member['uid']] = "('$member[uid]')";
  222. }
  223. if(!isset($members[$fuid]))
  224. return MSG_PRIVILEGE_NONE_ERROR;
  225. $last_msg = htmlspecialchars(cutStr(clearExpress(trim($message)), 150));
  226. $type = 0;
  227. FDB::query("INSERT INTO ".FDB::table('user_msg_index')."(mlid) VALUES('$mlid')");
  228. $miid = FDB::insertId();
  229. FDB::query("INSERT INTO ".MessageService::getTablaName($mlid)."(miid,mlid,uid,message,dateline,status) VALUES('$miid', '$mlid', '$fuid', '$message', '".TIME_UTC."', 0)");
  230. if($mlist['type'] == 1)
  231. {
  232. $msg_config = array('last_uid' => $fuid, 'last_user_name' => $fusername, 'last_msg' => $last_msg);
  233. $msg_config = addslashes(serialize($msg_config));
  234. $result = FDB::query("INSERT INTO ".FDB::table('user_msg_member')."(mlid, uid, is_new, num, last_update, last_dateline) VALUES('$mlid', '$tuid', '1', '1', '0', '".TIME_UTC."')", 'SILENT');
  235. if(!$result)
  236. FDB::query("UPDATE ".FDB::table('user_msg_member')." SET is_new = 1, num = num + 1, last_dateline='".TIME_UTC."' WHERE mlid='$mlid' AND uid='$tuid'");
  237. FDB::query("UPDATE ".FDB::table('user_msg_member')." SET is_new = 0, num = num + 1, last_update='".TIME_UTC."', last_dateline='".TIME_UTC."' WHERE mlid='$mlid' AND uid='$fuid'");
  238. }
  239. else
  240. {
  241. }
  242. FDB::query("UPDATE ".FDB::table('user_msg_list')." SET msg_config='$msg_config' WHERE mlid='$mlid'");
  243. $result = FDB::query("INSERT INTO ".FDB::table('user_notice')."(uid, type, num, create_time) VALUES('$tuid',5,1,'".TIME_UTC."')", 'SILENT');
  244. if(!$result)
  245. FDB::query("UPDATE ".FDB::table('user_notice')." SET num = num + 1, create_time='".TIME_UTC."' WHERE uid='$tuid' AND type=5");
  246. return $miid;
  247. }
  248. public function deleteByMiid($uid,$miid)
  249. {
  250. if(empty($miid) || empty($uid))
  251. return 0;
  252. $index = FDB::fetchFirst("SELECT * FROM ".FDB::table('user_msg_index')." AS mi
  253. LEFT JOIN ".FDB::table('user_msg_list')." AS ml ON ml.mlid=mi.mlid
  254. WHERE mi.miid='$miid'");
  255. $users = explode('_', $index['min_max']);
  256. if(!in_array($uid, $users))
  257. return MSG_PRIVILEGE_NONE_ERROR;
  258. $mlid = $index['mlid'];
  259. if($index['uid'] != $uid)
  260. {
  261. FDB::query("UPDATE ".MessageService::getTablaName($mlid)." SET status = 2 WHERE miid='$miid' AND status=0");
  262. $update_num = FDB::affectedRows();
  263. FDB::query("DELETE FROM ".MessageService::getTablaName($mlid)." WHERE miid='$miid' AND status=1");
  264. $delete_num = FDB::affectedRows();
  265. }
  266. else
  267. {
  268. FDB::query("UPDATE ".MessageService::getTablaName($mlid)." SET status = 1 WHERE miid='$miid' AND status=0");
  269. $update_num = FDB::affectedRows();
  270. FDB::query("DELETE FROM ".MessageService::getTablaName($mlid)." WHERE miid='$miid' AND status=2");
  271. $delete_num = FDB::affectedRows();
  272. }
  273. if(!FDB::resultFirst("SELECT COUNT(*) FROM ".MessageService::getTablaName($mlid)." WHERE mlid='$index[mlid]'"))
  274. {
  275. FDB::query("DELETE FROM ".FDB::table('user_msg_list')." WHERE mlid='$mlid'");
  276. FDB::query("DELETE FROM ".FDB::table('user_msg_member')." WHERE mlid='$mlid'");
  277. FDB::query("DELETE FROM ".FDB::table('user_msg_index')." WHERE mlid='$mlid'");
  278. }
  279. else
  280. {
  281. FDB::query("UPDATE ".FDB::table('user_msg_member')." SET num = num - ".($update_num + $delete_num)." WHERE mlid='".$mlid."' AND uid='$uid'");
  282. }
  283. return 1;
  284. }
  285. public function deleteByMlid($uid, $mlid)
  286. {
  287. if(empty($mlid) || empty($uid))
  288. return 0;
  289. $list = FDB::fetchFirst("SELECT * FROM ".FDB::table('user_msg_list')." WHERE mlid='$mlid'");
  290. if(empty($list))
  291. return MSG_MLIST_NONE_ERROR;
  292. if($list['type'] == 1)
  293. {
  294. $user = explode('_', $list['min_max']);
  295. if(!in_array($uid, $user))
  296. return MSG_PRIVILEGE_NONE_ERROR;
  297. }
  298. else
  299. {
  300. if($uid != $list['uid'])
  301. return MSG_PRIVILEGE_NONE_ERROR;
  302. }
  303. if($list['type'] == 1)
  304. {
  305. if($uid == $list['uid'])
  306. {
  307. FDB::query("DELETE FROM ".MessageService::getTablaName($mlid)." WHERE mlid='$mlid' AND status = 2");
  308. FDB::query("UPDATE ".MessageService::getTablaName($mlid)." SET status = 1 WHERE mlid='$mlid' AND status=0");
  309. }
  310. else
  311. {
  312. FDB::query("DELETE FROM ".MessageService::getTablaName($mlid)." WHERE mlid='$mlid' AND status=1");
  313. FDB::query("UPDATE ".MessageService::getTablaName($mlid)." SET status=2 WHERE mlid='$mlid' AND status=0");
  314. }
  315. $count = FDB::resultFirst("SELECT COUNT(*) FROM ".MessageService::getTablaName($mlid)." WHERE mlid='$mlid'");
  316. if(!$count)
  317. {
  318. FDB::query("DELETE FROM ".FDB::table('user_msg_list')." WHERE mlid='$mlid'");
  319. FDB::query("DELETE FROM ".FDB::table('user_msg_member')." WHERE mlid='$mlid'");
  320. FDB::query("DELETE FROM ".FDB::table('user_msg_index')." WHERE mlid='$mlid'");
  321. }
  322. else
  323. {
  324. FDB::query("DELETE FROM ".FDB::table('user_msg_member')." WHERE mlid='$mlid' AND uid='$uid'");
  325. }
  326. }
  327. else
  328. {
  329. }
  330. return 1;
  331. }
  332. public function getMsgCount($uid)
  333. {
  334. return FDB::resultFirst('SELECT COUNT(mlid) FROM '.FDB::table('user_msg_member')." WHERE uid='$uid'");
  335. }
  336. public function getMsgList($uid,$limit)
  337. {
  338. $list = array();
  339. if(empty($uid))
  340. return $list;
  341. $members = $tuids = array();
  342. $query = FDB::query('SELECT *
  343. FROM '.FDB::table('user_msg_member').' AS mm
  344. LEFT JOIN '.FDB::table('user_msg_list')." AS ml ON ml.mlid=mm.mlid
  345. WHERE mm.uid='$uid' ORDER BY mm.last_dateline DESC LIMIT $limit");
  346. while($member = FDB::fetch($query))
  347. {
  348. if($member['type'] == 1)
  349. {
  350. $users = explode('_', $member['min_max']);
  351. $member['tuid'] = $users[0] == $uid ? $users[1] : $users[0];
  352. }
  353. else
  354. $member['tuid'] = 0;
  355. $tuids[$member['tuid']] = $member['tuid'];
  356. $members[] = $member;
  357. }
  358. if($members)
  359. {
  360. $user_ids = array();
  361. foreach($members as $key => $data)
  362. {
  363. $daterange = 5;
  364. $data['founddateline'] = $data['dateline'];
  365. $data['time'] = getBeforeTimelag($data['last_dateline']);
  366. $msg_config = unserialize($data['msg_config']);
  367. if($msg_config['first_uid'])
  368. {
  369. $data['first_uid'] = $msg_config['first_uid'];
  370. $data['first_user_name'] = $msg_config['first_user_name'];
  371. $data['first_msg'] = $msg_config['first_msg'];
  372. }
  373. if($msg_config['last_uid'])
  374. {
  375. $data['last_uid'] = $msg_config['last_uid'];
  376. $data['last_user_name'] = $msg_config['last_user_name'];
  377. $data['last_msg'] = $msg_config['last_msg'];
  378. }
  379. $data['msg_fuid'] = $msg_config['last_uid'];
  380. $data['msg_fuser_name'] = $msg_config['last_user_name'];
  381. $data['message'] = $msg_config['last_msg'];
  382. $data['new'] = $data['is_new'];
  383. unset($data['min_max']);
  384. $list[$key] = $data;
  385. $list[$key]['msg_tuser'] = &$user_ids[$data['tuid']];
  386. }
  387. FS('User')->usersFormat($user_ids);
  388. }
  389. return $list;
  390. }
  391. public function getListByMlid($mlid,$uid)
  392. {
  393. static $list = array();
  394. $key = $mlid.'_'.$uid;
  395. if(!isset($list[$key]))
  396. {
  397. $data = FDB::fetchFirst('SELECT *
  398. FROM '.FDB::table('user_msg_member').' AS mm
  399. LEFT JOIN '.FDB::table('user_msg_list')." AS ml ON ml.mlid=mm.mlid
  400. WHERE mm.uid='$uid' AND ml.mlid = '$mlid'");
  401. if(!empty($data))
  402. {
  403. if($data['type'] == 1)
  404. {
  405. $users = explode('_', $data['min_max']);
  406. $data['tuid'] = $users[0] == $uid ? $users[1] : $users[0];
  407. }
  408. else
  409. $data['tuid'] = 0;
  410. $data['msg_tuser'] = FS('User')->getUserCache($data['tuid']);
  411. }
  412. $list[$key] = $data;
  413. }
  414. return $list[$key];
  415. }
  416. public function getMsgsByMlid($mlid,$uid,$limit)
  417. {
  418. $list = array();
  419. if(empty($mlid))
  420. return $list;
  421. $mlist = MessageService::getListByMlid($mlid,$uid);
  422. if(empty($mlist) || $mlist['type'] != 1)
  423. return $list;
  424. $where = '';
  425. if($mlist['uid'] == $uid)
  426. $where .= ' AND status IN (0,2)';
  427. else
  428. $where .= ' AND status IN (0,1)';
  429. $query = FDB::query('SELECT *
  430. FROM '.MessageService::getTablaName($mlid)."
  431. WHERE mlid='$mlid' $where ORDER BY dateline DESC LIMIT $limit");
  432. while($data = FDB::fetch($query))
  433. {
  434. $data['time'] = getBeforeTimelag($data['dateline']);
  435. $list[] = $data;
  436. }
  437. FDB::query("UPDATE ".FDB::table('user_msg_member')." SET is_new=0 WHERE mlid='$mlid' AND uid='$uid' AND is_new=1");
  438. return array_reverse($list);
  439. }
  440. public function getRelation($fuid, $tuid)
  441. {
  442. if($fuid < $tuid)
  443. return $fuid.'_'.$tuid;
  444. elseif($fuid > $tuid)
  445. return $tuid.'_'.$fuid;
  446. else
  447. return '';
  448. }
  449. public function getTablaName($id)
  450. {
  451. $id = substr((string)$id, -1, 1);
  452. return FDB::table('user_msg_'.$id);
  453. }
  454. /**
  455. * ?????????
  456. * @param mix $uid ????????????
  457. * @return array
  458. */
  459. public function getBlackUsers($uid)
  460. {
  461. $users = array();
  462. if(is_array($uid))
  463. {
  464. $uid = implode(',',$uid);
  465. $res = FDB::query('SELECT uid,black_users FROM '.FDB::table('user_status')." WHERE uid IN ($uid)");
  466. while($data = FDB::fetch($res))
  467. {
  468. $users[$data['uid']] = explode("\n",$data['black_users']);
  469. }
  470. }
  471. else
  472. {
  473. $users = FDB::resultFirst("SELECT black_users FROM ".FDB::table('user_status')." WHERE uid='$uid'");
  474. $users = explode("\n",$users);
  475. }
  476. return $users;
  477. }
  478. /**
  479. * ?????????
  480. * @param int $uid ????
  481. * @param mix $user_name ????????????
  482. * @return void
  483. */
  484. public function setBlackUsers($uid,$user_name)
  485. {
  486. $user_name = !is_array($user_name) ? array($user_name) : $user_name;
  487. if(!in_array('{ALL}', $user_name))
  488. $user_name = '{ALL}';
  489. else
  490. $user_name = implode('\n', $user_name);
  491. FDB::query("UPDATE ".FDB::table('user_status')." SET black_users='$user_name' WHERE uid='$uid'");
  492. }
  493. }
  494. ?>