Align search result CSS with Wikimedia UI color palette
[lhc/web/wiklou.git] / includes / http / Http.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\Logger\LoggerFactory;
22
23 /**
24 * Various HTTP related functions
25 * @ingroup HTTP
26 */
27 class Http {
28 static public $httpEngine = false;
29
30 /**
31 * Perform an HTTP request
32 *
33 * @param string $method HTTP method. Usually GET/POST
34 * @param string $url Full URL to act on. If protocol-relative, will be expanded to an http:// URL
35 * @param array $options Options to pass to MWHttpRequest object.
36 * Possible keys for the array:
37 * - timeout Timeout length in seconds
38 * - connectTimeout Timeout for connection, in seconds (curl only)
39 * - postData An array of key-value pairs or a url-encoded form data
40 * - proxy The proxy to use.
41 * Otherwise it will use $wgHTTPProxy (if set)
42 * Otherwise it will use the environment variable "http_proxy" (if set)
43 * - noProxy Don't use any proxy at all. Takes precedence over proxy value(s).
44 * - sslVerifyHost Verify hostname against certificate
45 * - sslVerifyCert Verify SSL certificate
46 * - caInfo Provide CA information
47 * - maxRedirects Maximum number of redirects to follow (defaults to 5)
48 * - followRedirects Whether to follow redirects (defaults to false).
49 * Note: this should only be used when the target URL is trusted,
50 * to avoid attacks on intranet services accessible by HTTP.
51 * - userAgent A user agent, if you want to override the default
52 * MediaWiki/$wgVersion
53 * - logger A \Psr\Logger\LoggerInterface instance for debug logging
54 * @param string $caller The method making this request, for profiling
55 * @return string|bool (bool)false on failure or a string on success
56 */
57 public static function request( $method, $url, $options = [], $caller = __METHOD__ ) {
58 wfDebug( "HTTP: $method: $url\n" );
59
60 $options['method'] = strtoupper( $method );
61
62 if ( !isset( $options['timeout'] ) ) {
63 $options['timeout'] = 'default';
64 }
65 if ( !isset( $options['connectTimeout'] ) ) {
66 $options['connectTimeout'] = 'default';
67 }
68
69 $req = MWHttpRequest::factory( $url, $options, $caller );
70 $status = $req->execute();
71
72 if ( $status->isOK() ) {
73 return $req->getContent();
74 } else {
75 $errors = $status->getErrorsByType( 'error' );
76 $logger = LoggerFactory::getInstance( 'http' );
77 $logger->warning( $status->getWikiText( false, false, 'en' ),
78 [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
79 return false;
80 }
81 }
82
83 /**
84 * Simple wrapper for Http::request( 'GET' )
85 * @see Http::request()
86 * @since 1.25 Second parameter $timeout removed. Second parameter
87 * is now $options which can be given a 'timeout'
88 *
89 * @param string $url
90 * @param array $options
91 * @param string $caller The method making this request, for profiling
92 * @return string|bool false on error
93 */
94 public static function get( $url, $options = [], $caller = __METHOD__ ) {
95 $args = func_get_args();
96 if ( isset( $args[1] ) && ( is_string( $args[1] ) || is_numeric( $args[1] ) ) ) {
97 // Second was used to be the timeout
98 // And third parameter used to be $options
99 wfWarn( "Second parameter should not be a timeout.", 2 );
100 $options = isset( $args[2] ) && is_array( $args[2] ) ?
101 $args[2] : [];
102 $options['timeout'] = $args[1];
103 $caller = __METHOD__;
104 }
105 return Http::request( 'GET', $url, $options, $caller );
106 }
107
108 /**
109 * Simple wrapper for Http::request( 'POST' )
110 * @see Http::request()
111 *
112 * @param string $url
113 * @param array $options
114 * @param string $caller The method making this request, for profiling
115 * @return string|bool false on error
116 */
117 public static function post( $url, $options = [], $caller = __METHOD__ ) {
118 return Http::request( 'POST', $url, $options, $caller );
119 }
120
121 /**
122 * A standard user-agent we can use for external requests.
123 * @return string
124 */
125 public static function userAgent() {
126 global $wgVersion;
127 return "MediaWiki/$wgVersion";
128 }
129
130 /**
131 * Checks that the given URI is a valid one. Hardcoding the
132 * protocols, because we only want protocols that both cURL
133 * and php support.
134 *
135 * file:// should not be allowed here for security purpose (r67684)
136 *
137 * @todo FIXME this is wildly inaccurate and fails to actually check most stuff
138 *
139 * @param string $uri URI to check for validity
140 * @return bool
141 */
142 public static function isValidURI( $uri ) {
143 return preg_match(
144 '/^https?:\/\/[^\/\s]\S*$/D',
145 $uri
146 );
147 }
148
149 /**
150 * Gets the relevant proxy from $wgHTTPProxy
151 *
152 * @return mixed The proxy address or an empty string if not set.
153 */
154 public static function getProxy() {
155 global $wgHTTPProxy;
156
157 if ( $wgHTTPProxy ) {
158 return $wgHTTPProxy;
159 }
160
161 return "";
162 }
163 }