1<?php
2/**
3 * DokuWiki Plugin xymon
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Erwan Martin <public@fzwte.net>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once DOKU_PLUGIN.'action.php';
17
18class action_plugin_xymon extends DokuWiki_Action_Plugin {
19
20    public function register(Doku_Event_Handler &$controller) {
21       $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handle_update');
22    }
23
24
25    public function handle_update(Doku_Event &$event, $param) {
26		global $conf;
27
28        $xymon_color="clear";
29
30        //find the date of the update message cache file
31		$update_message_cache_filename = $conf['cachedir'].'/messages.txt';
32		if(!file_exists($update_message_cache_filename)) {
33		    throw new Exception('Could not find update messages file: '.$update_message_cache_filename);
34		}
35		$update_message_cache_date = @filemtime($update_message_cache_filename);
36
37		//find the date of the xymon cache file
38		$xymon_status_cache_file = $conf['datadir']."/xymon/xymonstatus.txt";
39		if(file_exists($xymon_status_cache_file)) {
40            $xymon_status_cache_file_date = @filemtime($xymon_status_cache_file);
41		}
42		else {
43			$xymon_status_cache_file_date = 0;
44		}
45
46		//if the update message cache file is newer, update the xymon status page
47		if($update_message_cache_date > $xymon_status_cache_file_date) {
48            $data = io_readFile($update_message_cache_filename);
49		    $msgs = explode("\n%\n",$data);
50		    foreach($msgs as $msg){
51		        if($msg) {
52                    if (preg_match('/security/i', $msg)) {
53						$xymon_color = 'red';
54						break;
55					}
56					if(!preg_match('/^New release candidate/i', $msg)) {
57						$xymon_color = 'yellow';
58					}
59				}
60		    }
61			$xymon_color = $xymon_color == 'clear' ? 'green' : $xymon_color;
62
63			if (!file_exists($conf['datadir'].'/xymon/')) {
64				@mkdir($conf['datadir'].'/xymon/');
65			}
66			$fp = !file_exists($xymon_status_cache_file) || is_writable($xymon_status_cache_file) ? fopen($xymon_status_cache_file, "w") : 0;
67			if ($fp)	{
68				fwrite($fp, "xymon_color: ".$xymon_color."\n\n");
69				fwrite($fp, $data);
70				fclose($fp);
71			}
72			else {
73				msg("Xymon plugin: could not create file ".$xymon_status_cache_file, -1);
74			}
75		}
76	}
77
78}
79