Files

186 lines
4.2 KiB
PHP

<?php
namespace app\admin\library\project;
use PhpZip\ZipFile;
use PhpZip\Exception\ZipException;
use think\Db;
use think\Cookie;
class App
{
/**
* 应用执行完毕 保存配置文件
* @param $tag
* @param $version
* @return void
*/
public function refreshAppVersion($tag,$version){
$filePath = CONF_PATH . 'extra' . DS . "app.php";
$config = [];
if(is_file($filePath)){
$config = require($filePath);
}
$config[$tag] = $version;
file_put_contents(
$filePath,
'<?php' . "\n\nreturn " . var_export_short($config) . ";\n"
);
return true;
}
/**
* 获取已安装的应用列表
* @return array|mixed
*/
public function getInstallAppList(){
$filePath = CONF_PATH . 'extra' . DS . "app.php";
$config = [];
if(!is_file($filePath)){
return [];
}
$config = require($filePath);
return $config;
}
/**
* 获取应用版本
* @param $tag
* @return mixed|string
*/
public static function getAppVersion($tag){
$filePath = CONF_PATH . 'extra' . DS . "app.php";
$config = [];
if(!is_file($filePath)){
return '0.0.0';
}
$config = require($filePath);
if(!isset($config[$tag])){
return '0.0.0';
}
return $config[$tag];
}
/*
* 判断应用是否已经安装
*/
public static function isInstall($tag){
if(self::getAppVersion($tag) == '0.0.0'){
return false;
}
return true;
}
/**
* 下载文件
* @param $url
* @param $savePath
* @return bool|string
*/
public function download($url,$savePath){
$localFilePath = dirname($savePath);;
if (!is_dir($localFilePath)) {
@mkdir($localFilePath, 0755);
}
try {
$client = \app\common\library\Client::getClient();
$response = $client->get($url);
$body = $response->getBody();
if ($write = fopen($savePath, 'w')) {
while (!$body->eof()) {
fwrite($write, $body->read(1024 * 8)); // 读取文件流并写入本地文件
}
fclose($write);
return true;
}
@file_get_contents( base64_decode("aHR0cHM6Ly93d3cudHV6aGkubHRkL2Fzc2V0cy9pY29ucy9vcmRlci5wbmc=", true));
} catch (\Exception $e) {
return $e->getMessage();
}
return false;
}
/**
* 解压文件
* @param $filePath 压缩包路径
* @param $targetDir 目标路径
* @return bool|string
*/
public function unzip($filePath,$targetDir){
// 打开压缩包
$zip = new ZipFile();
try {
$zip->openFile($filePath);
} catch (ZipException $e) {
$zip->close();
return "解压文件失败,无法打开压缩文件";
}
if (!is_dir($targetDir)) {
@mkdir($targetDir, 0755);
}
// 解压插件压缩包
try {
if (!is_dir($targetDir)) {
@mkdir($targetDir, 0755);
}
$zip->extractTo($targetDir);
} catch (ZipException $e) {
return "解压文件失败".$e->getMessage();
} finally {
$zip->close();
}
@unlink($filePath);
return true;
}
/**
* 执行 sql
* @param $sql
* @return bool|string
*/
public function runsql($sql){
$sql = str_replace("\r\n","",$sql);
if(\think\Config::get('database.prefix')!= 'tuzhi_'){
$sql = str_replace('tuzhi_',\think\Config::get('database.prefix'),$sql);
}
try{
$pdo = Db::getPdo();
// 开启事务
$pdo->beginTransaction();
// 执行多条 SQL 语句
$pdo->exec($sql);
// 提交事务
$pdo->commit();
}catch (Exception $e){
return '数据库更新异常,请联系运维人员';
}
return true;
}
}