<?php
define('EmpireCMSAdmin', '1');
require('../../class/connect.php'); //�������ݿ������ļ��͹��������ļ�
require('../../class/db_sql.php'); //�������ݿ�����ļ�
require('../../class/functions.php');
$link = db_connect(); //����MYSQL
$empire = new mysqlquery(); //�������ݿ������
$editor = 9; //����Ŀ¼���

require_once 'skycaiji2cms/skycaiji2cms.php';
$scj2cms = new skycaiji2cms(
    dirname(__FILE__),
    $public_r['newsurl'] . 'e/extend/skycaijidg',
    $ecms_config['db']['dbchar'],
    false
);

if (stripos($ecms_config['sets']['pagechar'], 'gb') === 0) {
    //gbk����
    $scj2cms->pluginLang = include 'data/lang_gbk.php';
} else {
    $scj2cms->pluginLang = include 'data/lang_utf8.php';
}
$scjConfig = ECMS_PATH . 'd/file/skycaijidg_config.php';
$pluginConfig = null;
if (file_exists($scjConfig)) {
    $pluginConfig = include $scjConfig;
    $pluginConfig = unserialize(base64_decode($pluginConfig));
}
$scj2cms->pluginConfig = is_array($pluginConfig) ? $pluginConfig : array();
$scj2cms->funcApiPost = '_scjFuncApiPost';

//���������
$ecms_config['esafe']['ckhash'] = 2;//�ر�HASHģʽ
chdir(ECMS_PATH . 'e/admin');
require LoadLang("pub/fun.php");
require("../class/delpath.php");
require("../class/copypath.php");
require("../class/t_functions.php");
require("../data/dbcache/class.php");
require("../data/dbcache/MemberLevel.php");
require("../member/class/user.php");
require("../class/hinfofun.php");


ob_clean();
ob_start();

register_shutdown_function('_scjExit');

$scj2cms->apiPost();//����

function _scjFuncApiPost()
{
    global $scj2cms;
    global $empire, $class_r, $class_zr, $bclassid, $public_r, $dbtbpre, $emod_r, $lur;
    if (empty($_POST['title'])) {
        $scj2cms->returnJson(0, $scj2cms->pluginLang['dg_error_title']);
    }
    if (empty($_POST['newstext'])) {
        $scj2cms->returnJson(0, $scj2cms->pluginLang['dg_error_newstext']);
    }
    //��Ŀ
    $classid = $_POST['classid'];
    if (empty($classid)) {
        $scj2cms->returnJson(0, $scj2cms->pluginLang['dg_error_classid']);
    }
    $classData = null;
    if (is_numeric($classid)) {
        $classid = intval($classid);
        $classData = $empire->fetch1("select * from {$dbtbpre}enewsclass where classid='{$classid}'");
    } else {
        $classid = addslashes($classid);
        $classData = $empire->fetch1("select * from {$dbtbpre}enewsclass where classname='{$classid}'");
    }
    if (empty($classData)) {
        $scj2cms->returnJson(0, $scj2cms->pluginLang['dg_error_class'] . $classid);
    }
    $_POST['classid'] = $classid = $classData['classid'];
    $_POST['bclassid'] = $classData['bclassid'];

    //����
    $author = $scj2cms->randLine($scj2cms->pluginConfig['author']);
    $userData = null;
    if (is_numeric($author)) {
        $author = intval($author);
        $userData = $empire->fetch1("select * from {$dbtbpre}enewsuser where userid='{$author}'");
    } else {
        $author = addslashes($author);
        $userData = $empire->fetch1("select * from {$dbtbpre}enewsuser where username='{$author}'");
    }
    if (empty($userData)) {
        $scj2cms->returnJson(0, $scj2cms->pluginLang['dg_error_user'] . $author);
    }
    //�����������Ŀ
    $copyclassid = $_POST['copyclassid'];
    if (isset($copyclassid)) {
        $copyclassid = explode(',', $copyclassid);
        foreach ($copyclassid as $k => $v) {
            $classData = null;
            if (is_numeric($v)) {
                $v = intval($v);
                $classData = $empire->fetch1("select * from {$dbtbpre}enewsclass where classid='{$v}'");
            } else {
                $v = addslashes($v);
                $classData = $empire->fetch1("select * from {$dbtbpre}enewsclass where classname='{$v}'");
            }
            if (empty($classData)) {
                unset($copyclassid[$k]);
            } else {
                $copyclassid[$k] = $classData['classid'];
            }
        }
        $copyclassid = array_unique($copyclassid);
        $copyclassid = array_values($copyclassid);
        $_POST['copyclassid'] = $copyclassid;
    }
    //��������
    $titlefont = $_POST['titlefont'];
    if (isset($titlefont)) {
        $titlefont = explode(',', $titlefont);
        foreach ($titlefont as $k => $v) {
            unset($titlefont[$v]);
            $titlefont[$v] = $v;
        }
        $_POST['titlefont'] = $titlefont;
    }
    //Ĭ�ϲ���
    $defaParams = array('checked' => 1, 'isgood' => 0, 'firsttitle' => 0, 'newstime' => date('Y-m-d H:i:s'), 'dokey' => 1, 'autosize' => 5000, 'getfirsttitlespicw' => 105, 'getfirsttitlespich' => 118, 'istop' => 0, 'groupid' => 0, 'info_diyotherlink' => 0, 'newspath' => date('Y-m-d'));
    foreach ($defaParams as $k => $v) {
        if (!isset($_POST[$k])) {
            $_POST[$k] = $v;
        }
    }

    $GLOBALS['_empire'] = $empire;
    $GLOBALS['_return_url'] = ($scj2cms->isSsl() ? 'https' : 'http') . '://' . $scj2cms->getHost() . $public_r['newsurl'] . 'e/action/ShowInfo.php?classid=' . $_POST['classid'] . '&id=';

    $ecms_config['esafe']['thedolog'] = 1;//�ر���־
    AddNews($_POST, $userData['userid'], $userData['username']);
}

function urlxx()
{
    $url = '';
    if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
        $url = $_SERVER['HTTP_X_REWRITE_URL'];
    } elseif (isset($_SERVER['REQUEST_URI'])) {
        $url = $_SERVER['REQUEST_URI'];
    } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
        $url = $_SERVER['ORIG_PATH_INFO'] . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
    } else {
        $url = '';
    }
    return $url;
}

function _scjExit()
{
    global $scj2cms;
    $html = ob_get_contents();
    ob_clean();
    if (strpos($html, '{') === 0) {
        //json
        exit($html);
    } else {
        $return = array('id' => 0, 'target' => '', 'desc' => '', 'error' => '');
        if (stripos($html, 'AddNews.php?enews=AddNews') !== false) {
            //���ӳɹ�
            $return['id'] = $GLOBALS['_empire']->id;
            $return['target'] = $GLOBALS['_return_url'] . $return['id'];
        } elseif (preg_match('/<div align="center">\s*<br>\s*<b>([\s\S]*?)<\/b>\s*<br>/i', $html, $error)) {
            //ʧ����Ϣ
            $return['error'] = $error[1];
        }
        $scj2cms->returnJson($return['id'], $return['error'], $return['target'], $return['desc']);
    }
}