<?php


@$vercode=''; //此处取值请自行修改

if(isset($_POST['vercode'])){
if ($_POST['vercode']!=$vercode){
 echo("[err]invalid vercode[/err]");
 exit();
}
}

define('APPTYPEID', 4);
define('CURSCRIPT', 'portal');

require './source/class/class_core.php';
require './source/function/function_home.php';
require './source/function/function_portal.php';

$discuz = & discuz_core::instance();


$cachelist = array('blockclass', 'portalcategory');
$discuz->cachelist = $cachelist;



$discuz->init();
//zzcity add
$member = array();
$member = DB::fetch_first("SELECT * FROM ".DB::table('common_member')." WHERE username='".$_POST['username']."'");
$cookietime=$_G['gp_cookietime'] ? 2592000 : 0;
 $_G['uid'] = $member['uid'];
 $_G['username'] = $member['username'];
 $_G['adminid'] = $member['adminid'];
 $_G['groupid'] = $member['groupid'];
 $_G['formhash'] = formhash();
 $_G['session']['invisible'] = getuserprofile('invisible');
 $_G['member'] = $member;
 $_G['core']->session->isnew = 1;

 dsetcookie('auth', authcode("{$member['password']}\t{$member['uid']}", 'ENCODE'), $cookietime, 1, true);
 dsetcookie('loginuser');
 dsetcookie('activationauth');
 dsetcookie('pmnum');
$discuz->_init_user();

//zzcity

$_GET['mod']='portalcp'; //zzcity add
$_GET['ac']='article';//zzcity add

if(empty($_GET['mod']) || !in_array($_GET['mod'], array('list', 'view', 'comment', 'portalcp', 'topic', 'attachment'))) $_GET['mod'] = 'index';

define('CURMODULE', $_GET['mod']);
//runhooks();

//require_once libfile('portal/'.$_GET['mod'], 'module');
//替换source/module/portal/portal_portalcp.php开始
//if(!defined('IN_DISCUZ')) {
// exit('Access Denied');
//}

$ac = in_array($_GET['ac'], array('comment', 'article', 'block', 'portalblock', 'topic', 'diy', 'upload', 'category'))?$_GET['ac']:'index';
if(empty($ac)) {
 showmessage('没有正确指定操作');
}
require_once libfile('function/portalcp');

//require_once libfile('portalcp/'.$ac, 'include');
//替换source/include/portalcp/portal_article.php开始



/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: portalcp_article.php 9631 2010-04-30 07:55:17Z zhangguosheng $
 */

//if(!defined('IN_DISCUZ')) {
// exit('Access Denied');
//}

$op = in_array($_GET['op'], array('addpage', 'edit', 'delpage', 'delete', 'related')) ? $_GET['op'] : 'add';
$aid = intval($_G['gp_aid']);
$seccodecheck = $_G['setting']['seccodestatus'] & 4;
$secqaacheck = $_G['setting']['secqaa']['status'] & 2;

$allowmanage = $allowadd = 0;
//if($_G['group']['allowpostarticle'] || $_G['group']['allowmanagearticle']) {
 $allowadd = 1;//发布权限
//}

$article = $article_content = array();
//if($aid) {
// $query = DB::query("SELECT * FROM ".DB::table('portal_article_title')." WHERE aid='$aid'");
// $article = DB::fetch($query);
// if(!$article) {
//  showmessage('article_not_exist');
// }
// if($_G['group']['allowmanagearticle'] || ($_G['group']['allowpostarticle'] && $article['uid'] == $_G['uid'])) {
//  $allowmanage = 1;//管理权限
// }
//}

//if(submitcheck("articlesubmit", 0, $seccodecheck, $secqaacheck)) {

// if(($aid && !$allowmanage) || (!$aid && !$allowadd)) {
//  showmessage('article_edit_nopermission');
// }

 $_POST['title'] = getstr(trim($_POST['title']), 80, 1, 1, 1);
 if(strlen($_POST['title']) < 1) {
//  showmessage('title_not_too_little');
  exit('[err]title_not_too_little[/err]');
 }

 if(empty($_POST['summary'])) $_POST['summary'] = preg_replace("/(\s|###NextPage###)+/", ' ', $_POST['content']);
 $summary = portalcp_get_summary($_POST['summary']);
 $prename = getstr(dhtmlspecialchars($_POST['prename']), 255, 1, 1, 1);

 $_G['gp_author'] = dhtmlspecialchars($_G['gp_author']);
 $_G['gp_from'] = dhtmlspecialchars($_G['gp_from']);
 $_G['gp_fromurl'] = dhtmlspecialchars($_G['gp_fromurl']);
 $_G['gp_shorttitle'] = getstr(trim(dhtmlspecialchars($_G['gp_shorttitle'])), 80, 1, 1, 1);
 $setarr = array(
  'title' => $_POST['title'],
  'shorttitle' => $_G['gp_shorttitle'],
  'author' => $_G['gp_author'],
  'from' => $_G['gp_from'],
  'fromurl' => $_G['gp_fromurl'],
  'url' => $_POST['url'],
  'summary' => $summary,
  'prename' => $prename,
  'preurl' => $_POST['preurl'],
  'catid' => intval($_POST['catid'])
 );

 if(empty($setarr['catid'])) {
//  showmessage('请选择一个合适的系统分类');
  exit('[err]请选择一个合适的系统分类[/err]');
 }

// if($_FILES['pic']) {
//  if($files = pic_upload($_FILES['pic'], 'portal', 300, 300, 2)) {
//   $setarr['pic'] = $files['pic'];
//   $setarr['thumb'] = $files['thumb'];
//   $setarr['remote'] = $files['remote'];
//  }
//  if($setarr['pic'] && $article['pic']) {
//   pic_delete($article['pic'], 'portal', $article['thumb'], $article['remote']);
//  }
// }

   //zzcity add
   function zzfileext($filename) {
    return strtolower(substr(strrchr($filename, '.'), 1, 10));
   }
   //
   function zzis_image_ext($ext) {
    static $imgext  = array('jpg', 'jpeg');
    return in_array($ext, $imgext) ? 1 : 0;
   }
   
   $ashowurl =  empty($_POST['ashowurl']) ? "/data/attachment/portal" : trim($_POST['ashowurl']);
   
   $ashowurl = substr($ashowurl,-1)=="/" ? $ashowurl : $ashowurl.'/';
   if(!empty($_POST['pic'])){
    $setarr['pic'] = str_replace($ashowurl,'',$_POST['pic']);    
   }else{
    $attachlist=explode(',',str_replace($ashowurl,'',$_POST['etattachs'])); 
    for($i=0; $i<count($attachlist); $i++) {
     if(zzis_image_ext(zzfileext($attachlist[$i]))){
      $setarr['pic']=$attachlist[$i];
      break;
     }
    }      
   }
   
   
   if(!empty($setarr['pic']) && !getglobal('setting/ftp/on')){
    require_once libfile('class/image');
    $image = new image();
    
    if($image->Thumb(getglobal('setting/attachdir').'./portal/'.$setarr['pic'], '', 300, 300, 2)) {
     $setarr['thumb'] = 1;
    }

   }
   if(getglobal('setting/ftp/on')) {
    $setarr['remote'] = 1;
   }
     
   //zzcity
 if(empty($article)) {
  $setarr['uid'] = $_G['uid'];
  $setarr['username'] = $_G['username'];
  $setarr['dateline'] = $_G['timestamp'];
  $setarr['id'] = intval($_POST['id']);
  if($setarr['id']) $setarr['idtype'] = $_POST['idtype']=='blogid'?'blogid':'tid';
  $aid = DB::insert('portal_article_title', $setarr, 1);

  DB::query('UPDATE '.DB::table('portal_category')." SET articles=articles+1 WHERE catid = '$setarr[catid]'");
 } else {
  DB::update('portal_article_title', $setarr, array('aid' => $aid));
 }

 $cid = intval($_POST['cid']);
 if($cid) {
  $query = DB::query("SELECT * FROM ".DB::table('portal_article_content')." WHERE cid='$cid' AND aid='$aid'");
  $article_content = DB::fetch($query);
 }

 $content = getstr($_POST['content'], 0, 1, 1, 1, 0, 1);
 $contents = explode('###NextPage###', $content);
 $content_count = count($contents);

 $pageorder = intval($_POST['pageorder']);

 if($pageorder>0) {
  $startorder = $pageorder - 1;
  $pageorder = DB::result(DB::query("SELECT pageorder FROM ".DB::table('portal_article_content')." WHERE aid='$aid' ORDER BY pageorder LIMIT $startorder, 1"), 0);

  if($article_content && $article_content['pageorder'] == $pageorder) {
   $content_count = $content_count - 1;
  }
  if($content_count > 0) {
   DB::query('UPDATE '.DB::table('portal_article_content')." SET pageorder = pageorder+$content_count WHERE aid='$aid' AND pageorder>='$pageorder'");
  }
 } else {
  $pageorder = DB::result(DB::query("SELECT MAX(pageorder) FROM ".DB::table('portal_article_content')." WHERE aid='$aid'"), 0);
  $pageorder = $pageorder + 1;
 }

 if($article_content) {
  $setarr = array(
   'content' => trim($contents[0]),
   'pageorder' => $pageorder,
   'dateline' => $_G['timestamp']
  );
  DB::update('portal_article_content', $setarr, array('cid'=>$cid));
  unset($contents[0]);
 }

 if($contents) {
  $inserts = array();
  foreach ($contents as $key => $value) {
   $value = trim($value);
   $inserts[] = "('$aid', '$value', '".($pageorder+$key)."', '$_G[timestamp]')";
  }
  DB::query("INSERT INTO ".DB::table('portal_article_content')."
   (aid, content, pageorder, dateline)
   VALUES ".implode(',', $inserts));

  DB::query('UPDATE '.DB::table('portal_article_title')." SET contents = contents+".count($inserts)." WHERE aid='$aid'");
 }

 $newaids = array();
 $_POST['attach_ids'] = explode(',', $_POST['attach_ids']);
 foreach ($_POST['attach_ids'] as $newaid) {
  $newaid = intval($newaid);
  if($newaid) $newaids[$newaid] = $newaid;
 }
 if($newaids) {
  DB::update('portal_attachment', array('aid'=>$aid), "attachid IN (".dimplode($newaids).") AND aid='0'");
 }


 if($_POST['raids']) {
  DB::query("DELETE FROM ".DB::table('portal_article_related')." WHERE aid='$aid' OR raid='$aid'");

  $replaces = array();
  $query = DB::query("SELECT aid FROM ".DB::table('portal_article_title')." WHERE aid IN (".dimplode($_POST['raids']).")");
  while ($value = DB::fetch($query)) {
   if($value['aid'] != $aid) {
    $replaces[] = "('$aid', '$value[aid]')";
    $replaces[] = "('$value[aid]', '$aid')";
   }
  }
  if($replaces) {
   DB::query("REPLACE INTO ".DB::table('portal_article_related')." (aid,raid) VALUES ".implode(',', $replaces));
  }
 }

 if($_G['gp_from_idtype'] && $_G['gp_from_id']) {

  $id = intval($_G['gp_from_id']);
  $notify = array();
  switch ($_G['gp_from_idtype']) {
   case 'blogid':
    $blog = DB::fetch_first("SELECT * FROM ".DB::table('home_blog')." WHERE blogid='$id'");
    if(!empty($blog)) {
     $notify = array(
      'url' => "home.php?mod=space&do=blog&id=$id",
      'subject' => $blog['subject']
     );
     $touid = $blog['uid'];
    }
    break;
   case 'tid':
    $thread = DB::fetch_first("SELECT * FROM ".DB::table('forum_thread')." WHERE tid='$id'");
    if(!empty($thread)) {
     $notify = array(
      'url' => "forum.php?mod=viewthread&tid=$id",
      'subject' => $thread['subject']
     );
     $touid = $thread['authorid'];
    }
    break;
  }
  if(!empty($notify)) {
   $notify['newurl'] = 'portal.php?mod=view&aid='.$aid;
   require_once libfile('function/notification');
   notification_add($touid, 'pusearticle', 'puse_article', $notify, 1);
  }
 }


/* if($_POST['addpage']) {
  $url = 'portal.php?mod=portalcp&ac=article&op=addpage&aid='.$aid;
 } else {
  $url = $_POST['url']?"portal.php?mod=list&catid=$_POST[catid]":'portal.php?mod=view&aid='.$aid;
 }
*/ 
// showmessage('do_success', $url);
 echo('[ok]');

//}

//if ($op == 'delpage') {
//
// if(!$aid || !$allowmanage) {
//  showmessage('article_edit_nopermission');
// }
//
// $pageorder = intval($_GET['pageorder']);
// $aid = intval($_GET['aid']);
// $cid = intval($_GET['cid']);
//
// if($aid && $cid) {
//  $count = DB::result(DB::query("SELECT COUNT(*) FROM ".DB::table('portal_article_content')." WHERE aid='$aid'"), 0);
//  if($count > 1) {
//   DB::query('DELETE FROM '.DB::table('portal_article_content')." WHERE cid='$cid' AND aid='$aid'");
//   DB::query('UPDATE '.DB::table('portal_article_title')." SET contents = contents-1 WHERE aid='$aid'");
//  } else {
//   showmessage('当前为最后一页，不能被删除');
//  }
// }
// showmessage('do_success', "portal.php?mod=portalcp&ac=article&op=edit&quickforward=1&aid=$aid");
//
//} elseif($op == 'delete') {
//
// if(!$aid || !$allowmanage) {
//  showmessage('article_edit_nopermission');
// }
//
// if(submitcheck('deletesubmit')) {
//  include_once libfile('function/delete');
//  $article = deletearticle(array(intval($_POST['aid'])), intval($_POST['optype']));
//  showmessage('article_delete_success', "portal.php?mod=list&catid={$article[0][catid]}");
// }
//
//} elseif($op == 'related') {
//
// $raid = intval($_GET['raid']);
// $ra = array();
// if($raid) {
//  $query = DB::query("SELECT * FROM ".DB::table('portal_article_title')." WHERE aid='$raid'");
//  $ra = DB::fetch($query);
// }
//
//} else {
/*
 if(($aid && !$allowmanage) || (!$aid && !$allowadd)) {
  showmessage('article_edit_nopermission');
 }

 $page = empty($_GET['page'])?1:intval($_GET['page']);
 if($page<1) $page = 1;
 $start = $page-1;

 $pageselect = '';

 $category = array();
 $query = DB::query('SELECT * FROM '.DB::table('portal_category'));
 while($value = DB::fetch($query)) {
  $category[$value[catid]] = $value;
 }

 foreach ($category as $key=>$value) {
  $upid = $value['upid'];
  if($upid) {
   $category[$upid]['children'][] = $key;
   $category[$key]['level'] = 1;
   while($category[$upid]) {
    $category[$key]['level'] += 1;
    $upid = $category[$upid]['upid'];
   }
  } else {
   $category[$key]['level'] = 0;
  }
 }

 $_GET['catid'] = $article['catid'] ? $article['catid'] : ($_GET['catid'] && isset($category[$_GET['catid']]) ? $_GET['catid'] : '');
 $categoryselect = "<select id=\"catid\" name=\"catid\" width=\"120\">";
 $selected = empty($_GET['catid'])?' selected':'';
 $categoryselect .= "<option value=\"0\"$selected>-----</option>";
 foreach ($category as $value) {
  if($value['level'] == 0) {
   $selected = $_GET['catid'] == $value['catid']?' selected':'';
   if ($selected) {echo $selected; echo $value['catid']; echo '<br>';}
   $categoryselect .= "<option value=\"$value[catid]\"{$selected}>$value[catname]</option>";
   if(!$value['children']) {
    continue;
   }
   foreach ($value['children'] as $catid) {// 子分类
    $selected = $_GET['catid'] == $catid?' selected':'';
    $categoryselect .= "<option value=\"{$category[$catid][catid]}\"{$selected}>-- {$category[$catid][catname]}</option>";
    if($category[$catid]['children']) {
     foreach ($category[$catid]['children'] as $catid2) {// 三级分类
      $selected = $_GET['catid'] == $catid2?' selected':'';
      $categoryselect .= "<option value=\"{$category[$catid2][catid]}\"{$selected}>---- {$category[$catid2][catname]}</option>";
     }
    }
   }
  }
 }
 $categoryselect .= "</select>";

 if($article) {

  if($op == 'addpage') {
   $article_content = array();
  } else {
   $query = DB::query("SELECT * FROM ".DB::table('portal_article_content')." WHERE aid='$aid' ORDER BY pageorder LIMIT $start,1");
   $article_content = DB::fetch($query);
  }

  $article['attach_image'] = $article['attach_file'] = '';
  $query = DB::query("SELECT * FROM ".DB::table('portal_attachment')." WHERE aid='$aid' ORDER BY attachid");
  while ($value = DB::fetch($query)) {
   if($value['isimage']) {
    $article['attach_image'] .= get_uploadcontent($value);
   } else {
    $article['attach_file'] .= get_uploadcontent($value);
   }
  }

  if($article['contents'] > 0) {
   $pageselect = '<select name="pageorder">';
   $pageselect .= "<option value=\"0\">末尾</option>";
   for($i=1; $i<=$article['contents']; $i++) {
    $selected = ($op!='addpage' && $page == $i)?' selected':'';
    $pageselect .= "<option value=\"$i\"$selected>$i</option>";
   }
   $pageselect .= '</select>';
  }

  $multi = multi($article['contents'], 1, $page, "portal.php?mod=portalcp&ac=article&aid=$aid");

  if($article['pic']) {
   $article['pic'] = pic_get($article['pic'], 'portal', $article['thumb'], $article['remote'], 1);
  }

  $article['related'] = array();
  if($page < 2 && $op != 'addpage') {
   $query = DB::query("SELECT a.aid,a.title
    FROM ".DB::table('portal_article_related')." r
    LEFT JOIN ".DB::table('portal_article_title')." a ON a.aid=r.raid
    WHERE r.aid='$aid'");
   while ($value = DB::fetch($query)) {
    $article['related'][] = $value;
   }
  }
 }

 $_GET['from_id'] = empty ($_GET['from_id'])?0:intval($_GET['from_id']);
 if($_GET['from_idtype'] != 'blogid') $_GET['from_idtype'] = 'tid';


 $idtypes = array($_GET['from_idtype'] => ' selected');
 if($_GET['from_idtype'] && $_GET['from_id']) {

  $havepush = db::result(db::query("SELECT COUNT(*) FROM ".db::table('portal_article_title')." WHERE id='$_GET[from_id]' AND idtype='$_GET[from_idtype]'"), 0);
  if($havepush) {
   showmessage('已经推送到文章里面了，不能重复推送', '', array(), array('return'=>true));
  }


  switch ($_GET['from_idtype']) {
  case 'blogid':
   $query = DB::query("SELECT b.*, bf.message FROM ".DB::table('home_blog')." b
    LEFT JOIN ".DB::table('home_blogfield')." bf ON bf.blogid=b.blogid
    WHERE b.blogid='$_GET[from_id]'");
   if($blog = DB::fetch($query)) {
    if($blog['friend']) {
     showmessage('当前日志因隐私设置不能进行文章推送操作');
    }
    $article['title'] = getstr($blog['subject'], 0);
    $article['summary'] = portalcp_get_summary($blog['message']);
    $article_content['content'] = dhtmlspecialchars($blog['message']);
   }
   break;
  default:
   $posttable = getposttablebytid($_GET['from_id']);
   $query = DB::query("SELECT t.*, p.* FROM ".DB::table('forum_thread')." t
    LEFT JOIN ".DB::table($posttable)." p ON p.tid=t.tid AND p.first='1'
    WHERE t.tid='$_GET[from_id]'");
   if($thread = DB::fetch($query)) {
    $article['title'] = $thread['subject'];

    $language = lang('forum/misc');
    $thread['message'] = preg_replace($language['post_edit_regexp'], '', $thread['message']);

    require_once libfile('function/discuzcode');
    $thread['message'] = discuzcode($thread['message'], $thread['smileyoff'], $thread['bbcodeoff'], $thread['htmlon']);

    $article['summary'] = portalcp_get_summary($thread['message']);
    $article_content['content'] = dhtmlspecialchars($thread['message']);

    $query = DB::query("SELECT aid FROM ".DB::table('forum_attachment')." WHERE pid='$thread[pid]'");
    while($attach = DB::fetch($query)) {
     $attachcode = '[attach]'.$attach['aid'].'[/attach]';
     if(!strexists($article_content['content'], $attachcode)) {
      $article_content['content'] .= '<br /><br />'.$attachcode;
     }
    }
   }
   break;
  }
 }
*/
//}

//include_once template("portal/portalcp_article");

function portalcp_get_summary($message) {
 $message = preg_replace(array("/\[attach\].*?\[\/attach\]/", "/\&[a-z]+\;/i"), '', $message);
 $message = preg_replace("/\[.*?\]/", '', $message);
 $message = getstr(strip_tags($message), 200);
 return $message;
}




//替换source/include/portalcp/portal_article.php结束
//替换source/module/portal/portal_portalcp.php结束
?>
