1<?php
2
3/////////////////////////////////////////////////////////////////
4/// getID3() by James Heinrich <info@getid3.org>               //
5//  available at https://github.com/JamesHeinrich/getID3       //
6//            or https://www.getid3.org                        //
7//            or http://getid3.sourceforge.net                 //
8//  see readme.txt for more details                            //
9/////////////////////////////////////////////////////////////////
10//                                                             //
11// module.graphic.svg.php                                      //
12// module for analyzing SVG Image files                        //
13// dependencies: NONE                                          //
14//                                                            ///
15/////////////////////////////////////////////////////////////////
16
17if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
18	exit;
19}
20
21class getid3_svg extends getid3_handler
22{
23	/**
24	 * @return bool
25	 */
26	public function Analyze() {
27		$info = &$this->getid3->info;
28
29		$this->fseek($info['avdataoffset']);
30
31		$SVGheader = $this->fread(4096);
32		if (preg_match('#\<\?xml([^\>]+)\?\>#i', $SVGheader, $matches)) {
33			$info['svg']['xml']['raw'] = $matches;
34		}
35		if (preg_match('#\<\!DOCTYPE([^\>]+)\>#i', $SVGheader, $matches)) {
36			$info['svg']['doctype']['raw'] = $matches;
37		}
38		if (preg_match('#\<svg([^\>]+)\>#i', $SVGheader, $matches)) {
39			$info['svg']['svg']['raw'] = $matches;
40		}
41		if (isset($info['svg']['svg']['raw'])) {
42
43			$sections_to_fix = array('xml', 'doctype', 'svg');
44			foreach ($sections_to_fix as $section_to_fix) {
45				if (!isset($info['svg'][$section_to_fix])) {
46					continue;
47				}
48				$section_data = array();
49				while (preg_match('/ "([^"]+)"/', $info['svg'][$section_to_fix]['raw'][1], $matches)) {
50					$section_data[] = $matches[1];
51					$info['svg'][$section_to_fix]['raw'][1] = str_replace($matches[0], '', $info['svg'][$section_to_fix]['raw'][1]);
52				}
53				while (preg_match('/([^\s]+)="([^"]+)"/', $info['svg'][$section_to_fix]['raw'][1], $matches)) {
54					$section_data[] = $matches[0];
55					$info['svg'][$section_to_fix]['raw'][1] = str_replace($matches[0], '', $info['svg'][$section_to_fix]['raw'][1]);
56				}
57				$section_data = array_merge($section_data, preg_split('/[\s,]+/', $info['svg'][$section_to_fix]['raw'][1]));
58				foreach ($section_data as $keyvaluepair) {
59					$keyvaluepair = trim($keyvaluepair);
60					if ($keyvaluepair) {
61						$keyvalueexploded = explode('=', $keyvaluepair);
62						$key   = (isset($keyvalueexploded[0]) ? $keyvalueexploded[0] : '');
63						$value = (isset($keyvalueexploded[1]) ? $keyvalueexploded[1] : '');
64						$info['svg'][$section_to_fix]['sections'][$key] = trim($value, '"');
65					}
66				}
67			}
68
69			$info['fileformat']                  = 'svg';
70			$info['video']['dataformat']         = 'svg';
71			$info['video']['lossless']           = true;
72			//$info['video']['bits_per_sample']    = 24;
73			$info['video']['pixel_aspect_ratio'] = (float) 1;
74
75			if (!empty($info['svg']['svg']['sections']['width'])) {
76				$info['svg']['width']  = intval($info['svg']['svg']['sections']['width']);
77			}
78			if (!empty($info['svg']['svg']['sections']['height'])) {
79				$info['svg']['height'] = intval($info['svg']['svg']['sections']['height']);
80			}
81			if (!empty($info['svg']['svg']['sections']['version'])) {
82				$info['svg']['version'] = $info['svg']['svg']['sections']['version'];
83			}
84			if (!isset($info['svg']['version']) && isset($info['svg']['doctype']['sections'])) {
85				foreach ($info['svg']['doctype']['sections'] as $key => $value) {
86					if (preg_match('#//W3C//DTD SVG ([0-9\.]+)//#i', $key, $matches)) {
87						$info['svg']['version'] = $matches[1];
88						break;
89					}
90				}
91			}
92
93			if (!empty($info['svg']['width'])) {
94				$info['video']['resolution_x'] = $info['svg']['width'];
95			}
96			if (!empty($info['svg']['height'])) {
97				$info['video']['resolution_y'] = $info['svg']['height'];
98			}
99
100			return true;
101		}
102		$this->error('Did not find expected <svg> tag');
103		return false;
104	}
105
106}
107