61b39a1f09e1775924c5c7ddd24545db85f2c212
[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 use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
31
32 /**
33 * TER2 connection handling class for the TYPO3 Extension Manager.
34 *
35 * It contains methods for downloading and uploading extensions and related code
36 *
37 * @author Karsten Dambekalns <karsten@typo3.org>
38 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
39 */
40 class TerUtility {
41
42 /**
43 * @var string
44 */
45 public $wsdlUrl;
46
47 /**
48 * Fetches an extension from the given mirror
49 *
50 * @param string $extensionKey Extension Key
51 * @param string $version Version to install
52 * @param string $expectedMd5 Expected MD5 hash of extension file
53 * @param string $mirrorUrl URL of mirror to use
54 * @throws ExtensionManagerException
55 * @return array T3X data
56 */
57 public function fetchExtension($extensionKey, $version, $expectedMd5, $mirrorUrl) {
58 $extensionPath = \TYPO3\CMS\Core\Utility\GeneralUtility::strtolower($extensionKey);
59 $mirrorUrl .= $extensionPath[0] . '/' . $extensionPath[1] . '/' . $extensionPath . '_' . $version . '.t3x';
60 $t3x = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($mirrorUrl, 0, array(TYPO3_user_agent));
61 $md5 = md5($t3x);
62 if ($t3x === FALSE) {
63 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);
64 }
65 if ($md5 === $expectedMd5) {
66 // Fetch and return:
67 $extensionData = $this->decodeExchangeData($t3x);
68 } else {
69 throw new ExtensionManagerException('Error: MD5 hash of downloaded file not as expected:<br />' . $md5 . ' != ' . $expectedMd5, 1334426098);
70 }
71 return $extensionData;
72 }
73
74 /**
75 * Decode server data
76 * This is information like the extension list, extension
77 * information etc., return data after uploads (new em_conf)
78 * On success, returns an array with data array and stats
79 * array as key 0 and 1.
80 *
81 * @param string $externalData Data stream from remove server
82 * @throws ExtensionManagerException
83 * @return array $externalData
84 * @see fetchServerData(), processRepositoryReturnData()
85 */
86 public function decodeServerData($externalData) {
87 $parts = explode(':', $externalData, 4);
88 $dat = base64_decode($parts[2]);
89 gzuncompress($dat);
90 // compare hashes ignoring any leading whitespace. See bug #0000365.
91 if (ltrim($parts[0]) == md5($dat)) {
92 if ($parts[1] == 'gzcompress') {
93 if (function_exists('gzuncompress')) {
94 $dat = gzuncompress($dat);
95 } else {
96 throw new ExtensionManagerException('Decoding Error: No decompressor available for compressed content. gzuncompress() function is not available!', 1342859463);
97 }
98 }
99 $listArr = unserialize($dat);
100 if (!is_array($listArr)) {
101 throw new ExtensionManagerException('Error: Unserialized information was not an array - strange!', 1342859489);
102 }
103 } else {
104 throw new ExtensionManagerException('Error: MD5 hashes in T3X data did not match!', 1342859505);
105 }
106 return $listArr;
107 }
108
109 /**
110 * Decodes extension upload array.
111 * This kind of data is when an extension is uploaded to TER
112 *
113 * @param string $stream Data stream
114 * @throws ExtensionManagerException
115 * @return array Array with result on success, otherwise an error string.
116 */
117 public function decodeExchangeData($stream) {
118 $parts = explode(':', $stream, 3);
119 if ($parts[1] == 'gzcompress') {
120 if (function_exists('gzuncompress')) {
121 $parts[2] = gzuncompress($parts[2]);
122 } else {
123 throw new ExtensionManagerException('Decoding Error: No decompressor available for compressed content. gzcompress()/gzuncompress() ' . 'functions are not available!', 1344761814);
124 }
125 }
126 if (md5($parts[2]) === $parts[0]) {
127 $output = unserialize($parts[2]);
128 if (!is_array($output)) {
129 throw new ExtensionManagerException('Error: Content could not be unserialized to an array. Strange (since MD5 hashes match!)', 1344761938);
130 }
131 } else {
132 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);
133 }
134 return $output;
135 }
136
137 }