OSDN Git Service

e7f0673bf81b2c50185e6747b66664e6fb48cfe2
[trpgtools-onweb/AjaxChat.git] / main.php
1 <?php
2 /*
3  * Ajax Chat for TRPG ver.1.1.1
4  * (c)2007 Cake All righ1ts reserved.
5  * Mail : cake_67@users.sourceforge.jp
6  * Home : http://trpgtools-onweb.sourceforge.jp/
7  *
8  * [注意事項他]
9  * 本チャットスクリプトの使用および配布は、BSDライセンスに基づきます。
10  * BSDライセンスの詳細につきましては、添付のライセンス.txtを参照してください。
11  *
12  * BSDライセンス概要&使用上の注意
13  * 1. このスクリプトはフリーソフトです。以下の条件を満たす限りにおいて、使用・改造・再配布(オリジナルおよび改造版の両方とも)は自由です。
14  * 再配布する場合、上記著作権表示、本条件書きおよび第2項・第3項の責任限定規定を必ず含めてください。
15  * 2. 同梱のアイコンはPetite Prier様(http://snow.if.tv/)の素材です。
16       本スクリプトの使用および再配布時にアイコンをそのまま用いる場合は、配布元の規定も遵守してください。
17  * 3. 本スクリプトは無保証です。自己責任で使用してください。このスクリプトを使用したいかなる損害に対しても、作者は一切の責任を負いません。
18  * 4. 設置および使用方法に関する質問は、配布サイトの掲示板にお願いします。ただし、必ず回答できるとは限りません。
19  * メールによる質問は、ご遠慮ください。
20  */
21
22 // 設定ファイルの読み込み
23 require_once 'trpgchat-ini.php';
24
25 // HTML出力
26 print html_header('main', $title.' ログ表示画面', true, true);
27
28  ?>
29
30 <script type="text/javascript">
31 <!--
32
33 //変数の設定
34 var getdata_on = <?php print $getdata_on;?>*1000;
35 var getdata_off = <?php print $getdata_off;?>*1000;
36 var logfile= '<?php print $recent_log;?>';
37 var now_member = '<?php print $now_member;?>';
38 var color_system = '#<?php print $color_system;?>';
39 var status_color = '#<?php print $status_color; ?>';
40
41 // 起動時の処理
42 $(function()
43 {
44     //ログの取得
45     getLog();
46     // タイマー
47     Timer = setInterval('getLog()', getdata_off);
48 });
49
50 // ログ取得
51 function getLog(){
52     $.ajax({
53         type: "GET",
54         ifModified: true,
55         url: logfile,
56         success: function(data){
57     // 最新行番号の取得
58     if (!isNaN(document.getElementById('id'))) {
59         last_id = 0;
60     } else {
61         last_id = document.getElementById('id').firstChild.nodeValue;
62     }
63
64     // ログの整形
65         var recent = data.split('\n');
66
67         var text = new Array();
68         for(var i=0;i<recent.length; i++){
69         // 各行を<>で分割して配列に
70             var unit = recent[i].split('<>');
71
72         // データがなければ終了
73             // last_idで終点判断
74             if(unit[0] && last_id < unit[0]){
75                     text[i] = setData(unit);
76             } else {
77                 break;
78             }
79         }
80
81         var text_rev = text.reverse();
82
83         for(var i=0;i<text_rev.length; i++){
84         //出力
85             $("#online").after(text_rev[i]);
86         }
87     }
88     });
89     // 在室表示
90     showMember();
91 }
92
93 // 在室表示
94 function showMember() {
95     $.ajax({
96         type: "GET",
97         ifModified: true,
98         url: now_member,
99         success: function(data2){
100         // ログの整形
101             var member = data2.split('\n');
102             var text2 = "";
103             var unit2 = new Array();
104             for(var i=0;i<member.length; i++){
105                 var unit2 = member[i].split('<>');
106         // データがなければ終了
107                 if(!unit2[0]){
108                     break;
109                 }
110                 var text2 = text2+'<span id="'+unit2[0]+'" class="member" onclick="inputWhisper(\''+unit2[0]+'\',\''+unit2[1]+'\')">'+unit2[0]+'</span>'+'<>';
111                 // my_hashに自分のハッシュ登録
112                 if (!$(window.parent.document.getElementById("my_hash")).attr("value") && $(window.parent.document.getElementById("pwd_on")).attr("value") && $(window.parent.document.getElementById("name_on")).attr("value")==unit2[0]) {
113                     window.parent.document.getElementById("my_hash").value = unit2[1];
114                 }
115             }
116     // 出力
117             if (text2) {
118                 $("#indi").html(text2);
119             }
120         }
121     });
122 }
123
124 // ログイン・ログオフ
125 function login() {
126     clearInterval(Timer);
127     Timer = setInterval('getLog()', getdata_on);
128     $("#help").hide();
129     $("#online").show();
130     clearLog();
131 }
132 function logout() {
133     clearInterval(Timer);
134     Timer = setInterval('getLog()', getdata_off);
135     window.parent.document.getElementById("whisper_mark").src="<?php print $whisper_on; ?>"
136     window.location.reload();
137 }
138
139 // 遅延関数(http://zombiebook.seesaa.net/article/26143018.html)
140 function wait(fn, tm){
141     return function (){
142         var slf = this, arg = arguments;
143         setTimeout(function (){fn.apply(slf, arg);}, tm);
144     }
145 }
146
147 // リフレッシュ
148 function clearLog(){
149     $.ajax({
150         type: "GET",
151         ifModified: false,
152         url: logfile,
153         success: function(data){
154         var recent = data.split('\n');
155         var text = new Array();
156         $(".com_row").remove();
157         for(var i=0;i<recent.length; i++){
158             var unit = recent[i].split('<>');
159             if (unit[0]) {
160                 text[i] = setData(unit);
161             }
162         }
163         var text_rev = text.reverse();
164         for(var i=0;i<text_rev.length; i++){
165         //出力
166             if(text_rev[i]){
167                 $("#online").after(text_rev[i]);
168             }
169         }
170     }
171     });
172 }
173
174 // ささやき指定
175 function inputWhisper(name, whisperTo) {
176     var flag = whisperTo ? true : false;
177     if (flag==false){
178         alert(name+"さんは入室パスワード未入力のため、「ささやき」はできません。");
179     } else if (!window.parent.document.getElementById("pwd_on").value){
180         alert("パスワードを入力しないと「ささやき」はできません。");
181     } else if (name=='全員'){
182         alert("「全員」という名前の方に「ささやき」はできません(笑)");
183     } else {
184         window.parent.document.getElementById("whisper_to").value = name;
185         window.parent.document.getElementById("whisper_to_hash").value = whisperTo;
186         window.parent.document.getElementById("whisper_mark").src="<?php print $whisper_on; ?>"
187     }
188 }
189
190 // ログ行整形
191 function setData(unit) {
192     if (!unit) {
193         return false;
194     }
195     //行番号:最新行
196     var last_id_tag = '<span id="id" style="display:none;">'+unit[0]+'</span>';
197     //データの整形
198     var name = unit[1];
199     var pc = unit[2];
200     var color = unit[3];
201     var com = unit[4];
202     var d = new Date();
203     d.setTime(unit[5]*1000);
204     var year = d.getYear();
205     if (year < 2000) { year += 1900;}
206     var month = d.getMonth()+1;
207     if (month < 10) { month = "0" + month;}
208     var day = d.getDate();
209     if (day < 10) { day = "0" + day;}
210     var hour = d.getHours();
211     if (hour < 10) { hour = "0" + hour;}
212     var min = d.getMinutes();
213     if (min < 10) { min = "0" + min;}
214     var sec = d.getSeconds();
215     if (sec < 10) { sec = "0" + sec;}
216     var datetime = year+'/'+month+'/'+day+' '+hour+':'+min+':'+sec;
217     var whisper = 'n';
218     if(unit[9] || unit[10]) {
219         var whisper = 'y';
220         if ($(window.parent.document.getElementById("my_hash")).attr("value") == unit[8]) whisper = 'to';
221         if ($(window.parent.document.getElementById("my_hash")).attr("value") == unit[10]) var whisper = 'from'
222     }
223     if(unit[11] == '1' && unit[12]) {
224         var status = unit[12];
225     }
226
227     // 行の整形
228     if (unit[0]!='0' && whisper!='y') {
229         row =
230         '<div class="com_row" id="com_row_'+unit[0]+'">'+last_id_tag+'<span style="font-weight:bold; color:'+color+';" id="'+unit[0]+'" title="'+name+'">【'+pc+'】</span>:'+
231         '<span class="body" title="'+datetime+'">';
232         if (whisper=='to' || whisper=='from') {
233             row = row+'<span style="color:'+color_system+';">(';
234             if (whisper=='to') row = row+unit[9]+'さんへ';
235             if (whisper=='from') row = row+unit[2]+'さんから';
236             row = row+'のささやき) </span>';
237
238         }
239         row = row+com+'</span>';
240         if (status) {
241             row = row+' <span style="color:'+status_color+';">('+status+')</span>';
242         }
243         row = row+'</div>';
244     } else {
245         row = last_id_tag;
246     }
247     return row;
248 }
249 //-->
250 </script>
251 </head>
252 <body>
253 <div id="help" class="help" style="display:inline;">
254 <?php print nl2br($help);?>
255 <hr>
256 </div>
257 <div id="online" class="online" style="display:none;">参加者: <span id="indi"></span></div>
258
259 <div align="right" id="pastlog" class="pastlog">
260 <a href="./pastlog_index.php" target="pastlog">もっと前のログを見る</a></div>
261
262 <noscript>
263 <p><img src="image/blog_jscript.gif" alt="Javascript" width="80" height="15">本チャットは、JavaScript が有効な環境でのみ使用可能です。</p>
264 </noscript>
265
266 <?php
267 // フッター
268 print html_footer('');
269  ?>