Revert "Use display name in category page subheadings if provided"
[lhc/web/wiklou.git] / includes / media / 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
27 /**
28 * This contains some static methods for
29 * validating XMP properties. See XMPInfo and XMPReader classes.
30 *
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
36 *
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.
39 *
40 * These validation functions can also be used to modify the data. See the gps and flash one's
41 * for example.
42 *
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
45 */
46 class XMPValidate implements LoggerAwareInterface {
47
48 /**
49 * @var LoggerInterface
50 */
51 private $logger;
52
53 public function __construct( LoggerInterface $logger ) {
54 $this->setLogger( $logger );
55 }
56
57 public function setLogger( LoggerInterface $logger ) {
58 $this->logger = $logger;
59 }
60 /**
61 * Function to validate boolean properties ( True or False )
62 *
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
66 */
67 public function validateBoolean( $info, &$val, $standalone ) {
68 if ( !$standalone ) {
69 // this only validates standalone properties, not arrays, etc
70 return;
71 }
72 if ( $val !== 'True' && $val !== 'False' ) {
73 $this->logger->info( __METHOD__ . " Expected True or False but got $val" );
74 $val = null;
75 }
76 }
77
78 /**
79 * function to validate rational properties ( 12/10 )
80 *
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
84 */
85 public function validateRational( $info, &$val, $standalone ) {
86 if ( !$standalone ) {
87 // this only validates standalone properties, not arrays, etc
88 return;
89 }
90 if ( !preg_match( '/^(?:-?\d+)\/(?:\d+[1-9]|[1-9]\d*)$/D', $val ) ) {
91 $this->logger->info( __METHOD__ . " Expected rational but got $val" );
92 $val = null;
93 }
94 }
95
96 /**
97 * function to validate rating properties -1, 0-5
98 *
99 * if its outside of range put it into range.
100 *
101 * @see MWG spec
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
105 */
106 public function validateRating( $info, &$val, $standalone ) {
107 if ( !$standalone ) {
108 // this only validates standalone properties, not arrays, etc
109 return;
110 }
111 if ( !preg_match( '/^[-+]?\d*(?:\.?\d*)$/D', $val )
112 || !is_numeric( $val )
113 ) {
114 $this->logger->info( __METHOD__ . " Expected rating but got $val" );
115 $val = null;
116
117 return;
118 } else {
119 $nVal = (float)$val;
120 if ( $nVal < 0 ) {
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)" );
125 $val = '-1';
126
127 return;
128 }
129 if ( $nVal > 5 ) {
130 $this->logger->info( __METHOD__ . " Rating too high, setting to 5" );
131 $val = '5';
132
133 return;
134 }
135 }
136 }
137
138 /**
139 * function to validate integers
140 *
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
144 */
145 public function validateInteger( $info, &$val, $standalone ) {
146 if ( !$standalone ) {
147 // this only validates standalone properties, not arrays, etc
148 return;
149 }
150 if ( !preg_match( '/^[-+]?\d+$/D', $val ) ) {
151 $this->logger->info( __METHOD__ . " Expected integer but got $val" );
152 $val = null;
153 }
154 }
155
156 /**
157 * function to validate properties with a fixed number of allowed
158 * choices. (closed choice)
159 *
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
163 */
164 public function validateClosed( $info, &$val, $standalone ) {
165 if ( !$standalone ) {
166 // this only validates standalone properties, not arrays, etc
167 return;
168 }
169
170 // check if its in a numeric range
171 $inRange = false;
172 if ( isset( $info['rangeLow'] )
173 && isset( $info['rangeHigh'] )
174 && is_numeric( $val )
175 && ( intval( $val ) <= $info['rangeHigh'] )
176 && ( intval( $val ) >= $info['rangeLow'] )
177 ) {
178 $inRange = true;
179 }
180
181 if ( !isset( $info['choices'][$val] ) && !$inRange ) {
182 $this->logger->info( __METHOD__ . " Expected closed choice, but got $val" );
183 $val = null;
184 }
185 }
186
187 /**
188 * function to validate and modify flash structure
189 *
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
193 */
194 public function validateFlash( $info, &$val, $standalone ) {
195 if ( $standalone ) {
196 // this only validates flash structs, not individual properties
197 return;
198 }
199 if ( !( isset( $val['Fired'] )
200 && isset( $val['Function'] )
201 && isset( $val['Mode'] )
202 && isset( $val['RedEyeMode'] )
203 && isset( $val['Return'] )
204 ) ) {
205 $this->logger->info( __METHOD__ . " Flash structure did not have all the required components" );
206 $val = null;
207 } else {
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 ) );
213 }
214 }
215
216 /**
217 * function to validate LangCode properties ( en-GB, etc )
218 *
219 * This is just a naive check to make sure it somewhat looks like a lang code.
220 *
221 * @see BCP 47
222 * @see https://wwwimages2.adobe.com/content/dam/Adobe/en/devnet/xmp/pdfs/
223 * XMP%20SDK%20Release%20cc-2014-12/XMPSpecificationPart1.pdf page 22 (section 8.2.2.4)
224 *
225 * @param array $info Information about current property
226 * @param mixed &$val Current value to validate
227 * @param bool $standalone If this is a simple property or array
228 */
229 public function validateLangCode( $info, &$val, $standalone ) {
230 if ( !$standalone ) {
231 // this only validates standalone properties, not arrays, etc
232 return;
233 }
234 if ( !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $val ) ) {
235 // this is a rather naive check.
236 $this->logger->info( __METHOD__ . " Expected Lang code but got $val" );
237 $val = null;
238 }
239 }
240
241 /**
242 * function to validate date properties, and convert to (partial) Exif format.
243 *
244 * Dates can be one of the following formats:
245 * YYYY
246 * YYYY-MM
247 * YYYY-MM-DD
248 * YYYY-MM-DDThh:mmTZD
249 * YYYY-MM-DDThh:mm:ssTZD
250 * YYYY-MM-DDThh:mm:ss.sTZD
251 *
252 * @param array $info Information about current property
253 * @param mixed &$val Current value to validate. Converts to TS_EXIF as a side-effect.
254 * in cases where there's only a partial date, it will give things like
255 * 2011:04.
256 * @param bool $standalone If this is a simple property or array
257 */
258 public function validateDate( $info, &$val, $standalone ) {
259 if ( !$standalone ) {
260 // this only validates standalone properties, not arrays, etc
261 return;
262 }
263 $res = [];
264 // @codingStandardsIgnoreStart Long line that cannot be broken
265 if ( !preg_match(
266 /* ahh! scary regex... */
267 '/^([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',
268 $val, $res )
269 ) {
270 // @codingStandardsIgnoreEnd
271
272 $this->logger->info( __METHOD__ . " Expected date but got $val" );
273 $val = null;
274 } else {
275 /*
276 * $res is formatted as follows:
277 * 0 -> full date.
278 * 1 -> year, 2-> month, 3-> day, 4-> hour, 5-> minute, 6->second
279 * 7-> Timezone specifier (Z or something like +12:30 )
280 * many parts are optional, some aren't. For example if you specify
281 * minute, you must specify hour, day, month, and year but not second or TZ.
282 */
283
284 /*
285 * First of all, if year = 0000, Something is wrongish,
286 * so don't extract. This seems to happen when
287 * some programs convert between metadata formats.
288 */
289 if ( $res[1] === '0000' ) {
290 $this->logger->info( __METHOD__ . " Invalid date (year 0): $val" );
291 $val = null;
292
293 return;
294 }
295
296 if ( !isset( $res[4] ) ) { // hour
297 // just have the year month day (if that)
298 $val = $res[1];
299 if ( isset( $res[2] ) ) {
300 $val .= ':' . $res[2];
301 }
302 if ( isset( $res[3] ) ) {
303 $val .= ':' . $res[3];
304 }
305
306 return;
307 }
308
309 if ( !isset( $res[7] ) || $res[7] === 'Z' ) {
310 // if hour is set, then minute must also be or regex above will fail.
311 $val = $res[1] . ':' . $res[2] . ':' . $res[3]
312 . ' ' . $res[4] . ':' . $res[5];
313 if ( isset( $res[6] ) && $res[6] !== '' ) {
314 $val .= ':' . $res[6];
315 }
316
317 return;
318 }
319
320 // Extra check for empty string necessary due to TZ but no second case.
321 $stripSeconds = false;
322 if ( !isset( $res[6] ) || $res[6] === '' ) {
323 $res[6] = '00';
324 $stripSeconds = true;
325 }
326
327 // Do timezone processing. We've already done the case that tz = Z.
328
329 // We know that if we got to this step, year, month day hour and min must be set
330 // by virtue of regex not failing.
331
332 $unix = wfTimestamp( TS_UNIX, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] );
333 $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
334 $offset += intval( substr( $res[7], 4, 2 ) ) * 60;
335 if ( substr( $res[7], 0, 1 ) === '-' ) {
336 $offset = -$offset;
337 }
338 $val = wfTimestamp( TS_EXIF, $unix + $offset );
339
340 if ( $stripSeconds ) {
341 // If seconds weren't specified, remove the trailing ':00'.
342 $val = substr( $val, 0, -3 );
343 }
344 }
345 }
346
347 /** function to validate, and more importantly
348 * translate the XMP DMS form of gps coords to
349 * the decimal form we use.
350 *
351 * @see http://www.adobe.com/devnet/xmp/pdfs/XMPSpecificationPart2.pdf
352 * section 1.2.7.4 on page 23
353 *
354 * @param array $info Unused (info about prop)
355 * @param string &$val GPS string in either DDD,MM,SSk or
356 * or DDD,MM.mmk form
357 * @param bool $standalone If its a simple prop (should always be true)
358 */
359 public function validateGPS( $info, &$val, $standalone ) {
360 if ( !$standalone ) {
361 return;
362 }
363
364 $m = [];
365 if ( preg_match(
366 '/(\d{1,3}),(\d{1,2}),(\d{1,2})([NWSE])/D',
367 $val, $m )
368 ) {
369 $coord = intval( $m[1] );
370 $coord += intval( $m[2] ) * ( 1 / 60 );
371 $coord += intval( $m[3] ) * ( 1 / 3600 );
372 if ( $m[4] === 'S' || $m[4] === 'W' ) {
373 $coord = -$coord;
374 }
375 $val = $coord;
376
377 return;
378 } elseif ( preg_match(
379 '/(\d{1,3}),(\d{1,2}(?:.\d*)?)([NWSE])/D',
380 $val, $m )
381 ) {
382 $coord = intval( $m[1] );
383 $coord += floatval( $m[2] ) * ( 1 / 60 );
384 if ( $m[3] === 'S' || $m[3] === 'W' ) {
385 $coord = -$coord;
386 }
387 $val = $coord;
388
389 return;
390 } else {
391 $this->logger->info( __METHOD__
392 . " Expected GPSCoordinate, but got $val." );
393 $val = null;
394
395 return;
396 }
397 }
398 }