<?php
/*
	[LocoySpider] (C)2005-2010 Lewell Inc.
	火车采集器 PHPCMS 2008SP4 GBK 文章发布接口
	最后更新:2010.07.24 by RQ204
	视频教程请参考:
	DedeCMS: http://video.locoy.com/jiekou/dede/dede56_article.7z
	帝国CMS: http://video.locoy.com/jiekou/ecms/ecms65_article.7z
*/

/********密码验证***********/
$password='123456';				                   //这个密码是登陆验证用的.您需要在模块里设置和这里一样的密码....注意一定需要修改.
if($password!=$_GET['pw']) exit('验证密码错误');   //安全检测,密码不符则退出


/****以下代码非专业人员不建议修改***************/
define('IN_ADMIN', TRUE);
require dirname(__FILE__).'/include/admin/global.func.php';
require dirname(__FILE__).'/include/common.inc.php';
require_once 'log.class.php';
require 'form.class.php';
require 'priv_role.class.php';
require_once 'cache.func.php';
require_once 'version.inc.php';
require PHPCMS_ROOT.'languages/'.LANG.'/phpcms_admin.lang.php';

foreach($CATEGORY as $ct)
{
	if($ct['module']=='phpcms'&&$ct['modelid'] ==1) $cates[]=array('cid'=>$ct['catid'],'pid'=>$ct['parentid'],'cname'=>$ct['catname']);
}

if(!$dosubmit)
{
	echo "<select name='list'>";
	echo maketree($cates,0,'');
	echo '</select>';
	exit();
}

$modelid=1;
$action = 'add';
$catid = isset($info[catid]) ? intval($info[catid]) : 0;
$specialid = isset($specialid) ? intval($specialid) : 0;
if(!isset($forward) && str_exists(HTTP_REFERER, '?')) $forward = HTTP_REFERER;

//error_reporting(E_ALL);
session_start();
require_once 'admin/process.class.php';
require_once 'admin/content.class.php';
require_once 'attachment.class.php';
$mod='phpcms';
$_groupid = 1;
$G[allowpost]=true;
$_SESSION['is_admin'] = 1;
$_username=$username;

$userinfo=$db->get_one("Select userid as uid from ".DB_PRE."member where username='$username'");//print_r($userinfo);
if(!$userinfo) exit("不存在用户名$username");
$uid=$userinfo['uid'];
$_userid=$uid;

$ROLE = cache_read('role.php');
$GROUP = cache_read('member_group.php');
$POS = cache_read('position.php');
$STATUS = cache_read('status.php');
$_roleid = cache_read('admin_role_'.$_userid.'.php');
if(!$_roleid) showmessage('您没有任何角色权限！');
$priv_role = new priv_role();
if(!$priv_role->module()) showmessage('您没有操作权限！');
$_userid=$uid;

if(is_numeric($contentid) && $contentid>0)
{
	$data = $c->get($contentid);
	$catid = $data['catid'];
	$modelid = $CATEGORY[$catid]['modelid'];
}
if(!isset($catid) || !isset($CATEGORY[$catid])) showmessage('缺少 catid 参数!');
extract(cache_read('category_'.$catid.'.php'));

	$allow_manage = $priv_role->check('catid', $catid, 'manage');
	$allow_add = $allow_manage ? true : $priv_role->check('catid', $catid, 'add');
	$allow_check = $allow_manage ? true : $priv_role->check('catid', $catid, 'check');
	$allow_view = $allow_manage ? true : $priv_role->check('catid', $catid, 'view');

	$attachment = new attachment($mod, $catid);
	$p = new process($workflowid);
	$PROCESS = cache_read('process_'.$workflowid.'.php');
	//获取该工作流下面的状态
	$workflow_infos =$p->listinfo("workflowid='$workflowid'");

	$submenu = $allowprocessids = array();
	if($allow_add)
	{
		$submenu[] = array('<font color="red">发布信息</font>', '?mod='.$mod.'&file='.$file.'&action=add&catid='.$catid);
		$submenu[] = array('我发布的信息', '?mod='.$mod.'&file='.$file.'&action=my&catid='.$catid);
	}
	if($allow_check)
	{
		foreach($PROCESS as $pid=>$processname)
		{
			if($priv_role->check('processid', $pid))
			{
				foreach($workflow_infos AS $_w)
				{
					if($pid == $_w['processid'] && $_w['passstatus']==99)
					{
						$allow_manage = 1;
					}
					else
					{
						$allow_manage = 0;
					}
				}
				$allow_processids[] = $pid;
				if($pid==1) $add_status = '&status=3';
				$submenu[] = array($processname, '?mod='.$mod.'&file='.$file.'&action=check&catid='.$catid.'&processid='.$pid.$add_status);
			}
		}
	}
	if($allow_manage)
	{
		$submenu[] = array('管理', '?mod='.$mod.'&file='.$file.'&action=manage&catid='.$catid);
		$submenu[] = array('回收站', '?mod='.$mod.'&file='.$file.'&action=recycle&catid='.$catid);
		$submenu[] = array('碎片', '?mod='.$mod.'&file='.$file.'&action=block&catid='.$catid);
		$submenu[] = array('定时发布', '?mod='.$mod.'&file='.$file.'&action=publish&catid='.$catid);
	}
	elseif($allow_view)
	{
		$submenu[] = array('浏览', '?mod='.$mod.'&file='.$file.'&action=browse&catid='.$catid);
	}
	$submenu[] = array('搜索', '?mod='.$mod.'&file='.$file.'&action=search&catid='.$catid);
	$menu = admin_menu($CATEGORY[$catid]['catname'].' 栏目管理', $submenu);

	if(!isset($processid) || !in_array($processid, $allow_processids)) $processid = $allow_processids[0];

if(!$priv_role->check('catid', $catid, 'add') && !$allow_manage) showmessage('无发布权限！');
if($dosubmit)
{
	//增加判断如果发布时间大于当前时间则设定为定时发布状态98
	$info['status'] = ($status == 2 || $status == 3) ? $status : ($allow_manage ? ($PHPCMS['publish'] && (strtotime($info['inputtime']) > TIME) ? 98 : 99)  : 3);
	$c = new content();
	if(isset($info['inputtime'])) $info['updatetime'] = $info['inputtime'];
	$contentid = $c->add($info,$cat_selected);

	//如果状态为定时发布，文章id作为key，发布时间作为value，写入缓存
	if($info['status']==98) {
		$tmp_publisharr = cache_read('publish.php');
		$tmp_publisharr[$contentid] = strtotime($info['updatetime']);
		$tmp_publisharr[$contentid] = strtotime($info['updatetime']);
		cache_write('publish.php', $tmp_publisharr);
		unset($tmp_publisharr);
	}

	if($contentid) showmessage('发布成功！', '?mod=phpcms&file=content&action=add&catid='.$catid);
}

/***生成目录的一个遍历算法***/
function maketree($ar,$id,$pre)
{
	$ids='';
	foreach($ar as $k=>$v){
		$pid=$v['pid'];
		$cname=$v['cname'];
		$cid=$v['cid'];
		if($pid==$id)
		{
			$ids.="<option value='$cid'>{$pre}{$cname}</option>";
			foreach($ar as $kk=>$vv)
			{
				$pp=$vv['pid'];
				if($pp==$cid)
				{ 
					$ids.=maketree($ar,$cid,$pre."&nbsp;&nbsp;");
					break;
				}
			}
		}
	}
	return $ids;
}
?>