<?php
include INDEX_ROOT.'admin/head.php';
admin_head('应用处理层');
$m=$_GET['m'];
if($m=='new'){
    if(empty($_POST))sysmsg('POST来的数据不能为空');
    if(empty($_POST['name']))sysmsg('应用名称不能为空');
    if(empty($_POST['home']))sysmsg('应用主页不能为空');
    if(empty($_POST['domain']))sysmsg('授权回调域名不能为空');
    if(empty($_POST['present']))sysmsg('应用介绍不能为空');


    $aes=new lib\Aes();
    $appid='mag-'.substr(md5(time()),-10);
    $password=substr(md5($appid.md5(rand(100000,999999).time())),-20);

    $name=$_POST['name'];
    $home=$_POST['home'];
    $domain=$_POST['domain'];
    $present=$_POST['present'];
    $type=$_POST['grant_types'];
    $owner=$uid;
    $account=new \auth\appmaster();

    $uid=$account->reg($appid,$password,$domain,$type,null,$owner,$name,$home,$present);
    alert('添加成功');
    go('/admin/app.html');
}elseif($m=='del'){
    if(empty($_GET['id']))sysmsg('应用ID不能为空');
    $account=new \auth\appmaster();
    $row=$account->del($_GET['id']);
    if(!$row) {
        alert('删除失败');
    }else{
        alert('删除成功');
    }
    go('-1');
}elseif($m=='edit'){
    if(empty($_POST))sysmsg('POST来的数据不能为空');
    if(empty($_POST['name']))sysmsg('应用名称不能为空');
    if(empty($_POST['home']))sysmsg('应用主页不能为空');
    if(empty($_POST['domain']))sysmsg('授权回调域名不能为空');
    if(empty($_POST['present']))sysmsg('应用介绍不能为空');
    $appid=$_GET['id'];
    $name=$_POST['name'];
    $home=$_POST['home'];
    $domain=$_POST['domain'];
    $present=$_POST['present'];

    $account=new \auth\appmaster();
    $account->edit($appid,$name,$home,$domain,$present);
    alert('修改成功');
    go('-1');
}else{
    alert('参数错误，请重新输入');
    go('-1');
}

admin_foot();
?>