Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / media / XMPValidate.php
blob3e29c12b2434214dbcf58873d50962179e12cda3
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 /**
25 * This contains some static methods for
26 * validating XMP properties. See XMPInfo and XMPReader classes.
28 * Each of these functions take the same parameters
29 * * an info array which is a subset of the XMPInfo::items array
30 * * A value (passed as reference) to validate. This can be either a
31 * simple value or an array
32 * * A boolean to determine if this is validating a simple or complex values
34 * It should be noted that when an array is being validated, typically the validation
35 * function is called once for each value, and then once at the end for the entire array.
37 * These validation functions can also be used to modify the data. See the gps and flash one's
38 * for example.
40 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf starting at pg 28
41 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf starting at pg 11
43 class XMPValidate {
44 /**
45 * function to validate boolean properties ( True or False )
47 * @param array $info information about current property
48 * @param &$val Mixed current value to validate
49 * @param $standalone Boolean if this is a simple property or array
51 public static function validateBoolean( $info, &$val, $standalone ) {
52 if ( !$standalone ) {
53 // this only validates standalone properties, not arrays, etc
54 return;
56 if ( $val !== 'True' && $val !== 'False' ) {
57 wfDebugLog( 'XMP', __METHOD__ . " Expected True or False but got $val" );
58 $val = null;
63 /**
64 * function to validate rational properties ( 12/10 )
66 * @param array $info information about current property
67 * @param &$val Mixed current value to validate
68 * @param $standalone Boolean if this is a simple property or array
70 public static function validateRational( $info, &$val, $standalone ) {
71 if ( !$standalone ) {
72 // this only validates standalone properties, not arrays, etc
73 return;
75 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
76 wfDebugLog( 'XMP', __METHOD__ . " Expected rational but got $val" );
77 $val = null;
82 /**
83 * function to validate rating properties -1, 0-5
85 * if its outside of range put it into range.
87 * @see MWG spec
88 * @param array $info information about current property
89 * @param &$val Mixed current value to validate
90 * @param $standalone Boolean if this is a simple property or array
92 public static function validateRating( $info, &$val, $standalone ) {
93 if ( !$standalone ) {
94 // this only validates standalone properties, not arrays, etc
95 return;
97 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
98 || !is_numeric( $val )
99 ) {
100 wfDebugLog( 'XMP', __METHOD__ . " Expected rating but got $val" );
101 $val = null;
102 return;
103 } else {
104 $nVal = (float) $val;
105 if ( $nVal < 0 ) {
106 // We do < 0 here instead of < -1 here, since
107 // the values between 0 and -1 are also illegal
108 // as -1 is meant as a special reject rating.
109 wfDebugLog( 'XMP', __METHOD__ . " Rating too low, setting to -1 (Rejected)" );
110 $val = '-1';
111 return;
113 if ( $nVal > 5 ) {
114 wfDebugLog( 'XMP', __METHOD__ . " Rating too high, setting to 5" );
115 $val = '5';
116 return;
122 * function to validate integers
124 * @param array $info information about current property
125 * @param &$val Mixed current value to validate
126 * @param $standalone Boolean if this is a simple property or array
128 public static function validateInteger( $info, &$val, $standalone ) {
129 if ( !$standalone ) {
130 // this only validates standalone properties, not arrays, etc
131 return;
133 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
134 wfDebugLog( 'XMP', __METHOD__ . " Expected integer but got $val" );
135 $val = null;
141 * function to validate properties with a fixed number of allowed
142 * choices. (closed choice)
144 * @param array $info information about current property
145 * @param &$val Mixed current value to validate
146 * @param $standalone Boolean if this is a simple property or array
148 public static function validateClosed( $info, &$val, $standalone ) {
149 if ( !$standalone ) {
150 // this only validates standalone properties, not arrays, etc
151 return;
154 //check if its in a numeric range
155 $inRange = false;
156 if ( isset( $info['rangeLow'] )
157 && isset( $info['rangeHigh'] )
158 && is_numeric( $val )
159 && ( intval( $val ) <= $info['rangeHigh'] )
160 && ( intval( $val ) >= $info['rangeLow'] )
162 $inRange = true;
165 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
166 wfDebugLog( 'XMP', __METHOD__ . " Expected closed choice, but got $val" );
167 $val = null;
172 * function to validate and modify flash structure
174 * @param array $info information about current property
175 * @param &$val Mixed current value to validate
176 * @param $standalone Boolean if this is a simple property or array
178 public static function validateFlash( $info, &$val, $standalone ) {
179 if ( $standalone ) {
180 // this only validates flash structs, not individual properties
181 return;
183 if ( !( isset( $val['Fired'] )
184 && isset( $val['Function'] )
185 && isset( $val['Mode'] )
186 && isset( $val['RedEyeMode'] )
187 && isset( $val['Return'] )
188 ) ) {
189 wfDebugLog( 'XMP', __METHOD__ . " Flash structure did not have all the required components" );
190 $val = null;
191 } else {
192 $val = ( "\0" | ( $val['Fired'] === 'True' )
193 | ( intval( $val['Return'] ) << 1 )
194 | ( intval( $val['Mode'] ) << 3 )
195 | ( ( $val['Function'] === 'True' ) << 5 )
196 | ( ( $val['RedEyeMode'] === 'True' ) << 6 ) );
201 * function to validate LangCode properties ( en-GB, etc )
203 * This is just a naive check to make sure it somewhat looks like a lang code.
205 * @see rfc 3066
206 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf page 30 (section 8.2.2.5)
208 * @param array $info information about current property
209 * @param &$val Mixed current value to validate
210 * @param $standalone Boolean if this is a simple property or array
212 public static function validateLangCode( $info, &$val, $standalone ) {
213 if ( !$standalone ) {
214 // this only validates standalone properties, not arrays, etc
215 return;
217 if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val ) ) {
218 //this is a rather naive check.
219 wfDebugLog( 'XMP', __METHOD__ . " Expected Lang code but got $val" );
220 $val = null;
226 * function to validate date properties, and convert to (partial) Exif format.
228 * Dates can be one of the following formats:
229 * YYYY
230 * YYYY-MM
231 * YYYY-MM-DD
232 * YYYY-MM-DDThh:mmTZD
233 * YYYY-MM-DDThh:mm:ssTZD
234 * YYYY-MM-DDThh:mm:ss.sTZD
236 * @param array $info information about current property
237 * @param &$val Mixed current value to validate. Converts to TS_EXIF as a side-effect.
238 * in cases where there's only a partial date, it will give things like
239 * 2011:04.
240 * @param $standalone Boolean if this is a simple property or array
242 public static function validateDate( $info, &$val, $standalone ) {
243 if ( !$standalone ) {
244 // this only validates standalone properties, not arrays, etc
245 return;
247 $res = array();
248 if ( !preg_match(
249 /* ahh! scary regex... */
250 '/^([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',
251 $val, $res )
253 wfDebugLog( 'XMP', __METHOD__ . " Expected date but got $val" );
254 $val = null;
255 } else {
257 * $res is formatted as follows:
258 * 0 -> full date.
259 * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second
260 * 7-> Timezone specifier (Z or something like +12:30 )
261 * many parts are optional, some aren't. For example if you specify
262 * minute, you must specify hour, day, month, and year but not second or TZ.
266 * First of all, if year = 0000, Something is wrongish,
267 * so don't extract. This seems to happen when
268 * some programs convert between metadata formats.
270 if ( $res[1] === '0000' ) {
271 wfDebugLog( 'XMP', __METHOD__ . " Invalid date (year 0): $val" );
272 $val = null;
273 return;
276 if ( !isset( $res[4] ) ) { //hour
277 //just have the year month day (if that)
278 $val = $res[1];
279 if ( isset( $res[2] ) ) {
280 $val .= ':' . $res[2];
282 if ( isset( $res[3] ) ) {
283 $val .= ':' . $res[3];
285 return;
288 if ( !isset( $res[7] ) || $res[7] === 'Z' ) {
289 //if hour is set, then minute must also be or regex above will fail.
290 $val = $res[1] . ':' . $res[2] . ':' . $res[3]
291 . ' ' . $res[4] . ':' . $res[5];
292 if ( isset( $res[6] ) && $res[6] !== '' ) {
293 $val .= ':' . $res[6];
295 return;
298 // Extra check for empty string necessary due to TZ but no second case.
299 $stripSeconds = false;
300 if ( !isset( $res[6] ) || $res[6] === '' ) {
301 $res[6] = '00';
302 $stripSeconds = true;
305 // Do timezone processing. We've already done the case that tz = Z.
307 // We know that if we got to this step, year, month day hour and min must be set
308 // by virtue of regex not failing.
310 $unix = wfTimestamp( TS_UNIX, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] );
311 $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
312 $offset += intval( substr( $res[7], 4, 2 ) ) * 60;
313 if ( substr( $res[7], 0, 1 ) === '-' ) {
314 $offset = -$offset;
316 $val = wfTimestamp( TS_EXIF, $unix + $offset );
318 if ( $stripSeconds ) {
319 // If seconds weren't specified, remove the trailing ':00'.
320 $val = substr( $val, 0, -3 );
326 /** function to validate, and more importantly
327 * translate the XMP DMS form of gps coords to
328 * the decimal form we use.
330 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf
331 * section 1.2.7.4 on page 23
333 * @param array $info unused (info about prop)
334 * @param &$val String GPS string in either DDD,MM,SSk or
335 * or DDD,MM.mmk form
336 * @param $standalone Boolean if its a simple prop (should always be true)
338 public static function validateGPS( $info, &$val, $standalone ) {
339 if ( !$standalone ) {
340 return;
343 $m = array();
344 if ( preg_match(
345 '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
346 $val, $m )
348 $coord = intval( $m[1] );
349 $coord += intval( $m[2] ) * ( 1 / 60 );
350 $coord += intval( $m[3] ) * ( 1 / 3600 );
351 if ( $m[4] === 'S' || $m[4] === 'W' ) {
352 $coord = -$coord;
354 $val = $coord;
355 return;
356 } elseif ( preg_match(
357 '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
358 $val, $m )
360 $coord = intval( $m[1] );
361 $coord += floatval( $m[2] ) * ( 1 / 60 );
362 if ( $m[3] === 'S' || $m[3] === 'W' ) {
363 $coord = -$coord;
365 $val = $coord;
366 return;
368 } else {
369 wfDebugLog( 'XMP', __METHOD__
370 . " Expected GPSCoordinate, but got $val." );
371 $val = null;
372 return;