Update docs/hooks.txt for ShowSearchHitTitle
[mediawiki.git] / includes / libs / xmp / XMPValidate.php
blob31eaa3baa76ca41115b490917c1986a6481f171f
1 <?php
2 /**
3 * Methods for validating XMP properties.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Media
24 use Psr\Log\LoggerInterface;
25 use Psr\Log\LoggerAwareInterface;
27 /**
28 * This contains some static methods for
29 * validating XMP properties. See XMPInfo and XMPReader classes.
31 * Each of these functions take the same parameters
32 * * an info array which is a subset of the XMPInfo::items array
33 * * A value (passed as reference) to validate. This can be either a
34 * simple value or an array
35 * * A boolean to determine if this is validating a simple or complex values
37 * It should be noted that when an array is being validated, typically the validation
38 * function is called once for each value, and then once at the end for the entire array.
40 * These validation functions can also be used to modify the data. See the gps and flash one's
41 * for example.
43 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf starting at pg 28
44 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf starting at pg 11
46 class XMPValidate implements LoggerAwareInterface {
48 /**
49 * @var LoggerInterface
51 private $logger;
53 public function __construct( LoggerInterface $logger ) {
54 $this->setLogger( $logger );
57 public function setLogger( LoggerInterface $logger ) {
58 $this->logger = $logger;
60 /**
61 * Function to validate boolean properties ( True or False )
63 * @param array $info Information about current property
64 * @param mixed &$val Current value to validate
65 * @param bool $standalone If this is a simple property or array
67 public function validateBoolean( $info, &$val, $standalone ) {
68 if ( !$standalone ) {
69 // this only validates standalone properties, not arrays, etc
70 return;
72 if ( $val !== 'True' && $val !== 'False' ) {
73 $this->logger->info( __METHOD__ . " Expected True or False but got $val" );
74 $val = null;
78 /**
79 * function to validate rational properties ( 12/10 )
81 * @param array $info Information about current property
82 * @param mixed &$val Current value to validate
83 * @param bool $standalone If this is a simple property or array
85 public function validateRational( $info, &$val, $standalone ) {
86 if ( !$standalone ) {
87 // this only validates standalone properties, not arrays, etc
88 return;
90 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
91 $this->logger->info( __METHOD__ . " Expected rational but got $val" );
92 $val = null;
96 /**
97 * function to validate rating properties -1, 0-5
99 * if its outside of range put it into range.
101 * @see MWG spec
102 * @param array $info Information about current property
103 * @param mixed &$val Current value to validate
104 * @param bool $standalone If this is a simple property or array
106 public function validateRating( $info, &$val, $standalone ) {
107 if ( !$standalone ) {
108 // this only validates standalone properties, not arrays, etc
109 return;
111 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
112 || !is_numeric( $val )
114 $this->logger->info( __METHOD__ . " Expected rating but got $val" );
115 $val = null;
117 return;
118 } else {
119 $nVal = (float)$val;
120 if ( $nVal < 0 ) {
121 // We do < 0 here instead of < -1 here, since
122 // the values between 0 and -1 are also illegal
123 // as -1 is meant as a special reject rating.
124 $this->logger->info( __METHOD__ . " Rating too low, setting to -1 (Rejected)" );
125 $val = '-1';
127 return;
129 if ( $nVal > 5 ) {
130 $this->logger->info( __METHOD__ . " Rating too high, setting to 5" );
131 $val = '5';
133 return;
139 * function to validate integers
141 * @param array $info Information about current property
142 * @param mixed &$val Current value to validate
143 * @param bool $standalone If this is a simple property or array
145 public function validateInteger( $info, &$val, $standalone ) {
146 if ( !$standalone ) {
147 // this only validates standalone properties, not arrays, etc
148 return;
150 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
151 $this->logger->info( __METHOD__ . " Expected integer but got $val" );
152 $val = null;
157 * function to validate properties with a fixed number of allowed
158 * choices. (closed choice)
160 * @param array $info Information about current property
161 * @param mixed &$val Current value to validate
162 * @param bool $standalone If this is a simple property or array
164 public function validateClosed( $info, &$val, $standalone ) {
165 if ( !$standalone ) {
166 // this only validates standalone properties, not arrays, etc
167 return;
170 // check if its in a numeric range
171 $inRange = false;
172 if ( isset( $info['rangeLow'] )
173 && isset( $info['rangeHigh'] )
174 && is_numeric( $val )
175 && ( intval( $val ) <= $info['rangeHigh'] )
176 && ( intval( $val ) >= $info['rangeLow'] )
178 $inRange = true;
181 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
182 $this->logger->info( __METHOD__ . " Expected closed choice, but got $val" );
183 $val = null;
188 * function to validate and modify flash structure
190 * @param array $info Information about current property
191 * @param mixed &$val Current value to validate
192 * @param bool $standalone If this is a simple property or array
194 public function validateFlash( $info, &$val, $standalone ) {
195 if ( $standalone ) {
196 // this only validates flash structs, not individual properties
197 return;
199 if ( !( isset( $val['Fired'] )
200 && isset( $val['Function'] )
201 && isset( $val['Mode'] )
202 && isset( $val['RedEyeMode'] )
203 && isset( $val['Return'] )
204 ) ) {
205 $this->logger->info( __METHOD__ . " Flash structure did not have all the required components" );
206 $val = null;
207 } else {
208 $val = ( "\0" | ( $val['Fired'] === 'True' )
209 | ( intval( $val['Return'] ) << 1 )
210 | ( intval( $val['Mode'] ) << 3 )
211 | ( ( $val['Function'] === 'True' ) << 5 )
212 | ( ( $val['RedEyeMode'] === 'True' ) << 6 ) );
217 * function to validate LangCode properties ( en-GB, etc )
219 * This is just a naive check to make sure it somewhat looks like a lang code.
221 * @see BCP 47
222 * @see https://wwwimages2.adobe.com/content/dam/Adobe/en/devnet/xmp/pdfs/
223 * XMP%20SDK%20Release%20cc-2014-12/XMPSpecificationPart1.pdf page 22 (section 8.2.2.4)
225 * @param array $info Information about current property
226 * @param mixed &$val Current value to validate
227 * @param bool $standalone If this is a simple property or array
229 public function validateLangCode( $info, &$val, $standalone ) {
230 if ( !$standalone ) {
231 // this only validates standalone properties, not arrays, etc
232 return;
234 if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val ) ) {
235 // this is a rather naive check.
236 $this->logger->info( __METHOD__ . " Expected Lang code but got $val" );
237 $val = null;
242 * function to validate date properties, and convert to (partial) Exif format.
244 * Dates can be one of the following formats:
245 * YYYY
246 * YYYY-MM
247 * YYYY-MM-DD
248 * YYYY-MM-DDThh:mmTZD
249 * YYYY-MM-DDThh:mm:ssTZD
250 * YYYY-MM-DDThh:mm:ss.sTZD
252 * @param array $info Information about current property
253 * @param mixed &$val Current value to validate. Converts to TS_EXIF as a side-effect.
254 * in cases where there's only a partial date, it will give things like
255 * 2011:04.
256 * @param bool $standalone If this is a simple property or array
258 public function validateDate( $info, &$val, $standalone ) {
259 if ( !$standalone ) {
260 // this only validates standalone properties, not arrays, etc
261 return;
263 $res = [];
264 // @codingStandardsIgnoreStart Long line that cannot be broken
265 if ( !preg_match(
266 /* ahh! scary regex... */
267 '/^([0-3]\d{3})(?:-([01]\d)(?:-([0-3]\d)(?:T([0-2]\d):([0-6]\d)(?::([0-6]\d)(?:\.\d+)?)?([-+]\d{2}:\d{2}|Z)?)?)?)?$/D',
268 $val, $res )
270 // @codingStandardsIgnoreEnd
272 $this->logger->info( __METHOD__ . " Expected date but got $val" );
273 $val = null;
274 } else {
276 * $res is formatted as follows:
277 * 0 -> full date.
278 * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second
279 * 7-> Timezone specifier (Z or something like +12:30 )
280 * many parts are optional, some aren't. For example if you specify
281 * minute, you must specify hour, day, month, and year but not second or TZ.
285 * First of all, if year = 0000, Something is wrongish,
286 * so don't extract. This seems to happen when
287 * some programs convert between metadata formats.
289 if ( $res[1] === '0000' ) {
290 $this->logger->info( __METHOD__ . " Invalid date (year 0): $val" );
291 $val = null;
293 return;
296 if ( !isset( $res[4] ) ) { // hour
297 // just have the year month day (if that)
298 $val = $res[1];
299 if ( isset( $res[2] ) ) {
300 $val .= ':' . $res[2];
302 if ( isset( $res[3] ) ) {
303 $val .= ':' . $res[3];
306 return;
309 if ( !isset( $res[7] ) || $res[7] === 'Z' ) {
310 // if hour is set, then minute must also be or regex above will fail.
311 $val = $res[1] . ':' . $res[2] . ':' . $res[3]
312 . ' ' . $res[4] . ':' . $res[5];
313 if ( isset( $res[6] ) && $res[6] !== '' ) {
314 $val .= ':' . $res[6];
317 return;
320 // Extra check for empty string necessary due to TZ but no second case.
321 $stripSeconds = false;
322 if ( !isset( $res[6] ) || $res[6] === '' ) {
323 $res[6] = '00';
324 $stripSeconds = true;
327 // Do timezone processing. We've already done the case that tz = Z.
329 // We know that if we got to this step, year, month day hour and min must be set
330 // by virtue of regex not failing.
332 $unix = ConvertibleTimestamp::convert( TS_UNIX,
333 $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6]
335 $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
336 $offset += intval( substr( $res[7], 4, 2 ) ) * 60;
337 if ( substr( $res[7], 0, 1 ) === '-' ) {
338 $offset = -$offset;
340 $val = ConvertibleTimestamp::convert( TS_EXIF, $unix + $offset );
342 if ( $stripSeconds ) {
343 // If seconds weren't specified, remove the trailing ':00'.
344 $val = substr( $val, 0, -3 );
349 /** function to validate, and more importantly
350 * translate the XMP DMS form of gps coords to
351 * the decimal form we use.
353 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf
354 * section 1.2.7.4 on page 23
356 * @param array $info Unused (info about prop)
357 * @param string &$val GPS string in either DDD,MM,SSk or
358 * or DDD,MM.mmk form
359 * @param bool $standalone If its a simple prop (should always be true)
361 public function validateGPS( $info, &$val, $standalone ) {
362 if ( !$standalone ) {
363 return;
366 $m = [];
367 if ( preg_match(
368 '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
369 $val, $m )
371 $coord = intval( $m[1] );
372 $coord += intval( $m[2] ) * ( 1 / 60 );
373 $coord += intval( $m[3] ) * ( 1 / 3600 );
374 if ( $m[4] === 'S' || $m[4] === 'W' ) {
375 $coord = -$coord;
377 $val = $coord;
379 return;
380 } elseif ( preg_match(
381 '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
382 $val, $m )
384 $coord = intval( $m[1] );
385 $coord += floatval( $m[2] ) * ( 1 / 60 );
386 if ( $m[3] === 'S' || $m[3] === 'W' ) {
387 $coord = -$coord;
389 $val = $coord;
391 return;
392 } else {
393 $this->logger->info( __METHOD__
394 . " Expected GPSCoordinate, but got $val." );
395 $val = null;
397 return;