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

/src/include/model/comment_model.php

http://emlog.googlecode.com/
PHP | 314 lines | 272 code | 19 blank | 23 comment | 56 complexity | 99e2360f0bde3b3c7e9aaecc1595b3be MD5 | raw file
  1. <?php
  2. /**
  3. * ????
  4. * @copyright (c) Emlog All Rights Reserved
  5. * $Id: comment_model.php 2030 2011-09-13 15:49:40Z qiyuuu@gmail.com $
  6. */
  7. class Comment_Model {
  8. private $db;
  9. function __construct()
  10. {
  11. $this->db = MySql::getInstance();
  12. }
  13. /**
  14. * ????
  15. *
  16. * @param int $spot 0??? 1??? 2: ??
  17. * @param int $blogId
  18. * @param string $hide
  19. * @param int $page
  20. * @return array
  21. */
  22. function getComments($spot = 0, $blogId = null, $hide = null, $page = null)
  23. {
  24. $andQuery = '1=1';
  25. $andQuery .= $blogId ? " and a.gid=$blogId" : '';
  26. $andQuery .= $hide ? " and a.hide='$hide'" : '';
  27. $condition = '';
  28. if($page && $spot == 1)
  29. {
  30. $perpage_num = Option::get('admin_perpage_num');
  31. $startId = ($page - 1) * $perpage_num;
  32. $condition = "LIMIT $startId, ".$perpage_num;
  33. }
  34. if($spot == 0 || $spot == 2)
  35. {
  36. $sql = "SELECT * FROM ".DB_PREFIX."comment as a where $andQuery ORDER BY a.date ASC $condition";
  37. }else{
  38. $andQuery .= ROLE != 'admin' ? ' and b.author='.UID : '';
  39. $sql = "SELECT *,a.hide,a.date FROM ".DB_PREFIX."comment as a, ".DB_PREFIX."blog as b where $andQuery and a.gid=b.gid ORDER BY a.date DESC $condition";
  40. }
  41. $ret = $this->db->query($sql);
  42. $comments = array();
  43. while($row = $this->db->fetch_array($ret))
  44. {
  45. $row['poster'] = htmlspecialchars($row['poster']);
  46. $row['mail'] = htmlspecialchars($row['mail']);
  47. $row['url'] = htmlspecialchars($row['url']);
  48. $row['content'] = htmlClean($row['comment']);
  49. $row['date'] = smartDate($row['date']);
  50. $row['children'] = array();
  51. if($spot == 0) $row['level'] = isset($comments[$row['pid']]) ? $comments[$row['pid']]['level'] + 1 : 0;
  52. //$row['hide'];
  53. //$row['title'];
  54. //$row['gid'];
  55. $comments[$row['cid']] = $row;
  56. }
  57. if($spot == 0) {
  58. $commentStacks = array();
  59. $commentPageUrl = '';
  60. foreach($comments as $cid => $comment) {
  61. $pid = $comment['pid'];
  62. if($pid == 0) $commentStacks[] = $cid;
  63. if($pid != 0 && isset($comments[$pid])) {
  64. if($comments[$cid]['level'] > 4) {
  65. $comments[$cid]['pid'] = $pid = $comments[$pid]['pid'];
  66. }
  67. $comments[$pid]['children'][] = $cid;
  68. }
  69. }
  70. if(Option::get('comment_order') == 'newer') {
  71. $comments = array_reverse($comments, true);
  72. $commentStacks = array_reverse($commentStacks);
  73. }
  74. if(Option::get('comment_paging') == 'y') {
  75. $pageurl = Url::log($blogId);
  76. if(Option::get('isurlrewrite') == 0 && strpos($pageurl,'=') !== false) {
  77. $pageurl .= '&comment-page=';
  78. } else {
  79. $pageurl .= '/comment-page-';
  80. }
  81. $commentPageUrl = pagination(count($commentStacks), Option::get('comment_pnum'), $page, $pageurl, '#comments');
  82. $commentStacks = array_slice($commentStacks, ($page - 1) * Option::get('comment_pnum'), Option::get('comment_pnum'));
  83. }
  84. $comments = compact('comments','commentStacks','commentPageUrl');
  85. } elseif($spot == 2) {
  86. $commentStacks = array_keys($comments);
  87. $commentPageUrl = '';
  88. if(Option::get('comment_order') == 'newer') {
  89. $comments = array_reverse($comments, true);
  90. $commentStacks = array_reverse($commentStacks);
  91. }
  92. if(Option::get('comment_paging') == 'y') {
  93. $pageurl = './?post=' . $blogId . '&comment-page=';
  94. $commentPageUrl = pagination(count($commentStacks), Option::get('comment_pnum'), $page, $pageurl);
  95. $commentStacks = array_slice($commentStacks, ($page - 1) * Option::get('comment_pnum'), Option::get('comment_pnum'));
  96. }
  97. $comments = compact('comments','commentStacks','commentPageUrl');
  98. }
  99. return $comments;
  100. }
  101. function getOneComment($commentId)
  102. {
  103. $timezone = Option::get('timezone');
  104. $sql = "select * from ".DB_PREFIX."comment where cid=$commentId";
  105. $res = $this->db->query($sql);
  106. $commentArray = $this->db->fetch_array($res);
  107. $commentArray['comment'] = htmlClean(trim($commentArray['comment']));
  108. $commentArray['poster'] = trim($commentArray['poster']);
  109. $commentArray['date'] = gmdate("Y-m-d H:i",$commentArray['date'] + $timezone * 3600);
  110. return $commentArray;
  111. }
  112. function getCommentNum($blogId = null, $hide = null)
  113. {
  114. $comNum = '';
  115. $andQuery = '1=1';
  116. $andQuery .= $blogId ? " and a.gid=$blogId" : '';
  117. $andQuery .= $hide ? " and a.hide='$hide'" : '';
  118. if (ROLE == 'admin')
  119. {
  120. $sql = "SELECT count(*) FROM ".DB_PREFIX."comment as a where $andQuery";
  121. }else {
  122. $sql = "SELECT count(*) FROM ".DB_PREFIX."comment as a, ".DB_PREFIX."blog as b where $andQuery and a.gid=b.gid and b.author=".UID;
  123. }
  124. $res = $this->db->once_fetch_array($sql);
  125. $comNum = $res['count(*)'];
  126. return $comNum;
  127. }
  128. function delComment($commentId)
  129. {
  130. $row = $this->db->once_fetch_array("SELECT gid FROM ".DB_PREFIX."comment WHERE cid=$commentId");
  131. $blogId = intval($row['gid']);
  132. $commentIds = array($commentId);
  133. /* ?????ID */
  134. $query = $this->db->query("SELECT cid,pid FROM ".DB_PREFIX."comment WHERE gid=$blogId AND cid>$commentId ");
  135. while($row = $this->db->fetch_array($query)) {
  136. if(in_array($row['pid'],$commentIds)) {
  137. $commentIds[] = $row['cid'];
  138. }
  139. }
  140. $commentIds = implode(',',$commentIds);
  141. $this->db->query("DELETE FROM ".DB_PREFIX."comment WHERE cid IN ($commentIds)");
  142. $this->updateCommentNum($blogId);
  143. }
  144. function hideComment($commentId)
  145. {
  146. $row = $this->db->once_fetch_array("SELECT gid FROM ".DB_PREFIX."comment WHERE cid=$commentId");
  147. $blogId = intval($row['gid']);
  148. $commentIds = array($commentId);
  149. /* ?????ID */
  150. $query = $this->db->query("SELECT cid,pid FROM ".DB_PREFIX."comment WHERE gid=$blogId AND cid>$commentId ");
  151. while($row = $this->db->fetch_array($query)) {
  152. if(in_array($row['pid'],$commentIds)) {
  153. $commentIds[] = $row['cid'];
  154. }
  155. }
  156. $commentIds = implode(',',$commentIds);
  157. $this->db->query("UPDATE ".DB_PREFIX."comment SET hide='y' WHERE cid IN ($commentIds)");
  158. $this->updateCommentNum($blogId);
  159. }
  160. function showComment($commentId)
  161. {
  162. $row = $this->db->once_fetch_array("SELECT gid,pid FROM ".DB_PREFIX."comment WHERE cid=$commentId");
  163. $blogId = intval($row['gid']);
  164. $commentIds = array($commentId);
  165. /* ?????ID */
  166. while($row['pid'] != 0) {
  167. $commentId = intval($row['pid']);
  168. $commentIds[] = $commentId;
  169. $row = $this->db->once_fetch_array("SELECT pid FROM ".DB_PREFIX."comment WHERE cid=$commentId");
  170. }
  171. $commentIds = implode(',',$commentIds);
  172. $this->db->query("UPDATE ".DB_PREFIX."comment SET hide='n' WHERE cid IN ($commentIds)");
  173. $this->updateCommentNum($blogId);
  174. }
  175. function replyComment($blogId, $pid, $content, $hide)
  176. {
  177. $CACHE = Cache::getInstance();
  178. $user_cache = $CACHE->readCache('user');
  179. if(isset($user_cache[UID])) {
  180. $name = addslashes($user_cache[UID]['name_orig']);
  181. $mail = addslashes($user_cache[UID]['mail']);
  182. $url = addslashes(BLOG_URL);
  183. $ipaddr = getIp();
  184. $utctimestamp = time();
  185. if($pid != 0) {
  186. $comment = $this->getOneComment($pid);
  187. $content = '@' . addslashes($comment['poster']) . '?' . $content;
  188. }
  189. $this->db->query("INSERT INTO ".DB_PREFIX."comment (date,poster,gid,comment,mail,url,hide,ip,pid)
  190. VALUES ('$utctimestamp','$name','$blogId','$content','$mail','$url','$hide','$ipaddr','$pid')");
  191. $this->updateCommentNum($blogId);
  192. }
  193. }
  194. /**
  195. * ??????
  196. */
  197. function batchComment($action, $comments)
  198. {
  199. switch ($action)
  200. {
  201. case 'delcom':
  202. foreach($comments as $val)
  203. {
  204. $this->delComment($val);
  205. }
  206. break;
  207. case 'hidecom':
  208. foreach($comments as $val)
  209. {
  210. $this->hideComment($val);
  211. }
  212. break;
  213. case 'showcom':
  214. foreach($comments as $val)
  215. {
  216. $this->showComment($val);
  217. }
  218. break;
  219. }
  220. }
  221. function updateCommentNum($blogId)
  222. {
  223. $sql = "SELECT count(*) FROM ".DB_PREFIX."comment WHERE gid=$blogId AND hide='n'";
  224. $res = $this->db->once_fetch_array($sql);
  225. $comNum = $res['count(*)'];
  226. $this->db->query("UPDATE ".DB_PREFIX."blog SET comnum=$comNum WHERE gid=$blogId");
  227. return $comNum;
  228. }
  229. function addComment($name, $content, $mail, $url, $imgcode, $blogId, $pid)
  230. {
  231. $ipaddr = getIp();
  232. $utctimestamp = time();
  233. if($pid != 0) {
  234. $comment = $this->getOneComment($pid);
  235. $content = '@' . addslashes($comment['poster']) . '?' . $content;
  236. }
  237. $ischkcomment = Option::get('ischkcomment');
  238. $hide = ROLE == 'visitor' ? $ischkcomment : 'n';
  239. $sql = 'INSERT INTO '.DB_PREFIX."comment (date,poster,gid,comment,mail,url,hide,ip,pid)
  240. VALUES ('$utctimestamp','$name','$blogId','$content','$mail','$url','$hide','$ipaddr','$pid')";
  241. $ret = $this->db->query($sql);
  242. $cid = $this->db->insert_id();
  243. $CACHE = Cache::getInstance();
  244. if ($hide == 'n') {
  245. $this->db->query('UPDATE '.DB_PREFIX."blog SET comnum = comnum + 1 WHERE gid='$blogId'");
  246. $CACHE->updateCache(array('sta', 'comment'));
  247. doAction('comment_saved', $cid);
  248. header('Location: ' . Url::log($blogId).'#'.$cid);
  249. } else {
  250. $CACHE->updateCache('sta');
  251. doAction('comment_saved', $cid);
  252. emMsg('???????????????', Url::log($blogId));
  253. }
  254. }
  255. function isCommentExist($blogId, $name, $content)
  256. {
  257. $query = $this->db->query("SELECT cid FROM ".DB_PREFIX."comment WHERE gid=$blogId AND poster='$name' AND comment='$content'");
  258. $result = $this->db->num_rows($query);
  259. if ($result > 0)
  260. {
  261. return true;
  262. }else {
  263. return false;
  264. }
  265. }
  266. function isNameAndMailValid($name, $mail)
  267. {
  268. $CACHE = Cache::getInstance();
  269. $user_cache = $CACHE->readCache('user');
  270. foreach($user_cache as $user) {
  271. if($user['name'] == $name || ($mail != '' && $user['mail'] == $mail)) {
  272. return false;
  273. }
  274. }
  275. return true;
  276. }
  277. function isLogCanComment($blogId)
  278. {
  279. $query = $this->db->query("SELECT allow_remark FROM ".DB_PREFIX."blog WHERE gid=$blogId");
  280. $show_remark = $this->db->fetch_array($query);
  281. if ($show_remark['allow_remark'] == 'n' || $show_remark === false)
  282. {
  283. return false;
  284. }else {
  285. return true;
  286. }
  287. }
  288. function setCommentCookie($name,$mail,$url)
  289. {
  290. $cookietime = time() + 31536000;
  291. setcookie('commentposter',$name,$cookietime);
  292. setcookie('postermail',$mail,$cookietime);
  293. setcookie('posterurl',$url,$cookietime);
  294. }
  295. }