Security fix: Previously it was possible to include unprotected and even content...
[mediawiki.git] / includes / MediaTransformOutput.php
blob8745bb5853fc99b0983695e32cee2a191f877e79
1 <?php
3 /**
4 * Base class for the output of MediaHandler::doTransform() and File::transform().
6 * @addtogroup Media
7 */
8 abstract class MediaTransformOutput {
9 /**
10 * Get the width of the output box
12 function getWidth() {
13 return $this->width;
16 /**
17 * Get the height of the output box
19 function getHeight() {
20 return $this->height;
23 /**
24 * @return string The thumbnail URL
26 function getUrl() {
27 return $this->url;
30 /**
31 * @return string Destination file path (local filesystem)
33 function getPath() {
34 return $this->path;
37 /**
38 * Fetch HTML for this transform output
39 * @param array $attribs Advisory associative array of HTML attributes supplied
40 * by the linker. These can be incorporated into the output in any way.
41 * @param array $linkAttribs Attributes of a suggested enclosing <a> tag.
42 * May be ignored.
44 abstract function toHtml( $attribs = array() , $linkAttribs = false );
46 /**
47 * This will be overridden to return true in error classes
49 function isError() {
50 return false;
53 /**
54 * Wrap some XHTML text in an anchor tag with the given attributes
56 protected function linkWrap( $linkAttribs, $contents ) {
57 if ( $linkAttribs ) {
58 return Xml::tags( 'a', $linkAttribs, $contents );
59 } else {
60 return $contents;
66 /**
67 * Media transform output for images
69 * @addtogroup Media
71 class ThumbnailImage extends MediaTransformOutput {
72 /**
73 * @param string $path Filesystem path to the thumb
74 * @param string $url URL path to the thumb
75 * @private
77 function ThumbnailImage( $url, $width, $height, $path = false ) {
78 $this->url = $url;
79 # These should be integers when they get here.
80 # If not, there's a bug somewhere. But let's at
81 # least produce valid HTML code regardless.
82 $this->width = round( $width );
83 $this->height = round( $height );
84 $this->path = $path;
87 /**
88 * Return HTML <img ... /> tag for the thumbnail, will include
89 * width and height attributes and a blank alt text (as required).
91 * You can set or override additional attributes by passing an
92 * associative array of name => data pairs. The data will be escaped
93 * for HTML output, so should be in plaintext.
95 * If $linkAttribs is given, the image will be enclosed in an <a> tag.
97 * @param array $attribs
98 * @param array $linkAttribs
99 * @return string
100 * @public
102 function toHtml( $attribs = array(), $linkAttribs = false ) {
103 $attribs['src'] = $this->url;
104 $attribs['width'] = $this->width;
105 $attribs['height'] = $this->height;
106 if( !isset( $attribs['alt'] ) ) $attribs['alt'] = '';
107 return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
113 * Basic media transform error class
115 * @addtogroup Media
117 class MediaTransformError extends MediaTransformOutput {
118 var $htmlMsg, $textMsg, $width, $height, $url, $path;
120 function __construct( $msg, $width, $height /*, ... */ ) {
121 $args = array_slice( func_get_args(), 3 );
122 $htmlArgs = array_map( 'htmlspecialchars', $args );
123 $htmlArgs = array_map( 'nl2br', $htmlArgs );
125 $this->htmlMsg = wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $msg, true ) ), $htmlArgs );
126 $this->textMsg = wfMsgReal( $msg, $args );
127 $this->width = intval( $width );
128 $this->height = intval( $height );
129 $this->url = false;
130 $this->path = false;
133 function toHtml( $attribs = array(), $linkAttribs = false ) {
134 return "<table class=\"MediaTransformError\" style=\"" .
135 "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
136 $this->htmlMsg .
137 "</td></tr></table>";
140 function toText() {
141 return $this->textMsg;
144 function getHtmlMsg() {
145 return $this->htmlMsg;
148 function isError() {
149 return true;
154 * Shortcut class for parameter validation errors
156 * @addtogroup Media
158 class TransformParameterError extends MediaTransformError {
159 function __construct( $params ) {
160 parent::__construct( 'thumbnail_error',
161 max( isset( $params['width'] ) ? $params['width'] : 0, 180 ),
162 max( isset( $params['height'] ) ? $params['height'] : 0, 180 ),
163 wfMsg( 'thumbnail_invalid_params' ) );