Initial commit of PasteBin rewrite
Jeremy D

Jeremy D commited on 2012-01-23 18:30:34
Showing 11 changed files, with 958 additions and 0 deletions.

... ...
@@ -0,0 +1 @@
1
+geshi
... ...
@@ -0,0 +1,494 @@
1
+<?php
2
+// Show me the sauce.
3
+if (isset($_GET['sauce2']))
4
+{
5
+	echo '<!DOCTYPE html><html><head><title>PasteBin Source code</title></head><body>';
6
+
7
+	if (isset($_GET['f']) && $_GET['f'] == 'settings')
8
+		highlight_file(dirname(__FILE__) . '/settings.php');
9
+	elseif (isset($_GET['f']) && $_GET['f'] == 'language')
10
+		highlight_file(dirname(__FILE__) . '/languages/english.php');
11
+	else
12
+		highlight_file(__FILE__);
13
+
14
+	exit('</body></html>');
15
+}
16
+
17
+$_SERVER['REQUEST_URI'] = '/pastebin';
18
+require_once('../wp-ssi.php');
19
+$specialPage['noClear'] = true;
20
+$specialPage['class'] = 'post';
21
+$specialPage['contentClass'] = '';
22
+$specialPage['headerTitle'] = 'PasteBin';
23
+$specialPage['pageTitlePrefix'] = 'PasteBin';
24
+
25
+// Start the PasteBin up.
26
+$pasteBin = new pB();
27
+
28
+// Modified for WordPress, but this handles the sidebar and css.
29
+if (defined('WPLANG'))
30
+{
31
+	wp_enqueue_style('pastebin', pBS::get('css'));
32
+	ob_start();
33
+	$pasteBin->showRecent();
34
+	$specialPage['sidebar'] = ob_get_contents();
35
+	ob_end_clean();
36
+}
37
+
38
+// Handles the actions.
39
+if (isset($_POST['submit']))
40
+	$pasteBin->action_paste();
41
+if (isset($_GET['view']))
42
+	$pasteBin->action_view($_GET['view']);
43
+else
44
+	$pasteBin->action_index();
45
+
46
+if (defined('WPLANG'))
47
+	$specialPage['title'] = $pasteBin->title;
48
+
49
+/*
50
+* Main PasteBin class
51
+*/
52
+class pB
53
+{
54
+	/*
55
+	* Some basic stuff
56
+	*/
57
+	public $title = 'PasteBin';
58
+	private $action = 'index';
59
+	private $geshi_languages = array();
60
+	private $db = null;
61
+	private $usr = null;
62
+
63
+	/*
64
+	* Setup the settings when creating the object.
65
+	*/
66
+	public function __construct()
67
+	{
68
+		// Load the primary file.
69
+		require_once(dirname(__FILE__) . '/settings.php');
70
+
71
+		// Load up any settings that apply only to this pastebin.
72
+		if (file_exists(dirname(__FILE__) . '/settings-' . pathinfo(basename($_SERVER['SCRIPT_FILENAME']), PATHINFO_FILENAME) . '.php'))
73
+			require_once(dirname(__FILE__) . '/settings-' . pathinfo(basename($_SERVER['SCRIPT_FILENAME']), PATHINFO_FILENAME). '.php');
74
+
75
+		// Start up our database.
76
+		require_once(pBS::get('sources') . '/db.php');
77
+		if (file_exists(pBS::get('sources') . '/db-' . pBS::get('db') . '.php'))
78
+		{
79
+			require_once(pBS::get('sources') . '/db-' . pBS::get('db') . '.php');
80
+
81
+			$class = 'pDB_' . pBS::get('db');
82
+			if (class_exists($class))
83
+				$this->db = new $class;
84
+		}
85
+
86
+		// Start up our User handler.
87
+		require_once(pBS::get('sources') . '/user.php');
88
+		if (file_exists(pBS::get('sources') . '/user-' . pBS::get('user') . '.php'))
89
+		{
90
+			require_once(pBS::get('sources') . '/user-' . pBS::get('user') . '.php');
91
+
92
+			$class = 'pUser_' . pBS::get('user');
93
+			if (class_exists($class))
94
+				$this->usr = new $class;
95
+		}
96
+
97
+		// Start getting things going.
98
+		$this->loadLanguage();
99
+		$this->loadGeshi();
100
+
101
+		// At this point we are ready to go.
102
+		if (pBS::get('use_smf_theme'))
103
+			echo '
104
+			<div id="paste_recent" class="alignright">', $this->showRecent(), '</div>';
105
+	}
106
+
107
+	/*
108
+	* Load up the language, taking into account a session or selection.
109
+	*/
110
+	public function loadLanguage()
111
+	{
112
+		if (isset($_SESSION['user_language']))
113
+			$language = $_SESSION['user_language'];
114
+		// allow_url_include shouldn't be enabled!
115
+		elseif (isset($_GET['lang']) && file_exists(pBS::get('languages') . '/' . strtolower(htmlspecialchars($_GET['lang'])) . '.php'))
116
+		{
117
+			if (strpos($_GET['lang'], 'http://') !== false || strpos($_GET['lang'], 'ftp://') !== false)
118
+				exit('Hacking attempt');
119
+
120
+			$language = strtolower(htmlspecialchars($_GET['lang']));
121
+		}
122
+		elseif ($this->usr->id() > 0 && file_exists(pBS::get('languages') . '/' . $this->usr->language() . '.php'))
123
+			$language = $this->usr->language();
124
+		else
125
+			$language = pBS::get('default_language');
126
+
127
+		// Load the language.
128
+		require(pBS::get('languages') . '/' . $language . '.php');
129
+	}
130
+
131
+	/*
132
+	* Setup GeSHI
133
+	*/
134
+	public function loadGeshi()
135
+	{
136
+		if (!pBS::get('use_geshi'))
137
+			return false;
138
+
139
+		if (!($dir = @opendir(pBS::get('geshi_location') . '/geshi')))
140
+			return false;
141
+
142
+		$languages = array();
143
+		while ($file = readdir($dir))
144
+		{
145
+			if (substr ($file, 0, 1) == '.' || !stristr($file, '.') || $file == 'css-gen.cfg' )
146
+				continue;
147
+			$languages[] = substr($file, 0,  strpos($file, '.'));
148
+		}
149
+		closedir($dir);
150
+		sort($languages);
151
+
152
+		$this->geshi_languages = $languages;
153
+	}
154
+
155
+	/*
156
+	* Format a URL
157
+	* @param $act string The action we want to use
158
+	* @param $sa string The value of the action
159
+	* @param $extras array An array of key => value containing extras to add to the url.
160
+	* @return string Return the url.
161
+	*/
162
+	public function URL($act, $sa = '', $extras = array())
163
+	{
164
+		static $url_prefix;
165
+
166
+		// Build the first part of the url.
167
+		if (empty($url_prefix))
168
+		{
169
+			$url_prefix = 'http' . (isset($_SERVER['HTTPS']) ? 's': '')  . '://' . $_SERVER['HTTP_HOST'];
170
+
171
+			if ($_SERVER['SERVER_PORT'] != 80)
172
+				$url_perfix .= ':' . $_SERVER['SERVER_PORT'];
173
+
174
+			if (pBS::get('url_sef'))
175
+				$url_prefix .= pBS::get('sef_base');
176
+			elseif (pBS::get('use_portal'))
177
+				$url_prefix .= pBS::get('portal_url');
178
+			else
179
+				$url_prefix .= str_replace($_SERVER['DOCUMENT_ROOT'], '', __FILE__) . '?';
180
+		}
181
+
182
+		if ($act == 'index' && $url_prefix{strlen($url_prefix) -1} == '?')
183
+			return substr($url_prefix, 0, -1);
184
+		elseif ($act == 'index')
185
+			return $url_prefix;
186
+		elseif ($act == 'post')
187
+			return $url_prefix . '/?';
188
+
189
+		$url = $url_prefix;
190
+
191
+		if ($sa == '')
192
+			$url .= '?' . $act;
193
+		elseif (pBS::get('url_sef'))
194
+			$url .= '/' . $sa;
195
+		else
196
+			$url .= $act . '=' . $sa;
197
+
198
+		if (!empty($extras))
199
+		{
200
+			foreach ($extras AS $k => $v)
201
+			{
202
+				if (pBS::get('url_sef') && !empty($v))
203
+					$url .= '/' . $v;
204
+				elseif (!empty($v))
205
+					$url .= ';' . $k . '=' . $v;
206
+			}
207
+		}
208
+
209
+		// This may happen with a portal..
210
+		if (pBS::get('use_portal'))
211
+			$url = str_replace('?;', '?', $url);
212
+
213
+		return $url;
214
+	}
215
+
216
+	/*
217
+	* Show a new past form
218
+	*/
219
+	public function action_index()
220
+	{
221
+		$this->title = pBL('index_title');
222
+
223
+		// Trying to save this paste?
224
+		if (isset($_POST['save']))
225
+			$errors = $this->makePaste(0);
226
+
227
+		// Show any errors
228
+		if (!empty($errors))
229
+			echo '<div class="error_message">', implode('<br />', $errors), '</div>';
230
+
231
+		$this->postForm((!empty($_POST['code']) ? $_POST['code'] : ''));
232
+	}
233
+
234
+	/*
235
+	* Show a paste
236
+	* @param $id int The id of the paste
237
+	*/
238
+	public function action_view($id)
239
+	{
240
+		$this->title = pBL('view_title', $id);
241
+
242
+		$paste = $this->showPaste($id);
243
+
244
+		if (!empty($paste['key']) && $this->usr->is_admin())
245
+			echo '
246
+			<div class="information"><b>Key:</b> ', $paste['key'], '</div>';
247
+
248
+		if (!empty($paste['parsed']))
249
+			echo '
250
+			<div id="formated">
251
+				<h2>', pBL('formated_paste'), '</h2>
252
+				<div id="formated_paste">', $paste['parsed'], '</div>
253
+			</div>';
254
+
255
+		$this->postForm($paste['body'], $id, $paste['use_geshi'], $paste['language']);
256
+	}
257
+
258
+	/*
259
+	* Actually make the paste.
260
+	*/
261
+	public function action_paste()
262
+	{
263
+		$do_create = true;
264
+
265
+		if ($this->usr->id() > 0 && (empty($_POST['name']) || empty($_POST['email'])))
266
+			$do_create = false;
267
+
268
+		if (empty($_POST['code']))
269
+			$do_create = false;
270
+
271
+		// Get the data ready.
272
+		$data = array(
273
+			'paste_id' => !empty($_POST['view']) ? $_POST['view'] : 0,
274
+			'new_key' => isset($_POST['force_new_pw']) && $this->usr->is_admin(),
275
+			'name' => !empty($_POST['name']) ? $_POST['name'] : 'Guest',
276
+			'email' => !empty($_POST['email']) ? $_POST['email'] : 'guest@noemail.com',
277
+			'use_geshi' => !empty($_POST['use_geshi']),
278
+			'language' => !empty($_POST['type']) ? $_POST['type'] : 'php',
279
+			'body' => $_POST['code']
280
+		);
281
+
282
+		// Do a test.
283
+		$this->db->addPasteTest(&$data);
284
+
285
+		if (!$do_create)
286
+		{
287
+			$this->errors[] = 'Missing information (Username/email)';
288
+
289
+			if (!empty($_POST['view']))
290
+				$this->action_view($_POST['view']);
291
+			else
292
+				$this->action_index();
293
+
294
+			return false;
295
+		}
296
+
297
+		// Valid Numbers only..
298
+		$result = $this->db->addPaste($data);
299
+
300
+		// Send us there.
301
+		redirectexit($this->URL('view', $result['id'], array(
302
+			'update' => isset($data['updated']) ? 't' . time() : '',
303
+			'key' => !empty($data['key']) ? $data['key'] : '',
304
+		)));
305
+	}
306
+
307
+	/*
308
+	* Show some recent pastes
309
+	*/
310
+	public function showRecent()
311
+	{
312
+		$recent_limit = $this->usr->is_admin() ? pBS::get('recent_limit_admin') : pBS::get('recent_limit');
313
+
314
+		$recent = array();
315
+		if ($recent_limit > 0)
316
+			$recent = $this->db->fetchRecent($recent_limit);
317
+
318
+		// Output this.
319
+		echo '
320
+			<ul>		
321
+				<li class="widget">
322
+					<h2 class="widgettitle" title="I am not a Easter Egg">', pBL('recent'), '</h2>
323
+					<ul>
324
+						<li><a href="', $this->URL('index'), '">', pBL('create_new'), '</a></li>';
325
+
326
+			foreach ($recent as $rec)
327
+				echo '
328
+						<li><a href="', $this->URL('view', $rec), '">#', $rec, '</a></li>';
329
+
330
+		echo '
331
+					</ul>
332
+				</li>
333
+			</ul>';
334
+
335
+		// Added this for myself.
336
+		if (defined('WPLANG2'))
337
+			echo '
338
+			<br />
339
+			<ul>
340
+				<li class="widget">
341
+					<h2 class="widgettitle">See the Source</h2>
342
+					<ul>
343
+						<li><a href="./?sauce">Main Script</li>
344
+						<li><a href="./?sauce&f=settings">Settings</li>
345
+						<li><a href="./?sauce&f=language">Language</li>
346
+					</ul>
347
+				</li>
348
+			</ul>';
349
+	}
350
+
351
+	/*
352
+	* Shows a form for making/editing a paste
353
+	* @param $code string A string containing the actual code for the code box
354
+	* @param $id (optiona) int The id of the paste
355
+	* @param $use_geshi (optiona) bool Whether to use geshi or not.
356
+	* @param $geshi_language (optional) string The default language to use, ie php
357
+	*/
358
+	public function postForm($code, $id = 0, $use_geshi = true, $geshi_language = 'php')
359
+	{
360
+		echo '
361
+			<form method="post" action="', $this->URL('post'), '">
362
+				<div id="name_container">
363
+					<span id="name_text" class="container_text">', pBL('user_name'), ':</span>
364
+					<span id="name_value" class="container_value">';
365
+
366
+		if ($this->usr->is_guest())
367
+			echo '
368
+						<input type="text" name="name" value="', !empty($_POST['name']) ? htmlspecialchars($_POST['name']) : 'Guest', '" />';
369
+		else
370
+			echo '
371
+						', $this->usr->name(), '<input type="hidden" name="name" value="', $this->usr->name(), '" /></span>';
372
+
373
+		echo '
374
+					</span>
375
+				</div>
376
+
377
+				<div id="email_container">
378
+					<span id="email_text" class="container_text">', pBL('email'), ':</span>
379
+					<span id="email_value class="container_value">';
380
+
381
+		if ($this->usr->is_guest())
382
+			echo '
383
+						<input type="text" name="email" value="', !empty($_POST['email']) ? htmlspecialchars($_POST['email']) : 'your+name@domain.com', '" />';
384
+		else
385
+			echo '
386
+						', $this->usr->email(), '<input type="hidden" name="email" value="', $this->usr->email(), '" />';
387
+
388
+		echo '
389
+					</span>
390
+				</div>
391
+
392
+				<div id="code_container">
393
+					<div id="code_text" class="container"text">', pBL('code'), ':</div>
394
+					<div id="code_value" class="container_value">
395
+						<textarea name="code" style="width: 100%;" rows="30">', $code, '</textarea>
396
+					</div>
397
+				</div>
398
+
399
+				<ul id="settings_container">';
400
+
401
+		if (pBS::get('use_geshi'))
402
+		{
403
+			echo '
404
+					<li><input type="checkbox" name="use_geshi"', $use_geshi ? ' checked="checked"' : '', '/><span id="setting_geshi" class="setting_text">', pBL('enable_geshi'), '</span></li>';
405
+
406
+			if (!empty($this->geshi_languages))
407
+			{
408
+				echo '
409
+					<li><span id="setting_geshi_lang">Code Language:</span><select name="type">';
410
+
411
+				foreach ($this->geshi_languages AS $lang)
412
+					echo '
413
+						<option value="', $lang, '"', ($geshi_language == $lang ? 'selected="selected"' : ''), '>', $lang, '</option>';
414
+
415
+				echo '
416
+					</select></li>';
417
+			}
418
+		}
419
+
420
+		if (pBS::get('private') && $this->usr->is_admin())
421
+			echo '
422
+					<li><input type="checkbox" name="force_new_pw" /><strong>', pBL('force_new_key'), '</strong></li>';
423
+
424
+		if (pBS::get('human_check'))
425
+			echo '
426
+					<li>', pBS::get('human_question'), ':<input type="text" name="ru_human" value="', isset($_POST['ru_human']) ? $_POST['ru_human'] : '', '" /></li>';
427
+
428
+		echo '
429
+				</ul>';
430
+
431
+		if (!empty($id))
432
+			echo '
433
+				<input type="hidden" name="view" value="', $id, '" />';
434
+
435
+		echo '
436
+				<input id="submit" type="submit" name="submit" value="', pBL('submit'), '" />
437
+			</form>';
438
+	}
439
+
440
+	/*
441
+	* Shows an existing paste
442
+	* @param $id int The id of the paste to load up.
443
+	*/
444
+	public function showPaste($id)
445
+	{
446
+		// Get it from the database.
447
+		$paste = $this->db->fetchPaste($id);
448
+
449
+		if (!$this->usr->is_admin() && !empty($Paste['key']) && (empty($_REQUEST['key']) || $Paste['key'] != $_REQUEST['key']))
450
+			$this->error(pBL('error_no_access'), true);
451
+		elseif ($paste['board_id'] != pBS::get('paste_board'))
452
+			$this->error(pBL('error_no_access'), true);
453
+		elseif (empty($paste['approved']))
454
+			$this->error(pBL('error_approval'), true);
455
+
456
+		if (pBS::get('use_geshi') && !empty($paste['use_geshi']))
457
+		{
458
+			$type = !empty($_REQUEST['type']) ? $_REQUEST['type'] : (!empty($paste['language']) ? $Paste['language'] : 'php');
459
+
460
+			include_once(pBS::get('geshi_location')  . '/geshi.php');
461
+			
462
+			$geshi =& new GeSHi('', $type);
463
+			$geshi->set_header_type(GESHI_HEADER_PRE);
464
+			$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
465
+
466
+			$geshiErr =& new GeSHi($paste['body'], $type);
467
+			$geshiErr->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 2);
468
+			$topic_parsed = $geshiErr->parse_code();
469
+			$topic_parsed = str_replace('&lt;?php', '<&#063;php', $topic_parsed);
470
+
471
+			$paste['parsed'] = $topic_parsed;
472
+		}
473
+
474
+		return $paste;
475
+	}
476
+}
477
+
478
+/*
479
+* This is a function that passes the language calls to the
480
+* language class without using ugly $$var in the template.
481
+*/
482
+function pBL($string)
483
+{
484
+	$args = func_get_args();
485
+
486
+	if (count($args) == 1)
487
+		return pBL::$$string;
488
+	else
489
+	{
490
+		// Override it.
491
+		$args[0] = pBL::$$string;
492
+		return call_user_func_array('sprintf', $args);
493
+	}
494
+}
... ...
@@ -0,0 +1,32 @@
1
+<?php
2
+
3
+/*
4
+* PasteBin language
5
+*/
6
+class pBL
7
+{
8
+	public static $language = 'english';
9
+
10
+	public static $index_title = 'Create new';
11
+	public static $user_name = 'Name';
12
+	public static $email = 'Email';
13
+	public static $code = 'Code/text to paste';
14
+
15
+	public static $enable_geshi = 'Enable code highlighting';
16
+
17
+	public static $force_new_key = 'Force new key';
18
+
19
+	public static $submit = 'Submit';
20
+
21
+	public static $recent = 'Recent';
22
+	public static $create_new = 'Create new';
23
+
24
+	public static $error_no_access = 'Invlaid Key used';
25
+	public static $error_approval = 'This Paste requires approval';
26
+
27
+	// %1$s = ID of the paste.
28
+	public static $view_title = 'Viewing Paste %1$s';
29
+
30
+	public static $formated_paste = 'Formated Paste';
31
+	public static $plain_paste = 'Plain Paste';
32
+}
... ...
@@ -0,0 +1,18 @@
1
+.container_text
2
+{
3
+	font-weight: bold;
4
+}
5
+
6
+#settings_container
7
+{
8
+	list-style-type: none;
9
+}
10
+
11
+#formated_paste
12
+{
13
+	overflow: auto;
14
+}
15
+pre div
16
+{
17
+	display: inline !important;
18
+}
... ...
@@ -0,0 +1,2 @@
1
+<?php
2
+require_once(dirname(__FILE__) . '/index.php');
... ...
@@ -0,0 +1,21 @@
1
+<?php
2
+
3
+/*
4
+* PasteBin Settings
5
+*/
6
+class pBSe extends pBS
7
+{
8
+	private static $private = true;
9
+	private static $url_sef = false;
10
+	private static $paste_board = 17;
11
+	private static $recent_limit = 0;
12
+	private static $use_portal = true;
13
+	private static $portal_url = '/pastebin/private.php?';
14
+
15
+	public static function get($var)
16
+	{
17
+		if (isset(self::$$var))
18
+			return self::$$var;
19
+		return null;
20
+	}
21
+}
0 22
\ No newline at end of file
... ...
@@ -0,0 +1,65 @@
1
+<?php
2
+
3
+/*
4
+* PasteBin Settings
5
+*/
6
+class pBS
7
+{
8
+	// Private pastebin?
9
+	private static $private = false;
10
+
11
+	// Where are the sources?
12
+	private static $sources = './sources';
13
+
14
+	// Database Handler.
15
+	private static $db = 'smf';
16
+
17
+	// User Handler.
18
+	private static $user = 'smf';
19
+
20
+	// Language files location and default.
21
+	private static $languages = './languages';
22
+	private static $default_language = 'english';
23
+
24
+	// Where SMF is at.
25
+	private static $smf_dir = '../forum/';
26
+	private static $paste_board = 4;
27
+	private static $increase_postcout = true;
28
+	private static $enable_post_approval = false;
29
+	private static $use_smf_theme = false	;
30
+	private static $smf_theme_id = 1;
31
+
32
+	// The URLs
33
+	private static $url_sef = true;
34
+	private static $sef_base = '/pastebin';
35
+	private static $use_portal = false;
36
+	private static $portal_url = '';
37
+
38
+	// Where is the CSS?
39
+	private static $css = '/../pastebin/pb.css';
40
+
41
+	// Geshi stuff.
42
+	private static $use_geshi = true;
43
+	private static $geshi_location = './geshi';
44
+	private static $geshi_default = 'php';
45
+
46
+	// Be ye robot?
47
+	private static $human_check = true;
48
+	private static $human_question = 'A duck, cat and a goose walk into a bar. How many animals walked into a bar?';
49
+	private static $human_answer = '3';
50
+
51
+	// Recent limits.
52
+	private static $recent_limit = 10;
53
+	private static $recent_limit_admin = 50;
54
+
55
+	/*
56
+	* DO NOT MODIFY THIS.
57
+	* Allows applications to request settings without ability to change them.
58
+	*/
59
+	public static function get($var)
60
+	{
61
+		if (is_callable('pBSe::get') && pBSe::get($var) !== null)
62
+			return pBSe::get($var);
63
+		return self::$$var;
64
+	}
65
+}
... ...
@@ -0,0 +1,188 @@
1
+<?php
2
+
3
+/*
4
+* Basic handler for database interaction
5
+*/
6
+class pDB_smf extends pDB
7
+{
8
+	/*
9
+	* Gets SMF started up and ready for action.
10
+	*/
11
+	public function __construct()
12
+	{
13
+		// Wordpress does something with the cookie, so we need to pull in settings file to fix it.
14
+		global $time_start, $maintenance, $msubject, $mmessage, $mbname, $language;
15
+		global $boardurl, $boarddir, $sourcedir, $webmaster_email, $cookiename;
16
+		global $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error;
17
+		global $db_connection, $modSettings, $context, $sc, $user_info, $topic, $board, $txt;
18
+		global $smcFunc, $ssi_db_user, $scripturl, $ssi_db_passwd, $db_passwd, $cachedir;
19
+		global $ssi_theme, $ssi_layers;
20
+
21
+		// We strip the slashes from the cookie which resolves the issue.
22
+		require_once(pBS::get('smf_dir') . '/Settings.php');
23
+		if (isset($_COOKIE[$cookiename]))
24
+			$_COOKIE[$cookiename] = stripslashes($_COOKIE[$cookiename]);
25
+
26
+		// We need to setup this before we continue
27
+		if (pBS::get('use_smf_theme'))
28
+		{
29
+			$ssi_theme = pBS::get('smf_theme_id');
30
+			$ssi_layers = array('html', 'body');
31
+		}
32
+
33
+		require_once(pBS::get('smf_dir') . '/SSI.php');
34
+		require_once($sourcedir . '/Subs-Post.php');
35
+	}
36
+
37
+	/*
38
+	* Fetches the most recent from the SMF database.
39
+	* @param $limit int The limit on the recent pastes
40
+	*/
41
+	public function fetchRecent($limit)
42
+	{
43
+		$request = smcFunc::db_query('', '
44
+			SELECT t.id_topic as topic_id
45
+			FROM {db_prefix}topics AS t
46
+			WHERE t.id_board = {int:paste_board}
47
+			ORDER BY id_last_msg DESC
48
+			LIMIT {int:limit_recent}',
49
+			array(
50
+				'paste_board' => pBS::get('paste_board'),
51
+				'limit_recent' => $limit,
52
+				));
53
+
54
+		while($re = smcFunc::db_fetch_assoc($request))
55
+			$recent[] = $re['topic_id'];
56
+		smcFunc::db_free_result($request);
57
+		$recent = array_unique($recent);
58
+
59
+		return $recent;
60
+	}
61
+
62
+	/*
63
+	* Fetches all the information abouta a paste.
64
+	* @param $id int The id of the paste
65
+	*/
66
+	public function fetchPaste($id)
67
+	{
68
+		// Do the query..
69
+		$request = smcFunc::db_query('', '
70
+			SELECT id_msg, id_topic, id_board as board_id, body, subject, approved
71
+			FROM {db_prefix}messages
72
+			WHERE id_topic = {int:id_topic}
73
+			AND id_board = {int:paste_board}
74
+			ORDER BY poster_time DESC',
75
+			array(
76
+				'paste_board' => pBS::get('paste_board'),
77
+				'id_topic' => $id,
78
+				));
79
+		$topic = smcFunc::db_fetch_assoc($request);
80
+		smcFunc::db_free_result($request);
81
+
82
+		$ops = explode(':v:', $topic['subject']);
83
+		unset($ops[0]);
84
+
85
+		foreach($ops as $op)
86
+		{
87
+			$temp = explode('-', $op);
88
+			$Paste[$temp[0]] = $temp[1];
89
+		}
90
+
91
+		// This is how the data should return.
92
+		return array(
93
+			'id' => $topic['id_topic'],
94
+			'key' => $Paste['p'],
95
+			'board_id' => $topic['board_id'],
96
+			'approved' => $topic['approved'],
97
+			'use_geshi' => $Paste['use_geshi'],
98
+			'language' => $Paste['type'],
99
+			'body' => $topic['body'],
100
+			'parsed' => '',
101
+		);	
102
+	}
103
+
104
+	/*
105
+	* Tests adding/updating a paste to the database.
106
+	* @param $data array The data we are testing.
107
+		$data[paste_id] Int The id of the paste, default is 0.
108
+		$data[new_key] Bool To force a new key or not.
109
+		$data[name] String The name of the paster.
110
+		$data[email] String the email of the paster.
111
+		$data[use_geshi] Bool If we should use geshi highlighting or not.
112
+		$data[language] String The language of the code, default is php.
113
+		$data[body] String the actual contents of the paste.
114
+	*/
115
+	public function addPasteTest($data)
116
+	{
117
+	}
118
+
119
+	/*
120
+	* Actually adding/updating a paste to the database.
121
+	* @param $data array The data we are testing.
122
+	*	$data[paste_id] Int The id of the paste, default is 0.
123
+	*	$data[new_key] Bool To force a new key or not.
124
+	*	$data[name] String The name of the paster.
125
+	*	$data[email] String the email of the paster.
126
+	*	$data[use_geshi] Bool If we should use geshi highlighting or not.
127
+	*	$data[language] String The language of the code, default is php.
128
+	*	$data[body] String the actual contents of the paste.
129
+	* @return $result array The data we are returning.
130
+	*	$result[id] int The id of the paste.
131
+	*	$result[key] String The key of the paste, default is empty.
132
+	*	$result[updated] Bool Whether this was an update or not.
133
+	*/
134
+	public function addPaste($data)
135
+	{
136
+		// Fetch any data we need to know.
137
+		if (!empty($data['paste_id']))
138
+			$paste = $this->fetchPaste($data['paste_id']);
139
+
140
+		// Try to keep the key correct unless it should change.
141
+		if ((pBS::get('private') && empty($paste['key'])) || (!empty($paste['key']) && userInfo::_()->is_admin && isset($_POST['force_new_pw'])))
142
+			$data['key'] = $this->generateKey();
143
+		elseif (!empty($paste['key']))
144
+			$data['key'] = $paste['key'];
145
+
146
+		// Options needed for our post.
147
+		$topicOptions = array(
148
+			'id'		=> (!empty($paste['id']) ? $topic['id'] : 0) ,
149
+			'board'		=> pBS::get('paste_board'),
150
+			'mark_as_read'	=> false,
151
+			);
152
+		$posterOptions = array(
153
+			'id'		=> (isset(userInfo::_()->id) ? userInfo::_()->id: 0),
154
+			'name'		=> $data['name'],
155
+			'email'		=> $data['email'],
156
+			'ip'		=> userInfo::_()->ip,
157
+			'update_post_count'	=> (pBS::get('increase_postcout') && isset(userInfo::_()->id) ? 1 : 0),
158
+			);
159
+		$msgOptions = array(
160
+			'id'		=> 0,
161
+			'subject'	=> 'Paste-' . time() . ':v:use_geshi-' . (!empty($data['use_geshi']) ? 1 : 0) . ':v:type-' . (!empty($data['language']) ? $data['language'] : 'php') . (!empty($data['key']) ? ':v:p-' . $data['key'] : ''),
162
+			'body'		=> htmlspecialchars($data['body']),
163
+			'approved'	=> pBS::get('enable_post_approval') ? 0 : 1,
164
+			);
165
+
166
+		// Actually create the paste.
167
+		createPost($msgOptions, $topicOptions, $posterOptions);
168
+
169
+		// Return some info
170
+		return array(
171
+			'id' => $topicOptions['id'],
172
+			'key' => isset($data['key']) ? $data['key'] : '',
173
+			'updated' => $data['id'] ? true : false,
174
+		);
175
+	}
176
+}
177
+
178
+/*
179
+* smcFunc as a class.  Uses callStatic to emulate it.
180
+*/
181
+class smcFunc
182
+{
183
+	public static function __callStatic($name, $arguments)
184
+	{
185
+		global $smcFunc;
186
+		return call_user_func_array($smcFunc[$name], $arguments);
187
+	}
188
+}
0 189
\ No newline at end of file
... ...
@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+/*
4
+* Basic handler for database interaction
5
+*/
6
+class pDB
7
+{
8
+	/*
9
+	* How to create a key
10
+	*/
11
+	public function generateKey()
12
+	{
13
+		return substr(md5(uniqid(mt_rand(), true)), 0, 3) . substr(md5(uniqid(mt_rand(), true)), 0, 2);
14
+	}
15
+}
0 16
\ No newline at end of file
... ...
@@ -0,0 +1,113 @@
1
+<?php
2
+
3
+/*
4
+* Sets up the basic User handler for Pastebin
5
+*/
6
+class pUser_smf extends pUser
7
+{
8
+	/*
9
+	* The SMF user handler
10
+	*/
11
+	private $usr = null;
12
+
13
+	/*
14
+	* We don't need to do this since SMF is already started.
15
+	*/
16
+	public function __construct()
17
+	{
18
+		$this->usr = userInfo::_();
19
+	}
20
+
21
+	/*
22
+	* The users ID.
23
+	* @return int The id of the user.
24
+	*/
25
+	public function id()
26
+	{
27
+		return $this->usr->id;
28
+	}
29
+
30
+	/*
31
+	* The users language
32
+	* @return String english version of the language (ie german).
33
+	*/
34
+	public function language()
35
+	{
36
+		return $this->usr->language;
37
+	}
38
+
39
+	/*
40
+	* Is the user a guest?
41
+	* @return bool True if guest, false otherwise
42
+	*/
43
+	public function is_guest()
44
+	{
45
+		return $this->usr->is_guest;
46
+	}
47
+
48
+	/*
49
+	* Is the user an admin?
50
+	* @return bool True if admin, false otherwise
51
+	*/
52
+	public function is_admin()
53
+	{
54
+		return $this->usr->is_admin;
55
+	}
56
+
57
+	/*
58
+	* What is their name?
59
+	* @return String The name of the user. Guest is fine.
60
+	*/
61
+	public function name()
62
+	{
63
+		return $this->usr->name;
64
+	}
65
+
66
+	/*
67
+	* What is their email?
68
+	* @return String The email of the user. A default one is fine.
69
+	*/
70
+	public function email()
71
+	{
72
+		return $this->usr->email;
73
+	}
74
+}
75
+
76
+/*
77
+* userInfo as a class.  We kinda do a poor method, but its the best way for now.
78
+*/
79
+class userInfo
80
+{
81
+	public static $instanceID = 0;
82
+
83
+	public static function _()
84
+	{
85
+		if (self::$instanceID == 0)
86
+			self::$instanceID = new userInfo;
87
+		return self::$instanceID;
88
+	}
89
+
90
+	public function __set($key, $value)
91
+	{
92
+		global $user_info;
93
+		$user_info[$key] = $value;
94
+	}
95
+
96
+	public function __get($key)
97
+	{
98
+		global $user_info;
99
+		return isset($user_info[$key]) ? $user_info[$key] : null;
100
+	}
101
+
102
+	public function __isset($key)
103
+	{
104
+		global $user_info;
105
+		return isset($user_info[$key]);
106
+	}
107
+
108
+	public function __unset($key)
109
+	{
110
+		global $user_info;
111
+		unset($user_info[$key], $user_info[$key]);
112
+	}
113
+}
0 114
\ No newline at end of file
... ...
@@ -0,0 +1,9 @@
1
+<?php
2
+
3
+/*
4
+* Sets up the basic User handler for Pastebin
5
+*/
6
+class pUser
7
+{
8
+
9
+}
0 10
\ No newline at end of file
1 11