18a822cc1SAndreas Gohr<?php 28a822cc1SAndreas Gohr 3bc39777fSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams; 4bc39777fSAndreas Gohr 58a822cc1SAndreas Gohr/** 68c8c7007SAndreas Gohr * DokuWiki Plugin diagrams (Syntax Component) 78c8c7007SAndreas Gohr * 88c8c7007SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 98c8c7007SAndreas Gohr * @author Innovakom + CosmoCode <dokuwiki@cosmocode.de> 108a822cc1SAndreas Gohr */ 11046ca144SAndreas Gohrclass syntax_plugin_diagrams_embed extends syntax_plugin_diagrams_mediafile 128c8c7007SAndreas Gohr{ 13046ca144SAndreas Gohr /** @var int count the current embedded diagram */ 14046ca144SAndreas Gohr protected $count = 0; 158a822cc1SAndreas Gohr 168c8c7007SAndreas Gohr /** @inheritDoc */ 178a822cc1SAndreas Gohr public function connectTo($mode) 188a822cc1SAndreas Gohr { 19bc39777fSAndreas Gohr // only register if embed mode is enabled 2059e7180eSAndreas Gohr if (!($this->getConf('mode') & Diagrams::MODE_EMBED)) return; 21baa90558SAnna Dabrowska $this->Lexer->addSpecialPattern('<diagram(?: .*?)?>.*?(?:</diagram>)', $mode, 'plugin_diagrams_embed'); 228a822cc1SAndreas Gohr } 238a822cc1SAndreas Gohr 248c8c7007SAndreas Gohr /** @inheritDoc */ 258a822cc1SAndreas Gohr public function handle($match, $state, $pos, Doku_Handler $handler) 268a822cc1SAndreas Gohr { 278c8c7007SAndreas Gohr [$open, $rest] = sexplode('>', $match, 2); 288c8c7007SAndreas Gohr $params = substr($open, 9); 298c8c7007SAndreas Gohr $svg = substr($rest, 0, -10); 308c8c7007SAndreas Gohr 31317bdfc2SAndreas Gohr // embed positions 32317bdfc2SAndreas Gohr $svglen = strlen($svg); 33317bdfc2SAndreas Gohr $svgpos = $pos + strpos($match, '>'); 34317bdfc2SAndreas Gohr 3595ed8ca0SAndreas Gohr /** @var helper_plugin_diagrams $helper */ 3695ed8ca0SAndreas Gohr $helper = plugin_load('helper', 'diagrams'); 3795ed8ca0SAndreas Gohr if (!$helper->isDiagram($svg)) return false; 3895ed8ca0SAndreas Gohr 398c8c7007SAndreas Gohr $data = [ 408c8c7007SAndreas Gohr 'svg' => $svg, 418c8c7007SAndreas Gohr 'align' => '', 428c8c7007SAndreas Gohr 'width' => '', 438c8c7007SAndreas Gohr 'height' => '', 44317bdfc2SAndreas Gohr 'pos' => $svgpos, 45317bdfc2SAndreas Gohr 'len' => $svglen, 468c8c7007SAndreas Gohr ]; 478c8c7007SAndreas Gohr 488c8c7007SAndreas Gohr if (preg_match('/\b(left|right|center)\b/', $params, $matches)) { 498c8c7007SAndreas Gohr $data['align'] = $matches[1]; 508c8c7007SAndreas Gohr } 518c8c7007SAndreas Gohr if (preg_match('/\b(\d+)x(\d+)\b/', $params, $matches)) { 528c8c7007SAndreas Gohr $data['width'] = (int)$matches[1]; 538c8c7007SAndreas Gohr $data['height'] = (int)$matches[2]; 548a822cc1SAndreas Gohr } 558a822cc1SAndreas Gohr 568c8c7007SAndreas Gohr return $data; 578c8c7007SAndreas Gohr } 588c8c7007SAndreas Gohr 598c8c7007SAndreas Gohr /** @inheritDoc */ 608a822cc1SAndreas Gohr public function render($format, Doku_Renderer $renderer, $data) 618a822cc1SAndreas Gohr { 62100ff2a5SAndreas Gohr if (!$data) return false; 63*98c89536SAndreas Gohr global $ID; 64*98c89536SAndreas Gohr global $ACT; 65046ca144SAndreas Gohr global $INPUT; 668c8c7007SAndreas Gohr 67046ca144SAndreas Gohr switch ($format) { 68046ca144SAndreas Gohr case 'xhtml': 69*98c89536SAndreas Gohr if(act_clean($ACT) !== 'preview' && page_exists($ID)) { 70*98c89536SAndreas Gohr // this is "normal" rendering, we reference the diagram through the export 71*98c89536SAndreas Gohr // this applies the same CSP as mediafiles and will also show the exact behaviours 72*98c89536SAndreas Gohr $data['url'] = wl($ID, ['do' => 'export_diagrams', 'svg' => $this->count++], true, '&'); 73*98c89536SAndreas Gohr } else { 74*98c89536SAndreas Gohr // we're in preview and the diagram may not have been saved, yet. So we 75*98c89536SAndreas Gohr // reference it as data uri to prevent cross-origin access XSS 762aa4335dSAndreas Gohr $data['url'] = 'data:image/svg+xml;base64,' . base64_encode($data['svg']); 77*98c89536SAndreas Gohr } 78*98c89536SAndreas Gohr 79046ca144SAndreas Gohr parent::render($format, $renderer, $data); 80046ca144SAndreas Gohr return true; 81046ca144SAndreas Gohr case 'diagrams': 82046ca144SAndreas Gohr // This exports a single SVG during the export_diagrams action 83046ca144SAndreas Gohr if ($INPUT->int('svg') === $this->count++) { 84046ca144SAndreas Gohr $renderer->doc = $data['svg']; 85100ff2a5SAndreas Gohr } 868a822cc1SAndreas Gohr return true; 878a822cc1SAndreas Gohr } 88046ca144SAndreas Gohr return false; 89046ca144SAndreas Gohr } 90046ca144SAndreas Gohr 91046ca144SAndreas Gohr 928a822cc1SAndreas Gohr} 938c8c7007SAndreas Gohr 94