FileProvider.php 11.5 KB
Newer Older
1
<?php
2 3
namespace T3o\TerFe2\Provider;

4 5
/*
 * This file is part of the TYPO3 CMS project.
flyguide's avatar
flyguide committed
6
 *
7 8 9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
flyguide's avatar
flyguide committed
10
 *
11 12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
flyguide's avatar
flyguide committed
13
 *
14 15 16
 * The TYPO3 project - inspiring people to share!
 */

17
use T3o\TerFe2\Utility\CategoryUtility;
flyguide's avatar
flyguide committed
18 19 20 21

/**
 * Extension provider using local files
 */
22
class FileProvider extends \T3o\TerFe2\Provider\AbstractProvider
23 24 25
{

    /**
26
     * @var \T3o\TerFe2\Domain\Repository\ExtensionManagerCacheEntryRepository
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
     */
    protected $extensionManagerRepository;

    /**
     * @var string
     */
    protected $extensionRootPath = 'fileadmin/ter/';


    /**
     * Initialize provider
     *
     * @return void
     */
    public function initializeProvider()
    {
        // Set extension root path
        if (!empty($this->configuration['extensionRootPath'])) {
            $this->extensionRootPath = $this->configuration['extensionRootPath'];
        }
47
        $this->extensionRootPath = \T3o\TerFe2\Utility\FileUtility::getAbsoluteDirectory($this->extensionRootPath);
48 49

        // Get repository for extension manager cache entries
50
        $this->extensionManagerRepository = $this->objectManager->get(\T3o\TerFe2\Domain\Repository\ExtensionManagerCacheEntryRepository::class);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    }


    /**
     * Returns all extensions since last run
     *
     * @param integer $lastRun Timestamp of last update
     * @param integer $offset Offset to start with
     * @param integer $count Extension count to load
     * @return array Extension rows
     */
    public function getExtensions($lastRun, $offset, $count)
    {
        // Get extension list
        $extensions = $this->extensionManagerRepository->findLastUpdated($lastRun, $offset, $count);
        if (empty($extensions)) {
            return array();
        }

        // Load missing information from ext_emconf.php
        foreach ($extensions as $extensionKey => $extension) {
            $info = $this->getExtensionInfo($extension['extkey'], $extension['version'], $extension['t3xfilemd5']);
            if (empty($info) || !is_array($info)) {
                unset($extensions[$extensionKey]);
                continue;
            }
            foreach ($info as $key => $value) {
                if (empty($extension[$key])) {
                    $extensions[$extensionKey][$key] = $value;
                }
            }
        }

        return $this->buildExtensionStructure($extensions);
    }


    /**
     * Returns the url to an extension related file
     *
91
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
92 93 94
     * @param string $fileType File type
     * @return string Url to file
     */
95
    public function getFileUrl(\T3o\TerFe2\Domain\Model\Version $version, $fileType)
96 97 98 99 100
    {
        $filename = $this->getFileName($version, $fileType);
        $filename = $this->extensionRootPath . $filename;

        // Check if file exists
101
        if (!\T3o\TerFe2\Utility\FileUtility::fileExists($filename)) {
102
            if ($fileType === 't3x' || $fileType === 'zip') {
Jens Jacobsen's avatar
Jens Jacobsen committed
103
                //throw new \Exception('File "' . $filename . '" not found');
104 105
            }

106
            \T3o\TerFe2\Utility\LogUtility::addMessage('File "' . $filename . '" not found', 'ter_fe2', 2);
107 108 109 110
            return '';
        }

        // Get local url from absolute path
111
        return \T3o\TerFe2\Utility\FileUtility::getUrlFromAbsolutePath($filename);
112 113 114 115 116 117
    }


    /**
     * Returns name of an extension related file
     *
118
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
119 120 121
     * @param string $fileType File type
     * @return string File name
     */
122
    public function getFileName(\T3o\TerFe2\Domain\Model\Version $version, $fileType)
123 124 125 126 127 128 129 130 131 132
    {
        $extension = $version->getExtension()->getExtKey();
        $version = $version->getVersionString();
        return $this->generateFileName($extension, $version, $fileType);
    }


    /**
     * Returns all information about an extension version
     *
133
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
134 135
     * @return array Version details
     */
136
    public function getVersionDetails(\T3o\TerFe2\Domain\Model\Version $version)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    {
        $extensionKey = $version->getExtension()->getExtKey();
        $versionString = $version->getVersionString();

        $entry = $this->extensionManagerRepository->findOneByExtKeyAndVersionString($extensionKey, $versionString);
        if (!empty($entry) && is_array($entry)) {
            return $entry;
        }

        return array();
    }


    /**
     * Generates the name of an extension related file
     *
     * @param string $extension Extension key
     * @param string $version Version string
     * @param string $fileType File type
     * @return string File name
     */
    protected function generateFileName($extension, $version, $fileType)
    {
        if (empty($extension) || empty($version) || empty($fileType)) {
            return '';
        }
        $extension = strtolower($extension);
        $fileType = strtolower(trim($fileType, '. '));
        return $extension[0] . '/' . $extension[1] . '/' . $extension . '_' . $version . '.' . $fileType;
    }


    /**
     * Returns the content of an ext_emconf.php file
     *
     * @param string $extension Extension key
     * @param string $version Version string
174
     * @param string $fileHash hash of file
175
     * @return array Extension info array
176
     * @throws \Exception
177 178 179 180
     */
    protected function getExtensionInfo($extension, $version, $fileHash)
    {
        if (empty($extension) || empty($version) || empty($fileHash)) {
181
            throw new \Exception('Extension key, version and file hash are required to get extension info');
182 183 184 185 186
        }

        // Fetch file from extension root path
        $filename = $this->generateFileName($extension, $version, 't3x');
        $filename = $this->extensionRootPath . $filename;
187
        $content = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($filename);
188 189
        $filesize = strlen($content);
        if (empty($content)) {
190
            \T3o\TerFe2\Utility\LogUtility::addMessage('File "' . $filename . '" could not be fetched', 'ter_fe2', 2);
191 192 193 194 195
            return array();
        }

        // Check file hash
        if ($fileHash !== md5($content)) {
196
            \T3o\TerFe2\Utility\LogUtility::addMessage('File hash missmatch of file "' . $filename . '"', 'ter_fe2', 2);
197 198 199 200
            return array();
        }

        // Get EM_CONF array
201
        $extension = \T3o\TerFe2\Utility\ArchiveUtility::decompressT3xStream($content);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        $emConf = array();
        if (!empty($extension['EM_CONF'])) {
            $emConf = $extension['EM_CONF'];
        }
        unset($extension);
        unset($emConf['dependencies'], $emConf['conflicts'], $emConf['TYPO3_version'], $emConf['PHP_version']);

        // Remap keys
        $keyMap = array(
            'clearCacheOnLoad' => 'clearcacheonload',
            'author' => 'authorname',
            'author_email' => 'authoremail',
            'author_company' => 'authorcompany',
        );
        foreach ($emConf as $key => $value) {
            if (!empty($keyMap[$key])) {
                $emConf[$keyMap[$key]] = $value;
                unset($emConf[$key]);
            }
        }

        // Add file size
        $emConf['t3xfilesize'] = (int)$filesize;

        return $emConf;
    }


    /**
     * Build multidimensional array of extension information
     *
     * @param array $extensionRows Extension rows from repository
     * @return array All extension information
     */
    protected function buildExtensionStructure(array $extensionRows)
    {
        if (empty($extensionRows)) {
            return array();
        }

242 243
        $extensionModel = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension::class);
        $states = $extensionModel->getDefaultState(null);
244
        $states = array_flip($states);
245
        $categories = CategoryUtility::getDefaultCategories();
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
        $categories = array_flip($categories);

        $extensions = array();
        foreach ($extensionRows as $extension) {
            // Extension
            $extensions[$extension['extkey']]['ext_key'] = $extension['extkey'];
            $extensions[$extension['extkey']]['downloads'] = (int)$extension['alldownloadcounter'];
            $extensions[$extension['extkey']]['frontend_user'] = $extension['ownerusername'];
            $extensions[$extension['extkey']]['flattr_username'] = trim($extension['flattr_username']);

            // Versions
            $versionString = $extension['version'];
            $extensions[$extension['extkey']]['versions'][$versionString] = array(
                'title' => $extension['title'],
                'description' => $extension['description'],
                'version_number' => $extension['intversion'],
                'version_string' => $versionString,
                'upload_date' => $extension['lastuploaddate'],
                'upload_comment' => $extension['uploadcomment'],
                'state' => $states[(int)$extension['state']],
                'em_category' => $categories[(int)$extension['category']],
                'load_order' => $extension['loadOrder'],
                'priority' => $extension['priority'],
                'shy' => $extension['shy'],
                'internal' => $extension['internal'],
                'do_not_load_in_fe' => $extension['doNotLoadInFE'],
                'uploadfolder' => $extension['uploadfolder'],
                'clear_cache_on_load' => $extension['clearcacheonload'],
                'module' => $extension['module'],
                'create_dirs' => $extension['createDirs'],
                'modify_tables' => $extension['modify_tables'],
                'lock_type' => $extension['lockType'],
                'cgl_compliance' => $extension['CGLcompliance'],
                'cgl_compliance_note' => $extension['CGLcompliance_note'],
                'download_counter' => (int)$extension['downloadcounter'],
Markus Sommer's avatar
Markus Sommer committed
281
                'manual' => null,
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
                'repository' => $extension['repository'],
                'review_state' => $extension['reviewstate'],
                'file_hash' => $extension['t3xfilemd5'],
                't3x_file_size' => $extension['t3xfilesize'],
                'relations' => array(),
            );

            // Author
            $extensions[$extension['extkey']]['versions'][$versionString]['author'] = array(
                'name' => $extension['authorname'],
                'email' => $extension['authoremail'],
                'company' => $extension['authorcompany'],
                'username' => $extension['ownerusername'],
            );

            // Relations
            $dependencies = unserialize($extension['dependencies']);
            foreach ($dependencies as $relationType => $relations) {
                foreach ($relations as $relationKey => $versionRange) {
                    $version = $this->getVersionByRange($versionRange);
                    $extensions[$extension['extkey']]['versions'][$versionString]['relations'][] = array(
                        'relation_type' => $relationType,
                        'software_type' => '',
                        'relation_key' => $relationKey,
                        'minimum_version' => $version[0],
                        'maximum_version' => $version[1],
                    );
                }
            }
        }

        return $extensions;
    }
flyguide's avatar
flyguide committed
315
}