/common/module/post.php
PHP | 655 lines | 443 code | 6 blank | 206 comment | 80 complexity | 2012e6a5f174577b3f1413b28b93ee99 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 $title
- * @param $content
- * @param $path
- * @param $data
- * @return array
- */
- function post_add($title,$content,$path,$data=null) {
- $postid = get_conn()->insert('#@_post',array(
- 'title' => $title,
- 'content' => $content,
- 'path' => $path,
- 'type' => 'post',
- 'approved' => 'passed',
- 'datetime' => time(),
- 'edittime' => time(),
- ));
- $data['path'] = $path;
- return post_edit($postid,$data);
- }
- /**
- * ??????
- *
- * @param int $postid
- * @param array $data
- * @return array
- */
- function post_edit($postid,$data) {
- $postid = intval($postid);
- if ($post = post_get($postid)) {
- $tpl = tpl_init('page-404');
- $data = is_array($data) ? $data : array();
- // ?????
- if (isset($data['path'])) {
- $data['path'] = path_format($data['path'],array(
- 'ID' => $postid,
- 'PY' => $post['title'],
- 'MD5' => $postid,
- ));
- // ?????
- if ($data['path'] != $post['path']) {
- $post['path'] = post_get_path($post['listid'],$post['path']);
- $post['edittime'] = $post['edittime'] ? $post['edittime'] : time();
- // ??????24??
- if (time()-$post['edittime'] > 86400) {
- // ????????
- if (strncmp($data['path'],'/',1) === 0) {
- $path = ltrim($data['path'], '/');
- } elseif ($post['listid'] > 0) {
- $taxonomy = taxonomy_get($post['listid']);
- $path = $taxonomy['path'].'/'.$data['path'];
- }
- $html = tpl_loadfile(ABS_PATH.'/'.system_themes_path().'/'.esc_html(C('TPL-404')));
- tpl_clean($tpl);
- tpl_set_var(array(
- 'path' => ROOT.$post['path'],
- 'url' => ROOT.$path,
- 'title' => $post['title'],
- 'keywords' => post_get_taxonomy($post['keywords']),
- 'description' => $post['description'],
- ), $tpl);
- $html = tpl_parse($html, $tpl);
- $file = ABS_PATH.'/'.$post['path'];
- mkdirs(dirname($file));
- file_put_contents($file,$html);
- // ??????
- $data['edittime'] = time();
- }
- // ?????
- elseif (is_file(ABS_PATH.'/'.$post['path'])){
- unlink(ABS_PATH.'/'.$post['path']);
- }
- }
- }
- $category = isset($data['category']) ? $data['category'] : null;
- $keywords = isset($data['keywords']) ? $data['keywords'] : null;
- unset($data['category'],$data['keywords']);
- $meta_rows = empty($data['meta']) ? array() : $data['meta']; unset($data['meta']);
- $post_rows = $data; $data['meta'] = $meta_rows; $data['category'] = $category;
- // ????
- if (!empty($post_rows)) {
- get_conn()->update('#@_post',$post_rows,array('postid' => $postid));
- }
- if (!empty($meta_rows)) {
- post_edit_meta($postid,$meta_rows);
- }
- // ??????
- if ($data['category']) {
- taxonomy_make_relation('category',$postid,$data['category']);
- }
- // ?????
- if ($keywords) {
- $taxonomies = array();
- if (!is_array($keywords)) {
- // ????????????
- $keywords = str_replace(array('?','?'),array(',',' '),$keywords);
- // ??,?????
- $keywords = explode(',',$keywords);
- // ??????????????
- if (count($keywords)==1) $keywords = explode(' ',$keywords[0]);
- }
- // ????????
- $keywords = array_unique($keywords);
- // ?????????????HTML
- array_walk($keywords,create_function('&$s','$s=esc_html(trim($s));'));
- // ???????
- foreach($keywords as $key) {
- $taxonomies[] = taxonomy_add_tag($key, 'post_tag');
- }
- $data['keywords'] = implode(',',$keywords);
- // ????
- taxonomy_make_relation('post_tag',$postid,$taxonomies);
- }
- // ????
- post_clean_cache($postid);
- return array_merge($post,$data);
- }
- return null;
- }
- /**
- * ????????
- *
- * @param $postid
- * @param $path ???format_path()???????
- * @return bool
- */
- function post_path_exists($postid,$path) {
- if (strpos($path,'%ID')!==false && strpos($path,'%MD5')!==false) return false;
- if ($postid) {
- $sql = sprintf("SELECT COUNT(`postid`) FROM `#@_post` WHERE `path`='%s' AND `postid`<>'%d';", esc_sql($path), esc_sql($postid));
- } else {
- $sql = sprintf("SELECT COUNT(`postid`) FROM `#@_post` WHERE `path`='%s';",esc_sql($path));
- }
- return !(get_conn()->result($sql) == 0);
- }
- /**
- * ??????
- *
- * @param string $type
- * @return int
- */
- function post_count($type) {
- return get_conn()->result(sprintf("SELECT COUNT(`postid`) FROM `#@_post` WHERE `type`='%s' AND `approved`='passed';", $type));
- }
- /**
- * ???????
- *
- * @param int $postid
- * @return array
- */
- function post_get($postid) {
- $db = get_conn();
- $ckey = sprintf('post.%d',$postid);
- $post = fcache_get($ckey);
- if (fcache_not_null($post)) return $post;
- $rs = $db->query("SELECT * FROM `#@_post` WHERE `postid`=%d LIMIT 1 OFFSET 0;",$postid);
- // ????????
- if ($post = $db->fetch($rs)) {
- // ??????
- $post['content'] = media_decode($post['content']);
- // ??????
- $post['category'] = taxonomy_get_relation('category',$postid);
- $post['keywords'] = taxonomy_get_relation('post_tag',$postid);
- if ($meta = post_get_meta($post['postid'])) {
- $post['meta'] = $meta;
- }
- // ?????
- fcache_set($ckey,$post);
- return $post;
- }
- return null;
- }
- /**
- * ??????
- *
- * @param int $listid
- * @param string $path
- * @param string $prefix
- * @return string
- */
- function post_get_path($listid,$path,$prefix='') {
- if ($prefix) {
- $prefix = !substr_compare($prefix,'/',strlen($prefix)-1,1) ? $prefix : $prefix.'/';
- if (strncmp($prefix,'/',1) === 0) {
- return ltrim($prefix,'/').ltrim($path, '/');
- }
- }
- if (strncmp($path,'/',1) === 0) {
- $path = ltrim($prefix,'/').ltrim($path, '/');
- } elseif ($listid > 0) {
- $taxonomy = taxonomy_get($listid);
- if (isset($taxonomy['path'])) {
- $path = $taxonomy['path'].'/'.$prefix.$path;
- }
- } else {
- $path = $prefix.$path;
- }
- return $path;
- }
- /**
- * ???????
- *
- * @param int $postid
- * @param string $suffixs
- * @return array
- */
- function post_get_medias($postid, $suffixs='*') {
- $medias = array();
- if ($post = post_get($postid)) {
- if (isset($post['meta']['__medias__']) && $post['meta']['__medias__']) {
- foreach (explode(',', $post['meta']['__medias__']) as $id) {
- if (($media=media_get($id)) && ($suffixs=='*' || instr($media['suffix'], $suffixs))) {
- $medias[] = $media;
- }
- }
- }
- }
- return $medias;
- }
- /**
- * ?????
- *
- * @param array $keywords
- * @param bool $isjoin
- * @return array|string
- */
- function post_get_taxonomy($keywords, $isjoin=false) {
- $result = array();
- foreach((array)$keywords as $taxonomyid) {
- $taxonomy = taxonomy_get($taxonomyid);
- $result[$taxonomyid] = str_replace(chr(44), ',', $taxonomy['name']);
- }
- return $isjoin ? implode(',', $result) : $result;
- }
- /**
- * ?????????
- *
- * @param $postid
- * @return array
- */
- function post_get_meta($postid) {
- $db = get_conn(); $result = array(); $postid = intval($postid);
- $rs = $db->query("SELECT * FROM `#@_post_meta` WHERE `postid`=%d;",$postid);
- while ($row = $db->fetch($rs)) {
- $result[$row['key']] = is_serialized($row['value']) ? unserialize($row['value']) : $row['value'];
- }
- return $result;
- }
- /**
- * ?????????
- *
- * @param $postid
- * @param $data
- * @return bool
- */
- function post_edit_meta($postid,$data) {
- $db = get_conn(); $postid = intval($postid);
- if (!is_array($data)) return false;
- foreach ($data as $key=>$value) {
- // ????????????
- $length = (int) $db->result(vsprintf("SELECT COUNT(*) FROM `#@_post_meta` WHERE `postid`=%d AND `key`='%s';",array($postid,esc_sql($key))));
- // update
- if ($length > 0) {
- $db->update('#@_post_meta',array(
- 'value' => $value,
- ),array(
- 'postid' => $postid,
- 'key' => $key,
- ));
- }
- // insert
- else {
- // ???????
- $db->insert('#@_post_meta',array(
- 'postid' => $postid,
- 'key' => $key,
- 'value' => $value,
- ));
- }
- }
- return true;
- }
- /**
- * ??????
- *
- * @param $postid
- * @return bool
- */
- function post_clean_cache($postid) {
- return fcache_delete('post.'.$postid);
- }
- /**
- * ??????
- *
- * @param $postid
- * @return bool
- */
- function post_delete($postid) {
- $db = get_conn();
- $postid = intval($postid);
- if (!$postid) return false;
- if ($post = post_get($postid)) {
- // ????
- $post['path'] = post_get_path($post['listid'],$post['path']);
- // ????
- if (is_file(ABS_PATH.'/'.$post['path'])) {
- if (!unlink(ABS_PATH.'/'.$post['path'])) {
- return false;
- }
- }
- // ??????
- foreach($post['category'] as $taxonomyid) {
- taxonomy_delete_relation($postid,$taxonomyid);
- }
- // ???????
- foreach($post['keywords'] as $taxonomyid) {
- taxonomy_delete_relation($postid,$taxonomyid);
- }
- $db->delete('#@_post_meta',array('postid' => $postid));
- $db->delete('#@_post',array('postid' => $postid));
- // ????
- post_clean_cache($postid);
- return true;
- }
- return false;
- }
- /**
- * ????
- *
- * @param $postid
- * @return bool
- */
- function post_create($postid,&$preid=0,&$nextid=0) {
- $postid = intval($postid);
- if (!$postid) return false;
- if ($post = post_get($postid)) {
- $tpl = tpl_init('post');
- $b_guid = $inner = ''; comment_create($post['postid']); // ????
- // ????
- $post['list'] = taxonomy_get($post['listid']);
- $post['cmt_path'] = post_get_path($post['listid'],$post['path'], C('CMT-Path'));
- $post['path'] = post_get_path($post['listid'],$post['path']);
- // ????????????>????>????
- if (empty($post['template'])) {
- if ($post['listid'] > 0) {
- $taxonomy = taxonomy_get($post['listid']);
- $post['template'] = $taxonomy['page'];
- }
- // ??????
- if (empty($post['template'])) {
- $model = model_get_bycode($post['model']);
- $post['template'] = $model['page'];
- }
- }
- // ????
- $html = tpl_loadfile(ABS_PATH.'/'.system_themes_path().'/'.esc_html($post['template']));
-
- $vars = array(
- 'postid' => $post['postid'],
- 'userid' => $post['userid'],
- 'author' => $post['author'],
- 'views' => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=post_views&postid='.$post['postid'].'&updated=true"></script>',
- 'comment' => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=post_comment&postid='.$post['postid'].'"></script>',
- 'people' => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=post_comment_people&postid='.$post['postid'].'"></script>',
- 'digg' => $post['digg'],
- 'date' => $post['datetime'],
- 'edittime' => $post['edittime'],
- 'keywords' => post_get_taxonomy($post['keywords']),
- 'prepage' => post_prepage($post['listid'],$post['postid'],$preid),
- 'nextpage' => post_nextpage($post['listid'],$post['postid'],$nextid),
- 'cmt_state' => $post['comments'],
- 'cmt_ajaxinfo' => ROOT.'common/gateway.php?func=post_ajax_comment&postid='.$post['postid'],
- 'cmt_replyurl' => ROOT.'common/gateway.php?func=post_send_comment&postid='.$post['postid'],
- 'cmt_listsurl' => ROOT.$post['cmt_path'],
- 'description' => $post['description'],
- );
- // ????????
- $images = post_get_medias($post['postid'], C('UPIMG-Exts'));
- foreach($images as $k=>$image) {
- if ($k == 0) $vars['image'] = $image['url'];
- $vars['images'][($k+1)] = $image['url'];
- }
- // ??????
- if (isset($post['list'])) {
- $vars['list'] = array(
- 'id' => $post['list']['taxonomyid'],
- 'name' => $post['list']['name'],
- 'path' => ROOT.$post['list']['path'].'/',
- 'count' => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=taxonomy_count&listid='.$post['list']['taxonomyid'].'"></script>',
- );
- if (isset($post['list']['meta'])) {
- foreach((array)$post['list']['meta'] as $k=>$v) {
- $vars['list'][$k] = $v;
- }
- }
- }
- // ????
- tpl_clean($tpl);
- tpl_set_var($b_guid, $inner, $tpl);
- tpl_set_var($vars, $tpl);
- // ???????
- if (isset($post['meta'])) {
- tpl_set_var('post', $post['meta'], $tpl);
- }
- // ????
- $guide = system_category_guide($post['listid']);
- // ????
- if ($post['content'] && strpos($post['content'],'<!--pagebreak-->')!==false) {
- $contents = explode('<!--pagebreak-->',$post['content']);
- // ???
- $pages = count($contents);
- if (($pos=strrpos($post['path'],'.')) !== false) {
- $basename = substr($post['path'],0,$pos);
- $suffix = substr($post['path'],$pos);
- } else {
- $basename = $post['path'];
- $suffix = '';
- }
- foreach($contents as $i=>$content) {
- $page = $i + 1;
- if ($page == 1) {
- $path = $basename.$suffix;
- $title = $post['title'];
- } else {
- $path = $basename.'_'.$page.$suffix;
- $title = $post['title'].' ('.$page.')';
- tpl_set_var('views', '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=post_views&postid='.$post['postid'].'"></script>', $tpl);
- }
- tpl_set_var(array(
- 'guide' => $guide ? $guide.' >> '.$title : $title,
- 'title' => $title,
- 'content' => $content,
- 'path' => ROOT.$path,
- ), $tpl);
- $pagehtml = tpl_parse($html, $tpl);
- // ??????
- if (stripos($pagehtml,'{pagelist') !== false) {
- $pagehtml = preg_replace('/\{(pagelist)[^\}]*\/\}/isU',
- pages_list(ROOT.$basename.'_$'.$suffix, '!_$', $page, $pages, 1),
- $pagehtml
- );
- }
- // ???????
- $file = ABS_PATH.'/'.$path;
- // ????
- mkdirs(dirname($file));
- // ????
- file_put_contents($file,$pagehtml);
- }
- }
- // ????
- else {
- tpl_set_var(array(
- 'guide' => $guide ? $guide.' >> '.$post['title'] : $post['title'],
- 'title' => $post['title'],
- 'content' => $post['content'],
- 'path' => ROOT.$post['path'],
- ), $tpl);
- // ??????
- if (stripos($html,'{pagelist') !== false) {
- $html = preg_replace('/\{(pagelist)[^\}]*\/\}/isU','',$html);
- }
- $html = tpl_parse($html, $tpl);
- // ???????
- $file = ABS_PATH.'/'.$post['path'];
- // ????
- mkdirs(dirname($file));
- // ????
- return file_put_contents($file,$html);
- }
- }
- return true;
- }
- /**
- * ???
- *
- * @param int $listid
- * @param int $postid
- * @param int &$preid
- * @return string
- */
- function post_prepage($listid,$postid,&$preid=0) {
- $preid = get_conn()->result(sprintf("SELECT `objectid` FROM `#@_term_relation` WHERE `taxonomyid`=%d AND `objectid`<%d ORDER BY `objectid` DESC LIMIT 1 OFFSET 0;", esc_sql($listid), esc_sql($postid)));
- if ($preid) {
- $post = post_get($preid);
- $post['path'] = post_get_path($post['listid'],$post['path']);
- $result = '<a href="'.ROOT.$post['path'].'">'.$post['title'].'</a>';
- } elseif($listid) {
- $post = post_get($postid);
- $post['list'] = taxonomy_get($post['listid']);
- $result = '<a href="'.ROOT.$post['list']['path'].'/">['.$post['list']['name'].']</a>';
- } else {
- $result = '['.__('Not Supported').']';
- }
- return $result;
- }
- /**
- * ???
- *
- * @param int $listid
- * @param int $postid
- * @param int &$nextid
- * @return string
- */
- function post_nextpage($listid,$postid,&$nextid=0) {
- $nextid = get_conn()->result(sprintf("SELECT `objectid` FROM `#@_term_relation` WHERE `taxonomyid`=%d AND `objectid`>%d ORDER BY `objectid` ASC LIMIT 1 OFFSET 0;", esc_sql($listid), esc_sql($postid)));
- if ($nextid) {
- $post = post_get($nextid);
- $post['path'] = post_get_path($post['listid'],$post['path']);
- $result = '<a href="'.ROOT.$post['path'].'">'.$post['title'].'</a>';
- } elseif($listid) {
- $post = post_get($postid);
- $post['list'] = taxonomy_get($post['listid']);
- $result = '<a href="'.ROOT.$post['list']['path'].'/">['.$post['list']['name'].']</a>';
- } else {
- $result = '['.__('Not Supported').']';
- }
- return $result;
- }
- /**
- * ??????
- *
- * @return
- */
- function post_gateway_ajax_comment() {
- $postid = isset($_GET['postid']) ? $_GET['postid'] : 0;
- $comment_count = comment_count($postid);
- $comment_people = comment_people($postid);
- return array($comment_count,$comment_people);
- }
- /**
- * ?????
- *
- * @return string
- */
- function post_gateway_views() {
- $postid = isset($_GET['postid']) ? $_GET['postid'] : 0;
- $updated = isset($_GET['updated']) ? $_GET['updated'] : null;
- if (post_get($postid)) {
- $db = get_conn();
- $views = $db->result(sprintf("SELECT `views` FROM `#@_post` WHERE `postid`=%d", esc_sql($postid)));
- if ($updated=='true' || $updated=='1') {
- $views++; no_cache();
- $db->update('#@_post',array('views' => $views),array( 'postid' => $postid));
- }
- } else {
- $views = 0;
- }
- return 'document.write('.$views.');';
- }
- /**
- * ????
- *
- * @return string
- */
- function post_gateway_comment() {
- $postid = isset($_GET['postid']) ? $_GET['postid'] : 0;
- return 'document.write('.comment_count($postid).');';
- }
- /**
- * ?????
- *
- * @return string
- */
- function post_gateway_comment_people() {
- $postid = isset($_GET['postid']) ? $_GET['postid'] : 0;
- return 'document.write('.comment_people($postid).');';
- }
- /**
- * ????
- *
- * @return void
- */
- function post_gateway_send_comment() {
- $postid = isset($_REQUEST['postid']) ? $_REQUEST['postid'] : 0;
- // ????????
- if (!($post = post_get($postid))) {
- return ajax_error(__('The post doesn\'t exist or has been deleted!'));
- }
- // ????????
- if ($post['comments'] != 'Yes') {
- return ajax_error(__('The post doesn\'t comment!'));
- }
- $parent = isset($_REQUEST['parent']) ? $_REQUEST['parent'] : 0;
- $content = isset($_REQUEST['content']) ? $_REQUEST['content'] : '';
- $content = esc_html(trim($content));
- // ??????
- if (!$content) return ajax_alert(__('Please enter a comment on the contents!'));
- if (mb_strlen($content,'UTF-8') > 500) return ajax_alert(sprintf(__('Maximum %d characters of Comment content!'), 500));
- global $_USER;
- if (!isset($_USER)) {
- $email = isset($_REQUEST['mail']) ? esc_html(trim($_REQUEST['mail'])) : '';
- if (!validate_is($email,VALIDATE_IS_EMAIL)) return ajax_alert(__('You must provide an e-mail address.'));
- $_USER = array(
- 'mail' => $email,
- 'name' => isset($_REQUEST['author']) ? esc_html(trim($_REQUEST['author'])) : '',
- 'url' => isset($_REQUEST['url']) ? esc_html(trim($_REQUEST['url'])) : '',
- );
- }
- // ????????
- $authcode = authcode();
- $cachekey = sprintf('comment.send.%s', $authcode);
- $session = fcache_get($cachekey);
- if (fcache_not_null($session)) {
- // ?????????
- if (time()-$session['time'] <= 3) {
- return ajax_error(__('You speak too fast, rest!'));
- }
- // ?????????
- if ($session['content'] == $content) {
- return ajax_error(__('You can not send duplicate comment!'));
- }
- // ?????????
- fcache_delete($cachekey);
- }
- // ????
- if (comment_add($postid,$content,$parent,$_USER)) {
- // ??????
- fcache_set($cachekey, array(
- 'time' => time(),
- 'content' => $content
- ), 86400);
- comment_create($postid);
- return ajax_success(__('Comment on the success!'));
- } else {
- return ajax_error(__('Comment failed!'));
- }
- }