1<?php
2/**
3 * DokuWiki Plugin diagramsnet (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author https://github.com/todag
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class syntax_plugin_diagramsnet extends DokuWiki_Syntax_Plugin
15{
16    /**
17     * @return string Syntax mode type
18     */
19    public function getType() {
20        return 'substition';
21    }
22
23    private $fileMatchPattern;
24    function __construct() {
25	    /**
26	     * Get the file suffix from configuration and set in in class variable.
27	     * If this is done in connectTo it will poll the settings multiple times, which may or may not be an issue...
28	     */
29	    global $conf;
30	    $this->fileMatchPattern = '\{\{[^\}]+?' . str_replace('.', '\.', $this->getConf('file_match_suffix')) . '[^\}]*?\}\}';
31    }
32
33    /**
34     * @return int Sort order - Low numbers go before high numbers
35     */
36    public function getSort() {
37        return 319;
38    }
39
40    /**
41     * Connect lookup pattern to lexer.
42     *
43     * @param string $mode Parser mode
44     */
45    public function connectTo($mode) {
46	    $this->Lexer->addSpecialPattern($this->fileMatchPattern, $mode, 'plugin_diagramsnet');
47    }
48
49    /**
50     * Handle matches of the diagramsnet plugin syntax
51     *
52     * @param string       $match   The match of the syntax
53     * @param int          $state   The state of the handler
54     * @param int          $pos     The position in the document
55     * @param Doku_Handler $handler The handler
56     *
57     * @return array Data for the renderer
58     */
59    public function handle($match, $state, $pos, Doku_Handler $handler) {
60        $params = Doku_Handler_Parse_Media($match);
61	return $params;
62    }
63
64    /**
65     * Render xhtml output or metadata
66     *
67     * @param string        $mode     Renderer mode (supported modes: xhtml)
68     * @param Doku_Renderer $renderer The renderer
69     * @param array         $data     The data from the handler() function
70     *
71     * @return bool If rendering was successful.
72     */
73    public function render($mode, Doku_Renderer $renderer, $data) {
74	if ($mode !== 'xhtml') {
75            return false;
76        }
77
78	global $conf;
79
80	/**
81	 * Check ACL and show either editor or viewer
82	 *
83	 */
84	if(auth_quickaclcheck(cleanID($data['src'])) >= AUTH_DELETE) {
85	    // If permissions >= 16 return full editor
86	    if($this->getConf('app_source_type') == 'external') {
87		$externalUrl = $this->getConf('external_url').$this->getConf('editor_parameters');
88	    } else {
89                $externalUrl = DOKU_URL.'lib/plugins/diagramsnet/lib/'.$this->getConf('editor_parameters');
90	    }
91	} else {
92	    // If permissions < 16 return viewer
93	    if($this->getConf('app_source_type') == 'external'){
94                $externalUrl = $this->getConf('external_url').$this->getConf('viewer_parameters');
95	    } else {
96		$externalUrl = DOKU_URL.'lib/plugins/diagramsnet/lib/'.$this->getConf('viewer_parameters');
97	    }
98	}
99	if(auth_quickaclcheck(cleanID($data['src'])) >= AUTH_READ) {
100	    if(!file_exists(mediaFN($data['src']))) {
101	        $data['title'] = 'Click to create new diagram file ['.$data['src'].']';
102	    }
103
104	    $anonymize_xml = $this->getConf('anonymize_xml');
105
106	    $attr = array(
107                'class'   => 'media',
108		'id'      => $data['src'],
109		'onclick' => "diagramsnetEdit(this,'$externalUrl','".$data['src']."', $anonymize_xml)",
110		'style'   => 'cursor:pointer;',
111                'src'     => ml($data['src']),
112	    	'width'   => $data['width'],
113                'height'  => $data['height'],
114            	'align'   => $data['align'],
115		'title'   => $data['title']
116	    );
117
118            $renderer->doc .= '<img '.buildAttributes($attr).'/>';
119	} else {
120            $renderer->doc .= "<font color='red'>** plugin response: Permission denied for file: '".$data['src']."' **</font>";
121	}
122        return true;
123    }
124}
125