<?php

/**
 * [Discuz!] (C)2001-2099 Discuz! Team
 * This is NOT a freeware, use is subject to license terms
 * https://license.discuz.vip
 */

if(!preg_match('/cli/i', php_sapi_name())) {
	exit;
}

const IN_API = true;
const CURSCRIPT = 'api_mserver';
const APPTYPEID = 200;
const DISABLEDEFENSE = true;

$_ENV['remote'] = new discuz_remote();
$_ENV['remote']->init();
$_ENV['remote']->loadservice();

class discuz_remote {

	var $mod;
	var $modobj;
	var $core;

	function init() {

		require_once('../../source/class/class_core.php');

		$cachelist = [];
		$this->core = C::app();

		$this->core->cachelist = $cachelist;


		$this->core->init_setting = true;

		$this->core->init_cron = false;
		$this->core->init_user = false;
		$this->core->init_session = false;
		$this->core->init_misc = false;
		$this->core->init_mobile = false;

		$this->core->init();

		define('SERVICE_DIR', getglobal('config/remote/dir') ? getglobal('config/remote/dir') : 'remote');
		$this->core->reject_robot();

		if(empty($_GET['mod']) || preg_match('/[^0-9a-z]/i', $_GET['mod'])) {
			$this->mod = 'index';
		} else {
			$this->mod = $_GET['mod'];
		}
	}

	function loadservice() {

		if(!$this->core->config['remote']['on']) {
			$this->error(1, 'remote service is down');
		}

		if(!$this->core->config['remote']['appkey']) {
			$this->error(1, 'remote service need a appkey, please edit you config.global.php');
		}

		if($this->mod != 'index') {

			$sign = $_GET['sign'];
			unset($_GET['sign']);

			if(empty($sign) || $sign != $this->sign($_GET)) {
			}
		}

		if(!$this->check_timestamp()) {
			$this->error(5, 'your request is time out');
		}

		$modfile = DISCUZ_ROOT.'./api/'.SERVICE_DIR.'/mod/mod_'.$this->mod.'.php';
		if(!is_file($modfile)) {
			$this->error(3, 'mod file is missing');
		}

		require $modfile;
		$classname = 'mod_'.$this->mod;
		if(class_exists($classname)) {
			$service = new $classname;
			$service->run();
		}
	}

	function check_timestamp() {
		if(empty($_GET['timestamp'])) {
			return 1;
		}

		$ttl = abs(empty($_GET['ttl']) ? 600 : $_GET['ttl']);
		$check = abs(TIMESTAMP - $_GET['timestamp']);
		return $check > $ttl ? 0 : 1;
	}

	function sign($arg) {
		$str = '';
		foreach($arg as $k => $v) {
			$str .= $k.'='.$v.'&';
		}
		return md5($str.getglobal('config/remote/appkey'));
	}

	function error($code, $msg) {
		$code = sprintf('%04d', $code);
		echo $code.':'.ucfirst($msg);
		exit();
	}

}

class remote_service {

	var $version = '1.0.0';
	var $config;

	function __construct() {
		$this->config = getglobal('config/remote');
	}

	function run() {
		remote_service::success('service is done.');
	}

	function error($code, $msg) {
		$code = sprintf('%04d', $code);
		echo $code.':'.ucfirst($msg);
		exit();
	}

	function success($msg) {
		remote_service::error(0, $msg);
	}

}

