PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/common/module/comment.php

http://lazycms.googlecode.com/
PHP | 358 lines | 230 code | 8 blank | 120 comment | 41 complexity | 7b167f59b3d0576c3d9fbdb2e9ce84a2 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * +---------------------------------------------------------------------------+
  4. * | LL LLLL LL L LLLL LLLL |
  5. * | LL LL L LLL LL LL L LL LL |
  6. * | LL LLLL LLLLL LL LL LL LLLL LLL LL LL LL LL |
  7. * | LL LL LL LL LL LL L LLL LL LLLLL LL LL LL |
  8. * | LL LLLLL LL LLLL LL L L LL LLLLL LL LL LL |
  9. * | LL LL LL LL LLLL LL L LL LL LLLL LL |
  10. * | LL LL LL LL LL LL L L LL L LL LLLL LL |
  11. * | LLLLLL LLLLL LLLLL LL LLLL L LL LLLL LL LLLLLL |
  12. * | LL |
  13. * | LL |
  14. * +---------------------------------------------------------------------------+
  15. * | Copyright (C) 2007-2010 LazyCMS.com All rights reserved. |
  16. * +---------------------------------------------------------------------------+
  17. * | LazyCMS is free software. See LICENSE for copyright notices and details. |
  18. * +---------------------------------------------------------------------------+
  19. */
  20. defined('COM_PATH') or die('Restricted access!');
  21. /**
  22. * ????
  23. *
  24. * @param int $postid
  25. * @param string $content
  26. * @param int $parent
  27. * @param array $user
  28. * @return int
  29. */
  30. function comment_add($postid,$content,$parent=0,$user=null) {
  31. $userid = isset($user['userid']) ? $user['userid'] : 0;
  32. $author = isset($user['name']) ? esc_html($user['name']) : '';
  33. $email = isset($user['mail']) ? esc_html($user['mail']) : '';
  34. $url = isset($user['url']) ? esc_html($user['url']) : '';
  35. return get_conn()->insert('#@_comment', array(
  36. 'postid' => $postid,
  37. 'author' => $author,
  38. 'mail' => $email,
  39. 'url' => $url,
  40. 'ip' => sprintf('%u',ip2long(get_ip())),
  41. 'agent' => esc_html($_SERVER['HTTP_USER_AGENT']),
  42. 'date' => time(),
  43. 'content' => $content,
  44. 'parent' => $parent,
  45. 'userid' => $userid,
  46. 'approved' => 1,
  47. ));
  48. }
  49. /**
  50. * ????
  51. *
  52. * @param int $cmtid
  53. * @param string $content
  54. * @param string $status
  55. * @param array $user
  56. * @return int
  57. */
  58. function comment_edit($cmtid, $content, $status=null, $user=null) {
  59. $sets = array();
  60. if ($content !== null) $sets['content'] = $content;
  61. if ($status !== null) $sets['approved'] = $status;
  62. if (isset($user['name'])) $sets['author'] = esc_html($user['name']);
  63. if (isset($user['mail'])) $sets['mail'] = esc_html($user['mail']);
  64. if (isset($user['url'])) $sets['url'] = esc_html($user['url']);
  65. if (isset($user['userid'])) $sets['userid'] = esc_html($user['userid']);
  66. $result = get_conn()->update('#@_comment', $sets, array('cmtid' => $cmtid));
  67. // ??????
  68. comment_create(comment_get_postid($cmtid));
  69. return $result;
  70. }
  71. /**
  72. * ??postid
  73. *
  74. * @param int $cmtid
  75. * @return int
  76. */
  77. function comment_get_postid($cmtid) {
  78. return get_conn()->result(sprintf("SELECT `postid` FROM `#@_comment` WHERE `cmtid`=%d;", $cmtid));
  79. }
  80. /**
  81. * ??????
  82. *
  83. * @param int $cmtid
  84. * @return array
  85. */
  86. function comment_get($cmtid) {
  87. static $comments = array();
  88. if (isset($comments[$cmtid]))
  89. return $comments[$cmtid];
  90. $db = get_conn();
  91. $rs = $db->query("SELECT * FROM `#@_comment` WHERE `cmtid`=%d;", $cmtid);
  92. if ($data = $db->fetch($rs)) {
  93. $comments[$cmtid] = $data;
  94. }
  95. return $comments[$cmtid];
  96. }
  97. /**
  98. * ?????
  99. *
  100. * @param int $postid
  101. * @param int $parentid
  102. * @return array
  103. */
  104. function comment_get_trees($postid, $parentid=0) {
  105. static $trees;
  106. if (!$trees) {
  107. $db = get_conn();
  108. $rs = $db->query("SELECT * FROM `#@_comment` WHERE `postid`=%d;", $postid);
  109. while ($data = $db->fetch($rs)){
  110. $data['ip'] = long2ip($data['ip']);
  111. $data['ipaddr'] = ip2addr($data['ip']);
  112. if ($data['ip'] == $data['ipaddr']) {
  113. $data['ip'] = substr_replace($data['ip'], '*', strrpos($data['ip'], '.')+1);
  114. $data['ipaddr'] = $data['ip'];
  115. } else {
  116. $data['ip'] = substr_replace($data['ip'], '*', strrpos($data['ip'], '.')+1);
  117. }
  118. $trees[$data['cmtid']] = $data;
  119. }
  120. }
  121. // ???????????????????????????
  122. foreach ($trees as $id => $item) {
  123. if ($item['parent']) {
  124. $trees[$id]['parents'] = &$trees[$item['parent']];
  125. }
  126. }
  127. if ($parentid) {
  128. $result = isset($trees[$parentid]['parents']) ? $trees[$parentid]['parents'] : null;
  129. } else {
  130. $result = $trees;
  131. }
  132. return $result;
  133. }
  134. /**
  135. * ????
  136. *
  137. * @param int $cmtid
  138. * @return int
  139. */
  140. function comment_delete($cmtid) {
  141. $postid = comment_get_postid($cmtid);
  142. $result = get_conn()->delete('#@_comment', array('cmtid' => $cmtid));
  143. // ??????
  144. comment_create($postid);
  145. return $result;
  146. }
  147. /**
  148. * ???
  149. *
  150. * @param int $postid
  151. * @param string $status
  152. * @return int
  153. */
  154. function comment_count($postid,$status='all') {
  155. $where = 'WHERE 1';
  156. if ($postid) {
  157. $where.= sprintf(" AND `postid`='%d'", $postid);
  158. }
  159. if ($status != 'all') {
  160. $where.= sprintf(" AND `approved`='%s'", strval($status));
  161. }
  162. return get_conn()->result("SELECT COUNT(`cmtid`) FROM `#@_comment` {$where};");
  163. }
  164. /**
  165. * ????
  166. *
  167. * @param int $postid
  168. * @return int
  169. */
  170. function comment_people($postid) {
  171. return get_conn()->result(sprintf("SELECT COUNT(DISTINCT(`author`)) FROM `#@_comment` WHERE `postid`=%d;", $postid));
  172. }
  173. /**
  174. * ????
  175. *
  176. * @param array $comment
  177. * @param array $sblock
  178. * @return mixed
  179. */
  180. function comment_parse_reply($comment, $sblock) {
  181. $func = __FUNCTION__;
  182. $tpl = tpl_init('post-comment-reply');
  183. $sblock['inner'] = tpl_get_block_inner($sblock);
  184. tpl_clean($tpl);
  185. tpl_set_var(array(
  186. 'cmtid' => $comment['cmtid'],
  187. 'avatar' => get_avatar($comment['mail'], 16, 'mystery'),
  188. 'author' => $comment['author'] ? $comment['author'] : __('Anonymous'),
  189. 'email' => $comment['mail'],
  190. 'url' => !strncmp($comment['url'], 'http://', 7) ? $comment['url'] : 'http://' . $comment['url'],
  191. 'ip' => $comment['ip'],
  192. 'address' => $comment['ipaddr'],
  193. 'content' => nl2br($comment['content']),
  194. 'agent' => $comment['agent'],
  195. 'date' => $comment['date'],
  196. ), $tpl);
  197. if (isset($comment['parents'])) {
  198. tpl_set_var('contents_deep', $func($comment['parents'], $sblock), $tpl);
  199. }
  200. return tpl_parse($sblock['inner'], $tpl);
  201. }
  202. /**
  203. * ????
  204. *
  205. * @param int $postid
  206. * @return bool|int
  207. */
  208. function comment_create($postid) {
  209. $postid = intval($postid);
  210. if (!$postid) return false;
  211. if ($post = post_get($postid)) {
  212. // ???
  213. $tpl = tpl_init('comments');
  214. // ????
  215. if ($post['comments'] != 'Yes') return true;
  216. // ????
  217. $post['cmt_path'] = post_get_path($post['listid'],$post['path'], C('CMT-Path'));
  218. $guide = system_category_guide($post['listid']);
  219. $title = sprintf(__('Comment: %s'), $post['title']);
  220. // ????
  221. $html = tpl_loadfile(ABS_PATH.'/'.system_themes_path().'/'.esc_html(C('TPL-Comment')));
  222. // ??????
  223. if (stripos($html,'{pagelist') !== false) {
  224. $html = preg_replace('/\{(pagelist)[^\}]*\/\}/isU','{$pagelist}', $html);
  225. }
  226. // ????
  227. if (($pos=strrpos($post['cmt_path'],'.')) !== false) {
  228. $basename = substr($post['cmt_path'],0,$pos);
  229. $suffix = substr($post['cmt_path'],$pos);
  230. } else {
  231. $basename = $post['cmt_path'];
  232. $suffix = '';
  233. }
  234. // ?????
  235. if ($block = tpl_get_block($html,'comment,comments','list')) {
  236. $inner = $b_guid = '';
  237. // ????????ID
  238. $b_guid = guid($block['tag']);
  239. // ???????????
  240. $html = str_replace($block['tag'], '{$'.$b_guid.'}', $html);
  241. // ????
  242. if (comment_count($post['postid'],'1') == 0) {
  243. tpl_clean($tpl);
  244. tpl_set_var($b_guid, __('No comment!'), $tpl);
  245. tpl_set_var(array(
  246. 'guide' => $guide ? $guide.' &gt;&gt; '.$title : $title,
  247. 'title' => $title,
  248. 'keywords' => post_get_taxonomy($post['keywords']),
  249. 'description' => $post['description'],
  250. ), $tpl);
  251. $html = tpl_parse($html, $block, $tpl);
  252. // ???????
  253. $file = ABS_PATH.'/'.$post['cmt_path'];
  254. // ????
  255. mkdirs(dirname($file));
  256. // ????
  257. return file_put_contents($file, $html);
  258. }
  259. // ???
  260. else {
  261. // ????
  262. $number = tpl_get_attr($block['tag'],'number');
  263. // ????
  264. $order = tpl_get_attr($block['tag'],'order');
  265. // ?????
  266. $zebra = tpl_get_attr($block['tag'],'zebra');
  267. // ????
  268. $zebra = validate_is($zebra,VALIDATE_IS_NUMERIC) ? $zebra : 0;
  269. $number = validate_is($number,VALIDATE_IS_NUMERIC) ? $number : 10;
  270. $order = instr(strtoupper($order),'ASC,DESC') ? $order : 'DESC';
  271. // ????
  272. system_porcess_create($block['tag']);
  273. $db = get_conn(); $i = $length = 0; $page = 1;
  274. $rs = $db->query("SELECT * FROM `#@_comment` WHERE `postid`=%d AND `approved`='1' ORDER BY `cmtid` {$order};", $post['postid']);
  275. $total = $db->result(sprintf("SELECT COUNT(`cmtid`) FROM `#@_comment` WHERE `postid`=%d AND `approved`='1';", $post['postid']));
  276. $pages = ceil($total / $number);
  277. $pages = ((int)$pages == 0) ? 1 : $pages;
  278. while ($data = $db->fetch($rs)) {
  279. $block['inner'] = tpl_get_block_inner($block);
  280. $data['ip'] = long2ip($data['ip']);
  281. $data['ipaddr'] = ip2addr($data['ip']);
  282. if ($data['ip'] == $data['ipaddr']) {
  283. $data['ip'] = substr_replace($data['ip'], '*', strrpos($data['ip'], '.')+1);
  284. $data['ipaddr'] = $data['ip'];
  285. } else {
  286. $data['ip'] = substr_replace($data['ip'], '*', strrpos($data['ip'], '.')+1);
  287. }
  288. // ????
  289. tpl_clean($tpl);
  290. tpl_set_var(array(
  291. 'zebra' => ($i % ($zebra + 1)) ? '0' : '1',
  292. 'postid' => $post['postid'],
  293. 'cmtid' => $data['cmtid'],
  294. 'avatar' => get_avatar($data['mail'], 16, 'mystery'),
  295. 'author' => $data['author'] ? $data['author'] : __('Anonymous'),
  296. 'email' => $data['mail'],
  297. 'url' => !strncmp($data['url'],'http://',7) ? $data['url'] : 'http://'.$data['url'],
  298. 'ip' => $data['ip'],
  299. 'address' => $data['ipaddr'],
  300. 'content' => nl2br($data['content']),
  301. 'agent' => $data['agent'],
  302. 'date' => $data['date'],
  303. ), $tpl);
  304. // ????
  305. $inner.= tpl_parse($block['inner'], $block, $tpl); $i++; $length++;
  306. // ??
  307. if (($i%$number)==0 || $i==$total) {
  308. // ?????????????????????
  309. if ($inner=='' && $page>1) return false;
  310. tpl_clean($tpl);
  311. tpl_set_var($b_guid, $inner, $tpl);
  312. tpl_set_var(array(
  313. 'guide' => $guide ? $guide.' &gt;&gt; '.$title : $title,
  314. 'title' => $title,
  315. 'pagelist' => pages_list(ROOT.$basename.'_$'.$suffix, '!_$', $page, $pages, $length),
  316. 'keywords' => post_get_taxonomy($post['keywords']),
  317. 'description' => $post['description'],
  318. ), $tpl);
  319. $out = tpl_parse($html, $tpl);
  320. // ???????
  321. $file = ABS_PATH.'/'.$basename . ($page==1 ? '' : '_'.$page) . $suffix;
  322. // ????
  323. mkdirs(dirname($file));
  324. // ????
  325. file_put_contents($file, $out);
  326. $page++; $inner = ''; $length = 0;
  327. }
  328. }
  329. return true;
  330. }
  331. }
  332. // ????
  333. else {
  334. tpl_clean($tpl);
  335. tpl_set_var(array(
  336. 'guide' => $guide ? $guide.' &gt;&gt; '.$title : $title,
  337. 'title' => $title,
  338. 'keywords' => post_get_taxonomy($post['keywords']),
  339. 'description' => $post['description'],
  340. ), $tpl);
  341. $html = tpl_parse($html, $tpl);
  342. // ???????
  343. $file = ABS_PATH.'/'.$post['cmt_path'];
  344. // ????
  345. mkdirs(dirname($file));
  346. // ????
  347. return file_put_contents($file, $html);
  348. }
  349. }
  350. }