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
;
26 use Wikimedia\Timestamp\ConvertibleTimestamp
;
29 * This contains some static methods for
30 * validating XMP properties. See XMPInfo and XMPReader classes.
32 * Each of these functions take the same parameters
33 * * an info array which is a subset of the XMPInfo::items array
34 * * A value (passed as reference) to validate. This can be either a
35 * simple value or an array
36 * * A boolean to determine if this is validating a simple or complex values
38 * It should be noted that when an array is being validated, typically the validation
39 * function is called once for each value, and then once at the end for the entire array.
41 * These validation functions can also be used to modify the data. See the gps and flash one's
44 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart1.pdf starting at pg 28
45 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf starting at pg 11
47 class XMPValidate
implements LoggerAwareInterface
{
50 * @var LoggerInterface
54 public function __construct( LoggerInterface
$logger ) {
55 $this->setLogger( $logger );
58 public function setLogger( LoggerInterface
$logger ) {
59 $this->logger
= $logger;
62 * Function to validate boolean properties ( True or False )
64 * @param array $info Information about current property
65 * @param mixed &$val Current value to validate
66 * @param bool $standalone If this is a simple property or array
68 public function validateBoolean( $info, &$val, $standalone ) {
70 // this only validates standalone properties, not arrays, etc
73 if ( $val !== 'True' && $val !== 'False' ) {
74 $this->logger
->info( __METHOD__
. " Expected True or False but got $val" );
80 * function to validate rational properties ( 12/10 )
82 * @param array $info Information about current property
83 * @param mixed &$val Current value to validate
84 * @param bool $standalone If this is a simple property or array
86 public function validateRational( $info, &$val, $standalone ) {
88 // this only validates standalone properties, not arrays, etc
91 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
92 $this->logger
->info( __METHOD__
. " Expected rational but got $val" );
98 * function to validate rating properties -1, 0-5
100 * if its outside of range put it into range.
103 * @param array $info Information about current property
104 * @param mixed &$val Current value to validate
105 * @param bool $standalone If this is a simple property or array
107 public function validateRating( $info, &$val, $standalone ) {
108 if ( !$standalone ) {
109 // this only validates standalone properties, not arrays, etc
112 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
113 ||
!is_numeric( $val )
115 $this->logger
->info( __METHOD__
. " Expected rating but got $val" );
122 // We do < 0 here instead of < -1 here, since
123 // the values between 0 and -1 are also illegal
124 // as -1 is meant as a special reject rating.
125 $this->logger
->info( __METHOD__
. " Rating too low, setting to -1 (Rejected)" );
131 $this->logger
->info( __METHOD__
. " Rating too high, setting to 5" );
140 * function to validate integers
142 * @param array $info Information about current property
143 * @param mixed &$val Current value to validate
144 * @param bool $standalone If this is a simple property or array
146 public function validateInteger( $info, &$val, $standalone ) {
147 if ( !$standalone ) {
148 // this only validates standalone properties, not arrays, etc
151 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
152 $this->logger
->info( __METHOD__
. " Expected integer but got $val" );
158 * function to validate properties with a fixed number of allowed
159 * choices. (closed choice)
161 * @param array $info Information about current property
162 * @param mixed &$val Current value to validate
163 * @param bool $standalone If this is a simple property or array
165 public function validateClosed( $info, &$val, $standalone ) {
166 if ( !$standalone ) {
167 // this only validates standalone properties, not arrays, etc
171 // check if its in a numeric range
173 if ( isset( $info['rangeLow'] )
174 && isset( $info['rangeHigh'] )
175 && is_numeric( $val )
176 && ( intval( $val ) <= $info['rangeHigh'] )
177 && ( intval( $val ) >= $info['rangeLow'] )
182 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
183 $this->logger
->info( __METHOD__
. " Expected closed choice, but got $val" );
189 * function to validate and modify flash structure
191 * @param array $info Information about current property
192 * @param mixed &$val Current value to validate
193 * @param bool $standalone If this is a simple property or array
195 public function validateFlash( $info, &$val, $standalone ) {
197 // this only validates flash structs, not individual properties
200 if ( !( isset( $val['Fired'] )
201 && isset( $val['Function'] )
202 && isset( $val['Mode'] )
203 && isset( $val['RedEyeMode'] )
204 && isset( $val['Return'] )
206 $this->logger
->info( __METHOD__
. " Flash structure did not have all the required components" );
209 $val = ( "\0" |
( $val['Fired'] === 'True' )
210 |
( intval( $val['Return'] ) << 1 )
211 |
( intval( $val['Mode'] ) << 3 )
212 |
( ( $val['Function'] === 'True' ) << 5 )
213 |
( ( $val['RedEyeMode'] === 'True' ) << 6 ) );
218 * function to validate LangCode properties ( en-GB, etc )
220 * This is just a naive check to make sure it somewhat looks like a lang code.
223 * @see https://wwwimages2.adobe.com/content/dam/Adobe/en/devnet/xmp/pdfs/
224 * XMP%20SDK%20Release%20cc-2014-12/XMPSpecificationPart1.pdf page 22 (section 8.2.2.4)
226 * @param array $info Information about current property
227 * @param mixed &$val Current value to validate
228 * @param bool $standalone If this is a simple property or array
230 public function validateLangCode( $info, &$val, $standalone ) {
231 if ( !$standalone ) {
232 // this only validates standalone properties, not arrays, etc
235 if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val ) ) {
236 // this is a rather naive check.
237 $this->logger
->info( __METHOD__
. " Expected Lang code but got $val" );
243 * function to validate date properties, and convert to (partial) Exif format.
245 * Dates can be one of the following formats:
249 * YYYY-MM-DDThh:mmTZD
250 * YYYY-MM-DDThh:mm:ssTZD
251 * YYYY-MM-DDThh:mm:ss.sTZD
253 * @param array $info Information about current property
254 * @param mixed &$val Current value to validate. Converts to TS_EXIF as a side-effect.
255 * in cases where there's only a partial date, it will give things like
257 * @param bool $standalone If this is a simple property or array
259 public function validateDate( $info, &$val, $standalone ) {
260 if ( !$standalone ) {
261 // this only validates standalone properties, not arrays, etc
265 // @codingStandardsIgnoreStart Long line that cannot be broken
267 /* ahh! scary regex... */
268 '/^([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',
271 // @codingStandardsIgnoreEnd
273 $this->logger
->info( __METHOD__
. " Expected date but got $val" );
277 * $res is formatted as follows:
279 * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second
280 * 7-> Timezone specifier (Z or something like +12:30 )
281 * many parts are optional, some aren't. For example if you specify
282 * minute, you must specify hour, day, month, and year but not second or TZ.
286 * First of all, if year = 0000, Something is wrongish,
287 * so don't extract. This seems to happen when
288 * some programs convert between metadata formats.
290 if ( $res[1] === '0000' ) {
291 $this->logger
->info( __METHOD__
. " Invalid date (year 0): $val" );
297 if ( !isset( $res[4] ) ) { // hour
298 // just have the year month day (if that)
300 if ( isset( $res[2] ) ) {
301 $val .= ':' . $res[2];
303 if ( isset( $res[3] ) ) {
304 $val .= ':' . $res[3];
310 if ( !isset( $res[7] ) ||
$res[7] === 'Z' ) {
311 // if hour is set, then minute must also be or regex above will fail.
312 $val = $res[1] . ':' . $res[2] . ':' . $res[3]
313 . ' ' . $res[4] . ':' . $res[5];
314 if ( isset( $res[6] ) && $res[6] !== '' ) {
315 $val .= ':' . $res[6];
321 // Extra check for empty string necessary due to TZ but no second case.
322 $stripSeconds = false;
323 if ( !isset( $res[6] ) ||
$res[6] === '' ) {
325 $stripSeconds = true;
328 // Do timezone processing. We've already done the case that tz = Z.
330 // We know that if we got to this step, year, month day hour and min must be set
331 // by virtue of regex not failing.
333 $unix = ConvertibleTimestamp
::convert( TS_UNIX
,
334 $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6]
336 $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
337 $offset +
= intval( substr( $res[7], 4, 2 ) ) * 60;
338 if ( substr( $res[7], 0, 1 ) === '-' ) {
341 $val = ConvertibleTimestamp
::convert( TS_EXIF
, $unix +
$offset );
343 if ( $stripSeconds ) {
344 // If seconds weren't specified, remove the trailing ':00'.
345 $val = substr( $val, 0, -3 );
350 /** function to validate, and more importantly
351 * translate the XMP DMS form of gps coords to
352 * the decimal form we use.
354 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf
355 * section 1.2.7.4 on page 23
357 * @param array $info Unused (info about prop)
358 * @param string &$val GPS string in either DDD,MM,SSk or
360 * @param bool $standalone If its a simple prop (should always be true)
362 public function validateGPS( $info, &$val, $standalone ) {
363 if ( !$standalone ) {
369 '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
372 $coord = intval( $m[1] );
373 $coord +
= intval( $m[2] ) * ( 1 / 60 );
374 $coord +
= intval( $m[3] ) * ( 1 / 3600 );
375 if ( $m[4] === 'S' ||
$m[4] === 'W' ) {
381 } elseif ( preg_match(
382 '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
385 $coord = intval( $m[1] );
386 $coord +
= floatval( $m[2] ) * ( 1 / 60 );
387 if ( $m[3] === 'S' ||
$m[3] === 'W' ) {
394 $this->logger
->info( __METHOD__
395 . " Expected GPSCoordinate, but got $val." );