<?php
if (!defined('puyuetian')) {
    exit('403');
}

if ('update' != $_G['GET']['DO']) {
    ExitJson('请求参数非法');
}

if (!class_exists('ZipArchive')) {
    ExitJson('PHP不存在ZIP扩展库，请先下载ZIP扩展。');
}

$r = apiData("update&nowversion={$nowversion}&php_version=" . PHP_VERSION);
if ('ok' != $r['state']) {
    ExitJson($r['datas']['msg']);
}

$name   = md5($r['datas']['newversion']);
$up     = $_G['SYSTEM']['PATH'] . "app/superadmin/updatezip/{$name}.zip";
$base64 = $r['datas']['content'];
$md5    = $r['datas']['md5'];

if (md5($base64) != $md5) {
    ExitJson('文件校验失败，请重新下载');
}

$r = file_put_contents($up, base64_decode($r['datas']['content']));
if (!$r) {
    ExitJson('写入失败，请保证网站目录下文件具有被写入权限');
}

$zip = new ZipArchive;
$res = $zip->open($up);
if (true !== $res) {
    ExitJson('解压失败：' . $res);
}
$zip->extractTo($_G['SYSTEM']['PATH']);
$zip->close();
unlink($up);

ExitJson('更新成功', true);
