Merge to trunk everything in img_metadata branch.
[lhc/web/wiklou.git] / includes / media / XMPValidate.php
1 <?php
2 /**
3 * This contains some static methods for
4 * validating XMP properties. See XMPInfo and XMPReader classes.
5 *
6 * Each of these functions take the same parameters
7 * * an info array which is a subset of the XMPInfo::items array
8 * * A value (passed as reference) to validate. This can be either a
9 * simple value or an array
10 * * A boolean to determine if this is validating a simple or complex values
11 *
12 * It should be noted that when an array is being validated, typically the validation
13 * function is called once for each value, and then once at the end for the entire array.
14 *
15 * These validation functions can also be used to modify the data. See the gps and flash one's
16 * for example.
17 *
18 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf starting at pg 28
19 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf starting at pg 11
20 */
21 class XMPValidate {
22 /**
23 * function to validate boolean properties ( True or False )
24 *
25 * @param $info Array information about current property
26 * @param &$val Mixed current value to validate
27 * @param $standalone Boolean if this is a simple property or array
28 */
29 public static function validateBoolean( $info, &$val, $standalone ) {
30 if ( !$standalone ) {
31 // this only validates standalone properties, not arrays, etc
32 return;
33 }
34 if ( $val !== 'True' && $val !== 'False' ) {
35 wfDebugLog( 'XMP', __METHOD__ . " Expected True or False but got $val" );
36 $val = null;
37 }
38
39 }
40 /**
41 * function to validate rational properties ( 12/10 )
42 *
43 * @param $info Array information about current property
44 * @param &$val Mixed current value to validate
45 * @param $standalone Boolean if this is a simple property or array
46 */
47 public static function validateRational( $info, &$val, $standalone ) {
48 if ( !$standalone ) {
49 // this only validates standalone properties, not arrays, etc
50 return;
51 }
52 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
53 wfDebugLog( 'XMP', __METHOD__ . " Expected rational but got $val" );
54 $val = null;
55 }
56
57 }
58 /**
59 * function to validate rating properties -1, 0-5
60 *
61 * if its outside of range put it into range.
62 *
63 * @see MWG spec
64 * @param $info Array information about current property
65 * @param &$val Mixed current value to validate
66 * @param $standalone Boolean if this is a simple property or array
67 */
68 public static function validateRating( $info, &$val, $standalone ) {
69 if ( !$standalone ) {
70 // this only validates standalone properties, not arrays, etc
71 return;
72 }
73 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
74 || !is_numeric($val)
75 ) {
76 wfDebugLog( 'XMP', __METHOD__ . " Expected rating but got $val" );
77 $val = null;
78 return;
79 } else {
80 $nVal = (float) $val;
81 if ( $nVal < 0 ) {
82 // We do < 0 here instead of < -1 here, since
83 // the values between 0 and -1 are also illegal
84 // as -1 is meant as a special reject rating.
85 wfDebugLog( 'XMP', __METHOD__ . " Rating too low, setting to -1 (Rejected)");
86 $val = '-1';
87 return;
88 }
89 if ( $nVal > 5 ) {
90 wfDebugLog( 'XMP', __METHOD__ . " Rating too high, setting to 5");
91 $val = '5';
92 return;
93 }
94 }
95 }
96 /**
97 * function to validate integers
98 *
99 * @param $info Array information about current property
100 * @param &$val Mixed current value to validate
101 * @param $standalone Boolean if this is a simple property or array
102 */
103 public static function validateInteger( $info, &$val, $standalone ) {
104 if ( !$standalone ) {
105 // this only validates standalone properties, not arrays, etc
106 return;
107 }
108 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
109 wfDebugLog( 'XMP', __METHOD__ . " Expected integer but got $val" );
110 $val = null;
111 }
112
113 }
114 /**
115 * function to validate properties with a fixed number of allowed
116 * choices. (closed choice)
117 *
118 * @param $info Array information about current property
119 * @param &$val Mixed current value to validate
120 * @param $standalone Boolean if this is a simple property or array
121 */
122 public static function validateClosed( $info, &$val, $standalone ) {
123 if ( !$standalone ) {
124 // this only validates standalone properties, not arrays, etc
125 return;
126 }
127
128 //check if its in a numeric range
129 $inRange = false;
130 if ( isset( $info['rangeLow'] )
131 && isset( $info['rangeHigh'] )
132 && is_numeric( $val )
133 && ( intval( $val ) <= $info['rangeHigh'] )
134 && ( intval( $val ) >= $info['rangeLow'] )
135 ) {
136 $inRange = true;
137 }
138
139 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
140 wfDebugLog( 'XMP', __METHOD__ . " Expected closed choice, but got $val" );
141 $val = null;
142 }
143 }
144 /**
145 * function to validate and modify flash structure
146 *
147 * @param $info Array information about current property
148 * @param &$val Mixed current value to validate
149 * @param $standalone Boolean if this is a simple property or array
150 */
151 public static function validateFlash( $info, &$val, $standalone ) {
152 if ( $standalone ) {
153 // this only validates flash structs, not individual properties
154 return;
155 }
156 if ( !( isset( $val['Fired'] )
157 && isset( $val['Function'] )
158 && isset( $val['Mode'] )
159 && isset( $val['RedEyeMode'] )
160 && isset( $val['Return'] )
161 ) ) {
162 wfDebugLog( 'XMP', __METHOD__ . " Flash structure did not have all the required components" );
163 $val = null;
164 } else {
165 $val = ( "\0" | ( $val['Fired'] === 'True' )
166 | ( intval( $val['Return'] ) << 1 )
167 | ( intval( $val['Mode'] ) << 3 )
168 | ( ( $val['Function'] === 'True' ) << 5 )
169 | ( ( $val['RedEyeMode'] === 'True' ) << 6 ) );
170 }
171 }
172 /**
173 * function to validate LangCode properties ( en-GB, etc )
174 *
175 * This is just a naive check to make sure it somewhat looks like a lang code.
176 *
177 * @see rfc 3066
178 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf page 30 (section 8.2.2.5)
179 *
180 * @param $info Array information about current property
181 * @param &$val Mixed current value to validate
182 * @param $standalone Boolean if this is a simple property or array
183 */
184 public static function validateLangCode( $info, &$val, $standalone ) {
185 if ( !$standalone ) {
186 // this only validates standalone properties, not arrays, etc
187 return;
188 }
189 if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val) ) {
190 //this is a rather naive check.
191 wfDebugLog( 'XMP', __METHOD__ . " Expected Lang code but got $val" );
192 $val = null;
193 }
194
195 }
196 /**
197 * function to validate date properties, and convert to Exif format.
198 *
199 * @param $info Array information about current property
200 * @param &$val Mixed current value to validate. Converts to TS_EXIF as a side-effect.
201 * @param $standalone Boolean if this is a simple property or array
202 */
203 public static function validateDate( $info, &$val, $standalone ) {
204 if ( !$standalone ) {
205 // this only validates standalone properties, not arrays, etc
206 return;
207 }
208 $res = array();
209 if ( !preg_match(
210 /* ahh! scary regex... */
211 '/^([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'
212 , $val, $res)
213 ) {
214 wfDebugLog( 'XMP', __METHOD__ . " Expected date but got $val" );
215 $val = null;
216 } else {
217 /*
218 * $res is formatted as follows:
219 * 0 -> full date.
220 * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second
221 * 7-> Timezone specifier (Z or something like +12:30 )
222 * many parts are optional, some aren't. For example if you specify
223 * minute, you must specify hour, day, month, and year but not second or TZ.
224 */
225
226 /*
227 * First of all, if year = 0000, Something is wrongish,
228 * so don't extract. This seems to happen when
229 * some programs convert between metadata formats.
230 */
231 if ( $res[1] === '0000' ) {
232 wfDebugLog( 'XMP', __METHOD__ . " Invalid date (year 0): $val" );
233 $val = null;
234 return;
235 }
236 //if month, etc unspecified, full out as 01.
237 $res[2] = isset( $res[2] ) ? $res[2] : '01'; //month
238 $res[3] = isset( $res[3] ) ? $res[3] : '01'; //day
239 if ( !isset( $res[4] ) ) { //hour
240 //just have the year month day
241 $val = $res[1] . ':' . $res[2] . ':' . $res[3];
242 return;
243 }
244 //if hour is set, so is minute or regex above will fail.
245 //Extra check for empty string necessary due to TZ but no second case.
246 $res[6] = isset( $res[6] ) && $res[6] != '' ? $res[6] : '00';
247
248 if ( !isset( $res[7] ) || $res[7] === 'Z' ) {
249 $val = $res[1] . ':' . $res[2] . ':' . $res[3]
250 . ' ' . $res[4] . ':' . $res[5] . ':' . $res[6];
251 return;
252 }
253
254 //do timezone processing. We've already done the case that tz = Z.
255
256 $unix = wfTimestamp( TS_UNIX, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] );
257 $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
258 $offset += intval( substr( $res[7], 4, 2 ) ) * 60;
259 if ( substr( $res[7], 0, 1 ) === '-' ) {
260 $offset = -$offset;
261 }
262 $val = wfTimestamp( TS_EXIF, $unix + $offset );
263 }
264
265 }
266 /** function to validate, and more importantly
267 * translate the XMP DMS form of gps coords to
268 * the decimal form we use.
269 *
270 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf
271 * section 1.2.7.4 on page 23
272 *
273 * @param $info Array unused (info about prop)
274 * @param &$val String GPS string in either DDD,MM,SSk or
275 * or DDD,MM.mmk form
276 * @param $standalone Boolean if its a simple prop (should always be true)
277 */
278 public static function validateGPS ( $info, &$val, $standalone ) {
279 if ( !$standalone ) {
280 return;
281 }
282
283 $m = array();
284 if ( preg_match(
285 '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
286 $val, $m )
287 ) {
288 $coord = intval( $m[1] );
289 $coord += intval( $m[2] ) * (1/60);
290 $coord += intval( $m[3] ) * (1/3600);
291 if ( $m[4] === 'S' || $m[4] === 'W' ) {
292 $coord = -$coord;
293 }
294 $val = $coord;
295 return;
296 } elseif ( preg_match(
297 '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
298 $val, $m )
299 ) {
300 $coord = intval( $m[1] );
301 $coord += floatval( $m[2] ) * (1/60);
302 if ( $m[3] === 'S' || $m[3] === 'W' ) {
303 $coord = -$coord;
304 }
305 $val = $coord;
306 return;
307
308 } else {
309 wfDebugLog( 'XMP', __METHOD__
310 . " Expected GPSCoordinate, but got $val." );
311 $val = null;
312 return;
313 }
314
315 }
316
317 }