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

flyguide's avatar
flyguide committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*******************************************************************
 *  Copyright notice
 *
 *  (c) 2011 Kai Vogel <kai.vogel@speedprogs.de>, Speedprogs.de
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ******************************************************************/

/**
 * Extension provider using local files
 */
31
class FileProvider extends \T3o\TerFe2\Provider\AbstractProvider
32
33
34
{

    /**
35
     * @var \T3o\TerFe2\Domain\Repository\ExtensionManagerCacheEntryRepository
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
     */
    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'];
        }
56
        $this->extensionRootPath = \T3o\TerFe2\Utility\File::getAbsoluteDirectory($this->extensionRootPath);
57
58

        // Get repository for extension manager cache entries
59
        $this->extensionManagerRepository = $this->objectManager->get(\T3o\TerFe2\Domain\Repository\ExtensionManagerCacheEntryRepository::class);
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
91
92
93
94
95
96
97
98
99
    }


    /**
     * 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
     *
100
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
101
102
103
     * @param string $fileType File type
     * @return string Url to file
     */
104
    public function getFileUrl(\T3o\TerFe2\Domain\Model\Version $version, $fileType)
105
106
107
108
109
    {
        $filename = $this->getFileName($version, $fileType);
        $filename = $this->extensionRootPath . $filename;

        // Check if file exists
110
        if (!\T3o\TerFe2\Utility\File::fileExists($filename)) {
111
            if ($fileType === 't3x' || $fileType === 'zip') {
112
                #throw new \Exception('File "' . $filename . '" not found');
113
114
            }

115
            \T3o\TerFe2\Utility\Log::addMessage('File "' . $filename . '" not found', 'ter_fe2', 2);
116
117
118
119
            return '';
        }

        // Get local url from absolute path
120
        return \T3o\TerFe2\Utility\File::getUrlFromAbsolutePath($filename);
121
122
123
124
125
126
    }


    /**
     * Returns name of an extension related file
     *
127
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
128
129
130
     * @param string $fileType File type
     * @return string File name
     */
131
    public function getFileName(\T3o\TerFe2\Domain\Model\Version $version, $fileType)
132
133
134
135
136
137
138
139
140
141
    {
        $extension = $version->getExtension()->getExtKey();
        $version = $version->getVersionString();
        return $this->generateFileName($extension, $version, $fileType);
    }


    /**
     * Returns all information about an extension version
     *
142
     * @param \T3o\TerFe2\Domain\Model\Version $version Version object
143
144
     * @return array Version details
     */
145
    public function getVersionDetails(\T3o\TerFe2\Domain\Model\Version $version)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    {
        $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
     * @return array Extension info array
     */
    protected function getExtensionInfo($extension, $version, $fileHash)
    {
        if (empty($extension) || empty($version) || empty($fileHash)) {
188
            throw new \Exception('Extension key, version and file hash are required to get extension info');
189
190
191
192
193
        }

        // Fetch file from extension root path
        $filename = $this->generateFileName($extension, $version, 't3x');
        $filename = $this->extensionRootPath . $filename;
194
        $content = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($filename);
195
196
        $filesize = strlen($content);
        if (empty($content)) {
197
            \T3o\TerFe2\Utility\Log::addMessage('File "' . $filename . '" could not be fetched', 'ter_fe2', 2);
198
199
200
201
202
            return array();
        }

        // Check file hash
        if ($fileHash !== md5($content)) {
203
            \T3o\TerFe2\Utility\Log::addMessage('File hash missmatch of file "' . $filename . '"', 'ter_fe2', 2);
204
205
206
207
            return array();
        }

        // Get EM_CONF array
208
        $extension = \T3o\TerFe2\Utility\Archive::decompressT3xStream($content);
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
242
243
244
245
246
247
248
        $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();
        }

249
250
        $extensionModel = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension::class);
        $states = $extensionModel->getDefaultState(null);
251
        $states = array_flip($states);
Sebastian Diez's avatar
Sebastian Diez committed
252
        $categories = Tx_TerFe2_Utility_Category::getDefaultCategories(null);
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
281
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
315
316
317
318
319
320
321
        $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'],
                'manual' => NULL,
                '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
322
323

}