1<?php 2/** 3 * DokuWiki Plugin diagramsnet (Action Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author https://github.com/todag 7 * 8 */ 9 10// must be run within Dokuwiki 11if(!defined('DOKU_INC')) { 12 die(); 13} 14 15class action_plugin_diagramsnet extends DokuWiki_Action_Plugin { 16 17 public function register(Doku_Event_Handler $controller) { 18 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_ajax_call'); 19 } 20 21 /** 22 * handle ajax requests 23 */ 24 function _ajax_call(Doku_Event $event, $param) { 25 if ($event->data !== 'plugin_diagramsnet') { 26 return; 27 } 28 //no other ajax call handlers needed 29 $event->stopPropagation(); 30 $event->preventDefault(); 31 32 //e.g. access additional request variables 33 global $INPUT; 34 global $conf; 35 $data = $INPUT->str('data'); 36 $action = $INPUT->str('action'); 37 38 // Initialize $result object 39 $result = new StdClass; 40 41 /** 42 * Check ACL before writing any data. 43 * AUTH_DELETE (int 16) is needed for editing. 44 * According DokuWiki AUTH_UPLOAD (int 8) is enough to upload 45 * a *new* file. This permission level is not supported in 46 * this plugin currently. 47 */ 48 if ($action == 'save' && auth_quickaclcheck(cleanID($data)) >= AUTH_DELETE) { 49 /** 50 * Check ACL and make sure namespace has a media directory before trying to save. 51 * 52 */ 53 $content = $INPUT->str('content'); 54 $base64data = explode(",", $content)[1]; 55 56 $continue = true; 57 58 // Check that the media directory exists 59 if($continue == true && !file_exists(dirname(mediaFN($data))) && !mkdir(dirname(mediaFN($data)), 0755, true)) { 60 $result->message = 'Unable to create namespace media directory!'; 61 $continue = false; 62 } 63 64 // If attic is enabled, save old revision 65 if($continue == true && $this->getConf('enable_attic') == 1) { 66 media_saveOldRevision($data); 67 } 68 69 // Open file for writing 70 if($continue == true && !$whandle = fopen(mediaFN($data),'w')) { 71 $result->message = 'Unable to open file handle!'; 72 $continue = false; 73 } 74 75 // Write data to file 76 if($continue == true && !fwrite($whandle, base64_decode($base64data))) { 77 $result->message = 'Unable to write data to file!'; 78 } else { 79 $result->message = 'success'; 80 } 81 82 // Close and finish up 83 fclose($whandle); 84 header('Content-Type: application/json'); 85 echo json_encode($result); 86 87 } elseif ($action == 'save') { 88 $result->message = 'Permission denied!'; 89 header('Content-Type: application/json'); 90 echo json_encode($result); 91 } 92 93 if($action == 'get' && auth_quickaclcheck(cleanID($data)) >= AUTH_READ) { 94 if(file_exists(mediaFN($data)) && $fc = file_get_contents(mediaFN($data))) { 95 // File read successfully 96 $result->message = 'success'; 97 $result->content = "data:image/png;base64,".base64_encode($fc); 98 } elseif (file_exists(mediaFN($data)) && !$fc = file_get_contents(mediaFN($data))) { 99 // Failed to read existing file 100 $result->message = 'Failed to read data from file!'; 101 } else { 102 // No existing file, assume user is creating new file 103 $result->message = 'success'; 104 $result->content = "data:image/png;base64,"; 105 } 106 107 // Return result 108 header('Content-Type: application/json'); 109 echo json_encode($result); 110 } elseif ($action == 'get') { 111 $result->message = 'Permission denied!'; 112 header('Content-Type: application/json'); 113 echo json_encode($result); 114 } 115 } 116} 117?> 118