-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
202 lines (176 loc) · 6.49 KB
/
index.php
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
// import all libs
require_once 'lib/Nette/Tools.php';
foreach (Tools::glob(dirname(__FILE__) . '/lib/*.php') as $_) require_once $_;
// init
$table = dirname(__FILE__) . '/db/guestbook';
if (!is_dir(dirname($table)) && !mkdir(dirname($table), 0777, TRUE))
die('Cannot create database :-(');
if (!file_exists($table) && !(
(($_ = metatable::open($table)) && $_->close())))
die('Cannot create table :-(');
if (!(${'@table'} = metatable::open($table,
$_SERVER['REQUEST_METHOD'] === 'GET'
? metatable::READONLY
: (metatable::READWRITE | metatable::STRINGS_GC))))
die('Cannot open table :-(');
$_ = ${'@table'}->get('=', '*');
${'@all'} = array();
if (!empty($_)) ${'@all'} = array_keys(array_reverse(array_shift($_)));
${'@limit'} = 10;
${'@page'} = 1;
if (!empty($_SERVER['QUERY_STRING'])) ${'@page'} = intval($_SERVER['QUERY_STRING']);
// create form
${'@form'} = new Form;
${'@form'}->addText('name', 'Jméno:')
->addRule(Form::FILLED, 'Anonymy tu nechceme.');
${'@form'}->addtext('email', 'E-mail:')
->setEmptyValue('@')
->addCondition(Form::FILLED)
->addRule(Form::EMAIL, 'Podivný e-mail.');
${'@form'}->addText('www', 'WWW:')
->setEmptyValue('http://');
${'@form'}->addTextarea('text', 'Text:')
->addRule(Form::FILLED, 'Žádný text?');
${'@form'}['text']->getControlPrototype()->rows(2);
${'@form'}->addSubmit('ok', 'Přidat')
->onClick[] = '_add';
function _add()
{
$now = date('Y-m-d H:i:s');
foreach ($GLOBALS['@form']->getValues() as $k => $v)
$GLOBALS['@table']->set($now, $k, $v);
$GLOBALS['@table']->set('=', $now, TRUE);
$GLOBALS['@table']->close();
header('HTTP/1.1 303 See Other');
$request = new HttpRequest;
header('Location: ' . $request->getOriginalUri()->getAbsoluteUri());
exit();
}
${'@form'}->isSubmitted();
// at
${'@at'} = new at;
${'@at'}->fn('', fn('_echo', array(fn::ph())));
function _echo($block)
{
$filters = preg_split('~\s*\|\s*~', $block[0]);
$var = $GLOBALS['@' . array_shift($filters)];
foreach ($filters as $filter)
$var = call_user_func($GLOBALS['@filter:' . $filter], $var);
return $var;
}
${'@at'}->fn('each', fn('_each', array(fn::ph())));
function _each($block)
{
$ret = '';
foreach (array_slice($GLOBALS['@all'], ($GLOBALS['@page'] - 1) * $GLOBALS['@limit'],
$GLOBALS['@limit']) as $i)
{
$values = $GLOBALS['@table']->get($i, '*');
$values[$i]['date'] = strtotime($i);
foreach ($values[$i] as $k => $v) $GLOBALS['@' . $k] = $v;
$ret .= $GLOBALS['@at']->run($block);
}
return $ret;
}
${'@at'}->fn('pages', fn('_pages', array(fn::ph())));
function _pages($block)
{
$ret = '';
list($any, $current) = array_merge(array_filter($block, 'is_array'));
for ($i = 1, $stop = ceil(count($GLOBALS['@all']) / $GLOBALS['@limit']); $i <= $stop; ++$i) {
$GLOBALS['@i'] = $i;
if ($i === $GLOBALS['@page']) {
$ret .= $GLOBALS['@at']->run($current[1]);
} else {
$ret .= $GLOBALS['@at']->run($any[1]);
}
}
return $ret;
}
${'@at'}->fn('on', fn('_on', array(FALSE, fn::ph(), fn::ph())));
${'@at'}->fn('on!', fn('_on', array(TRUE, fn::ph(), fn::ph())));
function _on($not, $cond, $block)
{
if ($not === empty($GLOBALS['@' . trim($cond)])) return $GLOBALS['@at']->run($block);
}
// filters
${'@filter:texy'} = fn(array(new Texy, 'process'), array(fn::ph()));
${'@filter:escape'} = fn('htmlspecialchars', array(fn::ph(), ENT_QUOTES));
${'@filter:date'} = fn('date', array('j. n. Y, H:i:s', fn::ph()));
${'@filter:mailize'} = fn('_mailize', array(fn::ph(), FALSE));
${'@filter:mailto'} = fn('_mailize', array(fn::ph(), TRUE));
function _mailize($email, $mailto = FALSE)
{
$ret = '';
if ($mailto) $email = 'mailto:' . $email;
for ($i = 0, $len = strlen($email); $i < $len; ++$i) {
$ord = ord($email{$i});
if ($ord <= 0x7F && $ord !== 64 /* @ */ && $ord !== 46 /* . */ && $i % 15 !== 0) {
$ret .= chr($ord);
continue;
}
if (!$mailto && $i & 1) $ret .= '<!---->';
if ($ord <= 0x7F) $wchar = $ord;
else if ($ord <= 0xC2) $wchar = FALSE;
else if ($ord <= 0xDF) $wchar = ($ord & 0x1F) << 6 |
(ord($email{++$i}) & 0x3F);
else if ($ord <= 0xEF) $wchar = ($ord & 0x0F) << 12 |
(ord($email{++$i}) & 0x3F) << 6 |
(ord($email{++$i}) & 0x3F);
else if ($ord <= 0xF4) $wchar = ($wchar & 0x0F) << 18 |
(ord($email{++$i}) & 0x3F) << 12 |
(ord($email{++$i}) & 0x3F) << 6 |
(ord($email{++$i}) & 0x3F);
else $wchar = FALSE;
$ret .= '&#' . intval($wchar) . ';';
}
return $ret;
}
// run!
echo ${'@at'}->run(substr(file_get_contents(__FILE__), __COMPILER_HALT_OFFSET__));
__halt_compiler() ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Kniha návštěv</title>
<style type="text/css">
* { color: #888; font-family: sans-serif; }
html { text-align: center; font-size: small; }
body { width: 600px; margin: 50px auto; text-align: left; }
h1 { text-align: center; font-family: "Trebuchet MS", serif; }
.right { text-align: right; }
.center { text-align: center; }
hr { border: none; border-bottom: dotted #888 1px; }
form table { width: 96%; }
form td, form th { padding: 0 5px; }
input[type=text], textarea { font-size: inherit; font-family: inherit; width: 100%; }
a { color: #666; text-decoration: underline; }
a:hover { text-decoration: none; background: #eee; }
.warn, .err { color: #222; font-style: italic; }
.err { font-weight: bold; }
</style>
</head>
<body>
<h1>Kniha návštěv</h1>
@{form}
@each {
<hr>
@{text | texy}
<p class="right">
@{date | date | escape} —
@on www {<a href="@{www | escape}">@{name | escape}</a>}
@on! www {@{name | escape}}
@on email {<<a href="@{email | mailto}">@{email | mailize}</a>>}
</p>
}
<hr>
<p class="center">
@pages {
@{<a href="?@{i}">@{i}</a> }
@{<strong>@{i}</strong> }
}
</p>
</body>
</html>