Merge "(Bug 27559) Preserve tab selection after submit in [[Special:Preferences]]"
[lhc/web/wiklou.git] / includes / media / XMPValidate.php
1 <?php
2 /**
3 * Methods for validating XMP properties.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * This contains some static methods for
26 * validating XMP properties. See XMPInfo and XMPReader classes.
27 *
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
33 *
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.
36 *
37 * These validation functions can also be used to modify the data. See the gps and flash one's
38 * for example.
39 *
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
42 */
43 class XMPValidate {
44 /**
45 * function to validate boolean properties ( True or False )
46 *
47 * @param $info Array information about current property
48 * @param &$val Mixed current value to validate
49 * @param $standalone Boolean if this is a simple property or array
50 */
51 public static function validateBoolean( $info, &$val, $standalone ) {
52 if ( !$standalone ) {
53 // this only validates standalone properties, not arrays, etc
54 return;
55 }
56 if ( $val !== 'True' && $val !== 'False' ) {
57 wfDebugLog( 'XMP', __METHOD__ . " Expected True or False but got $val" );
58 $val = null;
59 }
60
61 }
62
63 /**
64 * function to validate rational properties ( 12/10 )
65 *
66 * @param $info Array information about current property
67 * @param &$val Mixed current value to validate
68 * @param $standalone Boolean if this is a simple property or array
69 */
70 public static function validateRational( $info, &$val, $standalone ) {
71 if ( !$standalone ) {
72 // this only validates standalone properties, not arrays, etc
73 return;
74 }
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;
78 }
79
80 }
81
82 /**
83 * function to validate rating properties -1, 0-5
84 *
85 * if its outside of range put it into range.
86 *
87 * @see MWG spec
88 * @param $info Array information about current property
89 * @param &$val Mixed current value to validate
90 * @param $standalone Boolean if this is a simple property or array
91 */
92 public static function validateRating( $info, &$val, $standalone ) {
93 if ( !$standalone ) {
94 // this only validates standalone properties, not arrays, etc
95 return;
96 }
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;
112 }
113 if ( $nVal > 5 ) {
114 wfDebugLog( 'XMP', __METHOD__ . " Rating too high, setting to 5" );
115 $val = '5';
116 return;
117 }
118 }
119 }
120
121 /**
122 * function to validate integers
123 *
124 * @param $info Array information about current property
125 * @param &$val Mixed current value to validate
126 * @param $standalone Boolean if this is a simple property or array
127 */
128 public static function validateInteger( $info, &$val, $standalone ) {
129 if ( !$standalone ) {
130 // this only validates standalone properties, not arrays, etc
131 return;
132 }
133 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
134 wfDebugLog( 'XMP', __METHOD__ . " Expected integer but got $val" );
135 $val = null;
136 }
137
138 }
139
140 /**
141 * function to validate properties with a fixed number of allowed
142 * choices. (closed choice)
143 *
144 * @param $info Array information about current property
145 * @param &$val Mixed current value to validate
146 * @param $standalone Boolean if this is a simple property or array
147 */
148 public static function validateClosed( $info, &$val, $standalone ) {
149 if ( !$standalone ) {
150 // this only validates standalone properties, not arrays, etc
151 return;
152 }
153
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'] )
161 ) {
162 $inRange = true;
163 }
164
165 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
166 wfDebugLog( 'XMP', __METHOD__ . " Expected closed choice, but got $val" );
167 $val = null;
168 }
169 }
170
171 /**
172 * function to validate and modify flash structure
173 *
174 * @param $info Array information about current property
175 * @param &$val Mixed current value to validate
176 * @param $standalone Boolean if this is a simple property or array
177 */
178 public static function validateFlash( $info, &$val, $standalone ) {
179 if ( $standalone ) {
180 // this only validates flash structs, not individual properties
181 return;
182 }
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 ) );
197 }
198 }
199
200 /**
201 * function to validate LangCode properties ( en-GB, etc )
202 *
203 * This is just a naive check to make sure it somewhat looks like a lang code.
204 *
205 * @see rfc 3066
206 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf page 30 (section 8.2.2.5)
207 *
208 * @param $info Array information about current property
209 * @param &$val Mixed current value to validate
210 * @param $standalone Boolean if this is a simple property or array
211 */
212 public static function validateLangCode( $info, &$val, $standalone ) {
213 if ( !$standalone ) {
214 // this only validates standalone properties, not arrays, etc
215 return;
216 }
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;
221 }
222
223 }
224
225 /**
226 * function to validate date properties, and convert to (partial) Exif format.
227 *
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
235 *
236 * @param $info Array 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
241 */
242 public static function validateDate( $info, &$val, $standalone ) {
243 if ( !$standalone ) {
244 // this only validates standalone properties, not arrays, etc
245 return;
246 }
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 )
252 ) {
253 wfDebugLog( 'XMP', __METHOD__ . " Expected date but got $val" );
254 $val = null;
255 } else {
256 /*
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.
263 */
264
265 /*
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.
269 */
270 if ( $res[1] === '0000' ) {
271 wfDebugLog( 'XMP', __METHOD__ . " Invalid date (year 0): $val" );
272 $val = null;
273 return;
274 }
275
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];
281 }
282 if ( isset( $res[3] ) ) {
283 $val .= ':' . $res[3];
284 }
285 return;
286 }
287
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];
294 }
295 return;
296 }
297
298
299 // Extra check for empty string necessary due to TZ but no second case.
300 $stripSeconds = false;
301 if ( !isset( $res[6] ) || $res[6] === '' ) {
302 $res[6] = '00';
303 $stripSeconds = true;
304 }
305
306 // Do timezone processing. We've already done the case that tz = Z.
307
308 // We know that if we got to this step, year, month day hour and min must be set
309 // by virtue of regex not failing.
310
311 $unix = wfTimestamp( TS_UNIX, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] );
312 $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
313 $offset += intval( substr( $res[7], 4, 2 ) ) * 60;
314 if ( substr( $res[7], 0, 1 ) === '-' ) {
315 $offset = -$offset;
316 }
317 $val = wfTimestamp( TS_EXIF, $unix + $offset );
318
319 if ( $stripSeconds ) {
320 // If seconds weren't specified, remove the trailing ':00'.
321 $val = substr( $val, 0, -3 );
322 }
323 }
324
325 }
326
327 /** function to validate, and more importantly
328 * translate the XMP DMS form of gps coords to
329 * the decimal form we use.
330 *
331 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf
332 * section 1.2.7.4 on page 23
333 *
334 * @param $info Array unused (info about prop)
335 * @param &$val String GPS string in either DDD,MM,SSk or
336 * or DDD,MM.mmk form
337 * @param $standalone Boolean if its a simple prop (should always be true)
338 */
339 public static function validateGPS ( $info, &$val, $standalone ) {
340 if ( !$standalone ) {
341 return;
342 }
343
344 $m = array();
345 if ( preg_match(
346 '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
347 $val, $m )
348 ) {
349 $coord = intval( $m[1] );
350 $coord += intval( $m[2] ) * (1/60);
351 $coord += intval( $m[3] ) * (1/3600);
352 if ( $m[4] === 'S' || $m[4] === 'W' ) {
353 $coord = -$coord;
354 }
355 $val = $coord;
356 return;
357 } elseif ( preg_match(
358 '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
359 $val, $m )
360 ) {
361 $coord = intval( $m[1] );
362 $coord += floatval( $m[2] ) * (1/60);
363 if ( $m[3] === 'S' || $m[3] === 'W' ) {
364 $coord = -$coord;
365 }
366 $val = $coord;
367 return;
368
369 } else {
370 wfDebugLog( 'XMP', __METHOD__
371 . " Expected GPSCoordinate, but got $val." );
372 $val = null;
373 return;
374 }
375 }
376 }