[BUGFIX] Only throw EM Offline Exception when EM is offline
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / Connection / TerUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility\Connection;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
18
19 /**
20 * TER2 connection handling class for the TYPO3 Extension Manager.
21 *
22 * It contains methods for downloading and uploading extensions and related code
23 */
24 class TerUtility {
25
26 /**
27 * @var string
28 */
29 public $wsdlUrl;
30
31 /**
32 * @var \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility
33 * @inject
34 */
35 protected $configurationUtility;
36
37 /**
38 * Fetches an extension from the given mirror
39 *
40 * @param string $extensionKey Extension Key
41 * @param string $version Version to install
42 * @param string $expectedMd5 Expected MD5 hash of extension file
43 * @param string $mirrorUrl URL of mirror to use
44 * @throws ExtensionManagerException
45 * @return array T3X data
46 */
47 public function fetchExtension($extensionKey, $version, $expectedMd5, $mirrorUrl) {
48 if (!empty($this->configurationUtility->getCurrentConfiguration('extensionmanager')['offlineMode']['value'])) {
49 throw new ExtensionManagerException('Extension Manager is in offline mode. No TER connection available.', 1437078620);
50 }
51 $extensionPath = \TYPO3\CMS\Core\Utility\GeneralUtility::strtolower($extensionKey);
52 $mirrorUrl .= $extensionPath[0] . '/' . $extensionPath[1] . '/' . $extensionPath . '_' . $version . '.t3x';
53 $t3x = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($mirrorUrl, 0, array(TYPO3_user_agent));
54 $md5 = md5($t3x);
55 if ($t3x === FALSE) {
56 throw new 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);
57 }
58 if ($md5 === $expectedMd5) {
59 // Fetch and return:
60 $extensionData = $this->decodeExchangeData($t3x);
61 } else {
62 throw new ExtensionManagerException('Error: MD5 hash of downloaded file not as expected: ' . $md5 . ' != ' . $expectedMd5, 1334426098);
63 }
64 return $extensionData;
65 }
66
67 /**
68 * Decode server data
69 * This is information like the extension list, extension
70 * information etc., return data after uploads (new em_conf)
71 * On success, returns an array with data array and stats
72 * array as key 0 and 1.
73 *
74 * @param string $externalData Data stream from remove server
75 * @throws ExtensionManagerException
76 * @return array $externalData
77 * @see fetchServerData(), processRepositoryReturnData()
78 */
79 public function decodeServerData($externalData) {
80 $parts = explode(':', $externalData, 4);
81 $dat = base64_decode($parts[2]);
82 gzuncompress($dat);
83 // compare hashes ignoring any leading whitespace. See bug #0000365.
84 if (ltrim($parts[0]) == md5($dat)) {
85 if ($parts[1] == 'gzcompress') {
86 if (function_exists('gzuncompress')) {
87 $dat = gzuncompress($dat);
88 } else {
89 throw new ExtensionManagerException('Decoding Error: No decompressor available for compressed content. gzuncompress() function is not available!', 1342859463);
90 }
91 }
92 $listArr = unserialize($dat);
93 if (!is_array($listArr)) {
94 throw new ExtensionManagerException('Error: Unserialized information was not an array - strange!', 1342859489);
95 }
96 } else {
97 throw new ExtensionManagerException('Error: MD5 hashes in T3X data did not match!', 1342859505);
98 }
99 return $listArr;
100 }
101
102 /**
103 * Decodes extension upload array.
104 * This kind of data is when an extension is uploaded to TER
105 *
106 * @param string $stream Data stream
107 * @throws ExtensionManagerException
108 * @return array Array with result on success, otherwise an error string.
109 */
110 public function decodeExchangeData($stream) {
111 $parts = explode(':', $stream, 3);
112 if ($parts[1] == 'gzcompress') {
113 if (function_exists('gzuncompress')) {
114 $parts[2] = gzuncompress($parts[2]);
115 } else {
116 throw new ExtensionManagerException('Decoding Error: No decompressor available for compressed content. gzcompress()/gzuncompress() ' . 'functions are not available!', 1344761814);
117 }
118 }
119 if (md5($parts[2]) === $parts[0]) {
120 $output = unserialize($parts[2]);
121 if (!is_array($output)) {
122 throw new ExtensionManagerException('Error: Content could not be unserialized to an array. Strange (since MD5 hashes match!)', 1344761938);
123 }
124 } else {
125 throw new 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);
126 }
127 return $output;
128 }
129
130 }