<?php
// $Id: index.php 56 2010-09-20 05:32:35Z yd2004 $

/**
 * 工作目录
 * 建议包含文件，使用绝对路径。
 */
define('DIDA_ROOT', getcwd());

header('Content-Type: text/html; charset=utf-8');

require_once DIDA_ROOT . '/includes/bootstrap.inc';
bootstrap('full');// 程序初始化
/* * 
 *  ?tid=tid&title=title&body=body&tags=aaa,bbb,ccc  
*/
if(!$_REQUEST['tid']){
	$tid = $_REQUEST['pid'];
}else{
	$tid = $_REQUEST['tid'];
}
$tags = str_replace('|||',',',$_REQUEST['tags']);

if($_REQUEST['title'] != '' && $_REQUEST['body'] != ''){
	$module = 'article';
	$article['title'] = $_REQUEST['title'];
	$article['body'] = str_replace('|||', '<hr>', $_REQUEST['body']);
	$article['body'] = preg_replace('/alt=\"(.*?)\"/i', '', $article['body']);
	$article['body'] = preg_replace('/title=\"(.*?)\"/i', '', $article['body']);
	//$article['body'] = stripslashes($article['body']);
	$article['domain'] = strtolower($_REQUEST['domain']);
	$article['description'] = $_REQUEST['description'];
	/*文章正文内链到景点二级域名*/
	$jids = get_dict_data();
	foreach($jids as $key=>$val){
		$article['body'] = preg_replace('/'.$val->title.'/i', '<a href="'.$val->url.'">'.$val->title.'</a>', $article['body'], 3);
		if(strpos($article['body'], $val->title) || strpos('%%%'.$article['title'], $val->title)){
			$arr[$val->nid] = $val->nid;
			$tags1 .= $val->title.',';
		}
	}
	$tags1 = substr($tags1,0,-1);
	if($tags){
		$tags = $tags.','.$tags1;
	}else{
		$tags = $tags1;
	}
	//$article['body'] = preg_replace_callback('/(<img.*?src="|<IMG.*?src=")(.*?)"/ms', 'remote_image', $article['body']);
	$article['format'] = 2;
	$article['uid'] = 1;
	$article['flag'] = 0;
	$article['fid'] = '0';
	$article['click'] = rand(100,500);
	//$article['fields'] = array('jdid' => $arr);
	$article['fields'] = array('lanmu'=>$tid, 'tags'=>$tags, 'litpic'=>$_REQUEST['litpic'], 'jdid' => $arr);
	//print_r($article);exit();
	$function = $module.'_save';
	$count = db_query('SELECT COUNT(*) FROM {'.$module.'} WHERE title = ?', array($article['title']), array('return' => 'column'));
	if(!$count){
		$res = call_user_func($function,$article);
		/*
		$comments = explode('|||', $_REQUEST['comments']);
		if($comments){foreach($comments as $val){
			$comment->uid = rand(1,50);
			$comment->name = '';
			$comment->body = $val;
			$comment->ext_id = $res->nid;
			$comment->ext_id2 = $tid;
			$comment->title = '评论:'.$article['title'];
			$comment->status = 1;
			$comment->format = 1;
			$comment->ext_type = 'article';
			$comment->created = time() + rand(0,86400);
			$comment->updated = $comment->created;
			//echo $comment->ext_id;
			//print_r($comment);exit();
			if($comment->body != ''){
				db_write_record('comment', $comment);
			}
		}}
		*/
		if($res->nid){echo '<script>window.close();</script>';}
	}else{
		echo '重复';
	}
}else{
	echo '没有选择栏目';
}


function remote_image($mat){
	$imgurl = '<img style="display:block;margin:0 auto;" src="/remote_image.php?pic='.base64_encode($mat[2]).'"';
	return $imgurl;
}

/**
 * 获取词组文本
 */
function get_dict_data() {
  $data = jingdian_list('', 0, 140);if($data){foreach($data as $val){
		 $tmp = explode('(', $val->title);
		 $val->title = $tmp[0];
	}}
	return $data;
}

/**
 * 正向最大匹配，如：中华人民共和国，拆分为：中华人民共和国
 * @param (string) $text
 
function get_tags($text) {
  
  $text = preg_replace("/[^\x{4e00}-\x{9fa5}]/u", '。', $text);
  
  $vars = str_split($text, 3);
  $dict = get_dict_data();

  // 字符串长度
  $count = count($vars);
  
  // 词语最大长度
  $max_len = 8;
  
  // 开始位置
  $start = 0;
  
  $keywords = array();
  for ($start; $start < $count; ++$start) {
    
    $i = $start;
    $k = $j = 0;
    $var = '';

    for ($i; $i < $count; $i++, $j++) {
      
      if ($vars[$i] == '。') {
        break;
      }
      
      $var .= $vars[$i];
      
      if (isset($dict[$var])) {
        
        $keywords[$start] = $var;
        $k = $j;
        
      }
      
      if ($j > $max_len) {
        break;
      }
    }
    
    $start += $k;
    
  }
  
  return $keywords ? array_unique($keywords) : false;
}
*/
/*
for($i = 1540; $i < 3800; $i++){
	$article = article_load($i);
	$tags = get_tags($article->body);

	$arr = get_dict_data();

	$tags = array_flip($tags);
	$brr = array_intersect_key($arr,$tags);
	//$text = strtr($article->body,$brr);
	$text = str_replace(array_keys($brr), array_values($brr), $article->body, 10);	
	//db_query("UPDATE {article} SET body = ? WHERE nid = ?", array($text, $i));
	//$text = str_replace(array_values($brr), array_keys($brr), $article->body);	
}
*/