|
接口把已有的tag随机加到文章中,该改什么地方“
<?php
/*********密码验证****************************/
$password='jiekou';
if($password!=$_POST['pw']) exit('验证密码错误!');
//note 加载数据库配置文件
include('wp-config.php');
$dbhost=DB_HOST;
$dbuser=DB_USER;
$dbname=DB_NAME;
$dbpw=DB_PASSWORD;
$dbcharset = 'utf8';
$charset = 'utf-8';
$tablepre=$table_prefix;
/****以下代码非专业人员不建议修改***************/
set_magic_quotes_runtime(0);
define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
$timestamp=time();
if(PHP_VERSION < '4.1.0') {
$_GET = &$HTTP_GET_VARS;
$_POST = &$HTTP_POST_VARS;
$_COOKIE = &$HTTP_COOKIE_VARS;
$_SERVER = &$HTTP_SERVER_VARS;
$_ENV = &$HTTP_ENV_VARS;
$_FILES = &$HTTP_POST_FILES;
}
if (isset($_REQUEST['GLOBALS']) OR isset($_FILES['GLOBALS'])) {
exit('Request tainting attempted.');
}
foreach(array('_COOKIE', '_POST', '_GET') as $_request) {
foreach($_request as $_key => $_value) {
$_key{0} != '_' && $_key = daddslashes($_value);
}
}
if (!MAGIC_QUOTES_GPC && $_FILES) {
$_FILES = daddslashes($_FILES);
}
/*****************以下是核心的处理代码******************/
extract($_POST,EXTR_OVERWRITE);// 获取POST过来的参数
$db = new db($dbhost,$dbuser,$dbpw,$dbname,$charset,$dbcharset);
if(empty($actiontype) || !in_array($actiontype, array('getcat', 'addcat', 'addartices'))){
exit("参数错误");
}
//note 获取所有分类
if($actiontype == 'getcat'){
$sql = "SELECT tt.term_id,tt.term_taxonomy_id,t.name,tt.term_id,t.term_id,tt.taxonomy,tt.parent from ".$tablepre."terms t,".$tablepre."term_taxonomy tt where t.term_id=tt.term_id AND tt.taxonomy='category' ";
$cateinfo = $db->get_array($sql);// 获取所有分类
$ids = maketree($cateinfo,0,'');
echo substr($ids, 0, -1);
exit;
}
//note 添加分类
if($actiontype == 'addcat'){
$namestring = iconv("gb2312", "UTF-8", $namestring);
if(empty($namestring)) exit('分类名不能为空');
$parent = $parent == '' ? 0 : $parent;
if($parent){
$sql = "SELECT term_id FROM {$tablepre}term_taxonomy WHERE term_taxonomy_id='{$parent}' LIMIT 1";
$parent_taxonomy = $db->fetch_first($sql);
$parentid = $parent_taxonomy['term_id'];
}else{
$parentid = 0;
}
$name_arr = explode(",",$namestring);
foreach ($name_arr as $key => $name){
if(!empty($name)){
//note 判断分类是否已存在
$sql = "SELECT term_id FROM {$tablepre}terms WHERE name='{$name}' LIMIT 1";
$term_info = $db->fetch_first($sql);
if($term_info['term_id']){
echo '此名已存在:' . $name . '<br/>';
continue;
}
$slug = urlencode($name);
$sql = "INSERT INTO {$tablepre}terms SET name='{$name}',slug='{$slug}'";
$result = $db->query($sql);
$term_id = $db->insert_id();
$sql = "INSERT INTO {$tablepre}term_taxonomy SET term_id='{$term_id}',taxonomy='category',parent='{$parentid}'";
$result = $db->query($sql);
if($result){
echo '添加分类<' . $name . '>成功<br/>';
continue;
}else{
echo '添加分类<' . $name . '>失败<br/>';
continue;
}
}
}
}
//note 发布文章
if($actiontype == 'addartices'){
$title = iconv("gb2312", "UTF-8", $title);
$content = iconv("gb2312", "UTF-8", $content);
$username = iconv("gb2312", "UTF-8", $username);
$post_excerpt = iconv("gb2312", "UTF-8", $excerpt);
$tags = iconv("gb2312", "UTF-8", $tags);
$post_mime_type = iconv("gb2312", "UTF-8", $mime_type);
if(!$title){
exit('标题不能为空');
}
if(!$content){
exit('内容不能为空');
}
//note 获得所有文章分类
$sql = "SELECT term_taxonomy_id FROM {$tablepre}term_taxonomy";
$catarr = $db->get_array($sql);
$catid_arr = array();
foreach ($catarr as $v){
$catid_arr[] = $v['term_taxonomy_id'];
}
//note 如果没有发送分类或分类错误,则随机发布
if(empty($catid) || !in_array($catid, $catid_arr)){
$key = mt_rand(0,count($catid_arr)-1);
$catid = $catid_arr[$key];
}
//note 随机分配发布用户
$userinfo=$db->get_array("Select ID from {$tablepre}users");
$user_key = mt_rand(0,count($userinfo)-1);
$uid = $userinfo[$user_key]['ID'];
//note 摘要
if(empty($post_excerpt)){
$post_excerpt = substr(strip_tags($content), 0, 100);
}
//note 发布文章
$post_date=date("Y-m-d H:i:s");
$name = urlencode($title);
$sql="INSERT INTO `".$tablepre."posts` ( `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`,`post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES ('$uid', '$post_date', '$post_date', '$content', '$title', '$post_excerpt', 'publish', 'open', 'open', '', '$name', '', '', '$post_date', '$post_date', '$post_content_filtered', 0, '', '0', 'post', '$post_mime_type', '0')";
$query=$db->query($sql);
$postid=$db->insert_id();
$sqledit="INSERT INTO `".$tablepre."postmeta` (post_id ,meta_key ,meta_value ) VALUES ($postid,'_edit_lock','$timestamp'),($postid,'_edit_last',1)";
$query2=$db->query($sqledit);
$sqlcid="INSERT INTO `".$tablepre."term_relationships` (object_id ,term_taxonomy_id ) VALUES ($postid,'$catid')";
$result=$db->query($sqlcid);
//note 更改分类的count
$sql_update="UPDATE {$tablepre}term_taxonomy SET count=count+1 WHERE term_taxonomy_id='{$catid}'";
$result=$db->query($sql_update);
//note 处理文章标签
if($tags){
$tagss= array_unique(explode(",",$tags));
foreach($tagss as $var){
$ssql="SELECT * from ".$tablepre."terms where name='$var' ";
$squery=$db->fetch_first($ssql);
if($squery){
$tagidss=$db->fetch_first($ssql);
$tagids=$tagidss['term_id'];
$gettagid="SELECT term_taxonomy_id,term_id from ".$tablepre."term_taxonomy where term_id='$tagids' ";
$gettag=$db->fetch_first($gettagid);
$tagid=$gettag['term_taxonomy_id'];
$sqltag="INSERT INTO `".$tablepre."term_relationships` (object_id ,term_taxonomy_id ) VALUES ($postid,'$tagid')";
$result=$db->query($sqltag);
}else{
$uc=urlencode($var);
$addtag="INSERT INTO `".$tablepre."terms` (name,slug) VALUES ('$var','$uc')";
$addquery=$db->query($addtag);
$addtagid=$db->insert_id($addtag);
$addterm="INSERT INTO `".$tablepre."term_taxonomy` (term_id,taxonomy) VALUES ('$addtagid','post_tag')";
$termquery=$db->query($addterm);
$tagids=$db->insert_id();
$sqltag="INSERT INTO `".$tablepre."term_relationships` (object_id ,term_taxonomy_id ) VALUES ($postid,'$tagids')";
$result=$db->query($sqltag);
}
}
}
if($result){
exit('发布成功');
}else{
exit('发布失败');
}
}
exit;
/***生成目录的一个遍历算法***/
function maketree($ar,$id,$pre)
{
$ids = '';
foreach($ar as $k=>$v){
$fup=$v['parent'];
$name=$v['name'];
$fid=$v['term_id'];
$term_taxonomy_id = $v['term_taxonomy_id'];
if($fup==$id)
{
$ids.=$term_taxonomy_id . '|' . $pre . $name . '□';
foreach($ar as $kk=>$vv)
{
$pp=$vv['parent'];
if($pp==$fid)
{
$ids.=maketree($ar,$fid,$pre."--");
break;
}
}
}
}
return $ids;
}
/****************************以下为公共类库及函数库******************************/
function daddslashes($string, $force = 0) {
!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
if(!MAGIC_QUOTES_GPC || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val, $force);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
/*当前为数据库操作类库*/
class db {
var $mlink;
function db($dbhost, $dbuser, $dbpw, $dbname = '',$charset='gbk',$dbcharset='gbk', $pconnect=0){
if($pconnect){
if(!$this->mlink = @mysql_pconnect($dbhost, $dbuser, $dbpw)){
$this->halt('Can not connect to MySQL');
}
} else {
if(!$this->mlink = @mysql_connect($dbhost, $dbuser, $dbpw)){
$this->halt('Can not connect to MySQL');
}
}
if($this->version()>'4.1'){
if('utf-8'==strtolower($dbcharset)){
$dbcharset='utf8';
}
if($dbcharset){
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this->mlink);
}
if($charset){
mysql_query("SET $charset", $this->mlink);
}
if($this->version() > '5.0.1'){
mysql_query("SET sql_mode=''", $this->mlink);
}
}
if($dbname){
mysql_select_db($dbname, $this->mlink);
}
}
function select_db($dbname){
return mysql_select_db($dbname, $this->mlink);
}
function get_array($sql){
$list = array();
$query=$this->query($sql);
while($row=$this->fetch_array($query)){
$list[]=$row;
}
return $list;
}
function fetch_array($query, $result_type = MYSQL_ASSOC){
return (is_resource($query))? mysql_fetch_array($query, $result_type) :false;
}
function result_first($sql){
$query = $this->query($sql);
return $this->result($query, 0);
}
function fetch_first($sql){
$query = $this->query($sql);
return $this->fetch_array($query);
}
function fetch_total($table,$where='1'){
return $this->result_first("SELECT COUNT(*) num FROM ".DB_TABLEPRE."$table WHERE $where");
}
function query($sql, $type = ''){
global $mquerynum;
$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql, $this->mlink)) && $type != 'SILENT'){
$this->halt("MySQL Query Error",'TRUE',$sql);
}
$mquerynum++;
return $query;
}
function affected_rows(){
return mysql_affected_rows($this->mlink);
}
function error(){
return (($this->mlink) ? mysql_error($this->mlink) : mysql_error());
}
function errno(){
return intval(($this->mlink) ? mysql_errno($this->mlink) : mysql_errno());
}
function result($query, $row){
$query = @mysql_result($query, $row);
return $query;
}
function num_rows($query){
$query = mysql_num_rows($query);
return $query;
}
function num_fields($query){
return mysql_num_fields($query);
}
function free_result($query){
return mysql_free_result($query);
}
function insert_id(){
return ($id = mysql_insert_id($this->mlink)) >= 0 ? $id : $this->result($this->query('SELECT last_insert_id()'), 0);
}
function fetch_row($query){
$query = mysql_fetch_row($query);
return $query;
}
function fetch_fields($query){
return mysql_fetch_field($query);
}
function version(){
return mysql_get_server_info($this->mlink);
}
function close(){
return mysql_close($this->mlink);
}
function halt($msg, $debug=true, $sql=''){
@ini_set("date.timezone","Asia/Shanghai");
$output .="<html>\n<head>\n";
$output .="<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$charset."\">\n";
$output .="<title>$msg</title>\n";
$output .="</head>\n<body><table>";
$output .="<b>MySql Error Info</b><table><tr><td width='100px'><b>Message</b></td><td>$msg</td></tr>\n"; www.jnhylcy.com
$output .="<tr><td><b>Time</b></td><td>".date("Y-m-d H:i:s")."<br /></td></tr>\n";
$output .="<tr><td><b>Script</b></td><td> ".$_SERVER['PHP_SELF']."<br /></td></tr>\n\n";
$output .="<tr><td><b>SQL</b></td><td> ".htmlspecialchars($sql)."<br />\n</td></tr><tr><td><b>Error</b></td><td> ".$this->error()."</td></tr><br />\n";
$output .="<tr><td><b>Errno.</b></td><td> ".$this->errno()."</td></tr></table>";
$output .="\n</body></html>";
echo $output;
exit();
}
}
?>
复制代码 |
|