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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
<?php
/**
* Sticky Notes pastebin
* @ver 0.3
* @license BSD License - www.opensource.org/licenses/bsd-license.php
*
* Copyright (c) 2012 Sayak Banerjee <sayakb@kde.org>
* Copyright (c) 2013 Athmane Madjoudj <athmane@fedoraproject.org>
* All rights reserved. Do not remove this copyright notice.
*/
// Invoke required files
include_once('init.php');
// Collect some data
$paste_id = $core->variable('id', 0);
$hash = $core->variable('hash', 0);
$mode = $core->variable('mode', '');
$project = $core->variable('project', '');
$password = $core->variable('password', '');
$sid = $core->variable('session_id_' . $paste_id, '', true);
$mode = strtolower($mode);
// Password exempt
$exempt = false;
// Trim trailing /
if (strrpos($password, '/') == strlen($password) - 1)
{
$password = substr($password, 0, strlen($password) - 1);
}
if (empty($mode))
{
$mode = $core->variable('format', '');
$_GET['mode'] = $mode;
}
// Check for mode validity
if ($mode && $mode != 'raw' && $mode != 'xml' && $mode != 'json')
{
die;
}
// Initialize the skin file
if ($mode != 'raw')
{
$skin->init('tpl_show');
}
// We want paste id
if ($paste_id == 0)
{
$core->redirect($core->path() . 'all/');
}
// Escape the paste id
$db->escape($paste_id);
// Get the paste data
$sql = "SELECT * FROM {$db->prefix}main WHERE id = {$paste_id} LIMIT 1";
$row = $db->query($sql, true);
// Check if something was returned
if ($row == null)
{
if ($mode == 'xml' || $mode == 'json')
{
$skin->assign('error_message', 'err_not_found');
echo $skin->output("api_error.{$mode}");
die;
}
else if ($mode == 'raw')
{
die($lang->get('error_404'));
}
else
{
$skin->assign(array(
'error_text' => $lang->get('error_404'),
'data_visibility' => 'hidden',
));
$skin->kill();
}
}
// Is it a private paste?
if ($row['private'] == "1")
{
if (empty($hash) || $row['hash'] != $hash)
{
if ($mode == 'xml' || $mode == 'json')
{
$skin->assign('error_message', 'err_invalid_hash');
echo $skin->output("api_error.{$mode}");
die;
}
else if ($mode == 'raw')
{
die($lang->get('error_hash'));
}
else
{
$skin->assign(array(
'error_text' => $lang->get('error_hash'),
'data_visibility' => 'hidden',
));
$skin->kill();
}
}
}
// Check if password cookie is there
if (!empty($row['password']) && !empty($sid))
{
// Escape the session id
$db->escape($sid);
// Clean up the session data every 30 seconds
if (time() % 30 == 0)
{
$age = time() - 1200;
$db->query("DELETE FROM {$db->prefix}session " .
"WHERE timestamp < {$age}");
}
$pass_data = $db->query("SELECT sid FROM {$db->prefix}session " .
"WHERE sid = '{$sid}'", true);
if (!empty($pass_data['sid']))
{
$exempt = true;
}
}
// Is it password protected?
if (!empty($row['password']) && empty($password) && !$exempt)
{
if ($mode == 'xml' || $mode == 'json')
{
$skin->assign('error_message', 'err_password_required');
echo $skin->output("api_error.{$mode}");
die;
}
else if ($mode == 'raw')
{
die($lang->get('err_passreqd'));
}
else
{
$skin->init('tpl_show_password');
$skin->title("#{$row['id']} • " . $lang->get('site_title'));
$skin->output();
exit;
}
}
// Check password
if (!empty($row['password']) && !empty($password) && !$exempt)
{
$check = sha1(sha1($password) . $row['salt']);
if ($check != $row['password'])
{
if ($mode == 'xml' || $mode == 'json')
{
$skin->assign('error_message', 'err_invalid_password');
echo $skin->output("api_error.{$mode}");
die;
}
else if ($mode == 'raw')
{
die($lang->get('invalid_password'));
}
else
{
$skin->assign(array(
'error_text' => $lang->get('invalid_password'),
'data_visibility' => 'hidden',
));
$skin->kill();
}
}
else
{
// Create a session
$sid = sha1(time() . $core->remote_ip());
$core->set_cookie('session_id_' . $paste_id, $sid);
$db->query("INSERT INTO {$db->prefix}session " .
"(sid, timestamp) VALUES ('{$sid}', " . time() . ")");
}
}
// Is it raw? just dump the code then
if ($mode == 'raw')
{
header('Content-type: text/plain; charset=UTF-8');
header('Content-Disposition: inline; filename="pastedata"');
echo $row['data'];
exit;
}
// Prepare GeSHi
$geshi = new GeSHi($row['data'], $row['language']);
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 2);
$geshi->set_header_type(GESHI_HEADER_DIV);
//$geshi->set_line_style('background: #f7f7f7; text-shadow: 0px 1px #fff; padding: 1px;',
// 'background: #fbfbfb; text-shadow: 0px 1px #fff; padding: 1px;');
$geshi->set_overall_style('word-wrap:break-word;');
// Generate the data
$user = empty($row['author']) ? $lang->get('anonymous') : htmlspecialchars($row['author']);
$time = date('d M Y, h:i:s e', $row['timestamp']);
$info = $lang->get('posted_info');
$info = preg_replace('/\_\_user\_\_/', $user, $info);
$info = preg_replace('/\_\_time\_\_/', $time, $info);
// Before we display, we need to escape the data from the skin/lang parsers
$code_data = (empty($mode) ? $geshi->parse_code() : htmlspecialchars($row['data']));
$lang->escape($code_data);
$skin->escape($code_data);
// Fix JSON rendering. The JSON spec disallows newlines in string literals
// https://www.ietf.org/rfc/rfc4627.txt :
// "All Unicode characters may be placed within the
// quotation marks except for the characters that must be escaped:
// quotation mark, reverse solidus, and the control characters (U+0000
// through U+001F)."
// ... and since \n is a control character, sticky-notes putting newlines in
// string literals (unescaped) breaks spec.
if ($mode && $mode == 'json') {
$code_data = str_replace("\n", "\\n", $code_data);
}
// Shorten the current URL
$url_shortener = new URLShortener();
$short_url = $url_shortener->shorten($nav->get_paste($row['id'], $hash, $project, true, ''));
// Assign template variables
$skin->assign(array(
'paste_id' => $row['id'],
'paste_data' => $code_data,
'paste_lang' => htmlspecialchars($row['language']),
'paste_info' => $info,
'paste_user' => $user,
'paste_timestamp' => $row['timestamp'],
'raw_url' => $nav->get_paste($row['id'], $hash, $project, false, 'raw'),
'share_url' => urlencode($core->base_uri()),
'share_title' => urlencode($lang->get('paste') . ' #' . $row['id']),
'error_visibility' => 'hidden',
'geshi_stylesheet' => $geshi->get_stylesheet(),
'short_url' => $short_url,
));
// Let's output the page now
$skin->title("#{$row['id']} • " . $lang->get('site_title'));
if ($mode == 'raw')
{
$skin->output(false, true);
}
else if ($mode)
{
echo $skin->output("api_show.{$mode}");
}
else
{
$skin->output();
}
?>
|