UpdateModel::getInfoZip PHP Method

getInfoZip() private static method

Open a zip archive and inspect its contents for the requested paths.
private static getInfoZip ( string $Path, array $InfoPaths, boolean $TmpPath = false, boolean $ThrowError = true ) : array
$Path string
$InfoPaths array
$TmpPath boolean
$ThrowError boolean
return array
    private static function getInfoZip($Path, $InfoPaths, $TmpPath = false, $ThrowError = true)
    {
        // Extract the zip file so we can make sure it has appropriate information.
        $Zip = null;
        $ZipOpened = false;
        if (class_exists('ZipArchive', false)) {
            $Zip = new ZipArchive();
            $ZipOpened = $Zip->open($Path);
            if ($ZipOpened !== true) {
                $Zip = null;
            }
        }
        if (!$Zip) {
            require_once PATH_LIBRARY . "/vendors/pclzip/class.pclzipadapter.php";
            $Zip = new PclZipAdapter();
            $ZipOpened = $Zip->open($Path);
        }
        if ($ZipOpened !== true) {
            if ($ThrowError) {
                $Errors = array(ZipArchive::ER_EXISTS => 'ER_EXISTS', ZipArchive::ER_INCONS => 'ER_INCONS', ZipArchive::ER_INVAL => 'ER_INVAL', ZipArchive::ER_MEMORY => 'ER_MEMORY', ZipArchive::ER_NOENT => 'ER_NOENT', ZipArchive::ER_NOZIP => 'ER_NOZIP', ZipArchive::ER_OPEN => 'ER_OPEN', ZipArchive::ER_READ => 'ER_READ', ZipArchive::ER_SEEK => 'ER_SEEK');
                $Error = val($ZipOpened, $Errors, 'Unknown Error');
                throw new Exception(t('Could not open addon file. Addons must be zip files.') . " ({$Path} {$Error})", 400);
            }
            return [];
        }
        if ($TmpPath === false) {
            $TmpPath = dirname($Path) . '/' . basename($Path, '.zip') . '/';
        }
        if (file_exists($TmpPath)) {
            Gdn_FileSystem::removeFolder($TmpPath);
        }
        $Result = [];
        for ($i = 0; $i < $Zip->numFiles; $i++) {
            $Entry = $Zip->statIndex($i);
            if (preg_match('#(\\.\\.[\\/])#', $Entry['name'])) {
                throw new Gdn_UserException("Invalid path in zip file: " . htmlspecialchars($Entry['name']));
            }
            $Name = '/' . ltrim($Entry['name'], '/');
            foreach ($InfoPaths as $InfoPath) {
                $Preg = '`(' . str_replace(array('.', '*'), array('\\.', '.*'), $InfoPath) . ')$`';
                if (preg_match($Preg, $Name, $Matches)) {
                    $Base = trim(substr($Name, 0, -strlen($Matches[1])), '/');
                    if (strpos($Base, '/') !== false) {
                        continue;
                        // file nested too deep.
                    }
                    if (!file_exists($TmpPath)) {
                        mkdir($TmpPath, 0777, true);
                    }
                    $Zip->extractTo($TmpPath, $Entry['name']);
                    $Result[] = array('Name' => $Matches[1], 'Path' => $TmpPath . rtrim($Entry['name'], '/'), 'Base' => $Base);
                }
            }
        }
        return $Result;
    }