xref: /plugin/diagrams/helper.php (revision 95ed8ca06adb5fd6c820ea6e5ad119eac0d9199e)
1*95ed8ca0SAndreas Gohr<?php
2*95ed8ca0SAndreas Gohr/**
3*95ed8ca0SAndreas Gohr * DokuWiki Plugin diagrams (Helper Component)
4*95ed8ca0SAndreas Gohr *
5*95ed8ca0SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*95ed8ca0SAndreas Gohr * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
7*95ed8ca0SAndreas Gohr */
8*95ed8ca0SAndreas Gohrclass helper_plugin_diagrams extends \dokuwiki\Extension\Plugin
9*95ed8ca0SAndreas Gohr{
10*95ed8ca0SAndreas Gohr    /**
11*95ed8ca0SAndreas Gohr     * Check if the given SVG is a diagrams.net diagram
12*95ed8ca0SAndreas Gohr     *
13*95ed8ca0SAndreas Gohr     * This is done by ensuring that the service host is part of the SVG header
14*95ed8ca0SAndreas Gohr     *
15*95ed8ca0SAndreas Gohr     * @param string $svg The raw SVG data (first 500 bytes are enough)
16*95ed8ca0SAndreas Gohr     * @return bool
17*95ed8ca0SAndreas Gohr     */
18*95ed8ca0SAndreas Gohr    public function isDiagram($svg) {
19*95ed8ca0SAndreas Gohr        $svg = substr($svg, 0, 500); // makes checking a tiny bit faster
20*95ed8ca0SAndreas Gohr        $confServiceUrl = $this->getConf('service_url'); // like "https://diagrams.xyz.org/?embed=1&..."
21*95ed8ca0SAndreas Gohr        $serviceHost = parse_url($confServiceUrl, PHP_URL_HOST); // Host-Portion of the Url, e.g. "diagrams.xyz.org"
22*95ed8ca0SAndreas Gohr        return strpos($svg, 'embed.diagrams.net') || strpos($svg, 'draw.io') || strpos($svg, $serviceHost);
23*95ed8ca0SAndreas Gohr    }
24*95ed8ca0SAndreas Gohr}
25*95ed8ca0SAndreas Gohr
26