|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BookStack\Console\Commands; |
| 4 | + |
| 5 | +use BookStack\Http\HttpRequestService; |
| 6 | +use BookStack\Theming\ThemeModule; |
| 7 | +use BookStack\Theming\ThemeModuleException; |
| 8 | +use BookStack\Theming\ThemeModuleManager; |
| 9 | +use BookStack\Theming\ThemeModuleZip; |
| 10 | +use Illuminate\Console\Command; |
| 11 | +use Illuminate\Support\Str; |
| 12 | + |
| 13 | +class InstallModuleCommand extends Command |
| 14 | +{ |
| 15 | + /** |
| 16 | + * The name and signature of the console command. |
| 17 | + * |
| 18 | + * @var string |
| 19 | + */ |
| 20 | + protected $signature = 'bookstack:install-module |
| 21 | + {location : The URL or path of the module file}'; |
| 22 | + |
| 23 | + /** |
| 24 | + * The console command description. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $description = 'Install a module to the currently configured theme'; |
| 29 | + |
| 30 | + protected array $cleanupActions = []; |
| 31 | + |
| 32 | + /** |
| 33 | + * Execute the console command. |
| 34 | + */ |
| 35 | + public function handle(): int |
| 36 | + { |
| 37 | + $location = $this->argument('location'); |
| 38 | + |
| 39 | + // Get the ZIP file containing the module files |
| 40 | + $zipPath = $this->getPathToZip($location); |
| 41 | + if (!$zipPath) { |
| 42 | + $this->cleanup(); |
| 43 | + return 1; |
| 44 | + } |
| 45 | + |
| 46 | + // Validate module zip file (metadata, size, etc...) and get module instance |
| 47 | + $zip = new ThemeModuleZip($zipPath); |
| 48 | + $themeModule = $this->validateAndGetModuleInfoFromZip($zip); |
| 49 | + if (!$themeModule) { |
| 50 | + $this->cleanup(); |
| 51 | + return 1; |
| 52 | + } |
| 53 | + |
| 54 | + // Get the theme folder in use, attempting to create one if no active theme in use |
| 55 | + $themeFolder = $this->getThemeFolder(); |
| 56 | + if (!$themeFolder) { |
| 57 | + $this->cleanup(); |
| 58 | + return 1; |
| 59 | + } |
| 60 | + |
| 61 | + // Get the modules folder of the theme, attempting to create it if not existing, |
| 62 | + // and create a new module manager instance. |
| 63 | + $moduleFolder = $this->getModuleFolder($themeFolder); |
| 64 | + $manager = new ThemeModuleManager($moduleFolder); |
| 65 | + |
| 66 | + // Handle existing modules with the same name |
| 67 | + $exitingModulesWithName = $manager->getByName($themeModule->name); |
| 68 | + $shouldContinue = $this->handleExistingModulesWithSameName($exitingModulesWithName, $manager); |
| 69 | + if (!$shouldContinue) { |
| 70 | + $this->cleanup(); |
| 71 | + return 1; |
| 72 | + } |
| 73 | + |
| 74 | + // Extract module ZIP into the theme modules folder |
| 75 | + try { |
| 76 | + $newModule = $manager->addFromZip($themeModule->name, $zip); |
| 77 | + } catch (ThemeModuleException $exception) { |
| 78 | + $this->error("ERROR: Failed to install module with error: {$exception->getMessage()}"); |
| 79 | + $this->cleanup(); |
| 80 | + return 1; |
| 81 | + } |
| 82 | + |
| 83 | + $this->info("Module {$newModule->name} ({$newModule->version}) successfully installed!"); |
| 84 | + $this->info("It has been installed at {$moduleFolder}/{$newModule->folderName}."); |
| 85 | + $this->cleanup(); |
| 86 | + return 0; |
| 87 | + } |
| 88 | + |
| 89 | + protected function handleExistingModulesWithSameName(array $existingModules, ThemeModuleManager $manager): bool |
| 90 | + { |
| 91 | + if (count($existingModules) === 0) { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + $this->warn("The following modules already exist with the same name:"); |
| 96 | + foreach ($existingModules as $folder => $module) { |
| 97 | + $this->line("{$module->name} ({$folder}:{$module->version}) - {$module->description}}"); |
| 98 | + } |
| 99 | + $this->line(''); |
| 100 | + |
| 101 | + $choices = ['Cancel Module Install', 'Add Alongside Existing']; |
| 102 | + if (count($existingModules) === 1) { |
| 103 | + $choices[] = 'Replace Existing Module'; |
| 104 | + } |
| 105 | + $choice = $this->choice("What would you like to do?", $choices, 0, null, false); |
| 106 | + if ($choice === 'Cancel Module Install') { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + if ($choice === 'Replace Existing Module') { |
| 111 | + $existingModuleFolder = array_key_first($existingModules); |
| 112 | + $this->info("Replacing existing module in {$existingModuleFolder} folder"); |
| 113 | + $manager->deleteModuleFolder($existingModuleFolder); |
| 114 | + } |
| 115 | + |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + protected function getModuleFolder(string $themeFolder): string|null |
| 120 | + { |
| 121 | + $path = $themeFolder . DIRECTORY_SEPARATOR . 'modules'; |
| 122 | + if (!file_exists($path)) { |
| 123 | + if (!is_dir($path)) { |
| 124 | + $this->error("ERROR: Cannot create a modules folder, file already exists at {$path}"); |
| 125 | + } |
| 126 | + |
| 127 | + $created = mkdir($path, 0755, true); |
| 128 | + if (!$created) { |
| 129 | + $this->error("ERROR: Failed to create a modules folder at {$path}"); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return $path; |
| 134 | + } |
| 135 | + |
| 136 | + protected function getThemeFolder(): string|null |
| 137 | + { |
| 138 | + $path = theme_path(''); |
| 139 | + if (!$path) { |
| 140 | + $shouldCreate = $this->confirm('No active theme folder found, would you like to create one?'); |
| 141 | + if (!$shouldCreate) { |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + $folder = 'custom'; |
| 146 | + while (file_exists(base_path("themes" . DIRECTORY_SEPARATOR . $folder))) { |
| 147 | + $folder = 'custom-' . Str::random(4); |
| 148 | + } |
| 149 | + |
| 150 | + $path = base_path("themes/{$folder}"); |
| 151 | + $created = mkdir($path, 0755, true); |
| 152 | + if (!$created) { |
| 153 | + $this->error('Failed to create a theme folder to use. This may be a permissions issue. Try manually configuring an active theme.'); |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + $this->info("Created theme folder at {$path}"); |
| 158 | + $this->warn("You will need to set APP_THEME={$folder} in your BookStack env configuration to enable this theme!"); |
| 159 | + } |
| 160 | + |
| 161 | + return $path; |
| 162 | + } |
| 163 | + |
| 164 | + protected function validateAndGetModuleInfoFromZip(ThemeModuleZip $zip): ThemeModule|null |
| 165 | + { |
| 166 | + if (!$zip->exists()) { |
| 167 | + $this->error("ERROR: Cannot open ZIP file at {$zip->getPath()}"); |
| 168 | + return null; |
| 169 | + } |
| 170 | + |
| 171 | + if ($zip->getContentsSize() > (50 * 1024 * 1024)) { |
| 172 | + $this->error("ERROR: Module ZIP file is too large. Maximum size is 50MB."); |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + try { |
| 177 | + $themeModule = $zip->getModuleInstance(); |
| 178 | + } catch (ThemeModuleException $exception) { |
| 179 | + $this->error("ERROR: Failed to read module metadata with error: {$exception->getMessage()}"); |
| 180 | + return null; |
| 181 | + } |
| 182 | + |
| 183 | + return $themeModule; |
| 184 | + } |
| 185 | + |
| 186 | + protected function downloadModuleFile(string $location): string |
| 187 | + { |
| 188 | + $httpRequests = app()->make(HttpRequestService::class); |
| 189 | + $client = $httpRequests->buildClient(30); |
| 190 | + |
| 191 | + $resp = $client->get($location, ['stream' => true]); |
| 192 | + |
| 193 | + $tempFile = tempnam(sys_get_temp_dir(), 'bookstack_module_'); |
| 194 | + $fileHandle = fopen($tempFile, 'w'); |
| 195 | + |
| 196 | + stream_copy_to_stream($resp->getBody()->detach(), $fileHandle); |
| 197 | + |
| 198 | + fclose($fileHandle); |
| 199 | + |
| 200 | + $this->cleanupActions[] = function () use ($tempFile) { |
| 201 | + unlink($tempFile); |
| 202 | + }; |
| 203 | + |
| 204 | + return $tempFile; |
| 205 | + } |
| 206 | + |
| 207 | + protected function getPathToZip(string $location): string|null |
| 208 | + { |
| 209 | + $lowerLocation = strtolower($location); |
| 210 | + $isRemote = str_starts_with($lowerLocation, 'http://') || str_starts_with($lowerLocation, 'https://'); |
| 211 | + |
| 212 | + if ($isRemote) { |
| 213 | + // Warning about fetching from source |
| 214 | + $host = parse_url($location, PHP_URL_HOST); |
| 215 | + $this->warn("This will download a module from {$host}. Modules can contain code which would have the ability to do anything on the BookStack host server."); |
| 216 | + $trustHost = $this->confirm('Are you sure you trust this source?'); |
| 217 | + if (!$trustHost) { |
| 218 | + return null; |
| 219 | + } |
| 220 | + |
| 221 | + // Check if the connection is http. If so, warn the user. |
| 222 | + if (str_starts_with($lowerLocation, 'http://')) { |
| 223 | + $this->warn('You are downloading a module from an insecure HTTP source. We recommend using HTTPS sources.'); |
| 224 | + if (!$this->confirm('Do you wish to continue?')) { |
| 225 | + return null; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + // Download ZIP and get its location |
| 230 | + return $this->downloadModuleFile($location); |
| 231 | + } |
| 232 | + |
| 233 | + // Validate file and get full location |
| 234 | + $zipPath = realpath($location); |
| 235 | + if (!$zipPath || !is_file($zipPath)) { |
| 236 | + $this->error("ERROR: Module file not found at {$location}"); |
| 237 | + return null; |
| 238 | + } |
| 239 | + |
| 240 | + return $zipPath; |
| 241 | + } |
| 242 | + |
| 243 | + protected function cleanup(): void |
| 244 | + { |
| 245 | + foreach ($this->cleanupActions as $action) { |
| 246 | + $action(); |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments