r71943 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71942‎ | r71943 | r71944 >
Date:12:18, 30 August 2010
Author:daniel
Status:ok (Comments)
Tags:
Comment:
use new UploadVerifyFile hook, introduced in r71942
Modified paths:
  • /trunk/extensions/PagedTiffHandler/PagedTiffHandler.i18n.php (modified) (history)
  • /trunk/extensions/PagedTiffHandler/PagedTiffHandler.php (modified) (history)
  • /trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php
@@ -59,13 +59,13 @@
6060 * - identify-warnings
6161 * - check for running-identify-service
6262 */
63 - static function check( $saveName, $tempName, &$error ) {
 63+ static function verifyFile( $upload, $mime, &$error ) {
6464 global $wgTiffMaxEmbedFiles, $wgTiffMaxMetaSize, $wgMaxUploadSize,
6565 $wgTiffRejectOnError, $wgTiffRejectOnWarning, $wgTiffUseTiffReader,
6666 $wgTiffReaderPath, $wgTiffReaderCheckEofForJS;
6767
68 - # XXX: it would be much nicer if the hook would get the mime type as a parameter
69 - $mime = MimeMagic::singleton()->guessMimeType( $tempName, false );
 68+ $tempName = $upload->getTempPath();
 69+ $saveName = $upload->getLocalFile()->getName();
7070
7171 if ( $mime != "image/tiff" ) {
7272 # not a tiff file, do not check
@@ -78,58 +78,70 @@
7979 $tr = new TiffReader( $tempName );
8080 $tr->check();
8181 if ( !$tr->isValidTiff() ) {
82 - $error = 'tiff_bad_file';
83 - wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 82+ $error = array( 'tiff_bad_file' );
 83+ wfDebug( __METHOD__ . ": {$error[0]} ($saveName)\n" );
8484 return false;
8585 }
8686 if ( $tr->checkScriptAtEnd( $wgTiffReaderCheckEofForJS ) ) {
87 - $error = 'tiff_script_detected';
88 - wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 87+ $error = array( 'tiff_script_detected' );
 88+ wfDebug( __METHOD__ . ": {$error[0]} ($saveName)\n" );
8989 return false;
9090 }
9191 if ( !$tr->checkSize() ) {
92 - $error = 'tiff_size_error';
93 - wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 92+ $error = array( 'tiff_size_error' );
 93+ wfDebug( __METHOD__ . ": {$error[0]} ($saveName)\n" );
9494 return false;
9595 }
9696 }
9797 $meta = self::getTiffImage( false, $tempName )->retrieveMetaData();
9898 if ( !$meta && $meta != - 1 ) {
99 - $error = 'tiff_out_of_service';
100 - wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 99+ $error = array( 'tiff_out_of_service' );
 100+ wfDebug( __METHOD__ . ": {$error[0]} ($saveName)\n" );
101101 return false;
102102 }
103103 if ( $meta == - 1 ) {
104 - $error = 'tiff_error_cached';
105 - wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 104+ $error = array( 'tiff_error_cached' );
 105+ wfDebug( __METHOD__ . ": {$error[0]} ($saveName)\n" );
106106 }
107 - return self::extCheck( $meta, $error, $saveName );
 107+
 108+ $ok = self::verifyMetaData( $meta, $error, $saveName );
 109+
 110+ if ( !$ok ) {
 111+ wfDebug( __METHOD__ . ": file is ok ($saveName)\n" );
 112+ }
 113+
 114+ return $ok;
108115 }
109116
110 - static function extCheck( $meta, &$error, $saveName = '' ) {
 117+ static function verifyMetaData( $meta, &$error, $saveName = '' ) {
111118 global $wgTiffMaxEmbedFiles, $wgTiffMaxMetaSize;
112119
113120 $errors = PagedTiffHandler::getMetadataErrors( $meta );
114121 if ( $errors ) {
115 - $error = 'tiff_bad_file';
 122+ $error = array( 'tiff_bad_file', PagedTiffHandler::joinMessages( $errors ) );
116123
117 - // NOTE: in future, it will become possible to pass parameters
118 - // $error = array( 'tiff_bad_file' , PagedTiffHandler::joinMessages( $errors ) );
119 - // does that work now? ^DK
120 -
121 - wfDebug( __METHOD__ . ": $error ($saveName) " . PagedTiffHandler::joinMessages( $errors, false ) . "\n" );
 124+ wfDebug( __METHOD__ . ": {$error[0]} ($saveName) " . PagedTiffHandler::joinMessages( $errors, false ) . "\n" );
122125 return false;
123126 }
124 - if ( ( strlen( serialize( $meta ) ) + 1 ) > $wgTiffMaxMetaSize ) {
125 - $error = 'tiff_too_much_meta';
126 - wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 127+
 128+ if ( $meta['page_amount'] <= 0 || empty( $meta['page_data'] ) ) {
 129+ $error = array( 'tiff_page_error', $meta['page_amount'] );
 130+ wfDebug( __METHOD__ . ": {$error[0]} ($saveName)\n" );
127131 return false;
128132 }
129133 if ( $wgTiffMaxEmbedFiles && $meta['page_amount'] > $wgTiffMaxEmbedFiles ) {
130 - $error = 'tiff_too_much_embed_files';
131 - wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 134+ $error = array( 'tiff_too_much_embed_files', $meta['page_amount'], $wgTiffMaxEmbedFiles );
 135+ wfDebug( __METHOD__ . ": {$error[0]} ($saveName)\n" );
132136 return false;
133137 }
 138+ $len = strlen( serialize( $meta ) );
 139+ if ( ( $len + 1 ) > $wgTiffMaxMetaSize ) {
 140+ $error = array( 'tiff_too_much_meta', $len, $wgTiffMaxMetaSize );
 141+ wfDebug( __METHOD__ . ": {$error[0]} ($saveName)\n" );
 142+ return false;
 143+ }
 144+
 145+ wfDebug( __METHOD__ . ": metadata is ok ($saveName)\n" );
134146 return true;
135147 }
136148
@@ -327,7 +339,7 @@
328340 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
329341 }
330342
331 - if ( !self::extCheck( $meta, $error, $dstPath ) ) {
 343+ if ( !self::verifyMetaData( $meta, $error, $dstPath ) ) {
332344 return $this->doThumbError( $params, $error );
333345 }
334346
@@ -502,7 +514,9 @@
503515 * If it returns false, Image will reload the metadata from the file and update the database
504516 */
505517 function isMetadataValid( $image, $metadata ) {
506 - if ( is_string( $metadata ) ) $metadata = unserialize( $metadata );
 518+ if ( is_string( $metadata ) ) {
 519+ $metadata = unserialize( $metadata );
 520+ }
507521
508522 if ( !isset( $metadata['TIFF_METADATA_VERSION'] ) ) {
509523 return false;
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler.i18n.php
@@ -27,7 +27,7 @@
2828 'tiff_error_cached' => 'This file can only be rerendered after the caching interval.',
2929 'tiff_size_error' => 'The reported file size does not match the actual file size.',
3030 'tiff_script_detected' => 'The uploaded file contains scripts.',
31 - 'tiff_bad_file' => 'The uploaded file contains errors.',
 31+ 'tiff_bad_file' => 'The uploaded file contains errors: $1',
3232 'tiff-file-info-size' => '(page $5, $1 × $2 pixel, file size: $3, MIME type: $4)',
3333 );
3434
@@ -47,7 +47,7 @@
4848 'tiff_error_cached' => 'Error message shown when a error occurres and it is cached.',
4949 'tiff_size_error' => 'Error message shown when the reported file size does not match the actual file size.',
5050 'tiff_script_detected' => 'Error message shown when the uploaded file contains scripts.',
51 - 'tiff_bad_file' => 'Error message shown when the uploaded file contains errors.',
 51+ 'tiff_bad_file' => 'Error message shown when the uploaded file contains errors. First parameter contains error messages',
5252 'tiff-file-info-size' => 'Information about the image dimensions etc. on image page. Extended by page information',
5353 );
5454
@@ -67,7 +67,7 @@
6868 'tiff_error_cached' => 'Error message shown when a error occurres and it is cached.',
6969 'tiff_size_error' => 'Error message shown when the reported file size does not match the actual file size.',
7070 'tiff_script_detected' => 'Error message shown when the uploaded file contains scripts.',
71 - 'tiff_bad_file' => 'Error message shown when the uploaded file contains errors.',
 71+ 'tiff_bad_file' => 'Error message shown when the uploaded file contains errors. First parameter contains error messages.',
7272 'tiff-file-info-size' => 'Information about the image dimensions etc. on image page. Extended by page information',
7373 );
7474
@@ -151,7 +151,7 @@
152152 'tiff_error_cached' => 'Diese Datei kann erst nach Ablauf der Caching-Periode neu gerendert werden.',
153153 'tiff_size_error' => 'Die errechnete Größe der Datei stimmt nicht mit der tatsächlichen überein.',
154154 'tiff_script_detected' => 'Die hochgeladene Datei enthält Skripte.',
155 - 'tiff_bad_file' => 'Die hochgeladene Datei ist fehlerhaft.',
 155+ 'tiff_bad_file' => 'Die hochgeladene Datei ist fehlerhaft: $1',
156156 'tiff-file-info-size' => '(Seite $5, $1 × $2 Pixel, Dateigröße: $3, MIME-Typ: $4)',
157157 );
158158
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler.php
@@ -121,7 +121,7 @@
122122 $wgAutoloadClasses['PagedTiffHandlerSeleniumTestSuite'] = $dir . 'selenium/PagedTiffHandlerTestSuite.php';
123123
124124 $wgMediaHandlers['image/tiff'] = 'PagedTiffHandler';
125 -$wgHooks['UploadVerification'][] = 'PagedTiffHandler::check';
 125+$wgHooks['UploadVerifyFile'][] = 'PagedTiffHandler::verifyFile';
126126 $wgHooks['LanguageGetMagic'][] = 'PagedTiffHandler::addTiffLossyMagicWordLang';
127127
128128 define('TIFF_METADATA_VERSION', '1.0');

Follow-up revisions

RevisionCommit summaryAuthorDate
r71977removed some dead wood, as per comment on r71943daniel20:53, 30 August 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r71942interoduced new hoop in Uploadbase::verifyFile, as per comments to r71789daniel12:10, 30 August 2010

Comments

#Comment by Duesentrieb (talk | contribs)   12:19, 30 August 2010

requires r71942

#Comment by TheDJ (talk | contribs)   14:53, 30 August 2010

This is not new, but I notice that there is no return after meta==-1. This will lead to meta and error being passed to verifyMetaData() (and potentially overwriting error[0] ?) is this the desired behavior, or is it an oversight ?

Status & tagging log