/common/module/comment.php
PHP | 358 lines | 230 code | 8 blank | 120 comment | 41 complexity | 7b167f59b3d0576c3d9fbdb2e9ce84a2 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
- <?php
- /**
- * +---------------------------------------------------------------------------+
- * | LL LLLL LL L LLLL LLLL |
- * | LL LL L LLL LL LL L LL LL |
- * | LL LLLL LLLLL LL LL LL LLLL LLL LL LL LL LL |
- * | LL LL LL LL LL LL L LLL LL LLLLL LL LL LL |
- * | LL LLLLL LL LLLL LL L L LL LLLLL LL LL LL |
- * | LL LL LL LL LLLL LL L LL LL LLLL LL |
- * | LL LL LL LL LL LL L L LL L LL LLLL LL |
- * | LLLLLL LLLLL LLLLL LL LLLL L LL LLLL LL LLLLLL |
- * | LL |
- * | LL |
- * +---------------------------------------------------------------------------+
- * | Copyright (C) 2007-2010 LazyCMS.com All rights reserved. |
- * +---------------------------------------------------------------------------+
- * | LazyCMS is free software. See LICENSE for copyright notices and details. |
- * +---------------------------------------------------------------------------+
- */
- defined('COM_PATH') or die('Restricted access!');
- /**
- * ????
- *
- * @param int $postid
- * @param string $content
- * @param int $parent
- * @param array $user
- * @return int
- */
- function comment_add($postid,$content,$parent=0,$user=null) {
- $userid = isset($user['userid']) ? $user['userid'] : 0;
- $author = isset($user['name']) ? esc_html($user['name']) : '';
- $email = isset($user['mail']) ? esc_html($user['mail']) : '';
- $url = isset($user['url']) ? esc_html($user['url']) : '';
- return get_conn()->insert('#@_comment', array(
- 'postid' => $postid,
- 'author' => $author,
- 'mail' => $email,
- 'url' => $url,
- 'ip' => sprintf('%u',ip2long(get_ip())),
- 'agent' => esc_html($_SERVER['HTTP_USER_AGENT']),
- 'date' => time(),
- 'content' => $content,
- 'parent' => $parent,
- 'userid' => $userid,
- 'approved' => 1,
- ));
- }
- /**
- * ????
- *
- * @param int $cmtid
- * @param string $content
- * @param string $status
- * @param array $user
- * @return int
- */
- function comment_edit($cmtid, $content, $status=null, $user=null) {
- $sets = array();
- if ($content !== null) $sets['content'] = $content;
- if ($status !== null) $sets['approved'] = $status;
- if (isset($user['name'])) $sets['author'] = esc_html($user['name']);
- if (isset($user['mail'])) $sets['mail'] = esc_html($user['mail']);
- if (isset($user['url'])) $sets['url'] = esc_html($user['url']);
- if (isset($user['userid'])) $sets['userid'] = esc_html($user['userid']);
- $result = get_conn()->update('#@_comment', $sets, array('cmtid' => $cmtid));
- // ??????
- comment_create(comment_get_postid($cmtid));
- return $result;
- }
- /**
- * ??postid
- *
- * @param int $cmtid
- * @return int
- */
- function comment_get_postid($cmtid) {
- return get_conn()->result(sprintf("SELECT `postid` FROM `#@_comment` WHERE `cmtid`=%d;", $cmtid));
- }
- /**
- * ??????
- *
- * @param int $cmtid
- * @return array
- */
- function comment_get($cmtid) {
- static $comments = array();
- if (isset($comments[$cmtid]))
- return $comments[$cmtid];
-
- $db = get_conn();
- $rs = $db->query("SELECT * FROM `#@_comment` WHERE `cmtid`=%d;", $cmtid);
- if ($data = $db->fetch($rs)) {
- $comments[$cmtid] = $data;
- }
- return $comments[$cmtid];
- }
- /**
- * ?????
- *
- * @param int $postid
- * @param int $parentid
- * @return array
- */
- function comment_get_trees($postid, $parentid=0) {
- static $trees;
- if (!$trees) {
- $db = get_conn();
- $rs = $db->query("SELECT * FROM `#@_comment` WHERE `postid`=%d;", $postid);
- while ($data = $db->fetch($rs)){
- $data['ip'] = long2ip($data['ip']);
- $data['ipaddr'] = ip2addr($data['ip']);
- if ($data['ip'] == $data['ipaddr']) {
- $data['ip'] = substr_replace($data['ip'], '*', strrpos($data['ip'], '.')+1);
- $data['ipaddr'] = $data['ip'];
- } else {
- $data['ip'] = substr_replace($data['ip'], '*', strrpos($data['ip'], '.')+1);
- }
- $trees[$data['cmtid']] = $data;
- }
- }
- // ???????????????????????????
- foreach ($trees as $id => $item) {
- if ($item['parent']) {
- $trees[$id]['parents'] = &$trees[$item['parent']];
- }
- }
- if ($parentid) {
- $result = isset($trees[$parentid]['parents']) ? $trees[$parentid]['parents'] : null;
- } else {
- $result = $trees;
- }
- return $result;
- }
- /**
- * ????
- *
- * @param int $cmtid
- * @return int
- */
- function comment_delete($cmtid) {
- $postid = comment_get_postid($cmtid);
- $result = get_conn()->delete('#@_comment', array('cmtid' => $cmtid));
- // ??????
- comment_create($postid);
- return $result;
- }
- /**
- * ???
- *
- * @param int $postid
- * @param string $status
- * @return int
- */
- function comment_count($postid,$status='all') {
- $where = 'WHERE 1';
- if ($postid) {
- $where.= sprintf(" AND `postid`='%d'", $postid);
- }
- if ($status != 'all') {
- $where.= sprintf(" AND `approved`='%s'", strval($status));
- }
- return get_conn()->result("SELECT COUNT(`cmtid`) FROM `#@_comment` {$where};");
- }
- /**
- * ????
- *
- * @param int $postid
- * @return int
- */
- function comment_people($postid) {
- return get_conn()->result(sprintf("SELECT COUNT(DISTINCT(`author`)) FROM `#@_comment` WHERE `postid`=%d;", $postid));
- }
- /**
- * ????
- *
- * @param array $comment
- * @param array $sblock
- * @return mixed
- */
- function comment_parse_reply($comment, $sblock) {
- $func = __FUNCTION__;
- $tpl = tpl_init('post-comment-reply');
- $sblock['inner'] = tpl_get_block_inner($sblock);
- tpl_clean($tpl);
- tpl_set_var(array(
- 'cmtid' => $comment['cmtid'],
- 'avatar' => get_avatar($comment['mail'], 16, 'mystery'),
- 'author' => $comment['author'] ? $comment['author'] : __('Anonymous'),
- 'email' => $comment['mail'],
- 'url' => !strncmp($comment['url'], 'http://', 7) ? $comment['url'] : 'http://' . $comment['url'],
- 'ip' => $comment['ip'],
- 'address' => $comment['ipaddr'],
- 'content' => nl2br($comment['content']),
- 'agent' => $comment['agent'],
- 'date' => $comment['date'],
- ), $tpl);
- if (isset($comment['parents'])) {
- tpl_set_var('contents_deep', $func($comment['parents'], $sblock), $tpl);
- }
- return tpl_parse($sblock['inner'], $tpl);
- }
- /**
- * ????
- *
- * @param int $postid
- * @return bool|int
- */
- function comment_create($postid) {
- $postid = intval($postid);
- if (!$postid) return false;
- if ($post = post_get($postid)) {
- // ???
- $tpl = tpl_init('comments');
- // ????
- if ($post['comments'] != 'Yes') return true;
- // ????
- $post['cmt_path'] = post_get_path($post['listid'],$post['path'], C('CMT-Path'));
- $guide = system_category_guide($post['listid']);
- $title = sprintf(__('Comment: %s'), $post['title']);
- // ????
- $html = tpl_loadfile(ABS_PATH.'/'.system_themes_path().'/'.esc_html(C('TPL-Comment')));
- // ??????
- if (stripos($html,'{pagelist') !== false) {
- $html = preg_replace('/\{(pagelist)[^\}]*\/\}/isU','{$pagelist}', $html);
- }
- // ????
- if (($pos=strrpos($post['cmt_path'],'.')) !== false) {
- $basename = substr($post['cmt_path'],0,$pos);
- $suffix = substr($post['cmt_path'],$pos);
- } else {
- $basename = $post['cmt_path'];
- $suffix = '';
- }
- // ?????
- if ($block = tpl_get_block($html,'comment,comments','list')) {
- $inner = $b_guid = '';
- // ????????ID
- $b_guid = guid($block['tag']);
- // ???????????
- $html = str_replace($block['tag'], '{$'.$b_guid.'}', $html);
- // ????
- if (comment_count($post['postid'],'1') == 0) {
- tpl_clean($tpl);
- tpl_set_var($b_guid, __('No comment!'), $tpl);
- tpl_set_var(array(
- 'guide' => $guide ? $guide.' >> '.$title : $title,
- 'title' => $title,
- 'keywords' => post_get_taxonomy($post['keywords']),
- 'description' => $post['description'],
- ), $tpl);
- $html = tpl_parse($html, $block, $tpl);
- // ???????
- $file = ABS_PATH.'/'.$post['cmt_path'];
- // ????
- mkdirs(dirname($file));
- // ????
- return file_put_contents($file, $html);
- }
- // ???
- else {
- // ????
- $number = tpl_get_attr($block['tag'],'number');
- // ????
- $order = tpl_get_attr($block['tag'],'order');
- // ?????
- $zebra = tpl_get_attr($block['tag'],'zebra');
- // ????
- $zebra = validate_is($zebra,VALIDATE_IS_NUMERIC) ? $zebra : 0;
- $number = validate_is($number,VALIDATE_IS_NUMERIC) ? $number : 10;
- $order = instr(strtoupper($order),'ASC,DESC') ? $order : 'DESC';
- // ????
- system_porcess_create($block['tag']);
- $db = get_conn(); $i = $length = 0; $page = 1;
- $rs = $db->query("SELECT * FROM `#@_comment` WHERE `postid`=%d AND `approved`='1' ORDER BY `cmtid` {$order};", $post['postid']);
- $total = $db->result(sprintf("SELECT COUNT(`cmtid`) FROM `#@_comment` WHERE `postid`=%d AND `approved`='1';", $post['postid']));
- $pages = ceil($total / $number);
- $pages = ((int)$pages == 0) ? 1 : $pages;
- while ($data = $db->fetch($rs)) {
- $block['inner'] = tpl_get_block_inner($block);
- $data['ip'] = long2ip($data['ip']);
- $data['ipaddr'] = ip2addr($data['ip']);
- if ($data['ip'] == $data['ipaddr']) {
- $data['ip'] = substr_replace($data['ip'], '*', strrpos($data['ip'], '.')+1);
- $data['ipaddr'] = $data['ip'];
- } else {
- $data['ip'] = substr_replace($data['ip'], '*', strrpos($data['ip'], '.')+1);
- }
- // ????
- tpl_clean($tpl);
- tpl_set_var(array(
- 'zebra' => ($i % ($zebra + 1)) ? '0' : '1',
- 'postid' => $post['postid'],
- 'cmtid' => $data['cmtid'],
- 'avatar' => get_avatar($data['mail'], 16, 'mystery'),
- 'author' => $data['author'] ? $data['author'] : __('Anonymous'),
- 'email' => $data['mail'],
- 'url' => !strncmp($data['url'],'http://',7) ? $data['url'] : 'http://'.$data['url'],
- 'ip' => $data['ip'],
- 'address' => $data['ipaddr'],
- 'content' => nl2br($data['content']),
- 'agent' => $data['agent'],
- 'date' => $data['date'],
- ), $tpl);
- // ????
- $inner.= tpl_parse($block['inner'], $block, $tpl); $i++; $length++;
- // ??
- if (($i%$number)==0 || $i==$total) {
- // ?????????????????????
- if ($inner=='' && $page>1) return false;
- tpl_clean($tpl);
- tpl_set_var($b_guid, $inner, $tpl);
- tpl_set_var(array(
- 'guide' => $guide ? $guide.' >> '.$title : $title,
- 'title' => $title,
- 'pagelist' => pages_list(ROOT.$basename.'_$'.$suffix, '!_$', $page, $pages, $length),
- 'keywords' => post_get_taxonomy($post['keywords']),
- 'description' => $post['description'],
- ), $tpl);
- $out = tpl_parse($html, $tpl);
- // ???????
- $file = ABS_PATH.'/'.$basename . ($page==1 ? '' : '_'.$page) . $suffix;
- // ????
- mkdirs(dirname($file));
- // ????
- file_put_contents($file, $out);
- $page++; $inner = ''; $length = 0;
- }
- }
- return true;
- }
- }
- // ????
- else {
- tpl_clean($tpl);
- tpl_set_var(array(
- 'guide' => $guide ? $guide.' >> '.$title : $title,
- 'title' => $title,
- 'keywords' => post_get_taxonomy($post['keywords']),
- 'description' => $post['description'],
- ), $tpl);
- $html = tpl_parse($html, $tpl);
- // ???????
- $file = ABS_PATH.'/'.$post['cmt_path'];
- // ????
- mkdirs(dirname($file));
- // ????
- return file_put_contents($file, $html);
- }
- }
- }