Merge "Drop index oi_name_archive_name on table oldimage"
[lhc/web/wiklou.git] / includes / libs / xmp / XMPValidate.php
1 <?php
2 /**
3 * Methods for validating XMP properties.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 use Psr\Log\LoggerInterface;
25 use Psr\Log\LoggerAwareInterface;
26 use Wikimedia\Timestamp\ConvertibleTimestamp;
27
28 /**
29 * This contains some static methods for
30 * validating XMP properties. See XMPInfo and XMPReader classes.
31 *
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
37 *
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.
40 *
41 * These validation functions can also be used to modify the data. See the gps and flash one's
42 * for example.
43 *
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
46 */
47 class XMPValidate implements LoggerAwareInterface {
48
49 /**
50 * @var LoggerInterface
51 */
52 private $logger;
53
54 public function __construct( LoggerInterface $logger ) {
55 $this->setLogger( $logger );
56 }
57
58 public function setLogger( LoggerInterface $logger ) {
59 $this->logger = $logger;
60 }
61 /**
62 * Function to validate boolean properties ( True or False )
63 *
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
67 */
68 public function validateBoolean( $info, &$val, $standalone ) {
69 if ( !$standalone ) {
70 // this only validates standalone properties, not arrays, etc
71 return;
72 }
73 if ( $val !== 'True' && $val !== 'False' ) {
74 $this->logger->info( __METHOD__ . " Expected True or False but got $val" );
75 $val = null;
76 }
77 }
78
79 /**
80 * function to validate rational properties ( 12/10 )
81 *
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
85 */
86 public function validateRational( $info, &$val, $standalone ) {
87 if ( !$standalone ) {
88 // this only validates standalone properties, not arrays, etc
89 return;
90 }
91 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
92 $this->logger->info( __METHOD__ . " Expected rational but got $val" );
93 $val = null;
94 }
95 }
96
97 /**
98 * function to validate rating properties -1, 0-5
99 *
100 * if its outside of range put it into range.
101 *
102 * @see MWG spec
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
106 */
107 public function validateRating( $info, &$val, $standalone ) {
108 if ( !$standalone ) {
109 // this only validates standalone properties, not arrays, etc
110 return;
111 }
112 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
113 || !is_numeric( $val )
114 ) {
115 $this->logger->info( __METHOD__ . " Expected rating but got $val" );
116 $val = null;
117
118 return;
119 } else {
120 $nVal = (float)$val;
121 if ( $nVal < 0 ) {
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)" );
126 $val = '-1';
127
128 return;
129 }
130 if ( $nVal > 5 ) {
131 $this->logger->info( __METHOD__ . " Rating too high, setting to 5" );
132 $val = '5';
133
134 return;
135 }
136 }
137 }
138
139 /**
140 * function to validate integers
141 *
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
145 */
146 public function validateInteger( $info, &$val, $standalone ) {
147 if ( !$standalone ) {
148 // this only validates standalone properties, not arrays, etc
149 return;
150 }
151 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
152 $this->logger->info( __METHOD__ . " Expected integer but got $val" );
153 $val = null;
154 }
155 }
156
157 /**
158 * function to validate properties with a fixed number of allowed
159 * choices. (closed choice)
160 *
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
164 */
165 public function validateClosed( $info, &$val, $standalone ) {
166 if ( !$standalone ) {
167 // this only validates standalone properties, not arrays, etc
168 return;
169 }
170
171 // check if its in a numeric range
172 $inRange = false;
173 if ( isset( $info['rangeLow'] )
174 && isset( $info['rangeHigh'] )
175 && is_numeric( $val )
176 && ( intval( $val ) <= $info['rangeHigh'] )
177 && ( intval( $val ) >= $info['rangeLow'] )
178 ) {
179 $inRange = true;
180 }
181
182 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
183 $this->logger->info( __METHOD__ . " Expected closed choice, but got $val" );
184 $val = null;
185 }
186 }
187
188 /**
189 * function to validate and modify flash structure
190 *
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
194 */
195 public function validateFlash( $info, &$val, $standalone ) {
196 if ( $standalone ) {
197 // this only validates flash structs, not individual properties
198 return;
199 }
200 if ( !( isset( $val['Fired'] )
201 && isset( $val['Function'] )
202 && isset( $val['Mode'] )
203 && isset( $val['RedEyeMode'] )
204 && isset( $val['Return'] )
205 ) ) {
206 $this->logger->info( __METHOD__ . " Flash structure did not have all the required components" );
207 $val = null;
208 } else {
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 ) );
214 }
215 }
216
217 /**
218 * function to validate LangCode properties ( en-GB, etc )
219 *
220 * This is just a naive check to make sure it somewhat looks like a lang code.
221 *
222 * @see BCP 47
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)
225 *
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
229 */
230 public function validateLangCode( $info, &$val, $standalone ) {
231 if ( !$standalone ) {
232 // this only validates standalone properties, not arrays, etc
233 return;
234 }
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" );
238 $val = null;
239 }
240 }
241
242 /**
243 * function to validate date properties, and convert to (partial) Exif format.
244 *
245 * Dates can be one of the following formats:
246 * YYYY
247 * YYYY-MM
248 * YYYY-MM-DD
249 * YYYY-MM-DDThh:mmTZD
250 * YYYY-MM-DDThh:mm:ssTZD
251 * YYYY-MM-DDThh:mm:ss.sTZD
252 *
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
256 * 2011:04.
257 * @param bool $standalone If this is a simple property or array
258 */
259 public function validateDate( $info, &$val, $standalone ) {
260 if ( !$standalone ) {
261 // this only validates standalone properties, not arrays, etc
262 return;
263 }
264 $res = [];
265 // @codingStandardsIgnoreStart Long line that cannot be broken
266 if ( !preg_match(
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',
269 $val, $res )
270 ) {
271 // @codingStandardsIgnoreEnd
272
273 $this->logger->info( __METHOD__ . " Expected date but got $val" );
274 $val = null;
275 } else {
276 /*
277 * $res is formatted as follows:
278 * 0 -> full date.
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.
283 */
284
285 /*
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.
289 */
290 if ( $res[1] === '0000' ) {
291 $this->logger->info( __METHOD__ . " Invalid date (year 0): $val" );
292 $val = null;
293
294 return;
295 }
296
297 if ( !isset( $res[4] ) ) { // hour
298 // just have the year month day (if that)
299 $val = $res[1];
300 if ( isset( $res[2] ) ) {
301 $val .= ':' . $res[2];
302 }
303 if ( isset( $res[3] ) ) {
304 $val .= ':' . $res[3];
305 }
306
307 return;
308 }
309
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];
316 }
317
318 return;
319 }
320
321 // Extra check for empty string necessary due to TZ but no second case.
322 $stripSeconds = false;
323 if ( !isset( $res[6] ) || $res[6] === '' ) {
324 $res[6] = '00';
325 $stripSeconds = true;
326 }
327
328 // Do timezone processing. We've already done the case that tz = Z.
329
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.
332
333 $unix = ConvertibleTimestamp::convert( TS_UNIX,
334 $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6]
335 );
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 ) === '-' ) {
339 $offset = -$offset;
340 }
341 $val = ConvertibleTimestamp::convert( TS_EXIF, $unix + $offset );
342
343 if ( $stripSeconds ) {
344 // If seconds weren't specified, remove the trailing ':00'.
345 $val = substr( $val, 0, -3 );
346 }
347 }
348 }
349
350 /** function to validate, and more importantly
351 * translate the XMP DMS form of gps coords to
352 * the decimal form we use.
353 *
354 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf
355 * section 1.2.7.4 on page 23
356 *
357 * @param array $info Unused (info about prop)
358 * @param string &$val GPS string in either DDD,MM,SSk or
359 * or DDD,MM.mmk form
360 * @param bool $standalone If its a simple prop (should always be true)
361 */
362 public function validateGPS( $info, &$val, $standalone ) {
363 if ( !$standalone ) {
364 return;
365 }
366
367 $m = [];
368 if ( preg_match(
369 '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
370 $val, $m )
371 ) {
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' ) {
376 $coord = -$coord;
377 }
378 $val = $coord;
379
380 return;
381 } elseif ( preg_match(
382 '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
383 $val, $m )
384 ) {
385 $coord = intval( $m[1] );
386 $coord += floatval( $m[2] ) * ( 1 / 60 );
387 if ( $m[3] === 'S' || $m[3] === 'W' ) {
388 $coord = -$coord;
389 }
390 $val = $coord;
391
392 return;
393 } else {
394 $this->logger->info( __METHOD__
395 . " Expected GPSCoordinate, but got $val." );
396 $val = null;
397
398 return;
399 }
400 }
401 }