f37adf963c8190c20502e2505730039a8cb7ab4b
[lhc/web/www.git] / www / plugins-dist / medias / metadata / svg.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 /**
14 * Informations meta d'un SVG
15 *
16 * @package SPIP\Medias\Metadata
17 **/
18
19 if (!defined('_ECRIRE_INC_VERSION')) {
20 return;
21 }
22 include_spip('inc/autoriser');
23
24 /**
25 * Déterminer les dimensions d'un svg, et enlever ses scripts si nécessaire
26 *
27 * On utilise safehtml qui n'est pas apropriée pour ça en attendant mieux
28 * cf http://www.slideshare.net/x00mario/the-image-that-called-me
29 * http://heideri.ch/svgpurifier/SVGPurifier/index.php
30 *
31 * @param string $file
32 * @return array Tableau (largeur, hauteur)
33 */
34 function metadata_svg_dist($file) {
35 $meta = array();
36
37 $texte = spip_file_get_contents($file);
38
39 // Securite si pas autorise : virer les scripts et les references externes
40 // sauf si on est en mode javascript 'ok' (1), cf. inc_version
41 if ($GLOBALS['filtrer_javascript'] < 1
42 and !autoriser('televerser', 'script')
43 ) {
44 include_spip('inc/texte');
45 $new = trim(safehtml($texte));
46 // petit bug safehtml
47 if (substr($new, 0, 2) == ']>') {
48 $new = ltrim(substr($new, 2));
49 }
50 if ($new != $texte) {
51 ecrire_fichier($file, $texte = $new);
52 }
53 }
54
55 $width = $height = 150;
56 if (preg_match(',<svg[^>]+>,', $texte, $s)) {
57 $s = $s[0];
58 if (preg_match(',\WviewBox\s*=\s*.\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+),i', $s, $r)) {
59 $width = $r[3];
60 $height = $r[4];
61 } else {
62 // si la taille est en centimetre, estimer le pixel a 1/64 de cm
63 if (preg_match(',\Wwidth\s*=\s*.(\d+)([^"\']*),i', $s, $r)) {
64 if ($r[2] != '%') {
65 $width = $r[1];
66 if ($r[2] == 'cm') {
67 $width <<= 6;
68 }
69 }
70 }
71 if (preg_match(',\Wheight\s*=\s*.(\d+)([^"\']*),i', $s, $r)) {
72 if ($r[2] != '%') {
73 $height = $r[1];
74 if ($r[2] == 'cm') {
75 $height <<= 6;
76 }
77 }
78 }
79 }
80 }
81 $meta['largeur'] = $width;
82 $meta['hauteur'] = $height;
83
84 return $meta;
85 }