Completed
Push — master ( 8fe45b...3e4cf7 )
by Jeroen
08:37
created

MediaBundle/Helper/File/SVGMimeTypeGuesser.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\MediaBundle\Helper\File;
4
5
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
6
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
7
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
8
9
@trigger_error(sprintf('The "%s" class is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. Use the "symfony/mime" component instead.', __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
10
11
/**
12
 * @deprecated this class is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. Use the "symfony/mime" component instead.
13
 *
14
 * Simple Mime type guesser to detect SVG image files, it will test if the file is an XML file and return SVG mime type
15
 * if the XML contains a valid SVG namespace...
16
 */
17
class SVGMimeTypeGuesser implements MimeTypeGuesserInterface
18
{
19
    private $_MIMETYPE_NAMESPACES = array(
20
        'http://www.w3.org/2000/svg' => 'image/svg+xml',
21
    );
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function guess($path)
27
    {
28
        if (!is_file($path)) {
29
            throw new FileNotFoundException($path);
30
        }
31
32
        if (!is_readable($path)) {
33
            throw new AccessDeniedException($path);
34
        }
35
36
        if (!self::isSupported()) {
37
            return;
38
        }
39
40
        $dom = new \DOMDocument();
41
        $xml = $dom->load($path, LIBXML_NOERROR + LIBXML_ERR_FATAL + LIBXML_ERR_NONE);
42
        if ($xml === false) {
43
            return;
44
        }
45
        $xpath = new \DOMXPath($dom);
46
        foreach ($xpath->query('namespace::*') as $node) {
47
            if (isset($this->_MIMETYPE_NAMESPACES[$node->nodeValue])) {
48
                return $this->_MIMETYPE_NAMESPACES[$node->nodeValue];
49
            }
50
        }
51
52
        return;
53
    }
54
55
    /**
56
     * Returns whether this guesser is supported on the current OS
57
     *
58
     * @return bool
59
     */
60
    public static function isSupported()
61
    {
62
        return class_exists('DOMDocument') && class_exists('DOMXPath');
63
    }
64
}
65