1cf2dcf1bSAndreas Gohr<?php 2cf2dcf1bSAndreas Gohr 3cf2dcf1bSAndreas Gohrnamespace dokuwiki\plugin\extension; 4cf2dcf1bSAndreas Gohr 5cf2dcf1bSAndreas Gohruse dokuwiki\HTTP\DokuHTTPClient; 6cf2dcf1bSAndreas Gohruse dokuwiki\Utf8\PhpString; 7cf2dcf1bSAndreas Gohruse RecursiveDirectoryIterator; 8cf2dcf1bSAndreas Gohruse RecursiveIteratorIterator; 9cf2dcf1bSAndreas Gohruse splitbrain\PHPArchive\ArchiveCorruptedException; 10cf2dcf1bSAndreas Gohruse splitbrain\PHPArchive\ArchiveIllegalCompressionException; 11cf2dcf1bSAndreas Gohruse splitbrain\PHPArchive\ArchiveIOException; 12cf2dcf1bSAndreas Gohruse splitbrain\PHPArchive\Tar; 13cf2dcf1bSAndreas Gohruse splitbrain\PHPArchive\Zip; 14cf2dcf1bSAndreas Gohr 15cf2dcf1bSAndreas Gohr/** 16cf2dcf1bSAndreas Gohr * Install and deinstall extensions 17cf2dcf1bSAndreas Gohr * 18cf2dcf1bSAndreas Gohr * This manages all the file operations and downloads needed to install an extension. 19cf2dcf1bSAndreas Gohr */ 20cf2dcf1bSAndreas Gohrclass Installer 21cf2dcf1bSAndreas Gohr{ 22cf2dcf1bSAndreas Gohr /** @var string[] a list of temporary directories used during this installation */ 23cf2dcf1bSAndreas Gohr protected array $temporary = []; 24cf2dcf1bSAndreas Gohr 25cf2dcf1bSAndreas Gohr /** @var bool if changes have been made that require a cache purge */ 26cf2dcf1bSAndreas Gohr protected $isDirty = false; 27cf2dcf1bSAndreas Gohr 28cf2dcf1bSAndreas Gohr /** @var bool Replace existing files? */ 29cf2dcf1bSAndreas Gohr protected $overwrite = false; 30cf2dcf1bSAndreas Gohr 31cf2dcf1bSAndreas Gohr /** @var string The last used URL to install an extension */ 32cf2dcf1bSAndreas Gohr protected $sourceUrl = ''; 33cf2dcf1bSAndreas Gohr 3425d28a01SAndreas Gohr protected $processed = []; 3525d28a01SAndreas Gohr 3625d28a01SAndreas Gohr public const STATUS_SKIPPED = 'skipped'; 3725d28a01SAndreas Gohr public const STATUS_UPDATED = 'updated'; 3825d28a01SAndreas Gohr public const STATUS_INSTALLED = 'installed'; 39*80bc92fbSAndreas Gohr public const STATUS_REMOVED = 'removed'; 4025d28a01SAndreas Gohr 4125d28a01SAndreas Gohr 42cf2dcf1bSAndreas Gohr /** 43cf2dcf1bSAndreas Gohr * Initialize a new extension installer 44cf2dcf1bSAndreas Gohr * 45cf2dcf1bSAndreas Gohr * @param bool $overwrite 46cf2dcf1bSAndreas Gohr */ 47cf2dcf1bSAndreas Gohr public function __construct($overwrite = false) 48cf2dcf1bSAndreas Gohr { 49cf2dcf1bSAndreas Gohr $this->overwrite = $overwrite; 50cf2dcf1bSAndreas Gohr } 51cf2dcf1bSAndreas Gohr 52cf2dcf1bSAndreas Gohr /** 53cf2dcf1bSAndreas Gohr * Destructor 54cf2dcf1bSAndreas Gohr * 55cf2dcf1bSAndreas Gohr * deletes any dangling temporary directories 56cf2dcf1bSAndreas Gohr */ 57cf2dcf1bSAndreas Gohr public function __destruct() 58cf2dcf1bSAndreas Gohr { 5925d28a01SAndreas Gohr foreach ($this->temporary as $dir) { 6025d28a01SAndreas Gohr io_rmdir($dir, true); 6125d28a01SAndreas Gohr } 62cf2dcf1bSAndreas Gohr $this->cleanUp(); 63cf2dcf1bSAndreas Gohr } 64cf2dcf1bSAndreas Gohr 65cf2dcf1bSAndreas Gohr /** 66160d3688SAndreas Gohr * Install an extension by ID 6725d28a01SAndreas Gohr * 68160d3688SAndreas Gohr * This will simply call installExtension after constructing an extension from the ID 6925d28a01SAndreas Gohr * 7025d28a01SAndreas Gohr * The $skipInstalled parameter should only be used when installing dependencies 7125d28a01SAndreas Gohr * 7225d28a01SAndreas Gohr * @param string $id the extension ID 7325d28a01SAndreas Gohr * @param bool $skipInstalled Ignore the overwrite setting and skip installed extensions 7425d28a01SAndreas Gohr * @throws Exception 7525d28a01SAndreas Gohr */ 76160d3688SAndreas Gohr public function installFromId($id, $skipInstalled = false) 77160d3688SAndreas Gohr { 7825d28a01SAndreas Gohr $extension = Extension::createFromId($id); 7925d28a01SAndreas Gohr if ($skipInstalled && $extension->isInstalled()) return; 80160d3688SAndreas Gohr $this->installExtension($extension); 81160d3688SAndreas Gohr } 82160d3688SAndreas Gohr 83160d3688SAndreas Gohr /** 84160d3688SAndreas Gohr * Install an extension 85160d3688SAndreas Gohr * 86160d3688SAndreas Gohr * This will simply call installFromUrl() with the URL from the extension 87160d3688SAndreas Gohr * 88160d3688SAndreas Gohr * @param Extension $extension 89160d3688SAndreas Gohr * @throws Exception 90160d3688SAndreas Gohr */ 91160d3688SAndreas Gohr public function installExtension(Extension $extension) 92160d3688SAndreas Gohr { 9325d28a01SAndreas Gohr $url = $extension->getDownloadURL(); 9425d28a01SAndreas Gohr if (!$url) { 9525d28a01SAndreas Gohr throw new Exception('error_nourl', [$extension->getId()]); 9625d28a01SAndreas Gohr } 9725d28a01SAndreas Gohr $this->installFromUrl($url); 9825d28a01SAndreas Gohr } 9925d28a01SAndreas Gohr 10025d28a01SAndreas Gohr /** 101cf2dcf1bSAndreas Gohr * Install extensions from a given URL 102cf2dcf1bSAndreas Gohr * 103cf2dcf1bSAndreas Gohr * @param string $url the URL to the archive 104cf2dcf1bSAndreas Gohr * @param null $base the base directory name to use 105cf2dcf1bSAndreas Gohr * @throws Exception 106cf2dcf1bSAndreas Gohr */ 107cf2dcf1bSAndreas Gohr public function installFromUrl($url, $base = null) 108cf2dcf1bSAndreas Gohr { 109cf2dcf1bSAndreas Gohr $this->sourceUrl = $url; 110cf2dcf1bSAndreas Gohr $archive = $this->downloadArchive($url); 111cf2dcf1bSAndreas Gohr $this->installFromArchive( 112cf2dcf1bSAndreas Gohr $archive, 113cf2dcf1bSAndreas Gohr $base 114cf2dcf1bSAndreas Gohr ); 115cf2dcf1bSAndreas Gohr } 116cf2dcf1bSAndreas Gohr 117cf2dcf1bSAndreas Gohr /** 118cf2dcf1bSAndreas Gohr * Install extensions from a user upload 119cf2dcf1bSAndreas Gohr * 120cf2dcf1bSAndreas Gohr * @param string $field name of the upload file 121cf2dcf1bSAndreas Gohr * @throws Exception 122cf2dcf1bSAndreas Gohr */ 123cf2dcf1bSAndreas Gohr public function installFromUpload($field) 124cf2dcf1bSAndreas Gohr { 125cf2dcf1bSAndreas Gohr $this->sourceUrl = ''; 126cf2dcf1bSAndreas Gohr if ($_FILES[$field]['error']) { 127cf2dcf1bSAndreas Gohr throw new Exception('msg_upload_failed', [$_FILES[$field]['error']]); 128cf2dcf1bSAndreas Gohr } 129cf2dcf1bSAndreas Gohr 130cf2dcf1bSAndreas Gohr $tmp = $this->mkTmpDir(); 131cf2dcf1bSAndreas Gohr if (!move_uploaded_file($_FILES[$field]['tmp_name'], "$tmp/upload.archive")) { 132cf2dcf1bSAndreas Gohr throw new Exception('msg_upload_failed', ['move failed']); 133cf2dcf1bSAndreas Gohr } 134cf2dcf1bSAndreas Gohr $this->installFromArchive( 135cf2dcf1bSAndreas Gohr "$tmp/upload.archive", 136cf2dcf1bSAndreas Gohr $this->fileToBase($_FILES[$field]['name']), 137cf2dcf1bSAndreas Gohr ); 138cf2dcf1bSAndreas Gohr } 139cf2dcf1bSAndreas Gohr 140cf2dcf1bSAndreas Gohr /** 141cf2dcf1bSAndreas Gohr * Install extensions from an archive 142cf2dcf1bSAndreas Gohr * 143cf2dcf1bSAndreas Gohr * The archive is extracted to a temporary directory and then the contained extensions are installed. 144cf2dcf1bSAndreas Gohr * This is is the ultimate installation procedure and all other install methods will end up here. 145cf2dcf1bSAndreas Gohr * 146cf2dcf1bSAndreas Gohr * @param string $archive the path to the archive 147cf2dcf1bSAndreas Gohr * @param string $base the base directory name to use 148cf2dcf1bSAndreas Gohr * @throws Exception 149cf2dcf1bSAndreas Gohr */ 150cf2dcf1bSAndreas Gohr public function installFromArchive($archive, $base = null) 151cf2dcf1bSAndreas Gohr { 152cf2dcf1bSAndreas Gohr if ($base === null) $base = $this->fileToBase($archive); 153cf2dcf1bSAndreas Gohr $target = $this->mkTmpDir() . '/' . $base; 154cf2dcf1bSAndreas Gohr $this->extractArchive($archive, $target); 155cf2dcf1bSAndreas Gohr $extensions = $this->findExtensions($target, $base); 156cf2dcf1bSAndreas Gohr foreach ($extensions as $extension) { 15725d28a01SAndreas Gohr // check installation status 15825d28a01SAndreas Gohr if ($extension->isInstalled()) { 15925d28a01SAndreas Gohr if (!$this->overwrite) { 16025d28a01SAndreas Gohr $this->processed[$extension->getId()] = self::STATUS_SKIPPED; 161cf2dcf1bSAndreas Gohr continue; 162cf2dcf1bSAndreas Gohr } 16325d28a01SAndreas Gohr $status = self::STATUS_UPDATED; 16425d28a01SAndreas Gohr } else { 16525d28a01SAndreas Gohr $status = self::STATUS_INSTALLED; 16625d28a01SAndreas Gohr } 167cf2dcf1bSAndreas Gohr 168b2a05b76SAndreas Gohr // check PHP requirements 1694fd6a1d7SAndreas Gohr self::ensurePhpCompatibility($extension); 17025d28a01SAndreas Gohr 17125d28a01SAndreas Gohr // install dependencies first 17225d28a01SAndreas Gohr foreach ($extension->getDependencyList() as $id) { 17325d28a01SAndreas Gohr if (isset($this->processed[$id])) continue; 17425d28a01SAndreas Gohr if ($id == $extension->getId()) continue; // avoid circular dependencies 17525d28a01SAndreas Gohr $this->installFromId($id, true); 17625d28a01SAndreas Gohr } 17725d28a01SAndreas Gohr 17825d28a01SAndreas Gohr // now install the extension 1794fd6a1d7SAndreas Gohr self::ensurePermissions($extension); 180cf2dcf1bSAndreas Gohr $this->dircopy( 181cf2dcf1bSAndreas Gohr $extension->getCurrentDir(), 182cf2dcf1bSAndreas Gohr $extension->getInstallDir() 183cf2dcf1bSAndreas Gohr ); 184cf2dcf1bSAndreas Gohr $this->isDirty = true; 1857c9966a5SAndreas Gohr $extension->getManager()->storeUpdate($this->sourceUrl); 186cf2dcf1bSAndreas Gohr $this->removeDeletedFiles($extension); 18725d28a01SAndreas Gohr $this->processed[$extension->getId()] = $status; 188cf2dcf1bSAndreas Gohr } 189cf2dcf1bSAndreas Gohr 190cf2dcf1bSAndreas Gohr $this->cleanUp(); 191cf2dcf1bSAndreas Gohr } 192cf2dcf1bSAndreas Gohr 193cf2dcf1bSAndreas Gohr /** 194cf2dcf1bSAndreas Gohr * Uninstall an extension 195cf2dcf1bSAndreas Gohr * 196cf2dcf1bSAndreas Gohr * @param Extension $extension 197cf2dcf1bSAndreas Gohr * @throws Exception 198cf2dcf1bSAndreas Gohr */ 199cf2dcf1bSAndreas Gohr public function uninstall(Extension $extension) 200cf2dcf1bSAndreas Gohr { 201cf2dcf1bSAndreas Gohr // FIXME check if dependencies are still needed 202*80bc92fbSAndreas Gohr // FIXME check if other extensions depend on this one 203cf2dcf1bSAndreas Gohr 204160d3688SAndreas Gohr if (!$extension->isInstalled()) { 205160d3688SAndreas Gohr throw new Exception('error_notinstalled', [$extension->getId()]); 206160d3688SAndreas Gohr } 207160d3688SAndreas Gohr 208cf2dcf1bSAndreas Gohr if ($extension->isProtected()) { 209cf2dcf1bSAndreas Gohr throw new Exception('error_uninstall_protected', [$extension->getId()]); 210cf2dcf1bSAndreas Gohr } 211cf2dcf1bSAndreas Gohr 2124fd6a1d7SAndreas Gohr self::ensurePermissions($extension); 2134fd6a1d7SAndreas Gohr 214cf2dcf1bSAndreas Gohr if (!io_rmdir($extension->getInstallDir(), true)) { 215cf2dcf1bSAndreas Gohr throw new Exception('msg_delete_failed', [$extension->getId()]); 216cf2dcf1bSAndreas Gohr } 217cf2dcf1bSAndreas Gohr self::purgeCache(); 218*80bc92fbSAndreas Gohr 219*80bc92fbSAndreas Gohr $this->processed[$extension->getId()] = self::STATUS_REMOVED; 220cf2dcf1bSAndreas Gohr } 221cf2dcf1bSAndreas Gohr 222cf2dcf1bSAndreas Gohr /** 223cf2dcf1bSAndreas Gohr * Download an archive to a protected path 224cf2dcf1bSAndreas Gohr * 225cf2dcf1bSAndreas Gohr * @param string $url The url to get the archive from 226cf2dcf1bSAndreas Gohr * @return string The path where the archive was saved 227cf2dcf1bSAndreas Gohr * @throws Exception 228cf2dcf1bSAndreas Gohr */ 229cf2dcf1bSAndreas Gohr public function downloadArchive($url) 230cf2dcf1bSAndreas Gohr { 231cf2dcf1bSAndreas Gohr // check the url 232cf2dcf1bSAndreas Gohr if (!preg_match('/https?:\/\//i', $url)) { 233cf2dcf1bSAndreas Gohr throw new Exception('error_badurl'); 234cf2dcf1bSAndreas Gohr } 235cf2dcf1bSAndreas Gohr 236cf2dcf1bSAndreas Gohr // try to get the file from the path (used as plugin name fallback) 237cf2dcf1bSAndreas Gohr $file = parse_url($url, PHP_URL_PATH); 238cf2dcf1bSAndreas Gohr $file = $file ? PhpString::basename($file) : md5($url); 239cf2dcf1bSAndreas Gohr 240cf2dcf1bSAndreas Gohr // download 241cf2dcf1bSAndreas Gohr $http = new DokuHTTPClient(); 242cf2dcf1bSAndreas Gohr $http->max_bodysize = 0; 243cf2dcf1bSAndreas Gohr $http->timeout = 25; //max. 25 sec 244cf2dcf1bSAndreas Gohr $http->keep_alive = false; // we do single ops here, no need for keep-alive 245cf2dcf1bSAndreas Gohr $http->agent = 'DokuWiki HTTP Client (Extension Manager)'; 246cf2dcf1bSAndreas Gohr 247cf2dcf1bSAndreas Gohr $data = $http->get($url); 248cf2dcf1bSAndreas Gohr if ($data === false) throw new Exception('error_download', [$url, $http->error, $http->status]); 249cf2dcf1bSAndreas Gohr 250cf2dcf1bSAndreas Gohr // get filename from headers 251cf2dcf1bSAndreas Gohr if (preg_match( 252cf2dcf1bSAndreas Gohr '/attachment;\s*filename\s*=\s*"([^"]*)"/i', 253cf2dcf1bSAndreas Gohr (string)($http->resp_headers['content-disposition'] ?? ''), 254cf2dcf1bSAndreas Gohr $match 255cf2dcf1bSAndreas Gohr )) { 256cf2dcf1bSAndreas Gohr $file = PhpString::basename($match[1]); 257cf2dcf1bSAndreas Gohr } 258cf2dcf1bSAndreas Gohr 259cf2dcf1bSAndreas Gohr // clean up filename 260cf2dcf1bSAndreas Gohr $file = $this->fileToBase($file); 261cf2dcf1bSAndreas Gohr 262cf2dcf1bSAndreas Gohr // create tmp directory for download 263cf2dcf1bSAndreas Gohr $tmp = $this->mkTmpDir(); 264cf2dcf1bSAndreas Gohr 265cf2dcf1bSAndreas Gohr // save the file 266cf2dcf1bSAndreas Gohr if (@file_put_contents("$tmp/$file", $data) === false) { 267cf2dcf1bSAndreas Gohr throw new Exception('error_save'); 268cf2dcf1bSAndreas Gohr } 269cf2dcf1bSAndreas Gohr 270cf2dcf1bSAndreas Gohr return "$tmp/$file"; 271cf2dcf1bSAndreas Gohr } 272cf2dcf1bSAndreas Gohr 273cf2dcf1bSAndreas Gohr 274cf2dcf1bSAndreas Gohr /** 275cf2dcf1bSAndreas Gohr * Delete outdated files 276cf2dcf1bSAndreas Gohr */ 277cf2dcf1bSAndreas Gohr public function removeDeletedFiles(Extension $extension) 278cf2dcf1bSAndreas Gohr { 279cf2dcf1bSAndreas Gohr $extensiondir = $extension->getInstallDir(); 280cf2dcf1bSAndreas Gohr $definitionfile = $extensiondir . '/deleted.files'; 281cf2dcf1bSAndreas Gohr if (!file_exists($definitionfile)) return; 282cf2dcf1bSAndreas Gohr 283cf2dcf1bSAndreas Gohr $list = file($definitionfile); 284cf2dcf1bSAndreas Gohr foreach ($list as $line) { 285cf2dcf1bSAndreas Gohr $line = trim(preg_replace('/#.*$/', '', $line)); 286cf2dcf1bSAndreas Gohr $line = str_replace('..', '', $line); // do not run out of the extension directory 287cf2dcf1bSAndreas Gohr if (!$line) continue; 288cf2dcf1bSAndreas Gohr 289cf2dcf1bSAndreas Gohr $file = $extensiondir . '/' . $line; 290cf2dcf1bSAndreas Gohr if (!file_exists($file)) continue; 291cf2dcf1bSAndreas Gohr 292cf2dcf1bSAndreas Gohr io_rmdir($file, true); 293cf2dcf1bSAndreas Gohr } 294cf2dcf1bSAndreas Gohr } 295cf2dcf1bSAndreas Gohr 29625d28a01SAndreas Gohr /** 29725d28a01SAndreas Gohr * Purge all caches 29825d28a01SAndreas Gohr */ 299cf2dcf1bSAndreas Gohr public static function purgeCache() 300cf2dcf1bSAndreas Gohr { 301cf2dcf1bSAndreas Gohr // expire dokuwiki caches 302cf2dcf1bSAndreas Gohr // touching local.php expires wiki page, JS and CSS caches 303cf2dcf1bSAndreas Gohr global $config_cascade; 304cf2dcf1bSAndreas Gohr @touch(reset($config_cascade['main']['local'])); 305cf2dcf1bSAndreas Gohr 306cf2dcf1bSAndreas Gohr if (function_exists('opcache_reset')) { 307cf2dcf1bSAndreas Gohr opcache_reset(); 308cf2dcf1bSAndreas Gohr } 309cf2dcf1bSAndreas Gohr } 310cf2dcf1bSAndreas Gohr 311cf2dcf1bSAndreas Gohr /** 312160d3688SAndreas Gohr * Get the list of processed extensions and their status during an installation run 31325d28a01SAndreas Gohr * 31425d28a01SAndreas Gohr * @return array id => status 31525d28a01SAndreas Gohr */ 31625d28a01SAndreas Gohr public function getProcessed() 31725d28a01SAndreas Gohr { 31825d28a01SAndreas Gohr return $this->processed; 31925d28a01SAndreas Gohr } 32025d28a01SAndreas Gohr 321b2a05b76SAndreas Gohr 322b2a05b76SAndreas Gohr /** 323b2a05b76SAndreas Gohr * Ensure that the given extension is compatible with the current PHP version 324b2a05b76SAndreas Gohr * 325b2a05b76SAndreas Gohr * Throws an exception if the extension is not compatible 326b2a05b76SAndreas Gohr * 327b2a05b76SAndreas Gohr * @param Extension $extension 328b2a05b76SAndreas Gohr * @throws Exception 329b2a05b76SAndreas Gohr */ 3304fd6a1d7SAndreas Gohr public static function ensurePhpCompatibility(Extension $extension) 331b2a05b76SAndreas Gohr { 332b2a05b76SAndreas Gohr $min = $extension->getMinimumPHPVersion(); 333b2a05b76SAndreas Gohr if ($min && version_compare(PHP_VERSION, $min, '<')) { 334b2a05b76SAndreas Gohr throw new Exception('error_minphp', [$extension->getId(), $min, PHP_VERSION]); 335b2a05b76SAndreas Gohr } 336b2a05b76SAndreas Gohr 337b2a05b76SAndreas Gohr $max = $extension->getMaximumPHPVersion(); 338b2a05b76SAndreas Gohr if ($max && version_compare(PHP_VERSION, $max, '>')) { 339b2a05b76SAndreas Gohr throw new Exception('error_maxphp', [$extension->getId(), $max, PHP_VERSION]); 340b2a05b76SAndreas Gohr } 341b2a05b76SAndreas Gohr } 342b2a05b76SAndreas Gohr 3434fd6a1d7SAndreas Gohr /** 3444fd6a1d7SAndreas Gohr * Ensure the file permissions are correct before attempting to install 3454fd6a1d7SAndreas Gohr * 3464fd6a1d7SAndreas Gohr * @throws Exception if the permissions are not correct 3474fd6a1d7SAndreas Gohr */ 3484fd6a1d7SAndreas Gohr public static function ensurePermissions(Extension $extension) 3494fd6a1d7SAndreas Gohr { 3504fd6a1d7SAndreas Gohr $target = $extension->getInstallDir(); 3514fd6a1d7SAndreas Gohr 3524fd6a1d7SAndreas Gohr // updates 3534fd6a1d7SAndreas Gohr if (file_exists($target)) { 3544fd6a1d7SAndreas Gohr if (!is_writable($target)) throw new Exception('noperms'); 3554fd6a1d7SAndreas Gohr return; 3564fd6a1d7SAndreas Gohr } 3574fd6a1d7SAndreas Gohr 3584fd6a1d7SAndreas Gohr // new installs 3594fd6a1d7SAndreas Gohr $target = dirname($target); 3604fd6a1d7SAndreas Gohr if (!is_writable($target)) { 3614fd6a1d7SAndreas Gohr if ($extension->isTemplate()) throw new Exception('notplperms'); 3624fd6a1d7SAndreas Gohr throw new Exception('nopluginperms'); 3634fd6a1d7SAndreas Gohr } 3644fd6a1d7SAndreas Gohr } 365b2a05b76SAndreas Gohr 36625d28a01SAndreas Gohr /** 367cf2dcf1bSAndreas Gohr * Get a base name from an archive name (we don't trust) 368cf2dcf1bSAndreas Gohr * 369cf2dcf1bSAndreas Gohr * @param string $file 370cf2dcf1bSAndreas Gohr * @return string 371cf2dcf1bSAndreas Gohr */ 372cf2dcf1bSAndreas Gohr protected function fileToBase($file) 373cf2dcf1bSAndreas Gohr { 374cf2dcf1bSAndreas Gohr $base = PhpString::basename($file); 375cf2dcf1bSAndreas Gohr $base = preg_replace('/\.(tar\.gz|tar\.bz|tar\.bz2|tar|tgz|tbz|zip)$/', '', $base); 376cf2dcf1bSAndreas Gohr return preg_replace('/\W+/', '', $base); 377cf2dcf1bSAndreas Gohr } 378cf2dcf1bSAndreas Gohr 379cf2dcf1bSAndreas Gohr /** 380cf2dcf1bSAndreas Gohr * Returns a temporary directory 381cf2dcf1bSAndreas Gohr * 382cf2dcf1bSAndreas Gohr * The directory is registered for cleanup when the class is destroyed 383cf2dcf1bSAndreas Gohr * 384cf2dcf1bSAndreas Gohr * @return string 385cf2dcf1bSAndreas Gohr * @throws Exception 386cf2dcf1bSAndreas Gohr */ 387cf2dcf1bSAndreas Gohr protected function mkTmpDir() 388cf2dcf1bSAndreas Gohr { 389cf2dcf1bSAndreas Gohr try { 390cf2dcf1bSAndreas Gohr $dir = io_mktmpdir(); 391cf2dcf1bSAndreas Gohr } catch (\Exception $e) { 392cf2dcf1bSAndreas Gohr throw new Exception('error_dircreate', [], $e); 393cf2dcf1bSAndreas Gohr } 394cf2dcf1bSAndreas Gohr if (!$dir) throw new Exception('error_dircreate'); 395cf2dcf1bSAndreas Gohr $this->temporary[] = $dir; 396cf2dcf1bSAndreas Gohr return $dir; 397cf2dcf1bSAndreas Gohr } 398cf2dcf1bSAndreas Gohr 399cf2dcf1bSAndreas Gohr /** 400cf2dcf1bSAndreas Gohr * Find all extensions in a given directory 401cf2dcf1bSAndreas Gohr * 402cf2dcf1bSAndreas Gohr * This allows us to install extensions from archives that contain multiple extensions and 403cf2dcf1bSAndreas Gohr * also caters for the fact that archives may or may not contain subdirectories for the extension(s). 404cf2dcf1bSAndreas Gohr * 405cf2dcf1bSAndreas Gohr * @param string $dir 406cf2dcf1bSAndreas Gohr * @return Extension[] 407cf2dcf1bSAndreas Gohr */ 408cf2dcf1bSAndreas Gohr protected function findExtensions($dir, $base = null) 409cf2dcf1bSAndreas Gohr { 410cf2dcf1bSAndreas Gohr // first check for plugin.info.txt or template.info.txt 411cf2dcf1bSAndreas Gohr $extensions = []; 412cf2dcf1bSAndreas Gohr $iterator = new RecursiveDirectoryIterator($dir); 413cf2dcf1bSAndreas Gohr foreach (new RecursiveIteratorIterator($iterator) as $file) { 414cf2dcf1bSAndreas Gohr if ( 415cf2dcf1bSAndreas Gohr $file->getFilename() === 'plugin.info.txt' || 416cf2dcf1bSAndreas Gohr $file->getFilename() === 'template.info.txt' 417cf2dcf1bSAndreas Gohr ) { 41825d28a01SAndreas Gohr $extensions[] = Extension::createFromDirectory($file->getPath()); 419cf2dcf1bSAndreas Gohr } 420cf2dcf1bSAndreas Gohr } 421cf2dcf1bSAndreas Gohr if ($extensions) return $extensions; 422cf2dcf1bSAndreas Gohr 423cf2dcf1bSAndreas Gohr // still nothing? we assume this to be a single extension that is either 424cf2dcf1bSAndreas Gohr // directly in the given directory or in single subdirectory 425cf2dcf1bSAndreas Gohr $base = $base ?? PhpString::basename($dir); 426cf2dcf1bSAndreas Gohr $files = glob($dir . '/*'); 427cf2dcf1bSAndreas Gohr if (count($files) === 1 && is_dir($files[0])) { 428cf2dcf1bSAndreas Gohr $dir = $files[0]; 429cf2dcf1bSAndreas Gohr } 430cf2dcf1bSAndreas Gohr return [Extension::createFromDirectory($dir, null, $base)]; 431cf2dcf1bSAndreas Gohr } 432cf2dcf1bSAndreas Gohr 433cf2dcf1bSAndreas Gohr /** 434cf2dcf1bSAndreas Gohr * Extract the given archive to the given target directory 435cf2dcf1bSAndreas Gohr * 436cf2dcf1bSAndreas Gohr * Auto-guesses the archive type 437cf2dcf1bSAndreas Gohr * @throws Exception 438cf2dcf1bSAndreas Gohr */ 439cf2dcf1bSAndreas Gohr protected function extractArchive($archive, $target) 440cf2dcf1bSAndreas Gohr { 441cf2dcf1bSAndreas Gohr $fh = fopen($archive, 'rb'); 442cf2dcf1bSAndreas Gohr if (!$fh) throw new Exception('error_archive_read', [$archive]); 443cf2dcf1bSAndreas Gohr $magic = fread($fh, 5); 444cf2dcf1bSAndreas Gohr fclose($fh); 445cf2dcf1bSAndreas Gohr 446cf2dcf1bSAndreas Gohr if (strpos($magic, "\x50\x4b\x03\x04") === 0) { 447cf2dcf1bSAndreas Gohr $archiver = new Zip(); 448cf2dcf1bSAndreas Gohr } else { 449cf2dcf1bSAndreas Gohr $archiver = new Tar(); 450cf2dcf1bSAndreas Gohr } 451cf2dcf1bSAndreas Gohr try { 452cf2dcf1bSAndreas Gohr $archiver->open($archive); 453cf2dcf1bSAndreas Gohr $archiver->extract($target); 454cf2dcf1bSAndreas Gohr } catch (ArchiveIOException|ArchiveCorruptedException|ArchiveIllegalCompressionException $e) { 455cf2dcf1bSAndreas Gohr throw new Exception('error_archive_extract', [$archive, $e->getMessage()], $e); 456cf2dcf1bSAndreas Gohr } 457cf2dcf1bSAndreas Gohr } 458cf2dcf1bSAndreas Gohr 459cf2dcf1bSAndreas Gohr /** 460cf2dcf1bSAndreas Gohr * Copy with recursive sub-directory support 461cf2dcf1bSAndreas Gohr * 462cf2dcf1bSAndreas Gohr * @param string $src filename path to file 463cf2dcf1bSAndreas Gohr * @param string $dst filename path to file 464cf2dcf1bSAndreas Gohr * @throws Exception 465cf2dcf1bSAndreas Gohr */ 466cf2dcf1bSAndreas Gohr protected function dircopy($src, $dst) 467cf2dcf1bSAndreas Gohr { 468cf2dcf1bSAndreas Gohr global $conf; 469cf2dcf1bSAndreas Gohr 470cf2dcf1bSAndreas Gohr if (is_dir($src)) { 471cf2dcf1bSAndreas Gohr if (!$dh = @opendir($src)) { 472cf2dcf1bSAndreas Gohr throw new Exception('error_copy_read', [$src]); 473cf2dcf1bSAndreas Gohr } 474cf2dcf1bSAndreas Gohr 475cf2dcf1bSAndreas Gohr if (io_mkdir_p($dst)) { 476cf2dcf1bSAndreas Gohr while (false !== ($f = readdir($dh))) { 477cf2dcf1bSAndreas Gohr if ($f == '..' || $f == '.') continue; 478cf2dcf1bSAndreas Gohr $this->dircopy("$src/$f", "$dst/$f"); 479cf2dcf1bSAndreas Gohr } 480cf2dcf1bSAndreas Gohr } else { 481cf2dcf1bSAndreas Gohr throw new Exception('error_copy_mkdir', [$dst]); 482cf2dcf1bSAndreas Gohr } 483cf2dcf1bSAndreas Gohr 484cf2dcf1bSAndreas Gohr closedir($dh); 485cf2dcf1bSAndreas Gohr } else { 486cf2dcf1bSAndreas Gohr $existed = file_exists($dst); 487cf2dcf1bSAndreas Gohr 488cf2dcf1bSAndreas Gohr if (!@copy($src, $dst)) { 489cf2dcf1bSAndreas Gohr throw new Exception('error_copy_copy', [$src, $dst]); 490cf2dcf1bSAndreas Gohr } 491cf2dcf1bSAndreas Gohr if (!$existed && $conf['fperm']) chmod($dst, $conf['fperm']); 492cf2dcf1bSAndreas Gohr @touch($dst, filemtime($src)); 493cf2dcf1bSAndreas Gohr } 494cf2dcf1bSAndreas Gohr } 495cf2dcf1bSAndreas Gohr 496cf2dcf1bSAndreas Gohr /** 49725d28a01SAndreas Gohr * Reset caches if needed 498cf2dcf1bSAndreas Gohr */ 499cf2dcf1bSAndreas Gohr protected function cleanUp() 500cf2dcf1bSAndreas Gohr { 501cf2dcf1bSAndreas Gohr if ($this->isDirty) { 502cf2dcf1bSAndreas Gohr self::purgeCache(); 503cf2dcf1bSAndreas Gohr $this->isDirty = false; 504cf2dcf1bSAndreas Gohr } 505cf2dcf1bSAndreas Gohr } 506cf2dcf1bSAndreas Gohr} 507