xref: /plugin/diagrams/helper.php (revision 5f757686d683fd550247221d4a6d2848f21773e6)
195ed8ca0SAndreas Gohr<?php
295ed8ca0SAndreas Gohr/**
395ed8ca0SAndreas Gohr * DokuWiki Plugin diagrams (Helper Component)
495ed8ca0SAndreas Gohr *
595ed8ca0SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
695ed8ca0SAndreas Gohr * @author  Innovakom + CosmoCode <dokuwiki@cosmocode.de>
795ed8ca0SAndreas Gohr */
895ed8ca0SAndreas Gohrclass helper_plugin_diagrams extends \dokuwiki\Extension\Plugin
995ed8ca0SAndreas Gohr{
1095ed8ca0SAndreas Gohr    /**
11*5f757686SAndreas Gohr     * Check if the given file is a diagrams.net diagram
12*5f757686SAndreas Gohr     *
13*5f757686SAndreas Gohr     * @param string $file
14*5f757686SAndreas Gohr     * @return bool
15*5f757686SAndreas Gohr     */
16*5f757686SAndreas Gohr    public function isDiagramFile($file) {
17*5f757686SAndreas Gohr        $svg = file_get_contents($file, false, null, 0, 500);
18*5f757686SAndreas Gohr        return $this->isDiagram($svg);
19*5f757686SAndreas Gohr    }
20*5f757686SAndreas Gohr
21*5f757686SAndreas Gohr    /**
2295ed8ca0SAndreas Gohr     * Check if the given SVG is a diagrams.net diagram
2395ed8ca0SAndreas Gohr     *
2495ed8ca0SAndreas Gohr     * This is done by ensuring that the service host is part of the SVG header
2595ed8ca0SAndreas Gohr     *
2695ed8ca0SAndreas Gohr     * @param string $svg The raw SVG data (first 500 bytes are enough)
2795ed8ca0SAndreas Gohr     * @return bool
2895ed8ca0SAndreas Gohr     */
2995ed8ca0SAndreas Gohr    public function isDiagram($svg) {
3095ed8ca0SAndreas Gohr        $svg = substr($svg, 0, 500); // makes checking a tiny bit faster
3195ed8ca0SAndreas Gohr        $confServiceUrl = $this->getConf('service_url'); // like "https://diagrams.xyz.org/?embed=1&..."
3295ed8ca0SAndreas Gohr        $serviceHost = parse_url($confServiceUrl, PHP_URL_HOST); // Host-Portion of the Url, e.g. "diagrams.xyz.org"
3395ed8ca0SAndreas Gohr        return strpos($svg, 'embed.diagrams.net') || strpos($svg, 'draw.io') || strpos($svg, $serviceHost);
3495ed8ca0SAndreas Gohr    }
3595ed8ca0SAndreas Gohr}
3695ed8ca0SAndreas Gohr
37