18c8c7007SAndreas Gohr<?php 2bc39777fSAndreas Gohr 3bc39777fSAndreas Gohruse dokuwiki\plugin\diagrams\Diagrams; 4bc39777fSAndreas Gohr 58c8c7007SAndreas Gohr/** 68c8c7007SAndreas Gohr * DokuWiki Plugin diagrams (Action Component) 78c8c7007SAndreas Gohr * 88c8c7007SAndreas Gohr * This handles loading and saving embedded diagrams 98c8c7007SAndreas Gohr * 108c8c7007SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 118c8c7007SAndreas Gohr * @author Innovakom + CosmoCode <dokuwiki@cosmocode.de> 128c8c7007SAndreas Gohr */ 138c8c7007SAndreas Gohrclass action_plugin_diagrams_embed extends \dokuwiki\Extension\ActionPlugin 148c8c7007SAndreas Gohr{ 1595ed8ca0SAndreas Gohr /** @var helper_plugin_diagrams */ 1695ed8ca0SAndreas Gohr protected $helper; 178c8c7007SAndreas Gohr 188c8c7007SAndreas Gohr /** @inheritDoc */ 198c8c7007SAndreas Gohr public function register(Doku_Event_Handler $controller) 208c8c7007SAndreas Gohr { 21bc39777fSAndreas Gohr // only register if embed mode is enabled 2259e7180eSAndreas Gohr if (!($this->getConf('mode') & Diagrams::MODE_EMBED)) return; 23bc39777fSAndreas Gohr 248c8c7007SAndreas Gohr $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleLoad'); 25bc39777fSAndreas Gohr $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleSave'); 2695ed8ca0SAndreas Gohr 2795ed8ca0SAndreas Gohr $this->helper = plugin_load('helper', 'diagrams'); 288c8c7007SAndreas Gohr } 298c8c7007SAndreas Gohr 308c8c7007SAndreas Gohr /** 318c8c7007SAndreas Gohr * Load the SVG for an embedded diagram 328c8c7007SAndreas Gohr * 338c8c7007SAndreas Gohr * This locks the page for editing 348c8c7007SAndreas Gohr * 3559e7180eSAndreas Gohr * @param Doku_Event $event Event object AJAX_CALL_UNKNOWN 368c8c7007SAndreas Gohr */ 3759e7180eSAndreas Gohr public function handleLoad(Doku_Event $event) 38146e874bSAndreas Gohr { 398c8c7007SAndreas Gohr if ($event->data !== 'plugin_diagrams_embed_load') return; 408c8c7007SAndreas Gohr $event->preventDefault(); 418c8c7007SAndreas Gohr $event->stopPropagation(); 428c8c7007SAndreas Gohr 438c8c7007SAndreas Gohr global $INPUT; 448c8c7007SAndreas Gohr 458c8c7007SAndreas Gohr $id = $INPUT->str('id'); 468c8c7007SAndreas Gohr $pos = $INPUT->int('pos'); 478c8c7007SAndreas Gohr $len = $INPUT->int('len'); 488c8c7007SAndreas Gohr 49*ca5b8841SAndreas Gohr if (auth_quickaclcheck($id) < AUTH_EDIT) { 508c8c7007SAndreas Gohr http_status(403); 518c8c7007SAndreas Gohr return; 528c8c7007SAndreas Gohr } 538c8c7007SAndreas Gohr 548c8c7007SAndreas Gohr if (!page_exists($id)) { 558c8c7007SAndreas Gohr http_status(404); 568c8c7007SAndreas Gohr return; 578c8c7007SAndreas Gohr } 588c8c7007SAndreas Gohr 598c8c7007SAndreas Gohr if (checklock($id)) { 608c8c7007SAndreas Gohr http_status(423, 'Page Locked'); 618c8c7007SAndreas Gohr return; 628c8c7007SAndreas Gohr } 638c8c7007SAndreas Gohr 648c8c7007SAndreas Gohr $svg = rawWiki($id); 6595ed8ca0SAndreas Gohr if (!$this->helper->isDiagram($svg)) { 6695ed8ca0SAndreas Gohr http_status(400); 6795ed8ca0SAndreas Gohr return; 6895ed8ca0SAndreas Gohr } 6995ed8ca0SAndreas Gohr 7095ed8ca0SAndreas Gohr lock($id); // FIXME we probably need some periodic lock renewal while editing? 7195ed8ca0SAndreas Gohr header('Content-Type: image/svg+xml'); 728c8c7007SAndreas Gohr echo substr($svg, $pos, $len); 738c8c7007SAndreas Gohr } 748c8c7007SAndreas Gohr 758c8c7007SAndreas Gohr /** 768c8c7007SAndreas Gohr * Save a new embedded diagram 778c8c7007SAndreas Gohr * 7859e7180eSAndreas Gohr * @param Doku_Event $event AJAX_CALL_UNKNOWN 798c8c7007SAndreas Gohr */ 8059e7180eSAndreas Gohr public function handleSave(Doku_Event $event) 818c8c7007SAndreas Gohr { 82317bdfc2SAndreas Gohr if ($event->data !== 'plugin_diagrams_embed_save') return; 838c8c7007SAndreas Gohr $event->preventDefault(); 848c8c7007SAndreas Gohr $event->stopPropagation(); 858c8c7007SAndreas Gohr 868c8c7007SAndreas Gohr global $INPUT; 878c8c7007SAndreas Gohr 888c8c7007SAndreas Gohr $id = $INPUT->str('id'); 89317bdfc2SAndreas Gohr $svg = $INPUT->str('svg'); 908c8c7007SAndreas Gohr $pos = $INPUT->int('pos'); 918c8c7007SAndreas Gohr $len = $INPUT->int('len'); 928c8c7007SAndreas Gohr 938c8c7007SAndreas Gohr 948c8c7007SAndreas Gohr if (auth_quickaclcheck($id) < AUTH_EDIT) { 958c8c7007SAndreas Gohr http_status(403); 968c8c7007SAndreas Gohr return; 978c8c7007SAndreas Gohr } 988c8c7007SAndreas Gohr 998c8c7007SAndreas Gohr if (!page_exists($id)) { 1008c8c7007SAndreas Gohr http_status(404); 1018c8c7007SAndreas Gohr return; 1028c8c7007SAndreas Gohr } 1038c8c7007SAndreas Gohr 1048c8c7007SAndreas Gohr if (!checkSecurityToken()) { 1058c8c7007SAndreas Gohr http_status(403); 1068c8c7007SAndreas Gohr return; 1078c8c7007SAndreas Gohr } 1088c8c7007SAndreas Gohr 109146e874bSAndreas Gohr if (empty($svg) || substr($svg, 0, 4) !== '<svg') { 110146e874bSAndreas Gohr http_status(400); 111146e874bSAndreas Gohr return; 112146e874bSAndreas Gohr } 113146e874bSAndreas Gohr 11495ed8ca0SAndreas Gohr if (!$this->helper->isDiagram($svg)) { 11595ed8ca0SAndreas Gohr http_status(400); 11695ed8ca0SAndreas Gohr return; 11795ed8ca0SAndreas Gohr } 11895ed8ca0SAndreas Gohr 1198c8c7007SAndreas Gohr $original = rawWiki($id); 1208c8c7007SAndreas Gohr $new = substr($original, 0, $pos) . $svg . substr($original, $pos + $len); 1218c8c7007SAndreas Gohr saveWikiText($id, $new, $this->getLang('embedSaveSummary')); 1228c8c7007SAndreas Gohr unlock($id); 1238c8c7007SAndreas Gohr echo 'OK'; 1248c8c7007SAndreas Gohr } 1258c8c7007SAndreas Gohr 1268c8c7007SAndreas Gohr} 1278c8c7007SAndreas Gohr 128