uses perms field from auth groupex
[wikifarm.git] / install / cookbook / chat.php
CommitLineData
b1b3c24c
DB
1<?php
2
3$ROSPatterns['/\\(:chat(\\s+(.*))?:\\)/e'] = "CreateChat('$2')";
4$HandleActions['chat'] = 'HandleChat';
5
6Markup('chatBegin','inline','/\\(:chatBegin (\\w+):\\)/','');
7Markup('chatEnd','inline','/\\(:chatEnd (\\w+):\\)/','<br/>');
8Markup('chatNext','inline','/\\(:chatNext:\\)/','<br/>');
9Markup('chatTalk','inline','/\\(:chatTalk (\\w+) (\\w+):\\)/e',"ChatTalk('$1', '$2')");
10
11function CreateChat($size = 10) {
12 $idchat = substr(md5(rand()),0,4);
13 if (!$size) { $size = 10; }
14 return
15 '(:chatBegin '.$idchat.':)'."\n".
16 '(:chatEnd '.$idchat.':)'."\n\n".
17 '(:if auth edit:)(:chatTalk '.$idchat.' '.$size.':)(:if:)';
18}
19
20function ChatTalk($idchat, $size = 10) {
21 return Keep(
22 '<form method="post" id="chat-'.$idchat.'">
23 <input type="text" name="postchat" size="50"/>
24 <input type="hidden" name="chatId" value="'.$idchat.'"/>
25 <input type="hidden" name="size" value="'.$size.'"/>
26 <input type="hidden" name="action" value="chat"/>
27 </form>
28 <script type="text/javascript">
29 document.getElementById("chat-'.$idchat.'").postchat.focus();
30 </script>');
31}
32
33function HandleChat() {
34 global $pagename, $CurrentTime, $Author;
35 Lock(2);
36 if (isset($_REQUEST['postchat']) && $_REQUEST['postchat'] &&
37 ($page = RetrieveAuthPage($pagename, 'edit', true))) {
38 $postchat = stripmagic($_REQUEST['postchat']);
39 $idchat = stripmagic($_REQUEST['chatId']);
40 $size = stripmagic($_REQUEST['size']);
41
42 $origine = array('(:chatNext:)');
43 $destination = array('');
44 $chatsyntax = RetrieveAuthPage('Site.ChatSyntax', 'read', false);
45 if ($chatsyntax) {
46 $chatsyntax = explode("\n",$chatsyntax['text']);
47 foreach($chatsyntax as $s) if (preg_match('/^(.*) => (.*)$/', trim($s), $matches)) {
48 $origine[] = $matches[1];
49 $destination[] = $matches[2];
50 }
51 }
52
53 $newchat = $CurrentTime.' [[~'.$Author.']] : '.str_replace($origine, $destination, $postchat);
54
55 $talkpos = strpos($page['text'], '(:chatTalk '.$idchat.' ');
56 $beginpos = strpos($page['text'], '(:chatBegin '.$idchat.':)');
57 $endpos = strpos($page['text'], '(:chatEnd '.$idchat.':)');
58
59 $beginpos += strlen('(:chatBegin '.$idchat.':)');
60 $lignes = explode('(:chatNext:)',substr($page['text'], $beginpos, $endpos - $beginpos));
61
62 if ($talkpos > $beginpos) {
63 $afac = count($lignes) - $size - 1;
64 $lignes[] = $newchat;
65 for ($i = 0; $i < $afac; $i++) {
66 unset($lignes[$i]);
67 }
68 } else {
69 $nouvelleslignes = array($newchat);
70 for ($i = 0; $i < count($lignes) && $i < $size - 1; $i++) {
71 $nouvelleslignes[] = $lignes[$i];
72 }
73 $lignes = $nouvelleslignes;
74 }
75 $chatcontent = implode('(:chatNext:)', $lignes);
76 $page['text'] = substr($page['text'],0,$beginpos).$chatcontent.substr($page['text'],$endpos);
77 WritePage($pagename,$page);
78 Lock(0);
79 Redirect($pagename);
80 }
81 Lock(0);
82 return "";
83}
84function afac() {
85 global $pagename, $Author, $CurrentTime;
86 if (isset($_REQUEST['postchat']) && $_REQUEST['postchat']) {
87 Lock(2);
88 $page = RetrieveAuthPage($pagename, 'edit', false);
89 if (!$page) {
90 Lock(0);
91 return "";
92 }
93
94
95
96
97 $poschat = strpos($page['text'],'(:beginchat:)');
98 $finchat = strpos($page['text'],'(:chat');
99 if ($poschat === false) {
100 $poschat = $finchat;
101 $page['text'] = substr($page['text'],0,$poschat).'(:beginchat:)'.substr($page['text'],$poschat);
102 $finchat += strlen('(:beginchat:)');
103 }
104 $poschat += strlen('(:beginchat:)');
105
106 $avantchat = substr($page['text'],0,$poschat);
107 $chatcontent = substr($page['text'], $poschat, $finchat - $poschat);
108 $aprechat = substr($page['text'],$finchat);
109
110 $lignes = explode("\n\n",$chatcontent);
111 $page['text'] = $avantchat.implode("\n\n", $lignes)."\n\n".$CurrentTime.' [[~'.$Author.']] : '.$newchat.$aprechat;
112 WritePage($pagename,$page);
113 Lock(0);
114 Redirect($pagename);
115 return "";
116 }
117 $page = RetrieveAuthPage($pagename, 'edit', false);
118 if (!$page) {
119 return '';
120 }
121 return '<form method="post"><input type="text" name="postchat" size="50"/></form><script type="text/javascript">document.getElementsByName("postchat")[0].focus();</script>';
122}
123
124?>