1<?php
2/**
3 * DokuWiki Plugin simplechat (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
12class syntax_plugin_simplechat extends DokuWiki_Syntax_Plugin {
13    /**
14     * @return string Syntax mode type
15     */
16    public function getType() {
17        return 'substition';
18    }
19    /**
20     * @return string Paragraph type
21     */
22    public function getPType() {
23        return 'block';
24    }
25    /**
26     * @return int Sort order - Low numbers go before high numbers
27     */
28    public function getSort() {
29        return 155;
30    }
31
32    /**
33     * Connect lookup pattern to lexer.
34     *
35     * @param string $mode Parser mode
36     */
37    public function connectTo($mode) {
38        $this->Lexer->addSpecialPattern('~~simplechat~~',$mode,'plugin_simplechat');
39    }
40
41	private function chatroomform() {
42	global $INFO;
43		$result  = "";
44		$result .= "<div id='sc-wrap'>";
45		// $result .= "<h2>Simple Chat</h2>";
46		if( isset($INFO['userinfo'] ) || ($this->getConf('showanonymousip') == 1)) {
47			$result .= "<input type='hidden' id='sc-username' value='".$INFO['client']."'>";
48		} else {
49			$result .= "<input type='hidden' id='sc-username' value='anonymous'>";
50		}
51		$result .= "<input type='hidden' id='sc-roomname' value='".$INFO['id']."'>";
52		$result .= "<div id='sc-chatframe'><div id='sc-chatarea'></div></div>";
53		$result .= "<form id='sc-messagearea'><textarea id='sc-send' maxlength = '250'></textarea><p>Your message:</p></form>";
54		$result .= "</div><br style='clear:both;'>";
55		return $result;
56	}
57
58    /**
59     * Handle matches of the simplechat syntax
60     *
61     * @param string $match The match of the syntax
62     * @param int    $state The state of the handler
63     * @param int    $pos The position in the document
64     * @param Doku_Handler    $handler The handler
65     * @return array Data for the renderer
66     */
67    public function handle($match, $state, $pos, &$handler){
68        $data = array();
69        return $data;
70    }
71
72    /**
73     * Render xhtml output or metadata
74     *
75     * @param string         $mode      Renderer mode (supported modes: xhtml)
76     * @param Doku_Renderer  $renderer  The renderer
77     * @param array          $data      The data from the handler() function
78     * @return bool If rendering was successful.
79     */
80    public function render($mode, &$renderer, $data) {
81	global $INFO;
82	global $conf;
83
84        if($mode != 'xhtml') return false;
85		// check to see if chat directory is created
86		$dirname = DOKU_INC.'data/chats';
87		if( !is_dir( $dirname ) ) {
88			@mkdir($dirname, 0755 , true );
89		}
90		// see if we need to clean up an old chat log
91		if( $this->getConf('chatretentiontimer') > 0 ) {
92			$room = str_replace(array(' ','.','/',':'),array('','','-','-'),$INFO['id']); // need to clean this. remove spaces, remove dots , change slashes to underlines
93			$filename = DOKU_INC.'data/chats/log_'.$room.'.txt';
94			if( file_exists( $filename ) ) {
95				// count lines, see if we are over limit
96				$linecount = 0;
97				$linemax = $this->getConf('maxloglinecount');
98				$overlinecount = false;
99				$fh = @fopen( $filename, "r" );
100				if( $fh ) {
101					while(!feof($fh)){
102						$line = fgets($fh);
103						if( $linecount++ > $linemax ) {
104							$overlinecount = true;
105							break;
106						}
107					}
108					fclose($fh);
109				}
110				if( ((time() - filemtime( $filename ) ) > ( $this->getConf('chatretentiontimer') * 60 )) or $overlinecount ) {
111					if( $this->getConf('savelogsflag') == 1 ) {
112						$date = date_create();
113						$newdirname = DOKU_INC.'data/chats/'.$room ;
114						$newfilename = DOKU_INC.'data/chats/'.$room.'/log_'.date("Y-m-d_H:i:s").'.txt';
115						@mkdir( $newdirname );
116						@rename( $filename , $newfilename );
117					} else {
118						@unlink( $filename ); // its too old. remove it.
119					}
120				}
121			}
122		}
123
124		$renderer->doc .= $this->chatroomform();
125        return true;
126    }
127}
128
129// vim:ts=4:sw=4:et:
130