MediaWiki
REL1_24
|
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 00069 public static function validateRational( $info, &$val, $standalone ) { 00070 if ( !$standalone ) { 00071 // this only validates standalone properties, not arrays, etc 00072 return; 00073 } 00074 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) { 00075 wfDebugLog( 'XMP', __METHOD__ . " Expected rational but got $val" ); 00076 $val = null; 00077 } 00078 } 00079 00090 public static function validateRating( $info, &$val, $standalone ) { 00091 if ( !$standalone ) { 00092 // this only validates standalone properties, not arrays, etc 00093 return; 00094 } 00095 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val ) 00096 || !is_numeric( $val ) 00097 ) { 00098 wfDebugLog( 'XMP', __METHOD__ . " Expected rating but got $val" ); 00099 $val = null; 00100 00101 return; 00102 } else { 00103 $nVal = (float)$val; 00104 if ( $nVal < 0 ) { 00105 // We do < 0 here instead of < -1 here, since 00106 // the values between 0 and -1 are also illegal 00107 // as -1 is meant as a special reject rating. 00108 wfDebugLog( 'XMP', __METHOD__ . " Rating too low, setting to -1 (Rejected)" ); 00109 $val = '-1'; 00110 00111 return; 00112 } 00113 if ( $nVal > 5 ) { 00114 wfDebugLog( 'XMP', __METHOD__ . " Rating too high, setting to 5" ); 00115 $val = '5'; 00116 00117 return; 00118 } 00119 } 00120 } 00121 00129 public static function validateInteger( $info, &$val, $standalone ) { 00130 if ( !$standalone ) { 00131 // this only validates standalone properties, not arrays, etc 00132 return; 00133 } 00134 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) { 00135 wfDebugLog( 'XMP', __METHOD__ . " Expected integer but got $val" ); 00136 $val = null; 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 00241 public static function validateDate( $info, &$val, $standalone ) { 00242 if ( !$standalone ) { 00243 // this only validates standalone properties, not arrays, etc 00244 return; 00245 } 00246 $res = array(); 00247 // @codingStandardsIgnoreStart Long line that cannot be broken 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 // @codingStandardsIgnoreEnd 00254 00255 wfDebugLog( 'XMP', __METHOD__ . " Expected date but got $val" ); 00256 $val = null; 00257 } else { 00258 /* 00259 * $res is formatted as follows: 00260 * 0 -> full date. 00261 * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second 00262 * 7-> Timezone specifier (Z or something like +12:30 ) 00263 * many parts are optional, some aren't. For example if you specify 00264 * minute, you must specify hour, day, month, and year but not second or TZ. 00265 */ 00266 00267 /* 00268 * First of all, if year = 0000, Something is wrongish, 00269 * so don't extract. This seems to happen when 00270 * some programs convert between metadata formats. 00271 */ 00272 if ( $res[1] === '0000' ) { 00273 wfDebugLog( 'XMP', __METHOD__ . " Invalid date (year 0): $val" ); 00274 $val = null; 00275 00276 return; 00277 } 00278 00279 if ( !isset( $res[4] ) ) { //hour 00280 //just have the year month day (if that) 00281 $val = $res[1]; 00282 if ( isset( $res[2] ) ) { 00283 $val .= ':' . $res[2]; 00284 } 00285 if ( isset( $res[3] ) ) { 00286 $val .= ':' . $res[3]; 00287 } 00288 00289 return; 00290 } 00291 00292 if ( !isset( $res[7] ) || $res[7] === 'Z' ) { 00293 //if hour is set, then minute must also be or regex above will fail. 00294 $val = $res[1] . ':' . $res[2] . ':' . $res[3] 00295 . ' ' . $res[4] . ':' . $res[5]; 00296 if ( isset( $res[6] ) && $res[6] !== '' ) { 00297 $val .= ':' . $res[6]; 00298 } 00299 00300 return; 00301 } 00302 00303 // Extra check for empty string necessary due to TZ but no second case. 00304 $stripSeconds = false; 00305 if ( !isset( $res[6] ) || $res[6] === '' ) { 00306 $res[6] = '00'; 00307 $stripSeconds = true; 00308 } 00309 00310 // Do timezone processing. We've already done the case that tz = Z. 00311 00312 // We know that if we got to this step, year, month day hour and min must be set 00313 // by virtue of regex not failing. 00314 00315 $unix = wfTimestamp( TS_UNIX, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] ); 00316 $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60; 00317 $offset += intval( substr( $res[7], 4, 2 ) ) * 60; 00318 if ( substr( $res[7], 0, 1 ) === '-' ) { 00319 $offset = -$offset; 00320 } 00321 $val = wfTimestamp( TS_EXIF, $unix + $offset ); 00322 00323 if ( $stripSeconds ) { 00324 // If seconds weren't specified, remove the trailing ':00'. 00325 $val = substr( $val, 0, -3 ); 00326 } 00327 } 00328 } 00329 00342 public static function validateGPS( $info, &$val, $standalone ) { 00343 if ( !$standalone ) { 00344 return; 00345 } 00346 00347 $m = array(); 00348 if ( preg_match( 00349 '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D', 00350 $val, $m ) 00351 ) { 00352 $coord = intval( $m[1] ); 00353 $coord += intval( $m[2] ) * ( 1 / 60 ); 00354 $coord += intval( $m[3] ) * ( 1 / 3600 ); 00355 if ( $m[4] === 'S' || $m[4] === 'W' ) { 00356 $coord = -$coord; 00357 } 00358 $val = $coord; 00359 00360 return; 00361 } elseif ( preg_match( 00362 '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D', 00363 $val, $m ) 00364 ) { 00365 $coord = intval( $m[1] ); 00366 $coord += floatval( $m[2] ) * ( 1 / 60 ); 00367 if ( $m[3] === 'S' || $m[3] === 'W' ) { 00368 $coord = -$coord; 00369 } 00370 $val = $coord; 00371 00372 return; 00373 } else { 00374 wfDebugLog( 'XMP', __METHOD__ 00375 . " Expected GPSCoordinate, but got $val." ); 00376 $val = null; 00377 00378 return; 00379 } 00380 } 00381 }