1317bdfc2SAndreas Gohr<?php 2317bdfc2SAndreas Gohr 3317bdfc2SAndreas Gohr/** 4317bdfc2SAndreas Gohr * Action component of diagrams plugin 5317bdfc2SAndreas Gohr * 659e7180eSAndreas Gohr * This handles general operations independent of the configured mode 759e7180eSAndreas Gohr * 859e7180eSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 959e7180eSAndreas Gohr * @author Innovakom + CosmoCode <dokuwiki@cosmocode.de> 10317bdfc2SAndreas Gohr */ 11317bdfc2SAndreas Gohrclass action_plugin_diagrams_action extends DokuWiki_Action_Plugin 12317bdfc2SAndreas Gohr{ 13*2bec1a22SAndreas Gohr /** @var helper_plugin_diagrams */ 14*2bec1a22SAndreas Gohr protected $helper; 15317bdfc2SAndreas Gohr 1659e7180eSAndreas Gohr /**@inheritDoc */ 17317bdfc2SAndreas Gohr public function register(Doku_Event_Handler $controller) 18317bdfc2SAndreas Gohr { 19317bdfc2SAndreas Gohr $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'addJsinfo'); 20317bdfc2SAndreas Gohr $controller->register_hook('MEDIAMANAGER_STARTED', 'AFTER', $this, 'addJsinfo'); 21317bdfc2SAndreas Gohr $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'checkConf'); 22*2bec1a22SAndreas Gohr $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleCache'); 23*2bec1a22SAndreas Gohr 24*2bec1a22SAndreas Gohr $this->helper = plugin_load('helper', 'diagrams'); 25317bdfc2SAndreas Gohr } 26317bdfc2SAndreas Gohr 27317bdfc2SAndreas Gohr /** 2859e7180eSAndreas Gohr * Add data to JSINFO 29317bdfc2SAndreas Gohr * 3059e7180eSAndreas Gohr * full service URL 3159e7180eSAndreas Gohr * digram mode 3259e7180eSAndreas Gohr * security token used for uploading 3359e7180eSAndreas Gohr * 3459e7180eSAndreas Gohr * @param Doku_Event $event DOKUWIKI_STARTED|MEDIAMANAGER_STARTED 35317bdfc2SAndreas Gohr */ 36317bdfc2SAndreas Gohr public function addJsinfo(Doku_Event $event) 37317bdfc2SAndreas Gohr { 38317bdfc2SAndreas Gohr global $JSINFO; 39317bdfc2SAndreas Gohr $JSINFO['sectok'] = getSecurityToken(); 4059e7180eSAndreas Gohr $JSINFO['plugins']['diagrams'] = [ 4159e7180eSAndreas Gohr 'service_url' => $this->getConf('service_url'), 4259e7180eSAndreas Gohr 'mode' => $this->getConf('mode'), 4359e7180eSAndreas Gohr ]; 44317bdfc2SAndreas Gohr } 45317bdfc2SAndreas Gohr 46317bdfc2SAndreas Gohr /** 47317bdfc2SAndreas Gohr * Check if DokuWiki is properly configured to handle SVG diagrams 48317bdfc2SAndreas Gohr * 4959e7180eSAndreas Gohr * @param Doku_Event $event DOKUWIKI_STARTED 50317bdfc2SAndreas Gohr */ 51317bdfc2SAndreas Gohr public function checkConf(Doku_Event $event) 52317bdfc2SAndreas Gohr { 53317bdfc2SAndreas Gohr $mime = getMimeTypes(); 54317bdfc2SAndreas Gohr if (!array_key_exists('svg', $mime) || $mime['svg'] !== 'image/svg+xml') { 55317bdfc2SAndreas Gohr msg($this->getLang('missingConfig'), -1); 56317bdfc2SAndreas Gohr } 57317bdfc2SAndreas Gohr } 58*2bec1a22SAndreas Gohr 59*2bec1a22SAndreas Gohr /** 60*2bec1a22SAndreas Gohr * Save the PNG cache of a diagram 61*2bec1a22SAndreas Gohr * 62*2bec1a22SAndreas Gohr * @param Doku_Event $event AJAX_CALL_UNKNOWN 63*2bec1a22SAndreas Gohr */ 64*2bec1a22SAndreas Gohr public function handleCache(Doku_Event $event) 65*2bec1a22SAndreas Gohr { 66*2bec1a22SAndreas Gohr if ($event->data !== 'plugin_diagrams_savecache') return; 67*2bec1a22SAndreas Gohr $event->preventDefault(); 68*2bec1a22SAndreas Gohr $event->stopPropagation(); 69*2bec1a22SAndreas Gohr 70*2bec1a22SAndreas Gohr global $INPUT; 71*2bec1a22SAndreas Gohr 72*2bec1a22SAndreas Gohr $svg = $INPUT->str('svg'); // raw svg 73*2bec1a22SAndreas Gohr $png = $INPUT->str('png'); // data uri 74*2bec1a22SAndreas Gohr 75*2bec1a22SAndreas Gohr if (!checkSecurityToken()) { 76*2bec1a22SAndreas Gohr http_status(403); 77*2bec1a22SAndreas Gohr return; 78*2bec1a22SAndreas Gohr } 79*2bec1a22SAndreas Gohr 80*2bec1a22SAndreas Gohr if (!$this->helper->isDiagram($svg)) { 81*2bec1a22SAndreas Gohr http_status(400); 82*2bec1a22SAndreas Gohr return; 83*2bec1a22SAndreas Gohr } 84*2bec1a22SAndreas Gohr 85*2bec1a22SAndreas Gohr if(!preg_match('/^data:image\/png;base64,/', $png)) { 86*2bec1a22SAndreas Gohr http_status(400); 87*2bec1a22SAndreas Gohr return; 88*2bec1a22SAndreas Gohr } 89*2bec1a22SAndreas Gohr $png = base64_decode(explode(',',$png)[1]); 90*2bec1a22SAndreas Gohr 91*2bec1a22SAndreas Gohr if(substr($png, 1, 3) !== 'PNG') { 92*2bec1a22SAndreas Gohr http_status(400); 93*2bec1a22SAndreas Gohr return; 94*2bec1a22SAndreas Gohr } 95*2bec1a22SAndreas Gohr 96*2bec1a22SAndreas Gohr $cacheName = getCacheName($svg, '.diagrams.png'); 97*2bec1a22SAndreas Gohr if(io_saveFile($cacheName, $png)) { 98*2bec1a22SAndreas Gohr echo 'OK'; 99*2bec1a22SAndreas Gohr } else { 100*2bec1a22SAndreas Gohr http_status(500); 101*2bec1a22SAndreas Gohr } 102*2bec1a22SAndreas Gohr } 103317bdfc2SAndreas Gohr} 104