From 4260ee14e37bcecd58de9f478c4b2b9ee27b4c39 Mon Sep 17 00:00:00 2001
From: Karson <karsonzhang@163.com>
Date: Wed, 7 Feb 2018 23:49:17 +0800
Subject: [PATCH] 新增命令行插件升级和打包 修复后台保持登录不起作用的BUG 优化安装程序的文字提示

---
 .gitignore                                     |  5 ++---
 application/admin/command/Addon.php            | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
 application/admin/command/Install/install.lock |  1 +
 application/admin/library/Auth.php             |  2 +-
 public/install.php                             | 10 +++++-----
 public/uploads/.gitkeep                        |  1 +
 runtime/.gitkeep                               |  1 +
 7 files changed, 85 insertions(+), 19 deletions(-)
 create mode 100644 application/admin/command/Install/install.lock
 create mode 100644 public/uploads/.gitkeep
 create mode 100644 runtime/.gitkeep

diff --git a/.gitignore b/.gitignore
index cd13b27..b883f26 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,12 +1,11 @@
 /nbproject/
 /thinkphp/
 /vendor/
-/runtime/
+/runtime/*
 /addons/*
-/application/admin/command/Install/*.lock
 /public/assets/libs/
 /public/assets/addons/*
-/public/uploads/
+/public/uploads/*
 .idea
 composer.lock
 *.log
diff --git a/application/admin/command/Addon.php b/application/admin/command/Addon.php
index c1283f6..216ac47 100644
--- a/application/admin/command/Addon.php
+++ b/application/admin/command/Addon.php
@@ -19,8 +19,9 @@ class Addon extends Command
         $this
                 ->setName('addon')
                 ->addOption('name', 'a', Option::VALUE_REQUIRED, 'addon name', null)
-                ->addOption('action', 'c', Option::VALUE_REQUIRED, 'action(create/enable/disable/install/uninstall/refresh)', 'create')
+                ->addOption('action', 'c', Option::VALUE_REQUIRED, 'action(create/enable/disable/install/uninstall/refresh/upgrade/package)', 'create')
                 ->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force override', null)
+                ->addOption('release', 'r', Option::VALUE_OPTIONAL, 'addon release version', null)
                 ->setDescription('Addon manager');
     }
 
@@ -30,6 +31,8 @@ class Addon extends Command
         $action = $input->getOption('action') ?: '';
         //强制覆盖
         $force = $input->getOption('force');
+        //版本
+        $release = $input->getOption('release') ?: '';
 
         include dirname(__DIR__) . DS . 'common.php';
 
@@ -37,15 +40,15 @@ class Addon extends Command
         {
             throw new Exception('Addon name could not be empty');
         }
-        if (!$action || !in_array($action, ['create', 'disable', 'enable', 'install', 'uninstall', 'refresh']))
+        if (!$action || !in_array($action, ['create', 'disable', 'enable', 'install', 'uninstall', 'refresh', 'upgrade', 'package']))
         {
             throw new Exception('Please input correct action name');
         }
-        
+
         // 查询一次SQL,判断连接是否正常
         Db::execute("SELECT 1");
-        
-        $addonDir = ADDON_PATH . $name;
+
+        $addonDir = ADDON_PATH . $name . DS;
         switch ($action)
         {
             case 'create':
@@ -65,9 +68,9 @@ class Addon extends Command
                     'addon'          => $name,
                     'addonClassName' => ucfirst($name)
                 ];
-                $this->writeToFile("addon", $data, $addonDir . DS . ucfirst($name) . '.php');
-                $this->writeToFile("config", $data, $addonDir . DS . 'config.php');
-                $this->writeToFile("info", $data, $addonDir . DS . 'info.ini');
+                $this->writeToFile("addon", $data, $addonDir . ucfirst($name) . '.php');
+                $this->writeToFile("config", $data, $addonDir . 'config.php');
+                $this->writeToFile("info", $data, $addonDir . 'info.ini');
                 $output->info("Create Successed!");
                 break;
             case 'disable':
@@ -117,7 +120,7 @@ class Addon extends Command
                 }
                 try
                 {
-                    Service::install($name, 0);
+                    Service::install($name, 0, ['version' => $release]);
                 }
                 catch (AddonException $e)
                 {
@@ -137,7 +140,7 @@ class Addon extends Command
                     {
                         throw new Exception("Operation is aborted!");
                     }
-                    Service::install($name, 1);
+                    Service::install($name, 1, ['version' => $release]);
                 }
                 catch (Exception $e)
                 {
@@ -187,6 +190,67 @@ class Addon extends Command
                 Service::refresh();
                 $output->info("Refresh Successed!");
                 break;
+            case 'upgrade':
+                Service::upgrade($name, ['version' => $release]);
+                $output->info("Upgrade Successed!");
+                break;
+            case 'package':
+                $infoFile = $addonDir . 'info.ini';
+                if (!is_file($infoFile))
+                {
+                    throw new Exception(__('Addon info file was not found'));
+                }
+
+                $info = get_addon_info($name);
+                if (!$info)
+                {
+                    throw new Exception(__('Addon info file data incorrect'));
+                }
+                $infoname = isset($info['name']) ? $info['name'] : '';
+                if (!$infoname || !preg_match("/^[a-z]+$/i", $infoname) || $infoname != $name)
+                {
+                    throw new Exception(__('Addon info name incorrect'));
+                }
+
+                $infoversion = isset($info['version']) ? $info['version'] : '';
+                if (!$infoversion || !preg_match("/^\d+\.\d+\.\d+$/i", $infoversion))
+                {
+                    throw new Exception(__('Addon info version incorrect'));
+                }
+
+                $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
+                if (!is_dir($addonTmpDir))
+                {
+                    @mkdir($addonTmpDir, 0755, true);
+                }
+                $addonFile = $addonTmpDir . $infoname . '-' . $infoversion . '.zip';
+                if (!class_exists('ZipArchive'))
+                {
+                    throw new Exception(__('ZinArchive not install'));
+                }
+                $zip = new \ZipArchive;
+                $zip->open($addonFile, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
+
+                $files = new \RecursiveIteratorIterator(
+                        new \RecursiveDirectoryIterator($addonDir), \RecursiveIteratorIterator::LEAVES_ONLY
+                );
+
+                foreach ($files as $name => $file)
+                {
+                    if (!$file->isDir())
+                    {
+                        $filePath = $file->getRealPath();
+                        $relativePath = substr($filePath, strlen($addonDir));
+                        if (!in_array($file->getFilename(), ['.git', '.DS_Store', 'Thumbs.db']))
+                        {
+                            $zip->addFile($filePath, $relativePath);
+                        }
+                    }
+                }
+                $zip->close();
+                $output->info("Package Successed!");
+                break;
+
             default :
                 break;
         }
diff --git a/application/admin/command/Install/install.lock b/application/admin/command/Install/install.lock
new file mode 100644
index 0000000..56a6051
--- /dev/null
+++ b/application/admin/command/Install/install.lock
@@ -0,0 +1 @@
+1
\ No newline at end of file
diff --git a/application/admin/library/Auth.php b/application/admin/library/Auth.php
index 00a9974..7ff03d9 100644
--- a/application/admin/library/Auth.php
+++ b/application/admin/library/Auth.php
@@ -126,7 +126,7 @@ class Auth extends \fast\Auth
             $expiretime = time() + $keeptime;
             $key = md5(md5($this->id) . md5($keeptime) . md5($expiretime) . $this->token);
             $data = [$this->id, $keeptime, $expiretime, $key];
-            Cookie::set('keeplogin', implode('|', $data));
+            Cookie::set('keeplogin', implode('|', $data), 86400 * 30);
             return true;
         }
         return false;
diff --git a/public/install.php b/public/install.php
index fea5ba0..cd65321 100644
--- a/public/install.php
+++ b/public/install.php
@@ -77,7 +77,7 @@ else if (!extension_loaded("PDO"))
 }
 else if (!is_really_writable($dbConfigFile))
 {
-    $errInfo = "当前权限不足,无法写入配置文件application/database.php";
+    $errInfo = '当前权限不足,无法写入配置文件application/database.php<br><a href="http://forum.fastadmin.net/?q=%E6%9D%83%E9%99%90%E4%B8%8D%E8%B6%B3" target="_blank">点击查看解决办法</a>';
 }
 else
 {
@@ -86,7 +86,7 @@ else
     {
         if (!is_dir(ROOT_PATH . $v))
         {
-            $errInfo = '当前代码不完整,请加入QQ群(<a href="' . $link['qqun'] . '" target="_blank">636393962</a>),在群共享免费下载FastAdmin完整包后再尝试安装';
+            $errInfo = '当前代码仅包含核心代码,请前往官网下载完整包或资源包覆盖后再尝试安装,<a href="http://www.fastadmin.net/download.html?ref=install" target="_blank">立即前往下载</a>';
             break;
         }
     }
@@ -95,7 +95,7 @@ else
 if (!$errInfo && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST')
 {
     $err = '';
-    $mysqlHostname = isset($_POST['mysqlHost']) ? $_POST['mysqlHost'] : 'localhost';
+    $mysqlHostname = isset($_POST['mysqlHost']) ? $_POST['mysqlHost'] : '127.0.0.1';
     $mysqlHostport = 3306;
     $hostArr = explode(':', $mysqlHostname);
     if (count($hostArr) > 1)
@@ -330,7 +330,7 @@ if (!$errInfo && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD']
             <h2>安装 <?php echo $sitename; ?></h2>
             <div>
 
-                <p>若你在安装中遇到麻烦可点击  <a href="<?php echo $link['doc']; ?>" target="_blank">安装文档</a> <a href="<?php echo $link['forum']; ?>" target="_blank">交流论坛</a> <a href="<?php echo $link['qqun']; ?>">QQ交流群</a></p>
+                <p>若你在安装中遇到麻烦可点击  <a href="<?php echo $link['doc']; ?>" target="_blank">安装文档</a> <a href="<?php echo $link['forum']; ?>" target="_blank">交流社区</a> <a href="<?php echo $link['qqun']; ?>">QQ交流群</a></p>
                 <!--<p><?php echo $sitename; ?>还支持在命令行php think install一键安装</p>-->
 
                 <form method="post">
@@ -345,7 +345,7 @@ if (!$errInfo && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD']
                     <div class="form-group">
                         <div class="form-field">
                             <label>MySQL 数据库地址</label>
-                            <input type="text" name="mysqlHost" value="localhost" required="">
+                            <input type="text" name="mysqlHost" value="127.0.0.1" required="">
                         </div>
 
                         <div class="form-field">
diff --git a/public/uploads/.gitkeep b/public/uploads/.gitkeep
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/public/uploads/.gitkeep
@@ -0,0 +1 @@
+
diff --git a/runtime/.gitkeep b/runtime/.gitkeep
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/runtime/.gitkeep
@@ -0,0 +1 @@
+
--
libgit2 0.24.0