This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pico_private.php
134 lines (112 loc) · 3.98 KB
/
pico_private.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
<?php
/**
* A plugin that let you create a private Pico with authentication form
*
* @author Johan BLEUZEN
* @link http://www.johanbleuzen.fr
* @license http://opensource.org/licenses/MIT
*/
class Pico_Private {
private $config;
private $users;
private $url;
private $theme;
private $base_url;
public function __construct() {
$plugin_path = dirname(__FILE__);
if(file_exists($plugin_path .'/pico_private_conf.php')) {
global $pico_private_conf;
include_once($plugin_path .'/pico_private_conf.php');
$this->config = $pico_private_conf['config'];
$this->users = $pico_private_conf['users'];
session_start();
}
}
public function config_loaded(&$settings) {
$this->theme = $settings['theme'];
$this->base_url = $settings['base_url'];
}
public function request_url(&$url) {
$this->url = $url;
if($this->config['private'] == 'all') {
if($url == 'login') {
if(! isset($_SESSION['authed']) || $_SESSION['authed'] == false) {
return;
} else {
$this->redirect('/');
exit;
}
}
if(!isset($_SESSION['authed']) || $_SESSION['authed'] == false) {
$this->redirect('/login');
}
}
if($url == 'logout') {
session_destroy();
$this->redirect('/');
}
}
public function before_read_file_meta(&$headers) {
$headers['private'] = 'Private';
}
public function before_render(&$twig_vars, &$twig) {
if((!isset($_SESSION['authed']) || $_SESSION['authed'] == false) && $this->config['private'] == "all") {
$this->handleLogin($twig_vars, $twig);
}
$privateMeta = $twig_vars['meta']['private'];
if((!isset($_SESSION['authed']) || $_SESSION['authed'] == false) && $this->config['private'] == "meta" && $privateMeta == true) {
$twig_vars['redirect_url'] = "/" . $this->url;
$this->handleLogin($twig_vars, $twig);
}
if(isset($_SESSION['authed'])) {
$twig_vars['authed'] = $_SESSION['authed'];
$twig_vars['username'] = $_SESSION['username'];
}
}
private function redirect($url) {
header('Location: '. $this->base_url . $url);
exit;
}
private function handleLogin($twig_vars, $twig) {
if(isset($_POST['username'])) {
$postUsername = $_POST['username'];
}
if(isset($_POST['password'])) {
$postPassword = $_POST['password'];
}
if(!empty($postUsername) && !empty($postPassword)) {
$authenticated = false;
if($this->config['hash_type'] == 'sha1') {
if(isset($this->users[$postUsername]) == true && ($this->users[$postUsername] == sha1($postPassword))) {
$authenticated = true;
}
} else if($this->config['hash_type'] == 'bcrypt') {
if(isset($this->users[$postUsername]) == true && password_verify($postPassword, $this->users[$postUsername])) {
$authenticated = true;
}
}
if($authenticated == true) {
$_SESSION['authed'] = true;
$_SESSION['username'] = $postUsername;
if(isset($_POST['redirect_url'])) {
$this->redirect($_POST['redirect_url']);
}
$this->redirect('/');
} else {
$twig_vars['login_error'] = 'Invalid login';
$twig_vars['username'] = $postUsername;
}
}
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
$loader = new Twig_Loader_Filesystem(THEMES_DIR . $this->theme);
$twig_login = new Twig_Environment($loader, $twig_vars);
$twig_vars['meta']['title'] = "Login";
if(file_exists(THEMES_DIR . $this->theme . "/login.html")) {
echo $twig_login->render('login.html', $twig_vars);
} else {
echo '<h1>Pico private error</h1>';
echo '<h2>No "login.html" file found in theme ' . $this->theme;
}
exit;
}
}