Allow logging into cli.php by giving an existing sessionID
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 1 Jul 2020 09:40:18 +0000 (11:40 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 2 Jul 2020 10:50:51 +0000 (12:50 +0200)
wcfsetup/install/files/lib/system/CLIWCF.class.php

index 15353cfae9750da53bdaf6173aaaf6ed37c3fba0..6a22658fcab918e1a17496cba547d43d095a476f 100644 (file)
@@ -3,6 +3,7 @@ namespace wcf\system;
 use phpline\console\ConsoleReader;
 use phpline\internal\Log;
 use phpline\TerminalFactory;
+use wcf\data\session\SessionEditor;
 use wcf\system\cli\command\CLICommandHandler;
 use wcf\system\cli\command\CLICommandNameCompleter;
 use wcf\system\cli\DatabaseCLICommandHistory;
@@ -28,7 +29,7 @@ set_exception_handler([CLIWCF::class, 'handleCLIException']);
  * Extends WCF class with functions for CLI.
  * 
  * @author     Tim Duesterhus
- * @copyright  2001-2019 WoltLab GmbH
+ * @copyright  2001-2020 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @package    WoltLabSuite\Core\System
  */
@@ -86,7 +87,7 @@ class CLIWCF extends WCF {
                
                $this->initArgv();
                $this->initPHPLine();
-               $this->initAuth();
+               $this->initAuth(self::getArgvParser()->sessionID);
                $this->checkForUpdates();
                $this->initCommands();
        }
@@ -95,15 +96,20 @@ class CLIWCF extends WCF {
         * @inheritDoc
         */
        public static function destruct() {
-               if (self::getReader() !== null && self::getReader()->getHistory() instanceof DatabaseCLICommandHistory) {
-                       /** @var DatabaseCLICommandHistory $history */
-                       $history = self::getReader()->getHistory();
-                       
-                       $history->save();
-                       $history->autoSave = false;
+               // Giving a sessionID disables saving of the command history.
+               if (!self::getArgvParser()->sessionID) {
+                       if (self::getReader() !== null && self::getReader()->getHistory() instanceof DatabaseCLICommandHistory) {
+                               /** @var DatabaseCLICommandHistory $history */
+                               $history = self::getReader()->getHistory();
+                               
+                               $history->save();
+                               $history->autoSave = false;
+                       }
                }
                
-               self::getSession()->delete();
+               if (!self::getArgvParser()->sessionID) {
+                       self::getSession()->delete();
+               }
        }
        
        /**
@@ -126,7 +132,8 @@ class CLIWCF extends WCF {
                        'version' => WCF::getLanguage()->get('wcf.cli.help.version'),
                        'disableUpdateCheck' => WCF::getLanguage()->get('wcf.cli.help.disableUpdateCheck'),
                        'exitOnFail' => WCF::getLanguage()->get('wcf.cli.help.exitOnFail'),
-                       'packageID=i' => WCF::getLanguage()->get('wcf.cli.help.packageID')
+                       'packageID=i' => WCF::getLanguage()->get('wcf.cli.help.packageID'),
+                       'sessionID=s' => 'sessionid',
                ]);
                self::getArgvParser()->setOptions([
                        ArgvParser::CONFIG_CUMULATIVE_FLAGS => true,
@@ -227,30 +234,40 @@ class CLIWCF extends WCF {
        /**
         * Does the user authentification.
         */
-       protected function initAuth() {
-               do {
-                       $line = self::getReader()->readLine(WCF::getLanguage()->get('wcf.user.username').'> ');
-                       if ($line === null) exit;
-                       $username = StringUtil::trim($line);
-               }
-               while ($username === '');
-               
-               do {
-                       $line = self::getReader()->readLine(WCF::getLanguage()->get('wcf.user.password').'> ', '*');
-                       if ($line === null) exit;
-                       $password = StringUtil::trim($line);
-               }
-               while ($password === '');
-               
-               // check credentials and switch user
-               try {
-                       $user = UserAuthenticationFactory::getInstance()->getUserAuthentication()->loginManually($username, $password);
-                       WCF::getSession()->changeUser($user);
+       protected function initAuth($sessionID = null) {
+               if ($sessionID !== null) {
+                       self::getSession()->delete();
+                       self::getSession()->load(SessionEditor::class, $sessionID);
+                       if (!self::getUser()->userID) {
+                               self::getReader()->println('Invalid sessionID');
+                               exit(1);
+                       }
                }
-               catch (UserInputException $e) {
-                       $message = WCF::getLanguage()->getDynamicVariable('wcf.user.'.$e->getField().'.error.'.$e->getType(), ['username' => $username]);
-                       self::getReader()->println($message);
-                       exit(1);
+               else {
+                       do {
+                               $line = self::getReader()->readLine(WCF::getLanguage()->get('wcf.user.username').'> ');
+                               if ($line === null) exit;
+                               $username = StringUtil::trim($line);
+                       }
+                       while ($username === '');
+                       
+                       do {
+                               $line = self::getReader()->readLine(WCF::getLanguage()->get('wcf.user.password').'> ', '*');
+                               if ($line === null) exit;
+                               $password = StringUtil::trim($line);
+                       }
+                       while ($password === '');
+                       
+                       // check credentials and switch user
+                       try {
+                               $user = UserAuthenticationFactory::getInstance()->getUserAuthentication()->loginManually($username, $password);
+                               WCF::getSession()->changeUser($user);
+                       }
+                       catch (UserInputException $e) {
+                               $message = WCF::getLanguage()->getDynamicVariable('wcf.user.'.$e->getField().'.error.'.$e->getType(), ['username' => $username]);
+                               self::getReader()->println($message);
+                               exit(1);
+                       }
                }
                
                // initialize history