1<?php
2
3/**
4 * Ajax Chat plugin code
5 * Some functions borrowed from Esther Brunner's Discussion plugin.
6 *
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  Pavel Vitis <pavel [dot] vitis [at] seznam [dot] cz>
9 * @author  Dave Lawson <dlawson@masterytech.com>
10 * @author  Esther Brunner <esther@kaffeehaus.ch>
11 */
12
13global $ID;
14global $REV;
15global $ACT;
16
17if (chat_plugin::isChatID($ID) && file_exists(wikiFN($ID))) {
18  session_write_close();
19  if ($ACT == 'show') {
20    print "Redirecting...<script type='text/javascript'>document.location='".wl(chat_plugin::stripChatNS($ID), true)."#chat';</script>";
21    exit();
22  }
23}
24
25class chat_plugin {
26
27    /**
28     * @author  Esther Brunner <esther@kaffeehaus.ch>
29     * @author  Pavel Vitis <pavel [dot] vitis [at] seznam [dot] cz>
30     */
31    function chatHtml() {
32      global $ID;
33      global $ACT;
34      global $conf;
35      global $lang;
36
37      $dID = chat_plugin::addChatNS($ID);
38
39      // check mode and existing page
40      if (($ACT !== 'show' && $ACT !== 'export_xhtml') || !file_exists(wikiFN($ID))) {
41        return '';
42    }
43
44      // check for existing chat page if automatic is disabled.
45      if (!$conf['chat']['automatic'] && !file_exists(wikiFN($dID)))
46        return '';
47
48      // check for existing chat page.
49      if (!file_exists(wikiFN($dID))) {
50        if ($conf['chat']['automatic']) {
51          // automatic mode - don't try to recreate a deleted discussion page.
52          $revs = getRevisions($dID);
53          if (count($revs) != 0)
54            return '';
55        }
56        else {
57          // manual mode - don't create non-existing discussion pages.
58          return '';
59        }
60      }
61
62      $html = '';
63
64	if (!$_REQUEST['nosound'] && $conf['chat']['audio-notify']) {
65	    $html .= "<applet code=\"ChatNotify.class\" ".
66		"codebase=\"".DOKU_BASE."lib/plugins/chat/\" ".
67		"id=\"newmsg_sound\" width=\"1\" height=\"1\" hspace=\"0\" vspace=\"0\" style=\"height:0px;\">".
68		"<param name=\"audio\" value=\"newmsg\"></applet>\n";
69	}
70	else {
71	}
72
73      $html .= '<div class="chat">';
74//      $html .= '<h2 style="margin-left:0px !important;">'.$lang['chat_title'].'</h2>';
75
76      if ($conf['chat']['recentfirst']) {
77        $html .= chat_plugin::chatFormHtml($dID);
78        $html .= chat_plugin::chatPageHtml($dID);
79      } else {
80        $html .= chat_plugin::chatPageHtml($dID);
81        $html .= chat_plugin::chatFormHtml($dID);
82      }
83      $html .= '</div><!-- chat -->';
84
85      return $html;
86    }
87
88    /**
89     * Is specified page ID in a chat namespace?
90     *
91     * @author Dave Lawson <dlawson@masterytech.com>
92     */
93    function isChatID($ID) {
94	global $conf;
95	$dNS = $conf['chat']['namespace'];
96	$pID = noNS($ID);
97	return strpos($ID, "$dNS:$pID") !== false;
98    }
99
100    /**
101     * Convert a chat page ID to a normal page ID.
102     *
103     * @author Dave Lawson <dlawson@masterytech.com>
104     */
105    function stripChatNS($ID) {
106	global $conf;
107	$dNS = $conf['chat']['namespace'];
108	return str_replace("$dNS:", '', $ID);
109    }
110
111    /**
112     * Convert a normal page ID to a chat page ID.
113     *
114     * @author Dave Lawson <dlawson@masterytech.com>
115     */
116    function addChatNS($ID) {
117        global $conf;
118	$dNS = $conf['chat']['namespace'];
119        if (chat_plugin::isChatID($ID)) {
120	    return $ID;
121        }
122        $dID = getNS($ID);
123	if ($dID !== '') {
124	    $dID .= ':';
125        }
126	$dID .= $dNS.':'.noNS($ID);
127        return $dID;
128    }
129
130    /**
131     * Display existing comments if availaible
132     *
133     * @author Esther Brunner <esther@kaffeehaus.ch>
134     */
135    function chatPageHtml($dID) {
136	global $ID;
137
138	$html = '';
139
140	$dFN = wikiFN($dID);
141	if (file_exists($dFN)) {
142	    $html .= '<div id="chatListSentMessage" class="chat-line chat-message-sending" style="display:none;height:0px;"></div>';
143	    $html .= '<div id="chatList" class="chat-list">';
144	    $html .= '<em>Loading messages...</em>';
145//	    $html .= html_secedit(p_cached_xhtml($dFN), false);
146	    $html .= "</div>";
147	}
148
149	return $html;
150    }
151
152
153    /**
154     * @author  Pavel Vitis <pavel [dot] vitis [at] seznam [dot] cz>
155     */
156    function chatFormHtml($chatID) {
157	global $DATE;
158	global $INFO;
159	global $USERINFO;
160	global $PRE;
161	global $SUF;
162	global $SUM;
163	global $lang;
164	global $conf;
165	global $auth;
166	global $ID;
167	global $commentId;
168
169	$maxmsgsize = 1 * $conf['chat']['max-message-size'];
170        if ($maxmsgsize <= 0) {
171	    $maxmsgsize = 256;
172        }
173	if ($masmsgsize > 2*1024) {
174	    $maxmsgsize = 2*1024;
175        }
176
177	if ($conf['useacl'] && $_SERVER['REMOTE_USER'] /*&& auth_quickaclcheck($dID) >= AUTH_EDIT*/) {
178	    $anonymous = false;
179	}
180	else {
181	    $anonymous = true;
182	}
183
184	// only if we are NOT in a comment page itself
185	if (chat_plugin::isChatID($ID)) {
186	    $mainID = chat_plugin::stripChatNS($ID);
187	    echo '<hr noshade="noshade" size="1" />';
188	    echo '<div class="level1">'.html_wikilink($mainID,$lang['comments_back'].' '.$mainID).'</div>';
189	    return true;
190	}
191
192	// guest comments must be allowed or you need to be logged in
193	if (!$conf['chat']['allowguests'] && $_SERVER['REMOTE_USER'] == '') return '';
194
195	$refreshURL = wl(chat_plugin::stripChatNS($ID), 'rid='.time(), true).'#chat';
196
197	if ($anonymous) {
198	    $user = $_COOKIE['chatName'];
199	    if (empty($user)) {
200		$user = "guest".date(time(),'hms');
201	    }
202	}
203	else {
204	    $user = $INFO['userinfo']['name'];
205	}
206
207	$volume = (int)$_COOKIE['chatVolume'];
208	if ($volume < 0) {
209	    $volume = 0;
210	}
211	if ($volume > 4) {
212	    $volume = 4;
213	}
214
215//echo $_SERVER['REMOTE_USER'];
216	$html = '';
217
218        $html .=
219	    '<div class="chat-form">'.
220	    '<form action="" method="post" id="chatForm" name="chatForm" onsubmit="return false;">'.
221	    '<input type="hidden" name="pageId" value="'.$chatID.'" />';
222
223	if ($conf['useacl'] && auth_quickaclcheck($chatID) >= AUTH_EDIT)
224	{
225            $html .=
226    		($anonymous ?
227		'<input type="text" name="user" id="user" maxlength="30" autocomplete="off" value="'.$user.'" class="edit chat" style="width:100px !important;margin-right:5px;" />'
228		:
229		'<input type="hidden" name="user" value="'.$user.'" />'
230		).
231		'<input type="text" name="chatMessage" id="chatMessage" maxlength="'.$maxmsgsize.'" class="edit chat" autocomplete="off" style="margin-right:2px;" />';
232
233	    $html .=
234		'<input type="submit" value="Send" class="button" />';
235	}
236	if ($conf['useacl'] && $_SERVER['REMOTE_USER'] && auth_quickaclcheck($ID) >= AUTH_EDIT) {
237	    $html .=
238		'<input type="button" name="clearButton" value="Clear messages" class="button" />';
239	}
240
241        $html .=
242	    '</form>';
243
244	if (!$_REQUEST['nosound'] && $conf['chat']['audio-notify']) {
245            $html .= '<br /><div style="display:inline;padding-top:0px;margin-top:0px;margin-left:0px;padding-left:0px;vertical-align:middle;">'.
246        	'<div title="Set notify volume" style="display:inline;cursor:pointer;">'.
247		'<img id="chatVolumeIcon" src="'.DOKU_BASE.'lib/images/blank.gif" style="cursor:pointer;width:0px;margin-top:2px;" border="0" onclick="chatToggleVolume();" alt="" />'.
248		'</div>'.
249		'<script type="text/javascript">'.
250		'var vol = getCookie("chatVolume")*1;'.
251		'chatSetVolume(vol,true,false);'.
252		'</script></div>';
253	}
254	else {
255	    $html .= "<br />";
256	}
257
258	if ($conf['chat']['show-progress']) {
259	    $html .=
260		'<form action="" id="chat-info-pad" onsubmit="return false;" style="display:inline;">'.
261    		'<input class="edit" style="border:0px;" type="text" name="ajaxActive" id="ajaxActive" />'.
262    		'<input class="edit" style="border:0px;" type="text" name="ajaxRefreshInterval" id="ajaxRefreshInterval" />'.
263    		'<input class="edit" style="border:0px;width:90pt;" type="text" name="ajaxLastStatus" id="ajaxLastStatus" />'.
264    		'<input class="edit" style="border:0px;width:90pt;" type="text" name="requestTimerInfo" id="requestTimerInfo" />'.
265    		'<input class="edit" style="border:0px;width:90pt;" type="text" name="ajaxNextCheck" id="ajaxNextCheck" />'.
266		'</form>';
267	}
268
269	$html .= '</div>';
270
271	return $html;
272    }
273}
274
275//Setup VIM: ex: et ts=4 enc=utf-8 :
276