-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.php
590 lines (543 loc) · 17.9 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
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
<?php
# Bumpy Booby
# Copyright (c) 2013-2015 Pierre Monchalin
# <http://bumpy-booby.derivoile.fr>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
### Load classes
function loadclass($classe) { require './classes/'.str_replace('\\', '/', $classe).'.class.php'; }
spl_autoload_register('loadClass');
### Catch errors
set_error_handler(array('Text', 'capture_error'));
define('NAME', 'Bumpy Booby');
define('VERSION', '0.3');
define('AUTHOR', 'Pierre Monchalin');
define('URL', 'http://bumpy-booby.derivoile.fr');
### Languages
define('LANGUAGES', 'en,fr'); # Separated by a comma
define('DEFAULT_LANGUAGE', 'en'); # Used only during installation
### Directories and files
define('DIR_CURRENT', dirname(__FILE__).'/');
define('DIR_DATABASE', dirname(__FILE__).'/database/');
define('DIR_LANGUAGES', dirname(__FILE__).'/languages/');
define('FOLDER_PROJECT', 'project_%name%/');
define('FOLDER_UPLOADS', 'uploads/');
define('FOLDER_IDENTICONS', 'identicons/');
define('FILE_LOGS', 'logs.txt');
define('FILE_CONFIG', 'config.php');
define('FILE_USERS', 'users.php');
define('FILE_UPLOADS', 'uploads.php');
define('FILE_ISSUES', 'issues.php');
### Standart settings
define('DEFAULT_COLOR', '#333333');
define('DEFAULT_STATUS', 'default');
define('DEFAULT_GROUP', 'default');
define('DEFAULT_GROUP_SUPERUSER', 'superuser');
define('DEFAULT_USER', 'nobody');
define('DEFAULT_PROJECT', 'default');
define('PRIVATE_LABEL', 'private');
define('TIMEOUT', 3600); # 1 hour
define('TIMEOUT_TOKENS', 900); # 15 minutes
define('SALT', 'How are you doing, pumpkin?');
### Thanks to Sebsauvage and Shaarli for the way I store data
define('PHPPREFIX', '<?php /* '); # Prefix to encapsulate data in php code.
define('PHPSUFFIX', ' */ ?>'); # Suffix to encapsulate data in php code.
### Default settings
if (is_file(DIR_DATABASE.FILE_CONFIG)) {
$config = Text::unhash(get_file(FILE_CONFIG));
$config['users'] = Text::unhash(get_file(FILE_USERS));
# We need $config to load the correct language
require DIR_LANGUAGES.'Trad_'.$config['language'].'.class.php';
}
else {
# We load language first because we need it in $config
if (isset($_POST['language']) && Text::check_language($_POST['language'])) {
# Needed at installation
require DIR_LANGUAGES.'Trad_'.$_POST['language'].'.class.php';
}
else {
require DIR_LANGUAGES.'Trad_'.DEFAULT_LANGUAGE.'.class.php';
}
$config = Settings::get_default_config(DEFAULT_LANGUAGE);
}
### Upgrade
if ($config['version'] != VERSION) {
require DIR_CURRENT.'upgrade.php';
exit;
}
### Manage sessions
$cookie = session_get_cookie_params();
# Force cookie path (but do not change lifetime)
session_set_cookie_params($cookie['lifetime'], Text::dir($_SERVER["SCRIPT_NAME"]));
# Use cookies to store session.
ini_set('session.use_cookies', 1);
# Force cookies for session.
ini_set('session.use_only_cookies', 1);
# Prevent php to use sessionID in URL if cookies are disabled.
ini_set('session.use_trans_sid', false);
session_name('BumpyBooby');
session_start();
$page = new Page();
### Try to get the current project
function getProject() {
global $config;
if (onlyDefaultProject()) { return DEFAULT_PROJECT; }
if (isset($_GET['project'])) {
if (!isset($config['projects'][$_GET['project']])) { return false; }
if (!canAccessProject($_GET['project'])) { return false; }
return $_GET['project'];
}
return false;
}
function onlyDefaultProject() {
global $config;
if (count($config['projects']) == 1
&& isset($config['projects'][DEFAULT_PROJECT])
) {
return true;
}
return false;
}
### Check permissions
function canAccess($page) {
global $config;
if ($config['last_update'] === false) { return true; }
if (!isset($config['permissions'][$page])) { return true; }
if (!$config['loggedin']) { $group = 'none'; }
else {
if (isset($config['users'][$_SESSION['id']])) {
$group = $config['users'][$_SESSION['id']]['group'];
}
else { $group = 'none'; }
}
if (in_array($group, $config['permissions'][$page])) {
return true;
}
return false;
}
function canAccessProject($project) {
global $config;
if (!isset($config['projects'][$project])) { return false; }
if (!$config['loggedin']) { $group = 'none'; }
else {
if (isset($config['users'][$_SESSION['id']])) {
$group = $config['users'][$_SESSION['id']]['group'];
}
else { $group = 'none'; }
}
if (in_array($group, $config['projects'][$project]['can_access'])) {
return true;
}
return false;
}
### Manage tokens
if (!isset($_SESSION['tokens'])) { $_SESSION['tokens'] = array(); }
function getToken() {
$rnd = Text::randomKey(40);
$_SESSION['tokens'][$rnd] = time()+TIMEOUT_TOKENS;
return $rnd;
}
function tokenOk($token) {
if (isset($_SESSION['tokens'][$token])) {
$t = $_SESSION['tokens'][$token];
unset($_SESSION['tokens'][$token]); // Token is used: destroy it.
if (time() > $t) { return false; } // Token expired.
return true; // Token is ok.
}
return false; // Wrong token, or already used.
}
### Returns the IP address of the client
# (used to prevent session cookie hijacking)
function getAllIPs() {
$ip = $_SERVER["REMOTE_ADDR"];
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip .= '_'.$_SERVER['HTTP_X_FORWARDED_FOR'];
}
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip .= '_'.$_SERVER['HTTP_CLIENT_IP'];
}
return $ip;
}
### Authentification
function logout() {
if (isset($_SESSION['uid'])) {
unset($_SESSION['uid']);
unset($_SESSION['id']);
unset($_SESSION['username']);
unset($_SESSION['ip']);
}
}
if (isset($_POST['login'])
&& isset($_POST['username'])
&& isset($_POST['password'])
&& isset($_POST['token'])
) {
if (!tokenOk($_POST['token'])) {
$page->addAlert(Trad::A_ERROR_TOKEN);
}
else {
logout();
$settings = new Settings;
$matching_user = array();
$wait = time();
foreach ($config['users'] as $u) {
if ($u['username'] == $_POST['username']) {
$wait = max($wait, $u['wait_until']);
if ($u['hash'] ==
Text::getHash($_POST['password'], $_POST['username'])
) {
$matching_user = $u;
}
else {
$settings->login_failed($u['id']);
}
}
}
if ($wait > time()) {
$page->addAlert(str_replace(
array('%duration%', '%period%'),
Text::timeDiff($wait, time()),
Trad::A_ERROR_LOGIN_WAIT
));
}
elseif (!empty($matching_user)) {
$_SESSION['uid'] = Text::randomKey(40);
$_SESSION['id'] = $matching_user['id'];
$_SESSION['username'] = $matching_user['username'];
$_SESSION['ip'] = getAllIPs();
$_SESSION['expires_on'] = time()+TIMEOUT;
# 0 means "When browser closes"
session_set_cookie_params(0, Text::dir($_SERVER["SCRIPT_NAME"]));
session_regenerate_id(true);
logm('Login successful.');
$settings->login_successful($u['id']);
$page->addAlert(Trad::A_LOGGED, 'alert-success');
}
else {
logm('Login failed for user “'.str_replace(
array("\n", "\t"),
'',
$_POST['username'])
.'”');
$wait = time();
foreach ($config['users'] as $u) {
if ($u['username'] == $_POST['username']) {
$wait = max($wait, $u['wait_until']);
}
}
if ($wait > time()) {
$page->addAlert(str_replace(
array('%duration%', '%period%'),
Text::timeDiff($wait,time()),
Trad::A_ERROR_CONNEXION_WAIT
));
}
else {
$page->addAlert(Trad::A_ERROR_CONNEXION);
}
}
}
}
elseif (isset($_POST['logout']) && isset($_POST['token'])) {
if (tokenOk($_POST['token'])) {
logout();
$page->addAlert(Trad::A_LOGGED_OUT, 'alert-success');
}
else {
$page->addAlert(Trad::A_ERROR_TOKEN);
}
}
if (!isset($_SESSION['uid'])
|| empty($_SESSION['uid'])
|| $_SESSION['ip'] != getAllIPs()
|| time() > $_SESSION['expires_on']
) {
logout();
$config['loggedin'] = false;
}
else {
$_SESSION['expires_on'] = time()+TIMEOUT;
$config['loggedin'] = true;
}
### Manage directories and files
function update_file($filename, $content) {
if (file_put_contents(DIR_DATABASE.$filename, $content) === false
|| strcmp(file_get_contents(DIR_DATABASE.$filename), $content) != 0)
{
logm('Enable to write file “'. DIR_DATABASE.$filename.'”');
Text::stop(str_replace(
'%name%',
DIR_DATABASE.$filename,
Trad::A_ERROR_FILE_WRITE
));
}
}
function get_file($filename) {
$text = file_get_contents(DIR_DATABASE.$filename);
if ($text === false) {
logm('Enable to read file “'. DIR_DATABASE.$filename.'”');
Text::stop(str_replace(
'%name%',
DIR_DATABASE.$filename,
Trad::A_ERROR_FILE
));
}
return $text;
}
function check_dir($dirname) {
if (!is_dir(DIR_DATABASE.$dirname)
&& (!mkdir(DIR_DATABASE.$dirname, 0705)
|| !chmod(DIR_DATABASE.$dirname, 0705))
) {
logm('Enable to create directory “'. DIR_DATABASE.$filename.'”');
Text::stop(str_replace(
'%name%',
DIR_DATABASE.$dirname,
Trad::A_ERROR_DIRECTORY)
);
}
}
function check_file($filename, $content = '') {
if (!is_file(DIR_DATABASE.$filename)) {
update_file($filename, $content);
}
}
function logm($text) {
global $config;
if ($config['logs_enabled']) {
$text = date('Y-m-d H:i:s').' – '.$_SERVER["REMOTE_ADDR"].' – '.$text."\n";
file_put_contents(DIR_DATABASE.FILE_LOGS, $text, FILE_APPEND);
}
}
check_dir('');
check_dir(FOLDER_UPLOADS);
check_dir(FOLDER_IDENTICONS);
check_file(FILE_LOGS);
check_file(FILE_UPLOADS, Text::hash(array()));
check_file(FILE_USERS, Text::hash(array()));
check_file('.htaccess', "Allow from none\nDeny from all\n");
if (!is_file(DIR_DATABASE.FILE_CONFIG)) {
$page->load('install');
}
else {
if (!isset($_GET['page'])) { $_GET['page'] = 'home'; }
if ($_GET['page'] == 'ajax') {
require dirname(__FILE__).'/pages/ajax.php';
exit;
}
if ($_GET['page'] == 'rss') {
require dirname(__FILE__).'/pages/rss.php';
exit;
}
elseif ($_GET['page'] == 'identicons') {
require dirname(__FILE__).'/pages/identicons.php';
exit;
}
elseif ($_GET['page'] == 'downloads') {
require dirname(__FILE__).'/pages/downloads.php';
# We do not exit because we want to load error/404 or error/403
# in some cases
}
else {
$page->load($_GET['page']);
}
}
$menu = '';
if (getProject()) {
if (canAccess('dashboard')) {
$menu .= '<li class="m_dashboard">'
.'<a href="'.Url::parse(getProject().'/dashboard').'">'
.Trad::T_DASHBOARD
.'</a>'
.'</li>';
}
if (canAccess('issues')) {
$menu .= '<li class="m_issues">'
.'<a href="'.Url::parse(getProject().'/issues').'">'
.Trad::T_BROWSE_ISSUES
.'</a>'
.'</li>';
}
if (canAccess('new_issue')) {
$menu .= '<li class="m_newissue">'
.'<a href="'.Url::parse(getProject().'/issues/new').'">'
.Trad::T_NEW_ISSUE
.'</a>'
.'</li>';
}
}
elseif (canAccess('home')) {
$menu = '<li class="m_home">'
.'<a href="'.Url::parse('home').'">'.Trad::T_PROJECTS.'</a>'
.'</li>';
}
if (canAccess('settings')) {
$menu .= '<li class="m_settings">'
.'<a href="'.Url::parse('settings').'">'.Trad::T_SETTINGS.'</a>'
.'</li>';
}
?>
<!DOCTYPE html>
<!--[if lt IE 8]><html dir="ltr" lang="<?php echo $config['language']; ?>" class="ie lt8 lt9"><![endif]-->
<!--[if IE 8]><html dir="ltr" lang="<?php echo $config['language']; ?>" class="ie ie8 lt9"><![endif]-->
<!--[if IE 9]><html dir="ltr" lang="<?php echo $config['language']; ?>" class="ie ie9"><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html dir="ltr" lang="<?php echo $config['language']; ?>"><!--<![endif]-->
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<link rel="shortcut icon" href="<?php echo Url::parse('favicon.ico'); ?>" />
<link rel="apple-touch-icon" href="<?php echo Url::parse('apple-touch-icon.png'); ?>" />
<link rel="stylesheet" href="<?php echo Url::parse('public/css/app.min.css'); ?>" />
<!--[if lt IE 9]>
<script src="<?php echo Url::parse('public/js/html5.js'); ?>"></script>
<script src="<?php echo Url::parse('public/js/respond.js'); ?>"></script>
<![endif]-->
<title><?php echo $page->getTitle(); ?> – <?php echo $config['title']; ?></title>
<?php
if (getProject()) {
echo '<link rel="alternate" type="application/rss+xml" href="'
.Url::parse(getProject().'/rss').'" title="'.Trad::W_RSS.'" />';
}
?>
</head>
<body>
<?php echo $page->getAlerts(); ?>
<!--[if lt IE 8]>
<div class="alert alert-error"><?php echo Trad::A_IE; ?></div>
<![endif]-->
<header>
<div class="header-inner">
<a href="javascript:;" class="a-menu">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</a>
<span class="brand"><?php
echo '<a href="'.Url::parse('home').'">'.$config['title'].'</a>';
if (getProject() && !onlyDefaultProject()) {
echo '<span class="slash">/</span><a class="a-project" href="'.Url::parse(getProject().'/dashboard').'">'.getProject().'</a>';
}
?></span>
</div>
</header>
<div class="main">
<aside class="main-right">
<div class="main-right-open div-affix"><?php echo Trad::W_MENU; ?></div>
<div class="main-right-inner div-affix">
<nav>
<ul>
<?php echo $menu; ?>
</ul>
</nav>
<?php
if (canAccess('search') && getProject()) {
?>
<form action="<?php echo Url::parse(getProject().'/search'); ?>" method="post" class="form-search">
<input type="hidden" name="action" value="search" />
<input type="text" name="q" value="<?php echo (isset($_GET['q'])) ? htmlspecialchars($_GET['q']) : ''; ?>" placeholder="<?php echo Trad::S_SEARCH; ?>" class="input-left" />
<button type="submit" class="a-icon-hover"><i class="icon-white icon-search"></i></button>
</form>
<?php
}
?>
<div class="div-labels">
<?php
if (getProject()) {
foreach ($config['labels'] as $k => $v) {
if ($k != PRIVATE_LABEL
|| canAccess('private_issues')
) {
echo '<a href="'.Url::parse(getProject().'/labels/'.$k).'" class="label" style="background-color:'.$v['color'].'">'.$v['name'].'</a>';
}
}
}
?>
</div>
<?php
if (!$config['loggedin']) {
?>
<form action="" method="post" class="form-log-in">
<?php
if (canAccess('signup')) {
?>
<a href="<?php echo Url::parse('signup'); ?>" class="btn"><?php echo Trad::V_SIGNUP; ?></a>
<?php
}
?>
<input type="text" name="username" placeholder="<?php echo Trad::F_USERNAME2; ?>" />
<input type="password" name="password" placeholder="<?php echo Trad::F_PASSWORD2; ?>" class="input-left" />
<button type="submit" class="a-icon-hover"><i class="icon-white icon-circle-arrow-right"></i></button>
<input type="hidden" name="token" value="<?php echo getToken(); ?>" />
<input type="hidden" name="login" value="1" />
</form>
<?php
}
else {
?>
<form method="post" class="form-log-out">
<p><?php echo str_replace('%user%', '<a href="'.Url::parse('users/'.intval($_SESSION['id'])).'">'.htmlspecialchars($_SESSION['username']).'</a>', Trad::S_WELCOME); ?></p>
<input type="hidden" name="token" value="<?php echo getToken(); ?>" />
<input type="hidden" name="logout" value="1" />
<button type="submit" class="a-icon-hover"><i class="icon-white icon-off"></i></button>
</form>
<?php
}
?>
<div class="div-copyright">
<?php
if (getProject()) {
echo '<a href="'.Url::parse(getProject().'/rss').'">'
.Trad::W_RSS
.'</a><br />';
}
echo str_replace(
'%name%',
'<a href="'.URL.'">'.NAME.' '.VERSION.'</a>',
Trad::S_COPYRIGHT
);
?>
</div>
</div>
</aside>
<section class="main-left">
<?php echo $page->getContent(); ?>
</section>
</div>
<script src="<?php echo Url::parse('public/js/highlighter.js'); ?>"></script>
<script src="<?php echo Url::parse('public/js/jquery-1.9.1.min.js'); ?>"></script>
<script>
var ajax = "<?php echo Url::parse('public/ajax'); ?>",
token = "<?php echo getToken(); ?>",
verb_edit = "<?php echo Trad::V_EDIT; ?>",
verb_preview = "<?php echo Trad::V_PREVIEW; ?>",
confirm_delete_issue = "<?php echo Trad::A_CONFIRM_DELETE_ISSUE; ?>",
confirm_delete_comment = "<?php echo Trad::A_CONFIRM_DELETE_COMMENT; ?>",
confirm_delete_upload = "<?php echo Trad::A_CONFIRM_DELETE_UPLOAD; ?>";
$(document).ready(function() {
$(".m_<?php echo $page->getSafePage(); ?>").addClass("active");
});
</script>
<script src="<?php echo Url::parse('public/js/scripts.min.js'); ?>"></script>
<script>
<?php echo $page->getJavascript(); ?>
</script>
</body>
</html>