1<?php
2    /*
3     * plugin should use this method to register its handlers
4     * with the dokuwiki's event controller
5     */
6
7    if(!defined('DOKU_INC')) die();
8
9
10    class action_plugin_drawio extends DokuWiki_Action_Plugin {
11
12        public function register(Doku_Event_Handler $controller) {
13            $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'addjsinfo');
14            $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this,'_ajax_call');
15            // $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ());
16        }
17
18        // function insert_button(Doku_Event $event, $param) {
19        //     $event->data[] = array (
20        //         'type' => 'format',
21        //         'title' => $this->getLang('abutton'),
22        //         'icon' => '../../plugins/drawio/icon.png',
23        //         'open' => '<abutton>',
24        //         'close' => '',
25        //         'block' => false,
26        //     );
27        // }
28
29        /**
30         *  add drawio config options to jsinfo
31         */
32
33	    function addjsinfo($event, $params){
34            global $JSINFO;
35	        $JSINFO['plugin_drawio'] = array(
36                'zIndex' => $this->getConf('zIndex'),
37                'url' => $this->getConf('url'),
38                'toolbar_possible_extension' => array_map('trim', explode(",",$this->getConf('toolbar_possible_extension')))
39            );
40	    }
41
42        /**
43         * handle ajax requests
44         */
45        function _ajax_call(Doku_Event $event, $param) {
46            if ($event->data !== 'plugin_drawio') {
47                return;
48            }
49            //no other ajax call handlers needed
50            $event->stopPropagation();
51            $event->preventDefault();
52
53            //e.g. access additional request variables
54            global $conf, $lang;
55            global $INPUT; //available since release 2012-10-13 "Adora Belle"
56            $name = $INPUT->str('imageName');
57            $action = $INPUT->str('action');
58
59            $suffix = strpos($action, "draft_") === 0 ? '.draft':'';
60            $media_id = $name . $suffix;
61			$media_id = cleanID($media_id);
62			$fl = mediaFN($media_id);
63
64			// Get user info
65			global $USERINFO;
66			global $INPUT;
67			global $INFO;
68
69			$user = $INPUT->server->str('REMOTE_USER');
70			$groups = (array) $USERINFO['grps'];
71			$auth_ow = (($conf['mediarevisions']) ? AUTH_UPLOAD : AUTH_DELETE);
72			$id = cleanID($name);
73
74			// Check ACL
75			$auth = auth_aclcheck($id, $user, $groups);
76			$access_granted = ($auth >= $auth_ow);
77
78			// AJAX request
79			if ($action == 'get_auth')
80			{
81                $json = new JSON();
82				echo $json->encode($access_granted);
83				return;
84            }
85						;
86			if (!$access_granted)
87				return array($lang['media_perm_upload'], 0);
88
89			io_makeFileDir($fl);
90		    if($action == 'save'){
91
92				$old = @filemtime($fl);
93				if(!file_exists(mediaFN($media_id, $old)) && file_exists($fl)) {
94					// add old revision to the attic if missing
95					media_saveOldRevision($media_id);
96				}
97				$filesize_old = file_exists($fl) ? filesize($fl) : 0;
98
99				// prepare directory
100				io_createNamespace($media_id, 'media');
101
102                // Write content to file
103                $content = $INPUT->str('content');
104                $base64data = explode(",", $content)[1];
105                //$whandle = fopen($file_path,'w');
106                $whandle = fopen($fl, 'w');
107                fwrite($whandle,base64_decode($base64data));
108                fclose($whandle);
109
110				@clearstatcache(true, $fl);
111				$new = @filemtime($fl);
112				chmod($fl, $conf['fmode']);
113
114				// Add to log
115				$filesize_new = filesize($fl);
116				$sizechange = $filesize_new - $filesize_old;
117				if ($filesize_old != 0) {
118				    addMediaLogEntry($new, $media_id, DOKU_CHANGE_TYPE_EDIT, '', '', null, $sizechange);
119				} else {
120					addMediaLogEntry($new, $media_id, DOKU_CHANGE_TYPE_CREATE, $lang['created'], '', null, $sizechange);
121                }
122            }
123            if($action == 'get_png'){
124				if (!file_exists($fl)) return;
125                // Return image in the base64 for draw.io
126                $json = new JSON();
127                header('Content-Type: application/json');
128                //$fc = file_get_contents($file_path);
129                $fc = file_get_contents($fl);
130				echo $json->encode(array("content" => "data:image/png;base64,".base64_encode($fc)));
131            }
132            if($action == 'get_svg'){
133				if (!file_exists($fl)) return;
134                // Return image in the base64 for draw.io
135                $json = new JSON();
136                header('Content-Type: application/json');
137                //$fc = file_get_contents($file_path);
138                $fc = file_get_contents($fl);
139				echo $json->encode(array("content" => "data:image/svg+xml;base64,".base64_encode($fc)));
140            }
141
142            // Draft section
143            if($action == 'draft_save'){
144                // prepare directory
145                io_createNamespace($media_id, 'media');
146
147                // Format content of draft file
148                $json = new JSON();
149                $content = $INPUT->str('content');
150
151                // Write content to file
152                $whandle = fopen($fl, 'w');
153                fwrite($whandle, $content);
154                fclose($whandle);
155            }
156            if($action == 'draft_rm'){
157                unlink($fl);
158            }
159            if($action == 'draft_get'){
160                header('Content-Type: application/json');
161                $json = new JSON();
162                if (file_exists($fl)){
163                    echo file_get_contents($fl);
164                }else {
165                    echo $json->encode(array("content" => "NaN"));
166                }
167            }
168        }
169    }
170?>
171