<?php
// +----------------------------------------------------------------------
// | 云业内容管理系统 (云业CMS或yunyecms)
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2030 http://www.yunyecms.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云业科技 <kefu@yunyecms.com>
// +----------------------------------------------------------------------
//YmM0OWM5ZWY1ODk5ZGRkNzM0NUxxUjVMaWE1WWFGNWE2NTU2Nmg1NUNHNTdPNzU3dWZJQ2prdXBIa3VKcERUVlBtaUpaNWRXNTVaV050Y3lrc1EyOXdlWEpwWjJoMElDaGpLU0F5TURFMkxUSXdNekFnYUhSMGNEb3ZMM2QzZHk1NWRXNTVaV050Y3k1amIyMGdRV3hzSUhKcFoyaDBjeUJ5WlhObGNuWmxaQzRnUVhWMGFHOXlPaURsaUpqbW1aUHBvNTRnUERFMU1EQXpNakU1UUhGeExtTnZiVDQ9OGQ3ZTc5OTU5YjQ0MmEyNWQxNGVlMzg2ZmQyODM2OTkzNGE
//MD5:c610bc3d074f2ea0c833d32de3bfa0c3
// +----------------------------------------------------------------------
defined('IN_YUNYECMS') or exit('No permission.');
core::load_admin_class('admin');
class addons extends YUNYE_admin
{
	private $server;
	private $PostHash;
	private $yunyecms_com_member;
	function __construct()
	{
		parent::__construct();
		$this->db->tablename("addons");
		$this->PostHash = GetyunyecmsHash();
		$this->server = yunyecms_strdecode("YmM0OWM5ZWY1ODk5ZGRkNzM0YUhSMGNEb3ZMM2QzZHk1NWRXNTVaV050Y3k1amIyMD04ZDdlNzk5NTliNDQyYTI1ZDE0ZWUzODZmZDI4MzY5OTM0YQ==");
	}
	//获取全部应用列表页
	public function init()
	{
		$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', "online") . '\" target=\"maincontent\">应用</a></li><li  class=\"breadcrumb-item\"><a href=\"' . url('index') . '\" target=\"maincontent\">应用</a></li><li class=\"breadcrumb-item active\">全部应用</li>';
		$addonsurl = $this->server . "/index.php?m=addons&c=index&a=getaddons";
		$postdata['checkcode'] = 'yunyecmsaddons';
		$postdata['clientip'] = getip();
		$postdata['clienthost'] = getserver_domain();
		$addonsdata = curl_post($addonsurl, $postdata);
		//var_dump($addonsdata);
		if (!empty($addonsdata)) {
			$addonsdata = yunyecms_json_decode($addonsdata);
			foreach ($addonsdata as $key => $var) {
				$checkhas = $this->check_addons_exist($var['id']);
				$addonsdata[$key]['isinstalled'] = $checkhas;
				$addonsdata[$key]['pic'] = $this->server . $var['pic'];
				$addonsdata[$key]['pricetype'] = $this->getapps_price_powerid($var['inspower'], $var['price']);
			}
		}
		require tpl_adm('addons');
	}
	//获取应用详情信息
	public function detail()
	{
		$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', "online") . '\" target=\"maincontent\">应用</a></li><li  class=\"breadcrumb-item\"><a href=\"' . url('index') . '\" target=\"maincontent\">应用</a></li><li class=\"breadcrumb-item active\">全部应用</li>';
		$addonsurl = $this->server . "/index.php?m=addons&c=index&a=getaddons_detail";
		$id = $_GET['id'];
		if (empty($id) || !is_numeric($id)) {
			messagebox("参数错误", url_admin('init'));
		}
		$id = usafestr($id);
		$postdata['checkcode'] = 'yunyecmsaddons_detail';
		$postdata['clientip'] = getip();
		$postdata['addonsid'] = $id;
		$postdata['clienthost'] = getserver_domain();
		$addonsdetail = curl_post($addonsurl, $postdata);
		if (!empty($addonsdetail)) {
			$addonsdetail = yunyecms_json_decode($addonsdetail);
			$addonsdetail['pic'] = $this->server . $addonsdetail['pic'];
			$addonsdetail['content'] = remoteimgs($addonsdetail['content'], $this->server);
			$addonsdetail['pricetype'] = $this->getapps_price_powerid($addonsdetail['inspower'], $addonsdetail['price']);
			if ($addonsdetail['morepic']) {
				$addonsdetail['morepic'] = unserialize($addonsdetail['morepic']);
				if (is_array($addonsdetail['morepic'])) {
					foreach ($addonsdetail['morepic'] as $key => $var) {
						$addonsdetail['morepic'][$key] = $this->server . $var;
					}
				}
			}
		}
		require tpl_adm('addons_detail');
	}

	//获取插件的更新版本信息
	public function getaddons_upgrade()
	{
		$addonsurl = $this->server . "/index.php?m=addons&c=index&a=getaddons_upgrade";
		$id = $_POST['id'];
		if (empty($id) || !is_numeric($id)) {
			exit(0);
		}
		$id = usafestr($id);
		$postdata['checkcode'] = 'yunyecmsaddons_upgrade';
		$postdata['addons_upgrade_id'] = $id;
		$addons_upgrade = curl_post($addonsurl, $postdata);
		if (!empty($addons_upgrade)) {
			$addons_upgrade = yunyecms_json_decode($addons_upgrade);
			foreach ($addons_upgrade as $key => $var) {
				$addons_upgrade[$key]["day"] = date("d", $var["addtime"]);
				$addons_upgrade[$key]["year"] = date("Y-m", $var["addtime"]);
				$addons_upgrade[$key]["time"] = date("H:i", $var["addtime"]);
			}
		} else {
			exit(0);
		}
		exit(json_encode($addons_upgrade));
	}

	//获取插件详情的属性信息
	public function getaddons_attrib()
	{
		$addonsurl = $this->server . "/index.php?m=addons&c=index&a=getaddons_attrib";
		$id = usafestr($_POST['id']);
		$phpver = usafestr($_POST['phpver']);
		$dbver = usafestr($_POST['dbver']);
		$fitver = usafestr($_POST['fitver']);
		$catid = usafestr($_POST['catid']);
		if (empty($id) || !is_numeric($id)) {
			exit(0);
		}
		$id = usafestr($id);
		$postdata['checkcode'] = 'yunyecmsaddons_attrib';
		$postdata['addons_id'] = $id;
		$postdata['addons_phpver'] = $phpver;
		$postdata['addons_dbver'] = $dbver;
		$postdata['addons_fitver'] = $fitver;
		$postdata['addons_catid'] = $catid;
		$addons_attrib = curl_post($addonsurl, $postdata);
		if (!empty($addons_attrib)) {
			$addons_attrib = yunyecms_json_decode($addons_attrib);
		} else {
			exit(0);
		}
		exit(json_encode($addons_attrib));
	}

	//获取插件的更新版本信息
	public function getaddons_recommend()
	{
		$addonsurl = $this->server . "/index.php?m=addons&c=index&a=getaddons_recommend";
		$catid = $_POST['catid'];
		if (empty($catid) || !is_numeric($catid)) {
			exit(0);
		}
		$catid = usafestr($catid);
		$postdata['checkcode'] = 'yunyecmsaddons_recommend';
		$postdata['addons_catid'] = $catid;
		$addons_recommend = curl_post($addonsurl, $postdata);
		if (!empty($addons_recommend)) {
			$addons_recommend = yunyecms_json_decode($addons_recommend);
			foreach ($addons_recommend as $key => $var) {
				$addons_recommend[$key]['url'] = url_admin('detail', 'addons', array('id' => $var['id']));
				$addons_recommend[$key]['stitle'] = getsubstr($var['title'], 0, 15);
				$addons_recommend[$key]['pic'] = $this->server . $var['pic'];
			}
		} else {
			exit(0);
		}
		exit(json_encode($addons_recommend));
	}
	//应用安装
	public function install()
	{
		if (!getroot('addons', 'install')) {
			messagebox("您没有应用安装权限，请联系管理员！", 'back', "warn");
		}
		$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', "online") . '\" target=\"maincontent\">应用</a></li><li><a href=\"' . url('index') . '\" target=\"maincontent\">应用</a></li><li class=\"breadcrumb-item active\">全部应用</li>';
		$addonsurl = $this->server . "/index.php?m=addons&c=index&a=getaddons_install_info_init";
		$id = $_GET['id'];
		if (empty($id) || !is_numeric($id)) {
			messagebox("参数错误", url_admin('init'));
		}
		$id = usafestr($id);
		if ($this->check_addons_exist($id)) {
			messagebox("您已经安装过该应用了！", url_admin('init'));
		}
		$postdata['checkcode'] = 'yunyecmsaddons_install_info';
		$postdata['clientip'] = getip();
		$postdata['addonsid'] = $id;
		$postdata['clienthost'] = getserver_domain();
		$addonsdetail = curl_post($addonsurl, $postdata);
		$installinfo = "<a href='javascript:;' class='btn bg-purple margin btn-flat' style='margin-left:150px;' onclick='javascript:install_addons(\"getpackage\");'> <i class='icon-puzzle'></i> 开始安装 </a>";
		if (!empty($addonsdetail)) {
			$addonsdetail = yunyecms_json_decode($addonsdetail);
			$inspower = $addonsdetail['inspower'];
			$price = $addonsdetail['price'];
			$islogin = $this->check_yunyecms_login();
			if ($islogin) $this->yunyecms_com_member = $islogin;
			$nologinstr = "<div class=\"alert alert-info alert-dismissible\">
                <h4><i class=\"icon fa fa-info\"></i> 温馨提示!</h4>
                该应用为付费类型，您必须先登录您的云业CMS帐户！
              </div><a href='" . url_admin('yunyecms_login', 'addons') . "' class='btn btn-primary btn-lg margin btn-flat' style='margin-left:150px;'> <i class='icon-user'></i> 登录云业CMS帐户 </a>";
			if ($price > 0) {
				if (!$islogin) {
					$installinfo = $nologinstr;
				} else {
					$yy_userid = $this->yunyecms_com_member["id"];
					$checkorder = $this->check_yyorder($yy_userid, $addonsdetail['id'], $addonsdetail['catid']);
					if (!$checkorder) {
						$buyurl = $this->server . '/index.php?a=show&c=index&m=content&catid=' . $addonsdetail['catid'] . '&id=' . $addonsdetail['id'];
						$installinfo = "<a href='" . $buyurl . "' class='btn btn-danger btn-lg margin btn-flat' style='margin-left:150px;' target='_blank'> <i class='icon-basket'></i> 购买此应用 </a> <div class=\"alert alert-info alert-dismissible\">
                <h4><i class=\"icon fa fa-info\"></i> 温馨提示!</h4>
                该应用为付费应用，您必须购买{$addonsdetail['title']}应用才可以安装，谢谢！
              </div>";
					}
				}
			} elseif ($inspower == 2) {
				if (!$islogin) {
					$installinfo = $nologinstr;
				} else {
					$buyurl = $this->server . '/index.php?a=index&c=membergroup_goods&m=member';
					if (!($this->yunyecms_com_member["groupid"] == 2 || $this->yunyecms_com_member["groupid"] == 3)) {
						$installinfo = "<a href='" . $buyurl . "' class='btn btn-danger btn-lg margin btn-flat' style='margin-left:150px;' target='_blank'> <i class='icon-basket'></i> 购买会员 </a> <div class=\"alert alert-info alert-dismissible\">
					<h4><i class=\"icon fa fa-info\"></i> 温馨提示!</h4>
					该应用为会员应用，您必须购买会员才可以安装，谢谢！
				  </div>";
					}
				}
			} elseif ($inspower == 3) {
				if (!$islogin) {
					$installinfo = $nologinstr;
				} else {
					$buyurl = $this->server . '/index.php?a=index&c=grant_goods&m=grant';
					$yy_userid = $this->yunyecms_com_member["id"];
					$curgrant = $this->checkgrant($yy_userid);
					if (!$curgrant) {
						$installinfo = "<a href='" . $buyurl . "' class='btn btn-danger btn-lg margin btn-flat' style='margin-left:150px;' target='_blank'> <i class='icon-basket'></i> 购买授权 </a> <div class=\"alert alert-info alert-dismissible\">
					<h4><i class=\"icon fa fa-info\"></i> 温馨提示!</h4>
					1、该应用为授权应用，您必须购买授权才可以安装，谢谢！<br/>
					2、您必须使用授权域名登录后台才可以在线安装！
				  </div>";
					}
				}
			}
			$addonsdetail['pic'] = $this->server . $addonsdetail['pic'];
			$addonsdetail['pricetype'] = $this->getapps_price_powerid($addonsdetail['inspower'], $addonsdetail['price']);
		}
		require tpl_adm('addons_install');
	}


	public function install_process()
	{
		if (!empty($_POST['step'])) {
			$step = usafestr($_POST['step']);
		} else {
			$msg['code'] = "100";
			$msg['desc'] = "参数错误";
			$msg['step'] = $step;
			exit(json_encode($msg));
		}
		if (!empty($_POST['para'])) {
			$para = usafestr($_POST['para']);
		}
		if (!function_exists('gzopen') || !extension_loaded('zip')) {
			$msg['code'] = "109";
			$msg['desc'] = "您的服务器不支持ZIP模块，请联系管理员";
			exit(json_encode($msg));
		}
		if (!getroot('addons', 'install')) {
			$msg['code'] = "111";
			$msg['desc'] = "您没有应用安装权限，请联系管理员！";
			exit(json_encode($msg));
		}
		$docroot = $this->getdocroot();
		if ($step == "getpackage") {
			$addonsurl = "{$this->server}/index.php?m=addons&c=index&a=getaddons_install_info";
			$id = $_POST['addonsid'];
			$id = usafestr($id);
			$postdata['checkcode'] = 'yunyecmsaddons_install_info';
			$postdata['addonsid'] = $id;
			$postdata['clientip'] = getip();
			$postdata['clienthost'] = getserver_domain();
			$strsql = "select grantcode from `#yunyecms_config` order by id desc limit 0,1";
			$curconfig = $this->db->find($strsql);
			$postdata['grantcode'] = $curconfig["grantcode"];
			$islogin = $this->check_yunyecms_login();
			if ($islogin) {
				$this->yunyecms_com_member = $islogin;
				$userdata['yyuserid'] = $this->yunyecms_com_member['id'];
				$userdata['yyloginrnd'] = $this->yunyecms_com_member['rnd'];
				$userdata['yyloginpwd'] = $this->yunyecms_com_member['pwd'];
				$userdata = yunyecms_json_encode($userdata);
				$postdata['userdata'] = $userdata;
			}
			$addonsdetail = curl_post($addonsurl, $postdata);
			if (!empty($addonsdetail)) {
				$addonsdetail = yunyecms_json_decode($addonsdetail);
				$addons_file = $this->server . $addonsdetail['picfile'];
				$package_md5 = $addonsdetail['package_md5'];
				$version_num = $addonsdetail['version_num'];
			}
			if (!empty($version_num)) {
				if (YUNYECMS_VER < $version_num) {
					$needver = formatver($version_num);
					$msg['code'] = "104";
					$msg['desc'] = "<span class='text-red'>该应用需在yunyecms{$needver}以上版本安装，请先升级您的程序！</span>";
					exit(json_encode($msg));
				}
			}
			$msg['loaded'] = "<label class='info' id='upgrade_loading'>正在安装中 <i class='fa fa-spin fa-refresh'></i></label>";
			if (empty($addonsdetail['picfile'])) {
				$msg['code'] = "101";
				$msg['desc'] = "该应用的安装包不存在！";
				exit(json_encode($msg));
			} else {
				$addons_local_file = Get_RemoteFile($addons_file, ADDONS_INSTALL_ROOT, $package_md5);
				if (!$addons_local_file) {
					$msg['code'] = "102";
					$msg['desc'] = "下载安装包失败！";
					exit(json_encode($msg));
				} else {
					$file = str_replace('\\', '/', str_ireplace($docroot, '', $addons_local_file));
					$msg['code'] = "103";
					$msg['step'] = "upzip";
					$msg['para'] = GetFileName($addons_file);
					$msg['desc'] = "成功下载{$addonsdetail['title']}安装包！";
				}
			}
		} elseif ($step == "upzip") {
			if ($para) {
				$zipfile = ADDONS_INSTALL_ROOT . $para;
				$zip = new ZipArchive(); //新建一个ZipArchive的对象
				if ($zip->open($zipfile) != true) {
					$msg['code'] = "111";
					$msg['desc'] = "安装包读取失败！";
					exit(json_encode($msg));
				}
				$extractpath = ADDONS_INSTALL_ROOT . RemoveFileext($para);
				$zip->extractTo($extractpath);
				$zip->close(); //关闭处理的zip文件
				$msg['code'] = "112";
				$msg['step'] = "upgradeSource";
				$msg['para'] = $para;
				$msg['desc'] = "成功解压安装包！";
			}
		} elseif ($step == "upgradeSource") {
			if ($para) {
				$addons_dir_post = $_POST['addons_dir'];
				$addons_dir_post = usafestr($addons_dir_post);
				$zipfile = ADDONS_INSTALL_ROOT . $para;
				$zipfile_path = ADDONS_INSTALL_ROOT . RemoveFileext($para);
				$zip_file_dir = @opendir($zipfile_path);
				while ($file = readdir($zip_file_dir)) {
					if ($file != '.' && $file != '..' && is_dir($zipfile_path . DIRECTORY_SEPARATOR . $file) && $file != '.svn' && $file != 'index.htm') {
						$available_zip_dir[] = $file;
					}
				}
				$aa = json_encode($available_zip_dir);
				if ($addons_dir_post) {
					if ($available_zip_dir && count($available_zip_dir) == 1) {
						if ($addons_dir_post != $available_zip_dir[0]) {
							$msg['code'] = "133";
							$msg['desc'] = "安装包里的安装目录和服务器不一致！";
							exit(json_encode($msg));
						}
					} else {
						$msg['code'] = "134";
						$msg['desc'] = "安装包错误！";
						exit(json_encode($msg));
					}
				}
				$zip = new ZipArchive(); //新建一个ZipArchive的对象
				if ($zip->open($zipfile) != true) {
					$msg['code'] = "131";
					$msg['desc'] = "安装包读取失败！";
					exit(json_encode($msg));
				}
				$addonsdir = YUNYECMS_ADDONS;
				if (!is_dir(YUNYECMS_ADDONS)) dir_create(YUNYECMS_ADDONS);
				//if($addons_dir_post) $addonsdir=$addonsdir.$addons_dir_post;
				$zip->extractTo($addonsdir);
				$zip->close(); //关闭处理的zip文件
				$ret = file_delete($zipfile, 1);
				$retdir = dir_delete($zipfile_path);
				$msg['code'] = "132";
				$msg['step'] = "getsqlpackage";
				$msg['desc'] = "<span class='text-red'>成功安装应用程序文件！</span><br/>删除安装包成功！<br/> 请稍等片刻，安装程序正在检测数据库更新......";
			}
		} elseif ($step == "getsqlpackage") {
			$addons_dir_post = $_POST['addons_dir'];
			$addons_dir_post = usafestr($addons_dir_post);
			$sql_file = YUNYECMS_ADDONS . $addons_dir_post . DIRECTORY_SEPARATOR . "addons.sql";
			if (is_file($sql_file)) {
				$sqldata = file_get_contents(ToBackslash($sql_file));
				global $yunyecms_dbconfig;
				$dbpre = $yunyecms_dbconfig['tablepre'];
				$this->sql_execute($sqldata, $dbpre);
				$msg['code'] = "140";
				$msg['step'] = "setlocaladdons";
				$msg['desc'] = "成功安装数据库" . ToBackslash(str_ireplace($docroot, '', $sql_file)) . "！";
			} else {
				$msg['code'] = "141";
				$msg['desc'] = "本次更新无需更改数据库！";
				$msg['step'] = "setlocaladdons";
			}
			$ret = file_delete($sql_file, 1);
		} elseif ($step == "setlocaladdons") {
			$addonsurl = "{$this->server}/index.php?m=addons&c=index&a=getaddons_install_info";
			$id = $_POST['addonsid'];
			$id = usafestr($id);
			$postdata['checkcode'] = 'yunyecmsaddons_install_info';
			$postdata['addonsid'] = $id;
			$clientip = getip();
			$clienthost = getserver_domain();
			$postdata['clientip'] = $clientip;
			$postdata['clienthost'] = $clienthost;
			$strsql = "select grantcode from `#yunyecms_config` order by id desc limit 0,1";
			$curconfig = $this->db->find($strsql);
			$postdata['grantcode'] = $curconfig["grantcode"];
			$islogin = $this->check_yunyecms_login();
			if ($islogin) {
				$this->yunyecms_com_member = $islogin;
				$userdata['yyuserid'] = $this->yunyecms_com_member['id'];
				$userdata['yyloginrnd'] = $this->yunyecms_com_member['rnd'];
				$userdata['yyloginpwd'] = $this->yunyecms_com_member['pwd'];
				$userdata = yunyecms_json_encode($userdata);
				$postdata['userdata'] = $userdata;
			}
			$addonsdetail = curl_post($addonsurl, $postdata);
			if (!empty($addonsdetail)) {
				$addonsdetail = yunyecms_json_decode($addonsdetail);
			}
			if (empty($addonsdetail)) {
				$msg['code'] = "161";
				$msg['step'] = "";
				$msg['desc'] = "安装{$addonsdetail['title']}出错！";
				exit(json_encode($msg));
			} else {
				if ($this->check_addons($addonsdetail['addons_dir'], $addonsdetail['id'])) {
					$msg['code'] = "162";
					$msg['step'] = "";
					$msg['desc'] = "应用安装出错,该应用已存在！";
					exit(json_encode($msg));
				}
			}
			$data["title"] = $addonsdetail['title'];
			$data["addons_dir"] = $addonsdetail['addons_dir'];
			$data["addons_cat"] = $addonsdetail['catid'];
			$data["status"] = $addonsdetail['status'];
			$data["icon"] = ADDONS . $addonsdetail['addons_dir'] . "/icon.png";
			$data["preview"] = ADDONS . $addonsdetail['addons_dir'] . "/preview.jpg";
			$data["ordernum"] = $addonsdetail['ordernum'];
			$data["server_id"] = $addonsdetail['id'];
			$data["addons_table"] = $addonsdetail['appons_table'];
			$data["addtime"] = time();
			$retres = $this->db->insert($data, 'addons', 1);
			if ($retres) {
				$syndata["addons_dir"] = $data["addons_dir"];
				$syndata["addons_id"] = $data["server_id"];
				$syndata["addons_cat"] = $data["addons_cat"];
				$syndata["clientip"] = $clientip;
				$syndata["clienthost"] = $clienthost;
				if ($islogin) {
					$syndata['addons_userid'] = $this->yunyecms_com_member['id'];
					$syndata['addons_mobile'] = $this->yunyecms_com_member['username'];
				}
				$remoteurl_client = $this->server . "/index.php?m=addons&c=index&a=addaddons_client";
				$syndata['checkcode'] = 'yunyecms_addaddons_client';
				$ret_addons_client = curl_post($remoteurl_client, $syndata);
				$doing = "安装应用{$data["title"]}";
				$yyact = "addons_install";
				insert_admlogs($doing, $yyact);
				$msg['code'] = "999";
				$msg['step'] = "";
				$msg['desc'] = "<span class='text-red'>成功安装应用{$data["title"]}！</span> <a href='" . url_admin('addons_list', 'addons') . "' class='btn btn-primary margin btn-flat' style='margin-left:15px;' > <i class='fa fa-th-large'></i> 点此查看 </a>";
			} else {
				$msg['code'] = "163";
				$msg['step'] = "";
				$msg['desc'] = "更新版本号出错！";
			}
		} else {
			$msg['desc'] = "安装完成！";
			$msg['step'] = "";
			$msg['code'] = "999";
		}
		exit(json_encode($msg));
	}


	public function addons_list()
	{
		$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', "lang") . '\" target=\"maincontent\">系统</a></li><li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', 'autoupdate') . '\" target=\"maincontent\">自动更新</a></li><li class=\"breadcrumb-item active\">更新记录</li>';
		if (isset($_REQUEST)) {
			if (!empty($_REQUEST["searchkey"])) {
				$searchkey = usafestr(trim($_REQUEST["searchkey"]));
				$where = $where . " and ( `title`  like '%{$searchkey}%' )";
			}
		}
		$where = " title<>'' ";
		$pagearr = $this->db->infolist($where, 20, "ordernum asc");
		if ($pagearr["count"] != 0) {
			$list = $pagearr["query"];
			foreach ($list as $key => $var) {
			}
			$page = $pagearr["page"];
		}
		require tpl_adm('addons_list');
	}


	public function finaldelete()
	{
		if (!getroot('addons', 'del')) {
			messagebox("您没有应用卸载权限，请联系管理员！", 'back', "warn");
		}
		$id = $_REQUEST["id"];
		$id = usafestr($id);
		global $yunyecms_dbconfig;
		$dbpre = $yunyecms_dbconfig['tablepre'];
		if (is_numeric($id) && !empty($id)) {
			$cur_addons = $this->db->getbyid($id);
			$tablename = $cur_addons['addons_table'];
			if (empty($tablename)) {
				messagebox("卸载失败，错误的参数！", 'back', "warn");
			} else {
				$tablename = yexplode(",", $tablename);
				if (is_array($tablename)) {
					foreach ($tablename as $v) {
						$tbname = $dbpre . 'addons_' . $v;
						$sqldelmodtb = " DROP TABLE IF EXISTS {$tbname}";
						$query1 = $this->db->query($sqldelmodtb);
					}
				} else {
					$tbname = $dbpre . 'addons_' . $tablename;
					$sqldelmodtb = " DROP TABLE IF EXISTS {$tbname}";
					$query1 = $this->db->query($sqldelmodtb);
				}
				$appdir_real = YUNYECMS_ADDONS . $cur_addons['addons_dir'];
				if (is_dir($appdir_real)) $retdir = dir_delete($appdir_real);
			}
			$retres = $this->db->delete("id={$id}", "addons");
			if ($retres !== false) {
				$yyact = "addons_Del";
				$logcontent['tablename'] = "addons";
				$logcontent['action'] = $yyact;
				$logcontent['ids'] = serialize($id);
				$logcontent = serialize($logcontent);
				$doing = "卸载应用：{$id} ";
				insert_admlogs($doing, $yyact, $logcontent);
				messagebox('应用卸载成功！', url_admin('addons_list'), "success");
			} else {
				messagebox(Lan('admin_delete_error'), url_admin('addons_list'), "warn");
			}
		} else {
			messagebox("错误的参数！", 'back', "warn");
		}
	}


	public function check()
	{
		$retres = $this->info_check("addons");
		if (false !== $retres) {
			messagebox("应用开启成功!", url_admin('addons_list'), "success");
		} else {
			messagebox("应用开启失败!", url_admin('addons_list'), "warn");
		}
	}

	public function nocheck()
	{
		$retres = $this->info_check("addons", 0);
		if (false !== $retres) {
			messagebox("应用停用成功!", url_admin('addons_list'), "success");
		} else {
			messagebox("应用停用失败!", url_admin('addons_list'), "warn");
		}
	}


	private function sql_execute($sql, $tablepre)
	{
		$sqls = $this->sql_split($sql, $tablepre);
		if (is_array($sqls)) {
			foreach ($sqls as $sql) {
				if (trim($sql) != '') {
					$this->db->query($sql);
				}
			}
		} else {
			$this->db->query($sqls);
		}
		return true;
	}

	private function  sql_split($sql, $tablepre)
	{
		if ($tablepre != "yunyecms_") $sql = str_ireplace("yunyecms_", $tablepre, $sql);
		$sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql);
		//if($r_tablepre != $s_tablepre) $sql = str_replace($s_tablepre, $r_tablepre, $sql);
		$sql = str_replace("\r", "\n", $sql);
		$ret = array();
		$num = 0;
		$queriesarray = explode(";\n", trim($sql));
		unset($sql);
		foreach ($queriesarray as $query) {
			$ret[$num] = '';
			$queries = explode("\n", trim($query));
			$queries = array_filter($queries);
			foreach ($queries as $query) {
				$str1 = substr($query, 0, 1);
				if ($str1 != '#' && $str1 != '-') $ret[$num] .= $query;
			}
			$num++;
		}
		return $ret;
	}

	private function getdocroot()
	{
		$doc_root = $_SERVER['DOCUMENT_ROOT'];
		if (!empty($doc_root) && substr($doc_root, -1) == "/") {
			$doc_root = substr($doc_root, 0, -1);
		}
		if (strpos($doc_root, "\\")) {
			$doc_root = str_ireplace('\\', '/', $doc_root);
		}
		return $doc_root;
	}


	function strToUtf8($str)
	{
		$encode = mb_detect_encoding($str, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'));
		if ($encode == 'UTF-8') {
			return $str;
		} else {
			return mb_convert_encoding($str, 'UTF-8', $encode);
		}
	}

	private function  getapps_price_powerid($inspower, $price)
	{
		if ($price > 0) {
			return "￥" . $price;
		} else {
			switch ($inspower) {
				case 1:
					$retstr = "免费";
					break;
				case 2:
					$retstr = "会员插件";
					break;
				case 3:
					$retstr = "授权用户免费";
					break;
			}
		}
		return $retstr;
	}

	private  function check_yunyecms_login()
	{
		$userid = ugetcookie("yunyecms_com_userid");
		$rnd = usafestr(ugetcookie("yunyecms_com_loginrnd"));
		if (empty($userid)) {
			return false;
		} else {
			$userid = usafestr(yunyecms_strdecode($userid));
			if (empty($userid) && !is_numeric($userid)) {
				return false;
			}
			$remoteurl = $this->server . "/index.php?m=addons&c=index&a=membercheck_islogin";
			$postdata['checkcode'] = 'yunyecmsaddons_member_check_islogin';
			$postdata['userid'] = $userid;
			$postdata["loginrnd"] = $rnd;
			$curmember = curl_post($remoteurl, $postdata);
			if (!empty($curmember)) {
				$curmember = yunyecms_json_decode($curmember);
				return $curmember;
			} else {
				return false;
			}
		}
	}

	//订单查询
	private  function check_yyorder($yy_userid, $addons_id, $addons_catid)
	{
		if (empty($yy_userid) || empty($addons_id) || empty($addons_catid) || !is_numeric($yy_userid) || !is_numeric($addons_id) || !is_numeric($addons_catid)) {
			return false;
		} else {
			$remoteurl = $this->server . "/index.php?m=addons&c=index&a=member_ordercheck";
			$postdata['checkcode'] = 'yunyecmsaddons_member_ordercheck';
			$postdata['yy_userid'] = $yy_userid;
			$postdata['addons_id'] = $addons_id;
			$postdata['addons_catid'] = $addons_catid;
			$curorder = curl_post($remoteurl, $postdata);
			if (!empty($curorder)) {
				$curorder = yunyecms_json_decode($curorder);
				return $curorder;
			} else {
				return false;
			}
		}
	}

	//授权检测
	private  function checkgrant($yy_userid)
	{
		if (empty($yy_userid) || !is_numeric($yy_userid)) {
			return false;
		} else {
			$strsql = "select grantcode from `#yunyecms_config` order by id desc limit 0,1";
			$curconfig = $this->db->find($strsql);
			$remoteurl = $this->server . "/index.php?m=addons&c=index&a=member_grantcheck";
			$postdata['grantcode'] = $curconfig["grantcode"];
			$postdata['checkcode'] = 'yunyecmsaddons_member_grantcheck';
			$postdata['yy_userid'] = $yy_userid;
			$postdata['clientip'] = getip();
			$postdata['clienthost'] = getserver_domain();
			$curgrant = curl_post($remoteurl, $postdata);
			if (!empty($curgrant)) {
				$curgrant = yunyecms_json_decode($curgrant);
				return $curgrant;
			} else {
				return false;
			}
		}
	}


	public function yunyecms_login()
	{
		$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', "online") . '\" target=\"maincontent\">应用</a></li><li><a href=\"' . url_admin('init') . '\" target=\"maincontent\">应用</a></li><li class=\"breadcrumb-item active\">登录云业CMS帐户</li>';
		$token = make_rand_letternum(16);
		$_SESSION["yunyecms_login_token"] = $token;
		require tpl_adm('addons_yunyecms_login');
	}

	//检测登录信息
	public function yunyecms_logincheck()
	{
		$_POST = yytrim($_POST);
		if (empty($_POST['mobile']) || !is_numeric($_POST['mobile'])) {
			output_json(array("code" => 103, "msg" => "手机号不能为空"));
		} else {
			$mobile = usafestr($_POST['mobile']);
		}
		if (empty($_POST['pwd'])) {
			output_json(array("code" => 104, "msg" => "密码不能为空"));
		} else {
			$pwd = yypwd($_POST['pwd']);
		}
		$token = trim($_POST["token"]);
		if (empty($token) || $token != $_SESSION['yunyecms_login_token']) {
			output_json(array("code" => 105, "msg" => "安全码验证失败！"));
		}
		$remoteurl = $this->server . "/index.php?m=addons&c=index&a=membercheck";
		$postdata['checkcode'] = 'yunyecmsaddons_member_check';
		$postdata['mobile'] = $mobile;
		$curmember = curl_post($remoteurl, $postdata);
		if (!empty($curmember)) {
			$curmember = yunyecms_json_decode($curmember);
		}
		if (empty($curmember)) {
			output_json(array("code" => 103, "msg" => "手机号错误"));
		}
		if ($pwd == $curmember['pwd']) {
			if ($curmember["status"]) {
				$logintime = time();
				$userid = $curmember["id"];
				$loginrnd = make_rand_letternum(16);
				$member_rnd_url = $this->server . "/index.php?m=addons&c=index&a=member_uprnd";
				$postdata_rnd['checkcode'] = 'yunyecmsaddons_member_uprnd';
				$postdata_rnd['userid'] = $userid;
				$postdata_rnd['loginrnd'] = $loginrnd;
				$syn_rnd = curl_post($member_rnd_url, $postdata_rnd);
				usetcookie("yunyecms_com_userid", yunyecms_strencode($userid));
				usetcookie("yunyecms_com_username", $curmember["username"]);
				usetcookie("yunyecms_com_groupid", $curmember["groupid"]);
				usetcookie("yunyecms_com_mobile", $curmember["mobile"]);
				usetcookie("yunyecms_com_loginrnd", $loginrnd);
				output_json(array("code" => 100, "msg" => "登录成功"));
			} else {
				output_json(array("code" => 101, "msg" => "登录失败"));
			}
		} else {
			output_json(array("code" => 102, "msg" => "密码错误"));
		}
	}

	private function check_addons($addons_dir, $addons_id)
	{
		$addons_dir = usafestr(trim($addons_dir));
		$addons_id = usafestr(trim($addons_id));
		if (empty($addons_dir) || !is_numeric($addons_id)) {
			return false;
		} else {
			$cntcheckone = $this->db->GetCount("select count(*) from `#yunyecms_addons` where  `addons_dir`='$addons_dir' and  `server_id`='$addons_id'  ");
			if ($cntcheckone >= 1) {
				return true;
			} else {
				return false;
			}
		}
	}

	private function check_addons_exist($addons_id)
	{
		$addons_id = usafestr(trim($addons_id));
		if (empty($addons_id) || !is_numeric($addons_id)) {
			return false;
		} else {
			$cntcheckone = $this->db->GetCount("select count(*) from `#yunyecms_addons` where  `server_id`='$addons_id'  ");
			if ($cntcheckone >= 1) {
				return true;
			} else {
				return false;
			}
		}
	}
}
