[BUGFIX] Fix warning during extension installation
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / Connection / TerUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility\Connection;
3
4 /* **************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * (c) 2006-2013 Karsten Dambekalns <karsten@typo3.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * TER2 connection handling class for the TYPO3 Extension Manager.
32 *
33 * It contains methods for downloading and uploading extensions and related code
34 *
35 * @author Karsten Dambekalns <karsten@typo3.org>
36 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
37 */
38 class TerUtility {
39
40 /**
41 * @var string
42 */
43 public $wsdlUrl;
44
45 /**
46 * Fetches an extension from the given mirror
47 *
48 * @param string $extensionKey Extension Key
49 * @param string $version Version to install
50 * @param string $expectedMd5 Expected MD5 hash of extension file
51 * @param string $mirrorUrl URL of mirror to use
52 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
53 * @return array T3X data
54 */
55 public function fetchExtension($extensionKey, $version, $expectedMd5, $mirrorUrl) {
56 $extensionPath = \TYPO3\CMS\Core\Utility\GeneralUtility::strtolower($extensionKey);
57 $mirrorUrl .= $extensionPath[0] . '/' . $extensionPath[1] . '/' . $extensionPath . '_' . $version . '.t3x';
58 $t3x = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($mirrorUrl, 0, array(TYPO3_user_agent));
59 $md5 = md5($t3x);
60 if ($t3x === FALSE) {
61 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('The T3X file "%s" could not be fetched. Possible reasons: network problems, allow_url_fopen is off,' . ' cURL is not enabled in Install Tool.', $mirrorUrl), 1334426097);
62 }
63 if ($md5 === $expectedMd5) {
64 // Fetch and return:
65 $extensionData = $this->decodeExchangeData($t3x);
66 } else {
67 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Error: MD5 hash of downloaded file not as expected:<br />' . $md5 . ' != ' . $expectedMd5, 1334426098);
68 }
69 return $extensionData;
70 }
71
72 /**
73 * Decode server data
74 * This is information like the extension list, extension
75 * information etc., return data after uploads (new em_conf)
76 * On success, returns an array with data array and stats
77 * array as key 0 and 1.
78 *
79 * @param string $externalData Data stream from remove server
80 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
81 * @return array $externalData
82 * @see fetchServerData(), processRepositoryReturnData()
83 */
84 public function decodeServerData($externalData) {
85 $parts = explode(':', $externalData, 4);
86 $dat = base64_decode($parts[2]);
87 gzuncompress($dat);
88 // compare hashes ignoring any leading whitespace. See bug #0000365.
89 if (ltrim($parts[0]) == md5($dat)) {
90 if ($parts[1] == 'gzcompress') {
91 if (function_exists('gzuncompress')) {
92 $dat = gzuncompress($dat);
93 } else {
94 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Decoding Error: No decompressor available for compressed content. gzuncompress() function is not available!', 1342859463);
95 }
96 }
97 $listArr = unserialize($dat);
98 if (!is_array($listArr)) {
99 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Error: Unserialized information was not an array - strange!', 1342859489);
100 }
101 } else {
102 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Error: MD5 hashes in T3X data did not match!', 1342859505);
103 }
104 return $listArr;
105 }
106
107 /**
108 * Decodes extension upload array.
109 * This kind of data is when an extension is uploaded to TER
110 *
111 * @param string $stream Data stream
112 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
113 * @return array Array with result on success, otherwise an error string.
114 */
115 public function decodeExchangeData($stream) {
116 $parts = explode(':', $stream, 3);
117 if ($parts[1] == 'gzcompress') {
118 if (function_exists('gzuncompress')) {
119 $parts[2] = gzuncompress($parts[2]);
120 } else {
121 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Decoding Error: No decompressor available for compressed content. gzcompress()/gzuncompress() ' . 'functions are not available!', 1344761814);
122 }
123 }
124 if (md5($parts[2]) === $parts[0]) {
125 $output = unserialize($parts[2]);
126 if (!is_array($output)) {
127 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Error: Content could not be unserialized to an array. Strange (since MD5 hashes match!)', 1344761938);
128 }
129 } else {
130 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Error: MD5 mismatch. Maybe the extension file was downloaded and saved as a text file by the ' . 'browser and thereby corrupted!? (Always select "All" filetype when saving extensions)', 1344761991);
131 }
132 return $output;
133 }
134
135 }