r53324 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r53323‎ | r53324 | r53325 >
Date:19:18, 15 July 2009
Author:ashley
Status:deferred
Tags:
Comment:
coding style cleanup for upload stuff
Modified paths:
  • /trunk/phase3/includes/upload/UploadBase.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadFromChunks.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadFromStash.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadFromUrl.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/upload/UploadFromStash.php
@@ -1,56 +1,61 @@
22 <?php
 3+
34 class UploadFromStash extends UploadBase {
 5+
46 static function isValidSessionKey( $key, $sessionData ) {
57 return !empty( $key ) &&
68 is_array( $sessionData ) &&
79 isset( $sessionData[$key] ) &&
810 isset( $sessionData[$key]['version'] ) &&
9 - $sessionData[$key]['version'] == self::SESSION_VERSION
10 - ;
 11+ $sessionData[$key]['version'] == self::SESSION_VERSION;
1112 }
12 - static function isValidRequest(& $request ) {
13 - $sessionData = $request->getSessionData('wsUploadData');
 13+
 14+ static function isValidRequest( &$request ) {
 15+ $sessionData = $request->getSessionData( 'wsUploadData' );
1416 return self::isValidSessionKey(
1517 $request->getInt( 'wpSessionKey' ),
1618 $sessionData
1719 );
18 - }
 20+ }
 21+
1922 function initialize( $name, $sessionData ) {
2023 /**
2124 * Confirming a temporarily stashed upload.
2225 * We don't want path names to be forged, so we keep
2326 * them in the session on the server and just give
2427 * an opaque key to the user agent.
25 - */
26 - parent::initialize( $name,
27 - $sessionData['mTempPath'],
 28+ */
 29+ parent::initialize( $name,
 30+ $sessionData['mTempPath'],
2831 $sessionData['mFileSize'],
2932 false
3033 );
3134
32 - $this->mFileProps = $sessionData['mFileProps'];
 35+ $this->mFileProps = $sessionData['mFileProps'];
3336 }
34 - function initializeFromRequest( &$request ) {
 37+
 38+ function initializeFromRequest( &$request ) {
3539 $sessionKey = $request->getInt( 'wpSessionKey' );
3640 $sessionData = $request->getSessionData('wsUploadData');
37 -
 41+
3842 $desiredDestName = $request->getText( 'wpDestFile' );
3943 if( !$desiredDestName )
40 - $desiredDestName = $request->getText( 'wpUploadFile' );
 44+ $desiredDestName = $request->getText( 'wpUploadFile' );
4145 return $this->initialize( $desiredDestName, $sessionData[$sessionKey] );
4246 }
43 -
 47+
4448 /**
4549 * File has been previously verified so no need to do so again.
4650 */
4751 protected function verifyFile( $tmpfile ) {
4852 return true;
4953 }
 54+
5055 /**
5156 * We're here from "ignore warnings anyway" so return just OK
5257 */
5358 function checkWarnings() {
5459 return array();
5560 }
56 -}
57 -?>
\ No newline at end of file
 61+
 62+}
\ No newline at end of file
Index: trunk/phase3/includes/upload/UploadFromUrl.php
@@ -1,80 +1,108 @@
22 <?php
 3+
34 class UploadFromUrl extends UploadBase {
45 protected $mTempDownloadPath;
5 -
6 - //by default do a SYNC_DOWNLOAD
 6+
 7+ // by default do a SYNC_DOWNLOAD
78 protected $dl_mode = null;
8 -
 9+
 10+ /**
 11+ * Checks if the user is allowed to use the upload-by-URL feature
 12+ */
913 static function isAllowed( $user ) {
1014 if( !$user->isAllowed( 'upload_by_url' ) )
1115 return 'upload_by_url';
1216 return parent::isAllowed( $user );
1317 }
 18+
 19+ /**
 20+ * Checks if the upload from URL feature is enabled
 21+ */
1422 static function isEnabled() {
1523 global $wgAllowCopyUploads;
1624 return $wgAllowCopyUploads && parent::isEnabled();
17 - }
18 - /*entry point for Api upload:: ASYNC_DOWNLOAD (if possible) */
19 - function initialize( $name, $url, $asyncdownload = false) {
20 - global $wgTmpDirectory, $wgPhpCliPath;
21 -
22 - //check for $asyncdownload request:
23 - if($asyncdownload !== false){
24 - if($wgPhpCliPath && wfShellExecEnabled() ){
 25+ }
 26+
 27+ /* entry point for API upload:: ASYNC_DOWNLOAD (if possible) */
 28+ function initialize( $name, $url, $asyncdownload = false ) {
 29+ global $wgTmpDirectory, $wgPhpCliPath;
 30+
 31+ // check for $asyncdownload request:
 32+ if( $asyncdownload !== false){
 33+ if( $wgPhpCliPath && wfShellExecEnabled() ){
2534 $this->dl_mode = Http::ASYNC_DOWNLOAD;
26 - }else{
27 - $this->dl_mode = Http::SYNC_DOWNLOAD;
 35+ } else {
 36+ $this->dl_mode = Http::SYNC_DOWNLOAD;
2837 }
2938 }
30 -
 39+
3140 $local_file = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
3241 parent::initialize( $name, $local_file, 0, true );
33 -
34 - $this->mUrl = trim( $url );
 42+
 43+ $this->mUrl = trim( $url );
3544 }
 45+
3646 public function isAsync(){
3747 return $this->dl_mode == Http::ASYNC_DOWNLOAD;
3848 }
39 - /*entry point for SpecialUpload no ASYNC_DOWNLOAD possible: */
40 - function initializeFromRequest( &$request ) {
4149
42 - //set dl mode if not set:
43 - if(!$this->dl_mode)
 50+ /**
 51+ * Entry point for SpecialUpload no ASYNC_DOWNLOAD possible
 52+ * @param $request Object: WebRequest object
 53+ */
 54+ function initializeFromRequest( &$request ) {
 55+
 56+ // set dl mode if not set:
 57+ if( !$this->dl_mode )
4458 $this->dl_mode = Http::SYNC_DOWNLOAD;
45 -
 59+
4660 $desiredDestName = $request->getText( 'wpDestFile' );
4761 if( !$desiredDestName )
48 - $desiredDestName = $request->getText( 'wpUploadFile' );
49 - return $this->initialize(
50 - $desiredDestName,
51 - $request->getVal('wpUploadFileURL')
 62+ $desiredDestName = $request->getText( 'wpUploadFile' );
 63+ return $this->initialize(
 64+ $desiredDestName,
 65+ $request->getVal( 'wpUploadFileURL' )
5266 );
5367 }
 68+
5469 /**
5570 * Do the real fetching stuff
5671 */
57 - function fetchFile( ) {
58 - //entry point for SpecialUplaod
59 - if( self::isValidURI($this->mUrl) === false) {
60 - return Status::newFatal('upload-proto-error');
61 - }
62 - //now do the actual download to the target file:
63 - $status = Http::doDownload ( $this->mUrl, $this->mTempPath, $this->dl_mode );
64 -
65 - //update the local filesize var:
66 - $this->mFileSize = filesize( $this->mTempPath );
67 -
68 - return $status;
 72+ function fetchFile() {
 73+ // entry point for SpecialUplaod
 74+ if( self::isValidURI( $this->mUrl ) === false ) {
 75+ return Status::newFatal( 'upload-proto-error' );
 76+ }
 77+
 78+ // now do the actual download to the target file:
 79+ $status = Http::doDownload( $this->mUrl, $this->mTempPath, $this->dl_mode );
 80+
 81+ // update the local filesize var:
 82+ $this->mFileSize = filesize( $this->mTempPath );
 83+
 84+ return $status;
6985 }
70 -
 86+
 87+ /**
 88+ * @param $request Object: WebRequest object
 89+ */
7190 static function isValidRequest( $request ){
72 - if( !$request->getVal('wpUploadFileURL') )
 91+ if( !$request->getVal( 'wpUploadFileURL' ) )
7392 return false;
74 - //check that is a valid url:
75 - return self::isValidURI( $request->getVal('wpUploadFileURL') );
 93+ // check that is a valid url:
 94+ return self::isValidURI( $request->getVal( 'wpUploadFileURL' ) );
7695 }
 96+
 97+ /**
 98+ * Checks that the given URI is a valid one
 99+ * @param $uri Mixed: URI to check for validity
 100+ */
77101 static function isValidURI( $uri ){
78 - return preg_match('/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
79 - $uri, $matches);
 102+ return preg_match(
 103+ '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
 104+ $uri,
 105+ $matches
 106+ );
80107 }
 108+
81109 }
\ No newline at end of file
Index: trunk/phase3/includes/upload/UploadBase.php
@@ -40,6 +40,7 @@
4141 }
4242 return true;
4343 }
 44+
4445 /**
4546 * Returns true if the user can use this upload module or else a string
4647 * identifying the missing permission.
@@ -53,6 +54,7 @@
5455
5556 // Upload handlers. Should probably just be a global
5657 static $uploadHandlers = array( 'Stash', 'File', 'Url' );
 58+
5759 /**
5860 * Create a form of UploadBase depending on wpSourceType and initializes it
5961 */
@@ -62,16 +64,15 @@
6365 if( !$type )
6466 return null;
6567
66 - $type = ucfirst($type);
67 - $className = 'UploadFrom'.$type;
68 - wfDebug( __METHOD__.": class name: $className");
 68+ $type = ucfirst( $type );
 69+ $className = 'UploadFrom' . $type;
 70+ wfDebug( __METHOD__ . ": class name: $className" );
6971 if( !in_array( $type, self::$uploadHandlers ) )
7072 return null;
7173
7274 if( !call_user_func( array( $className, 'isEnabled' ) ) )
7375 return null;
7476
75 -
7677 if( !call_user_func( array( $className, 'isValidRequest' ), $request ) )
7778 return null;
7879
@@ -80,6 +81,7 @@
8182 $handler->initializeFromRequest( $request );
8283 return $handler;
8384 }
 85+
8486 /**
8587 * Check whether a request if valid for this handler
8688 */
@@ -105,10 +107,14 @@
106108 function fetchFile() {
107109 return Status::newGood();
108110 }
109 - //return the file size
 111+
 112+ /**
 113+ * Return the file size
 114+ */
110115 function isEmptyFile(){
111 - return empty( $this->mFileSize);
 116+ return empty( $this->mFileSize );
112117 }
 118+
113119 /**
114120 * Verify whether the upload is sane.
115121 * Returns self::OK or else an array with error information
@@ -117,7 +123,6 @@
118124 /**
119125 * If there was no filename or a zero size given, give up quick.
120126 */
121 -
122127 if( $this->isEmptyFile() )
123128 return array( 'status' => self::EMPTY_FILE );
124129
@@ -173,7 +178,7 @@
174179 */
175180 protected function verifyFile( $tmpfile ) {
176181 $this->mFileProps = File::getPropsFromPath( $this->mTempPath, $this->mFinalExtension );
177 - $this->checkMacBinary( );
 182+ $this->checkMacBinary();
178183
179184 #magically determine mime type
180185 $magic = MimeMagic::singleton();
@@ -181,7 +186,7 @@
182187
183188 #check mime type, if desired
184189 global $wgVerifyMimeType;
185 - if( isset($wgMimeTypeBlacklist) && !is_null($wgMimeTypeBlacklist) ) {
 190+ if( isset( $wgMimeTypeBlacklist ) && !is_null( $wgMimeTypeBlacklist ) ) {
186191 if ( $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) )
187192 return array( 'filetype-badmime', $mime );
188193
@@ -198,9 +203,8 @@
199204 }
200205 }
201206
202 -
203207 #check for htmlish code and javascript
204 - if( $this->detectScript ( $tmpfile, $mime, $this->mFinalExtension ) ) {
 208+ if( $this->detectScript( $tmpfile, $mime, $this->mFinalExtension ) ) {
205209 return 'uploadscripted';
206210 }
207211 if( $this->mFinalExtension == 'svg' || $mime == 'image/svg+xml' ) {
@@ -212,11 +216,11 @@
213217 /**
214218 * Scan the uploaded file for viruses
215219 */
216 - $virus = $this->detectVirus($tmpfile);
 220+ $virus = $this->detectVirus( $tmpfile );
217221 if ( $virus ) {
218222 return array( 'uploadvirus', $virus );
219223 }
220 - wfDebug( __METHOD__.": all clear; passing.\n" );
 224+ wfDebug( __METHOD__ . ": all clear; passing.\n" );
221225 return true;
222226 }
223227
@@ -255,7 +259,7 @@
256260 /*
257261 * Check whether the resulting filename is different from the desired one,
258262 * but ignore things like ucfirst() and spaces/underscore things
259 - **/
 263+ */
260264 $comparableName = str_replace( ' ', '_', $this->mDesiredDestName );
261265 global $wgCapitalLinks, $wgContLang;
262266 if ( $wgCapitalLinks ) {
@@ -288,7 +292,7 @@
289293 && self::isThumbName( $this->mLocalFile->getName() ) ){
290294 //make the title:
291295 $nt = $this->getTitle();
292 - $warning['file-thumbnail-no'] = substr( $filename , 0,
 296+ $warning['file-thumbnail-no'] = substr( $filename, 0,
293297 strpos( $nt->getText() , '-' ) +1 );
294298 }
295299
@@ -329,7 +333,7 @@
330334 * Really perform the upload.
331335 */
332336 function performUpload( $comment, $pageText, $watch, $user ) {
333 - wfDebug("\n\n\performUpload: sum:" . $comment . ' c: ' . $pageText . ' w:' .$watch);
 337+ wfDebug( "\n\n\performUpload: sum:" . $comment . ' c: ' . $pageText . ' w:' . $watch );
334338 $status = $this->mLocalFile->upload( $this->mTempPath, $comment, $pageText,
335339 File::DELETE_SOURCE, $this->mFileProps, false, $user );
336340
@@ -354,7 +358,6 @@
355359 * filter out illegal characters, and try to make a legible name
356360 * out of it. We'll strip some silently that Title would die on.
357361 */
358 -
359362 $basename = $this->mDesiredDestName;
360363
361364 $this->mFilteredName = wfStripIllegalFilenameChars( $basename );
@@ -436,10 +439,11 @@
437440 $status = $repo->storeTemp( $saveName, $tempName );
438441 return $status;
439442 }
 443+
440444 /* append to a stashed file */
441 - function appendToUploadFile($srcPath, $toAppendPath ){
 445+ function appendToUploadFile( $srcPath, $toAppendPath ){
442446 $repo = RepoGroup::singleton()->getLocalRepo();
443 - $status = $repo->append($srcPath, $toAppendPath);
 447+ $status = $repo->append( $srcPath, $toAppendPath );
444448 return $status;
445449 }
446450
@@ -459,19 +463,22 @@
460464 return false;
461465 }
462466 $mTempPath = $status->value;
463 - session_start();//start up the session (might have been previously closed to prevent php session locking)
464 - $key = $this->getSessionKey ();
 467+ session_start(); // start up the session (might have been previously closed to prevent php session locking)
 468+ $key = $this->getSessionKey();
465469 $_SESSION['wsUploadData'][$key] = array(
466470 'mTempPath' => $mTempPath,
467471 'mFileSize' => $this->mFileSize,
468472 'mSrcName' => $this->mSrcName,
469473 'mFileProps' => $this->mFileProps,
470474 'version' => self::SESSION_VERSION,
471 - );
472 - session_write_close();
 475+ );
 476+ session_write_close();
473477 return $key;
474478 }
475 - //pull session Key gen from stash in cases where we want to start an upload without much information
 479+
 480+ /**
 481+ * Pull session key gen from stash in cases where we want to start an upload without much information
 482+ */
476483 function getSessionKey(){
477484 $key = mt_rand( 0, 0x7fffffff );
478485 $_SESSION['wsUploadData'][$key] = array();
@@ -495,7 +502,7 @@
496503 */
497504 function cleanupTempFile() {
498505 if ( $this->mRemoveTempFile && $this->mTempPath && file_exists( $this->mTempPath ) ) {
499 - wfDebug( __METHOD__.": Removing temporary file {$this->mTempPath}\n" );
 506+ wfDebug( __METHOD__ . ": Removing temporary file {$this->mTempPath}\n" );
500507 unlink( $this->mTempPath );
501508 }
502509 }
@@ -504,8 +511,7 @@
505512 return $this->mTempPath;
506513 }
507514
508 -
509 - /**
 515+ /**
510516 * Split a file into a base name and all dot-delimited 'extensions'
511517 * on the end. Some web server configurations will fall back to
512518 * earlier pseudo-'extensions' to determine type and execute
@@ -548,7 +554,6 @@
549555 return false;
550556 }
551557
552 -
553558 /**
554559 * Checks if the mime type of the uploaded file matches the file extension.
555560 *
@@ -559,30 +564,30 @@
560565 public static function verifyExtension( $mime, $extension ) {
561566 $magic = MimeMagic::singleton();
562567
563 - if ( ! $mime || $mime == 'unknown' || $mime == 'unknown/unknown' )
564 - if ( ! $magic->isRecognizableExtension( $extension ) ) {
565 - wfDebug( __METHOD__.": passing file with unknown detected mime type; " .
 568+ if ( !$mime || $mime == 'unknown' || $mime == 'unknown/unknown' )
 569+ if ( !$magic->isRecognizableExtension( $extension ) ) {
 570+ wfDebug( __METHOD__ . ": passing file with unknown detected mime type; " .
566571 "unrecognized extension '$extension', can't verify\n" );
567572 return true;
568573 } else {
569 - wfDebug( __METHOD__.": rejecting file with unknown detected mime type; ".
 574+ wfDebug( __METHOD__ . ": rejecting file with unknown detected mime type; ".
570575 "recognized extension '$extension', so probably invalid file\n" );
571576 return false;
572577 }
573578
574 - $match= $magic->isMatchingExtension($extension,$mime);
 579+ $match = $magic->isMatchingExtension( $extension, $mime );
575580
576 - if ($match===NULL) {
577 - wfDebug( __METHOD__.": no file extension known for mime type $mime, passing file\n" );
 581+ if ( $match === NULL ) {
 582+ wfDebug( __METHOD__ . ": no file extension known for mime type $mime, passing file\n" );
578583 return true;
579 - } elseif ($match===true) {
580 - wfDebug( __METHOD__.": mime type $mime matches extension $extension, passing file\n" );
 584+ } elseif( $match === true ) {
 585+ wfDebug( __METHOD__ . ": mime type $mime matches extension $extension, passing file\n" );
581586
582587 #TODO: if it's a bitmap, make sure PHP or ImageMagic resp. can handle it!
583588 return true;
584589
585590 } else {
586 - wfDebug( __METHOD__.": mime type $mime mismatches file extension $extension, rejecting file\n" );
 591+ wfDebug( __METHOD__ . ": mime type $mime mismatches file extension $extension, rejecting file\n" );
587592 return false;
588593 }
589594 }
@@ -597,38 +602,44 @@
598603 * @param string $extension The extension of the file
599604 * @return bool true if the file contains something looking like embedded scripts
600605 */
601 - function detectScript($file, $mime, $extension) {
 606+ function detectScript( $file, $mime, $extension ) {
602607 global $wgAllowTitlesInSVG;
603608
604609 #ugly hack: for text files, always look at the entire file.
605610 #For binary field, just check the first K.
606611
607 - if (strpos($mime,'text/')===0) $chunk = file_get_contents( $file );
 612+ if( strpos( $mime,'text/' ) === 0 )
 613+ $chunk = file_get_contents( $file );
608614 else {
609615 $fp = fopen( $file, 'rb' );
610616 $chunk = fread( $fp, 1024 );
611617 fclose( $fp );
612618 }
613619
614 - $chunk= strtolower( $chunk );
 620+ $chunk = strtolower( $chunk );
615621
616 - if (!$chunk) return false;
 622+ if( !$chunk )
 623+ return false;
617624
618625 #decode from UTF-16 if needed (could be used for obfuscation).
619 - if (substr($chunk,0,2)=="\xfe\xff") $enc= "UTF-16BE";
620 - elseif (substr($chunk,0,2)=="\xff\xfe") $enc= "UTF-16LE";
621 - else $enc= NULL;
 626+ if( substr( $chunk, 0, 2 ) == "\xfe\xff" )
 627+ $enc = "UTF-16BE";
 628+ elseif( substr( $chunk, 0, 2 ) == "\xff\xfe" )
 629+ $enc = "UTF-16LE";
 630+ else
 631+ $enc = NULL;
622632
623 - if ($enc) $chunk= iconv($enc,"ASCII//IGNORE",$chunk);
 633+ if( $enc )
 634+ $chunk = iconv( $enc, "ASCII//IGNORE", $chunk );
624635
625 - $chunk= trim($chunk);
 636+ $chunk = trim( $chunk );
626637
627638 #FIXME: convert from UTF-16 if necessarry!
 639+ wfDebug( __METHOD__ . ": checking for embedded scripts and HTML stuff\n" );
628640
629 - wfDebug("SpecialUpload::detectScript: checking for embedded scripts and HTML stuff\n");
630 -
631641 #check for HTML doctype
632 - if (eregi("<!DOCTYPE *X?HTML",$chunk)) return true;
 642+ if( eregi( "<!DOCTYPE *X?HTML", $chunk ) )
 643+ return true;
633644
634645 /**
635646 * Internet Explorer for Windows performs some really stupid file type
@@ -645,7 +656,6 @@
646657 * Also returns true if Safari would mistake the given file for HTML
647658 * when served with a generic content-type.
648659 */
649 -
650660 $tags = array(
651661 '<a',
652662 '<body',
@@ -655,8 +665,9 @@
656666 '<pre',
657667 '<script', #also in safari
658668 '<table'
659 - );
660 - if( ! $wgAllowTitlesInSVG && $extension !== 'svg' && $mime !== 'image/svg' ) {
 669+ );
 670+
 671+ if( !$wgAllowTitlesInSVG && $extension !== 'svg' && $mime !== 'image/svg' ) {
661672 $tags[] = '<title';
662673 }
663674
@@ -667,22 +678,25 @@
668679 }
669680
670681 /*
671 - * look for javascript
672 - */
 682+ * look for JavaScript
 683+ */
673684
674685 #resolve entity-refs to look at attributes. may be harsh on big files... cache result?
675686 $chunk = Sanitizer::decodeCharReferences( $chunk );
676687
677688 #look for script-types
678 - if (preg_match('!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim',$chunk)) return true;
 689+ if( preg_match( '!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk ) )
 690+ return true;
679691
680692 #look for html-style script-urls
681 - if (preg_match('!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim',$chunk)) return true;
 693+ if( preg_match( '!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) )
 694+ return true;
682695
683696 #look for css-style script-urls
684 - if (preg_match('!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim',$chunk)) return true;
 697+ if( preg_match( '!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) )
 698+ return true;
685699
686 - wfDebug("SpecialUpload::detectScript: no scripts found\n");
 700+ wfDebug( __METHOD__ . ": no scripts found\n" );
687701 return false;
688702 }
689703
@@ -721,8 +735,6 @@
722736 return array_pop( $parts );
723737 }
724738
725 -
726 -
727739 /**
728740 * Generic wrapper function for a virus scanner program.
729741 * This relies on the $wgAntivirus and $wgAntivirusSetup variables.
@@ -733,18 +745,18 @@
734746 * or a string containing feedback from the virus scanner if a virus was found.
735747 * If textual feedback is missing but a virus was found, this function returns true.
736748 */
737 - function detectVirus($file) {
 749+ function detectVirus( $file ) {
738750 global $wgAntivirus, $wgAntivirusSetup, $wgAntivirusRequired, $wgOut;
739751
740752 if ( !$wgAntivirus ) {
741 - wfDebug( __METHOD__.": virus scanner disabled\n");
 753+ wfDebug( __METHOD__ . ": virus scanner disabled\n" );
742754 return NULL;
743755 }
744756
745757 if ( !$wgAntivirusSetup[$wgAntivirus] ) {
746 - wfDebug( __METHOD__.": unknown virus scanner: $wgAntivirus\n" );
 758+ wfDebug( __METHOD__ . ": unknown virus scanner: $wgAntivirus\n" );
747759 $wgOut->wrapWikiMsg( '<div class="error">$1</div>', array( 'virus-badscanner', $wgAntivirus ) );
748 - return wfMsg('virus-unknownscanner') . " $wgAntivirus";
 760+ return wfMsg( 'virus-unknownscanner' ) . " $wgAntivirus";
749761 }
750762
751763 # look up scanner configuration
@@ -761,7 +773,7 @@
762774 $command = str_replace( "%f", wfEscapeShellArg( $file ), $command );
763775 }
764776
765 - wfDebug( __METHOD__.": running virus scan: $command \n" );
 777+ wfDebug( __METHOD__ . ": running virus scan: $command \n" );
766778
767779 # execute virus scanner
768780 $exitCode = false;
@@ -788,20 +800,20 @@
789801
790802 if ( $mappedCode === AV_SCAN_FAILED ) {
791803 # scan failed (code was mapped to false by $exitCodeMap)
792 - wfDebug( __METHOD__.": failed to scan $file (code $exitCode).\n" );
 804+ wfDebug( __METHOD__ . ": failed to scan $file (code $exitCode).\n" );
793805
794806 if ( $wgAntivirusRequired ) {
795 - return wfMsg('virus-scanfailed', array( $exitCode ) );
 807+ return wfMsg( 'virus-scanfailed', array( $exitCode ) );
796808 } else {
797809 return NULL;
798810 }
799811 } else if ( $mappedCode === AV_SCAN_ABORTED ) {
800812 # scan failed because filetype is unknown (probably imune)
801 - wfDebug( __METHOD__.": unsupported file type $file (code $exitCode).\n" );
 813+ wfDebug( __METHOD__ . ": unsupported file type $file (code $exitCode).\n" );
802814 return NULL;
803815 } else if ( $mappedCode === AV_NO_VIRUS ) {
804816 # no virus found
805 - wfDebug( __METHOD__.": file passed virus scan.\n" );
 817+ wfDebug( __METHOD__ . ": file passed virus scan.\n" );
806818 return false;
807819 } else {
808820 $output = join( "\n", $output );
@@ -818,7 +830,7 @@
819831 }
820832 }
821833
822 - wfDebug( __METHOD__.": FOUND VIRUS! scanner feedback: $output \n" );
 834+ wfDebug( __METHOD__ . ": FOUND VIRUS! scanner feedback: $output \n" );
823835 return $output;
824836 }
825837 }
@@ -834,10 +846,10 @@
835847 function checkMacBinary() {
836848 $macbin = new MacBinary( $this->mTempPath );
837849 if( $macbin->isValid() ) {
838 - $dataFile = tempnam( wfTempDir(), "WikiMacBinary" );
 850+ $dataFile = tempnam( wfTempDir(), 'WikiMacBinary' );
839851 $dataHandle = fopen( $dataFile, 'wb' );
840852
841 - wfDebug( "SpecialUpload::checkMacBinary: Extracting MacBinary data fork to $dataFile\n" );
 853+ wfDebug( __METHOD__ . ": Extracting MacBinary data fork to $dataFile\n" );
842854 $macbin->extractData( $dataHandle );
843855
844856 $this->mTempPath = $dataFile;
@@ -870,8 +882,8 @@
871883 return 'fileexists-shared-forbidden';
872884
873885 return true;
 886+ }
874887
875 - }
876888 /**
877889 * Check if a user is the last uploader
878890 *
@@ -969,5 +981,4 @@
970982 return $blacklist;
971983 }
972984
973 -
974985 }
Index: trunk/phase3/includes/upload/UploadFromChunks.php
@@ -1,13 +1,13 @@
22 <?php
3 -/*
4 -* first destination checks are made (if ignorewarnings is not checked) errors / warning is returned.
5 -*
6 -* we return the uploadUrl
7 -* we then accept chunk uploads from the client.
8 -* return chunk id on each POSTED chunk
9 -* once the client posts done=1 concatenated the files together.
10 -* more info at: http://firefogg.org/dev/chunk_post.html
11 -*/
 3+/**
 4+ * first destination checks are made (if ignorewarnings is not checked) errors / warning is returned.
 5+ *
 6+ * we return the uploadUrl
 7+ * we then accept chunk uploads from the client.
 8+ * return chunk id on each POSTED chunk
 9+ * once the client posts done=1 concatenated the files together.
 10+ * more info at: http://firefogg.org/dev/chunk_post.html
 11+ */
1212 class UploadFromChunks extends UploadBase {
1313
1414 var $chunk_mode; //init, chunk, done
@@ -18,24 +18,23 @@
1919 const CHUNK = 2;
2020 const DONE = 3;
2121
22 - function initializeFromParams( $param , &$request) {
 22+ function initializeFromParams( $param, &$request ) {
2323 $this->initFromSessionKey( $param['chunksessionkey'], $request );
24 - //set the chunk mode:
 24+ // set the chunk mode:
2525 if( !$this->mSessionKey && !$param['done'] ){
26 - //session key not set init the chunk upload system:
 26+ // session key not set init the chunk upload system:
2727 $this->chunk_mode = UploadFromChunks::INIT;
2828 $this->mDesiredDestName = $param['filename'];
29 -
30 - }else if( $this->mSessionKey && !$param['done']){
31 - //this is a chunk piece
 29+ } else if( $this->mSessionKey && !$param['done'] ){
 30+ // this is a chunk piece
3231 $this->chunk_mode = UploadFromChunks::CHUNK;
33 - }else if( $this->mSessionKey && $param['done']){
34 - //this is the last chunk
 32+ } else if( $this->mSessionKey && $param['done'] ){
 33+ // this is the last chunk
3534 $this->chunk_mode = UploadFromChunks::DONE;
3635 }
3736 if( $this->chunk_mode == UploadFromChunks::CHUNK ||
38 - $this->chunk_mode == UploadFromChunks::DONE ){
39 - //set chunk related vars:
 37+ $this->chunk_mode == UploadFromChunks::DONE ){
 38+ // set chunk related vars:
4039 $this->mTempPath = $request->getFileTempName( 'chunk' );
4140 $this->mFileSize = $request->getFileSize( 'chunk' );
4241 }
@@ -44,188 +43,197 @@
4544 }
4645
4746 static function isValidRequest( $request ) {
48 - $sessionData = $request->getSessionData('wsUploadData');
49 - if(! self::isValidSessionKey(
 47+ $sessionData = $request->getSessionData( 'wsUploadData' );
 48+ if( !self::isValidSessionKey(
5049 $request->getInt( 'wpSessionKey' ),
51 - $sessionData) )
 50+ $sessionData ) )
5251 return false;
53 - //check for the file:
 52+ // check for the file:
5453 return (bool)$request->getFileTempName( 'file' );
5554 }
5655
57 - /* check warnings depending on chunk_mode*/
 56+ /* check warnings depending on chunk_mode */
5857 function checkWarnings(){
5958 $warning = array();
6059 return $warning;
6160 }
6261
6362 function isEmptyFile(){
64 - //does not apply to chunk init
65 - if( $this->chunk_mode == UploadFromChunks::INIT ){
 63+ // does not apply to chunk init
 64+ if( $this->chunk_mode == UploadFromChunks::INIT ){
6665 return false;
67 - }else{
 66+ } else {
6867 return parent::isEmptyFile();
6968 }
7069 }
71 - /* Verify whether the upload is sane.
 70+
 71+ /**
 72+ * Verify whether the upload is sane.
7273 * Returns self::OK or else an array with error information
7374 */
7475 function verifyUpload( $resultDetails ) {
75 - //no checks on chunk upload mode:
 76+ // no checks on chunk upload mode:
7677 if( $this->chunk_mode == UploadFromChunks::INIT )
7778 return self::OK;
7879
79 - //verify on init and last chunk request
 80+ // verify on init and last chunk request
8081 if( $this->chunk_mode == UploadFromChunks::CHUNK ||
8182 $this->chunk_mode == UploadFromChunks::DONE )
8283 return parent::verifyUpload( $resultDetails );
8384 }
84 - //only run verifyFile on completed uploaded chunks
 85+
 86+ // only run verifyFile on completed uploaded chunks
8587 function verifyFile( $tmpFile ){
8688 if( $this->chunk_mode == UploadFromChunks::DONE ){
87 - //first append last chunk (so we can do a real verifyFile check... (check file type etc)
 89+ // first append last chunk (so we can do a real verifyFile check... (check file type etc)
8890 $status = $this->doChunkAppend();
8991 if( $status->isOK() ){
9092 $this->mTempPath = $this->getRealPath( $this->mTempAppendPath );
91 - //verify the completed merged chunks as if it was the file that got uploaded:
92 - return parent::verifyFile( $this->mTempPath ) ;
93 - }else{
94 - //conflict of status returns (have to return the error ary) ... why we don't consistantly use a status object is beyond me..
 93+ // verify the completed merged chunks as if it was the file that got uploaded:
 94+ return parent::verifyFile( $this->mTempPath );
 95+ } else {
 96+ // conflict of status returns (have to return the error ary) ... why we don't consistantly use a status object is beyond me..
9597 return $status->getErrorsArray();
9698 }
97 - }else{
 99+ } else {
98100 return true;
99101 }
100102 }
101 - function getRealPath($srcPath){
 103+
 104+ function getRealPath( $srcPath ){
102105 $repo = RepoGroup::singleton()->getLocalRepo();
103 - if ( $repo->isVirtualUrl( $srcPath) ) {
 106+ if ( $repo->isVirtualUrl( $srcPath ) ) {
104107 return $repo->resolveVirtualUrl( $srcPath );
105108 }
106109 }
107 - //pretty ugly inter-mixing of mParam and local vars
 110+
 111+ // pretty ugly inter-mixing of mParam and local vars
108112 function setupChunkSession( $summary, $comment, $watch ) {
109113 $this->mSessionKey = $this->getSessionKey();
110 - $_SESSION['wsUploadData'][ $this->mSessionKey ] = array(
 114+ $_SESSION['wsUploadData'][$this->mSessionKey] = array(
111115 'mComment' => $comment,
112116 'mSummary' => $summary,
113117 'mWatch' => $watch,
114118 'mFilteredName' => $this->mFilteredName,
115 - 'mTempAppendPath' => null, //the repo append path (not temporary local node mTempPath)
 119+ 'mTempAppendPath' => null, // the repo append path (not temporary local node mTempPath)
116120 'mDesiredDestName' => $this->mDesiredDestName,
117 - 'version' => self::SESSION_VERSION,
118 - );
119 - return $this->mSessionKey;
 121+ 'version' => self::SESSION_VERSION,
 122+ );
 123+ return $this->mSessionKey;
120124 }
121 - function initFromSessionKey( $sessionKey, $request ){
 125+
 126+ function initFromSessionKey( $sessionKey, $request ){
122127 if( !$sessionKey || empty( $sessionKey ) ){
123128 return false;
124129 }
125130 $this->mSessionKey = $sessionKey;
126 - //load the sessionData array:
127 - $sessionData = $request->getSessionData('wsUploadData');
 131+ // load the sessionData array:
 132+ $sessionData = $request->getSessionData( 'wsUploadData' );
128133
129134 if( isset( $sessionData[$this->mSessionKey]['version'] ) &&
130135 $sessionData[$this->mSessionKey]['version'] == self::SESSION_VERSION ) {
131 - //update the local object from the session //
132 - $this->mComment = $sessionData[ $this->mSessionKey ][ 'mComment' ];
133 - $this->mSummary = $sessionData[ $this->mSessionKey ][ 'mSummary' ];
134 - $this->mWatch = $sessionData[ $this->mSessionKey ][ 'mWatch' ];
135 - $this->mIgnorewarnings = $sessionData[ $this->mSessionKey ][ 'mIgnorewarnings' ];
136 - $this->mFilteredName = $sessionData[ $this->mSessionKey ][ 'mFilteredName' ];
137 - $this->mTempAppendPath = $sessionData[ $this->mSessionKey ][ 'mTempAppendPath' ];
138 - $this->mDesiredDestName = $sessionData[ $this->mSessionKey ][ 'mDesiredDestName' ];
139 - }else{
140 - $this->status = Array( 'error'=> 'missing session data');
 136+ // update the local object from the session
 137+ $this->mComment = $sessionData[$this->mSessionKey]['mComment'];
 138+ $this->mSummary = $sessionData[$this->mSessionKey]['mSummary'];
 139+ $this->mWatch = $sessionData[$this->mSessionKey]['mWatch'];
 140+ $this->mIgnorewarnings = $sessionData[$this->mSessionKey]['mIgnorewarnings'];
 141+ $this->mFilteredName = $sessionData[$this->mSessionKey]['mFilteredName'];
 142+ $this->mTempAppendPath = $sessionData[$this->mSessionKey]['mTempAppendPath'];
 143+ $this->mDesiredDestName = $sessionData[$this->mSessionKey]['mDesiredDestName'];
 144+ } else {
 145+ $this->status = array( 'error' => 'missing session data' );
141146 return false;
142147 }
143148 }
144 - //lets us return an api result (as flow for chunk uploads is kind of different than others.
145 - function performUpload($summary='', $comment='', $watch='', $user){
 149+
 150+ // Lets us return an api result (as flow for chunk uploads is kind of different than others.
 151+ function performUpload( $summary = '', $comment = '', $watch = '', $user ){
146152 global $wgServer, $wgScriptPath, $wgUser;
 153+
147154 if( $this->chunk_mode == UploadFromChunks::INIT ){
148 - //firefogg expects a specific result per:
149 - //http://www.firefogg.org/dev/chunk_post.html
 155+ // firefogg expects a specific result per:
 156+ // http://www.firefogg.org/dev/chunk_post.html
150157
151 - //its oky to return the token here because
152 - //a) the user must have requested the token to get here and
153 - //b) should only happen over POST
154 - //c) (we need the token to validate chunks are coming from a non-xss request)
 158+ // it's okay to return the token here because
 159+ // a) the user must have requested the token to get here and
 160+ // b) should only happen over POST
 161+ // c) (we need the token to validate chunks are coming from a non-xss request)
155162 $token = urlencode( $wgUser->editToken() );
156163 ob_clean();
157164 echo ApiFormatJson::getJsonEncode( array(
158 - "uploadUrl" => "{$wgServer}{$wgScriptPath}/api.php?action=upload&".
 165+ 'uploadUrl' => "{$wgServer}{$wgScriptPath}/api.php?action=upload&".
159166 "token={$token}&format=json&enablechunks=true&chunksessionkey=".
160167 $this->setupChunkSession($summary, $comment, $watch ) ) );
161 - exit(0);
162 - }else if( $this->chunk_mode == UploadFromChunks::CHUNK ){
 168+ exit( 0 );
 169+ } else if( $this->chunk_mode == UploadFromChunks::CHUNK ){
163170 $status = $this->doChunkAppend();
164171 if( $status->isOK() ){
165 - //return success:
166 - //firefogg expects a specific result per:
167 - //http://www.firefogg.org/dev/chunk_post.html
 172+ // return success:
 173+ // firefogg expects a specific result per:
 174+ // http://www.firefogg.org/dev/chunk_post.html
168175 ob_clean();
169176 echo ApiFormatJson::getJsonEncode( array(
170 - "result"=>1,
171 - "filesize"=> filesize( $this->getRealPath( $this->mTempAppendPath ) )
 177+ 'result' => 1,
 178+ 'filesize' => filesize( $this->getRealPath( $this->mTempAppendPath ) )
172179 )
173180 );
174 - exit(0);
 181+ exit( 0 );
175182 /*return array(
176183 'result' => 1
177184 );*/
178 - }else{
 185+ } else {
179186 return $status;
180187 }
181 - }else if( $this->chunk_mode == UploadFromChunks::DONE ){
182 - //update the values from the local (session init) if not paseed again)
183 - if($summary == '')
184 - $summary = $this->mSummary;
 188+ } else if( $this->chunk_mode == UploadFromChunks::DONE ){
 189+ // update the values from the local (session init) if not paseed again)
 190+ if( $summary == '' )
 191+ $summary = $this->mSummary;
185192
186 - if($comment == '')
187 - $comment = $this->mComment;
 193+ if( $comment == '' )
 194+ $comment = $this->mComment;
188195
189 - if($watch == '')
190 - $watch = $this->mWatch;
191 - $status = parent::performUpload($summary, $comment, $watch, $user );
 196+ if( $watch == '' )
 197+ $watch = $this->mWatch;
 198+ $status = parent::performUpload( $summary, $comment, $watch, $user );
192199 if( !$status->isGood() ) {
193200 return $status;
194201 }
195202 $file = $this->getLocalFile();
196 - //firefogg expects a specific result per:
197 - //http://www.firefogg.org/dev/chunk_post.html
 203+ // firefogg expects a specific result per:
 204+ // http://www.firefogg.org/dev/chunk_post.html
198205 ob_clean();
199206 echo ApiFormatJson::getJsonEncode( array(
200 - "result"=>1,
201 - "done"=>1,
202 - "resultUrl"=> $file->getDescriptionUrl()
 207+ 'result' => 1,
 208+ 'done' => 1,
 209+ 'resultUrl' => $file->getDescriptionUrl()
203210 )
204211 );
205 - exit(0);
 212+ exit( 0 );
206213
207214 }
208215 }
209 - //append the given chunk to the temporary uploaded file. (if no temporary uploaded file exists created it.
 216+
 217+ // append the given chunk to the temporary uploaded file. (if no temporary uploaded file exists created it.
210218 function doChunkAppend(){
211 - //if we don't have a mTempAppendPath to generate a file from the chunk packaged var:
212 - if( ! $this->mTempAppendPath ){
213 - //die();
214 - //get temp name:
215 - //make a chunk store path. (append tmp file to chunk)
 219+ // if we don't have a mTempAppendPath to generate a file from the chunk packaged var:
 220+ if( !$this->mTempAppendPath ){
 221+ // get temp name:
 222+ // make a chunk store path. (append tmp file to chunk)
216223 $status = $this->saveTempUploadedFile( $this->mDestName, $this->mTempPath );
217224
218225 if( $status->isOK() ) {
219226 $this->mTempAppendPath = $status->value;
220 - $_SESSION[ 'wsUploadData' ][ $this->mSessionKey ][ 'mTempAppendPath' ] = $this->mTempAppendPath;
 227+ $_SESSION['wsUploadData'][$this->mSessionKey]['mTempAppendPath'] = $this->mTempAppendPath;
221228 }
222229 return $status;
223 - }else{
 230+ } else {
224231 if( is_file( $this->getRealPath( $this->mTempAppendPath ) ) ){
225 - $status = $this->appendToUploadFile( $this->mTempAppendPath, $this->mTempPath );
226 - }else{
 232+ $status = $this->appendToUploadFile( $this->mTempAppendPath, $this->mTempPath );
 233+ } else {
227234 $status->fatal( 'filenotfound', $this->mTempAppendPath );
228235 }
229236 return $status;
230237 }
231238 }
 239+
232240 }

Status & tagging log