summaryrefslogtreecommitdiff
path: root/includes/media/XMPValidate.php
blob: 87f8abfe4c5b2976cc0689b6ad4d5da8f767a966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/**
 * Methods for validating XMP properties.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Media
 */

/**
 * This contains some static methods for
 * validating XMP properties. See XMPInfo and XMPReader classes.
 *
 * Each of these functions take the same parameters
 * * an info array which is a subset of the XMPInfo::items array
 * * A value (passed as reference) to validate. This can be either a
 *	simple value or an array
 * * A boolean to determine if this is validating a simple or complex values
 *
 * It should be noted that when an array is being validated, typically the validation
 * function is called once for each value, and then once at the end for the entire array.
 *
 * These validation functions can also be used to modify the data. See the gps and flash one's
 * for example.
 *
 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf starting at pg 28
 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf starting at pg 11
 */
class XMPValidate {
	/**
	 * function to validate boolean properties ( True or False )
	 *
	 * @param array $info information about current property
	 * @param &$val Mixed current value to validate
	 * @param $standalone Boolean if this is a simple property or array
	 */
	public static function validateBoolean( $info, &$val, $standalone ) {
		if ( !$standalone ) {
			// this only validates standalone properties, not arrays, etc
			return;
		}
		if ( $val !== 'True' && $val !== 'False' ) {
			wfDebugLog( 'XMP', __METHOD__ . " Expected True or False but got $val" );
			$val = null;
		}

	}

	/**
	 * function to validate rational properties ( 12/10 )
	 *
	 * @param array $info information about current property
	 * @param &$val Mixed current value to validate
	 * @param $standalone Boolean if this is a simple property or array
	 */
	public static function validateRational( $info, &$val, $standalone ) {
		if ( !$standalone ) {
			// this only validates standalone properties, not arrays, etc
			return;
		}
		if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
			wfDebugLog( 'XMP', __METHOD__ . " Expected rational but got $val" );
			$val = null;
		}

	}

	/**
	 * function to validate rating properties -1, 0-5
	 *
	 * if its outside of range put it into range.
	 *
	 * @see MWG spec
	 * @param array $info information about current property
	 * @param &$val Mixed current value to validate
	 * @param $standalone Boolean if this is a simple property or array
	 */
	public static function validateRating( $info, &$val, $standalone ) {
		if ( !$standalone ) {
			// this only validates standalone properties, not arrays, etc
			return;
		}
		if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
			|| !is_numeric( $val )
		) {
			wfDebugLog( 'XMP', __METHOD__ . " Expected rating but got $val" );
			$val = null;
			return;
		} else {
			$nVal = (float)$val;
			if ( $nVal < 0 ) {
				// We do < 0 here instead of < -1 here, since
				// the values between 0 and -1 are also illegal
				// as -1 is meant as a special reject rating.
				wfDebugLog( 'XMP', __METHOD__ . " Rating too low, setting to -1 (Rejected)" );
				$val = '-1';
				return;
			}
			if ( $nVal > 5 ) {
				wfDebugLog( 'XMP', __METHOD__ . " Rating too high, setting to 5" );
				$val = '5';
				return;
			}
		}
	}

	/**
	 * function to validate integers
	 *
	 * @param array $info information about current property
	 * @param &$val Mixed current value to validate
	 * @param $standalone Boolean if this is a simple property or array
	 */
	public static function validateInteger( $info, &$val, $standalone ) {
		if ( !$standalone ) {
			// this only validates standalone properties, not arrays, etc
			return;
		}
		if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
			wfDebugLog( 'XMP', __METHOD__ . " Expected integer but got $val" );
			$val = null;
		}

	}

	/**
	 * function to validate properties with a fixed number of allowed
	 * choices. (closed choice)
	 *
	 * @param array $info information about current property
	 * @param &$val Mixed current value to validate
	 * @param $standalone Boolean if this is a simple property or array
	 */
	public static function validateClosed( $info, &$val, $standalone ) {
		if ( !$standalone ) {
			// this only validates standalone properties, not arrays, etc
			return;
		}

		//check if its in a numeric range
		$inRange = false;
		if ( isset( $info['rangeLow'] )
			&& isset( $info['rangeHigh'] )
			&& is_numeric( $val )
			&& ( intval( $val ) <= $info['rangeHigh'] )
			&& ( intval( $val ) >= $info['rangeLow'] )
		) {
			$inRange = true;
		}

		if ( !isset( $info['choices'][$val] ) && !$inRange ) {
			wfDebugLog( 'XMP', __METHOD__ . " Expected closed choice, but got $val" );
			$val = null;
		}
	}

	/**
	 * function to validate and modify flash structure
	 *
	 * @param array $info information about current property
	 * @param &$val Mixed current value to validate
	 * @param $standalone Boolean if this is a simple property or array
	 */
	public static function validateFlash( $info, &$val, $standalone ) {
		if ( $standalone ) {
			// this only validates flash structs, not individual properties
			return;
		}
		if ( !( isset( $val['Fired'] )
			&& isset( $val['Function'] )
			&& isset( $val['Mode'] )
			&& isset( $val['RedEyeMode'] )
			&& isset( $val['Return'] )
		) ) {
			wfDebugLog( 'XMP', __METHOD__ . " Flash structure did not have all the required components" );
			$val = null;
		} else {
			$val = ( "\0" | ( $val['Fired'] === 'True' )
				| ( intval( $val['Return'] ) << 1 )
				| ( intval( $val['Mode'] ) << 3 )
				| ( ( $val['Function'] === 'True' ) << 5 )
				| ( ( $val['RedEyeMode'] === 'True' ) << 6 ) );
		}
	}

	/**
	 * function to validate LangCode properties ( en-GB, etc )
	 *
	 * This is just a naive check to make sure it somewhat looks like a lang code.
	 *
	 * @see rfc 3066
	 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf page 30 (section 8.2.2.5)
	 *
	 * @param array $info information about current property
	 * @param &$val Mixed current value to validate
	 * @param $standalone Boolean if this is a simple property or array
	 */
	public static function validateLangCode( $info, &$val, $standalone ) {
		if ( !$standalone ) {
			// this only validates standalone properties, not arrays, etc
			return;
		}
		if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val ) ) {
			//this is a rather naive check.
			wfDebugLog( 'XMP', __METHOD__ . " Expected Lang code but got $val" );
			$val = null;
		}

	}

	/**
	 * function to validate date properties, and convert to (partial) Exif format.
	 *
	 * Dates can be one of the following formats:
	 * YYYY
	 * YYYY-MM
	 * YYYY-MM-DD
	 * YYYY-MM-DDThh:mmTZD
	 * YYYY-MM-DDThh:mm:ssTZD
	 * YYYY-MM-DDThh:mm:ss.sTZD
	 *
	 * @param array $info information about current property
	 * @param &$val Mixed current value to validate. Converts to TS_EXIF as a side-effect.
	 *	in cases where there's only a partial date, it will give things like
	 *	2011:04.
	 * @param $standalone Boolean if this is a simple property or array
	 */
	public static function validateDate( $info, &$val, $standalone ) {
		if ( !$standalone ) {
			// this only validates standalone properties, not arrays, etc
			return;
		}
		$res = array();
		if ( !preg_match(
			/* ahh! scary regex... */
			'/^([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',
			$val, $res )
		) {
			wfDebugLog( 'XMP', __METHOD__ . " Expected date but got $val" );
			$val = null;
		} else {
			/*
			 * $res is formatted as follows:
			 * 0 -> full date.
			 * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second
			 * 7-> Timezone specifier (Z or something like +12:30 )
			 * many parts are optional, some aren't. For example if you specify
			 * minute, you must specify hour, day, month, and year but not second or TZ.
			 */

			/*
			 * First of all, if year = 0000, Something is wrongish,
			 * so don't extract. This seems to happen when
			 * some programs convert between metadata formats.
			 */
			if ( $res[1] === '0000' ) {
				wfDebugLog( 'XMP', __METHOD__ . " Invalid date (year 0): $val" );
				$val = null;
				return;
			}

			if ( !isset( $res[4] ) ) { //hour
				//just have the year month day (if that)
				$val = $res[1];
				if ( isset( $res[2] ) ) {
					$val .= ':' . $res[2];
				}
				if ( isset( $res[3] ) ) {
					$val .= ':' . $res[3];
				}
				return;
			}

			if ( !isset( $res[7] ) || $res[7] === 'Z' ) {
				//if hour is set, then minute must also be or regex above will fail.
				$val = $res[1] . ':' . $res[2] . ':' . $res[3]
					. ' ' . $res[4] . ':' . $res[5];
				if ( isset( $res[6] ) && $res[6] !== '' ) {
					$val .= ':' . $res[6];
				}
				return;
			}

			// Extra check for empty string necessary due to TZ but no second case.
			$stripSeconds = false;
			if ( !isset( $res[6] ) || $res[6] === '' ) {
				$res[6] = '00';
				$stripSeconds = true;
			}

			// Do timezone processing. We've already done the case that tz = Z.

			// We know that if we got to this step, year, month day hour and min must be set
			// by virtue of regex not failing.

			$unix = wfTimestamp( TS_UNIX, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] );
			$offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
			$offset += intval( substr( $res[7], 4, 2 ) ) * 60;
			if ( substr( $res[7], 0, 1 ) === '-' ) {
				$offset = -$offset;
			}
			$val = wfTimestamp( TS_EXIF, $unix + $offset );

			if ( $stripSeconds ) {
				// If seconds weren't specified, remove the trailing ':00'.
				$val = substr( $val, 0, -3 );
			}
		}

	}

	/** function to validate, and more importantly
	 * translate the XMP DMS form of gps coords to
	 * the decimal form we use.
	 *
	 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf
	 *        section 1.2.7.4 on page 23
	 *
	 * @param array $info unused (info about prop)
	 * @param &$val String GPS string in either DDD,MM,SSk or
	 *           or DDD,MM.mmk form
	 * @param $standalone Boolean if its a simple prop (should always be true)
	 */
	public static function validateGPS( $info, &$val, $standalone ) {
		if ( !$standalone ) {
			return;
		}

		$m = array();
		if ( preg_match(
			'/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
			$val, $m )
		) {
			$coord = intval( $m[1] );
			$coord += intval( $m[2] ) * ( 1 / 60 );
			$coord += intval( $m[3] ) * ( 1 / 3600 );
			if ( $m[4] === 'S' || $m[4] === 'W' ) {
				$coord = -$coord;
			}
			$val = $coord;
			return;
		} elseif ( preg_match(
			'/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
			$val, $m )
		) {
			$coord = intval( $m[1] );
			$coord += floatval( $m[2] ) * ( 1 / 60 );
			if ( $m[3] === 'S' || $m[3] === 'W' ) {
				$coord = -$coord;
			}
			$val = $coord;
			return;

		} else {
			wfDebugLog( 'XMP', __METHOD__
				. " Expected GPSCoordinate, but got $val." );
			$val = null;
			return;
		}
	}
}