forked from CitizensDev/CitizensScriptRepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
698 lines (682 loc) · 30 KB
/
index.php
File metadata and controls
698 lines (682 loc) · 30 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
<?php
ini_set('display_errors', 'On');
session_start();
if(!isset($_SESSION['loggedIn'])){ $_SESSION['loggedIn'] = false; }
if(!isset($_SESSION['admin'])){ $_SESSION['admin'] = false; }
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');
// Get the arguments from the url
$_SERVER['REQUEST_URI_PATH'] = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
$path = explode('/', trim($_SERVER['REQUEST_URI_PATH'], '/'));
array_shift($_GET);
// Mailer
require_once('assets/phpmailer/mail.php');
// AreYouAHuman
require_once('assets/ayah.php');
function alphaID($in, $to_num = false, $pad_up = false){
/*
* Translates a number to a short alhanumeric version
*
* @author Kevin van Zonneveld <kevin@vanzonneveld.net>
* @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net)
* @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
* @version SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $
* @link http://kevin.vanzonneveld.net/
*
* @param mixed $in String or long input to translate
* @param boolean $to_num Reverses translation when true
* @param mixed $pad_up Number or boolean padds the result up to a specified length
*
* @return mixed string or long
*/
$index = "abcdefghijklmnopqrstuvwxyz0123456789";
$base = strlen($index);
if ($to_num) {
// Digital number <<-- alphabet letter code
$in = strrev($in);
$out = 0;
$len = strlen($in) - 1;
for ($t = 0; $t <= $len; $t++) {
$bcpow = bcpow($base, $len - $t);
$out = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
}
if (is_numeric($pad_up)) {
$pad_up--;
if ($pad_up > 0) {
$out -= pow($base, $pad_up);
}
}
} else {
// Digital number -->> alphabet letter code
if (is_numeric($pad_up)) {
$pad_up--;
if ($pad_up > 0) {
$in += pow($base, $pad_up);
}
}
$out = "";
for ($t = floor(log10($in) / log10($base)); $t >= 0; $t--) {
$a = floor($in / bcpow($base, $t));
$out = $out . substr($index, $a, 1);
$in = $in - ($a * bcpow($base, $t));
}
$out = strrev($out); // reverse
}
return $out;
}
function validEmail($email){
/*
* Email validation function. Credit to http://www.linuxjournal.com/article/9585.
*/
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") ||
↪checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}
function random_string(){
$character_set_array = array();
$character_set_array[] = array('count' => 4, 'characters' => 'abcdefghijklmnopqrstuvwxyz');
$character_set_array[] = array('count' => 2, 'characters' => '0123456789');
$temp_array = array();
foreach ($character_set_array as $character_set) {
for ($i = 0; $i < $character_set['count']; $i++) {
$temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)];
}
}
shuffle($temp_array);
return implode('', $temp_array);
}
function createPubID(){
$connectionHandle = $GLOBALS['connectionHandle'];
// Dangerous, but meh. Likelyhood of it getting stuck in a loop approaches infintesimal values quickly.
while(true){
$outputID = random_string();
$query = $connectionHandle->query("SELECT * FROM repo_entries WHERE pubID='$outputID'");
if($query->num_rows==0){ return $outputID; }
}
}
// Handle search queries in the string
if(isset($_POST['q'])){
$query = str_replace(array("%20", " "), "+", $_POST['q']);
header('Location: http://scripts.citizensnpcs.com/search/'.$query.'/1/1/1/1');
exit;
}
if(isset($_POST['q2'])){
$query = str_replace(array("%20", " "), "+", $_POST['searchBox']);
if(isset($_POST['1'])){
// Users
$query = $query."/1";
}else{
$query = $query."/0";
}
if(isset($_POST['2'])){
// Code
$query = $query."/1";
}else{
$query = $query."/0";
}
if(isset($_POST['3'])){
// Tags
$query = $query."/1";
}else{
$query = $query."/0";
}
if(isset($_POST['4'])){
// Descriptions
$query = $query."/1";
}else{
$query = $query."/0";
}
header('Location: http://scripts.citizensnpcs.com/search/'.$query);
exit;
}
function getCurrentTimeZone($username){
$username = $GLOBALS['connectionHandle']->real_escape_string($username);
$query = $GLOBALS['connectionHandle']->query("SELECT * FROM repo_users WHERE username='$username'");
if($query!=false){
$row = $query->fetch_assoc();
return trim($row['timezone']);
}
}
function getTimeZoneOptions($active){
$timezone_identifiers = DateTimeZone::listIdentifiers();
$selected = '';
$data = null;
$continent = null;
foreach( $timezone_identifiers as $value ){
if ( preg_match( '/^(America|Antartica|Arctic|Asia|Atlantic|Europe|Indian|Pacific)\//', $value ) ){
$ex=explode("/",$value);//obtain continent,city
if ($continent!=$ex[0]){
if ($continent!="") $data = $data.'</optgroup>';
$data = $data.'<optgroup label="'.$ex[0].'">';
}
$city=$ex[1];
$continent=$ex[0];
if($value==$active){ $selected='selected="selected"'; }
if(isset($ex[2])){ $city = implode("/",array($ex[1], $ex[2])); }
$data = $data.'<option value="'.$value.'" '.$selected.'>'.$city.'</option>';
}
$selected = '';
}
return $data;
}
function getResults($queryHandle, $numberPerPage, $pageNumber){
$outputArray = array();
$count = 0;
$limiter = ($pageNumber-1)*$numberPerPage;
while(count($outputArray)<$numberPerPage){
if($count>=$limiter){
$outputArray[count($outputArray)] = $queryHandle->fetch_assoc();
}else{
$queryHandle->fetch_assoc();
}
$count = $count+1;
}
return $outputArray;
}
// GeSHi
require_once('assets/geshi.php');
// Smarty
include('assets/Smarty/Smarty.class.php');
$smarty = new Smarty;
$smarty->setTemplateDir('/usr/share/nginx/www/scripts/assets/templates');
$smarty->setCompileDir('/usr/share/nginx/www/scripts/assets/Smarty/templates_c');
$smarty->setCacheDir('/usr/share/nginx/www/scripts/assets/Smarty/cache');
$smarty->setConfigDir('/usr/share/nginx/www/scripts/assets/Smarty/configs');
$smarty->assign('loggedIn', $_SESSION['loggedIn']);
$smarty->assign('admin', $_SESSION['admin']);
$smarty->assign('adminNeeded', false);
if($_SESSION['loggedIn']){ $smarty->assign('username', $_SESSION['username']); }
// This is just on git.
include('password.php');
$connectionHandle = new mysqli('localhost', 'repo', $password, 'ScriptRepo');
include('assets/bcrypt.php');
function isValidLogin($user, $password){
$bCrypt = new Bcrypt(12);
$username = $GLOBALS['connectionHandle']->real_escape_string($user);
$result = $GLOBALS['connectionHandle']->query("SELECT * FROM repo_users WHERE username='$username'");
$row = $result->fetch_assoc();
if($bCrypt->verify($password, $row['password'])){ return true; }else{ return false; }
}
function isActiveUser($user){
$username = $GLOBALS['connectionHandle']->real_escape_string($user);
$result = $GLOBALS['connectionHandle']->query("SELECT * FROM repo_users WHERE username='$username'");
$row = $result->fetch_assoc();
if($row['status']==1){ return true; }else{ return false; }
}
$bCrypt = new Bcrypt(12);
#$query2 = $connectionHandle->query("SELECT * FROM repo_flags");
//if($query2->num_rows>0){ $smarty->assign('adminNeeded', true); }
switch(strtolower($path[0])){
case 'credits':
$output = 'credits.tpl';
break;
case 'login':
$smarty->assign('activePage', 'login');
$smarty->assign('loginError', false);
$smarty->assign('loginMessage', false);
$smarty->assign('passwordError', false);
$smarty->assign('userError', false);
$smarty->assign('loginInfo', false);
$smarty->assign('username', false);
if(isset($_SESSION['loginInfo'])){
$smarty->assign('loginInfo', $_SESSION['loginInfo']);
unset($_SESSION['loginInfo']);
}
if(isset($_SESSION['loginMessage'])){
$smarty->assign('loginMessage', $_SESSION['loginMessage']);
unset($_SESSION['loginMessage']);
}
if(isset($_POST['loginForm'])){
// Checks
if($_POST['username']=="" || $_POST['password']==""){
$smarty->assign('username', $_POST['username']);
$smarty->assign('loginError', 'You must enter both a username and password.');
$smarty->assign('userError', true);
$smarty->assign('passwordError', true);
$output = 'login.tpl';
}elseif(!isValidLogin($_POST['username'], $_POST['password'])){
$smarty->assign('username', $_POST['username']);
$smarty->assign('loginError', 'Invalid username or password!');
$smarty->assign('passwordError', true);
$output = 'login.tpl';
}elseif(!isActiveUser($_POST['username'])){
$smarty->assign('username', $_POST['username']);
$smarty->assign('loginError', 'You must activate your email before you can log in! <a href="http://scripts.citizensnpcs.com/resendConfirmation">Resend confirmation email.</a>');
$_SESSION['attemptedUsername'] = $connectionHandle->real_escape_string($_POST['username']);
$output = 'login.tpl';
}else{
// Login
$_SESSION['loggedIn'] = true;
$_SESSION['username'] = $_POST['username'];
$query = $connectionHandle->query("SELECT * FROM repo_users WHERE username='".$_POST['username']."'");
if($query!==false){
$row = $query->fetch_assoc();
if($row['staff']==1){ $_SESSION['admin'] = true; }else{ $_SESSION['admin'] = false; } }
header('Location: http://scripts.citizensnpcs.com/');
exit;
}
}elseif($_SESSION['loggedIn']){
// Are they already logged in?
header('Location: http://scripts.citizensnpcs.com/user/'.$_SESSION['username']);
exit;
}else{
$output = 'login.tpl';
}
break;
case 'settings':
if(!$_SESSION['loggedIn']){
$_SESSION['loginInfo'] = 'You must be logged in to change settings!';
header('Location: http://scripts.citizensnpcs.com/login');
exit;
}
$smarty->assign('successMessage', false);
$currentTimezone = getCurrentTimeZone($_SESSION['username']);
$smarty->assign('timezones', getTimeZoneOptions($currentTimezone));
if(isset($_POST['Save'])){
// Handle the update.
if($_POST['timezone']!=$currentTimezone){
// They updated the timezone. Make the changes.
$newTimezone = $connectionHandle->real_escape_string($_POST['timezone']);
$connectionHandle->query("UPDATE repo_users SET timezone='$newTimezone' WHERE username='".$_SESSION['username']."'");
$currentTimezone = $newTimezone;
}
$smarty->assign('successMessage', "Successfully updated your settings.");
}
$smarty->assign('timezones', getTimeZoneOptions($currentTimezone));
$smarty->assign('activePage', 'settings');
$output = 'settings.tpl';
break;
case 'logout':
session_destroy();
session_start();
$_SESSION['loginMessage'] = 'You have been successfully logged out.';
header('Location: http://scripts.citizensnpcs.com/login');
exit;
break;
case "resendconfirmation":
$query = $connectionHandle->query("SELECT * FROM repo_users WHERE username='".$_SESSION['attemptedUsername']."'");
$row = $query->fetch_assoc();
$mailer = new Mailer();
$mailer->sendConfirmationEmail($row['email'], $_SESSION['attemptedUsername']);
$_SESSION['loginMessage'] = 'Confirmation email successfully sent to '.$row['email'].'!';
header('Location: http://scripts.citizensnpcs.com/login');
$output = 'login.tpl';
break;
case 'register':
$ayah = new AYAH($publisherKey, $scoringKey);
$mailer = new Mailer(true);
$smarty->assign('activePage', false);
$smarty->assign('username', false);
$smarty->assign('email', false);
$smarty->assign('registerError', false);
$smarty->assign('usernameError', false);
$smarty->assign('emailError', false);
$smarty->assign('passwordError', false);
$smarty->assign('ayahError', false);
$smarty->assign('ayah', $ayah->getPublisherHTML());
if(isset($_POST['registerForm'])){
$email = $connectionHandle->real_escape_string($_POST['email']);
$emailQuery = $connectionHandle->query("SELECT * FROM repo_users WHERE email='$email'");
$user = $connectionHandle->real_escape_string($_POST['username']);
$userQuery = $connectionHandle->query("SELECT * FROM repo_users WHERE user='$user'");
// Checks
if(strlen($_POST['password'])<5){
// Make sure the password is 5 characters long
$smarty->assign('registerError', 'Password must be more than 5 characters!');
$smarty->assign('username', $_POST['username']);
$smarty->assign('email', $_POST['email']);
$smarty->assign('passwordError', true);
$output = 'register.tpl';
}elseif($_POST['password']!==$_POST['passwordConfirm']){
// Make sure the passwords match
$smarty->assign('registerError', 'Passwords do not match!');
$smarty->assign('username', $_POST['username']);
$smarty->assign('email', $_POST['email']);
$smarty->assign('passwordError', true);
$output = 'register.tpl';
}elseif($user=="NeonMaster"){
$smarty->assign('registerError', 'OMG IT\'S NEON!');
$smarty->assign('username', $_POST['username']);
$smarty->assign('email', $_POST['email']);
$smarty->assign('passwordError', true);
$smarty->assign('usernameError', true);
}elseif(strlen($_POST['username'])<3 || strlen($_POST['username']>17)){
$smarty->assign('registerError', 'Username must be between 4 and 16 characters!');
$smarty->assign('username', $_POST['username']);
$smarty->assign('email', $_POST['email']);
$smarty->assign('usernameError', true);
$output = 'register.tpl';
}elseif(!validEmail($_POST['email'])){
// Make sure the email address is a valid email address
$smarty->assign('username', $_POST['username']);
$smarty->assign('email', $_POST['email']);
$smarty->assign('emailError', true);
$smarty->assign('registerError', 'Invalid email address!');
$output = 'register.tpl';
}elseif($emailQuery->num_rows>0){
// Make sure the email address isn't being used
$smarty->assign('username', $_POST['username']);
$smarty->assign('email', $_POST['email']);
$smarty->assign('emailError', true);
$smarty->assign('registerError', 'Email already in use!');
$output = 'register.tpl';
}elseif($userQuery->num_rows===0){
// Make sure the username isn't taken
$smarty->assign('username', $_POST['username']);
$smarty->assign('email', $_POST['email']);
$smarty->assign('userError', true);
$smarty->assign('registerError', 'Username already in use!');
$output = 'register.tpl';
}elseif(!$ayah->scoreResult()){
// Make sure the AYAH was correct
$smarty->assign('username', $_POST['username']);
$smarty->assign('email', $_POST['email']);
$smarty->assign('ayahError', true);
$smarty->assign('registerError', "The AreYouAHuman game wasn't completed properly. Please try it again.");
$output = 'register.tpl';
}else{
// Register
$bCrypt = new Bcrypt(12);
$pass = $bCrypt->hash($_POST['password']);
$connectionHandle->query("INSERT INTO repo_users (id, username, password, email, timezone, status, staff) VALUES ('NULL', '$user', '$pass', '$email', 'America/New_York', '0', false)") or die("MYSQL ERROR!");
// Send them their confirmation email, too.
$mailer = new Mailer();
$mailer->sendConfirmationEmail($email, $user);
header('Location: http://scripts.citizensnpcs.com/login');
$_SESSION['loginMessage'] = 'You have now been registered, but must confirm your email before you can login.';
exit;
}
}else{
$output = 'register.tpl';
}
break;
case 'post':
$smarty->assign('postError', false);
$smarty->assign('scriptError', false);
$smarty->assign('scriptCode', false);
$smarty->assign('description', false);
$smarty->assign('descriptionError', false);
$smarty->assign('typeError', false);
$smarty->assign('tagError', false);
$smarty->assign('tags', false);
$smarty->assign('name', false);
$smarty->assign('nameError', false);
if(!$_SESSION['loggedIn']){
$_SESSION['loginInfo'] = 'You must be logged in to post new scripts!';
header('Location: http://scripts.citizensnpcs.com/login');
exit;
}
if(isset($_POST['SubmitScript'])){
// Run some checks, make sure the data is good.
$tagsRaw = explode(',', $_POST['tags']);
$tags = array();
foreach($tagsRaw as $tag){
if($tag!=""){ array_push($tags, trim($tag)); }
}
if($_POST['name']==""){
// Name is empty
$smarty->assign('postError', "Name must not be empty!");
$smarty->assign('nameError', true);
}elseif($_POST['Description']==""){
// Description is empty.
$smarty->assign('postError', "Description must not be empty!");
$smarty->assign('descriptionError', true);
}elseif($_POST['scriptCode']==""){
// Script code is empty
$smarty->assign('postError', "Code must not be empty!");
$smarty->assign('scriptError', true);
}elseif($_POST['typeOfScript']=="None"){
// No type has been selected!
$smarty->assign('postError', "Script type must be selected!");
$smarty->assign('typeError', true);
}elseif(count($tags)==0){
$smarty->assign('postError', "You must enter at least one tag!");
$smarty->assign('tagError', true);
}else{
// Everything passed, lets get to work.
$typeOfScript = intval($_POST['typeOfScript']);
if(isset($_POST['privacy'])){ $privacy = 2; }else{ $privacy = 1; }
$scriptCode = $connectionHandle->real_escape_string($_POST['scriptCode']);
$description = $connectionHandle->real_escape_string($_POST['Description']);
$name = $connectionHandle->real_escape_string($_POST['name']);
$username = $_SESSION['username'];
$tagString = implode(', ', $tags);
$pubID = createPubID();
$timestamp = time();
// We've got all the variables, now lets run the queries.
$connectionHandle->query("INSERT INTO repo_entries (id, pubID, author, name, description, tags, privacy, scriptType, likes, timestamp, downloads, views) VALUES ('NULL', '$pubID', '$username', '$name', '$description', '$tagString', '$privacy', '$typeOfScript', 0, '$timestamp', 0, 0)");
$connectionHandle->query("INSERT INTO repo_code (id, pubID, code) VALUES ('NULL', '$pubID', '$scriptCode')");
header('Location: http://scripts.citizensnpcs.com/view/'.$pubID);
}
if(isset($_POST['scriptCode']) && $_POST['scriptCode']!=""){ $smarty->assign('scriptCode', $_POST['scriptCode']); }
if(isset($_POST['Description']) && $_POST['Description']!=""){ $smarty->assign('description', $_POST['Description']); }
if(isset($_POST['tags']) && $_POST['tags']!=""){ $smarty->assign('tags', $_POST['tags']); }
}
$smarty->assign('activePage', 'post');
$output = 'post.tpl';
break;
case 'verify':
$user = $connectionHandle->real_escape_string($path[1]);
$query = $connectionHandle->query("SELECT * FROM repo_users WHERE username='$user' AND status=0");
var_dump($path);
if(!isset($path[1]) || !isset($path[2]) || $path[2]!=md5($path[1]) || $query->num_rows===0){
// Something's wrong.
header('Location: http://scripts.citizensnpcs.com/');
exit;
}else{
// Verify user
$connectionHandle->query("UPDATE repo_users SET status=1 WHERE username='$user'");
$_SESSION['loginMessage'] = 'You have successfully confirmed your email. You may now log in.';
header('Location: http://scripts.citizensnpcs.com/login');
exit;
}
break;
case 'edit':
break;
case 'search':
$smarty->assign('activePage', false);
$query = $connectionHandle->real_escape_string(urldecode($path[1]));
$searchSettings = array($path[2], $path[3], $path[4], $path[5]);
$smarty->assign('searchQuery', $query);
$smarty->assign('searchSettings', $searchSettings);
$output = 'result.tpl';
break;
case 'admin':
$query = $connectionHandle->query("SELECT * FROM repo_users WHERE username=".$_SESSION['username']." AND staff=1");
if(!$_SESSION['loggedIn'] || $query->num_rows!==1){
header('Location: http://scripts.citizensnpcs.com/login');
$_SESSION['loginInfo'] = 'You must be logged in to do that!';
exit;
}
$smarty->assign('activePage', 'admin');
$output = 'admin.tpl';
break;
case 'support':
$smarty->assign('activePage', false);
$output = 'support.tpl';
break;
case 'test':
if(!$_SESSION['loggedIn'] || $_SESSION['username']!="AgentKid"){
header('Location: http://scripts.citizensnpcs.com/');
exit;
}
$queryEmail = $connectionHandle->query("SELECT * FROM repo_users WHERE status=0");
while($row = $queryEmail->fetch_assoc()){
$mailer = new Mailer();
$mailer->sendConfirmationEmail($row['email'], $row['username']);
echo "Mailed to ".$row['username']." at ".$row['email']."\n";
}
exit;
case 'list':
$smarty->assign('activePage', 'list');
$queryListing = $connectionHandle->query("SELECT * FROM repo_entries WHERE privacy=1");
$numberPerPage = 20;
$pageNumber = 1;
$resultPages = array(1, 2, 3, 4, 5);
// Get the page number and number of results per page.
if(isset($path[1])){
$pageNumber = intval($path[1]);
if(isset($path[2])){ $numberPerPage = intval($path[2]); }
}
if($queryListing!=false){
$numberOfPages = ceil($queryListing->num_rows/$numberPerPage);
$resultData = getResults($queryListing, $numberPerPage, $pageNumber);
$smarty->assign('resultArray', $resultData);
}
if($numberOfPages<5){
$limit = $numberOfPages;
$start = 1;
}elseif($pageNumber+2>$numberOfPages){
$limit = $numberOfPages;
$start = $pageNumber-(4-($numberOfPages-$pageNumber));
}else{
$limit = $pageNumber+2;
$start = $pageNumber-2;
}
if($numberOfPages!=0){ $resultPages = range($start, $limit); }else{ $resultPages = array(1); }
$smarty->assign('resultPageNumber', $pageNumber);
$smarty->assign('resultsPerPage', $numberPerPage);
$smarty->assign('resultPages', $resultPages);
$queryUsers = $connectionHandle->query("SELECT * FROM repo_users");
if($queryUsers!=false){ $userArray = getResults($queryUsers, $numberPerPage, $pageNumber); }
$smarty->assign('userArray', $userArray);
$output = 'list.tpl';
break;
case 'view':
$pubID = $connectionHandle->real_escape_string(strtolower($path[1]));
$smarty->assign('commentField', false);
$smarty->assign('commentFailure', false);
$smarty->assign('commentSuccess', false);
$query = $connectionHandle->query("SELECT * FROM repo_entries WHERE pubID='$pubID'");
$queryCode = $connectionHandle->query("SELECT * FROM repo_code WHERE pubID='$pubID'");
if($query->num_rows==0 && false){
$output = '404.tpl';
}else{
// $dataToUse gets taken by view.php and turned into the main page.
if(isset($_POST['commentField'])){
// So someone has commented on a page that does exist. Lets handle it.
if(!$_SESSION['loggedIn']){
// If they submitted a comment without being logged in, reject it.
$_SESSION['loginInfo'] = 'You must be logged in to view user profiles!';
header('Location: http://scripts.citizensnpcs.com/login');
exit;
}
$commentField = $connectionHandle->real_escape_string($_POST['commentField']);
if(strlen($commentField)<5){
$smarty->assign('commentFailure', 'Please don\'t spam. Comments must be longer than 5 characters.');
$smarty->assign('commentField', $commentField);
}else{
// Allow the comment!
$user = $_SESSION['username'];
$connectionHandle->query("INSERT INTO repo_comments (id, entryID, author, timestamp, content) VALUES ('NULL', '$pubID', '$user', now(), '$commentField')");
$smarty->assign('commentSuccess', 'Your comment has been posted.');
}
}
$queryComments = $connectionHandle->query("SELECT * FROM repo_comments WHERE entryID='$pubID'");
$commentData = array();
while($row = $queryComments->fetch_assoc()){
$commentData[count($commentData)] = $row;
}
$data = $query->fetch_assoc();
$smarty->assign('dataToUse', $data);
$code = $queryCode->fetch_assoc();
$geshi = new GeSHi(htmlspecialchars_decode($code['code']), 'yaml');
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5);
$smarty->assign('code', $geshi->parse_code());
$newviews = $data['views']+1;
$connectionHandle->query("UPDATE repo_entries SET views='$newviews' WHERE pubID='$pubID'");
$smarty->assign('activePage', 'view');
$smarty->assign('commentData', $commentData);
$output = 'view.tpl';
}
break;
case 'user':
if(!$_SESSION['loggedIn']){
$_SESSION['loginInfo'] = 'You must be logged in to view user profiles!';
header('Location: http://scripts.citizensnpcs.com/login');
exit;
}
break;
case 'recover':
if(isset($_POST['recover'])){
$_SESSION['loginInfo'] = 'A new password has been sent to your email address!';
header('Location: http://scripts.citizensnpcs.com/login');
exit;
}
$output = 'recover.tpl';
break;
case '':
case 'index':
case 'home':
$smarty->assign('activePage', 'home');
$output = 'home.tpl';
break;
default:
$output = '404.tpl';
break;
}
/*
* If the page is supposed to read raw data, echo it and die.
*/
if(!isset($output)){ $smarty->assign('output', '404.tpl'); }else{ $smarty->assign('output', $output); }
$smarty->display('index.tpl');
?>