Index: trunk/phase3/includes/HttpFunctions.php |
— | — | @@ -8,23 +8,29 @@ |
9 | 9 | * @ingroup HTTP |
10 | 10 | */ |
11 | 11 | class Http { |
| 12 | + // Syncronous download (in a single request) |
| 13 | + const SYNC_DOWNLOAD = 1; |
| 14 | + |
| 15 | + // Asynchronous download ( background process with multiple requests ) |
| 16 | + const ASYNC_DOWNLOAD = 2; |
| 17 | + |
12 | 18 | /** |
13 | | - * Perform an HTTP request |
| 19 | + * Get the contents of a file by HTTP |
14 | 20 | * @param $method string HTTP method. Usually GET/POST |
15 | 21 | * @param $url string Full URL to act on |
16 | | - * @param $opts options to pass to HttpRequest object |
17 | | - * @returns mixed (bool)false on failure or a string on success |
| 22 | + * @param $timeout int Seconds to timeout. 'default' falls to $wgHTTPTimeout |
| 23 | + * @param $curlOptions array Optional array of extra params to pass |
| 24 | + * to curl_setopt() |
18 | 25 | */ |
19 | 26 | public static function request( $method, $url, $opts = array() ) { |
20 | | - $opts['method'] = strtoupper( $method ); |
21 | | - if ( !array_key_exists( 'timeout', $opts ) ) { |
22 | | - $opts['timeout'] = 'default'; |
23 | | - } |
24 | | - $req = HttpRequest::factory( $url, $opts ); |
| 27 | + $opts['method'] = ( strtoupper( $method ) == 'GET' || strtoupper( $method ) == 'POST' ) |
| 28 | + ? strtoupper( $method ) : null; |
| 29 | + $req = HttpRequest::newRequest( $url, $opts ); |
25 | 30 | $status = $req->doRequest(); |
26 | | - if ( $status->isOK() ) { |
27 | | - return $req->getContent(); |
| 31 | + if( $status->isOK() ) { |
| 32 | + return $status->value; |
28 | 33 | } else { |
| 34 | + wfDebug( 'http error: ' . $status->getWikiText() ); |
29 | 35 | return false; |
30 | 36 | } |
31 | 37 | } |
— | — | @@ -33,8 +39,10 @@ |
34 | 40 | * Simple wrapper for Http::request( 'GET' ) |
35 | 41 | * @see Http::request() |
36 | 42 | */ |
37 | | - public static function get( $url, $timeout = 'default', $opts = array() ) { |
38 | | - $opts['timeout'] = $timeout; |
| 43 | + public static function get( $url, $timeout = false, $opts = array() ) { |
| 44 | + global $wgSyncHTTPTimeout; |
| 45 | + if( $timeout ) |
| 46 | + $opts['timeout'] = $timeout; |
39 | 47 | return Http::request( 'GET', $url, $opts ); |
40 | 48 | } |
41 | 49 | |
— | — | @@ -46,90 +54,24 @@ |
47 | 55 | return Http::request( 'POST', $url, $opts ); |
48 | 56 | } |
49 | 57 | |
50 | | - /** |
51 | | - * Check if the URL can be served by localhost |
52 | | - * @param $url string Full url to check |
53 | | - * @return bool |
54 | | - */ |
55 | | - public static function isLocalURL( $url ) { |
56 | | - global $wgCommandLineMode, $wgConf; |
57 | | - if ( $wgCommandLineMode ) { |
58 | | - return false; |
59 | | - } |
60 | | - |
61 | | - // Extract host part |
62 | | - $matches = array(); |
63 | | - if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) { |
64 | | - $host = $matches[1]; |
65 | | - // Split up dotwise |
66 | | - $domainParts = explode( '.', $host ); |
67 | | - // Check if this domain or any superdomain is listed in $wgConf as a local virtual host |
68 | | - $domainParts = array_reverse( $domainParts ); |
69 | | - for ( $i = 0; $i < count( $domainParts ); $i++ ) { |
70 | | - $domainPart = $domainParts[$i]; |
71 | | - if ( $i == 0 ) { |
72 | | - $domain = $domainPart; |
73 | | - } else { |
74 | | - $domain = $domainPart . '.' . $domain; |
75 | | - } |
76 | | - if ( $wgConf->isLocalVHost( $domain ) ) { |
77 | | - return true; |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - return false; |
82 | | - } |
83 | | - |
84 | | - /** |
85 | | - * A standard user-agent we can use for external requests. |
86 | | - * @returns string |
87 | | - */ |
88 | | - public static function userAgent() { |
89 | | - global $wgVersion; |
90 | | - return "MediaWiki/$wgVersion"; |
91 | | - } |
92 | | - |
93 | | - /** |
94 | | - * Checks that the given URI is a valid one |
95 | | - * @param $uri Mixed: URI to check for validity |
96 | | - * @returns bool |
97 | | - */ |
98 | | - public static function isValidURI( $uri ) { |
99 | | - return preg_match( |
100 | | - '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/', |
101 | | - $uri, |
102 | | - $matches |
103 | | - ); |
104 | | - } |
105 | | - /** |
106 | | - * Fetch a URL, write the result to a file. |
107 | | - * @params $url string url to fetch |
108 | | - * @params $targetFilePath string full path (including filename) to write the file to |
109 | | - * @params $async bool whether the download should be asynchronous (defaults to false) |
110 | | - * @params $redirectCount int used internally to keep track of the number of redirects |
111 | | - * |
112 | | - * @returns Status -- for async requests this will contain the request key |
113 | | - */ |
114 | | - public static function doDownload( $url, $targetFilePath, $async = false, $redirectCount = 0 ) { |
| 58 | + public static function doDownload( $url, $target_file_path, $dl_mode = self::SYNC_DOWNLOAD, |
| 59 | + $redirectCount = 0 ) |
| 60 | + { |
115 | 61 | global $wgPhpCli, $wgMaxUploadSize, $wgMaxRedirects; |
116 | | - |
117 | 62 | // do a quick check to HEAD to insure the file size is not > $wgMaxUploadSize |
118 | | - $headRequest = HttpRequest::factory( $url, array( 'headersOnly' => true ) ); |
| 63 | + $headRequest = HttpRequest::newRequest( $url, array( 'headers_only' => true ) ); |
119 | 64 | $headResponse = $headRequest->doRequest(); |
120 | | - if ( !$headResponse->isOK() ) { |
| 65 | + if( !$headResponse->isOK() ) { |
121 | 66 | return $headResponse; |
122 | 67 | } |
123 | 68 | $head = $headResponse->value; |
124 | 69 | |
125 | 70 | // check for redirects: |
126 | | - if ( $redirectCount < 0 ) { |
127 | | - $redirectCount = 0; |
128 | | - } |
129 | | - if ( isset( $head['Location'] ) && strrpos( $head[0], '302' ) !== false ) { |
130 | | - if ( $redirectCount < $wgMaxRedirects ) { |
131 | | - if ( self::isValidURI( $head['Location'] ) ) { |
132 | | - return self::doDownload( $head['Location'], $targetFilePath, |
133 | | - $async, $redirectCount++ ); |
| 71 | + if( isset( $head['Location'] ) && strrpos( $head[0], '302' ) !== false ) { |
| 72 | + if( $redirectCount < $wgMaxRedirects ) { |
| 73 | + if( self::isValidURI( $head['Location'] ) ) { |
| 74 | + return self::doDownload( $head['Location'], $target_file_path, |
| 75 | + $dl_mode, $redirectCount++ ); |
134 | 76 | } else { |
135 | 77 | return Status::newFatal( 'upload-proto-error' ); |
136 | 78 | } |
— | — | @@ -138,355 +80,332 @@ |
139 | 81 | } |
140 | 82 | } |
141 | 83 | // we did not get a 200 ok response: |
142 | | - if ( strrpos( $head[0], '200 OK' ) === false ) { |
| 84 | + if( strrpos( $head[0], '200 OK' ) === false ) { |
143 | 85 | return Status::newFatal( 'upload-http-error', htmlspecialchars( $head[0] ) ); |
144 | 86 | } |
145 | 87 | |
146 | | - $contentLength = $head['Content-Length']; |
147 | | - if ( $contentLength ) { |
148 | | - if ( $contentLength > $wgMaxUploadSize ) { |
149 | | - return Status::newFatal( 'requested file length ' . $contentLength . |
150 | | - ' is greater than $wgMaxUploadSize: ' . $wgMaxUploadSize ); |
| 88 | + $content_length = ( isset( $head['Content-Length'] ) ) ? $head['Content-Length'] : null; |
| 89 | + if( $content_length ) { |
| 90 | + if( $content_length > $wgMaxUploadSize ) { |
| 91 | + return Status::newFatal( 'requested file length ' . $content_length . |
| 92 | + ' is greater than $wgMaxUploadSize: ' . $wgMaxUploadSize ); |
151 | 93 | } |
152 | 94 | } |
153 | 95 | |
154 | 96 | // check if we can find phpCliPath (for doing a background shell request to |
155 | 97 | // php to do the download: |
156 | | - if ( $async && $wgPhpCli && wfShellExecEnabled() ) { |
| 98 | + if( $wgPhpCli && wfShellExecEnabled() && $dl_mode == self::ASYNC_DOWNLOAD ) { |
157 | 99 | wfDebug( __METHOD__ . "\nASYNC_DOWNLOAD\n" ); |
158 | | - // setup session and shell call: |
159 | | - return self::startBackgroundRequest( $url, $targetFilePath, $contentLength ); |
| 100 | + //setup session and shell call: |
| 101 | + return self::initBackgroundDownload( $url, $target_file_path, $content_length ); |
160 | 102 | } else { |
161 | 103 | wfDebug( __METHOD__ . "\nSYNC_DOWNLOAD\n" ); |
162 | 104 | // SYNC_DOWNLOAD download as much as we can in the time we have to execute |
163 | 105 | $opts['method'] = 'GET'; |
164 | | - $opts['targetFilePath'] = $mTargetFilePath; |
165 | | - $req = HttpRequest::factory( $url, $opts ); |
| 106 | + $opts['target_file_path'] = $target_file_path; |
| 107 | + $req = HttpRequest::newRequest( $url, $opts ); |
166 | 108 | return $req->doRequest(); |
167 | 109 | } |
168 | 110 | } |
169 | 111 | |
170 | 112 | /** |
171 | | - * Start backgrounded (i.e. non blocking) request. The |
172 | | - * backgrounded request will provide updates to the user's session |
173 | | - * data. |
174 | | - * @param $url string the URL to download |
175 | | - * @param $targetFilePath string the destination for the downloaded file |
176 | | - * @param $contentLength int (optional) the length of the download from the HTTP header |
| 113 | + * a non blocking request (generally an exit point in the application) |
| 114 | + * should write to a file location and give updates |
177 | 115 | * |
178 | | - * @returns Status |
179 | 116 | */ |
180 | | - private static function startBackgroundRequest( $url, $targetFilePath, $contentLength = null ) { |
| 117 | + private static function initBackgroundDownload( $url, $target_file_path, |
| 118 | + $content_length = null ) |
| 119 | + { |
181 | 120 | global $IP, $wgPhpCli, $wgServer; |
182 | 121 | $status = Status::newGood(); |
183 | 122 | |
184 | | - // generate a session id with all the details for the download (pid, targetFilePath ) |
185 | | - $requestKey = self::createRequestKey(); |
186 | | - $sessionID = session_id(); |
| 123 | + // generate a session id with all the details for the download (pid, target_file_path ) |
| 124 | + $upload_session_key = self::getUploadSessionKey(); |
| 125 | + $session_id = session_id(); |
187 | 126 | |
188 | 127 | // store the url and target path: |
189 | | - $_SESSION['wsBgRequest'][$requestKey]['url'] = $url; |
190 | | - $_SESSION['wsBgRequest'][$requestKey]['targetFilePath'] = $targetFilePath; |
| 128 | + $_SESSION['wsDownload'][$upload_session_key]['url'] = $url; |
| 129 | + $_SESSION['wsDownload'][$upload_session_key]['target_file_path'] = $target_file_path; |
191 | 130 | // since we request from the cmd line we lose the original host name pass in the session: |
192 | | - $_SESSION['wsBgRequest'][$requestKey]['orgServer'] = $wgServer; |
| 131 | + $_SESSION['wsDownload'][$upload_session_key]['orgServer'] = $wgServer; |
193 | 132 | |
194 | | - if ( $contentLength ) { |
195 | | - $_SESSION['wsBgRequest'][$requestKey]['contentLength'] = $contentLength; |
196 | | - } |
| 133 | + if( $content_length ) |
| 134 | + $_SESSION['wsDownload'][$upload_session_key]['content_length'] = $content_length; |
197 | 135 | |
198 | 136 | // set initial loaded bytes: |
199 | | - $_SESSION['wsBgRequest'][$requestKey]['loaded'] = 0; |
| 137 | + $_SESSION['wsDownload'][$upload_session_key]['loaded'] = 0; |
200 | 138 | |
201 | 139 | // run the background download request: |
202 | | - $cmd = $wgPhpCli . ' ' . $IP . "/maintenance/httpSessionDownload.php " . |
203 | | - "--sid {$sessionID} --usk {$requestKey} --wiki " . wfWikiId(); |
| 140 | + $cmd = $wgPhpCli . ' ' . $IP . "/maintenance/http_session_download.php " . |
| 141 | + "--sid {$session_id} --usk {$upload_session_key} --wiki " . wfWikiId(); |
204 | 142 | $pid = wfShellBackgroundExec( $cmd ); |
205 | 143 | // the pid is not of much use since we won't be visiting this same apache any-time soon. |
206 | | - if ( !$pid ) |
207 | | - return Status::newFatal( 'http-could-not-background' ); |
| 144 | + if( !$pid ) |
| 145 | + return Status::newFatal( 'could not run background shell exec' ); |
208 | 146 | |
209 | | - // update the status value with the $requestKey (for the user to |
210 | | - // check on the status of the download) |
211 | | - $status->value = $requestKey; |
| 147 | + // update the status value with the $upload_session_key (for the user to |
| 148 | + // check on the status of the upload) |
| 149 | + $status->value = $upload_session_key; |
212 | 150 | |
213 | 151 | // return good status |
214 | 152 | return $status; |
215 | 153 | } |
216 | 154 | |
217 | | - /** |
218 | | - * Returns a unique, random string that can be used as an request key and |
219 | | - * preloads it into the session data. |
220 | | - * |
221 | | - * @returns string |
222 | | - */ |
223 | | - static function createRequestKey() { |
224 | | - if ( !array_key_exists( 'wsBgRequest', $_SESSION ) ) { |
225 | | - $_SESSION['wsBgRequest'] = array(); |
226 | | - } |
227 | | - |
228 | | - $key = uniqid( 'bgrequest', true ); |
229 | | - |
230 | | - // This is probably over-defensive. |
231 | | - while ( array_key_exists( $key, $_SESSION['wsBgRequest'] ) ) { |
232 | | - $key = uniqid( 'bgrequest', true ); |
233 | | - } |
234 | | - $_SESSION['wsBgRequest'][$key] = array(); |
235 | | - |
| 155 | + static function getUploadSessionKey() { |
| 156 | + $key = mt_rand( 0, 0x7fffffff ); |
| 157 | + $_SESSION['wsUploadData'][$key] = array(); |
236 | 158 | return $key; |
237 | 159 | } |
238 | 160 | |
239 | 161 | /** |
240 | | - * Recover the necessary session and request information |
241 | | - * @param $sessionID string |
242 | | - * @param $requestKey string the HTTP request key |
| 162 | + * used to run a session based download. Is initiated via the shell. |
243 | 163 | * |
244 | | - * @returns array request information |
| 164 | + * @param $session_id String: the session id to grab download details from |
| 165 | + * @param $upload_session_key String: the key of the given upload session |
| 166 | + * (a given client could have started a few http uploads at once) |
245 | 167 | */ |
246 | | - private static function recoverSession( $sessionID, $requestKey ) { |
247 | | - global $wgUser, $wgServer, $wgSessionsInMemcached; |
248 | | - |
| 168 | + public static function doSessionIdDownload( $session_id, $upload_session_key ) { |
| 169 | + global $wgUser, $wgEnableWriteAPI, $wgAsyncHTTPTimeout, $wgServer, |
| 170 | + $wgSessionsInMemcached, $wgSessionHandler, $wgSessionStarted; |
| 171 | + wfDebug( __METHOD__ . "\n\n doSessionIdDownload :\n\n" ); |
249 | 172 | // set session to the provided key: |
250 | | - session_id( $sessionID ); |
251 | | - // fire up mediaWiki session system: |
| 173 | + session_id( $session_id ); |
| 174 | + //fire up mediaWiki session system: |
252 | 175 | wfSetupSession(); |
253 | 176 | |
254 | 177 | // start the session |
255 | | - if ( session_start() === false ) { |
| 178 | + if( session_start() === false ) { |
256 | 179 | wfDebug( __METHOD__ . ' could not start session' ); |
257 | 180 | } |
258 | 181 | // get all the vars we need from session_id |
259 | | - if ( !isset( $_SESSION[ 'wsBgRequest' ][ $requestKey ] ) ) { |
260 | | - wfDebug( __METHOD__ . ' Error:could not find upload session' ); |
| 182 | + if( !isset( $_SESSION[ 'wsDownload' ][$upload_session_key] ) ) { |
| 183 | + wfDebug( __METHOD__ . ' Error:could not find upload session'); |
261 | 184 | exit(); |
262 | 185 | } |
263 | 186 | // setup the global user from the session key we just inherited |
264 | 187 | $wgUser = User::newFromSession(); |
265 | 188 | |
266 | 189 | // grab the session data to setup the request: |
267 | | - $sd =& $_SESSION['wsBgRequest'][$requestKey]; |
| 190 | + $sd =& $_SESSION['wsDownload'][$upload_session_key]; |
268 | 191 | |
269 | 192 | // update the wgServer var ( since cmd line thinks we are localhost |
270 | 193 | // when we are really orgServer) |
271 | | - if ( isset( $sd['orgServer'] ) && $sd['orgServer'] ) { |
| 194 | + if( isset( $sd['orgServer'] ) && $sd['orgServer'] ) { |
272 | 195 | $wgServer = $sd['orgServer']; |
273 | 196 | } |
274 | 197 | // close down the session so we can other http queries can get session |
275 | 198 | // updates: (if not $wgSessionsInMemcached) |
276 | | - if ( !$wgSessionsInMemcached ) { |
| 199 | + if( !$wgSessionsInMemcached ) |
277 | 200 | session_write_close(); |
278 | | - } |
279 | 201 | |
280 | | - return $sd; |
281 | | - } |
| 202 | + $req = HttpRequest::newRequest( $sd['url'], array( |
| 203 | + 'target_file_path' => $sd['target_file_path'], |
| 204 | + 'upload_session_key'=> $upload_session_key, |
| 205 | + 'timeout' => $wgAsyncHTTPTimeout, |
| 206 | + 'do_close_session_update' => true |
| 207 | + ) ); |
| 208 | + // run the actual request .. (this can take some time) |
| 209 | + wfDebug( __METHOD__ . 'do Session Download :: ' . $sd['url'] . ' tf: ' . |
| 210 | + $sd['target_file_path'] . "\n\n"); |
| 211 | + $status = $req->doRequest(); |
| 212 | + //wfDebug("done with req status is: ". $status->isOK(). ' '.$status->getWikiText(). "\n"); |
282 | 213 | |
283 | | - /** |
284 | | - * Update the session with the finished information. |
285 | | - * @param $sessionID string |
286 | | - * @param $requestKey string the HTTP request key |
287 | | - */ |
288 | | - private static function updateSession( $sessionID, $requestKey, $status ) { |
289 | | - |
290 | | - if ( session_start() === false ) { |
291 | | - wfDebug( __METHOD__ . ' ERROR:: Could not start session' ); |
| 214 | + // start up the session again: |
| 215 | + if( session_start() === false ) { |
| 216 | + wfDebug( __METHOD__ . ' ERROR:: Could not start session'); |
292 | 217 | } |
293 | | - |
294 | | - $sd =& $_SESSION['wsBgRequest'][$requestKey]; |
295 | | - if ( !$status->isOK() ) { |
| 218 | + // grab the updated session data pointer |
| 219 | + $sd =& $_SESSION['wsDownload'][$upload_session_key]; |
| 220 | + // if error update status: |
| 221 | + if( !$status->isOK() ) { |
296 | 222 | $sd['apiUploadResult'] = FormatJson::encode( |
297 | 223 | array( 'error' => $status->getWikiText() ) |
298 | 224 | ); |
299 | | - } else { |
300 | | - $sd['apiUploadResult'] = FormatJson::encode( $status->value ); |
301 | 225 | } |
302 | | - |
303 | | - session_write_close(); |
304 | | - } |
305 | | - |
306 | | - /** |
307 | | - * Take care of the downloaded file |
308 | | - * @param $sd array |
309 | | - * @param $status Status |
310 | | - * |
311 | | - * @returns Status |
312 | | - */ |
313 | | - private static function doFauxRequest( $sd, $status ) { |
314 | | - global $wgEnableWriteAPI; |
315 | | - |
316 | | - if ( $status->isOK() ) { |
| 226 | + // if status okay process upload using fauxReq to api: |
| 227 | + if( $status->isOK() ){ |
| 228 | + // setup the FauxRequest |
317 | 229 | $fauxReqData = $sd['mParams']; |
318 | 230 | |
319 | 231 | // Fix boolean parameters |
320 | | - foreach ( $fauxReqData as $k => $v ) { |
321 | | - if ( $v === false ) |
| 232 | + foreach( $fauxReqData as $k => $v ) { |
| 233 | + if( $v === false ) |
322 | 234 | unset( $fauxReqData[$k] ); |
323 | 235 | } |
324 | 236 | |
325 | 237 | $fauxReqData['action'] = 'upload'; |
326 | 238 | $fauxReqData['format'] = 'json'; |
327 | | - $fauxReqData['internalhttpsession'] = $requestKey; |
328 | | - |
| 239 | + $fauxReqData['internalhttpsession'] = $upload_session_key; |
329 | 240 | // evil but no other clean way about it: |
330 | | - $fauxReq = new FauxRequest( $fauxReqData, true ); |
331 | | - $processor = new ApiMain( $fauxReq, $wgEnableWriteAPI ); |
| 241 | + $faxReq = new FauxRequest( $fauxReqData, true ); |
| 242 | + $processor = new ApiMain( $faxReq, $wgEnableWriteAPI ); |
332 | 243 | |
333 | | - // init the mUpload var for the $processor |
| 244 | + //init the mUpload var for the $processor |
334 | 245 | $processor->execute(); |
335 | 246 | $processor->getResult()->cleanUpUTF8(); |
336 | 247 | $printer = $processor->createPrinterByName( 'json' ); |
337 | 248 | $printer->initPrinter( false ); |
338 | 249 | ob_start(); |
339 | 250 | $printer->execute(); |
| 251 | + $apiUploadResult = ob_get_clean(); |
340 | 252 | |
341 | 253 | // the status updates runner will grab the result form the session: |
342 | | - $status->value = ob_get_clean(); |
| 254 | + $sd['apiUploadResult'] = $apiUploadResult; |
343 | 255 | } |
344 | | - return $status; |
| 256 | + // close the session: |
| 257 | + session_write_close(); |
345 | 258 | } |
346 | 259 | |
347 | 260 | /** |
348 | | - * Run a session based download. |
349 | | - * |
350 | | - * @param $sessionID string: the session id with the download details |
351 | | - * @param $requestKey string: the key of the given upload session |
352 | | - * (a given client could have started a few http uploads at once) |
| 261 | + * Check if the URL can be served by localhost |
| 262 | + * @param $url string Full url to check |
| 263 | + * @return bool |
353 | 264 | */ |
354 | | - public static function doSessionIdDownload( $sessionID, $requestKey ) { |
355 | | - global $wgAsyncHTTPTimeout; |
| 265 | + public static function isLocalURL( $url ) { |
| 266 | + global $wgCommandLineMode, $wgConf; |
| 267 | + if ( $wgCommandLineMode ) { |
| 268 | + return false; |
| 269 | + } |
356 | 270 | |
357 | | - wfDebug( __METHOD__ . "\n\n doSessionIdDownload :\n\n" ); |
358 | | - $sd = self::recoverSession( $sessionID ); |
359 | | - $req = HttpRequest::factory( $sd['url'], |
360 | | - array( |
361 | | - 'targetFilePath' => $sd['targetFilePath'], |
362 | | - 'requestKey' => $requestKey, |
363 | | - 'timeout' => $wgAsyncHTTPTimeout, |
364 | | - ) ); |
| 271 | + // Extract host part |
| 272 | + $matches = array(); |
| 273 | + if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) { |
| 274 | + $host = $matches[1]; |
| 275 | + // Split up dotwise |
| 276 | + $domainParts = explode( '.', $host ); |
| 277 | + // Check if this domain or any superdomain is listed in $wgConf as a local virtual host |
| 278 | + $domainParts = array_reverse( $domainParts ); |
| 279 | + for ( $i = 0; $i < count( $domainParts ); $i++ ) { |
| 280 | + $domainPart = $domainParts[$i]; |
| 281 | + if ( $i == 0 ) { |
| 282 | + $domain = $domainPart; |
| 283 | + } else { |
| 284 | + $domain = $domainPart . '.' . $domain; |
| 285 | + } |
| 286 | + if ( $wgConf->isLocalVHost( $domain ) ) { |
| 287 | + return true; |
| 288 | + } |
| 289 | + } |
| 290 | + } |
| 291 | + return false; |
| 292 | + } |
365 | 293 | |
366 | | - // run the actual request .. (this can take some time) |
367 | | - wfDebug( __METHOD__ . 'do Session Download :: ' . $sd['url'] . ' tf: ' . |
368 | | - $sd['targetFilePath'] . "\n\n" ); |
369 | | - $status = $req->doRequest(); |
| 294 | + /** |
| 295 | + * Return a standard user-agent we can use for external requests. |
| 296 | + */ |
| 297 | + public static function userAgent() { |
| 298 | + global $wgVersion; |
| 299 | + return "MediaWiki/$wgVersion"; |
| 300 | + } |
370 | 301 | |
371 | | - self::updateSession( $sessionID, $requestKey, |
372 | | - self::handleFauxResponse( $sd, $status ) ); |
| 302 | + /** |
| 303 | + * Checks that the given URI is a valid one |
| 304 | + * @param $uri Mixed: URI to check for validity |
| 305 | + */ |
| 306 | + public static function isValidURI( $uri ){ |
| 307 | + return preg_match( |
| 308 | + '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/', |
| 309 | + $uri, |
| 310 | + $matches |
| 311 | + ); |
373 | 312 | } |
374 | 313 | } |
375 | 314 | |
376 | | -/** |
377 | | - * This wrapper class will call out to curl (if available) or fallback |
378 | | - * to regular PHP if necessary for handling internal HTTP requests. |
379 | | - */ |
380 | 315 | class HttpRequest { |
381 | | - private $targetFilePath; |
382 | | - private $requestKey; |
383 | | - protected $content; |
384 | | - protected $timeout = 'default'; |
385 | | - protected $headersOnly = null; |
386 | | - protected $postdata = null; |
387 | | - protected $proxy = null; |
388 | | - protected $no_proxy = false; |
389 | | - protected $sslVerifyHost = true; |
390 | | - protected $caInfo = null; |
391 | | - protected $method = "GET"; |
392 | | - protected $url; |
393 | | - public $status; |
| 316 | + var $target_file_path; |
| 317 | + var $upload_session_key; |
| 318 | + function __construct( $url, $opt ){ |
394 | 319 | |
395 | | - /** |
396 | | - * @param $url string url to use |
397 | | - * @param $options array (optional) extra params to pass |
398 | | - * Possible keys for the array: |
399 | | - * method |
400 | | - * timeout |
401 | | - * targetFilePath |
402 | | - * requestKey |
403 | | - * headersOnly |
404 | | - * postdata |
405 | | - * proxy |
406 | | - * no_proxy |
407 | | - * sslVerifyHost |
408 | | - * caInfo |
409 | | - */ |
410 | | - function __construct( $url = null, $opt ) { |
411 | | - global $wgHTTPTimeout; |
412 | | - |
| 320 | + global $wgSyncHTTPTimeout; |
413 | 321 | $this->url = $url; |
414 | | - |
415 | | - if ( !ini_get( 'allow_url_fopen' ) ) { |
416 | | - $this->status = Status::newFatal( 'allow_url_fopen needs to be enabled for http copy to work' ); |
417 | | - } elseif ( !Http::isValidURI( $this->url ) ) { |
418 | | - $this->status = Status::newFatal( 'bad-url' ); |
419 | | - } else { |
420 | | - $this->status = Status::newGood( 100 ); // continue |
| 322 | + // set the timeout to default sync timeout (unless the timeout option is provided) |
| 323 | + $this->timeout = ( isset( $opt['timeout'] ) ) ? $opt['timeout'] : $wgSyncHTTPTimeout; |
| 324 | + //check special key default |
| 325 | + if($this->timeout == 'default'){ |
| 326 | + $opts['timeout'] = $wgSyncHTTPTimeout; |
421 | 327 | } |
422 | 328 | |
423 | | - if ( array_key_exists( 'timeout', $opt ) && $opt['timeout'] != 'default' ) { |
424 | | - $this->timeout = $opt['timeout']; |
425 | | - } else { |
426 | | - $this->timeout = $wgHTTPTimeout; |
427 | | - } |
| 329 | + $this->method = ( isset( $opt['method'] ) ) ? $opt['method'] : 'GET'; |
| 330 | + $this->target_file_path = ( isset( $opt['target_file_path'] ) ) |
| 331 | + ? $opt['target_file_path'] : false; |
| 332 | + $this->upload_session_key = ( isset( $opt['upload_session_key'] ) ) |
| 333 | + ? $opt['upload_session_key'] : false; |
| 334 | + $this->headers_only = ( isset( $opt['headers_only'] ) ) ? $opt['headers_only'] : false; |
| 335 | + $this->do_close_session_update = isset( $opt['do_close_session_update'] ); |
| 336 | + $this->postData = isset( $opt['postdata'] ) ? $opt['postdata'] : ''; |
428 | 337 | |
429 | | - $members = array( "targetFilePath", "requestKey", "headersOnly", "postdata", |
430 | | - "proxy", "no_proxy", "sslVerifyHost", "caInfo", "method" ); |
431 | | - foreach ( $members as $o ) { |
432 | | - if ( array_key_exists( $o, $opt ) ) { |
433 | | - $this->$o = $opt[$o]; |
434 | | - } |
435 | | - } |
| 338 | + $this->proxy = isset( $opt['proxy'] )? $opt['proxy'] : ''; |
436 | 339 | |
437 | | - if ( is_array( $this->postdata ) ) { |
438 | | - $this->postdata = wfArrayToCGI( $this->postdata ); |
439 | | - } |
440 | | - } |
| 340 | + $this->ssl_verifyhost = (isset( $opt['ssl_verifyhost'] ))? $opt['ssl_verifyhost']: false; |
441 | 341 | |
442 | | - /** |
443 | | - * For backwards compatibility, we provide a __toString method so |
444 | | - * that any code that expects a string result from Http::Get() |
445 | | - * will see the content of the request. |
446 | | - */ |
447 | | - function __toString() { |
448 | | - return $this->content; |
| 342 | + $this->cainfo = (isset( $opt['cainfo'] ))? $op['cainfo']: false; |
| 343 | + |
449 | 344 | } |
450 | 345 | |
451 | | - /** |
452 | | - * Generate a new request object |
453 | | - * @see HttpRequest::__construct |
454 | | - */ |
455 | | - public static function factory( $url, $opt ) { |
456 | | - global $wgForceHTTPEngine; |
457 | | - |
458 | | - if ( function_exists( 'curl_init' ) && $wgForceHTTPEngine == "curl" ) { |
459 | | - return new CurlHttpRequest( $url, $opt ); |
| 346 | + public static function newRequest($url, $opt){ |
| 347 | + # select the handler (use curl if available) |
| 348 | + if ( function_exists( 'curl_init' ) ) { |
| 349 | + return new curlHttpRequest($url, $opt); |
460 | 350 | } else { |
461 | | - return new PhpHttpRequest( $url, $opt ); |
| 351 | + return new phpHttpRequest($url, $opt); |
462 | 352 | } |
463 | 353 | } |
464 | 354 | |
465 | | - public function getContent() { |
466 | | - return $this->content; |
| 355 | + /** |
| 356 | + * Get the contents of a file by HTTP |
| 357 | + * @param $url string Full URL to act on |
| 358 | + * @param $Opt associative array Optional array of options: |
| 359 | + * 'method' => 'GET', 'POST' etc. |
| 360 | + * 'target_file_path' => if curl should output to a target file |
| 361 | + * 'adapter' => 'curl', 'soket' |
| 362 | + */ |
| 363 | + public function doRequest() { |
| 364 | + # Make sure we have a valid url |
| 365 | + if( !Http::isValidURI( $this->url ) ) |
| 366 | + return Status::newFatal('bad-url'); |
| 367 | + //do the actual request: |
| 368 | + return $this->doReq(); |
467 | 369 | } |
| 370 | +} |
| 371 | +class curlHttpRequest extends HttpRequest { |
| 372 | + public function doReq(){ |
| 373 | + global $wgHTTPProxy, $wgTitle; |
468 | 374 | |
469 | | - public function handleOutput() { |
470 | | - // if we wrote to a target file close up or return error |
471 | | - if ( $this->targetFilePath ) { |
472 | | - $this->writer->close(); |
473 | | - if ( !$this->writer->status->isOK() ) { |
474 | | - $this->status = $this->writer->status; |
475 | | - return $this->status; |
| 375 | + $status = Status::newGood(); |
| 376 | + $c = curl_init( $this->url ); |
| 377 | + |
| 378 | + // only do proxy setup if ( not suppressed $this->proxy === false ) |
| 379 | + if( $this->proxy !== false ){ |
| 380 | + if( $this->proxy ){ |
| 381 | + curl_setopt( $c, CURLOPT_PROXY, $this->proxy ); |
| 382 | + } else if ( Http::isLocalURL( $this->url ) ) { |
| 383 | + curl_setopt( $c, CURLOPT_PROXY, 'localhost:80' ); |
| 384 | + } else if ( $wgHTTPProxy ) { |
| 385 | + curl_setopt( $c, CURLOPT_PROXY, $wgHTTPProxy ); |
476 | 386 | } |
477 | 387 | } |
478 | | - } |
479 | 388 | |
480 | | - public function doRequest() { |
481 | | - global $wgTitle; |
| 389 | + curl_setopt( $c, CURLOPT_TIMEOUT, $this->timeout ); |
| 390 | + curl_setopt( $c, CURLOPT_USERAGENT, Http::userAgent() ); |
482 | 391 | |
483 | | - if ( !$this->status->isOK() ) { |
484 | | - return $this->status; |
485 | | - } |
| 392 | + if( $this->ssl_verifyhost ) |
| 393 | + curl_setopt( $c, CURLOPT_SSL_VERIFYHOST, $this->ssl_verifyhost); |
486 | 394 | |
487 | | - $this->initRequest(); |
| 395 | + if( $this->cainfo ) |
| 396 | + curl_setopt( $c, CURLOPT_CAINFO, $this->cainfo); |
488 | 397 | |
489 | | - if ( !$this->no_proxy ) { |
490 | | - $this->proxySetup(); |
| 398 | + if ( $this->headers_only ) { |
| 399 | + curl_setopt( $c, CURLOPT_NOBODY, true ); |
| 400 | + curl_setopt( $c, CURLOPT_HEADER, true ); |
| 401 | + } elseif ( $this->method == 'POST' ) { |
| 402 | + curl_setopt( $c, CURLOPT_POST, true ); |
| 403 | + curl_setopt( $c, CURLOPT_POSTFIELDS, $this->postData ); |
| 404 | + // Suppress 'Expect: 100-continue' header, as some servers |
| 405 | + // will reject it with a 417 and Curl won't auto retry |
| 406 | + // with HTTP 1.0 fallback |
| 407 | + curl_setopt( $c, CURLOPT_HTTPHEADER, array( 'Expect:' ) ); |
| 408 | + } else { |
| 409 | + curl_setopt( $c, CURLOPT_CUSTOMREQUEST, $this->method ); |
491 | 410 | } |
492 | 411 | |
493 | 412 | # Set the referer to $wgTitle, even in command-line mode |
— | — | @@ -495,335 +414,268 @@ |
496 | 415 | # $_SERVER['REQUEST_URI'] gives a less reliable indication of the |
497 | 416 | # referring page. |
498 | 417 | if ( is_object( $wgTitle ) ) { |
499 | | - $this->setReferrer( $wgTitle->getFullURL() ); |
| 418 | + curl_setopt( $c, CURLOPT_REFERER, $wgTitle->getFullURL() ); |
500 | 419 | } |
501 | 420 | |
502 | | - $this->setupOutputHandler(); |
503 | | - |
504 | | - if ( $this->status->isOK() ) { |
505 | | - $this->spinTheWheel(); |
| 421 | + // set the write back function (if we are writing to a file) |
| 422 | + if( $this->target_file_path ) { |
| 423 | + $cwrite = new simpleFileWriter( $this->target_file_path, |
| 424 | + $this->upload_session_key, |
| 425 | + $this->do_close_session_update |
| 426 | + ); |
| 427 | + if( !$cwrite->status->isOK() ) { |
| 428 | + wfDebug( __METHOD__ . "ERROR in setting up simpleFileWriter\n" ); |
| 429 | + $status = $cwrite->status; |
| 430 | + return $status; |
| 431 | + } |
| 432 | + curl_setopt( $c, CURLOPT_WRITEFUNCTION, array( $cwrite, 'callbackWriteBody' ) ); |
506 | 433 | } |
507 | 434 | |
508 | | - if ( !$this->status->isOK() ) { |
509 | | - return $this->status; |
510 | | - } |
| 435 | + // start output grabber: |
| 436 | + if( !$this->target_file_path ) |
| 437 | + ob_start(); |
511 | 438 | |
512 | | - $this->handleOutput(); |
513 | | - |
514 | | - $this->finish(); |
515 | | - return $this->status; |
516 | | - } |
517 | | - |
518 | | - public function setupOutputHandler() { |
519 | | - if ( $this->targetFilePath ) { |
520 | | - $this->writer = new SimpleFileWriter( $this->targetFilePath, |
521 | | - $this->requestKey ); |
522 | | - if ( !$this->writer->status->isOK() ) { |
523 | | - wfDebug( __METHOD__ . "ERROR in setting up SimpleFileWriter\n" ); |
524 | | - $this->status = $this->writer->status; |
525 | | - return $this->status; |
| 439 | + //run the actual curl_exec: |
| 440 | + try { |
| 441 | + if ( false === curl_exec( $c ) ) { |
| 442 | + $error_txt ='Error sending request: #' . curl_errno( $c ) .' '. curl_error( $c ); |
| 443 | + wfDebug( __METHOD__ . $error_txt . "\n" ); |
| 444 | + $status = Status::newFatal( $error_txt ); |
526 | 445 | } |
527 | | - $this->setCallback( array( $this, 'readAndSave' ) ); |
528 | | - } else { |
529 | | - $this->setCallback( array( $this, 'readOnly' ) ); |
| 446 | + } catch ( Exception $e ) { |
| 447 | + // do something with curl exec error? |
530 | 448 | } |
531 | | - } |
532 | | - |
533 | | - public function setReferrer( $url ) { } |
534 | | - |
535 | | -} |
536 | | - |
537 | | -/** |
538 | | - * HttpRequest implemented using internal curl compiled into PHP |
539 | | - */ |
540 | | -class CurlHttpRequest extends HttpRequest { |
541 | | - private $c; |
542 | | - |
543 | | - public function initRequest() { |
544 | | - $this->c = curl_init( $this->url ); |
545 | | - } |
546 | | - |
547 | | - public function proxySetup() { |
548 | | - global $wgHTTPProxy; |
549 | | - |
550 | | - if ( is_string( $this->proxy ) ) { |
551 | | - curl_setopt( $this->c, CURLOPT_PROXY, $this->proxy ); |
552 | | - } else if ( Http::isLocalURL( $this->url ) ) { /* Not sure this makes any sense. */ |
553 | | - curl_setopt( $this->c, CURLOPT_PROXY, 'localhost:80' ); |
554 | | - } else if ( $wgHTTPProxy ) { |
555 | | - curl_setopt( $this->c, CURLOPT_PROXY, $wgHTTPProxy ); |
| 449 | + // if direct request output the results to the stats value: |
| 450 | + if( !$this->target_file_path && $status->isOK() ) { |
| 451 | + $status->value = ob_get_contents(); |
| 452 | + ob_end_clean(); |
556 | 453 | } |
557 | | - } |
558 | | - |
559 | | - public function setCallback( $cb ) { |
560 | | - curl_setopt( $this->c, CURLOPT_WRITEFUNCTION, $cb ); |
561 | | - } |
562 | | - |
563 | | - public function spinTheWheel() { |
564 | | - curl_setopt( $this->c, CURLOPT_TIMEOUT, $this->timeout ); |
565 | | - curl_setopt( $this->c, CURLOPT_USERAGENT, Http::userAgent() ); |
566 | | - curl_setopt( $this->c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 ); |
567 | | - |
568 | | - if ( $this->sslVerifyHost ) { |
569 | | - curl_setopt( $this->c, CURLOPT_SSL_VERIFYHOST, $this->sslVerifyHost ); |
| 454 | + // if we wrote to a target file close up or return error |
| 455 | + if( $this->target_file_path ) { |
| 456 | + $cwrite->close(); |
| 457 | + if( !$cwrite->status->isOK() ) { |
| 458 | + return $cwrite->status; |
| 459 | + } |
570 | 460 | } |
571 | 461 | |
572 | | - if ( $this->caInfo ) { |
573 | | - curl_setopt( $this->c, CURLOPT_CAINFO, $this->caInfo ); |
574 | | - } |
575 | | - |
576 | | - if ( $this->headersOnly ) { |
577 | | - curl_setopt( $this->c, CURLOPT_NOBODY, true ); |
578 | | - curl_setopt( $this->c, CURLOPT_HEADER, true ); |
579 | | - } elseif ( $this->method == 'POST' ) { |
580 | | - curl_setopt( $this->c, CURLOPT_POST, true ); |
581 | | - curl_setopt( $this->c, CURLOPT_POSTFIELDS, $this->postdata ); |
582 | | - // Suppress 'Expect: 100-continue' header, as some servers |
583 | | - // will reject it with a 417 and Curl won't auto retry |
584 | | - // with HTTP 1.0 fallback |
585 | | - curl_setopt( $this->c, CURLOPT_HTTPHEADER, array( 'Expect:' ) ); |
| 462 | + if ( $this->headers_only ) { |
| 463 | + $headers = explode( "\n", $status->value ); |
| 464 | + $headerArray = array(); |
| 465 | + foreach ( $headers as $header ) { |
| 466 | + if ( !strlen( trim( $header ) ) ) |
| 467 | + continue; |
| 468 | + $headerParts = explode( ':', $header, 2 ); |
| 469 | + if ( count( $headerParts ) == 1 ) { |
| 470 | + $headerArray[] = trim( $header ); |
| 471 | + } else { |
| 472 | + list( $key, $val ) = $headerParts; |
| 473 | + $headerArray[trim( $key )] = trim( $val ); |
| 474 | + } |
| 475 | + } |
| 476 | + $status->value = $headerArray; |
586 | 477 | } else { |
587 | | - curl_setopt( $this->c, CURLOPT_CUSTOMREQUEST, $this->method ); |
588 | | - } |
589 | | - |
590 | | - try { |
591 | | - if ( false === curl_exec( $this->c ) ) { |
592 | | - $error_txt = 'Error sending request: #' . curl_errno( $this->c ) . ' ' . curl_error( $this->c ); |
593 | | - wfDebug( __METHOD__ . $error_txt . "\n" ); |
594 | | - $this->status->fatal( $error_txt ); /* i18n? */ |
| 478 | + # Don't return the text of error messages, return false on error |
| 479 | + $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE ); |
| 480 | + if ( $retcode != 200 ) { |
| 481 | + wfDebug( __METHOD__ . ": HTTP return code $retcode\n" ); |
| 482 | + $status = Status::newFatal( "HTTP return code $retcode\n" ); |
595 | 483 | } |
596 | | - } catch ( Exception $e ) { |
597 | | - $errno = curl_errno( $this->c ); |
| 484 | + # Don't return truncated output |
| 485 | + $errno = curl_errno( $c ); |
598 | 486 | if ( $errno != CURLE_OK ) { |
599 | | - $errstr = curl_error( $this->c ); |
| 487 | + $errstr = curl_error( $c ); |
600 | 488 | wfDebug( __METHOD__ . ": CURL error code $errno: $errstr\n" ); |
601 | | - $this->status->fatal( "CURL error code $errno: $errstr\n" ); /* i18n? */ |
| 489 | + $status = Status::newFatal( " CURL error code $errno: $errstr\n" ); |
602 | 490 | } |
603 | 491 | } |
604 | | - } |
605 | 492 | |
606 | | - public function readOnly( $curlH, $content ) { |
607 | | - $this->content .= $content; |
608 | | - return strlen( $content ); |
| 493 | + curl_close( $c ); |
| 494 | + // return the result obj |
| 495 | + return $status; |
609 | 496 | } |
| 497 | +} |
| 498 | +class phpHttpRequest extends HttpRequest { |
| 499 | + public function doReq() { |
| 500 | + global $wgTitle, $wgHTTPProxy; |
| 501 | + # Check for php.ini allow_url_fopen |
| 502 | + if( !ini_get( 'allow_url_fopen' ) ) { |
| 503 | + return Status::newFatal( 'allow_url_fopen needs to be enabled for http copy to work' ); |
| 504 | + } |
610 | 505 | |
611 | | - public function readAndSave( $curlH, $content ) { |
612 | | - return $this->writer->write( $content ); |
613 | | - } |
| 506 | + // start with good status: |
| 507 | + $status = Status::newGood(); |
614 | 508 | |
615 | | - public function getCode() { |
616 | | - # Don't return truncated output |
617 | | - $code = curl_getinfo( $this->c, CURLINFO_HTTP_CODE ); |
618 | | - if ( $code < 400 ) { |
619 | | - $this->status->setResult( true, $code ); |
620 | | - } else { |
621 | | - $this->status->setResult( false, $code ); |
| 509 | + if ( $this->headers_only ) { |
| 510 | + $status->value = get_headers( $this->url, 1 ); |
| 511 | + return $status; |
622 | 512 | } |
623 | | - } |
624 | 513 | |
625 | | - public function finish() { |
626 | | - curl_close( $this->c ); |
627 | | - } |
| 514 | + // setup the headers |
| 515 | + $headers = array( "User-Agent: " . Http::userAgent() ); |
| 516 | + if ( is_object( $wgTitle ) ) { |
| 517 | + $headers[] = "Referer: ". $wgTitle->getFullURL(); |
| 518 | + } |
628 | 519 | |
629 | | -} |
630 | | - |
631 | | -class PhpHttpRequest extends HttpRequest { |
632 | | - private $reqHeaders; |
633 | | - private $callback; |
634 | | - private $fh; |
635 | | - |
636 | | - public function initRequest() { |
637 | | - $this->reqHeaders[] = "User-Agent: " . Http::userAgent(); |
638 | | - $this->reqHeaders[] = "Accept: */*"; |
639 | | - if ( $this->method == 'POST' ) { |
| 520 | + if( strcasecmp( $this->method, 'post' ) == 0 ) { |
640 | 521 | // Required for HTTP 1.0 POSTs |
641 | | - $this->reqHeaders[] = "Content-Length: " . strlen( $this->postdata ); |
642 | | - $this->reqHeaders[] = "Content-type: application/x-www-form-urlencoded"; |
| 522 | + $headers[] = "Content-Length: 0"; |
643 | 523 | } |
644 | | - } |
645 | 524 | |
646 | | - public function proxySetup() { |
647 | | - global $wgHTTPProxy; |
| 525 | + $httpContextOptions = array( |
| 526 | + 'method' => $this->method, |
| 527 | + 'header' => implode( "\r\n", $headers ), |
| 528 | + 'timeout' => $this->timeout |
| 529 | + ); |
648 | 530 | |
649 | | - if ( $this->proxy ) { |
650 | | - $this->proxy = 'tcp://' . $this->proxy; |
651 | | - } elseif ( Http::isLocalURL( $this->url ) ) { |
652 | | - $this->proxy = 'tcp://localhost:80'; |
| 531 | + // Proxy setup: |
| 532 | + if( $this->proxy ){ |
| 533 | + $httpContextOptions['proxy'] = 'tcp://' . $this->proxy; |
| 534 | + }else if ( Http::isLocalURL( $this->url ) ) { |
| 535 | + $httpContextOptions['proxy'] = 'tcp://localhost:80'; |
653 | 536 | } elseif ( $wgHTTPProxy ) { |
654 | | - $this->proxy = 'tcp://' . $wgHTTPProxy ; |
| 537 | + $httpContextOptions['proxy'] = 'tcp://' . $wgHTTPProxy ; |
655 | 538 | } |
656 | | - } |
657 | 539 | |
658 | | - public function setReferrer( $url ) { |
659 | | - $this->reqHeaders[] = "Referer: $url"; |
660 | | - } |
| 540 | + $fcontext = stream_context_create ( |
| 541 | + array( |
| 542 | + 'http' => $httpContextOptions |
| 543 | + ) |
| 544 | + ); |
661 | 545 | |
662 | | - public function setCallback( $cb ) { |
663 | | - $this->callback = $cb; |
664 | | - } |
| 546 | + $fh = fopen( $this->url, "r", false, $fcontext); |
665 | 547 | |
666 | | - public function readOnly( $contents ) { |
667 | | - if ( $this->headersOnly ) { |
668 | | - return false; |
669 | | - } |
670 | | - $this->content .= $contents; |
| 548 | + // set the write back function (if we are writing to a file) |
| 549 | + if( $this->target_file_path ) { |
| 550 | + $cwrite = new simpleFileWriter( $this->target_file_path, |
| 551 | + $this->upload_session_key, $this->do_close_session_update ); |
| 552 | + if( !$cwrite->status->isOK() ) { |
| 553 | + wfDebug( __METHOD__ . "ERROR in setting up simpleFileWriter\n" ); |
| 554 | + $status = $cwrite->status; |
| 555 | + return $status; |
| 556 | + } |
671 | 557 | |
672 | | - return strlen( $contents ); |
673 | | - } |
674 | | - |
675 | | - public function readAndSave( $contents ) { |
676 | | - if ( $this->headersOnly ) { |
677 | | - return false; |
678 | | - } |
679 | | - return $this->writer->write( $content ); |
680 | | - } |
681 | | - |
682 | | - public function finish() { |
683 | | - fclose( $this->fh ); |
684 | | - } |
685 | | - |
686 | | - public function spinTheWheel() { |
687 | | - $opts = array(); |
688 | | - if ( $this->proxy && !$this->no_proxy ) { |
689 | | - $opts['proxy'] = $this->proxy; |
690 | | - $opts['request_fulluri'] = true; |
691 | | - } |
692 | | - |
693 | | - $opts['method'] = $this->method; |
694 | | - $opts['timeout'] = $this->timeout; |
695 | | - $opts['header'] = implode( "\r\n", $this->reqHeaders ); |
696 | | - if ( version_compare( "5.3.0", phpversion(), ">" ) ) { |
697 | | - $opts['protocol_version'] = "1.0"; |
| 558 | + // Read $fh into the simpleFileWriter (grab in 64K chunks since |
| 559 | + // it's likely a ~large~ media file) |
| 560 | + while ( !feof( $fh ) ) { |
| 561 | + $contents = fread( $fh, 65536 ); |
| 562 | + $cwrite->callbackWriteBody( $fh, $contents ); |
| 563 | + } |
| 564 | + $cwrite->close(); |
| 565 | + // check for simpleFileWriter error: |
| 566 | + if( !$cwrite->status->isOK() ) { |
| 567 | + return $cwrite->status; |
| 568 | + } |
698 | 569 | } else { |
699 | | - $opts['protocol_version'] = "1.1"; |
| 570 | + // read $fh into status->value |
| 571 | + $status->value = @stream_get_contents( $fh ); |
700 | 572 | } |
| 573 | + //close the url file wrapper |
| 574 | + fclose( $fh ); |
701 | 575 | |
702 | | - if ( $this->postdata ) { |
703 | | - $opts['content'] = $this->postdata; |
| 576 | + // check for "false" |
| 577 | + if( $status->value === false ) { |
| 578 | + $status->error( 'file_get_contents-failed' ); |
704 | 579 | } |
705 | | - |
706 | | - $context = stream_context_create( array( 'http' => $opts ) ); |
707 | | - $this->fh = fopen( $this->url, "r", false, $context ); |
708 | | - $result = stream_get_meta_data( $this->fh ); |
709 | | - |
710 | | - if ( $result['timed_out'] ) { |
711 | | - $this->status->error( __CLASS__ . '::timed-out-in-headers' ); |
712 | | - } |
713 | | - |
714 | | - $this->headers = $result['wrapper_data']; |
715 | | - |
716 | | - $end = false; |
717 | | - $size = 8192; |
718 | | - while ( !$end ) { |
719 | | - $contents = fread( $this->fh, $size ); |
720 | | - $size = call_user_func( $this->callback, $contents ); |
721 | | - $end = ( $size == 0 ) || feof( $this->fh ); |
722 | | - } |
| 580 | + return $status; |
723 | 581 | } |
| 582 | + |
724 | 583 | } |
725 | 584 | |
726 | 585 | /** |
727 | 586 | * SimpleFileWriter with session id updates |
728 | 587 | */ |
729 | | -class SimpleFileWriter { |
730 | | - private $targetFilePath = null; |
731 | | - private $status = null; |
732 | | - private $sessionId = null; |
733 | | - private $sessionUpdateInterval = 0; // how often to update the session while downloading |
734 | | - private $currentFileSize = 0; |
735 | | - private $requestKey = null; |
736 | | - private $prevTime = 0; |
737 | | - private $fp = null; |
| 588 | +class simpleFileWriter { |
| 589 | + var $target_file_path; |
| 590 | + var $status = null; |
| 591 | + var $session_id = null; |
| 592 | + var $session_update_interval = 0; // how often to update the session while downloading |
738 | 593 | |
739 | | - /** |
740 | | - * @param $targetFilePath string the path to write the file out to |
741 | | - * @param $requestKey string the request to update |
742 | | - */ |
743 | | - function __construct__( $targetFilePath, $requestKey ) { |
744 | | - $this->targetFilePath = $targetFilePath; |
745 | | - $this->requestKey = $requestKey; |
| 594 | + function simpleFileWriter( $target_file_path, $upload_session_key, |
| 595 | + $do_close_session_update = false ) |
| 596 | + { |
| 597 | + $this->target_file_path = $target_file_path; |
| 598 | + $this->upload_session_key = $upload_session_key; |
746 | 599 | $this->status = Status::newGood(); |
| 600 | + $this->do_close_session_update = $do_close_session_update; |
747 | 601 | // open the file: |
748 | | - $this->fp = fopen( $this->targetFilePath, 'w' ); |
749 | | - if ( $this->fp === false ) { |
| 602 | + $this->fp = fopen( $this->target_file_path, 'w' ); |
| 603 | + if( $this->fp === false ) { |
750 | 604 | $this->status = Status::newFatal( 'HTTP::could-not-open-file-for-writing' ); |
751 | 605 | } |
752 | 606 | // true start time |
753 | 607 | $this->prevTime = time(); |
754 | 608 | } |
755 | 609 | |
756 | | - public function write( $dataPacket ) { |
| 610 | + public function callbackWriteBody( $ch, $data_packet ) { |
757 | 611 | global $wgMaxUploadSize, $wgLang; |
758 | 612 | |
759 | | - if ( !$this->status->isOK() ) { |
760 | | - return false; |
761 | | - } |
762 | | - |
763 | 613 | // write out the content |
764 | | - if ( fwrite( $this->fp, $dataPacket ) === false ) { |
765 | | - wfDebug( __METHOD__ . " ::could-not-write-to-file\n" ); |
| 614 | + if( fwrite( $this->fp, $data_packet ) === false ) { |
| 615 | + wfDebug( __METHOD__ ." ::could-not-write-to-file\n" ); |
766 | 616 | $this->status = Status::newFatal( 'HTTP::could-not-write-to-file' ); |
767 | | - return false; |
| 617 | + return 0; |
768 | 618 | } |
769 | 619 | |
770 | 620 | // check file size: |
771 | 621 | clearstatcache(); |
772 | | - $this->currentFileSize = filesize( $this->targetFilePath ); |
| 622 | + $this->current_fsize = filesize( $this->target_file_path ); |
773 | 623 | |
774 | | - if ( $this->currentFileSize > $wgMaxUploadSize ) { |
775 | | - wfDebug( __METHOD__ . " ::http-download-too-large\n" ); |
776 | | - $this->status = Status::newFatal( 'HTTP::file-has-grown-beyond-upload-limit-killing: ' . /* i18n? */ |
777 | | - 'downloaded more than ' . |
778 | | - $wgLang->formatSize( $wgMaxUploadSize ) . ' ' ); |
779 | | - return false; |
| 624 | + if( $this->current_fsize > $wgMaxUploadSize ) { |
| 625 | + wfDebug( __METHOD__ . " ::http download too large\n" ); |
| 626 | + $this->status = Status::newFatal( 'HTTP::file-has-grown-beyond-upload-limit-killing: ' . |
| 627 | + 'downloaded more than ' . |
| 628 | + $wgLang->formatSize( $wgMaxUploadSize ) . ' ' ); |
| 629 | + return 0; |
780 | 630 | } |
781 | | - // if more than session_update_interval second have passed updateProgress |
782 | | - if ( $this->requestKey && |
783 | | - ( ( time() - $this->prevTime ) > $this->sessionUpdateInterval ) ) { |
784 | | - $this->prevTime = time(); |
785 | | - $session_status = $this->updateProgress(); |
786 | | - if ( !$session_status->isOK() ) { |
787 | | - $this->status = $session_status; |
788 | | - wfDebug( __METHOD__ . ' update session failed or was canceled' ); |
789 | | - return false; |
790 | | - } |
| 631 | + // if more than session_update_interval second have passed update_session_progress |
| 632 | + if( $this->do_close_session_update && $this->upload_session_key && |
| 633 | + ( ( time() - $this->prevTime ) > $this->session_update_interval ) ) { |
| 634 | + $this->prevTime = time(); |
| 635 | + $session_status = $this->update_session_progress(); |
| 636 | + if( !$session_status->isOK() ) { |
| 637 | + $this->status = $session_status; |
| 638 | + wfDebug( __METHOD__ . ' update session failed or was canceled'); |
| 639 | + return 0; |
| 640 | + } |
791 | 641 | } |
792 | | - return strlen( $dataPacket ); |
| 642 | + return strlen( $data_packet ); |
793 | 643 | } |
794 | 644 | |
795 | | - public function updateProgress() { |
| 645 | + public function update_session_progress() { |
796 | 646 | global $wgSessionsInMemcached; |
797 | | - |
| 647 | + $status = Status::newGood(); |
798 | 648 | // start the session (if necessary) |
799 | | - if ( !$wgSessionsInMemcached ) { |
| 649 | + if( !$wgSessionsInMemcached ) { |
800 | 650 | wfSuppressWarnings(); |
801 | | - if ( session_start() === false ) { |
| 651 | + if( session_start() === false ) { |
802 | 652 | wfDebug( __METHOD__ . ' could not start session' ); |
803 | 653 | exit( 0 ); |
804 | 654 | } |
805 | 655 | wfRestoreWarnings(); |
806 | 656 | } |
807 | | - $sd =& $_SESSION['wsBgRequest'][ $this->requestKey ]; |
| 657 | + $sd =& $_SESSION['wsDownload'][ $this->upload_session_key ]; |
808 | 658 | // check if the user canceled the request: |
809 | | - if ( $sd['userCancel'] ) { |
810 | | - // @@todo kill the download |
| 659 | + if( isset( $sd['user_cancel'] ) && $sd['user_cancel'] == true ) { |
| 660 | + //@@todo kill the download |
811 | 661 | return Status::newFatal( 'user-canceled-request' ); |
812 | 662 | } |
813 | 663 | // update the progress bytes download so far: |
814 | | - $sd['loaded'] = $this->currentFileSize; |
| 664 | + $sd['loaded'] = $this->current_fsize; |
815 | 665 | |
816 | 666 | // close down the session so we can other http queries can get session updates: |
817 | | - if ( !$wgSessionsInMemcached ) |
| 667 | + if( !$wgSessionsInMemcached ) |
818 | 668 | session_write_close(); |
819 | 669 | |
820 | | - return Status::newGood(); |
| 670 | + return $status; |
821 | 671 | } |
822 | 672 | |
823 | 673 | public function close() { |
824 | | - $this->updateProgress(); |
825 | | - |
| 674 | + // do a final session update: |
| 675 | + if( $this->do_close_session_update ) { |
| 676 | + $this->update_session_progress(); |
| 677 | + } |
826 | 678 | // close up the file handle: |
827 | | - if ( false === fclose( $this->fp ) ) { |
| 679 | + if( false === fclose( $this->fp ) ) { |
828 | 680 | $this->status = Status::newFatal( 'HTTP::could-not-close-file' ); |
829 | 681 | } |
830 | 682 | } |