PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/class/bbs/TopicUtil.class.php

https://github.com/hylinux/ltebbs
PHP | 517 lines | 281 code | 90 blank | 146 comment | 56 complexity | 49589c82c76ae48fe781ce71e8c4acf8 MD5 | raw file
  1. <?php
  2. //vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldcolumn=1 foldmethod=marker:
  3. /**
  4. * 项目: 5anet(BBS)
  5. * 文件: class/bbs/TopicUtil.class.php
  6. *
  7. * 此类是用于操作主题和回复的工具类
  8. *
  9. * PHP Version 5
  10. *
  11. * @package: class.bbs
  12. * @author: Mike.G 黄叶 <hylinux@gmail.com>
  13. * @license: http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  14. * @copyright: www.5anet.com
  15. * @version: $Id: TopicUtil.class.php,v 1.1.1.1 2006-08-28 13:09:20 ghw Exp $
  16. * @Date: $Date: 2006-08-28 13:09:20 $
  17. */
  18. include_once CLASS_PATH.'user/UserUtil.class.php';
  19. include_once FUNCTION_PATH.'ConvertString.fun.php';
  20. include_once FUNCTION_PATH.'set_locale_time.fun.php';
  21. //include the language file
  22. if ( file_exists(LANG_PATH.SYSTEM_LANG.'/TopicUtil.lang.php') ) {
  23. include_once LANG_PATH.SYSTEM_LANG.'/TopicUtil.lang.php';
  24. }
  25. class TopicUtil {
  26. /**
  27. * 判断有无新回复
  28. * @param: &$db,
  29. * @param: $id
  30. * @return: boolan
  31. * @access: public
  32. * @static
  33. */
  34. public static function haveNewReply(&$db, $id, $user_id) {/*{{{*/
  35. //求用户最后访问的时间
  36. //$user_name = UserUtil::getUserNameById($db, $user_id);
  37. $last_time = UserUtil::getUserLastLogoutTime($db, $user_id);
  38. $sql = 'select last_access_date from bbs_subject where id=?';
  39. $sth = $db->Prepare($sql);
  40. $res = $db->Execute($sth, array($id));
  41. $rows = $res->FetchRow();
  42. $temp_time = $rows['last_access_date'];
  43. if ( $temp_time >= $last_time ) {
  44. return TRUE;
  45. } else {
  46. return FALSE;
  47. }
  48. }/*}}}*/
  49. /**
  50. * 判断帖子是否存在
  51. * @param: &$db,
  52. * @param: $id
  53. * @return: boolean
  54. * @access: public
  55. * @static
  56. */
  57. public static function isExists(&$db, $id) {/*{{{*/
  58. $sql = 'select count(*) as num from bbs_subject where id=?';
  59. $stmt = $db->Prepare($sql);
  60. $res = $db->Execute($stmt, array($id));
  61. $rows = $res->FetchRow();
  62. $num = $rows['num'];
  63. if ( $num ) {
  64. return TRUE;
  65. } else {
  66. return FALSE;
  67. }
  68. }/*}}}*/
  69. /**
  70. * 返回帖子的版块id
  71. * @param: &$db
  72. * @param: $id
  73. * $return: $layout_id integer
  74. * @access: public
  75. * @static
  76. */
  77. public static function getLayoutId(&$db, $id) {/*{{{*/
  78. $sql = 'select layout_id from bbs_subject where id=?';
  79. $stmt = $db->Prepare($sql);
  80. $res = $db->Execute($stmt, array($id));
  81. $rows = $res->FetchRow();
  82. return $rows['layout_id'];
  83. }/*}}}*/
  84. /**
  85. * 返回帖子的状态
  86. * @param: &$db,
  87. * @param: $id
  88. * @return: $topic_status integer
  89. * @access: public
  90. * @static
  91. */
  92. public static function getTopicStatus(&$db, $id) {/*{{{*/
  93. $sql = 'select subject_status from bbs_subject where id=?';
  94. $stmt = $db->Prepare($sql);
  95. $res = $db->Execute($stmt, array($id));
  96. $rows = $res->FetchRow();
  97. /**
  98. * 为0,则开放
  99. * 为1,则需要验证
  100. * 为2,则关闭
  101. */
  102. return $rows['subject_status'];
  103. }/*}}}*/
  104. /**
  105. * 求主题加上回复一共有的记录数
  106. * @param: &$db
  107. * @param: $id
  108. * @return: $recoreds integer
  109. * @access: public
  110. * @static
  111. */
  112. public static function getTopicNumber(&$db, $id) {/*{{{*/
  113. $sql = 'select count(*) as num from bbs_reply where subject_id=?';
  114. $sth = $db->Prepare($sql);
  115. $res = $db->Execute($sth, array($id));
  116. $rows = $res->FetchRow();
  117. return $rows['num']+1;
  118. }/*}}}*/
  119. /**
  120. * 求主题加上回复一共有的页数
  121. * @param: &$db
  122. * @param: $id
  123. * @return: $total_page
  124. * @access: public
  125. * @static
  126. */
  127. public static function getTotalPage(&$db, $id, $pre_page = 15) {/*{{{*/
  128. $total_number = self::getTopicNumber($db, $id);
  129. return ceil($total_number / $pre_page);
  130. }/*}}}*/
  131. /**
  132. * 帖子的标题
  133. * @param: &$db
  134. * @param: $id
  135. * @return: $title
  136. * @access: public
  137. * @static
  138. */
  139. public static function getTitle(&$db, $id) {/*{{{*/
  140. $sql = 'select title from bbs_subject where id=?';
  141. $sth = $db->Prepare($sql);
  142. $res = $db->Execute($sth, array($id));
  143. $rows = $res->FetchRow();
  144. return $rows['title'];
  145. }/*}}}*/
  146. /**
  147. * 更新帖子的浏览次数
  148. * @param; &$db
  149. * @param: $id
  150. * @return NULL
  151. * @access: public
  152. * @static
  153. */
  154. public static function updateViewNumber(&$db, $id) {/*{{{*/
  155. $sql = 'update bbs_subject set click_number=click_number+1 where id=?';
  156. $sth = $db->Prepare($sql);
  157. $db->Execute($sth, array($id));
  158. return 1;
  159. }/*}}}*/
  160. /**
  161. * 取得帖子的信息
  162. * @param: &$db
  163. * @param: $id
  164. * @param: $pre_page
  165. * @param: $offset_page
  166. * @return: $topic_array
  167. * @access: public
  168. * @static
  169. */
  170. public static function getTopicInfo(&$db, $id, $pre_page=10, $offset_page = 0 ) {/*{{{*/
  171. $topic_array = array();
  172. $topic_status = self::getTopicStatus($db, $id);
  173. //如果显示第一页,则必须给出主题
  174. if ( $offset_page == 0 ) {
  175. $sql = 'select title, express, author, content, post_date, is_edit, '.
  176. ' edit_user, edit_time, subject_status, is_best, is_top from bbs_subject where id=?';
  177. $sth = $db->Prepare($sql);
  178. $res = $db->Execute($sth, array($id));
  179. $rows = $res->FetchRow();
  180. $posttime = set_locale_time($rows['post_date']);
  181. $user_name = $rows['author'];
  182. $user_id = UserUtil::getUserId($db, $user_name);
  183. $user_header = UserUtil::getUserHeader($db, $user_id);
  184. $user_info = UserUtil::getUserInfo($db, $user_id);
  185. $register_date = $user_info['register_date'];
  186. $user_level = $user_info['user_level'];
  187. $user_address = $user_info['user_hometown'];
  188. $user_topic_number = $user_info['user_topic'];
  189. $user_sign = ConvertString($user_info['user_sign'], ROOT_URL, IMAGE_URL.'express/');
  190. $is_edit = 0;
  191. $edit_user = '';
  192. $edit_time = '';
  193. if ( $rows['is_edit'] ) {
  194. $is_edit = 1;
  195. $edit_user = $rows['edit_user'];
  196. $edit_time = $rows['edit_time'];
  197. }
  198. $user_online = UserUtil::isOnline($db, $user_id);
  199. $user_can_be_edit = 0;
  200. if ( !$_SESSION['user']['name'] ) {
  201. $user_can_be_edit = 0;
  202. } else {
  203. if ( strtolower($_SESSION['user']['name']) == strtolower($user_name) ) {
  204. $user_can_be_edit = 1;
  205. } else if ( strtolower($_SESSION['user']['name']) != strtolower($user_name) ) {
  206. //判断用户是否是这个版块的版主。
  207. $dep = UserUtil::getUserDep($db, $_SESSION['user']['name']);
  208. if ( $dep == 1 || $dep == 2 ) {
  209. $user_can_be_edit = 1;
  210. } else if ( $dep == 3 ) {
  211. $temp_layout_id = self::getLayoutId($db, $id);
  212. $user_can_be_edit = UserUtil::isThisLayoutAdmin($db, $id, $temp_layout_id, $_SESSION['user']['name']);
  213. }
  214. }
  215. }
  216. //判断是否有附件
  217. //如果有附件,则使用代码替换
  218. $content = '';
  219. if ( $topic_status == 2 ) {
  220. $content = TU_TOPIC_WAS_LOCKED;
  221. } else {
  222. $content = $rows['content'].self::haveAttach($db, $id);
  223. if ( $is_edit ) {
  224. $attach_string = TU_SUB_TITLE.$edit_user.TU_FROM.$edit_time.TU_EDIT;
  225. $content .= "\n\n".$attach_string;
  226. };
  227. }
  228. $title = $rows['title'];
  229. $title = htmlspecialchars($title);
  230. if ( $rows['is_best'] ) {
  231. $title = "<font color=red>[".BEST_LABEL."]</font>".$title;
  232. }
  233. if ( $rows['is_top'] ) {
  234. $title = "<font color=red>[".TOP_LABEL."]</font>".$title;
  235. }
  236. $topic_array[] = array(
  237. 'id' => $id,
  238. 'posttime' => $posttime,
  239. 'sort_number' => 1,
  240. 'user_name' => $user_name,
  241. 'user_id' => $user_id,
  242. 'user_header' => $user_header,
  243. 'user_sign'=> $user_sign,
  244. 'register_date' => $register_date,
  245. 'user_level' => $user_level,
  246. 'user_address' => $user_address,
  247. 'user_topic_number' => $user_topic_number,
  248. 'title' => $title,
  249. 'content' => ConvertString($content, ROOT_URL, IMAGE_URL.'express/'),
  250. 'online' => $user_online,
  251. 'can_be_edit' => $user_can_be_edit,
  252. 'is_topic' => 1,
  253. 'express' => $rows['express']
  254. );
  255. $pre_page = $pre_page - 1;
  256. } else if ( $offset_page >= 1 ) {
  257. $offset_page = $offset_page - 1;
  258. }
  259. //再查回复的帖子
  260. $sql = 'select id, title, express,author, content, post_date, is_edit, edit_user, '.
  261. ' edit_time, reply_status from bbs_reply where subject_id=? '.
  262. ' order by id asc';
  263. $res = $db->SelectLimit($sql, $pre_page, $offset_page, array($id));
  264. while ( $rows = $res->FetchRow() ) {
  265. $posttime = set_locale_time($rows['post_date']);
  266. $sort_number = $sort_begin;
  267. $user_name = $rows['author'];
  268. $user_id = UserUtil::getUserId($db, $user_name);
  269. $user_header = UserUtil::getUserHeader($db, $user_id);
  270. $user_info = UserUtil::getUserInfo($db, $user_id);
  271. $register_date = $user_info['register_date'];
  272. $user_level = $user_info['user_level'];
  273. $user_address = $user_info['user_hometown'];
  274. $user_topic_number = $user_info['user_topic'];
  275. $user_sign = ConvertString($user_info['user_sign'], ROOT_URL, IMAGE_URL.'express/');
  276. $is_edit = 0;
  277. $edit_user = '';
  278. $edit_time = '';
  279. if ( $rows['is_edit'] ) {
  280. $is_edit = 1;
  281. $edit_user = $rows['edit_user'];
  282. $edit_time = $rows['edit_time'];
  283. }
  284. $user_online = UserUtil::isOnline($db, $user_id);
  285. $user_can_be_edit = 0;
  286. if ( !$_SESSION['user']['name'] ) {
  287. $user_can_be_edit = 0;
  288. } else {
  289. if ( strtolower($_SESSION['user']['name']) == strtolower($user_name) ) {
  290. $user_can_be_edit = 1;
  291. } else if ( strtolower($_SESSION['user']['name']) != strtolower($user_name) ) {
  292. //判断用户是否是这个版块的版主。
  293. $dep = UserUtil::getUserDep($db, $_SESSION['user']['name']);
  294. if ( $dep == 1 || $dep == 2 ) {
  295. $user_can_be_edit = 1;
  296. } else if ( $dep == 3 ) {
  297. $temp_layout_id = self::getLayoutId($db, $id);
  298. $user_can_be_edit = UserUtil::isThisLayoutAdmin($db, $id, $temp_layout_id, $_SESSION['user']['name']);
  299. }
  300. }
  301. }
  302. $sort_number = self::getSortNumber($db, $id, $rows['id']);
  303. $content = '';
  304. $had_closed = 0;
  305. if ( $rows['reply_status'] ) {
  306. $had_closed = 1;
  307. }
  308. if ( $rows['reply_status'] || $topic_status == 2 ) {
  309. //如果回帖状态被设定,则表示改帖被关闭或者屏蔽
  310. $content = TU_TOPIC_WAS_LOCKED;
  311. } else {
  312. $content = $rows['content'].self::haveReplyAttach($db, $rows['id']);
  313. if ( $is_edit ) {
  314. $attach_string = TU_SUB_TITLE.$edit_user.TU_FROM.$edit_time.TU_EDIT;
  315. $content .= "\n\n".$attach_string;
  316. };
  317. }
  318. $title = htmlspecialchars($rows['title']);
  319. $topic_array[] = array(
  320. 'id' => $rows['id'],
  321. 'posttime' => $posttime,
  322. 'sort_number' => $sort_number,
  323. 'user_name' => $user_name,
  324. 'user_id' => $user_id,
  325. 'user_header' => $user_header,
  326. 'user_sign'=>$user_sign,
  327. 'register_date' => $register_date,
  328. 'user_level' => $user_level,
  329. 'user_address' => $user_address,
  330. 'user_topic_number' => $user_topic_number,
  331. 'title' => $title,
  332. 'content' => ConvertString($content, ROOT_URL, IMAGE_URL.'express/'),
  333. 'online' => $user_online,
  334. 'can_be_edit' => $user_can_be_edit,
  335. 'is_topic' => 0,
  336. 'express' => $rows['express'],
  337. 'had_closed' => $had_closed,
  338. );
  339. }
  340. return $topic_array;
  341. }/*}}}*/
  342. /**
  343. * 求回复在所有的回复中排在第几个位置
  344. * @param: &$db
  345. * @param: $id 主题的id
  346. * @param: $reply_id 帖子的id
  347. * @return: $num
  348. * @access: public
  349. * @static
  350. */
  351. public static function getSortNumber(&$db, $id, $reply_id) {/*{{{*/
  352. $sql = 'select count(*) as num from bbs_reply where subject_id=? '.
  353. ' and id<=? ';
  354. $sth = $db->Prepare($sql);
  355. $res = $db->Execute($sth, array($id, $reply_id));
  356. $rows = $res->FetchRow();
  357. return $rows['num'] + 1;
  358. }/*}}}*/
  359. /**
  360. * 判断用户是否有附件
  361. * @param: &$db
  362. * @param: $id
  363. * @return: $add_string
  364. * @access: public
  365. * @static
  366. */
  367. public static function haveAttach(&$db, $id) {/*{{{*/
  368. $sql = 'select subject_id, file_type from bbs_subject_attach where subject_id=?';
  369. $res = $db->SelectLimit($sql, 1, 0, array($id));
  370. $rows = $res->FetchRow();
  371. if ( $rows['subject_id'] ) {
  372. return "\n[img]upload/attach/".$rows['subject_id'].$rows['file_type'].'[/img]';
  373. }
  374. }/*}}}*/
  375. /**
  376. * 判断用户是否有附件
  377. * @param: &$db
  378. * @param: $id
  379. * @return: $add_string
  380. * @access: public
  381. * @static
  382. */
  383. public static function haveReplyAttach(&$db, $id) {/*{{{*/
  384. $sql = 'select reply_id, file_type from bbs_reply_attach where reply_id=?';
  385. $res = $db->SelectLimit($sql, 1, 0, array($id));
  386. $rows = $res->FetchRow();
  387. if ( $rows['reply_id'] ) {
  388. return "\n[img]upload/attach/reply/".$rows['reply_id'].$rows['file_type'].'[/img]';
  389. }
  390. }/*}}}*/
  391. /**
  392. * 判断用户回复是否存在
  393. * @param: &$db,
  394. * @param: $id
  395. * @return: boolean
  396. * @access: public
  397. * @static
  398. */
  399. public static function replyIsExists(&$db, $id) {/*{{{*/
  400. $sql = 'select count(*) as num from bbs_reply where id=?';
  401. $stmt = $db->Prepare($sql);
  402. $res = $db->Execute($stmt, array($id));
  403. $rows = $res->FetchRow();
  404. $num = $rows['num'];
  405. if ( $num ) {
  406. return TRUE;
  407. } else {
  408. return FALSE;
  409. }
  410. }/*}}}*/
  411. /**
  412. * 从用户回复中返回的版块id
  413. * @param: &$db
  414. * @param: $id
  415. * $return: $layout_id integer
  416. * @access: public
  417. * @static
  418. */
  419. public static function getLayoutFromReplyId(&$db, $id) {/*{{{*/
  420. $sql = 'select layout_id from bbs_reply where id=?';
  421. $stmt = $db->Prepare($sql);
  422. $res = $db->Execute($stmt, array($id));
  423. $rows = $res->FetchRow();
  424. return $rows['layout_id'];
  425. }/*}}}*/
  426. }
  427. ?>