<?php namespace Phpcmf\Controllers\Admin;
class Cloud extends \Phpcmf\Common
{
private $admin_url;
private $service_url;
private $license;
public function __construct(...$params)
{
parent::__construct(...$params);
if (is_file(MYPATH . 'Config/License.php')) {
$this->license = require MYPATH . 'Config/License.php';
if (!$this->license['license']) {
exit('程序不是最新,请在官网 http://www.kaiqibao.net/down_zip/ 下载[安装包]并覆盖src目录');
}
} else {
exit('当前程序版本:'.$this->cmf_version['version'].',需要更新到正式版,请在官网 http://www.kaiqibao.net/down_zip/ 下载[安装包]并覆盖src目录');
}
list($this->admin_url) = explode('?', FC_NOW_URL);
$this->service_url = 'https://www.kaiqibao.net/cloud.php?domain=' . dr_get_domain_name(ROOT_URL) . '&admin=' . urlencode($this->admin_url) . '&cms=' . $this->cmf_version['id'] . '&license=' . $this->license['license'];
if ($this->license['cloud']) {
$this->service_url = $this->license['cloud'] . '/index.php?s=cloud&c=api&domain=' . dr_get_domain_name(ROOT_URL) . '&admin=' . urlencode($this->admin_url) . '&license=' . $this->license['license'];
}
\Phpcmf\Service::V()->assign([
'is_syy' => $this->license['cloud'] ? 1 : 0,
'license' => $this->license,
'license_sn' => $this->license['license'],
'cms_version' => $this->cmf_version,
'cmf_version' => $this->cmf_version,
]);
}
public function service() {
$url = MASTER_URL;
\Phpcmf\Service::V()->assign([
'url' => $url,
]);
\Phpcmf\Service::V()->display('cloud_online.html');exit;
}
public function index() {
$domain = dr_get_domain_name(ROOT_URL);
$license_domain = 'https://www.kaiqibao.net/license/domain/'.$domain;
if ($this->license['domain']) {
$license_domain = trim($this->license['domain'], '/').'/index.php?s=license&m=show&domain='.$domain;
}
\Phpcmf\Service::V()->assign([
'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
[
'我的网站' => [\Phpcmf\Service::L('Router')->class.'/'.\Phpcmf\Service::L('Router')->method, 'fa fa-cog'],
]
),
'domain' => $domain,
'ip_address' => \Phpcmf\Service::L('input')->ip_address(),
'license_domain' => $license_domain,
]);
\Phpcmf\Service::V()->display('cloud_index.html');exit;
}
public function app() {
$id = [];
$local = dr_dir_map(dr_get_app_list(), 1);
foreach ($local as $dir) {
if (is_file(dr_get_app_dir($dir).'Config/App.php')) {
$cfg = require dr_get_app_dir($dir).'Config/App.php';
if (($cfg['type'] != 'module' || $cfg['ftype'] == 'module')
&& is_file(dr_get_app_dir($dir).'Config/Version.php')) {
$vsn = require dr_get_app_dir($dir).'Config/Version.php';
$vsn['id'] && $id[] = $vsn['id'];
}
}
}
\Phpcmf\Service::V()->assign([
'url' => MASTER_URL . '?s=cloud&' . http_build_query([
'domain' => rtrim(FC_NOW_HOST, '/'),
'catid' => '1,2,4'
]),
]);
\Phpcmf\Service::V()->display('cloud_online.html');exit;
}
public function func() {
\Phpcmf\Service::V()->assign([
'url' => $this->service_url.'&action=app&catid=16',
]);
\Phpcmf\Service::V()->display('cloud_online.html');exit;
}
public function template() {
\Phpcmf\Service::V()->assign([
'url' => MASTER_URL . '?s=cloud&' . http_build_query([
'domain' => rtrim(FC_NOW_HOST, '/'),
'catid' => '3'
]),
]);
\Phpcmf\Service::V()->display('cloud_online.html');exit;
}
public function update_sn() {
}
public function local() {
if (SITE_ID > 1) {
$this->_admin_msg(0, '请切换到[主站点]操作');exit;
}
$pre_installed = [
'safe',
'order',
'page',
'shang',
'fclient',
'license',
'variable',
'mfile',
'yaoqing',
'cqx',
'bdxz',
'bdtj',
'sitemap',
'favorite',
'bdxcx',
'sites',
'weixin',
'tixian',
'navigator',
'spider',
'fstatus',
'beifen',
'copy',
'bdts',
'mbdyf',
'tag',
'gathering',
'email',
'zan',
'block',
];
$data = [];
$local = dr_dir_map(dr_get_app_list(), 1);
foreach ($local as $dir) {
$path = dr_get_app_dir($dir);
if (is_file($path.'Config/App.php')) {
$key = strtolower($dir);
if (in_array($key, $pre_installed)) {
continue;
}
$cfg = require $path.'Config/App.php';
if (is_file($path.'Config/Version.php')) {
$vsn = require $path.'Config/Version.php';
$data[$key] = [
'id' => $vsn['id'],
'name' => $cfg['name'],
'type' => $cfg['type'],
'icon' => $cfg['icon'],
'author' => $cfg['author'],
'store' => $vsn['store'],
'version' => $vsn['version'],
'install' => is_file($path.'install.lock'),
];
}
}
}
\Phpcmf\Service::V()->assign([
'list' => $data,
'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
[
'本地应用' => [\Phpcmf\Service::L('Router')->class.'/'.\Phpcmf\Service::L('Router')->method, 'fa fa-puzzle-piece'],
'应用市场' => [\Phpcmf\Service::L('Router')->class.'/app', 'fa fa-cloud'],
'help' => [574],
]
),
]);
\Phpcmf\Service::V()->display('cloud_app.html');exit;
}
public function install_tpl() {
$id = dr_safe_filename(\Phpcmf\Service::L('input')->get('id'));
$dir = dr_safe_replace(\Phpcmf\Service::L('input')->get('dir'));
!$dir && $this->_json(0, dr_lang('缺少模板参数'));
\Phpcmf\Service::M('Site')->set_theme($dir, SITE_ID);
\Phpcmf\Service::M('Site')->set_template($dir, SITE_ID);
if (is_file(WRITEPATH.'temp/run-'.$id.'.php')) {
require WRITEPATH.'temp/run-'.$id.'.php';
}
\Phpcmf\Service::M('cache')->sync_cache('');
$this->_json(1, dr_lang('当前站点模板安装成功,请访问前台预览'));
}
public function install() {
$id = dr_safe_filename(\Phpcmf\Service::L('input')->get('id'));
$dir = dr_safe_replace(\Phpcmf\Service::L('input')->get('dir'));
!is_file(dr_get_app_dir($dir).'Config/App.php') && $this->_json(0, dr_lang('安装程序App.php不存在'));
$rt = \Phpcmf\Service::M('App')->install($dir);
!$rt['code'] && $this->_json(0, $rt['msg']);
if (is_file(WRITEPATH.'temp/run-'.$id.'.php')) {
require WRITEPATH.'temp/run-'.$id.'.php';
}
\Phpcmf\Service::M('cache')->sync_cache('');
$this->_json(1, dr_lang('安装成功,请刷新后台页面'));
}
public function uninstall() {
$dir = dr_safe_replace(\Phpcmf\Service::L('input')->get('dir'));
!preg_match('/^[a-z]+$/U', $dir) && $this->_json(0, dr_lang('目录[%s]格式不正确', $dir));
$path = dr_get_app_dir($dir);
!is_dir($path) && $this->_json(0, dr_lang('目录[%s]不存在', $path));
$rt = \Phpcmf\Service::M('App')->uninstall($dir);
\Phpcmf\Service::M('cache')->sync_cache('');
$this->_json($rt['code'], $rt['msg']);
}
public function login() {
if (IS_POST) {
$post = \Phpcmf\Service::L('input')->post('data');
$surl = $this->service_url.'&action=update_login&get_http=1&username='.$post['username'].'&password='.md5($post['password']);
$json = dr_catcher_data($surl);
if (!$json) {
$this->_json(0, '没有从服务端获取到数据');
}
exit($json);
}
\Phpcmf\Service::V()->display('cloud_login.html');exit;
}
function down_file() {
\Phpcmf\Service::V()->assign([
'ls' => intval($_GET['ls']),
'app_id' => 'app-'.intval($_GET['cid']),
]);
\Phpcmf\Service::V()->display('cloud_down_file.html');exit;
}
function install_app() {
$id = dr_safe_replace($_GET['id']);
$cache = \Phpcmf\Service::L('cache')->init()->get('cloud-update-'.$id);
if (!$cache) {
$this->_json(0, '本站:授权验证过期,请重试');
}
$file = WRITEPATH.'temp/'.$id.'.zip';
if (!is_file($file)) {
$this->_json(0, '本站:文件还没有被下载');
} elseif (!class_exists('ZipArchive')) {
$this->_json(0, '本站:php_zip扩展未开启,无法在线安装功能');
}
$cmspath = WRITEPATH.'temp/'.$id.'/';
if (!\Phpcmf\Service::L('file')->unzip($file, $cmspath)) {
cloud_msg(0, '本站:文件解压失败');
}
unlink($file);
$is_app = 0;
$is_tpl = 0;
if (is_file($cmspath.'Install.php') && strpos(file_get_contents($cmspath.'Install.php'), 'return') !== false) {
$ins = require $cmspath.'Install.php';
if (isset($ins['type']) && $ins['type'] == 'app') {
if ($ins['name'] && is_file($cmspath.'APPSPATH/'.ucfirst($ins['name']).'/Config/App.php')) {
$is_app = $ins['name'];
}
} elseif (isset($ins['type']) && $ins['type'] == 'tpl') {
if ($ins['name']) {
$is_tpl = $ins['name'];
}
}
}
if (is_file($cmspath.'Run.php')) {
copy($cmspath.'Run.php',WRITEPATH.'temp/run-'.$id.'.php');
}
if (!$is_tpl && !$is_app && is_dir($cmspath.'APPSPATH/')) {
$p = dr_dir_map($cmspath.'APPSPATH/', 1);
foreach ($p as $name) {
if (is_file($cmspath.'APPSPATH/'.$name.'/Config/App.php')
&& is_file($cmspath.'APPSPATH/'.$name.'/Config/Version.php')) {
if (is_file($cmspath.'APPSPATH/'.$name.'/install.lock')) {
unlink($cmspath.'APPSPATH/'.$name.'/install.lock');
}
$is_app = strtolower($name);
break;
}
}
}
if (is_dir($cmspath.'APPSPATH')) {
$this->_copy_dir($cmspath.'APPSPATH', APPSPATH);
}
if (is_dir($cmspath.'WEBPATH')) {
$this->_copy_dir($cmspath.'WEBPATH', ROOTPATH);
}
if (is_dir($cmspath.'ROOTPATH')) {
$this->_copy_dir($cmspath.'ROOTPATH', ROOTPATH);
}
if (is_dir($cmspath.'CSSPATH')) {
$this->_copy_dir($cmspath.'CSSPATH/', ROOTPATH.'static/');
}
if (is_dir($cmspath.'TPLPATH')) {
$this->_copy_dir($cmspath.'TPLPATH', TPLPATH);
}
if (is_dir($cmspath.'WRITEPATH')) {
$this->_copy_dir($cmspath.'WRITEPATH', WRITEPATH);
}
if (is_dir($cmspath.'FCPATH')) {
$this->_copy_dir($cmspath.'FCPATH', FCPATH);
}
if (is_dir($cmspath.'MYPATH')) {
$this->_copy_dir($cmspath.'MYPATH', MYPATH);
}
if (is_dir($cmspath.'COREPATH')) {
$this->_copy_dir($cmspath.'COREPATH', COREPATH);
}
dr_dir_delete($cmspath, 1);
if ($is_app) {
$msg = '程序导入完成</p><p style="margin-top:20px;"><a href="javascript:dr_load_ajax(\''.dr_lang('确定安装此程序吗?').'\', \''.dr_url('cloud/install', ['id' => $id, 'dir'=>$is_app]).'\', 0);">立即安装应用插件</a>';
} elseif ($is_tpl) {
$msg = '模板导入完成</p><p style="margin-top:20px;"><a href="javascript:dr_load_ajax(\''.dr_lang('确定安装此模板到当前站点吗?').'\', \''.dr_url('cloud/install_tpl', ['id' => $id, 'dir'=>$is_tpl]).'\', 0);">立即安装模板</a>';
} else {
$msg = '程序导入完成<br>请按本商品的使用教程来操作';
}
$this->_json(1, $msg);
}
public function update() {
$data = [];
$data['phpcmf'] = $this->cmf_version;
$data['phpcmf']['id'] = 'cms-1';
$data['phpcmf']['tname'] = '系统';
\Phpcmf\Service::V()->assign([
'list' => $data,
'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
[
'版本升级' => [\Phpcmf\Service::L('Router')->class.'/'.\Phpcmf\Service::L('Router')->method, 'fa fa-refresh'],
]
),
'cms_id' => $this->cmf_version['cms'],
'domain_id' => $this->license['id'],
]);
\Phpcmf\Service::V()->display('cloud_update.html');exit;
}
public function check_version() {
$cid = dr_safe_replace($_GET['id']);
if ($cid == 'cms-1') {
$dir = [
WRITEPATH,
ROOTPATH,
APPSPATH,
TPLPATH,
FCPATH,
MYPATH,
];
foreach ($dir as $t) {
!dr_check_put_path($t) && $this->_json(0, dr_lang('目录【%s】不可写', $t));
}
}
$vid = dr_safe_replace($_GET['version']);
$surl = $this->service_url.'&action=check_version&get_http=1&id='.$cid.'&version='.$vid;
$json = dr_catcher_data($surl);
!$json ? $this->_json(0, '没有从服务端获取到数据') : exit($json);
}
public function todo_update() {
\Phpcmf\Service::V()->assign([
'app_id' => dr_safe_replace($_GET['id']),
]);
\Phpcmf\Service::V()->display('cloud_todo_update.html');exit;
}
public function update_file() {
$id = dr_safe_replace($_GET['id']);
if (!$id) {
$this->_json(0, '没有选择任何升级程序');
}
$json = (array) $_GET['data'];
if (!$json) {
$this->_json(0, '没有从服务端获取到数据');
}
$data = dr_string2array($json);
if (!$data) {
$this->_json(0, '服务端数据异常,请重新下载', $json);
} elseif (!$data['code']) {
$this->_json(0, $data['msg']);
} elseif (!$data['data']['size']) {
$this->_json(0, '服务端文件总大小异常');
} elseif (!$data['data']['url']) {
$this->_json(0, '服务端文件下载地址异常');
}
\Phpcmf\Service::L('cache')->init()->save('cloud-update-'.$id, $data['data'], 3600);
$this->_json(1, 'ok', $data['data']);
}
public function update_file_down() {
$id = dr_safe_replace($_GET['id']);
$cache = \Phpcmf\Service::L('cache')->init()->get('cloud-update-'.$id);
if (!$cache) {
$this->_json(0, '授权验证过期,请重试');
} elseif (!$cache['size']) {
$this->_json(0, '关键数据不存在,请重试');
} elseif (!function_exists('fsockopen')) {
$this->_json(0, '本站:PHP环境不支持fsockopen');
}
$file = WRITEPATH.'temp/'.$id.'.zip';
set_time_limit(0);
touch($file);
if ($fp = fopen($cache['url'], "rb")) {
if (!$download_fp = fopen($file, "wb")) {
$this->_json(0, '本站:无法写入远程文件', $cache['url']);
}
while (!feof($fp)) {
if (!is_file($file)) {
fclose($download_fp);
$this->_json(0, '本站:临时文件被删除', $cache['url']);
}
fwrite($download_fp, fread($fp, 1024 * 8 ), 1024 * 8);
}
fclose($download_fp);
fclose($fp);
$this->_json(1, 'ok');
} else {
unlink($file);
$this->_json(0, '本站:fopen打开远程文件失败', $cache['url']);
}
}
public function update_file_check() {
$id = dr_safe_replace($_GET['id']);
$cache = \Phpcmf\Service::L('cache')->init()->get('cloud-update-'.$id);
if (!$cache) {
$this->_json(0, '本站:授权验证过期,请重试');
} elseif (!$cache['size']) {
$this->_json(0, '本站:关键数据不存在,请重试');
}
$file = WRITEPATH.'temp/'.$id.'.zip';
if (is_file($file)) {
$now = max(1, filesize($file));
$jd = max(1, round($now / $cache['size'] * 100, 0));
$this->_json($jd, '<p><label class="rleft">需下载文件大小:'.dr_format_file_size($cache['size']).',已下载:'.dr_format_file_size($now).'</label><label class="rright"><span class="ok">'.$jd.'%</span></label></p>');
} else {
$this->_json(0, '本站:文件还没有被下载');
}
}
public function update_file_install() {
$id = dr_safe_replace($_GET['id']);
$cache = \Phpcmf\Service::L('cache')->init()->get('cloud-update-'.$id);
if (!$cache) {
$this->_json(0, '本站:授权验证过期,请重试');
}
$file = WRITEPATH.'temp/'.$id.'.zip';
if (!is_file($file)) {
$this->_json(0, '本站:文件还没有被下载');
}
$cmspath = WRITEPATH.'temp/'.$id.'/';
if (!\Phpcmf\Service::L('file')->unzip($file, $cmspath)) {
cloud_msg(0, '本站:文件解压失败');
}
unlink($file);
list($type) = explode('-', $id);
if ($type == 'cms') {
if (is_dir($cmspath.'cache')) {
$this->_copy_dir($cmspath.'cache', WRITEPATH);
dr_dir_delete($cmspath.'cache', 1);
}
if (is_dir($cmspath.'src/App')) {
$this->_copy_dir($cmspath.'src/App', APPSPATH);
dr_dir_delete($cmspath.'src/App', 1);
}
if (is_dir($cmspath.'src/My')) {
$this->_copy_dir($cmspath.'src/My', MYPATH);
dr_dir_delete($cmspath.'src/My', 1);
}
if (is_dir($cmspath.'src')) {
$this->_copy_dir($cmspath.'src', FCPATH);
dr_dir_delete($cmspath.'src', 1);
}
$this->_copy_dir($cmspath, ROOTPATH);
} else {
if (is_dir($cmspath.'APPSPATH/')) {
$p = dr_dir_map($cmspath.'APPSPATH/', 1);
foreach ($p as $name) {
if (is_file($cmspath.'APPSPATH/'.$name.'/Config/App.php')) {
if (is_file($cmspath.'APPSPATH/'.$name.'/install.lock')) {
unlink($cmspath.'APPSPATH/'.$name.'/install.lock');
}
break;
}
}
}
if (is_dir($cmspath.'APPSPATH')) {
$this->_copy_dir($cmspath.'APPSPATH', APPSPATH);
}
if (is_dir($cmspath.'WEBPATH')) {
$this->_copy_dir($cmspath.'WEBPATH', ROOTPATH);
}
if (is_dir($cmspath.'ROOTPATH')) {
$this->_copy_dir($cmspath.'ROOTPATH', ROOTPATH);
}
if (is_dir($cmspath.'CSSPATH')) {
$this->_copy_dir($cmspath.'CSSPATH/', ROOTPATH.'static/');
}
if (is_dir($cmspath.'TPLPATH')) {
$this->_copy_dir($cmspath.'TPLPATH', TPLPATH);
}
if (is_dir($cmspath.'WRITEPATH')) {
$this->_copy_dir($cmspath.'WRITEPATH', WRITEPATH);
}
if (is_dir($cmspath.'FCPATH')) {
$this->_copy_dir($cmspath.'FCPATH', FCPATH);
}
if (is_dir($cmspath.'MYPATH')) {
$this->_copy_dir($cmspath.'MYPATH', MYPATH);
}
if (is_dir($cmspath.'COREPATH')) {
$this->_copy_dir($cmspath.'COREPATH', COREPATH);
}
}
dr_dir_delete($cmspath, 1);
$this->_json(1, '<p><label class="rleft">升级完成</label><label class="rright"><span class="ok">完成</span></label></p>');
}
private function _copy_dir($src, $dst) {
$dir = opendir($src);
if (!is_dir($dst)) {
@mkdir($dst);
}
$src = rtrim($src, '/');
$dst = rtrim($dst, '/');
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if (is_dir($src . '/' . $file) ) {
dr_mkdirs($dst . '/' . $file);
$this->_copy_dir($src . '/' . $file, $dst . '/' . $file);
continue;
} else {
dr_mkdirs(dirname($dst . '/' . $file));
$rt = copy($src . '/' . $file, $dst . '/' . $file);
if (!$rt) {
if (filesize($src . '/' . $file) > 1) {
$this->_error_msg($dst . '/' . $file, '移动失败');
}
}
}
}
}
closedir($dir);
}
function log_show() {
$url = 'https://www.kaiqibao.net/version.php?id='.\Phpcmf\Service::L('input')->get('id', true).'&version='.\Phpcmf\Service::L('input')->get('version', true);
\Phpcmf\Service::V()->assign([
'url' => $url,
]);
\Phpcmf\Service::V()->display('cloud_online.html');exit;
}
private function _get_app_version() {
$data = [];
$data = [];
$data['cms-1'] = $this->cmf_version['version'];
$local = dr_dir_map(APPSPATH, 1);
foreach ($local as $dir) {
if (is_file(APPSPATH.$dir.'/Config/App.php') && is_file(APPSPATH.$dir.'/Config/Version.php')) {
$cfg = require APPSPATH.$dir.'/Config/App.php';
if ($cfg['type'] != 'module') {
$vsn = require APPSPATH.$dir.'/Config/Version.php';
$vsn['id'] && $data[$cfg['type'].'-'.$vsn['id']] = $vsn['version'];
}
}
}
return $data;
}
private function _error_msg($filename, $msg) {
$html = '<p class=" p_error"><label class="rleft">'.$filename.'</label><label class="rright">'.$msg.'</label></p>';
$this->_json(0, $html);
}
}