-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_chat.php
More file actions
159 lines (132 loc) · 3.74 KB
/
basic_chat.php
File metadata and controls
159 lines (132 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Basic Chat Script
*
* 極めて簡素なチャットスクリプト
* 投稿して表示する機能しかありません
*
*/
/* 定数定義 */
define('LOG_FILE', 'log.dat');
define('MAX_NAME', 40);
define('MAX_TEXT', 200);
define('DEFAULT_NUM', 20);
/* メインルーチンここから */
if($_GET['mode'] === 'write')
{
write();
/* 書き込んだらView専用ページにリダイレクトしておく(リロードによる2重投稿防止) */
header('Location: '.$_SERVER['PHP_SELF']);
exit();
}
$log = array();
read();
/* メインルーチンここまで */
/**
* This is function read
*
* ファイルからログを読み出して $log に投入する
*
*/
function read()
{
global $log;
if(file_exists(LOG_FILE))
{
$file = file(LOG_FILE);
if(!$file)
{
$file[0][1] = 'ファイル読み込みエラー';
}
/* GETで渡されるnの値で表示件数を調整 */
if(!isset($_GET['n']))
$lines = DEFAULT_NUM;
elseif((int)$_GET['n'] === 0)
$lines = count($file);
else
$lines = (int)$_GET['n'];
/* ログの末尾から$lines件取り出す */
for($i = count($file) - 1; $i > count($file) - $lines - 1; $i--)
{
if($i < 0) continue;
$log[] = explode("\t", $file[$i]);
}
}
}
/**
* This is function write
*
* POSTデータを処理してログに書き込む
*
*/
function write()
{
/* POSTデータ処理 */
foreach($_POST as $key => &$value)
{
/* 改行文字除去(スペースに置換) */
$value = preg_replace('/(?:\x0D\x0A|[\x0A\x0A])+/u', ' ', $value);
/* コントロールコード除去 */
$value = preg_replace('/[\x00-\x1f\x7f]/u', '', $value);
if($key === 'name')
{
if(strlen($value) > MAX_NAME)
$value = substr($value, 0, MAX_NAME);
continue;
}
if($key === 'text')
{
if(strlen($value) > MAX_TEXT)
$value = substr($value, 0, MAX_TEXT);
continue;
}
}
if(!($fp = fopen(LOG_FILE, 'a+')))
exit('ファイル書き込みエラー');
flock($fp, LOCK_EX);
fseek($fp, 0, SEEK_SET);
$next = 0;
while(!feof($fp))
{
fgets($fp);
$next++;
}
/* ファイルに書き出し */
/* フォーマットは5カラム ID Name Text Host Time */
fwrite(
$fp,
implode(
"\t",
array($next, $_POST['name'], $_POST['text'], gethostbyaddr($_SERVER['REMOTE_ADDR']), time())
).PHP_EOL
);
flock($fp, LOCK_UN);
fclose($fp);
header('Location: '.$_SERVER['PHP_SELF']);
exit();
}
/* 以下HTML出力 */
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Basic Chat</title>
</head>
<body>
<h1>Simple Chat</h1>
<div>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>?mode=write">
<p><span style="width:60px;display:inline-block;">Name</span>: <input type="text" name="name" value="" size="<?php echo MAX_NAME; ?>"></p>
<p><span style="width:60px;display:inline-block;">Message</span>: <input type="text" name="text" value="" size="<?php echo MAX_TEXT; ?>"></p>
<p><input type="submit" name="submit" value="Submit"> <?php echo (isset($_GET['n']) && (int)$_GET['n'] === 0) ? '<a href='.$_SERVER['PHP_SELF'].'?n=20>Show Last 20</a>' : '<a href='.$_SERVER['PHP_SELF'].'?n=0>Show All</a>'; ?></p>
</form>
</div>
<hr>
<div>
<?php
/* 時刻は分かりやすいようフォーマットして出力 */
foreach($log as $line)
echo('<p><span style="width: 30px;display:inline-block;">'.$line[0].'</span>: <strong style="width: 100px;display:inline-block;">'.htmlspecialchars($line[1]).'</strong> <span>'.htmlspecialchars($line[2]).'</span> <span style="color:#CCC;">'.date('Y-m-d H:i:s', $line[4]).'</span></p>'.PHP_EOL);
?> </div>
</body>
</html>