Ejemplo n.º 1
0
 /**
  * @param $filename String
  * @param $expected String Serialized array
  * @dataProvider dataGetMetadata
  */
 public function testGetMetadata($filename, $expected)
 {
     $file = UnregisteredLocalFile::newFromPath($this->filePath . $filename, 'image/png');
     $actual = $this->handler->getMetadata($file, $this->filePath . $filename);
     //		$this->assertEquals( unserialize( $expected ), unserialize( $actual ) );
     $this->assertEquals($expected, $actual);
 }
 public function execute()
 {
     $out = $this->mSpecial->getOutput();
     $dbr = wfGetDB(DB_SLAVE);
     $row = $dbr->selectRow('moderation', array('mod_user AS user', 'mod_user_text AS user_text', 'mod_title AS title', 'mod_stash_key AS stash_key'), array('mod_id' => $this->id), __METHOD__);
     if (!$row) {
         throw new ModerationError('moderation-edit-not-found');
     }
     $user = $row->user ? User::newFromId($row->user) : User::newFromName($row->user_text, false);
     $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash($user);
     try {
         $file = $stash->getFile($row->stash_key);
     } catch (MWException $e) {
         return $this->send404ImageNotFound();
     }
     $is_thumb = $this->mSpecial->getRequest()->getVal('thumb');
     if ($is_thumb) {
         $thumb = $file->transform(array('width' => self::THUMB_WIDTH), File::RENDER_NOW);
         if ($thumb) {
             if ($thumb->fileIsSource()) {
                 $is_thumb = false;
             } else {
                 $file = new UnregisteredLocalFile(false, $stash->repo, $thumb->getStoragePath(), false);
             }
         }
     }
     if (!$file) {
         return $this->send404ImageNotFound();
     }
     $thumb_filename = '';
     if ($is_thumb) {
         $thumb_filename .= $file->getWidth() . 'px-';
     }
     $thumb_filename .= $row->title;
     $headers = array();
     $headers[] = "Content-Disposition: " . FileBackend::makeContentDisposition('inline', $thumb_filename);
     $out->disable();
     # No HTML output (image only)
     $file->getRepo()->streamFile($file->getPath(), $headers);
 }
Ejemplo n.º 3
0
 public function testInvalidDate()
 {
     global $wgShowEXIF;
     if (!$wgShowEXIF) {
         $this->markTestIncomplete("This test needs the exif extension.");
     }
     $file = UnregisteredLocalFile::newFromPath(dirname(__FILE__) . '/../../data/media/broken_exif_date.jpg', 'image/jpeg');
     // Throws an error if bug hit
     $meta = $file->formatMetadata();
     $this->assertNotEquals(false, $meta, 'Valid metadata extracted');
     // Find date exif entry
     $this->assertArrayHasKey('visible', $meta);
     $dateIndex = null;
     foreach ($meta['visible'] as $i => $data) {
         if ($data['id'] == 'exif-datetimeoriginal') {
             $dateIndex = $i;
         }
     }
     $this->assertNotNull($dateIndex, 'Date entry exists in metadata');
     $this->assertEquals('0000:01:00 00:02:27', $meta['visible'][$dateIndex]['value'], 'File with invalid date metadata (bug 29471)');
 }
Ejemplo n.º 4
0
	public function execute() {
		$handler = new PagedTiffHandler();

		$path = $this->mArgs[0];
		$file = UnregisteredLocalFile::newFromPath($path, "image/tiff");

		$metadata = $handler->getMetadata( $file, $path );

		if ( !$metadata ) {
		    print "FAILED! \n";
		    return;
		} 

		$metadata = unserialize( $metadata );

		if ( !$metadata ) {
		    print "BROKEN! \n";
		    return;
		} 

		print_r($metadata);
	}
Ejemplo n.º 5
0
 /**
  * A LocalFile wrapper around a file that has been temporarily stashed, so we can do things like create thumbnails for it
  * Arguably UnregisteredLocalFile should be handling its own file repo but that class is a bit retarded currently
  *
  * @param $repo FSRepo: repository where we should find the path
  * @param $path String: path to file
  * @param $key String: key to store the path and any stashed data under
  * @throws UploadStashBadPathException
  * @throws UploadStashFileNotFoundException
  */
 public function __construct($repo, $path, $key)
 {
     $this->fileKey = $key;
     // resolve mwrepo:// urls
     if ($repo->isVirtualUrl($path)) {
         $path = $repo->resolveVirtualUrl($path);
     } else {
         // check if path appears to be sane, no parent traversals, and is in this repo's temp zone.
         $repoTempPath = $repo->getZonePath('temp');
         if (!$repo->validateFilename($path) || strpos($path, $repoTempPath) !== 0) {
             wfDebug("UploadStash: tried to construct an UploadStashFile from a file that should already exist at '{$path}', but path is not valid\n");
             throw new UploadStashBadPathException('path is not valid');
         }
         // check if path exists! and is a plain file.
         if (!$repo->fileExists($path, FileRepo::FILES_ONLY)) {
             wfDebug("UploadStash: tried to construct an UploadStashFile from a file that should already exist at '{$path}', but path is not found\n");
             throw new UploadStashFileNotFoundException('cannot find path, or not a plain file');
         }
     }
     parent::__construct(false, $repo, $path, false);
     $this->name = basename($this->path);
 }
Ejemplo n.º 6
0
 function imageInfo($filename)
 {
     $info = array('width' => 0, 'height' => 0, 'bits' => 0, 'media' => '', 'major' => '', 'minor' => '');
     $magic = MimeMagic::singleton();
     $mime = $magic->guessMimeType($filename, true);
     list($info['major'], $info['minor']) = explode('/', $mime);
     $info['media'] = $magic->getMediaType($filename, $mime);
     $image = UnregisteredLocalFile::newFromPath($filename, $mime);
     $info['width'] = $image->getWidth();
     $info['height'] = $image->getHeight();
     $gis = $image->getImageSize($filename);
     if (isset($gis['bits'])) {
         $info['bits'] = $gis['bits'];
     }
     return $info;
 }
Ejemplo n.º 7
0
 /**
  * Same as before, but with auto-rotation disabled.
  * @dataProvider providerFilesNoAutoRotate
  */
 function testMetadataNoAutoRotate($name, $type, $info)
 {
     global $wgEnableAutoRotation;
     $wgEnableAutoRotation = false;
     $file = UnregisteredLocalFile::newFromPath($this->filePath . $name, $type);
     $this->assertEquals($info['width'], $file->getWidth(), "{$name}: width check");
     $this->assertEquals($info['height'], $file->getHeight(), "{$name}: height check");
     $wgEnableAutoRotation = true;
 }
	function execute() {
		global $wgRequest, $wgContLanguageCode;

		if ( !$this->scalerAccessRanges ) {
			$this->htmlError( 403, 'inplace_access_disabled' );
			return false;
		}

		/**
		 * Run access checks against REMOTE_ADDR rather than wfGetIP(), since we're not
		 * giving access even to trusted proxies, only direct clients.
		 */
		$allowed = false;
		foreach ( $this->scalerAccessRanges as $range ) {
			if ( IP::isInRange( $_SERVER['REMOTE_ADDR'], $range ) ) {
				$allowed = true;
				break;
			}
		}

		if ( !$allowed ) {
			$this->htmlError( 403, 'inplace_access_denied' );
			return false;
		}

		if ( !$wgRequest->wasPosted() ) {
			echo $this->dtd();
?>
<html>
<head><title>inplace-scaler.php Test Interface</title></head>
<body>
<form method="post" action="inplace-scaler.php" enctype="multipart/form-data" >
<p>File: <input type="file" name="data" /></p>
<p>Width: <input type="text" name="width" /></p>
<p>Page: <input type="page" name="page" /></p>
<p><input type="submit" value="OK" /></p>
</form>
</body>
</html>
<?php
			return true;
		}

		$tempDir = $this->tmpDir . '/' . gmdate( self::$tempDirFormat );
		if ( !is_dir( $tempDir ) ) {
			if ( !wfMkdirParents( $tempDir, null, __METHOD__ ) ) {
				$this->htmlError( 500, 'inplace_scaler_no_temp' );
				return false;
			}
		}

		$name = $wgRequest->getFileName( 'data' );
		$srcTemp = $wgRequest->getFileTempname( 'data' );

		$params = $_REQUEST;
		unset( $params['file'] );
		if ( get_magic_quotes_gpc() ) {
			$params = array_map( 'stripslashes', $params );
		}

		$i = strrpos( $name, '.' );
		$ext = File::normalizeExtension( $i ? substr( $name, $i + 1 ) : '' );

		$magic = MimeMagic::singleton();
		$mime = $magic->guessTypesForExtension( $ext );

		$image = UnregisteredLocalFile::newFromPath( $srcTemp, $mime );

		$handler = $image->getHandler();
		if ( !$handler ) {
			$this->htmlError( 400, 'inplace_scaler_no_handler' );
			return false;
		}

		if ( !isset( $params['page'] ) ) {
			$params['page'] = 1;
		}
		$srcWidth = $image->getWidth( $params['page'] );
		$srcHeight = $image->getHeight( $params['page'] );
		if ( $srcWidth <= 0 || $srcHeight <= 0 ) {
			$this->htmlError( 400, 'inplace_scaler_invalid_image' );
			return false;
		}

		list( $dstExt, $dstMime ) = $handler->getThumbType( $ext, $mime );
		if ( preg_match( '/[ \\n;=]/', $name ) ) {
			$dstName = "thumb.$ext";
		} else {
			$dstName = $name;
		}
		if ( $dstExt != $ext ) {
			$dstName = "$dstName.$dstExt";
		}

		$dstTemp = tempnam( $tempDir, 'mwimg' );

		$thumb = $handler->doTransform( $image, $dstTemp, false, $params );
		if ( !$thumb || $thumb->isError()  ) {
			$error = $thumb ? $thumb->getHtmlMsg() : '';
			$this->htmlErrorReal( 500, 'inplace_scaler_failed', array(''), $error );
			unlink( $dstTemp );
			return false;
		}
		$stat = stat( $dstTemp );
		if ( !$stat  ) {
			$this->htmlError( 500, 'inplace_scaler_no_output' );
			return false;
		}

		if ( $stat['size'] == 0 ) {
			$this->htmlError( 500, 'inplace_scaler_no_output' );
			unlink( $dstTemp );
			return false;
		}

		wfDebug( __METHOD__.": transformation completed successfully, streaming output...\n" );
		header( "Content-Type: $dstMime" );
		header( "Content-Disposition: inline;filename*=utf-8'$wgContLanguageCode'" . urlencode( $dstName ) );
		readfile( $dstTemp );
		unlink( $dstTemp );
	}
Ejemplo n.º 9
0
	function execute() {
		global $wgRequest;

		if ( !$wgRequest->wasPosted() ) {
			echo $this->dtd();
?>
<html>
<head><title>metadata.php Test interface</title>
<body>
<form method="post" action="metadata.php">
<p>Zone: <select name="zone" value="public">
<option>public</option>
<option>temp</option>
<option>deleted</option>
</select>
</p>
<p>Relative path: <input type="text" name="path"></p>
<p><input type="submit" value="OK" /></p>
</form>
</body></html>
<?php
			return true;
		}

		$zone = $wgRequest->getVal( 'zone' );
		$root = $this->getZoneRoot( $zone );
		if ( strval( $root ) == '' ) {
				$this->error( 400, 'webstore_invalid_zone', $zone );
				return false;
		}

		$rel = $wgRequest->getVal( 'path' );
		if ( !$this->validateFilename( $rel ) ) {
			$this->error( 400, 'webstore_path_invalid' );
			return false;
		}

		$fullPath = $root . '/' . $rel;

		$name = basename( $fullPath );
		$i = strrpos( $name, '.' );
		$ext = File::normalizeExtension( $i ? substr( $name, $i + 1 ) : '' );
		$magic = MimeMagic::singleton();
		$mime = $magic->guessTypesForExtension( $ext );
		$type = $magic->getMediaType( $fullPath, $mime);

		$stat = stat( $fullPath );
		if ( !$stat ) {
			$this->error( 400, 'webstore_metadata_not_found', $fullPath );
			return false;
		}

		$image = UnregisteredLocalFile::newFromPath( $fullPath, $mime );
		if ( !$image->getHandler() ) {
			$this->error( 400, 'webstore_no_handler' );
			return false;
		}
		$gis = $image->getImageSize();
		$handlerMeta = $image->getMetadata();
		$stat = stat( $fullPath );

		$metadata = array(
			'width' => $gis[0],
			'height' => $gis[1],
			'bits' => isset( $gis['bits'] ) ? $gis['bits'] : '',
			'type' => $type,
			'mime' => $mime,
			'metadata' => $handlerMeta,
			'size' => $stat['size'],
		);

		header( 'Content-Type: text/xml' );
		echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<response><status>success</status><metadata>\n";
		foreach ( $metadata as $field => $value ) {
			if ( is_bool( $value ) ) {
				$value = $value ? 1 : 0;
			}
			echo "<item name=\"$field\">" . htmlspecialchars( $value ) . "</item>\n";
		}
		echo "</metadata></response>\n";
	}