Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | // |
||
3 | // The world's least efficient wall implementation (now a bit more efficient) |
||
4 | // |
||
5 | |||
6 | date_default_timezone_set('Europe/Copenhagen'); |
||
7 | $filename = "tmp/wall.html"; |
||
8 | |||
9 | function colorify_ip($ip) |
||
10 | { |
||
11 | $parts = explode(".", $ip); |
||
12 | $color = sprintf("%02s", dechex($parts[1])) . |
||
13 | sprintf("%02s", dechex($parts[2])) . |
||
14 | sprintf("%02s", dechex($parts[3])); |
||
15 | return $color; |
||
16 | } |
||
17 | |||
18 | function add_line($msg) { |
||
19 | global $filename; |
||
20 | $f = fopen($filename, "a"); |
||
21 | $date = date("Y-m-d h:i:s"); |
||
22 | $msg = strip_tags(stripslashes($msg)); |
||
23 | $remote = $_SERVER["REMOTE_ADDR"]; |
||
24 | // generate unique-ish color for IP |
||
25 | $color = colorify_ip($remote); |
||
26 | fwrite($f, '<span style="color:#'.$color.'">'.$date.'</span> '.htmlspecialchars($msg).'<br />'."\r\n"); |
||
27 | fclose($f); |
||
28 | return refresh(0); |
||
29 | } |
||
30 | |||
31 | function refresh($lastrefresh) { |
||
32 | global $filename; |
||
33 | if(filemtime($filename) > $lastrefresh) { |
||
34 | $lines = file($filename); |
||
35 | // return the last 25 lines |
||
36 | return array("wall" => join("\n", array_slice($lines, -25)), "update" => filemtime($filename)); |
||
37 | } else { |
||
38 | return false; |
||
39 | } |
||
40 | } |
||
41 | |||
42 | require("sajax.php"); |
||
43 | // $sajax_debug_mode = true; |
||
44 | $sajax_failure_redirect = "http://sajax.info/sajaxfail.html"; |
||
45 | sajax_export( |
||
46 | array("name" => "add_line", "method" => "POST"), |
||
47 | array("name" => "refresh", "method" => "GET") |
||
48 | ); |
||
49 | sajax_handle_client_request(); |
||
50 | ?> |
||
51 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
||
52 | <html xmlns="http://www.w3.org/1999/xhtml"> |
||
53 | <head> |
||
54 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
||
55 | <title>Sajax graffiti wall example</title> |
||
56 | <style type="text/css"> |
||
57 | .date { |
||
58 | color: blue; |
||
59 | } |
||
60 | </style> |
||
61 | <script type="text/javascript" src="json_stringify.js"></script> |
||
62 | <script type="text/javascript" src="json_parse.js"></script> |
||
63 | <script type="text/javascript" src="sajax.js"></script> |
||
64 | <script type="text/javascript"><!-- |
||
65 | <?php sajax_show_javascript(); ?> |
||
66 | |||
67 | var check_n = 1; |
||
68 | var nextrefresh; |
||
69 | function refresh_cb(data) { |
||
70 | if(data !== false) { |
||
71 | document.getElementById("wall").innerHTML = data["wall"]; |
||
72 | lastrefresh = data["update"]; |
||
73 | clearTimeout(nextrefresh); |
||
74 | nextrefresh = setTimeout("refresh();", 1000); |
||
75 | } else { |
||
76 | clearTimeout(nextrefresh); |
||
77 | nextrefresh = setTimeout("refresh();", 2500); |
||
78 | } |
||
79 | document.getElementById("status").innerHTML = "Checked #" + check_n++; |
||
80 | } |
||
81 | |||
82 | var lastrefresh = <?php echo(filemtime($filename)); ?>; |
||
83 | function refresh() { |
||
84 | document.getElementById("status").innerHTML = "Checking.."; |
||
85 | x_refresh(lastrefresh, refresh_cb); |
||
86 | } |
||
87 | |||
88 | function add() { |
||
89 | var line; |
||
90 | var handle; |
||
91 | handle = document.getElementById("handle").value; |
||
92 | line = document.getElementById("line").value; |
||
93 | if(line == "") |
||
94 | return; |
||
95 | x_add_line("[" + handle + "] " + line, refresh_cb); |
||
96 | document.getElementById("line").value = ""; |
||
97 | } |
||
98 | |||
99 | function keypress(keyCode) { |
||
100 | if (keyCode==13) { |
||
101 | add(); |
||
102 | document.getElementById("line").select(); |
||
103 | return false; |
||
104 | } |
||
105 | return true; |
||
106 | } |
||
107 | |||
108 | nextrefresh = setTimeout("refresh();", 1000); |
||
109 | //--> |
||
110 | </script> |
||
111 | </head> |
||
112 | <body> |
||
113 | <b><a href="http://www.sajax.info/">Sajax</a> v<?php echo($sajax_version); ?></b> - You are a guinea pig - This example illustrates the simplest possible graffiti wall. It isn't meant to be perfect, featureful, or even useful.<br /> |
||
114 | <form action="" method="post" onsubmit="add(); return false;"> |
||
115 | <input type="text" name="handle" id="handle" value="(name)" onfocus="this.select()" style="width:130px;" /> |
||
116 | <input type="text" name="line" id="line" value="(enter your message here)" onfocus="this.select();" onkeypress="keypress(event.keyCode);" style="width:300px;" /> |
||
117 | <input type="button" name="check" value="Post message" onclick="add(); return false;" /> |
||
118 | </form> |
||
119 | <div id="wall"> <?php $temp = refresh(0); echo($temp["wall"]); ?></div> |
||
120 | <div id="status">Checked #0</div> |
||
121 | </body> |
||
122 | </html> |