3 * Methods for validating XMP properties.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use Psr\Log\LoggerInterface
;
25 use Psr\Log\LoggerAwareInterface
;
28 * This contains some static methods for
29 * validating XMP properties. See XMPInfo and XMPReader classes.
31 * Each of these functions take the same parameters
32 * * an info array which is a subset of the XMPInfo::items array
33 * * A value (passed as reference) to validate. This can be either a
34 * simple value or an array
35 * * A boolean to determine if this is validating a simple or complex values
37 * It should be noted that when an array is being validated, typically the validation
38 * function is called once for each value, and then once at the end for the entire array.
40 * These validation functions can also be used to modify the data. See the gps and flash one's
43 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf starting at pg 28
44 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf starting at pg 11
46 class XMPValidate
implements LoggerAwareInterface
{
49 * @var LoggerInterface
53 public function __construct( LoggerInterface
$logger ) {
54 $this->setLogger( $logger );
57 public function setLogger( LoggerInterface
$logger ) {
58 $this->logger
= $logger;
61 * Function to validate boolean properties ( True or False )
63 * @param array $info Information about current property
64 * @param mixed &$val Current value to validate
65 * @param bool $standalone If this is a simple property or array
67 public function validateBoolean( $info, &$val, $standalone ) {
69 // this only validates standalone properties, not arrays, etc
72 if ( $val !== 'True' && $val !== 'False' ) {
73 $this->logger
->info( __METHOD__
. " Expected True or False but got $val" );
79 * function to validate rational properties ( 12/10 )
81 * @param array $info Information about current property
82 * @param mixed &$val Current value to validate
83 * @param bool $standalone If this is a simple property or array
85 public function validateRational( $info, &$val, $standalone ) {
87 // this only validates standalone properties, not arrays, etc
90 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
91 $this->logger
->info( __METHOD__
. " Expected rational but got $val" );
97 * function to validate rating properties -1, 0-5
99 * if its outside of range put it into range.
102 * @param array $info Information about current property
103 * @param mixed &$val Current value to validate
104 * @param bool $standalone If this is a simple property or array
106 public function validateRating( $info, &$val, $standalone ) {
107 if ( !$standalone ) {
108 // this only validates standalone properties, not arrays, etc
111 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
112 ||
!is_numeric( $val )
114 $this->logger
->info( __METHOD__
. " Expected rating but got $val" );
121 // We do < 0 here instead of < -1 here, since
122 // the values between 0 and -1 are also illegal
123 // as -1 is meant as a special reject rating.
124 $this->logger
->info( __METHOD__
. " Rating too low, setting to -1 (Rejected)" );
130 $this->logger
->info( __METHOD__
. " Rating too high, setting to 5" );
139 * function to validate integers
141 * @param array $info Information about current property
142 * @param mixed &$val Current value to validate
143 * @param bool $standalone If this is a simple property or array
145 public function validateInteger( $info, &$val, $standalone ) {
146 if ( !$standalone ) {
147 // this only validates standalone properties, not arrays, etc
150 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
151 $this->logger
->info( __METHOD__
. " Expected integer but got $val" );
157 * function to validate properties with a fixed number of allowed
158 * choices. (closed choice)
160 * @param array $info Information about current property
161 * @param mixed &$val Current value to validate
162 * @param bool $standalone If this is a simple property or array
164 public function validateClosed( $info, &$val, $standalone ) {
165 if ( !$standalone ) {
166 // this only validates standalone properties, not arrays, etc
170 // check if its in a numeric range
172 if ( isset( $info['rangeLow'] )
173 && isset( $info['rangeHigh'] )
174 && is_numeric( $val )
175 && ( intval( $val ) <= $info['rangeHigh'] )
176 && ( intval( $val ) >= $info['rangeLow'] )
181 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
182 $this->logger
->info( __METHOD__
. " Expected closed choice, but got $val" );
188 * function to validate and modify flash structure
190 * @param array $info Information about current property
191 * @param mixed &$val Current value to validate
192 * @param bool $standalone If this is a simple property or array
194 public function validateFlash( $info, &$val, $standalone ) {
196 // this only validates flash structs, not individual properties
199 if ( !( isset( $val['Fired'] )
200 && isset( $val['Function'] )
201 && isset( $val['Mode'] )
202 && isset( $val['RedEyeMode'] )
203 && isset( $val['Return'] )
205 $this->logger
->info( __METHOD__
. " Flash structure did not have all the required components" );
208 $val = ( "\0" |
( $val['Fired'] === 'True' )
209 |
( intval( $val['Return'] ) << 1 )
210 |
( intval( $val['Mode'] ) << 3 )
211 |
( ( $val['Function'] === 'True' ) << 5 )
212 |
( ( $val['RedEyeMode'] === 'True' ) << 6 ) );
217 * function to validate LangCode properties ( en-GB, etc )
219 * This is just a naive check to make sure it somewhat looks like a lang code.
222 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf page 30 (section 8.2.2.5)
224 * @param array $info Information about current property
225 * @param mixed &$val Current value to validate
226 * @param bool $standalone If this is a simple property or array
228 public function validateLangCode( $info, &$val, $standalone ) {
229 if ( !$standalone ) {
230 // this only validates standalone properties, not arrays, etc
233 if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val ) ) {
234 // this is a rather naive check.
235 $this->logger
->info( __METHOD__
. " Expected Lang code but got $val" );
241 * function to validate date properties, and convert to (partial) Exif format.
243 * Dates can be one of the following formats:
247 * YYYY-MM-DDThh:mmTZD
248 * YYYY-MM-DDThh:mm:ssTZD
249 * YYYY-MM-DDThh:mm:ss.sTZD
251 * @param array $info Information about current property
252 * @param mixed &$val Current value to validate. Converts to TS_EXIF as a side-effect.
253 * in cases where there's only a partial date, it will give things like
255 * @param bool $standalone If this is a simple property or array
257 public function validateDate( $info, &$val, $standalone ) {
258 if ( !$standalone ) {
259 // this only validates standalone properties, not arrays, etc
263 // @codingStandardsIgnoreStart Long line that cannot be broken
265 /* ahh! scary regex... */
266 '/^([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',
269 // @codingStandardsIgnoreEnd
271 $this->logger
->info( __METHOD__
. " Expected date but got $val" );
275 * $res is formatted as follows:
277 * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second
278 * 7-> Timezone specifier (Z or something like +12:30 )
279 * many parts are optional, some aren't. For example if you specify
280 * minute, you must specify hour, day, month, and year but not second or TZ.
284 * First of all, if year = 0000, Something is wrongish,
285 * so don't extract. This seems to happen when
286 * some programs convert between metadata formats.
288 if ( $res[1] === '0000' ) {
289 $this->logger
->info( __METHOD__
. " Invalid date (year 0): $val" );
295 if ( !isset( $res[4] ) ) { // hour
296 // just have the year month day (if that)
298 if ( isset( $res[2] ) ) {
299 $val .= ':' . $res[2];
301 if ( isset( $res[3] ) ) {
302 $val .= ':' . $res[3];
308 if ( !isset( $res[7] ) ||
$res[7] === 'Z' ) {
309 // if hour is set, then minute must also be or regex above will fail.
310 $val = $res[1] . ':' . $res[2] . ':' . $res[3]
311 . ' ' . $res[4] . ':' . $res[5];
312 if ( isset( $res[6] ) && $res[6] !== '' ) {
313 $val .= ':' . $res[6];
319 // Extra check for empty string necessary due to TZ but no second case.
320 $stripSeconds = false;
321 if ( !isset( $res[6] ) ||
$res[6] === '' ) {
323 $stripSeconds = true;
326 // Do timezone processing. We've already done the case that tz = Z.
328 // We know that if we got to this step, year, month day hour and min must be set
329 // by virtue of regex not failing.
331 $unix = wfTimestamp( TS_UNIX
, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] );
332 $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
333 $offset +
= intval( substr( $res[7], 4, 2 ) ) * 60;
334 if ( substr( $res[7], 0, 1 ) === '-' ) {
337 $val = wfTimestamp( TS_EXIF
, $unix +
$offset );
339 if ( $stripSeconds ) {
340 // If seconds weren't specified, remove the trailing ':00'.
341 $val = substr( $val, 0, -3 );
346 /** function to validate, and more importantly
347 * translate the XMP DMS form of gps coords to
348 * the decimal form we use.
350 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf
351 * section 1.2.7.4 on page 23
353 * @param array $info Unused (info about prop)
354 * @param string &$val GPS string in either DDD,MM,SSk or
356 * @param bool $standalone If its a simple prop (should always be true)
358 public function validateGPS( $info, &$val, $standalone ) {
359 if ( !$standalone ) {
365 '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
368 $coord = intval( $m[1] );
369 $coord +
= intval( $m[2] ) * ( 1 / 60 );
370 $coord +
= intval( $m[3] ) * ( 1 / 3600 );
371 if ( $m[4] === 'S' ||
$m[4] === 'W' ) {
377 } elseif ( preg_match(
378 '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
381 $coord = intval( $m[1] );
382 $coord +
= floatval( $m[2] ) * ( 1 / 60 );
383 if ( $m[3] === 'S' ||
$m[3] === 'W' ) {
390 $this->logger
->info( __METHOD__
391 . " Expected GPSCoordinate, but got $val." );