<?php
if (!defined('R_P')) die("access die");
require_once(R_P . '/include/class.splitword.php');
class qmecms {
	var $channelid;
	var $channeltitle;
	var $channelselect;
	var $channeldir = '';
	var $auto = false;
	var $table;
	var $table_extend;
	var $param = array();
	var $param_pay = array();
	var $param_recycle = array();
	var $extend = array();
	var $has_extend = false;
	var $modify_views = false;
	var $mod = '';

	function qmecms() {
		global $channelid, $channel, $_MODULE, $_SET;
		$this -> channelid = $channelid;
		$this -> channeltitle = $channel[$channelid]['channeltitle'];
		$this -> channeldir = $channel[$channelid]['channeldir'];
		$this -> channelselect = $channel[$channelid]['channelselect'];
		$this -> table = "" . TABLEPRE . "" . $_MODULE[$this -> channelselect]['table_name'] . "_" . $this -> channelid;
		$this -> table_extend = "" . TABLEPRE . "" . $_MODULE[$this -> channelselect]['table_name'] . "s_" . $this -> channelid;
		$this -> auto = $channel[$channelid]['is_auto'];
		$this -> has_extend = is_array($_MODULE[$this -> channelselect]['extend']) && (count($_MODULE[$this -> channelselect]['extend']) > 0);
		$this -> modify_views = intval($_SET['modify_views']) ? true : false;
		$this -> mod = $_MODULE[$this -> channelselect]['table_name'];
		if (empty($channel[$channelid]) || empty($this -> channelselect)) qbase :: showmsg("构造所需数据异常");
	} 

	function get_table($channelid) {
		global $channel, $_MODULE;
		$channelselect = $channel[$channelid]['channelselect'];
		return isset($_MODULE[$channelselect]) ? "" . TABLEPRE . "" . $_MODULE[$channelselect]['table_name'] . "_" . $channelid : false;
	} 

	function param() {
		$this -> param['cid'] = intval($_POST['cid']);
		$this -> param['channelid'] = intval($this -> channelid);
		$this -> param['is_comend'] = intval($_POST['is_comend']);
		$this -> param['nocoment'] = intval($_POST['nocoment']);
		$this -> param['stars'] = intval($_POST['stars']);

		$this -> param['author'] = trim($_POST['author']);
		$this -> param['source'] = trim($_POST['source']);

		$this -> param['subject_pre'] = trim($_POST['subject_pre']);
		$this -> param['subject'] = htmlspecialchars(trim(strip_tags($_POST['subject'])));
		$this -> param['subject_color'] = trim($_POST['subject_color']);
		$this -> param['thumb'] = chop(trim($_POST['thumb']));


		if (!$this -> param['subject']) _output_js("请添加标题");

		if ($this -> modify_views) {
			$this -> param['dview'] = intval($_POST['dview']);
			$this -> param['wview'] = intval($_POST['wview']);
			$this -> param['mview'] = intval($_POST['mview']);
			$this -> param['views'] = intval($_POST['views']);
		} 

		$this -> param['editor'] = addslashes($GLOBALS['admin_info']['username']);
		$this -> param['postip'] = qbase :: get_ip();

		$this -> param['showphp'] = trim($_POST['showphp']);
		$this -> param['filename'] = formatFileName($_POST['filename']);
		if(!$this -> param['filename'] && intval($_POST['auto_filename']) > 0){
			$this -> param['filename'] = substr($this->autoFilename($this -> param['subject']), 0, 64);
		}
		$this -> param_recycle['status'] = isset($_POST['status']) ? intval($_POST['status']) : 0;
		$this -> param['dateline'] = $_SERVER['REQUEST_TIME'];
		$this -> param['edit_time'] = intval($_SERVER['REQUEST_TIME']);
		$filter = empty($_POST['filter']) ? array(0) : $_POST['filter'];
		$this -> param['filter'] = is_array($filter) ? implode(',' , $filter) : '0';

		$this -> param_pay['requirepoint'] = intval($_POST['requirepoint']);
		$this -> param_pay['read_purview'] = intval($_POST['read_purview']);
		$this -> param_pay['pay_type'] = intval($_POST['pay_type']);
		$this -> param_pay['read_last'] = intval($_POST['read_last']);
		$this -> param['pay'] = intval($this -> param_pay['read_purview'] || $this -> param_pay['requirepoint']);

		if (!defined('A_P')) {
			$this -> param['uid'] = intval($GLOBALS['user_info']['uid']);
			$this -> param['pay'] = 0;
			$this -> param['filter'] = '0';
			$this -> param['edit_time'] = 0;
			$this -> param['filename'] = '';
			$this -> param['showphp'] = '';
			$this -> param['dview'] = 0;
			$this -> param['wview'] = 0;
			$this -> param['mview'] = 0;
			$this -> param['views'] = 0;
			$this -> param['subject_color'] = '';
			$this -> param['subject_pre'] = '';
			$this -> param['is_comend'] = 0;
			$this -> param['nocoment'] = 0;
			$this -> param['stars'] = 0;
			$this -> param_recycle['status'] = 3;
		} 

		$this -> param['extdata'] = '';
		$extdata = array();
		$temp = $_POST['extdata'];
		if(is_array($temp['key']) && is_array($temp['value'])){
			foreach($temp['key'] as $key=>$val){
				$extdata['doc_' . trim($val)] =  trim($temp['value'][$key]);
			}
		}
		$this -> param['extdata'] = !empty($extdata) ? addslashes(stripslashes(serialize($extdata))) : '';
		$this -> get_extend();
	} 

	function pay_update($id) {
		if ($this -> param['pay']) {
			$this -> param_pay['channelid'] = $this -> channelid;
			$this -> param_pay['id'] = $id;
			qdb :: query(qbase :: createSql("" . TABLEPRE . "pay_mapping", $this -> param_pay , 'replace'));
		} else {
			qdb :: query("delete from " . TABLEPRE . "pay_mapping where channelid = '{$this->channelid}' and id = '{$id}' ");
		} 
	} 

	function extend_insert($id) {
		if (!$this -> has_extend) return ;
		$this -> extend['channelid'] = $this -> channelid;
		$this -> extend['id'] = $id;
		qdb :: query(qbase :: createSql(TABLEPRE . "extend_data_{$this->channelselect}", $this -> extend , 'replace'));
	} 

	function extend_update($id) {
		if (!$this -> has_extend) return ;
		$table = TABLEPRE . "extend_data_{$this->channelselect}";
		$where = " where channelid = '{$this->channelid}' and id = '{$id}' ";
		$num = qdb :: queryAlone("select count(*) as num from {$table} {$where} ");
		if ($num > 0) {
			if (!empty($this -> extend)) qdb :: query(qbase :: createSql($table, $this -> extend , 'update') . $where);
		} else {
			$this -> extend_insert($id);
		} 
	} 

	function get_extend() {
		$extend = array();
		$exts = array();
		$temp = $this -> getExtend($this -> channelselect);
		foreach($temp as $val) {
			$extend[$val['ext']] = $val['eid'];
		} 
		$exts = array_keys($extend);
		$data = $_POST['extend'];
		$param = array();
		if (!is_array($data)) return array();
		foreach($data as $key => $val) {
			if (!in_array($key, $exts)) continue;
			$value = is_array($val) ? implode(',', $val) : trim($val);
			$this -> extend['ext_' . $key] = $value;
		} 
	} 

	function add($id) {
		$this -> pay_update($id);
		$keywords = trim($_POST['keyword']);
		qbase :: get_object('tag') -> addKeyTag($id, $keywords);
		$this -> html($id);
	} 

	function modify($id) {
		unset($this -> param['dateline'], $this -> param['editor']);
		$old = $this -> get_item($id);
		$field = array('dview', 'wview', 'mview', 'views', 'cid', 'subject','subject_pre', 'subject_color','author','source','description','filter', 'thumb', 'filename', 'showphp', 'is_comend', 'nocoment', 'stars','extdata');
		$data = $this -> param;
		foreach($field as $v) {
			if ($data[$v] == $old[$v]) {
				unset($data[$v]);
			} 
		} 
		$old['status'] = intval($old['status']);
		if($old['status'] == 0){
			$sql = qbase :: createSql($this -> table, $data, 'update') . ", ver=ver+1 where id = '{$id}' ";
			qdb :: query($sql);
			if($this->param_recycle['status'] > 0){
				$this->moveToRecycle($id, $this->param_recycle);
			}
		}else{
			$tp = qdb::queryAlone("select data from " . TABLEPRE . "recycle where id = '{$id}' and channelid = '{$this->channelid}' ");
			if(!$tp) return false;
			$temp = @unserialize($tp);
			foreach($data as $key=>$val){
				$temp[$key] = $val;
			}
			$temp['status'] = $this->param_recycle['status'];
			$ser_data = addslashes(serialize($temp));
			qdb::query("update " . TABLEPRE . "recycle set data = '{$ser_data}', status = '{$temp['status']}' where id = '{$id}' and channelid = '{$this->channelid}' ");
			if($this->param_recycle['status'] == 0){
				$this->moveToRecycle($id, $this->param_recycle);
			}
		}

		$this -> pay_update($id);
		qbase :: get_object('content', $this -> channelid, $id) -> clear();
		$this -> html($id);
	} 

	function del($id) {
		$data = $this -> getInfo($id);
		$this -> delete_html($data);
		qdb :: Query("delete from {$this->table}  where id='$id'");
		qdb :: Query("delete from " . TABLEPRE . "recycle where id='$id' and channelid = '{$this->channelid}' ");
		qdb :: Query("delete from " . TABLEPRE . "admin_count where uid = '{$data['editor']}' and channelid = '{$data['channelid']}' and id = '{$id}' ");
		qdb :: Query("delete from " . TABLEPRE . "tag_mapping  where channelid = '{$this->channelid}' and id='$id'");
		qdb :: Query("delete from " . TABLEPRE . "digg  where channelid = '{$this->channelid}' and id='$id'");
		qdb :: Query("delete from " . TABLEPRE . "pay where channelid='{$this->channelid}' and id='$id'");
		qdb :: Query("delete from " . TABLEPRE . "pay_mapping where channelid='{$this->channelid}' and id='$id'");
		qdb :: Query("delete from " . TABLEPRE . "comment where channelid='{$this->channelid}' and id='$id'");
		qdb :: Query("delete from " . TABLEPRE . "comment_view where channelid='{$this->channelid}' and id='$id'");
		qdb :: Query("delete from " . TABLEPRE . "related  where cid='{$this->channelid}-{$id}'");
		if ($this -> has_extend) qdb :: Query("delete from " . TABLEPRE . "extend_data_{$this->channelselect}  where channelid='{$this->channelid}' and id='{$id}'");
		qbase :: get_object('content', $this -> channelid, $id) -> clear();
		qbase :: get_object('event', $this -> mod, array('channelid' => $this -> channelid, 'id' => $id, 'action' => 'del', 'data' => $data), true);
		return $data;
	} 

	function get_item($id) {
		$data = qdb :: QueryFirst("SELECT * FROM {$this->table} WHERE `id` = '{$id}' LIMIT 1");
		if(!empty($data)){
			$data['status'] = 0;
		}else{
			$tp = qdb::queryAlone("select data from " . TABLEPRE . "recycle where id = '{$id}' and channelid = '{$this->channelid}' ");
			if(!$tp) return array();
			$data = @unserialize($tp);
		}
		$pay = $this -> get_item_pay($id);
		if($data['extdata']){
			$temp = @unserialize($data['extdata']);
			$data['extdata'] = array();
			if(is_array($temp)){
				foreach($temp as $key=>$val){
					$data['extdata'][] = array(
						'key'=>substr($key, 4),
						'value'=>$val
					);
				}
			}
		}else{
			$data['extdata'] = array();
		}
		return array_merge($data, $pay);
	} 

	function full_search(){

	}

	function update_item($id, $item) {
		if (empty($item)) return;
		$sql = qbase :: createSql($this -> table, $item, 'update') . " where id = '{$id}' ";
		qdb :: query($sql);
	} 

	function get_item_pay($id) {
		$pay = qdb :: QueryFirst("SELECT requirepoint, read_purview, pay_type, read_last FROM " . TABLEPRE . "pay_mapping WHERE channelid = '{$this->channelid}' and  `id` = '{$id}' LIMIT 1");
		if (empty($pay)) {
			$pay = array(
				'requirepoint' => 0,
				'read_purview' => 0,
				'pay_type' => 0,
				'read_last' => 0,
			);
		} 
		return $pay;
	} 

	function del_item($pid) {
		$check = qdb :: fetchAll("SHOW TABLES like '{$this->table_extend}'");
		$data = array();
		if (count($check) > 0) {
			$data = qdb :: QueryFirst("select * from {$this->table_extend} where pid='$pid'");
			if (!empty($data)) qdb :: Query("delete from {$this->table_extend} where pid='$pid'");
		} 
		return $data;
	} 

	function get_related($cid) {
		return getRelatedByMap($cid);
	} 

	function related($id, $sid = 0, $action = 'add') {
		global $channel;
		if ($sid === 0) $sid = $this -> channelid;
		$cid = "{$sid}-{$id}";
		$related_complete = intval($_POST['related_complete']);
		if (!$related_complete) return;
		$related = $_POST['related'];
		if (empty($related)) $related = array();
		foreach(array('id', 'aid', 'subject') as $field) {
			if (empty($related[$field])) $related[$field] = array();
		} 
		if ($action == 'add' && empty($related['id'])) return;
		$aids = array();
		$old_aids = array();
		foreach($related['aid'] as $aid) {
			$aid = intval($aid);
			if ($aid < 1) continue;
			$aids[] = $aid;
		} 
		$temp = qdb :: fetchAll("select aid from " . TABLEPRE . "related where cid = '{$cid}' order by aid asc");
		foreach($temp as $aid) {
			$old_aids[] = $aid['aid'];
		} 

		$new_aids = array_diff($aids, $old_aids);
		$delete_aids = array_diff($old_aids, $aids);
		$update_aids = array_intersect($aids, $old_aids);

		foreach($delete_aids as $aid) {
			qdb :: query("delete from " . TABLEPRE . "related where aid = '{$aid}' and cid = '{$cid}'");
		} 
		if (is_array($related['subject'])) {
			foreach($related['subject'] as $key => $title) {
				if (empty($related['id'][$key]) || !is_array($related['id'][$key])) continue;
				$ids_temp = array();
				foreach($related['id'][$key] as $v) {
					$temp = explode("-", $v);
					$c = intval($temp[0]);
					$i = intval($temp[1]);
					if (!isset($channel[$c])) continue;
					$ids_temp[] = "{$c}-{$i}";
				} 
				$ids_temp = array_unique($ids_temp);
				if (empty($ids_temp)) continue;
				$ids = implode(',', $ids_temp);
				$aid = intval($related['aid'][$key]);
				$ordering = intval($related['ordering'][$key]);
				if ($aid == 0 || in_array($aid, $new_aids)) {
					qdb :: query("insert into " . TABLEPRE . "related (cid, title, ids, ordering) values ('{$cid}','{$title}','{$ids}', '{$ordering}')");
				} else if ($aid > 0 && in_array($aid, $update_aids)) {
					qdb :: query("update " . TABLEPRE . "related set title = '{$title}' , ids = '{$ids}', ordering = '{$ordering}' where aid = '{$aid}' and cid = '{$cid}' ");
				} 
			} 
		} 
	} 

	function getCid($id, $field = 'cid') {
		$data = $this -> getInfo($id);
		return $data[$field];
	} 

	function getAllSpecial() {
		return qdb :: fetchAll("select `specialid`,`specialname` from `" . TABLEPRE . "special` where `channelid`='{$this->channelid}'");
	} 

	function delSpecial($id, $sid) {
		$data = $this -> getInfo($id);
		purviewCheck($data['cid'], 'category');
		qdb :: Query("delete from " . TABLEPRE . "special_mapping where aid='$id' and channelid = '{$this->channelid}' and sid='$val' limit 1");
	} 

	function manage($ids) {
		$id = is_array($ids) ? implode(',', $ids) : $ids;
		if (empty($id)) return array();
		$data = qdb :: fetchAll("select m.channelid, m.subject , m.id, m.dateline, m.pay, m.filename, m.ver, m.thumb, c.cid, c.category_html, c.html, c.pur_browse, c.ciddir, c.title from {$this->table} as m left join " . TABLEPRE . "acategory as c on m.cid = c.cid where m.id in ({$id}) order by m.id desc");
		foreach($data as &$v) {
			$iif = !($v['html'] || $v['pur_browse'] || $v['pay']);
			$v['url'] = qbase :: GetHtmlDirType($v['channelid'], $v['id'], $v['ciddir'], $v['dateline'], $v['filename'], $v['ver'], $iif);
			$v['parent_url'] = qbase :: GetCidHtmlDirType($v['channelid'], $v['cid'], $v['ciddir'], $v['cfilename'], !($v['category_html'] || $v['pur_browse']));
		} 
		return $data;
	} 

	function update($id, $field, $val) {
		if (is_array($id)) {
			foreach($id as $v) {
				$this -> update($v, $field, $val);
			} 
		} else {
			qdb :: Query("update {$this->table} set {$field} = '{$val}' where id='{$id}'");
			qbase :: get_object('content', $this -> channelid, $id) -> clear();
		} 
	} 

	/**
	* change every links to short
	*/
	function short_url(&$message) {
		$match =qbase::get_object('links')->get_all_links($message);
		$data = array(array(), array(), array());
		foreach($match as $key => $val) {
			if (in_array($val, $data[0])) continue;
			if (!in_array(substr($val, 0, 3), array('htt', 'ftp'))) continue;
			$param = @parse_url($val);
			if (empty($param['host']) || empty($param['scheme'])) continue;
			$val = addslashes(preg_replace("/\"|\\\\|\'/i", "", $val));
			$code = qdb :: queryAlone("select code from " . TABLEPRE . "url where url = '{$val}' ");
			if (empty($code)) {
				$code = base_convert(sprintf("%u", crc32($val)), 10, 36);
				$host = qbase :: get_domain($param['host']);
				qdb :: query("replace into " . TABLEPRE . "url (code, host, url, views) values ('{$code}', '{$host}', '{$val}', 0)");
			}
			$data[0][] = $val;
			$data[1][] = W_P . "index.php?ac=url&u=" . urlencode($code);
		} 
		return str_replace($data[0], $data[1], $message);
	} 

	function reset_short_url(&$message) {
		$match =qbase::get_object('links')->get_all_links($message);
		$data = array(array(), array(), array());
		foreach($match as $key => $val) {
			$r = preg_match("/ac=url\&u=([a-z0-9]{1,10})/i", $val, $item);
			if (!$r) $r = preg_match("/ac\-url\-u\-([a-z0-9]{1,10})\.html/i", $val, $item);
			if (!$r) continue;
			$url = qdb :: queryAlone("select url from " . TABLEPRE . "url where code = '{$item[1]}' ");
			if (empty($url)) continue;
			$data[0][] = $val;
			$data[1][] = $url;
		} 
		return str_replace($data[0], $data[1], $message);
	} 

	function html($id) {
		if ($this -> auto == false) return false;
		$data = $this -> getInfo($id);
		$pages = isset($data['page']) ? intval($data['page']) : 1;
		if (!empty($data) && $data['pay'] == 0 && $data['pur_browse'] == 0 && $data['html'] == 0) {
			$html = new html();
			for($i = 1; $i <= $pages; $i++) {
				$url = $this -> getUrl($this -> channelid, $id) . "&page={$i}";
				$html -> createhtmlob($url);
			} 
		} 
	} 

	function delete_html($data) {
		if (!is_array($data) && intval($data) > 0) $data = $this -> getInfo(intval($data));
		if (!is_array($data) || empty($data)) return false;
		$pages = isset($data['page']) ? intval($data['page']) : 1;
		$iif = !($data['html'] || $data['pur_browse'] || $data['pay']);
		clearstatcache();
		for($i = 1;$i <= $pages;$i++) {
			if ($page > 1) $extPage = '_' . $page;
			else $extPage = '';
			for($j = 0;$j <= $data['ver'];$j++) {
				$url = qbase :: GetHtmlDirType($data['channelid'], $data['id'], $data['ciddir'], $data['dateline'], $data['filename'], $j, $iif, $extPage);
				$file = R_P . '/' . $this -> channeldir . '/' . qbase :: cutHtmlUrl($data['channelid'], $url);
				if (is_file($file)) @unlink($file);
			} 
		} 
	} 

	function getInfo($id) {
		$data = $this->get_item($id);
		$data = qdb :: queryFirst("select p.*, c.pur_browse, c.html, c.ciddir from {$this->table} as p left join " . TABLEPRE . "acategory as c on p.cid = c.cid where p.id = '{$id}' ");
		$pay = $this -> get_item_pay($id);
		return empty($data) ? array() : array_merge($data, $pay);
	} 

	function moveToRecycle($id, $param){
		if(is_array($id)){
			foreach($id as $vid){
				$this->moveToRecycle($vid, $param);
			}
			return true;
		}
		if(empty($param)) return false;
		$item = qdb::queryFirst("select * from {$this->table} where id = '{$id}'");
		if(empty($item)){
			$tp = qdb::queryAlone("select data from " . TABLEPRE . "recycle where id = '{$id}' and channelid = '{$this->channelid}' ");
			if(!$tp) return false;
			$item = @unserialize($tp);
		}else{
			$item['status'] = 0;
		}
		if(empty($item)) return false;

		foreach($param as $key=>$val){
			$item[$key] = $val;
		}
		if($param['status'] == 0){//move back
			unset($item['status'], $item['recycle']);
			$item = qbase::saddslashes($item);
			$sql = qbase::createSql($this->table, $item, 'replace');
			$id = qdb::query($sql, true);
			qdb::query("delete from " . TABLEPRE . "recycle where id = '{$id}' and channelid = '{$this->channelid}'");
			return false;
		}else{
			$temp = array();
			$temp['id'] = $item['id'];
			$temp['cid'] = $item['cid'];
			$temp['uid'] = $item['uid'];
			$temp['channelid'] = $item['channelid'];
			$temp['is_comend'] = $item['is_comend'];
			$temp['status'] = $item['status'];
			$temp['filter'] = $item['filter'];
			$temp['data'] = addslashes(serialize($item));
			$sql = qbase::createSql(TABLEPRE . "recycle", $temp, 'replace');
			$aid = qdb::query($sql, true);
			if($aid > 0){
				qdb::query("delete from {$this->table} where id = '{$id}' ");
				return true;
			}
			return false;
		}
	}

	function autoFilename($subject){
		global $_SET;
		$word = trim(implode('', qbase::splitWord($subject)));
		if(intval($_SET['google_filename'])){
			$en = qbase::load("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=". urlencode(qbase::iconvs($word, 'utf-8', $_SET['def_charset'])) ."&langpair=zh|en", 0, '','', false, '', 5);
			$en_arr = qbase::jsondecode($en);
			if(intval($en_arr['responseStatus']) == 200){
				$name = trim($en_arr['responseData']['translatedText']) ? formatFileName(str_replace(" ", $_SET['auto_filename_split'], trim($en_arr['responseData']['translatedText']))) : trim(c($word));
				return str_replace($_SET['auto_filename_split'] . $_SET['auto_filename_split'], $_SET['auto_filename_split'], $name);
			}else{
				return formatFileName(trim(c($word)));
			}
		}else{
			return formatFileName(trim(c($word)));
		}
	}

	function getUrl($channelid, $id) {
		global $_MODULE;
		return W_P . 'index.php?mod=' . $channelid . "&ac={$_MODULE[$this->channelselect]['user_content_php']}&id={$id}";
	} 

	function getExtend($channelselect) {
		return qdb :: fetchAll("select * from " . TABLEPRE . "extend where id = '{$channelselect}' order by ordering desc");
	} 

	function getExtendData($channelid, $id) {
		global $_MODULE;
		if (!$this -> has_extend) return array();
		$data = qdb :: queryFirst("select * from " . TABLEPRE . "extend_data_{$this->channelselect} where channelid = '{$channelid}' and id = '{$id}' ");
		unset($data['channelid'], $data['id']);
		return empty($data) ? array() : $data;
	} 

	function _category_num($cid, $param) {
		qdb :: query("update " . TABLEPRE . "acategory set cnum = cnum + {$param} where cid = '{$cid}' ");
		cache :: delete('cid_' . $cid);
	} 

	function addVote() {
		global $_SET, $admin_info, $user_info;
		$is_vote = intval($_POST['is_vote']);
		if ($is_vote > 0) {
			$vote_subject = trim($_POST['vote_subject']);
			$option = $_POST['option'];
			$number = $_POST['number'];
			$type = intval($_POST['type']);
			$totime = $_POST['totime'];
			$dateline = $_SERVER['REQUEST_TIME'];
			$username = $admin_info['username'] ? $admin_info['username'] : $user_info['username'];
			if (strpos($totime, "-")) {
				$time = explode("-", $totime);
				$totime = mktime(0, 0, 0, $time[1], $time[2], $time[0]);
			} else {
				$totime = 0;
			} 
			if (strlen($vote_subject) < 4 || empty($option[0])) return 0;

			$query = "insert into `" . TABLEPRE . "votesubject`(subject,type,inputer,addtime,totime,passed) values('$vote_subject','$type','$username','$dateline','$totime',1)";
			$voteid = qdb :: query($query, true);

			if ($voteid > 0) {
				for($i = 0; $i < count($option); $i++) {
					if (!$number[$i]) $number[$i] = 0;
					qdb :: query("insert into `" . TABLEPRE . "voteoption` (`voteid`,`voteoption`,`number`) values('$voteid','" . $option[$i] . "','" . $number[$i] . "')");
				} 
				$html = new html(R_P . "/support/vote/");
				$html -> createhtml($_SET['weburl'] . $_SET['webpath'] . "admin/plugin/vote/showvote.php?id=" . $voteid, "vote_" . $voteid . ".js");
				return $voteid;
			} else {
				return 0;
			} 
		} else {
			return 0;
		} 
	} 

	function delVote($voteid) {
		global $db, $tablepre;
		if (!$voteid) return false;
		qdb :: query("delete from `" . TABLEPRE . "votesubject` where voteid in ($voteid)");
		qdb :: query("delete from `" . TABLEPRE . "voteoption` where voteid in ($voteid)");
		qdb :: query("delete from `" . TABLEPRE . "voteip` where voteid in ($voteid)");
		if (strpos($voteid, ',')) {
			$voteids = explode(',', $voteid);
		} else {
			$voteids[0] = $voteid;
		} 
	} 
} 

?>