3 * This contains some static methods for
4 * validating XMP properties. See XMPInfo and XMPReader classes.
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
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.
15 * These validation functions can also be used to modify the data. See the gps and flash one's
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
23 * function to validate boolean properties ( True or False )
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
29 public static function validateBoolean( $info, &$val, $standalone ) {
31 // this only validates standalone properties, not arrays, etc
34 if ( $val !== 'True' && $val !== 'False' ) {
35 wfDebugLog( 'XMP', __METHOD__
. " Expected True or False but got $val" );
41 * function to validate rational properties ( 12/10 )
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
47 public static function validateRational( $info, &$val, $standalone ) {
49 // this only validates standalone properties, not arrays, etc
52 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
53 wfDebugLog( 'XMP', __METHOD__
. " Expected rational but got $val" );
59 * function to validate rating properties -1, 0-5
61 * if its outside of range put it into range.
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
68 public static function validateRating( $info, &$val, $standalone ) {
70 // this only validates standalone properties, not arrays, etc
73 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
76 wfDebugLog( 'XMP', __METHOD__
. " Expected rating but got $val" );
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)");
90 wfDebugLog( 'XMP', __METHOD__
. " Rating too high, setting to 5");
97 * function to validate integers
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
103 public static function validateInteger( $info, &$val, $standalone ) {
104 if ( !$standalone ) {
105 // this only validates standalone properties, not arrays, etc
108 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
109 wfDebugLog( 'XMP', __METHOD__
. " Expected integer but got $val" );
115 * function to validate properties with a fixed number of allowed
116 * choices. (closed choice)
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
122 public static function validateClosed( $info, &$val, $standalone ) {
123 if ( !$standalone ) {
124 // this only validates standalone properties, not arrays, etc
128 //check if its in a numeric range
130 if ( isset( $info['rangeLow'] )
131 && isset( $info['rangeHigh'] )
132 && is_numeric( $val )
133 && ( intval( $val ) <= $info['rangeHigh'] )
134 && ( intval( $val ) >= $info['rangeLow'] )
139 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
140 wfDebugLog( 'XMP', __METHOD__
. " Expected closed choice, but got $val" );
145 * function to validate and modify flash structure
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
151 public static function validateFlash( $info, &$val, $standalone ) {
153 // this only validates flash structs, not individual properties
156 if ( !( isset( $val['Fired'] )
157 && isset( $val['Function'] )
158 && isset( $val['Mode'] )
159 && isset( $val['RedEyeMode'] )
160 && isset( $val['Return'] )
162 wfDebugLog( 'XMP', __METHOD__
. " Flash structure did not have all the required components" );
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 ) );
173 * function to validate LangCode properties ( en-GB, etc )
175 * This is just a naive check to make sure it somewhat looks like a lang code.
178 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf page 30 (section 8.2.2.5)
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
184 public static function validateLangCode( $info, &$val, $standalone ) {
185 if ( !$standalone ) {
186 // this only validates standalone properties, not arrays, etc
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" );
197 * function to validate date properties, and convert to Exif format.
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
203 public static function validateDate( $info, &$val, $standalone ) {
204 if ( !$standalone ) {
205 // this only validates standalone properties, not arrays, etc
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'
214 wfDebugLog( 'XMP', __METHOD__
. " Expected date but got $val" );
218 * $res is formatted as follows:
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.
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.
231 if ( $res[1] === '0000' ) {
232 wfDebugLog( 'XMP', __METHOD__
. " Invalid date (year 0): $val" );
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];
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';
248 if ( !isset( $res[7] ) ||
$res[7] === 'Z' ) {
249 $val = $res[1] . ':' . $res[2] . ':' . $res[3]
250 . ' ' . $res[4] . ':' . $res[5] . ':' . $res[6];
254 //do timezone processing. We've already done the case that tz = Z.
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 ) === '-' ) {
262 $val = wfTimestamp( TS_EXIF
, $unix +
$offset );
266 /** function to validate, and more importantly
267 * translate the XMP DMS form of gps coords to
268 * the decimal form we use.
270 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf
271 * section 1.2.7.4 on page 23
273 * @param $info Array unused (info about prop)
274 * @param &$val String GPS string in either DDD,MM,SSk or
276 * @param $standalone Boolean if its a simple prop (should always be true)
278 public static function validateGPS ( $info, &$val, $standalone ) {
279 if ( !$standalone ) {
285 '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
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' ) {
296 } elseif ( preg_match(
297 '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
300 $coord = intval( $m[1] );
301 $coord +
= floatval( $m[2] ) * (1/60);
302 if ( $m[3] === 'S' ||
$m[3] === 'W' ) {
309 wfDebugLog( 'XMP', __METHOD__
310 . " Expected GPSCoordinate, but got $val." );