MediaWiki  REL1_20
XMPValidate.php
Go to the documentation of this file.
00001 <?php
00043 class XMPValidate {
00051         public static function validateBoolean( $info, &$val, $standalone ) {
00052                 if ( !$standalone ) {
00053                         // this only validates standalone properties, not arrays, etc
00054                         return;
00055                 }
00056                 if ( $val !== 'True' && $val !== 'False' ) {
00057                         wfDebugLog( 'XMP', __METHOD__ . " Expected True or False but got $val" );
00058                         $val = null;
00059                 }
00060 
00061         }
00062 
00070         public static function validateRational( $info, &$val, $standalone ) {
00071                 if ( !$standalone ) {
00072                         // this only validates standalone properties, not arrays, etc
00073                         return;
00074                 }
00075                 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
00076                         wfDebugLog( 'XMP', __METHOD__ . " Expected rational but got $val" );
00077                         $val = null;
00078                 }
00079 
00080         }
00081 
00092         public static function validateRating( $info, &$val, $standalone ) {
00093                 if ( !$standalone ) {
00094                         // this only validates standalone properties, not arrays, etc
00095                         return;
00096                 }
00097                 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
00098                         || !is_numeric($val)
00099                  ) {
00100                         wfDebugLog( 'XMP', __METHOD__ . " Expected rating but got $val" );
00101                         $val = null;
00102                         return;
00103                 } else {
00104                         $nVal = (float) $val;
00105                         if ( $nVal < 0 ) {
00106                                 // We do < 0 here instead of < -1 here, since
00107                                 // the values between 0 and -1 are also illegal
00108                                 // as -1 is meant as a special reject rating.
00109                                 wfDebugLog( 'XMP', __METHOD__ . " Rating too low, setting to -1 (Rejected)");
00110                                 $val = '-1';
00111                                 return;
00112                         }
00113                         if ( $nVal > 5 ) {
00114                                 wfDebugLog( 'XMP', __METHOD__ . " Rating too high, setting to 5");
00115                                 $val = '5';
00116                                 return;
00117                         }
00118                 }
00119         }
00120 
00128         public static function validateInteger( $info, &$val, $standalone ) {
00129                 if ( !$standalone ) {
00130                         // this only validates standalone properties, not arrays, etc
00131                         return;
00132                 }
00133                 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
00134                         wfDebugLog( 'XMP', __METHOD__ . " Expected integer but got $val" );
00135                         $val = null;
00136                 }
00137 
00138         }
00139 
00148         public static function validateClosed( $info, &$val, $standalone ) {
00149                 if ( !$standalone ) {
00150                         // this only validates standalone properties, not arrays, etc
00151                         return;
00152                 }
00153 
00154                 //check if its in a numeric range
00155                 $inRange = false;
00156                 if ( isset( $info['rangeLow'] ) 
00157                         && isset( $info['rangeHigh'] )
00158                         && is_numeric( $val )
00159                         && ( intval( $val ) <= $info['rangeHigh'] )
00160                         && ( intval( $val ) >= $info['rangeLow'] )
00161                 ) {
00162                         $inRange = true;
00163                 }
00164 
00165                 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
00166                         wfDebugLog( 'XMP', __METHOD__ . " Expected closed choice, but got $val" );
00167                         $val = null;
00168                 }
00169         }
00170 
00178         public static function validateFlash( $info, &$val, $standalone ) {
00179                 if ( $standalone ) {
00180                         // this only validates flash structs, not individual properties
00181                         return;
00182                 }
00183                 if ( !( isset( $val['Fired'] )
00184                         && isset( $val['Function'] )
00185                         && isset( $val['Mode'] )
00186                         && isset( $val['RedEyeMode'] )
00187                         && isset( $val['Return'] )
00188                 ) ) {
00189                         wfDebugLog( 'XMP', __METHOD__ . " Flash structure did not have all the required components" );
00190                         $val = null;
00191                 } else {
00192                         $val = ( "\0" | ( $val['Fired'] === 'True' )
00193                                 | ( intval( $val['Return'] ) << 1 )
00194                                 | ( intval( $val['Mode'] ) << 3 )
00195                                 | ( ( $val['Function'] === 'True' ) << 5 )
00196                                 | ( ( $val['RedEyeMode'] === 'True' ) << 6 ) );
00197                 }
00198         }
00199 
00212         public static function validateLangCode( $info, &$val, $standalone ) {
00213                 if ( !$standalone ) {
00214                         // this only validates standalone properties, not arrays, etc
00215                         return;
00216                 }
00217                 if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val) ) {
00218                         //this is a rather naive check.
00219                         wfDebugLog( 'XMP', __METHOD__ . " Expected Lang code but got $val" );
00220                         $val = null;
00221                 }
00222 
00223         }
00224 
00242         public static function validateDate( $info, &$val, $standalone ) {
00243                 if ( !$standalone ) {
00244                         // this only validates standalone properties, not arrays, etc
00245                         return;
00246                 }
00247                 $res = array();
00248                 if ( !preg_match(
00249                         /* ahh! scary regex... */
00250                         '/^([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'
00251                         , $val, $res)
00252                 ) {
00253                         wfDebugLog( 'XMP', __METHOD__ . " Expected date but got $val" );
00254                         $val = null;
00255                 } else {
00256                         /*
00257                          * $res is formatted as follows:
00258                          * 0 -> full date.
00259                          * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second
00260                          * 7-> Timezone specifier (Z or something like +12:30 )
00261                          * many parts are optional, some aren't. For example if you specify
00262                          * minute, you must specify hour, day, month, and year but not second or TZ.
00263                          */
00264 
00265                         /*
00266                          * First of all, if year = 0000, Something is wrongish,
00267                          * so don't extract. This seems to happen when
00268                          * some programs convert between metadata formats.
00269                          */
00270                         if ( $res[1] === '0000' ) {
00271                                 wfDebugLog( 'XMP', __METHOD__ . " Invalid date (year 0): $val" );
00272                                 $val = null;
00273                                 return;
00274                         }
00275 
00276                         if ( !isset( $res[4] ) ) { //hour
00277                                 //just have the year month day (if that)
00278                                 $val = $res[1];
00279                                 if ( isset( $res[2] ) ) {
00280                                         $val .= ':' . $res[2];
00281                                 }
00282                                 if ( isset( $res[3] ) ) {
00283                                         $val .= ':' . $res[3];
00284                                 }
00285                                 return;
00286                         }
00287 
00288                         if ( !isset( $res[7] ) || $res[7] === 'Z' ) {
00289                                 //if hour is set, then minute must also be or regex above will fail.
00290                                 $val = $res[1] . ':' . $res[2] . ':' . $res[3]
00291                                         . ' ' . $res[4] . ':' . $res[5];
00292                                 if ( isset( $res[6] ) && $res[6] !== '' ) {
00293                                         $val .= ':' . $res[6];
00294                                 }
00295                                 return;
00296                         }
00297 
00298 
00299                         // Extra check for empty string necessary due to TZ but no second case.
00300                         $stripSeconds = false;
00301                         if ( !isset( $res[6] ) || $res[6] === '' ) {
00302                                 $res[6] = '00';
00303                                 $stripSeconds = true;
00304                         }
00305 
00306                         // Do timezone processing. We've already done the case that tz = Z.
00307 
00308                         // We know that if we got to this step, year, month day hour and min must be set
00309                         // by virtue of regex not failing.
00310 
00311                         $unix = wfTimestamp( TS_UNIX, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] );
00312                         $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
00313                         $offset += intval( substr( $res[7], 4, 2 ) ) * 60;
00314                         if ( substr( $res[7], 0, 1 ) === '-' ) {
00315                                 $offset = -$offset;
00316                         }
00317                         $val = wfTimestamp( TS_EXIF, $unix + $offset );
00318 
00319                         if ( $stripSeconds ) {
00320                                 // If seconds weren't specified, remove the trailing ':00'.
00321                                 $val = substr( $val, 0, -3 );
00322                         }
00323                 }
00324 
00325         }
00326 
00339         public static function validateGPS ( $info, &$val, $standalone ) {
00340                 if ( !$standalone ) {
00341                         return;
00342                 }
00343 
00344                 $m = array();
00345                 if ( preg_match( 
00346                         '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
00347                         $val, $m )
00348                 ) {
00349                         $coord = intval( $m[1] );
00350                         $coord += intval( $m[2] ) * (1/60);
00351                         $coord += intval( $m[3] ) * (1/3600);
00352                         if ( $m[4] === 'S' || $m[4] === 'W' ) {
00353                                 $coord = -$coord;
00354                         }
00355                         $val = $coord;
00356                         return;
00357                 } elseif ( preg_match( 
00358                         '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
00359                         $val, $m )
00360                 ) {
00361                         $coord = intval( $m[1] );
00362                         $coord += floatval( $m[2] ) * (1/60);
00363                         if ( $m[3] === 'S' || $m[3] === 'W' ) {
00364                                 $coord = -$coord;
00365                         }
00366                         $val = $coord;
00367                         return;
00368 
00369                 } else {
00370                         wfDebugLog( 'XMP', __METHOD__ 
00371                                 . " Expected GPSCoordinate, but got $val." );
00372                         $val = null;
00373                         return;
00374                 }
00375         }
00376 }