1<?php
2/**
3 * DokuWiki Plugin whoisonline (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Matthew Mills <millsm@csus.edu>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12
13class syntax_plugin_whoisonline extends DokuWiki_Syntax_Plugin {
14    /**
15     * @return string Syntax mode type
16     */
17    public function getType() {
18        return 'substition';
19    }
20    /**
21     * @return string Paragraph type
22     */
23    public function getPType() {
24        return 'block';
25    }
26    /**
27     * @return int Sort order - Low numbers go before high numbers
28     */
29    public function getSort() {
30        return 155;
31    }
32
33	function getonlinelist( $displaymode ){
34	global $INFO;
35	global $conf;
36
37		// read in the file of peoples.
38		$filename = DOKU_PLUGIN . 'whoisonline/online.txt';
39		if( file_exists( $filename ) == true ) { // read in table
40			$online_users = json_decode(file_get_contents( $filename ), true);
41		} else { // first time. make dummy array
42			$online_users = array();
43		}
44
45		// add current user to new array
46		$displaypattern = $this->getConf('displayline');
47		if( isset($INFO['userinfo']) || ($this->getConf('ignoreAnonymous')==0)){
48			$userdisplay = str_replace( array( 	"{username}" ,
49												"{pageid}" ,
50												"{fullname}" ,
51												"{url}" ) ,
52										array(	$INFO["client"] ,
53												$INFO["id"] ,
54												$INFO["userinfo"]["name"] ,
55												$_SERVER["REQUEST_URI"] ) ,
56										$displaypattern );
57			$newlist = array(array("login"=>$INFO["client"] ,
58			"timeseen"=>time() ,
59			"display"=>$userdisplay)); // add current user
60		} else {
61			$newlist = array(); // if user is anonymous and we ignore them make empty new list
62		}
63		// transfer old list to new list removing old people
64		$maxtime = $this->getConf('minutesTillAway')*60;
65		if( sizeof($online_users) > 0 ) {
66			foreach( $online_users as $user ) {
67				if( $user['login']!=$INFO["client"] ) { // skip the already added user
68					if( (time() - $user["timeseen"])  < $maxtime ) array_push( $newlist , $user );
69				}
70			}
71		}
72		$onlinecount = sizeof( $newlist );
73
74		// ========= write array back out to file =========
75		file_put_contents($filename, json_encode($newlist));
76
77		$result = "";
78		if( $displaymode != "NOSHOW" ) {
79			$result .= "<div class='WIO_onlineWidget'>";
80			$result .= "<div class='WIO_count'>".$onlinecount."</div>";
81			$result .= "<div class='WIO_label'>online</div>";
82			if( $displaymode != "NOLIST" ) {
83				$result .= "<div class='WIO_panel' style='display:none;'>Loading</div>";
84			}
85			$result .= "</div>";
86		}
87		return $result;
88	}
89
90    /**
91     * Connect lookup pattern to lexer.
92     *
93     * @param string $mode Parser mode
94     */
95    public function connectTo($mode) {
96        $this->Lexer->addSpecialPattern('~~whoisonline[\#\;\:]?(?:NOLIST|NOSHOW|noshow|nolist)?~~',$mode,'plugin_whoisonline');
97    }
98
99    /**
100     * Handle matches of the whoisonline syntax
101     *
102     * @param string $match The match of the syntax
103     * @param int    $state The state of the handler
104     * @param int    $pos The position in the document
105     * @param Doku_Handler    $handler The handler
106     * @return array Data for the renderer
107     */
108    public function handle($match, $state, $pos, &$handler){
109        $data = array();
110        $data['display'] = strtoupper(substr($match, -8,-2)) ;
111
112        return $data;
113    }
114
115    /**
116     * Render xhtml output or metadata
117     *
118     * @param string         $mode      Renderer mode (supported modes: xhtml)
119     * @param Doku_Renderer  $renderer  The renderer
120     * @param array          $data      The data from the handler() function
121     * @return bool If rendering was successful.
122     */
123    public function render($mode, &$renderer, $data) {
124        if($mode != 'xhtml') return false;
125		$renderer->doc .= $this->getonlinelist($data['display']);
126        return true;
127    }
128}
129