<?php
if (!defined('R_P')) exit("access Die");
require_once(R_P . "/include/class.qmecms.php");
class qmecmsArticle extends qmecms {
	var $cate = array();
	var $keywords = array();
	var $nextpage = "[nextpage]";

	function qmecmsArticle() {
		$this -> qmecms();
		$this -> mod = 'article';
	} 

	function param() {
		parent :: param();
		if ($this -> param['cid'] < 1) _output_js("请选择栏目");
		$this -> cate = qdb :: queryFirst("select child, pur_browse from " . TABLEPRE . "acategory where cid='{$this->param['cid']}' limit 1");
		if (empty($this -> cate)) _output_js("出错了，当前所属栏目不存");
		if ($this -> cate['child'] > 0) _output_js("此栏目有子栏目，不能添加内容");
		$this -> param['description'] = trim(strip_tags($_POST['description']));
		$this -> param['is_link'] = intval($_POST['is_link']);
		if ($this -> param['is_link'] == 1) {
			$this -> param['linkurl'] = trim($_POST['linkurl']);
			if (!$this -> param['linkurl']) _output_js("请输入链接地址！");
		} else {
			$this -> param['is_link'] = 0;
			$this -> param['linkurl'] = '';
		} 
		$this->_loadThumb();
	} 

	function addAction() {
		$this -> param();
		$message = $this -> getMessage();
		$_POST['keyword'] = trim($_POST['keyword']);
		if (empty($_POST['keyword'])) {
			if (function_exists('keyword')) $temp = keyword($message, $this -> param['subject']);
			else $temp = array();
			$this -> keywords = array();
			foreach($temp as $val) {
				if (trim($val['name']) && $val['rank'] > 10) $this -> keywords[] = trim($val['name']);
			} 
			$_POST['keyword'] = trim(implode(',', $this -> keywords));
		} else {
			$keywrod = str_replace(array(",", "，"), "|", trim($_POST['keyword']));
			$this -> keywords = explode('|', $keywrod);
		} 
		$this -> param['keyword'] = implode(" ", $this -> keywords);
		$this -> param['page'] = substr_count($message, $this -> nextpage) + 1 ;
		$sql = qbase :: createSql($this -> table, $this -> param, 'insert');
		$id = qdb :: query($sql, true);
		if ($_POST['site_link']) {
			foreach($this -> keywords as $val) {
				$message = str_replace($val, addslashes("<a href='" . W_P . "index.php?ac=search&type=tag&word=" . $val . "'>" . $val . "</a>"), $message);
			} 
		} 
		$this -> updateMessage($id, $message);
		$this -> extend_insert($id);
		$this -> related($id, 0, 'add');
		$this -> add($id);
		$this->moveToRecycle($id, $this->param_recycle);
		qbase :: get_object('event', 'article', array('channelid' => $this -> channelid, 'id' => $id, 'action' => 'add', 'data' => $this -> param), true);
		$this -> _trackback($id, $this -> param['subject'], $message);
	} 

	function editAction() {
		$id = intval($_POST['id']);
		$this -> param();
		$message = $this -> getMessage();
		$this -> param['page'] = substr_count($message, "[nextpage]") + 1 ;
		$this -> updateMessage($id, $message);
		$this -> extend_update($id);
		$this -> related($id, 0, 'modify');
		$this -> modify($id);
		qbase :: get_object('event', 'article', array('channelid' => $this -> channelid, 'id' => $id, 'action' => 'modify', 'data' => $this -> param), true);
		$this -> _trackback($id, $this -> param['subject'], $message);
	} 

	function updateMessage($id, &$message) {
		$info = $this -> getInfo($id);
		if (empty($info)) return false;
		if ($info['is_link'] == 1) {
			if ($info['message']) {
				qdb :: query("update {$this->table} set message = 0 where id = '{$id}' ");
				qdb :: query("delete from " . TABLEPRE . "text  where text_id = '{$info['message']}' ");
			} 
		} else {
			if ($info['message']) {
				$text = qdb :: queryFirst("select message from " . TABLEPRE . "text where text_id = '{$info['message']}' ");
				if (empty($text)) {
					$text_id = qdb :: query("insert into " . TABLEPRE . "text (message) values ('{$message}')", true);
					qdb :: query("update {$this->table} set message = '{$text_id}' where id = '{$id}' ");
				} else {
					qdb :: query("update  " . TABLEPRE . "text set message = '{$message}' where text_id = '{$info['message']}' ");
				} 
			} else {
				$text_id = qdb :: query("insert into  " . TABLEPRE . "text (message) values ('{$message}')", true);
				qdb :: query("update {$this->table} set message = '{$text_id}' where id = '{$id}' ");
			} 
		} 
	} 

	function del($id) {
		$data = parent :: del($id);
		if ($data['message']) qdb :: Query("delete from " . TABLEPRE . "text where text_id='{$data['message']}'  ");
		qbase :: get_object('event', 'article', array('channelid' => $this -> channelid, 'id' => $id, 'action' => 'delete', 'data' => $data), true);
		return $data;
	} 

	function getMessage() {
		global $_SET;
		$message = stripslashes(trim($_POST['message']));
		if (!$message && !$this -> param['is_link']) _output_js("请填写" . $this -> channeltitle . "内容");
		$search = array ("'<style[^>]*?>.*?</style>'si");
		$message = preg_replace($search, '', $message);
		$message = qbase :: get_object('html_clear', intval($_POST['clear_tag'])) -> clear($message);
		if (intval($_POST['clear_link'])) {
			$message = $this -> clearSiteLink($message);
		} 

		if ($_SET['doc_link']) $message = $this -> short_url($message);

		return addslashes($message);
		/**
		* $num = substr_count($message, $this->nextpage);
		* if($num == 0) return $message;
		* else{
		* $temp = explode($this->nextpage, $message);
		* foreach($temp as &$val){
		* $val = qbase::get_object("tidy")->clean($val);
		* }
		* return implode($this->nextpage, $temp);
		* }
		*/
	} 

	function clearSiteLink(&$content) {
		global $main_domain;
		preg_match_all ("/<a[^\>]*>([^<\/]*)<\/a>/i", $content, $data);
		foreach($data[0] as $key => $val) {
			$flag = preg_match("/href\s*=s*[\'|\"]{0,1}(.+?)[\'|\"|\>| ]/ies", $val, $temp);
			if ($flag && strpos($temp[1], '://') !== false) {
				$t = @parse_url($temp[1]);
				if (strpos($t['host'], $main_domain) === false) {
					$content = str_replace($val, $data[1][$key], $content);
				} 
			} 
		} 
		return $content;
	} 

	function _loadThumb(){
		global $sSaveDir;
		if(!$this->param['thumb']) return;
		$url = str_replace(W_P, "", $this->param['thumb']);
		$thumb = '';
		if(substr($url, 0, 7) != 'http://') return ;
		$img = qbase::load($url);
		if(!$img) return;
		require_once(R_P . "/include/inc.editor.php");
		$ext = getExt($url);
		InitUpload();
		DoCreateNewDir();
		$thumb = $sSaveDir .  GetRndFileName($ext);
		$localfile = R_P .'/'. $thumb;
		qbase::writetofile($localfile);
		$info = @getimagesize($localfile);
		if($info === false){
			@unlink($localfile);
			return false;
		}else{
			$this->param['thumb'] = W_P . $thumb;
		}
	}

	function _trackback($id, $subject, &$msg) {
		if ($trackback = $_POST['trackback']) {
			$ping = parse_url($trackback);
			if (substr($ping['scheme'], 0, 4) == 'http' && $ping['host'] && $ping['path']) {
				$url = W_P . "index.php?mod={$this->channelid}&ac=show&id={$id}";
				qbase :: get_object('send_trackback') -> send($trackback, $subject, $url, stripslashes($msg));
			} 
		} 
	} 
} 

?>