1<?php
2/**
3 * Plugin Now: Inserts a timestamp.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Carlo Perassi <carlo@perassi.org>
7 */
8
9// based on http://wiki.splitbrain.org/plugin:tutorial
10
11// must be run within Dokuwiki
12if (!defined('DOKU_INC')) die();
13
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
15require_once(DOKU_PLUGIN . 'syntax.php');
16
17
18require_once(DOKU_PLUGIN . 'mantis/lib/nusoap.php');
19
20/**
21 * All DokuWiki plugins to extend the parser/rendering mechanism
22 * need to inherit from this class
23 */
24class syntax_plugin_mantis extends DokuWiki_Syntax_Plugin {
25
26
27	private $sPath = "data/cache/mantis/";
28
29    function getInfo() {
30        return array(
31        'author'  => 'Christoph Lang',
32        'email'   => 'calbity@gmx.de',
33        'date'    => '2010-04-05',
34        'name'    => 'Mantis Bug Tracker',
35        'desc'    => 'Show Bugs from Mantis',
36        'url'     => 'http://www.sdzecom.de'
37        );
38    }
39
40    private function _load($filename){
41
42		$Cache = null;
43		$Update = true;
44		if(file_exists($filename)){
45			$Content = file_get_contents($filename);
46			$Content = unserialize($Content);
47
48			$Update = $Content["Update"];
49
50			if(time() > $Update)
51				$Update = true;
52			else
53				$Update = false;
54
55			$Cache = $Content["Content"];
56
57		}
58
59		return array($Update,$Cache);
60    }
61
62    private function _save($filename,$rs,$timestamp){
63    		$timestamp = (time() + ($timestamp*60));
64    		$Cache = array();
65    		$Cache["Update"] = $timestamp;
66			$Cache["Content"] = $rs;
67			$Cache = serialize($Cache);
68			$handle = fopen($filename,"w");
69			fwrite($handle,$Cache);
70			fclose($handle);
71
72    }
73
74	private function getBox($text){
75
76		$sText = '<div class="redbox" style="text-align: left; border: 1px solid #BB8C8C; background-color: #ECDEDE; padding: 7px 10px; margin: 10px 0;">'.$text.'</div>';
77		return $sText;
78
79	}
80    private function replace($data) {
81
82		$info = $this->getInfo();
83		$sReturn = "";
84		if(!class_exists("soapclient")){
85        	$text = str_replace('[url]',$info['url'],$this->getLang('nosoap'));
86        	$sReturn =  $this->getBox($text);
87			return $sReturn;
88		}
89
90		$server = $this->getConf('mantis_server');
91		$user = $this->getConf('mantis_user');
92		$pass = $this->getConf('mantis_password');
93		$refresh = $this->getConf('mantis_refresh');
94		$limit = $this->getConf('mantis_limit');
95
96		if(empty($server) || empty($user) || empty($pass)){
97        	$text = str_replace('[url]',$info['url'],$this->getLang('noconfig'));
98        	$sReturn =  $this->getBox($text);
99			return $sReturn;
100        }
101
102      	if(!is_dir($this->sPath))
103      		mkdir($this->sPath);
104
105		$filename = $this->sPath.md5(implode(".",$data));
106
107		$Cache = $this->_load($filename);
108
109		//print_r($Cache);
110		if(is_array($Cache)){
111			if($Cache[0] == false)
112					return $Cache[1];
113		}
114
115		if (extension_loaded('soap'))
116			$client = new soapclient($server);
117		else
118			$client = new soapclient($server,true);
119
120		$sReturn .= "<h1>Project: ";
121		if(!is_numeric($data[1])){
122			try{
123				if (extension_loaded('soap')){
124					$return = $client->mc_projects_get_user_accessible($user,$pass);
125
126					$i=1;
127					$project_name = $data[$i];
128
129					while(isset($project_name) && !empty($project_name)){
130
131						if($return == false){
132							$text = str_replace(array('[url]','[project]'),array($server,$project_name),$this->getLang('accessdenied'));
133        					$sReturn =  $this->getBox($text);
134							return $sReturn;
135						}
136
137						if(empty($return)){
138							$text = str_replace(array('[url]','[project]'),array($info['url'],$project_name),$this->getLang('projectnotfound'));
139        					$sReturn =  $this->getBox($text);
140							return $sReturn;
141						}
142
143						foreach($return as $project){
144							if($project->name == $project_name){
145								$projectid = $project->id;
146								$return = $project->subprojects;
147							}
148						}
149						if($i > 1)
150							$sReturn .= " > ";
151						$sReturn .= $project_name." (".$projectid.")";
152						$i++;
153						$project_name = $data[$i];
154					}
155
156						if(empty($projectid)){
157							$text = str_replace(array('[url]','[project]'),array($info['url'],$data[($i-1)]),$this->getLang('projectnotfound'));
158        					$sReturn =  $this->getBox($text);
159							return $sReturn;
160						}
161				}else{
162					$return = $client->call("mc_projects_get_user_accessible",array($user,$pass));
163					$i=1;
164					$project_name = $data[$i];
165
166					while(isset($project_name) && !empty($project_name)){
167
168						if($return == false){
169							$text = str_replace(array('[url]','[project]'),array($server,$project_name),$this->getLang('accessdenied'));
170        					$sReturn =  $this->getBox($text);
171							return $sReturn;
172						}
173
174						if(empty($return)){
175							$text = str_replace(array('[url]','[project]'),array($info['url'],$project_name),$this->getLang('projectnotfound'));
176        					$sReturn =  $this->getBox($text);
177							return $sReturn;
178						}
179
180						foreach($return as $project){
181							if($project["name"] == $project_name){
182								$projectid = $project["id"];
183								$return = $project["subprojects"];
184							}
185						}
186						if($i > 1)
187							$sReturn .= " > ";
188						$sReturn .= $project_name." (".$projectid.")";
189						$i++;
190						$project_name = $data[$i];
191					}
192
193						if(empty($projectid)){
194							$text = str_replace(array('[url]','[project]'),array($info['url'],$data[($i-1)]),$this->getLang('projectnotfound'));
195        					$sReturn =  $this->getBox($text);
196							return $sReturn;
197						}
198				}
199
200			}catch(Exception $ex){
201
202				$text = str_replace('[url]',$server,$this->getLang('accessdenied'));
203        		$sReturn =  $this->getBox($text);
204				return $sReturn;
205			}
206		}else{
207
208			$projectid = $data[1];
209		}
210
211		// make the call
212		try{
213			if (extension_loaded('soap'))
214				$return = $client->mc_project_get_issues($user,$pass,$projectid,1,$limit);
215			else
216				$return = $client->call("mc_project_get_issues",array($user,$pass,$projectid,1,$limit));
217		}catch(Exception $ex){
218	        $text = str_replace('[url]',$info['url'],$this->getLang('accessdenied'));
219        	$sReturn =  $this->getBox($text);
220			return $sReturn;
221		}
222
223
224		if(empty($return)){
225        	$text = str_replace('[url]',$info['url'],$this->getLang('noissuesfound'));
226        	$sReturn =  $this->getBox($text);
227			return $sReturn;
228
229		}
230		$sReturn .= " - Issues: ".count($return)."</h1>";
231
232		$sReturn .= '<table class="inline" style="width:100%;">';
233
234		$i=0;
235
236		$sReturn .= '<tr class="row'.$i.'">';
237		$sReturn .= '<th class="col0">'.$this->getLang('table_summary').'</th><th class="col1">'.$this->getLang('table_reporter').'</th><th class="col2">'.$this->getLang('table_description').'</th>';
238		$sReturn .= '</tr>';
239
240		foreach($return as $key => $value){
241
242			$i++;
243			if (extension_loaded('soap')){
244
245				$sReturn .= '<tr class="row'.$i.'">';
246				$sReturn .= '<td class="col0">';
247
248				if($value->status->id == 60)
249					$sReturn .= "<del>";
250
251				$sReturn .= $value->summary;
252
253				if($value->status->id == 60)
254					$sReturn .= "</del>";
255
256				$sReturn .= '</td>';
257				$sReturn .= '<td class="col1">';
258
259				if(!empty($value->reporter->real_name))
260					$sReturn .= $value->reporter->name;
261				else
262					$sReturn .= $value->reporter->real_name;
263
264				$sReturn .= '</td>';
265				$sReturn .= '<td class="col2">';
266				$sReturn .= nl2br(trim($value->description));
267				$sReturn .= '</td>';
268				$sReturn .= '</tr>';
269
270			}else{
271
272				$sReturn .= '<tr class="row'.$i.'">';
273				$sReturn .= '<td class="col0">';
274
275				if($value["status"]["id"] == 60)
276					$sReturn .= "<del>";
277
278				$sReturn .= $value["summary"];
279
280				if($value["status"]["id"] == 60)
281					$sReturn .= "</del>";
282
283				$sReturn .= '</td>';
284				$sReturn .= '<td class="col1">';
285
286				if(!empty($value["reporter"]["real_name"]))
287					$sReturn .= $value["reporter"]["name"];
288				else
289					$sReturn .= $value["reporter"]["real_name"];
290
291				$sReturn .= '</td>';
292				$sReturn .= '<td class="col2">';
293				$sReturn .= nl2br(trim($value["description"]));
294				$sReturn .= '</td>';
295				$sReturn .= '</tr>';
296
297
298
299			}
300
301		}
302
303		$sReturn .= '</table>';
304
305		$sReturn = utf8_encode($sReturn);
306
307		$this->_save($filename,$sReturn,$refresh);
308
309        return $sReturn;
310    }
311
312    function connectTo($mode) {
313
314		//$this->Lexer->addSpecialPattern('\[\[Mantis\:.*?\]\]', $mode, 'plugin_mantis');
315		$this->Lexer->addSpecialPattern('{{Mantis>.*?}}', $mode, 'plugin_mantis');
316    }
317
318    function getType() { return 'substition'; }
319
320    function getSort() { return 215; }
321
322    function handle($match, $state, $pos, &$handler) {
323
324    	$match = substr($match,2,-2);
325    	$match = str_replace(":",">",$match);
326    	$arrData = explode(">",$match);
327
328        return $arrData;
329    }
330
331    function render($mode, &$renderer, $data) {
332
333        if ($mode == 'xhtml') {
334            $renderer->doc .= $this->replace($data);
335            return true;
336        }
337        return false;
338    }
339}
340