xref: /plugin/diagrams/helper.php (revision 54b7a78ab0ccd620e965bf709a938d44c0f563b9)
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    /**
115f757686SAndreas Gohr     * Check if the given file is a diagrams.net diagram
125f757686SAndreas Gohr     *
135f757686SAndreas Gohr     * @param string $file
145f757686SAndreas Gohr     * @return bool
155f757686SAndreas Gohr     */
165f757686SAndreas Gohr    public function isDiagramFile($file) {
175f757686SAndreas Gohr        $svg = file_get_contents($file, false, null, 0, 500);
185f757686SAndreas Gohr        return $this->isDiagram($svg);
195f757686SAndreas Gohr    }
205f757686SAndreas Gohr
215f757686SAndreas 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
31*54b7a78aSAndreas Gohr        $svg = ltrim($svg);
32*54b7a78aSAndreas Gohr        if (empty($svg) || substr($svg, 0, 4) !== '<svg') return false;
3395ed8ca0SAndreas Gohr        $confServiceUrl = $this->getConf('service_url'); // like "https://diagrams.xyz.org/?embed=1&..."
3495ed8ca0SAndreas Gohr        $serviceHost = parse_url($confServiceUrl, PHP_URL_HOST); // Host-Portion of the Url, e.g. "diagrams.xyz.org"
3595ed8ca0SAndreas Gohr        return strpos($svg, 'embed.diagrams.net') || strpos($svg, 'draw.io') || strpos($svg, $serviceHost);
3695ed8ca0SAndreas Gohr    }
3795ed8ca0SAndreas Gohr}
3895ed8ca0SAndreas Gohr
39