-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
255 lines (201 loc) · 4.89 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
6core.net paster
Christiaan Ottow ([email protected]) 2011
A tiny pastebin clone. I created this because I don't perse like all my pastes ending up
on a public server somewhere. This paster can be very quickly deployed on your own system
allowing you to keep control over your pastes. It is coded with security in mind, forces
the use of HTTPS, and imposes rate limits on posters.
*/
ob_start();
define('CONFIG', '../config.php');
define('TPLDIR','../tpl/');
$base = dirname($_SERVER['SCRIPT_NAME']);
if( $base != '/' )
{
$base .= '/';
}
define('BASEURL', $base );
require(CONFIG);
function do_cleanup()
{
global $dbh;
$stmt = $dbh->prepare("DELETE FROM `posts` WHERE `expires` < NOW()");
$stmt->execute();
}
function check_setup()
{
global $config;
// check register_globals
if( ini_get('register_globals') )
{
die('register_globals is enabled. I can\'t work like this.');
}
// check gpc_quotes
if( get_magic_quotes_gpc() )
{
die('magci_quotes_gpc is enabled. I can\'t work like this.');
}
// check SSL
if( !array_key_exists('HTTPS', $_SERVER) || $_SERVER['HTTPS'] != "on")
{
die('I really like encryption. Please use SSL.');
}
// sane config?
if( $config['limit_hour'] > $config['limit_day'] )
{
die('You should allow less posts per hour than per day, silly');
}
// htaccess installed?
if( !file_exists('.htaccess'))
{
die('You should install the included .htaccess file in the same dir as index.php');
}
return true;
}
function show_post( $ident )
{
global $dbh;
$stmt = $dbh->prepare("SELECT `text`,`mimetype` FROM `posts` WHERE `ident` = ?");
if( !$stmt )
{
die( 'mysql error' );
}
$stmt->bind_param('s', $ident );
$stmt->execute();
$stmt->store_result();
$stmt->bind_result( $content, $mimetype );
if( $stmt->num_rows == 1 )
{
$stmt->fetch();
// header("Content-Type: text/plain; charset=utf-8");
header("Content-Type: $mimetype");
require(TPLDIR.'post.php');
return;
}
// not found
header("HTTP/1.0 404 Not Found", true, 404);
require(TPLDIR.'404.php');
}
function show_form()
{
global $config;
require(TPLDIR.'header.php');
require(TPLDIR.'form.php');
require( TPLDIR.'footer.php');
}
function do_post()
{
global $dbh;
global $config;
require(TPLDIR.'header.php');
if( empty( $_POST['content'] ) || empty( $_POST['ttl'] ))
{
return;
}
if( strlen( $_POST['content']) > $config['post_max_chars'] )
{
$errmsg = "Your post exceeds the max limit of ".$config['post_max_chars'];
require( TPLDIR.'error.php');
return;
}
$ttl = intval( $_POST['ttl'] );
if( $ttl < $config['ttl_min'] )
{
$ttl = $config['ttl_min'];
} else if( $ttl > $config['ttl_max'] ) {
$ttl = $config['ttl_max'];
}
if( limit_exceeded() )
{
$errmsg = "You have reached your throttle limit, try again later.";
require( TPLDIR.'error.php');
return;
}
if( empty( $_POST['mimetype']) || substr($_POST['mimetype'], 0, 6) != 'image/')
{
$mimetype='text/plain; charset=utf-8';
}
else
{
$mimetype=$_POST['mimetype'];
}
// it's OK now, let's post it
$ident = generate_ident();
$stmt = $dbh->prepare("INSERT INTO `posts` SET `ident`= ?, `ip`=?, `date`=NOW(), `text`=?, `expires` = TIMESTAMPADD( SECOND, ?, NOW()), `mimetype`=?");
$stmt->bind_param('sssis', $ident, $_SERVER['REMOTE_ADDR'], $_POST['content'], $ttl, $mimetype );
$stmt->execute();
header("Location: ".BASEURL."p/".$ident);
require( TPLDIR.'footer.php');
}
function generate_ident()
{
global $dbh;
$exists = true;
while( $exists )
{
$set = 'abcdefghijklmnopqrstuvwxyz0123456789';
$ident = '';
for( $i=0; $i<16; $i++)
{
$ident .= $set[rand(0, strlen($set))];
}
$stmt = $dbh->prepare("SELECT EXISTS ( SELECT * FROM `posts` WHERE `ident` = ? )");
$stmt->bind_param('s', $ident );
$stmt->execute();
$stmt->bind_result( $_exists );
$exists = ( $_exists == 1 ? true : false );
}
return $ident;
}
function limit_exceeded()
{
global $dbh;
global $config;
// check day limit
return(
_limit_exceeded( 'DAY', $config['limit_day']) ||
_limit_exceeded('HOUR', $config['limit_hour'] )
);
}
function _limit_exceeded( $type, $limit )
{
global $dbh;
if( !in_array( $type, array('DAY', 'HOUR')))
return true;
$stmt = $dbh->prepare("SELECT COUNT(*) FROM `posts` WHERE `ip`= ? AND TIMESTAMPDIFF( $type, NOW(), `date` ) <= 1");
if( !$stmt )
{
die("Couldn't perform throttle check");
}
$stmt->bind_param("s", $_SERVER['REMOTE_ADDR'] );
$stmt->execute();
$stmt->bind_result( $count );
$stmt->fetch();
return( $count > $limit );
}
check_setup();
$dbh = mysqli_connect(
$config['mysql_host'],
$config['mysql_user'],
$config['mysql_pass'],
$config['mysql_db']
);
if( !$dbh )
{
die("Couldn't connect to database");
}
$ident = false;
do_cleanup();
if( array_key_exists( 'p', $_GET ) && ctype_alnum( $_GET['p'] ) )
{
$ident = $_GET['p'];
}
if( $ident )
{
show_post( $ident );
} else if( array_key_exists( 'content', $_POST ) ) {
do_post();
} else {
show_form();
}