[BUGFIX] Fix Http_Request after namespace changes
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Http / HttpRequest.php
1 <?php
2 namespace TYPO3\CMS\Core\Http;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2012 Philipp Gampe <philipp.gampe@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 require_once('HTTP/Request2.php');
31
32 /**
33 * HTTP Request Utility class
34 *
35 * Extends \HTTP_Request2 and sets TYPO3 environment defaults
36 *
37 * @author Philipp Gampe <philipp.gampe@typo3.org>
38 * @package TYPO3
39 * @subpackage t3lib
40 */
41 class HttpRequest extends \HTTP_Request2 {
42
43 /**
44 * Default constructor - sets TYPO3 defaults
45 *
46 * @param string|\Net_Url2 $url Request URL
47 * @param string $method Request Method (GET, HEAD or POST). Redirects reset this to GET unless "strict_redirects" is set.
48 * @param array $config Configuration for this request instance
49 * @link http://pear.php.net/manual/en/package.http.http-request2.config.php
50 */
51 public function __construct($url = NULL, $method = self::METHOD_GET, array $config = array()) {
52 parent::__construct($url, $method);
53 $this->setConfiguration($config);
54 }
55
56 /**
57 * Sets the configuration for this object instance.
58 * Merges default values with provided $config and overrides all
59 * not provided values with those from $TYPO3_CONF_VARS
60 *
61 * @param array $config Configuration options which override the default configuration
62 * @return void
63 * @see http://pear.php.net/manual/en/package.http.http-request2.config.php
64 */
65 public function setConfiguration(array $config = array()) {
66 // set a branded user-agent
67 $this->setHeader('user-agent', $GLOBALS['TYPO3_CONF_VARS']['HTTP']['userAgent']);
68 $default = array(
69 'adapter' => $GLOBALS['TYPO3_CONF_VARS']['HTTP']['adapter'],
70 'connect_timeout' => $GLOBALS['TYPO3_CONF_VARS']['HTTP']['connect_timeout'],
71 'timeout' => $GLOBALS['TYPO3_CONF_VARS']['HTTP']['timeout'],
72 'protocol_version' => $GLOBALS['TYPO3_CONF_VARS']['HTTP']['protocol_version'],
73 'follow_redirects' => (bool) $GLOBALS['TYPO3_CONF_VARS']['HTTP']['follow_redirects'],
74 'max_redirects' => $GLOBALS['TYPO3_CONF_VARS']['HTTP']['max_redirects'],
75 'strict_redirects' => (bool) $GLOBALS['TYPO3_CONF_VARS']['HTTP']['strict_redirects'],
76 'proxy_host' => $GLOBALS['TYPO3_CONF_VARS']['HTTP']['proxy_host'],
77 'proxy_port' => $GLOBALS['TYPO3_CONF_VARS']['HTTP']['proxy_port'],
78 'proxy_user' => $GLOBALS['TYPO3_CONF_VARS']['HTTP']['proxy_user'],
79 'proxy_password' => $GLOBALS['TYPO3_CONF_VARS']['HTTP']['proxy_password'],
80 'proxy_auth_scheme' => $GLOBALS['TYPO3_CONF_VARS']['HTTP']['proxy_auth_scheme'],
81 'ssl_verify_peer' => (bool) $GLOBALS['TYPO3_CONF_VARS']['HTTP']['ssl_verify_peer'],
82 'ssl_verify_host' => (bool) $GLOBALS['TYPO3_CONF_VARS']['HTTP']['ssl_verify_host'],
83 // we have to deal with Install Tool limitations and set this to NULL if it is empty
84 'ssl_cafile' => empty($GLOBALS['TYPO3_CONF_VARS']['HTTP']['ssl_cafile']) ? NULL : $GLOBALS['TYPO3_CONF_VARS']['HTTP']['ssl_cafile'],
85 'ssl_capath' => empty($GLOBALS['TYPO3_CONF_VARS']['HTTP']['ssl_capath']) ? NULL : $GLOBALS['TYPO3_CONF_VARS']['HTTP']['ssl_capath'],
86 'ssl_local_cert' => empty($GLOBALS['TYPO3_CONF_VARS']['HTTP']['ssl_local_cert']) ? NULL : $GLOBALS['TYPO3_CONF_VARS']['HTTP']['ssl_local_cert'],
87 'ssl_passphrase' => empty($GLOBALS['TYPO3_CONF_VARS']['HTTP']['ssl_passphrase']) ? NULL : $GLOBALS['TYPO3_CONF_VARS']['HTTP']['ssl_passphrase']
88 );
89 $configuration = array_merge($default, $config);
90 $this->setConfig($configuration);
91 }
92
93 /**
94 * Downloads chunk by chunk to file instead of saving the whole response into memory.
95 * $response->getBody() will be empty.
96 * An existing file will be overridden.
97 *
98 * @param string $directory The absolute path to the directory in which the file is saved.
99 * @param string $filename The filename - if not set, it is determined automatically.
100 * @return \HTTP_Request2_Response The response with empty body.
101 */
102 public function download($directory, $filename = '') {
103 $isAttached = FALSE;
104 // Do not store the body in memory
105 $this->setConfig('store_body', FALSE);
106 // Check if we already attached an instance of download. If so, just reuse it.
107 foreach ($this->observers as $observer) {
108 if ($observer instanceof \TYPO3\CMS\Core\Http\Observer\Download) {
109 /** @var \TYPO3\CMS\Core\Http\Observer\Download $attached */
110 $observer->setDirectory($directory);
111 $observer->setFilename($filename);
112 $isAttached = TRUE;
113 }
114 }
115 if (!$isAttached) {
116 /** @var \TYPO3\CMS\Core\Http\Observer\Download $observer */
117 $observer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Http\\Observer\\Download', $directory, $filename);
118 $this->attach($observer);
119 }
120 return $this->send();
121 }
122
123 }
124
125 ?>