MediaWiki  REL1_19
XMPValidate.php
Go to the documentation of this file.
00001 <?php
00021 class XMPValidate {
00029         public static function validateBoolean( $info, &$val, $standalone ) {
00030                 if ( !$standalone ) {
00031                         // this only validates standalone properties, not arrays, etc
00032                         return;
00033                 }
00034                 if ( $val !== 'True' && $val !== 'False' ) {
00035                         wfDebugLog( 'XMP', __METHOD__ . " Expected True or False but got $val" );
00036                         $val = null;
00037                 }
00038 
00039         }
00040 
00048         public static function validateRational( $info, &$val, $standalone ) {
00049                 if ( !$standalone ) {
00050                         // this only validates standalone properties, not arrays, etc
00051                         return;
00052                 }
00053                 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
00054                         wfDebugLog( 'XMP', __METHOD__ . " Expected rational but got $val" );
00055                         $val = null;
00056                 }
00057 
00058         }
00059 
00070         public static function validateRating( $info, &$val, $standalone ) {
00071                 if ( !$standalone ) {
00072                         // this only validates standalone properties, not arrays, etc
00073                         return;
00074                 }
00075                 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
00076                         || !is_numeric($val)
00077                  ) {
00078                         wfDebugLog( 'XMP', __METHOD__ . " Expected rating but got $val" );
00079                         $val = null;
00080                         return;
00081                 } else {
00082                         $nVal = (float) $val;
00083                         if ( $nVal < 0 ) {
00084                                 // We do < 0 here instead of < -1 here, since
00085                                 // the values between 0 and -1 are also illegal
00086                                 // as -1 is meant as a special reject rating.
00087                                 wfDebugLog( 'XMP', __METHOD__ . " Rating too low, setting to -1 (Rejected)");
00088                                 $val = '-1';
00089                                 return;
00090                         }
00091                         if ( $nVal > 5 ) {
00092                                 wfDebugLog( 'XMP', __METHOD__ . " Rating too high, setting to 5");
00093                                 $val = '5';
00094                                 return;
00095                         }
00096                 }
00097         }
00098 
00106         public static function validateInteger( $info, &$val, $standalone ) {
00107                 if ( !$standalone ) {
00108                         // this only validates standalone properties, not arrays, etc
00109                         return;
00110                 }
00111                 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
00112                         wfDebugLog( 'XMP', __METHOD__ . " Expected integer but got $val" );
00113                         $val = null;
00114                 }
00115 
00116         }
00117 
00126         public static function validateClosed( $info, &$val, $standalone ) {
00127                 if ( !$standalone ) {
00128                         // this only validates standalone properties, not arrays, etc
00129                         return;
00130                 }
00131 
00132                 //check if its in a numeric range
00133                 $inRange = false;
00134                 if ( isset( $info['rangeLow'] ) 
00135                         && isset( $info['rangeHigh'] )
00136                         && is_numeric( $val )
00137                         && ( intval( $val ) <= $info['rangeHigh'] )
00138                         && ( intval( $val ) >= $info['rangeLow'] )
00139                 ) {
00140                         $inRange = true;
00141                 }
00142 
00143                 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
00144                         wfDebugLog( 'XMP', __METHOD__ . " Expected closed choice, but got $val" );
00145                         $val = null;
00146                 }
00147         }
00148 
00156         public static function validateFlash( $info, &$val, $standalone ) {
00157                 if ( $standalone ) {
00158                         // this only validates flash structs, not individual properties
00159                         return;
00160                 }
00161                 if ( !( isset( $val['Fired'] )
00162                         && isset( $val['Function'] )
00163                         && isset( $val['Mode'] )
00164                         && isset( $val['RedEyeMode'] )
00165                         && isset( $val['Return'] )
00166                 ) ) {
00167                         wfDebugLog( 'XMP', __METHOD__ . " Flash structure did not have all the required components" );
00168                         $val = null;
00169                 } else {
00170                         $val = ( "\0" | ( $val['Fired'] === 'True' )
00171                                 | ( intval( $val['Return'] ) << 1 )
00172                                 | ( intval( $val['Mode'] ) << 3 )
00173                                 | ( ( $val['Function'] === 'True' ) << 5 )
00174                                 | ( ( $val['RedEyeMode'] === 'True' ) << 6 ) );
00175                 }
00176         }
00177 
00190         public static function validateLangCode( $info, &$val, $standalone ) {
00191                 if ( !$standalone ) {
00192                         // this only validates standalone properties, not arrays, etc
00193                         return;
00194                 }
00195                 if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val) ) {
00196                         //this is a rather naive check.
00197                         wfDebugLog( 'XMP', __METHOD__ . " Expected Lang code but got $val" );
00198                         $val = null;
00199                 }
00200 
00201         }
00202 
00220         public static function validateDate( $info, &$val, $standalone ) {
00221                 if ( !$standalone ) {
00222                         // this only validates standalone properties, not arrays, etc
00223                         return;
00224                 }
00225                 $res = array();
00226                 if ( !preg_match(
00227                         /* ahh! scary regex... */
00228                         '/^([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'
00229                         , $val, $res)
00230                 ) {
00231                         wfDebugLog( 'XMP', __METHOD__ . " Expected date but got $val" );
00232                         $val = null;
00233                 } else {
00234                         /*
00235                          * $res is formatted as follows:
00236                          * 0 -> full date.
00237                          * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second
00238                          * 7-> Timezone specifier (Z or something like +12:30 )
00239                          * many parts are optional, some aren't. For example if you specify
00240                          * minute, you must specify hour, day, month, and year but not second or TZ.
00241                          */
00242 
00243                         /*
00244                          * First of all, if year = 0000, Something is wrongish,
00245                          * so don't extract. This seems to happen when
00246                          * some programs convert between metadata formats.
00247                          */
00248                         if ( $res[1] === '0000' ) {
00249                                 wfDebugLog( 'XMP', __METHOD__ . " Invalid date (year 0): $val" );
00250                                 $val = null;
00251                                 return;
00252                         }
00253 
00254                         if ( !isset( $res[4] ) ) { //hour
00255                                 //just have the year month day (if that)
00256                                 $val = $res[1];
00257                                 if ( isset( $res[2] ) ) {
00258                                         $val .= ':' . $res[2];
00259                                 }
00260                                 if ( isset( $res[3] ) ) {
00261                                         $val .= ':' . $res[3];
00262                                 }
00263                                 return;
00264                         }
00265 
00266                         if ( !isset( $res[7] ) || $res[7] === 'Z' ) {
00267                                 //if hour is set, then minute must also be or regex above will fail.
00268                                 $val = $res[1] . ':' . $res[2] . ':' . $res[3]
00269                                         . ' ' . $res[4] . ':' . $res[5];
00270                                 if ( isset( $res[6] ) && $res[6] !== '' ) {
00271                                         $val .= ':' . $res[6];
00272                                 }
00273                                 return;
00274                         }
00275 
00276 
00277                         // Extra check for empty string necessary due to TZ but no second case.
00278                         $stripSeconds = false;
00279                         if ( !isset( $res[6] ) || $res[6] === '' ) {
00280                                 $res[6] = '00';
00281                                 $stripSeconds = true;
00282                         }
00283 
00284                         // Do timezone processing. We've already done the case that tz = Z.
00285 
00286                         // We know that if we got to this step, year, month day hour and min must be set
00287                         // by virtue of regex not failing.
00288 
00289                         $unix = wfTimestamp( TS_UNIX, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] );
00290                         $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
00291                         $offset += intval( substr( $res[7], 4, 2 ) ) * 60;
00292                         if ( substr( $res[7], 0, 1 ) === '-' ) {
00293                                 $offset = -$offset;
00294                         }
00295                         $val = wfTimestamp( TS_EXIF, $unix + $offset );
00296 
00297                         if ( $stripSeconds ) {
00298                                 // If seconds weren't specified, remove the trailing ':00'.
00299                                 $val = substr( $val, 0, -3 );
00300                         }
00301                 }
00302 
00303         }
00304 
00317         public static function validateGPS ( $info, &$val, $standalone ) {
00318                 if ( !$standalone ) {
00319                         return;
00320                 }
00321 
00322                 $m = array();
00323                 if ( preg_match( 
00324                         '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
00325                         $val, $m )
00326                 ) {
00327                         $coord = intval( $m[1] );
00328                         $coord += intval( $m[2] ) * (1/60);
00329                         $coord += intval( $m[3] ) * (1/3600);
00330                         if ( $m[4] === 'S' || $m[4] === 'W' ) {
00331                                 $coord = -$coord;
00332                         }
00333                         $val = $coord;
00334                         return;
00335                 } elseif ( preg_match( 
00336                         '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
00337                         $val, $m )
00338                 ) {
00339                         $coord = intval( $m[1] );
00340                         $coord += floatval( $m[2] ) * (1/60);
00341                         if ( $m[3] === 'S' || $m[3] === 'W' ) {
00342                                 $coord = -$coord;
00343                         }
00344                         $val = $coord;
00345                         return;
00346 
00347                 } else {
00348                         wfDebugLog( 'XMP', __METHOD__ 
00349                                 . " Expected GPSCoordinate, but got $val." );
00350                         $val = null;
00351                         return;
00352                 }
00353         }
00354 }