1<?php
2/**
3 * JGraphX diagram editor plugin, based on Hotdraw Plugin
4 */
5
6if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
7if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
8require_once(DOKU_PLUGIN.'syntax.php');
9
10class syntax_plugin_jdraw extends DokuWiki_Syntax_Plugin {
11
12    function getInfo() {
13        return array(
14            'author' => 'Pavel Vlasov',
15            'email'  => 'Pavel.Vlasov@hammurapi.com',
16            'date'   => '2010-04-06',
17            'name'   => 'jDraw Plugin',
18            'desc'   => 'Uses JGraphX diagram editor to draw diagrams - Based on draw plug-in.',
19            'url'    => 'http://wiki.splitbrain.org/plugin:jdraw',
20        );
21    }
22
23    function getType() {
24    	return 'substition';
25    }
26
27    function getSort() {
28    	return 303;
29    }
30
31    function connectTo($mode) {
32    	$this->Lexer->addSpecialPattern("{{jdraw>.+?}}",$mode,'plugin_jdraw');
33    }
34
35    /**
36     * Handle the match
37     */
38    function handle($match, $state, $pos, &$handler) {
39        return substr($match,8,-2);
40    }
41
42    /**
43     * Create output
44     */
45    function render($mode, &$renderer, $indata) {
46    	$name = $indata;
47
48    	global $conf;
49
50      	if($mode != 'xhtml') {
51      		return false;
52      	}
53
54        //check name
55        $name = strtolower($name);
56        if (strlen($name)==0) {
57          $renderer->doc.="---INVALID_DIAGRAM---";
58          return true;
59        }
60
61        $namespace="";
62        $lastColonPos = strripos($name,":");
63        if ($lastColonPos>0) {
64        	$namespace=substr($name, 0, $lastColonPos);
65        	$name = substr($name, $lastColonPos+1);
66        }
67
68        $namespace.=':';
69        $media_dir = $conf['mediadir'].'/'.str_replace(":","/",$namespace);
70        if (! file_exists($media_dir)) {
71        	mkdir ($media_dir);
72	        chmod ($media_dir, 0777);
73        }
74
75        $image_file = $name.'.'.$this->getConf('image_format');
76
77		if (! is_readable($media_dir.$image_file)) {
78			$image_file = $name.'.gif'; // for seamless migration from the previous version.
79		}
80
81
82		$types=getMimeTypes();
83		if (! $types['mxe']) {
84			msg("jDraw plugin needs entry in mime.conf: \"mxe text/xml\"",-1);
85		}
86
87		$random = rand(0,100000); //to prevent JS errors if one diagram is on a page mupltiple times
88
89        //base URL to fetch every file
90        $fetchpath = DOKU_BASE."lib/exe/fetch.php?media=$namespace";
91        $image_id = "jdraw_image__$random";
92        $renderer->doc.="<img src={$fetchpath}{$image_file}' id='{$image_id}' alt=' [diagram] '>";
93
94		$auth = auth_quickaclcheck("{$namespace}*");
95		if ($auth >= AUTH_DELETE) {
96			$appletQueryString="dokuBase=".bin2hex(DOKU_BASE);
97			$appletQueryString.="&authtok=".auth_createToken();
98			$appletQueryString.="&sectok=".getSecurityToken();
99			$appletQueryString.="&sessionName=".session_name();
100			$appletQueryString.="&sessionId=".session_id();
101			$appletQueryString.="&name=".bin2hex($indata);
102			$appletQueryString.="&image_format=".$this->getConf("image_format");
103			$appletQueryString.="&palettes=".bin2hex($this->getConf("palettes"));
104			//$appletQueryString.="&dokuCookie=".bin2hex(DOKU_COOKIE);
105			//$appletQueryString.="&dokuCookieValue=".bin2hex($_COOKIE[DOKU_COOKIE]);
106
107			if (! is_readable($media_dir.$name.".mxe")) {
108				$appletQueryString.="&isNew=yes";
109			}
110
111			$appletQueryString.="&rnd=".$random; // To prevent serving of cached page.
112
113	        $renderer->doc.="<a class='diagbutton' target='_jdraw_{$random}' href='".DOKU_BASE."lib/plugins/jdraw/jdraw.php?".$appletQueryString."'>Edit</a><br>";
114	    }
115
116	    return true;
117    }
118}
119