pm.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. <?php
  2. /*
  3. [UCenter] (C)2001-2099 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: pm.php 1160 2013-10-24 08:04:45Z jeffjzhang $
  6. */
  7. !defined('IN_UC') && exit('Access Denied');
  8. define('PMINBALCKLIST_ERROR', -6);
  9. define('PMSENDSELF_ERROR', -8);
  10. define('PMSENDNONE_ERROR', -9);
  11. define('PMSENDCHATNUM_ERROR', -10);
  12. define('PMTHREADNONE_ERROR', -11);
  13. define('PMPRIVILEGENONE_ERROR', -12);
  14. define('PMCHATTYPE_ERROR', -13);
  15. define('PMUIDTYPE_ERROR', -14);
  16. define('PMDATA_ERROR', -15);
  17. class pmmodel {
  18. var $db;
  19. var $base;
  20. function __construct(&$base) {
  21. $this->pmmodel($base);
  22. }
  23. function pmmodel(&$base) {
  24. $this->base = $base;
  25. $this->db = $base->db;
  26. }
  27. function pmintval($pmid) {
  28. return @is_numeric($pmid) ? $pmid : 0;
  29. }
  30. function getpmbypmid($uid, $pmid) {
  31. if(!$pmid) {
  32. return array();
  33. }
  34. $arr = array();
  35. $pm = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."pm_indexes i LEFT JOIN ".UC_DBTABLEPRE."pm_lists t ON t.plid=i.plid WHERE i.pmid='$pmid'");
  36. if($this->isprivilege($pm['plid'], $uid)) {
  37. $pms = $this->db->fetch_all("SELECT t.*, p.*, t.authorid as founderuid, t.dateline as founddateline FROM ".UC_DBTABLEPRE.$this->getposttablename($pm['plid'])." p LEFT JOIN ".UC_DBTABLEPRE."pm_lists t ON t.plid=p.plid WHERE p.pmid='$pm[pmid]'");
  38. $arr = $this->getpostlist($pms);
  39. }
  40. return $arr;
  41. }
  42. function isprivilege($plid, $uid) {
  43. if(!$plid || !$uid) {
  44. return true;
  45. }
  46. $query = $this->db->query("SELECT * FROM ".UC_DBTABLEPRE."pm_members WHERE plid='$plid' AND uid='$uid'");
  47. if($this->db->fetch_array($query)) {
  48. return true;
  49. } else {
  50. return false;
  51. }
  52. }
  53. function getpmbyplid($uid, $plid, $starttime, $endtime, $start, $ppp, $type = 0) {
  54. if(!$type) {
  55. $pm = $this->getprivatepmbyplid($uid, $plid, $starttime, $endtime, $start, $ppp);
  56. } else {
  57. $pm = $this->getchatpmbyplid($uid, $plid, $starttime, $endtime, $start, $ppp);
  58. }
  59. return $this->getpostlist($pm);
  60. }
  61. function getpostlist($list) {
  62. if(empty($list)) {
  63. return array();
  64. }
  65. $authoridarr = $authorarr = array();
  66. foreach($list as $key => $value) {
  67. $authoridarr[$value['authorid']] = $value['authorid'];
  68. }
  69. if($authoridarr) {
  70. $this->base->load('user');
  71. $authorarr = $_ENV['user']->id2name($authoridarr);
  72. }
  73. foreach($list as $key => $value) {
  74. if($value['pmtype'] == 1) {
  75. $users = explode('_', $value['min_max']);
  76. if($value['authorid'] == $users[0]) {
  77. $value['touid'] = $users[1];
  78. } else {
  79. $value['touid'] = $users[0];
  80. }
  81. } else {
  82. $value['touid'] = 0;
  83. }
  84. $value['author'] = $authorarr[$value['authorid']];
  85. $value['msgfromid'] = $value['authorid'];
  86. $value['msgfrom'] = $value['author'];
  87. $value['msgtoid'] = $value['touid'];
  88. unset($value['min_max']);
  89. unset($value['delstatus']);
  90. unset($value['lastmessage']);
  91. $list[$key] = $value;
  92. }
  93. return $list;
  94. }
  95. function setpmstatus($uid, $touids, $plids, $status = 0) {
  96. if(!$uid) {
  97. return false;
  98. }
  99. if(!$status) {
  100. $oldstatus = 1;
  101. $newstatus = 0;
  102. } else {
  103. $oldstatus = 0;
  104. $newstatus = 1;
  105. }
  106. if($touids) {
  107. foreach($touids as $key => $value) {
  108. if($uid == $value || !$value) {
  109. return false;
  110. }
  111. $relastionship[] = $this->relationship($uid, $value);
  112. }
  113. $plid = $plidpostarr = array();
  114. $query = $this->db->query("SELECT plid FROM ".UC_DBTABLEPRE."pm_lists WHERE min_max IN (".$this->base->implode($relationship).")");
  115. while($thread = $this->db->fetch_array($query)) {
  116. $plidarr[] = $thread['plid'];
  117. }
  118. if($plidarr) {
  119. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_members SET isnew='$newstatus' WHERE plid IN (".$this->base->implode($plidarr).") AND uid='$uid' AND isnew='$oldstatus'");
  120. }
  121. }
  122. if($plids) {
  123. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_members SET isnew='$newstatus' WHERE plid IN (".$this->base->implode($plids).") AND uid='$uid' AND isnew='$oldstatus'");
  124. }
  125. return true;
  126. }
  127. function set_ignore($uid) {
  128. return $this->db->query("DELETE FROM ".UC_DBTABLEPRE."newpm WHERE uid='$uid'");
  129. }
  130. function isnewpm($uid) {
  131. return $this->db->result_first("SELECT uid FROM ".UC_DBTABLEPRE."newpm WHERE uid='$uid'");
  132. }
  133. function lastpm($uid) {
  134. $lastpm = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."pm_members m LEFT JOIN ".UC_DBTABLEPRE."pm_lists t ON m.plid=t.plid WHERE m.uid='$uid' ORDER BY m.lastdateline DESC LIMIT 1");
  135. $lastmessage = unserialize($lastpm['lastmessage']);
  136. if($lastmessage['lastauthorid']) {
  137. $lastpm['lastauthorid'] = $lastmessage['lastauthorid'];
  138. $lastpm['lastauthor'] = $lastmessage['lastauthor'];
  139. $lastpm['lastsummary'] = $lastmessage['lastsummary'];
  140. } else {
  141. $lastpm['lastauthorid'] = $lastmessage['firstauthorid'];
  142. $lastpm['lastauthor'] = $lastmessage['firstauthor'];
  143. $lastpm['lastsummary'] = $lastmessage['firstsummary'];
  144. }
  145. return $lastpm;
  146. }
  147. function getpmnum($uid, $type = 0, $isnew = 0) {
  148. $newsql = '';
  149. $newnum = 0;
  150. if($isnew) {
  151. $newsql = 'AND m.isnew=1';
  152. }
  153. if(!$type) {
  154. $newnum = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."pm_members m WHERE m.uid='$uid' $newsql");
  155. } else {
  156. $newnum = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."pm_members m LEFT JOIN ".UC_DBTABLEPRE."pm_lists t ON t.plid=m.plid WHERE m.uid='$uid' $newsql AND t.pmtype='$type'");
  157. }
  158. return $newnum;
  159. }
  160. function getpmnumbyplid($uid, $plid) {
  161. return $this->db->result_first("SELECT pmnum FROM ".UC_DBTABLEPRE."pm_members WHERE plid='$plid' AND uid='$uid'");
  162. }
  163. function sendpm($fromuid, $fromusername, $touids, $subject, $message, $type = 0) {
  164. if(!$fromuid || !$fromusername || !$touids || !$message) {
  165. return 0;
  166. }
  167. $touids = array_unique($touids);
  168. $relationship = $existplid = $pm_member_insertsql = array();
  169. $this->base->load('user');
  170. $tmptouidarr = $touids;
  171. $blackls = $this->get_blackls($fromuid, $touids);
  172. foreach($tmptouidarr as $key => $value) {
  173. if($fromuid == $value || !$value) {
  174. return PMSENDSELF_ERROR;
  175. }
  176. if(in_array('{ALL}', $blackls[$value])) {
  177. unset($touids[$key]);
  178. continue;
  179. }
  180. $blackls[$value] = $_ENV['user']->name2id($blackls[$value]);
  181. if(!(isset($blackls[$value]) && !in_array($fromuid, $blackls[$value]))) {
  182. unset($touids[$key]);
  183. } else {
  184. $relationship[$value] = $this->relationship($fromuid, $value);
  185. }
  186. }
  187. if(empty($touids)) {
  188. return PMSENDNONE_ERROR;
  189. }
  190. if($type == 1 && count($touids) < 2) {
  191. return PMSENDCHATNUM_ERROR;
  192. }
  193. $_CACHE['badwords'] = $this->base->cache('badwords');
  194. if($_CACHE['badwords']['findpattern']) {
  195. $subject = @preg_replace($_CACHE['badwords']['findpattern'], $_CACHE['badwords']['replace'], $subject);
  196. $message = @preg_replace($_CACHE['badwords']['findpattern'], $_CACHE['badwords']['replace'], $message);
  197. }
  198. if(!$subject) {
  199. $subject = $this->removecode(trim($message), 80);
  200. } else {
  201. $subject = dhtmlspecialchars($subject);
  202. }
  203. $lastsummary = $this->removecode(trim(stripslashes($message)), 150);
  204. if(!$type) {
  205. $query = $this->db->query("SELECT plid, min_max FROM ".UC_DBTABLEPRE."pm_lists WHERE min_max IN (".$this->base->implode($relationship).")");
  206. while($thread = $this->db->fetch_array($query)) {
  207. $existplid[$thread['min_max']] = $thread['plid'];
  208. }
  209. $lastmessage = array('lastauthorid' => $fromuid, 'lastauthor' => $fromusername, 'lastsummary' => $lastsummary);
  210. $lastmessage = addslashes(serialize($lastmessage));
  211. foreach($relationship as $key => $value) {
  212. if(!isset($existplid[$value])) {
  213. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_lists(authorid, pmtype, subject, members, min_max, dateline, lastmessage) VALUES('$fromuid', '1', '$subject', 2, '$value', '".$this->base->time."', '$lastmessage')");
  214. $plid = $this->db->insert_id();
  215. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_indexes(plid) VALUES('$plid')");
  216. $pmid = $this->db->insert_id();
  217. $this->db->query("INSERT INTO ".UC_DBTABLEPRE.$this->getposttablename($plid)."(pmid, plid, authorid, message, dateline, delstatus) VALUES('$pmid', '$plid', '$fromuid', '$message', '".$this->base->time."', 0)");
  218. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_members(plid, uid, isnew, pmnum, lastupdate, lastdateline) VALUES('$plid', '$key', '1', '1', '0', '".$this->base->time."')");
  219. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_members(plid, uid, isnew, pmnum, lastupdate, lastdateline) VALUES('$plid', '$fromuid', '0', '1', '".$this->base->time."', '".$this->base->time."')");
  220. } else {
  221. $plid = $existplid[$value];
  222. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_indexes(plid) VALUES('$plid')");
  223. $pmid = $this->db->insert_id();
  224. $this->db->query("INSERT INTO ".UC_DBTABLEPRE.$this->getposttablename($plid)."(pmid, plid, authorid, message, dateline, delstatus) VALUES('$pmid', '$plid', '$fromuid', '$message', '".$this->base->time."', 0)");
  225. $result = $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_members(plid, uid, isnew, pmnum, lastupdate, lastdateline) VALUES('$plid', '$key', '1', '1', '0', '".$this->base->time."')", 'SILENT');
  226. if(!$result) {
  227. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_members SET isnew=1, pmnum=pmnum+1, lastdateline='".$this->base->time."' WHERE plid='$plid' AND uid='$key'");
  228. }
  229. $result = $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_members(plid, uid, isnew, pmnum, lastupdate, lastdateline) VALUES('$plid', '$fromuid', '0', '1', '".$this->base->time."', '".$this->base->time."')", 'SILENT');
  230. if(!$result) {
  231. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_members SET isnew=0, pmnum=pmnum+1, lastupdate='".$this->base->time."', lastdateline='".$this->base->time."' WHERE plid='$plid' AND uid='$fromuid'");
  232. }
  233. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_lists SET lastmessage='$lastmessage' WHERE plid='$plid'");
  234. }
  235. }
  236. } else {
  237. $lastmessage = array('firstauthorid' => $fromuid, 'firstauthor' => $fromusername, 'firstsummary' => $lastsummary);
  238. $lastmessage = addslashes(serialize($lastmessage));
  239. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_lists(authorid, pmtype, subject, members, min_max, dateline, lastmessage) VALUES('$fromuid', '2', '$subject', '".(count($touids)+1)."', '', '".$this->base->time."', '$lastmessage')");
  240. $plid = $this->db->insert_id();
  241. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_indexes(plid) VALUES('$plid')");
  242. $pmid = $this->db->insert_id();
  243. $this->db->query("INSERT INTO ".UC_DBTABLEPRE.$this->getposttablename($plid)."(pmid, plid, authorid, message, dateline, delstatus) VALUES('$pmid', '$plid', '$fromuid', '$message', '".$this->base->time."', 0)");
  244. $pm_member_insertsql[] = "('$plid', '$fromuid', '0', '1', '".$this->base->time."', '".$this->base->time."')";
  245. foreach($touids as $key => $value) {
  246. $pm_member_insertsql[] = "('$plid', '$value', '1', '1', '0', '".$this->base->time."')";
  247. }
  248. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_members(plid, uid, isnew, pmnum, lastupdate, lastdateline) VALUES ".implode(',', $pm_member_insertsql));
  249. }
  250. $newpm = array();
  251. foreach($touids as $key => $value) {
  252. $newpm[] = "('$value')";
  253. }
  254. $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."newpm(uid) VALUES ".implode(',', $newpm));
  255. return $pmid;
  256. }
  257. function replypm($plid, $fromuid, $fromusername, $message) {
  258. if(!$plid || !$fromuid || !$fromusername || !$message) {
  259. return 0;
  260. }
  261. $threadpm = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."pm_lists WHERE plid='$plid'");
  262. if(empty($threadpm)) {
  263. return PMTHREADNONE_ERROR;
  264. }
  265. if($threadpm['pmtype'] == 1) {
  266. $users = explode('_', $threadpm['min_max']);
  267. if($users[0] == $fromuid) {
  268. $touid = $users[1];
  269. } elseif($users[1] == $fromuid) {
  270. $touid = $users[0];
  271. } else {
  272. return PMPRIVILEGENONE_ERROR;
  273. }
  274. $blackls = $this->get_blackls($fromuid, $touid);
  275. if(in_array('{ALL}', $blackls[$touid])) {
  276. return PMINBALCKLIST_ERROR;
  277. }
  278. $this->base->load('user');
  279. $blackls[$touid] = $_ENV['user']->name2id($blackls[$touid]);
  280. if(!(isset($blackls[$touid]) && !in_array($fromuid, $blackls[$touid]))) {
  281. return PMINBALCKLIST_ERROR;
  282. }
  283. }
  284. $memberuid = array();
  285. $query = $this->db->query("SELECT * FROM ".UC_DBTABLEPRE."pm_members WHERE plid='$plid'");
  286. while($member = $this->db->fetch_array($query)) {
  287. $memberuid[$member['uid']] = "('$member[uid]')";
  288. }
  289. if(!isset($memberuid[$fromuid])) {
  290. return PMPRIVILEGENONE_ERROR;
  291. }
  292. $_CACHE['badwords'] = $this->base->cache('badwords');
  293. if($_CACHE['badwords']['findpattern']) {
  294. $message = @preg_replace($_CACHE['badwords']['findpattern'], $_CACHE['badwords']['replace'], $message);
  295. }
  296. $lastsummary = $this->removecode(trim(stripslashes($message)), 150);
  297. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_indexes(plid) VALUES('$plid')");
  298. $pmid = $this->db->insert_id();
  299. $this->db->query("INSERT INTO ".UC_DBTABLEPRE.$this->getposttablename($plid)."(pmid, plid, authorid, message, dateline, delstatus) VALUES('$pmid', '$plid', '$fromuid', '$message', '".$this->base->time."', 0)");
  300. if($threadpm['pmtype'] == 1) {
  301. $lastmessage = array('lastauthorid' => $fromuid, 'lastauthor' => $fromusername, 'lastsummary' => $lastsummary);
  302. $lastmessage = addslashes(serialize($lastmessage));
  303. $result = $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_members(plid, uid, isnew, pmnum, lastupdate, lastdateline) VALUES('$plid', '$touid', '1', '1', '0', '".$this->base->time."')", 'SILENT');
  304. if(!$result) {
  305. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_members SET isnew=1, pmnum=pmnum+1, lastdateline='".$this->base->time."' WHERE plid='$plid' AND uid='$touid'");
  306. }
  307. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_members SET isnew=0, pmnum=pmnum+1, lastupdate='".$this->base->time."', lastdateline='".$this->base->time."' WHERE plid='$plid' AND uid='$fromuid'");
  308. } else {
  309. $lastmessage = unserialize($threadpm['lastmessage']);
  310. $lastmessage = array('firstauthorid' => $lastmessage['firstauthorid'], 'firstauthor' => $lastmessage['firstauthor'], 'firstsummary' => $lastmessage['firstsummary'], 'lastauthorid' => $fromuid, 'lastauthor' => $fromusername, 'lastsummary' => $lastsummary);
  311. $lastmessage = addslashes(serialize($lastmessage));
  312. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_members SET isnew=1, pmnum=pmnum+1, lastdateline='".$this->base->time."' WHERE plid='$plid'");
  313. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_members SET isnew=0, lastupdate='".$this->base->time."' WHERE plid='$plid' AND uid='$fromuid'");
  314. }
  315. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_lists SET lastmessage='$lastmessage' WHERE plid='$plid'");
  316. $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."newpm(uid) VALUES ".implode(',', $memberuid)."");
  317. return $pmid;
  318. }
  319. function appendchatpm($plid, $uid, $touid) {
  320. if(!$plid || !$uid || !$touid) {
  321. return 0;
  322. }
  323. $threadpm = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."pm_lists WHERE plid='$plid'");
  324. if(empty($threadpm)) {
  325. return PMTHREADNONE_ERROR;
  326. }
  327. if($threadpm['pmtype'] != 2) {
  328. return PMCHATTYPE_ERROR;
  329. }
  330. if($threadpm['authorid'] != $uid) {
  331. return PMPRIVILEGENONE_ERROR;
  332. }
  333. $blackls = $this->get_blackls($uid, $touid);
  334. if(in_array('{ALL}', $blackls[$touid])) {
  335. return PMINBALCKLIST_ERROR;
  336. }
  337. $this->base->load('user');
  338. $blackls[$touid] = $_ENV['user']->name2id($blackls[$touid]);
  339. if(!(isset($blackls[$touid]) && !in_array($uid, $blackls[$touid]))) {
  340. return PMINBALCKLIST_ERROR;
  341. }
  342. $pmnum = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE.$this->getposttablename($plid)." WHERE plid='$plid'");
  343. $this->db->query("INSERT INTO ".UC_DBTABLEPRE."pm_members(plid, uid, isnew, pmnum, lastupdate, lastdateline) VALUES('$plid', '$touid', '1', '$pmnum', '0', '0')", 'SILENT');
  344. $num = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."pm_members WHERE plid='$plid'");
  345. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_lists SET members='$num' WHERE plid='$plid'");
  346. return 1;
  347. }
  348. function kickchatpm($plid, $uid, $touid) {
  349. if(!$uid || !$touid || !$plid || $uid == $touid) {
  350. return 0;
  351. }
  352. $threadpm = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."pm_lists WHERE plid='$plid'");
  353. if($threadpm['pmtype'] != 2) {
  354. return PMCHATTYPE_ERROR;
  355. }
  356. if($threadpm['authorid'] != $uid) {
  357. return PMPRIVILEGENONE_ERROR;
  358. }
  359. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_members WHERE plid='$plid' AND uid='$touid'");
  360. $num = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."pm_members WHERE plid='$plid'");
  361. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_lists SET members='$num' WHERE plid='$plid'");
  362. return 1;
  363. }
  364. function quitchatpm($uid, $plids) {
  365. if(!$uid || !$plids) {
  366. return 0;
  367. }
  368. $list = array();
  369. $query = $this->db->query("SELECT * FROM ".UC_DBTABLEPRE."pm_members m LEFT JOIN ".UC_DBTABLEPRE."pm_lists t ON m.plid=t.plid WHERE m.plid IN (".$this->base->implode($plids).") AND m.uid='$uid'");
  370. while($threadpm = $this->db->fetch_array($query)) {
  371. if($threadpm['pmtype'] != 2) {
  372. return PMCHATTYPE_ERROR;
  373. }
  374. if($threadpm['authorid'] == $uid) {
  375. return PMPRIVILEGENONE_ERROR;
  376. }
  377. $list[] = $threadpm['plid'];
  378. }
  379. if($list) {
  380. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_members WHERE plid IN (".$this->base->implode($list).") AND uid='$uid'");
  381. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_lists SET members=members-1 WHERE plid IN (".$this->base->implode($list).")");
  382. }
  383. return 1;
  384. }
  385. function deletepmbypmid($uid, $pmid) {
  386. if(!$uid || !$pmid) {
  387. return 0;
  388. }
  389. $index = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."pm_indexes i LEFT JOIN ".UC_DBTABLEPRE."pm_lists t ON i.plid=t.plid WHERE i.pmid='$pmid'");
  390. if($index['pmtype'] != 1) {
  391. return PMUIDTYPE_ERROR;
  392. }
  393. $users = explode('_', $index['min_max']);
  394. if(!in_array($uid, $users)) {
  395. return PMPRIVILEGENONE_ERROR;
  396. }
  397. if($index['authorid'] != $uid) {
  398. $this->db->query("UPDATE ".UC_DBTABLEPRE.$this->getposttablename($index['plid'])." SET delstatus=2 WHERE pmid='$pmid' AND delstatus=0");
  399. $updatenum = $this->db->affected_rows();
  400. $this->db->query("DELETE FROM ".UC_DBTABLEPRE.$this->getposttablename($index['plid'])." WHERE pmid='$pmid' AND delstatus=1");
  401. $deletenum = $this->db->affected_rows();
  402. } else {
  403. $this->db->query("UPDATE ".UC_DBTABLEPRE.$this->getposttablename($index['plid'])." SET delstatus=1 WHERE pmid='$pmid' AND delstatus=0");
  404. $updatenum = $this->db->affected_rows();
  405. $this->db->query("DELETE FROM ".UC_DBTABLEPRE.$this->getposttablename($index['plid'])." WHERE pmid='$pmid' AND delstatus=2");
  406. $deletenum = $this->db->affected_rows();
  407. }
  408. if(!$this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE.$this->getposttablename($index['plid'])." WHERE plid='$index[plid]'")) {
  409. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_lists WHERE plid='$index[plid]'");
  410. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_members WHERE plid='$index[plid]'");
  411. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_indexes WHERE plid='$index[plid]'");
  412. } else {
  413. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_members SET pmnum=pmnum-".($updatenum + $deletenum)." WHERE plid='".$index['plid']."' AND uid='$uid'");
  414. }
  415. return 1;
  416. }
  417. function deletepmbypmids($uid, $pmids) {
  418. if($pmids) {
  419. foreach($pmids as $key => $pmid) {
  420. $this->deletepmbypmid($uid, $pmid);
  421. }
  422. }
  423. return 1;
  424. }
  425. function deletepmbyplid($uid, $plid, $isuser = 0) {
  426. if(!$uid || !$plid) {
  427. return 0;
  428. }
  429. if($isuser) {
  430. $relationship = $this->relationship($uid, $plid);
  431. $sql = "SELECT * FROM ".UC_DBTABLEPRE."pm_lists WHERE min_max='$relationship'";
  432. } else {
  433. $sql = "SELECT * FROM ".UC_DBTABLEPRE."pm_lists WHERE plid='$plid'";
  434. }
  435. $query = $this->db->query($sql);
  436. if($list = $this->db->fetch_array($query)) {
  437. if($list['pmtype'] == 1) {
  438. $user = explode('_', $list['min_max']);
  439. if(!in_array($uid, $user)) {
  440. return PMPRIVILEGENONE_ERROR;
  441. }
  442. } else {
  443. if($uid != $list['authorid']) {
  444. return PMPRIVILEGENONE_ERROR;
  445. }
  446. }
  447. } else {
  448. return PMTHREADNONE_ERROR;
  449. }
  450. if($list['pmtype'] == 1) {
  451. if($uid == $list['authorid']) {
  452. $this->db->query("DELETE FROM ".UC_DBTABLEPRE.$this->getposttablename($list['plid'])." WHERE plid='$list[plid]' AND delstatus=2");
  453. $this->db->query("UPDATE ".UC_DBTABLEPRE.$this->getposttablename($list['plid'])." SET delstatus=1 WHERE plid='$list[plid]' AND delstatus=0");
  454. } else {
  455. $this->db->query("DELETE FROM ".UC_DBTABLEPRE.$this->getposttablename($list['plid'])." WHERE plid='$list[plid]' AND delstatus=1");
  456. $this->db->query("UPDATE ".UC_DBTABLEPRE.$this->getposttablename($list['plid'])." SET delstatus=2 WHERE plid='$list[plid]' AND delstatus=0");
  457. }
  458. $count = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE.$this->getposttablename($list['plid'])." WHERE plid='$list[plid]'");
  459. if(!$count) {
  460. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_lists WHERE plid='$list[plid]'");
  461. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_members WHERE plid='$list[plid]'");
  462. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_indexes WHERE plid='$list[plid]'");
  463. } else {
  464. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_members WHERE plid='$list[plid]' AND uid='$uid'");
  465. }
  466. } else {
  467. $this->db->query("DELETE FROM ".UC_DBTABLEPRE.$this->getposttablename($list['plid'])." WHERE plid='$list[plid]'");
  468. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_lists WHERE plid='$list[plid]'");
  469. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_members WHERE plid='$list[plid]'");
  470. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."pm_indexes WHERE plid='$list[plid]'");
  471. }
  472. return 1;
  473. }
  474. function deletepmbyplids($uid, $plids, $isuser = 0) {
  475. if($plids) {
  476. foreach($plids as $key => $plid) {
  477. $this->deletepmbyplid($uid, $plid, $isuser);
  478. }
  479. }
  480. return 1;
  481. }
  482. function getprivatepmbyplid($uid, $plid, $starttime = 0, $endtime = 0, $start = 0, $ppp = 0) {
  483. if(!$uid || !$plid) {
  484. return 0;
  485. }
  486. if(!$this->isprivilege($plid, $uid)) {
  487. return 0;
  488. }
  489. $thread = $this->db->fetch_first("SELECT * FROM ".UC_DBTABLEPRE."pm_lists WHERE plid='$plid'");
  490. if($thread['pmtype'] != 1) {
  491. return 0;
  492. }
  493. $pms = $addsql = array();
  494. $addsql[] = "p.plid='$plid'";
  495. if($thread['authorid'] == $uid) {
  496. $addsql[] = 'p.delstatus IN (0,2)';
  497. } else {
  498. $addsql[] = 'p.delstatus IN (0,1)';
  499. }
  500. if($starttime) {
  501. $addsql[]= "p.dateline>'$starttime'";
  502. }
  503. if($endtime) {
  504. $addsql[] = "p.dateline<'$endtime'";
  505. }
  506. if($addsql) {
  507. $addsql = implode(' AND ', $addsql);
  508. } else {
  509. $addsql = '';
  510. }
  511. if($ppp) {
  512. $limitsql = 'LIMIT '.intval($start).', '.intval($ppp);
  513. } else {
  514. $limitsql = '';
  515. }
  516. $pms = $this->db->fetch_all("SELECT t.*, p.*, t.authorid as founderuid, t.dateline as founddateline FROM ".UC_DBTABLEPRE.$this->getposttablename($plid)." p LEFT JOIN ".UC_DBTABLEPRE."pm_lists t ON p.plid=t.plid WHERE $addsql ORDER BY p.dateline DESC $limitsql");
  517. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_members SET isnew=0 WHERE plid='$plid' AND uid='$uid' AND isnew=1");
  518. return array_reverse($pms);
  519. }
  520. function getchatpmbyplid($uid, $plid, $starttime = 0, $endtime = 0, $start = 0, $ppp = 0) {
  521. if(!$uid || !$plid) {
  522. return 0;
  523. }
  524. if(!$this->isprivilege($plid, $uid)) {
  525. return 0;
  526. }
  527. $pms = $addsql = array();
  528. $addsql[] = "p.plid='$plid'";
  529. if($starttime) {
  530. $addsql[]= "p.dateline>'$starttime'";
  531. }
  532. if($endtime) {
  533. $addsql[] = "p.dateline<'$endtime'";
  534. }
  535. if($addsql) {
  536. $addsql = implode(' AND ', $addsql);
  537. } else {
  538. $addsql = '';
  539. }
  540. if($ppp) {
  541. $limitsql = 'LIMIT '.intval($start).', '.intval($ppp);
  542. } else {
  543. $limitsql = '';
  544. }
  545. $query = $this->db->query("SELECT t.*, p.*, t.authorid as founderuid, t.dateline as founddateline FROM ".UC_DBTABLEPRE.$this->getposttablename($plid)." p LEFT JOIN ".UC_DBTABLEPRE."pm_lists t ON p.plid=t.plid WHERE $addsql ORDER BY p.dateline DESC $limitsql");
  546. while($pm = $this->db->fetch_array($query)) {
  547. if($pm['pmtype'] != 2) {
  548. return 0;
  549. }
  550. $pms[] = $pm;
  551. }
  552. $this->db->query("UPDATE ".UC_DBTABLEPRE."pm_members SET isnew=0 WHERE plid='$plid' AND uid='$uid' AND isnew=1");
  553. return array_reverse($pms);
  554. }
  555. function getpmlist($uid, $filter, $start, $ppp = 10) {
  556. if(!$uid) {
  557. return 0;
  558. }
  559. $members = $touidarr = $tousernamearr = array();
  560. if($filter == 'newpm') {
  561. $addsql = 'm.isnew=1 AND ';
  562. } else {
  563. $addsql = '';
  564. }
  565. $query = $this->db->query("SELECT * FROM ".UC_DBTABLEPRE."pm_members m LEFT JOIN ".UC_DBTABLEPRE."pm_lists t ON t.plid=m.plid WHERE $addsql m.uid='$uid' ORDER BY m.lastdateline DESC LIMIT $start, $ppp");
  566. while($member = $this->db->fetch_array($query)) {
  567. if($member['pmtype'] == 1) {
  568. $users = explode('_', $member['min_max']);
  569. $member['touid'] = $users[0] == $uid ? $users[1] : $users[0];
  570. } else {
  571. $member['touid'] = 0;
  572. }
  573. $touidarr[$member['touid']] = $member['touid'];
  574. $members[] = $member;
  575. }
  576. $this->db->query("DELETE FROM ".UC_DBTABLEPRE."newpm WHERE uid='$uid'");
  577. $array = array();
  578. if($members) {
  579. $today = $this->base->time - $this->base->time % 86400;
  580. $this->base->load('user');
  581. $tousernamearr = $_ENV['user']->id2name($touidarr);
  582. foreach($members as $key => $data) {
  583. $daterange = 5;
  584. $data['founddateline'] = $data['dateline'];
  585. $data['dateline'] = $data['lastdateline'];
  586. $data['pmid'] = $data['plid'];
  587. $lastmessage = unserialize($data['lastmessage']);
  588. if($lastmessage['firstauthorid']) {
  589. $data['firstauthorid'] = $lastmessage['firstauthorid'];
  590. $data['firstauthor'] = $lastmessage['firstauthor'];
  591. $data['firstsummary'] = $lastmessage['firstsummary'];
  592. }
  593. if($lastmessage['lastauthorid']) {
  594. $data['lastauthorid'] = $lastmessage['lastauthorid'];
  595. $data['lastauthor'] = $lastmessage['lastauthor'];
  596. $data['lastsummary'] = $lastmessage['lastsummary'];
  597. }
  598. $data['msgfromid'] = $lastmessage['lastauthorid'];
  599. $data['msgfrom'] = $lastmessage['lastauthor'];
  600. $data['message'] = $lastmessage['lastsummary'];
  601. $data['new'] = $data['isnew'];
  602. $data['msgtoid'] = $data['touid'];
  603. if($data['lastdateline'] >= $today) {
  604. $daterange = 1;
  605. } elseif($data['lastdateline'] >= $today - 86400) {
  606. $daterange = 2;
  607. } elseif($data['lastdateline'] >= $today - 172800) {
  608. $daterange = 3;
  609. } elseif($data['lastdateline'] >= $today - 604800) {
  610. $daterange = 4;
  611. }
  612. $data['daterange'] = $daterange;
  613. $data['tousername'] = $tousernamearr[$data['touid']];
  614. unset($data['min_max']);
  615. $array[] = $data;
  616. }
  617. }
  618. return $array;
  619. }
  620. function getplidbypmid($pmid) {
  621. if(!$pmid) {
  622. return false;
  623. }
  624. return $this->db->result_first("SELECT plid FROM ".UC_DBTABLEPRE."pm_indexes WHERE pmid='$pmid'");
  625. }
  626. function getplidbytouid($uid, $touid) {
  627. if(!$uid || !$touid) {
  628. return 0;
  629. }
  630. return $this->db->result_first("SELECT plid FROM ".UC_DBTABLEPRE."pm_lists WHERE min_max='".$this->relationship($uid, $touid)."'");
  631. }
  632. function getuidbyplid($plid) {
  633. if(!$plid) {
  634. return array();
  635. }
  636. $uidarr = array();
  637. $query = $this->db->query("SELECT uid FROM ".UC_DBTABLEPRE."pm_members WHERE plid='$plid'");
  638. while($uid = $this->db->fetch_array($query)) {
  639. $uidarr[$uid['uid']] = $uid['uid'];
  640. }
  641. return $uidarr;
  642. }
  643. function chatpmmemberlist($uid, $plid) {
  644. if(!$uid || !$plid) {
  645. return 0;
  646. }
  647. $uidarr = $this->getuidbyplid($plid);
  648. if(empty($uidarr)) {
  649. return 0;
  650. }
  651. if(!isset($uidarr[$uid])) {
  652. return 0;
  653. }
  654. $authorid = $this->db->result_first("SELECT authorid FROM ".UC_DBTABLEPRE."pm_lists WHERE plid='$plid'");
  655. return array('author' => $authorid, 'member' => $uidarr);
  656. }
  657. function relationship($fromuid, $touid) {
  658. if($fromuid < $touid) {
  659. return $fromuid.'_'.$touid;
  660. } elseif($fromuid > $touid) {
  661. return $touid.'_'.$fromuid;
  662. } else {
  663. return '';
  664. }
  665. }
  666. function getposttablename($plid) {
  667. $id = substr((string)$plid, -1, 1);
  668. return 'pm_messages_'.intval($id);
  669. }
  670. function get_blackls($uid, $uids = array()) {
  671. if(!$uids) {
  672. $blackls = $this->db->result_first("SELECT blacklist FROM ".UC_DBTABLEPRE."memberfields WHERE uid='$uid'");
  673. } else {
  674. $uids = $this->base->implode($uids);
  675. $blackls = array();
  676. $query = $this->db->query("SELECT uid, blacklist FROM ".UC_DBTABLEPRE."memberfields WHERE uid IN ($uids)");
  677. while($data = $this->db->fetch_array($query)) {
  678. $blackls[$data['uid']] = explode(',', $data['blacklist']);
  679. }
  680. }
  681. return $blackls;
  682. }
  683. function set_blackls($uid, $blackls) {
  684. $this->db->query("UPDATE ".UC_DBTABLEPRE."memberfields SET blacklist='$blackls' WHERE uid='$uid'");
  685. return $this->db->affected_rows();
  686. }
  687. function update_blackls($uid, $username, $action = 1) {
  688. $username = !is_array($username) ? array($username) : $username;
  689. if($action == 1) {
  690. if(!in_array('{ALL}', $username)) {
  691. $usernames = $this->base->implode($username);
  692. $query = $this->db->query("SELECT username FROM ".UC_DBTABLEPRE."members WHERE username IN ($usernames)");
  693. $usernames = array();
  694. while($data = $this->db->fetch_array($query)) {
  695. $usernames[addslashes($data['username'])] = addslashes($data['username']);
  696. }
  697. if(!$usernames) {
  698. return 0;
  699. }
  700. $blackls = addslashes($this->db->result_first("SELECT blacklist FROM ".UC_DBTABLEPRE."memberfields WHERE uid='$uid'"));
  701. if($blackls) {
  702. $list = explode(',', $blackls);
  703. foreach($list as $k => $v) {
  704. if(in_array($v, $usernames)) {
  705. unset($usernames[$v]);
  706. }
  707. }
  708. }
  709. if(!$usernames) {
  710. return 1;
  711. }
  712. $listnew = implode(',', $usernames);
  713. $blackls .= $blackls !== '' ? ','.$listnew : $listnew;
  714. } else {
  715. $blackls = addslashes($this->db->result_first("SELECT blacklist FROM ".UC_DBTABLEPRE."memberfields WHERE uid='$uid'"));
  716. $blackls .= ',{ALL}';
  717. }
  718. } else {
  719. $blackls = addslashes($this->db->result_first("SELECT blacklist FROM ".UC_DBTABLEPRE."memberfields WHERE uid='$uid'"));
  720. $list = $blackls = explode(',', $blackls);
  721. foreach($list as $k => $v) {
  722. if(in_array($v, $username)) {
  723. unset($blackls[$k]);
  724. }
  725. }
  726. $blackls = implode(',', $blackls);
  727. }
  728. $this->db->query("UPDATE ".UC_DBTABLEPRE."memberfields SET blacklist='$blackls' WHERE uid='$uid'");
  729. return 1;
  730. }
  731. function removecode($str, $length) {
  732. static $uccode = null;
  733. if($uccode === null) {
  734. require_once UC_ROOT.'lib/uccode.class.php';
  735. $uccode = new uccode();
  736. }
  737. $str = $uccode->complie($str);
  738. return trim($this->base->cutstr(strip_tags($str), $length));
  739. }
  740. function ispminterval($uid, $interval = 0) {
  741. if(!$uid) {
  742. return 0;
  743. }
  744. $interval = intval($interval);
  745. if(!$interval) {
  746. return 1;
  747. }
  748. $lastupdate = $this->db->result_first("SELECT lastupdate FROM ".UC_DBTABLEPRE."pm_members WHERE uid='$uid' ORDER BY lastupdate DESC LIMIT 1");
  749. if(($this->base->time - $lastupdate) > $interval) {
  750. return 1;
  751. } else {
  752. return 0;
  753. }
  754. }
  755. function isprivatepmthreadlimit($uid, $maxnum = 0) {
  756. if(!$uid) {
  757. return 0;
  758. }
  759. $maxnum = intval($maxnum);
  760. if(!$maxnum) {
  761. return 1;
  762. }
  763. $num = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."pm_members m LEFT JOIN ".UC_DBTABLEPRE."pm_lists t ON m.plid=t.plid WHERE uid='$uid' AND lastupdate>'".($this->base->time-86400)."' AND t.pmtype=1");
  764. if($maxnum - $num < 0) {
  765. return 0;
  766. } else {
  767. return 1;
  768. }
  769. }
  770. function ischatpmthreadlimit($uid, $maxnum = 0) {
  771. if(!$uid) {
  772. return 0;
  773. }
  774. $maxnum = intval($maxnum);
  775. if(!$maxnum) {
  776. return 1;
  777. }
  778. $num = $this->db->result_first("SELECT COUNT(*) FROM ".UC_DBTABLEPRE."pm_lists WHERE authorid='$uid' AND dateline>'".($this->base->time-86400)."'");
  779. if($maxnum - $num < 0) {
  780. return 0;
  781. } else {
  782. return 1;
  783. }
  784. }
  785. }
  786. ?>