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 34*25d28a01SAndreas Gohr protected $processed = []; 35*25d28a01SAndreas Gohr 36*25d28a01SAndreas Gohr public const STATUS_SKIPPED = 'skipped'; 37*25d28a01SAndreas Gohr public const STATUS_UPDATED = 'updated'; 38*25d28a01SAndreas Gohr public const STATUS_INSTALLED = 'installed'; 39*25d28a01SAndreas Gohr public const STATUS_DELETED = 'deleted'; 40*25d28a01SAndreas Gohr 41*25d28a01SAndreas 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 { 59*25d28a01SAndreas Gohr foreach ($this->temporary as $dir) { 60*25d28a01SAndreas Gohr io_rmdir($dir, true); 61*25d28a01SAndreas Gohr } 62cf2dcf1bSAndreas Gohr $this->cleanUp(); 63cf2dcf1bSAndreas Gohr } 64cf2dcf1bSAndreas Gohr 65cf2dcf1bSAndreas Gohr /** 66*25d28a01SAndreas Gohr * Install an extension 67*25d28a01SAndreas Gohr * 68*25d28a01SAndreas Gohr * This will simply call installFromUrl() with the URL from the extension 69*25d28a01SAndreas Gohr * 70*25d28a01SAndreas Gohr * The $skipInstalled parameter should only be used when installing dependencies 71*25d28a01SAndreas Gohr * 72*25d28a01SAndreas Gohr * @param string $id the extension ID 73*25d28a01SAndreas Gohr * @param bool $skipInstalled Ignore the overwrite setting and skip installed extensions 74*25d28a01SAndreas Gohr * @throws Exception 75*25d28a01SAndreas Gohr */ 76*25d28a01SAndreas Gohr public function installFromId($id, $skipInstalled = false) { 77*25d28a01SAndreas Gohr $extension = Extension::createFromId($id); 78*25d28a01SAndreas Gohr if($skipInstalled && $extension->isInstalled()) return; 79*25d28a01SAndreas Gohr $url = $extension->getDownloadURL(); 80*25d28a01SAndreas Gohr if(!$url) { 81*25d28a01SAndreas Gohr throw new Exception('error_nourl', [$extension->getId()]); 82*25d28a01SAndreas Gohr } 83*25d28a01SAndreas Gohr $this->installFromUrl($url); 84*25d28a01SAndreas Gohr } 85*25d28a01SAndreas Gohr 86*25d28a01SAndreas Gohr /** 87cf2dcf1bSAndreas Gohr * Install extensions from a given URL 88cf2dcf1bSAndreas Gohr * 89cf2dcf1bSAndreas Gohr * @param string $url the URL to the archive 90cf2dcf1bSAndreas Gohr * @param null $base the base directory name to use 91cf2dcf1bSAndreas Gohr * @throws Exception 92cf2dcf1bSAndreas Gohr */ 93cf2dcf1bSAndreas Gohr public function installFromUrl($url, $base = null) 94cf2dcf1bSAndreas Gohr { 95cf2dcf1bSAndreas Gohr $this->sourceUrl = $url; 96cf2dcf1bSAndreas Gohr $archive = $this->downloadArchive($url); 97cf2dcf1bSAndreas Gohr $this->installFromArchive( 98cf2dcf1bSAndreas Gohr $archive, 99cf2dcf1bSAndreas Gohr $base 100cf2dcf1bSAndreas Gohr ); 101cf2dcf1bSAndreas Gohr } 102cf2dcf1bSAndreas Gohr 103cf2dcf1bSAndreas Gohr /** 104cf2dcf1bSAndreas Gohr * Install extensions from a user upload 105cf2dcf1bSAndreas Gohr * 106cf2dcf1bSAndreas Gohr * @param string $field name of the upload file 107cf2dcf1bSAndreas Gohr * @throws Exception 108cf2dcf1bSAndreas Gohr */ 109cf2dcf1bSAndreas Gohr public function installFromUpload($field) 110cf2dcf1bSAndreas Gohr { 111cf2dcf1bSAndreas Gohr $this->sourceUrl = ''; 112cf2dcf1bSAndreas Gohr if ($_FILES[$field]['error']) { 113cf2dcf1bSAndreas Gohr throw new Exception('msg_upload_failed', [$_FILES[$field]['error']]); 114cf2dcf1bSAndreas Gohr } 115cf2dcf1bSAndreas Gohr 116cf2dcf1bSAndreas Gohr $tmp = $this->mkTmpDir(); 117cf2dcf1bSAndreas Gohr if (!move_uploaded_file($_FILES[$field]['tmp_name'], "$tmp/upload.archive")) { 118cf2dcf1bSAndreas Gohr throw new Exception('msg_upload_failed', ['move failed']); 119cf2dcf1bSAndreas Gohr } 120cf2dcf1bSAndreas Gohr $this->installFromArchive( 121cf2dcf1bSAndreas Gohr "$tmp/upload.archive", 122cf2dcf1bSAndreas Gohr $this->fileToBase($_FILES[$field]['name']), 123cf2dcf1bSAndreas Gohr ); 124cf2dcf1bSAndreas Gohr } 125cf2dcf1bSAndreas Gohr 126cf2dcf1bSAndreas Gohr /** 127cf2dcf1bSAndreas Gohr * Install extensions from an archive 128cf2dcf1bSAndreas Gohr * 129cf2dcf1bSAndreas Gohr * The archive is extracted to a temporary directory and then the contained extensions are installed. 130cf2dcf1bSAndreas Gohr * This is is the ultimate installation procedure and all other install methods will end up here. 131cf2dcf1bSAndreas Gohr * 132cf2dcf1bSAndreas Gohr * @param string $archive the path to the archive 133cf2dcf1bSAndreas Gohr * @param string $base the base directory name to use 134cf2dcf1bSAndreas Gohr * @throws Exception 135cf2dcf1bSAndreas Gohr */ 136cf2dcf1bSAndreas Gohr public function installFromArchive($archive, $base = null) 137cf2dcf1bSAndreas Gohr { 138cf2dcf1bSAndreas Gohr if ($base === null) $base = $this->fileToBase($archive); 139cf2dcf1bSAndreas Gohr $target = $this->mkTmpDir() . '/' . $base; 140cf2dcf1bSAndreas Gohr $this->extractArchive($archive, $target); 141cf2dcf1bSAndreas Gohr $extensions = $this->findExtensions($target, $base); 142cf2dcf1bSAndreas Gohr foreach ($extensions as $extension) { 143*25d28a01SAndreas Gohr // check installation status 144*25d28a01SAndreas Gohr if ($extension->isInstalled()) { 145*25d28a01SAndreas Gohr if(!$this->overwrite) { 146*25d28a01SAndreas Gohr $this->processed[$extension->getId()] = self::STATUS_SKIPPED; 147cf2dcf1bSAndreas Gohr continue; 148cf2dcf1bSAndreas Gohr } 149*25d28a01SAndreas Gohr $status = self::STATUS_UPDATED; 150*25d28a01SAndreas Gohr } else { 151*25d28a01SAndreas Gohr $status = self::STATUS_INSTALLED; 152*25d28a01SAndreas Gohr } 153cf2dcf1bSAndreas Gohr 154*25d28a01SAndreas Gohr // FIXME check PHP requirements 155*25d28a01SAndreas Gohr 156*25d28a01SAndreas Gohr // install dependencies first 157*25d28a01SAndreas Gohr foreach ($extension->getDependencyList() as $id) { 158*25d28a01SAndreas Gohr if (isset($this->processed[$id])) continue; 159*25d28a01SAndreas Gohr if ($id == $extension->getId()) continue; // avoid circular dependencies 160*25d28a01SAndreas Gohr $this->installFromId($id, true); 161*25d28a01SAndreas Gohr } 162*25d28a01SAndreas Gohr 163*25d28a01SAndreas Gohr // now install the extension 164cf2dcf1bSAndreas Gohr $this->dircopy( 165cf2dcf1bSAndreas Gohr $extension->getCurrentDir(), 166cf2dcf1bSAndreas Gohr $extension->getInstallDir() 167cf2dcf1bSAndreas Gohr ); 168cf2dcf1bSAndreas Gohr $this->isDirty = true; 1697c9966a5SAndreas Gohr $extension->getManager()->storeUpdate($this->sourceUrl); 170cf2dcf1bSAndreas Gohr $this->removeDeletedFiles($extension); 171*25d28a01SAndreas Gohr $this->processed[$extension->getId()] = $status; 172cf2dcf1bSAndreas Gohr } 173cf2dcf1bSAndreas Gohr 174cf2dcf1bSAndreas Gohr $this->cleanUp(); 175cf2dcf1bSAndreas Gohr } 176cf2dcf1bSAndreas Gohr 177cf2dcf1bSAndreas Gohr /** 178cf2dcf1bSAndreas Gohr * Uninstall an extension 179cf2dcf1bSAndreas Gohr * 180cf2dcf1bSAndreas Gohr * @param Extension $extension 181cf2dcf1bSAndreas Gohr * @throws Exception 182cf2dcf1bSAndreas Gohr */ 183cf2dcf1bSAndreas Gohr public function uninstall(Extension $extension) 184cf2dcf1bSAndreas Gohr { 185cf2dcf1bSAndreas Gohr // FIXME check if dependencies are still needed 186cf2dcf1bSAndreas Gohr 187cf2dcf1bSAndreas Gohr if($extension->isProtected()) { 188cf2dcf1bSAndreas Gohr throw new Exception('error_uninstall_protected', [$extension->getId()]); 189cf2dcf1bSAndreas Gohr } 190cf2dcf1bSAndreas Gohr 191cf2dcf1bSAndreas Gohr if (!io_rmdir($extension->getInstallDir(), true)) { 192cf2dcf1bSAndreas Gohr throw new Exception('msg_delete_failed', [$extension->getId()]); 193cf2dcf1bSAndreas Gohr } 194cf2dcf1bSAndreas Gohr self::purgeCache(); 195cf2dcf1bSAndreas Gohr } 196cf2dcf1bSAndreas Gohr 197cf2dcf1bSAndreas Gohr /** 198cf2dcf1bSAndreas Gohr * Download an archive to a protected path 199cf2dcf1bSAndreas Gohr * 200cf2dcf1bSAndreas Gohr * @param string $url The url to get the archive from 201cf2dcf1bSAndreas Gohr * @return string The path where the archive was saved 202cf2dcf1bSAndreas Gohr * @throws Exception 203cf2dcf1bSAndreas Gohr */ 204cf2dcf1bSAndreas Gohr public function downloadArchive($url) 205cf2dcf1bSAndreas Gohr { 206cf2dcf1bSAndreas Gohr // check the url 207cf2dcf1bSAndreas Gohr if (!preg_match('/https?:\/\//i', $url)) { 208cf2dcf1bSAndreas Gohr throw new Exception('error_badurl'); 209cf2dcf1bSAndreas Gohr } 210cf2dcf1bSAndreas Gohr 211cf2dcf1bSAndreas Gohr // try to get the file from the path (used as plugin name fallback) 212cf2dcf1bSAndreas Gohr $file = parse_url($url, PHP_URL_PATH); 213cf2dcf1bSAndreas Gohr $file = $file ? PhpString::basename($file) : md5($url); 214cf2dcf1bSAndreas Gohr 215cf2dcf1bSAndreas Gohr // download 216cf2dcf1bSAndreas Gohr $http = new DokuHTTPClient(); 217cf2dcf1bSAndreas Gohr $http->max_bodysize = 0; 218cf2dcf1bSAndreas Gohr $http->timeout = 25; //max. 25 sec 219cf2dcf1bSAndreas Gohr $http->keep_alive = false; // we do single ops here, no need for keep-alive 220cf2dcf1bSAndreas Gohr $http->agent = 'DokuWiki HTTP Client (Extension Manager)'; 221cf2dcf1bSAndreas Gohr 222cf2dcf1bSAndreas Gohr $data = $http->get($url); 223cf2dcf1bSAndreas Gohr if ($data === false) throw new Exception('error_download', [$url, $http->error, $http->status]); 224cf2dcf1bSAndreas Gohr 225cf2dcf1bSAndreas Gohr // get filename from headers 226cf2dcf1bSAndreas Gohr if (preg_match( 227cf2dcf1bSAndreas Gohr '/attachment;\s*filename\s*=\s*"([^"]*)"/i', 228cf2dcf1bSAndreas Gohr (string)($http->resp_headers['content-disposition'] ?? ''), 229cf2dcf1bSAndreas Gohr $match 230cf2dcf1bSAndreas Gohr )) { 231cf2dcf1bSAndreas Gohr $file = PhpString::basename($match[1]); 232cf2dcf1bSAndreas Gohr } 233cf2dcf1bSAndreas Gohr 234cf2dcf1bSAndreas Gohr // clean up filename 235cf2dcf1bSAndreas Gohr $file = $this->fileToBase($file); 236cf2dcf1bSAndreas Gohr 237cf2dcf1bSAndreas Gohr // create tmp directory for download 238cf2dcf1bSAndreas Gohr $tmp = $this->mkTmpDir(); 239cf2dcf1bSAndreas Gohr 240cf2dcf1bSAndreas Gohr // save the file 241cf2dcf1bSAndreas Gohr if (@file_put_contents("$tmp/$file", $data) === false) { 242cf2dcf1bSAndreas Gohr throw new Exception('error_save'); 243cf2dcf1bSAndreas Gohr } 244cf2dcf1bSAndreas Gohr 245cf2dcf1bSAndreas Gohr return "$tmp/$file"; 246cf2dcf1bSAndreas Gohr } 247cf2dcf1bSAndreas Gohr 248cf2dcf1bSAndreas Gohr 249cf2dcf1bSAndreas Gohr /** 250cf2dcf1bSAndreas Gohr * Delete outdated files 251cf2dcf1bSAndreas Gohr */ 252cf2dcf1bSAndreas Gohr public function removeDeletedFiles(Extension $extension) 253cf2dcf1bSAndreas Gohr { 254cf2dcf1bSAndreas Gohr $extensiondir = $extension->getInstallDir(); 255cf2dcf1bSAndreas Gohr $definitionfile = $extensiondir . '/deleted.files'; 256cf2dcf1bSAndreas Gohr if (!file_exists($definitionfile)) return; 257cf2dcf1bSAndreas Gohr 258cf2dcf1bSAndreas Gohr $list = file($definitionfile); 259cf2dcf1bSAndreas Gohr foreach ($list as $line) { 260cf2dcf1bSAndreas Gohr $line = trim(preg_replace('/#.*$/', '', $line)); 261cf2dcf1bSAndreas Gohr $line = str_replace('..', '', $line); // do not run out of the extension directory 262cf2dcf1bSAndreas Gohr if (!$line) continue; 263cf2dcf1bSAndreas Gohr 264cf2dcf1bSAndreas Gohr $file = $extensiondir . '/' . $line; 265cf2dcf1bSAndreas Gohr if (!file_exists($file)) continue; 266cf2dcf1bSAndreas Gohr 267cf2dcf1bSAndreas Gohr io_rmdir($file, true); 268cf2dcf1bSAndreas Gohr } 269cf2dcf1bSAndreas Gohr } 270cf2dcf1bSAndreas Gohr 271*25d28a01SAndreas Gohr /** 272*25d28a01SAndreas Gohr * Purge all caches 273*25d28a01SAndreas Gohr */ 274cf2dcf1bSAndreas Gohr public static function purgeCache() 275cf2dcf1bSAndreas Gohr { 276cf2dcf1bSAndreas Gohr // expire dokuwiki caches 277cf2dcf1bSAndreas Gohr // touching local.php expires wiki page, JS and CSS caches 278cf2dcf1bSAndreas Gohr global $config_cascade; 279cf2dcf1bSAndreas Gohr @touch(reset($config_cascade['main']['local'])); 280cf2dcf1bSAndreas Gohr 281cf2dcf1bSAndreas Gohr if (function_exists('opcache_reset')) { 282cf2dcf1bSAndreas Gohr opcache_reset(); 283cf2dcf1bSAndreas Gohr } 284cf2dcf1bSAndreas Gohr } 285cf2dcf1bSAndreas Gohr 286cf2dcf1bSAndreas Gohr /** 287*25d28a01SAndreas Gohr * Get the list of processed extensions and the processing status 288*25d28a01SAndreas Gohr * 289*25d28a01SAndreas Gohr * @return array id => status 290*25d28a01SAndreas Gohr */ 291*25d28a01SAndreas Gohr public function getProcessed() 292*25d28a01SAndreas Gohr { 293*25d28a01SAndreas Gohr return $this->processed; 294*25d28a01SAndreas Gohr } 295*25d28a01SAndreas Gohr 296*25d28a01SAndreas Gohr /** 297cf2dcf1bSAndreas Gohr * Get a base name from an archive name (we don't trust) 298cf2dcf1bSAndreas Gohr * 299cf2dcf1bSAndreas Gohr * @param string $file 300cf2dcf1bSAndreas Gohr * @return string 301cf2dcf1bSAndreas Gohr */ 302cf2dcf1bSAndreas Gohr protected function fileToBase($file) 303cf2dcf1bSAndreas Gohr { 304cf2dcf1bSAndreas Gohr $base = PhpString::basename($file); 305cf2dcf1bSAndreas Gohr $base = preg_replace('/\.(tar\.gz|tar\.bz|tar\.bz2|tar|tgz|tbz|zip)$/', '', $base); 306cf2dcf1bSAndreas Gohr return preg_replace('/\W+/', '', $base); 307cf2dcf1bSAndreas Gohr } 308cf2dcf1bSAndreas Gohr 309cf2dcf1bSAndreas Gohr /** 310cf2dcf1bSAndreas Gohr * Returns a temporary directory 311cf2dcf1bSAndreas Gohr * 312cf2dcf1bSAndreas Gohr * The directory is registered for cleanup when the class is destroyed 313cf2dcf1bSAndreas Gohr * 314cf2dcf1bSAndreas Gohr * @return string 315cf2dcf1bSAndreas Gohr * @throws Exception 316cf2dcf1bSAndreas Gohr */ 317cf2dcf1bSAndreas Gohr protected function mkTmpDir() 318cf2dcf1bSAndreas Gohr { 319cf2dcf1bSAndreas Gohr try { 320cf2dcf1bSAndreas Gohr $dir = io_mktmpdir(); 321cf2dcf1bSAndreas Gohr } catch (\Exception $e) { 322cf2dcf1bSAndreas Gohr throw new Exception('error_dircreate', [], $e); 323cf2dcf1bSAndreas Gohr } 324cf2dcf1bSAndreas Gohr if (!$dir) throw new Exception('error_dircreate'); 325cf2dcf1bSAndreas Gohr $this->temporary[] = $dir; 326cf2dcf1bSAndreas Gohr return $dir; 327cf2dcf1bSAndreas Gohr } 328cf2dcf1bSAndreas Gohr 329cf2dcf1bSAndreas Gohr /** 330cf2dcf1bSAndreas Gohr * Find all extensions in a given directory 331cf2dcf1bSAndreas Gohr * 332cf2dcf1bSAndreas Gohr * This allows us to install extensions from archives that contain multiple extensions and 333cf2dcf1bSAndreas Gohr * also caters for the fact that archives may or may not contain subdirectories for the extension(s). 334cf2dcf1bSAndreas Gohr * 335cf2dcf1bSAndreas Gohr * @param string $dir 336cf2dcf1bSAndreas Gohr * @return Extension[] 337cf2dcf1bSAndreas Gohr */ 338cf2dcf1bSAndreas Gohr protected function findExtensions($dir, $base = null) 339cf2dcf1bSAndreas Gohr { 340cf2dcf1bSAndreas Gohr // first check for plugin.info.txt or template.info.txt 341cf2dcf1bSAndreas Gohr $extensions = []; 342cf2dcf1bSAndreas Gohr $iterator = new RecursiveDirectoryIterator($dir); 343cf2dcf1bSAndreas Gohr foreach (new RecursiveIteratorIterator($iterator) as $file) { 344cf2dcf1bSAndreas Gohr if ( 345cf2dcf1bSAndreas Gohr $file->getFilename() === 'plugin.info.txt' || 346cf2dcf1bSAndreas Gohr $file->getFilename() === 'template.info.txt' 347cf2dcf1bSAndreas Gohr ) { 348*25d28a01SAndreas Gohr $extensions[] = Extension::createFromDirectory($file->getPath()); 349cf2dcf1bSAndreas Gohr } 350cf2dcf1bSAndreas Gohr } 351cf2dcf1bSAndreas Gohr if ($extensions) return $extensions; 352cf2dcf1bSAndreas Gohr 353cf2dcf1bSAndreas Gohr // still nothing? we assume this to be a single extension that is either 354cf2dcf1bSAndreas Gohr // directly in the given directory or in single subdirectory 355cf2dcf1bSAndreas Gohr $base = $base ?? PhpString::basename($dir); 356cf2dcf1bSAndreas Gohr $files = glob($dir . '/*'); 357cf2dcf1bSAndreas Gohr if (count($files) === 1 && is_dir($files[0])) { 358cf2dcf1bSAndreas Gohr $dir = $files[0]; 359cf2dcf1bSAndreas Gohr } 360cf2dcf1bSAndreas Gohr return [Extension::createFromDirectory($dir, null, $base)]; 361cf2dcf1bSAndreas Gohr } 362cf2dcf1bSAndreas Gohr 363cf2dcf1bSAndreas Gohr /** 364cf2dcf1bSAndreas Gohr * Extract the given archive to the given target directory 365cf2dcf1bSAndreas Gohr * 366cf2dcf1bSAndreas Gohr * Auto-guesses the archive type 367cf2dcf1bSAndreas Gohr * @throws Exception 368cf2dcf1bSAndreas Gohr */ 369cf2dcf1bSAndreas Gohr protected function extractArchive($archive, $target) 370cf2dcf1bSAndreas Gohr { 371cf2dcf1bSAndreas Gohr $fh = fopen($archive, 'rb'); 372cf2dcf1bSAndreas Gohr if (!$fh) throw new Exception('error_archive_read', [$archive]); 373cf2dcf1bSAndreas Gohr $magic = fread($fh, 5); 374cf2dcf1bSAndreas Gohr fclose($fh); 375cf2dcf1bSAndreas Gohr 376cf2dcf1bSAndreas Gohr if (strpos($magic, "\x50\x4b\x03\x04") === 0) { 377cf2dcf1bSAndreas Gohr $archiver = new Zip(); 378cf2dcf1bSAndreas Gohr } else { 379cf2dcf1bSAndreas Gohr $archiver = new Tar(); 380cf2dcf1bSAndreas Gohr } 381cf2dcf1bSAndreas Gohr try { 382cf2dcf1bSAndreas Gohr $archiver->open($archive); 383cf2dcf1bSAndreas Gohr $archiver->extract($target); 384cf2dcf1bSAndreas Gohr } catch (ArchiveIOException|ArchiveCorruptedException|ArchiveIllegalCompressionException $e) { 385cf2dcf1bSAndreas Gohr throw new Exception('error_archive_extract', [$archive, $e->getMessage()], $e); 386cf2dcf1bSAndreas Gohr } 387cf2dcf1bSAndreas Gohr } 388cf2dcf1bSAndreas Gohr 389cf2dcf1bSAndreas Gohr /** 390cf2dcf1bSAndreas Gohr * Copy with recursive sub-directory support 391cf2dcf1bSAndreas Gohr * 392cf2dcf1bSAndreas Gohr * @param string $src filename path to file 393cf2dcf1bSAndreas Gohr * @param string $dst filename path to file 394cf2dcf1bSAndreas Gohr * @throws Exception 395cf2dcf1bSAndreas Gohr */ 396cf2dcf1bSAndreas Gohr protected function dircopy($src, $dst) 397cf2dcf1bSAndreas Gohr { 398cf2dcf1bSAndreas Gohr global $conf; 399cf2dcf1bSAndreas Gohr 400cf2dcf1bSAndreas Gohr if (is_dir($src)) { 401cf2dcf1bSAndreas Gohr if (!$dh = @opendir($src)) { 402cf2dcf1bSAndreas Gohr throw new Exception('error_copy_read', [$src]); 403cf2dcf1bSAndreas Gohr } 404cf2dcf1bSAndreas Gohr 405cf2dcf1bSAndreas Gohr if (io_mkdir_p($dst)) { 406cf2dcf1bSAndreas Gohr while (false !== ($f = readdir($dh))) { 407cf2dcf1bSAndreas Gohr if ($f == '..' || $f == '.') continue; 408cf2dcf1bSAndreas Gohr $this->dircopy("$src/$f", "$dst/$f"); 409cf2dcf1bSAndreas Gohr } 410cf2dcf1bSAndreas Gohr } else { 411cf2dcf1bSAndreas Gohr throw new Exception('error_copy_mkdir', [$dst]); 412cf2dcf1bSAndreas Gohr } 413cf2dcf1bSAndreas Gohr 414cf2dcf1bSAndreas Gohr closedir($dh); 415cf2dcf1bSAndreas Gohr } else { 416cf2dcf1bSAndreas Gohr $existed = file_exists($dst); 417cf2dcf1bSAndreas Gohr 418cf2dcf1bSAndreas Gohr if (!@copy($src, $dst)) { 419cf2dcf1bSAndreas Gohr throw new Exception('error_copy_copy', [$src, $dst]); 420cf2dcf1bSAndreas Gohr } 421cf2dcf1bSAndreas Gohr if (!$existed && $conf['fperm']) chmod($dst, $conf['fperm']); 422cf2dcf1bSAndreas Gohr @touch($dst, filemtime($src)); 423cf2dcf1bSAndreas Gohr } 424cf2dcf1bSAndreas Gohr } 425cf2dcf1bSAndreas Gohr 426cf2dcf1bSAndreas Gohr /** 427*25d28a01SAndreas Gohr * Reset caches if needed 428cf2dcf1bSAndreas Gohr */ 429cf2dcf1bSAndreas Gohr protected function cleanUp() 430cf2dcf1bSAndreas Gohr { 431cf2dcf1bSAndreas Gohr if ($this->isDirty) { 432cf2dcf1bSAndreas Gohr self::purgeCache(); 433cf2dcf1bSAndreas Gohr $this->isDirty = false; 434cf2dcf1bSAndreas Gohr } 435cf2dcf1bSAndreas Gohr } 436cf2dcf1bSAndreas Gohr} 437