Index: branches/new-upload/phase3/includes/UploadFromUrl.php |
— | — | @@ -51,7 +51,7 @@ |
52 | 52 | */ |
53 | 53 | function fetchFile( ) { |
54 | 54 | //entry point for SpecialUplaod |
55 | | - if( stripos($this->mUrl, 'http://') !== 0 && stripos($this->mUrl, 'ftp://') !== 0 ) { |
| 55 | + if( self::isValidURI($this->mUrl) === false) { |
56 | 56 | return Status::newFatal('upload-proto-error'); |
57 | 57 | } |
58 | 58 | //print "fetchFile:: $this->dl_mode"; |
— | — | @@ -69,7 +69,10 @@ |
70 | 70 | if( !$request->getVal('wpUploadFileURL') ) |
71 | 71 | return false; |
72 | 72 | //check that is a valid url: |
| 73 | + return self::isValidURI( $request->getVal('wpUploadFileURL') ); |
| 74 | + } |
| 75 | + static function isValidURI( $uri ){ |
73 | 76 | return preg_match('/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/', |
74 | | - $request->getVal('wpUploadFileURL'), $matches); |
| 77 | + $uri, $matches); |
75 | 78 | } |
76 | 79 | } |
\ No newline at end of file |
Index: branches/new-upload/phase3/includes/api/ApiUpload.php |
— | — | @@ -124,8 +124,8 @@ |
125 | 125 | $this->mUpload->initialize( $this->mParams['filename'], $this->mParams['url']); |
126 | 126 | |
127 | 127 | $status = $this->mUpload->fetchFile(); |
128 | | - if( !$status->isOK() ){ |
129 | | - $this->dieUsage( 'fetchfilerror', $status->getWikiText()); |
| 128 | + if( !$status->isOK() ){ |
| 129 | + return $this->dieUsage( 'fetchfilerror', $status->getWikiText()); |
130 | 130 | } |
131 | 131 | //check if we doing a async request set session info and return the upload_session_key) |
132 | 132 | if( $this->mUpload->isAsync() ){ |
Index: branches/new-upload/phase3/includes/DefaultSettings.php |
— | — | @@ -436,6 +436,7 @@ |
437 | 437 | $wgMaxUploadSize = 1024*1024*100; # 100MB |
438 | 438 | |
439 | 439 | |
| 440 | + |
440 | 441 | /** |
441 | 442 | * Point the upload navigation link to an external URL |
442 | 443 | * Useful if you want to use a shared repository by default |
Index: branches/new-upload/phase3/includes/HttpFunctions.php |
— | — | @@ -9,8 +9,9 @@ |
10 | 10 | const SYNC_DOWNLOAD = 1; //syncronys upload (in a single request) |
11 | 11 | const ASYNC_DOWNLOAD = 2; //asynchronous upload we should spawn out another process and monitor progress if possible) |
12 | 12 | |
13 | | - var $body = ''; |
| 13 | + var $body = ''; |
14 | 14 | |
| 15 | + static $redirectcount=0; |
15 | 16 | /** |
16 | 17 | * Simple wrapper for Http::request( 'GET' ) |
17 | 18 | */ |
— | — | @@ -27,13 +28,33 @@ |
28 | 29 | $req = new HttpRequest( $url, $opts ); |
29 | 30 | return $req->doRequest(); |
30 | 31 | } |
31 | | - public static function doDownload( $url, $target_file_path , $dl_mode = self::SYNC_DOWNLOAD ){ |
32 | | - global $wgPhpCliPath, $wgMaxUploadSize; |
33 | | - //do a quick check to HEAD to insure the file size is not > $wgMaxUploadSize to large no need to download it |
| 32 | + public static function doDownload( $url, $target_file_path , $dl_mode = self::SYNC_DOWNLOAD , $redirectCount=0){ |
| 33 | + global $wgPhpCliPath, $wgMaxUploadSize, $wgMaxRedirects; |
| 34 | + //do a quick check to HEAD to insure the file size is not > $wgMaxUploadSize |
34 | 35 | $head = get_headers($url, 1); |
35 | 36 | |
| 37 | + //check for non-valid result: |
| 38 | + |
| 39 | + wfDebug("\n head: " . print_r($head, true). "\n"); |
| 40 | + |
36 | 41 | //check for redirects: |
| 42 | + if( isset( $head['Location'] ) && strrpos($head[0], '302')!==false ){ |
| 43 | + if($redirectCount < $wgMaxRedirects){ |
| 44 | + if( UploadFromUrl::isValidURI( $head['Location'] )){ |
| 45 | + return self::doDownload ( $head['Location'], $target_file_path , $dl_mode, $redirectCount++); |
| 46 | + }else{ |
| 47 | + return Status::newFatal('upload-proto-error'); |
| 48 | + } |
| 49 | + }else{ |
| 50 | + return Status::newFatal('upload-too-many-redirects'); |
| 51 | + } |
| 52 | + } |
| 53 | + //we did not get a 200 ok response: |
| 54 | + if( strrpos($head[0], '200 OK') === false){ |
| 55 | + return Status::newFatal( 'upload-http-error', htmlspecialchars($head[0]) ); |
| 56 | + } |
37 | 57 | |
| 58 | + |
38 | 59 | $content_length = (isset($head['Content-Length']))?$head['Content-Length']:null; |
39 | 60 | if($content_length){ |
40 | 61 | if($content_length > $wgMaxUploadSize){ |
— | — | @@ -133,18 +154,19 @@ |
134 | 155 | //run the actual request .. (this can take some time) |
135 | 156 | wfDebug("do Request: " . $sd['url'] . ' tf: ' . $sd['target_file_path'] ); |
136 | 157 | $status = $req->doRequest(); |
137 | | - wfDebug("done with req status is: ". $status->isOK(). ' '.$status->getWikiText(). "\n"); |
| 158 | + //wfDebug("done with req status is: ". $status->isOK(). ' '.$status->getWikiText(). "\n"); |
138 | 159 | |
139 | 160 | //start up the session again: |
140 | 161 | if( session_start() === false){ |
141 | 162 | wfDebug( __METHOD__ . ' ERROR:: Could not start session'); |
142 | 163 | } |
143 | 164 | //grab the updated session data pointer |
144 | | - $sd =& $_SESSION[ 'wsDownload' ][$upload_session_key]; |
145 | | - |
| 165 | + $sd =& $_SESSION[ 'wsDownload' ][$upload_session_key]; |
146 | 166 | //if error update status: |
147 | 167 | if( !$status->isOK() ){ |
148 | | - $sd['error'] = $status->getWikiText(); |
| 168 | + $sd['apiUploadResult']= ApiFormatJson::getJsonEncode( |
| 169 | + array( 'error' => $status->getWikiText() ) |
| 170 | + ); |
149 | 171 | } |
150 | 172 | //if status oky process upload using fauxReq to api: |
151 | 173 | if( $status->isOK() ){ |
Index: branches/new-upload/phase3/languages/messages/MessagesEn.php |
— | — | @@ -1930,7 +1930,12 @@ |
1931 | 1931 | 'upload-misc-error-text' => 'An unknown error occurred during the upload. |
1932 | 1932 | Please verify that the URL is valid and accessible and try again. |
1933 | 1933 | If the problem persists, contact an [[Special:ListUsers/sysop|administrator]].', |
| 1934 | +'upload-too-many-redirects' => 'The URL contained too many redirects', |
| 1935 | +'upload-unknown-size' => 'Unknown size', |
1934 | 1936 | |
| 1937 | +//idealy we map out all the http errors and translations else just call this with the http resposne: |
| 1938 | +'upload-http-error' => "An http error occured : $1 ", |
| 1939 | + |
1935 | 1940 | # Some likely curl errors. More could be added from <http://curl.haxx.se/libcurl/c/libcurl-errors.html> |
1936 | 1941 | 'upload-curl-error6' => 'Could not reach URL', |
1937 | 1942 | 'upload-curl-error6-text' => 'The URL provided could not be reached. |