-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.inc.php
259 lines (231 loc) · 8.13 KB
/
main.inc.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
<?php
require_once 'vendor/autoload.php';
require_once 'parse-bib.php';
use Cocur\Slugify\Slugify;
header('Content-Type: text/html; charset=utf-8');
function get($array,$key,$default) {
return array_key_exists($key,$array) ? $array[$key] : $default;
}
global $BIB;
session_start();
function redirect($url) {
header("Location: $url");
die();
}
function reverse($name,$args=array()) {
global $BIB;
return $BIB->router->generate($name,$args);
}
function ensure_https($url) {
return preg_replace('/^http:/i','https:',$url);
}
function render($template,$args=array()) {
global $BIB;
echo $BIB->twig->render($template,$args);
}
function respond_404() {
header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
render('404.html');
}
function get_entry($entry_key) {
global $BIB;
$entry = $BIB->db->records[$entry_key];
if(!$entry) {
respond_404();
die();
}
return $entry;
}
function url_origin( $s, $use_forwarded_host = false )
{
$ssl = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
$sp = strtolower( $s['SERVER_PROTOCOL'] );
$protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
$port = $s['SERVER_PORT'];
$port = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
$host = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
$host = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
function request_url( $s, $use_forwarded_host = false )
{
return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
}
class Form {
function __construct($fields,$defaults=array()) {
$this->fields = $fields;
$this->data = $defaults;
foreach($this->fields as $field=>$options) {
if(isset($options['multi'])) {
if($_SERVER['REQUEST_METHOD']=='GET') {
$values = get($defaults,$field,array());
} else {
$values = array();
foreach($_POST as $name=>$value) {
if(preg_match('/^'.$field.'-(?P<n>\d+)-(?P<field>.*)$/',$name,$matches)) {
$n = $matches['n'];
$subfield = $matches['field'];
if(!isset($values[$n])) {
$values[$n] = array();
}
$values[$n][$subfield] = $value;
}
}
}
$this->data[$field] = $values;
} else {
if(isset($_POST[$field])) {
$this->data[$field] = $_POST[$field];
}
}
}
}
function clean() {
$this->cleaned_data = array();
foreach($this->fields as $field=>$options) {
$value = isset($this->data[$field]) ? $this->data[$field] : null;
if(isset($options['clean']) && is_callable($options['clean'])) {
$value = call_user_func_array($options['clean'],array($value));
}
$this->cleaned_data[$field] = $value;
}
}
}
class BibSite {
public $bibfile;
private $password;
public $template_dir;
public $root_url;
public $twig;
public $db;
public $type_options = array(
'article' => 'Article',
'book' => 'Book',
'chapter' => 'Chapter',
'online' => 'Web page',
'artwork' => 'Artwork',
'audio' => 'Audio',
'review' => 'Review',
'image' => 'Image',
'video' => 'Video',
'software' => 'Software',
'patent' => 'Patent',
'misc' => 'Miscellaneous'
);
function __construct($config) {
$this->config = $config;
$this->site_host = $config->site_host;
$this->site_title = $config->site_title;
$this->site_description = $config->site_description;
$this->language = $config->language;
$this->template_dir = $config->template_dir;
$this->password = $config->password;
$this->bibfile = $config->bibfile;
$this->root_url = $config->root_url;
$twig_loader = new Twig_Loader_Filesystem($this->template_dir);
$this->twig = new Twig_Environment($twig_loader,array(
));
$this->twig->addGlobal('settings',$this);
$this->twig->addGlobal('root',$this->root_url);
$this->twig->addGlobal('logged_in',array_key_exists('logged_in',$_SESSION) ? $_SESSION['logged_in'] : false);
$this->twig->addFunction(new Twig_SimpleFunction('reverse',"reverse"));
$this->twig->addFunction(new Twig_SimpleFunction('ensure_https',"ensure_https"));
$this->load_database();
$this->make_collections();
$this->router = new AltoRouter();
$this->router->setBasePath($this->root_url);
$this->router->addMatchTypes(array(
'key'=>'[^{}\s%#/]+'
));
}
function load_database() {
$bibjson = $this->bibfile . '.json';
if(is_file($bibjson) && is_readable($bibjson)) {
$mbib = filemtime($this->bibfile);
$mjson = filemtime($bibjson);
if($mjson > $mbib) {
$json = json_decode(file_get_contents($bibjson), TRUE);
$this->db = BibDatabase::from_json($json);
return;
}
}
$source = file_get_contents($this->bibfile);
$this->db = BibDatabase::from_bib($source);
file_put_contents($bibjson, json_encode($this->db->as_json()));
}
function make_collections() {
$this->collections = array();
foreach($this->db->records as $entry) {
$ecs = $this->entry_collections($entry);
foreach($ecs as $collection) {
$collection->entries[] = $entry;
}
}
ksort($this->collections);
}
function entry_collections($entry) {
$collections = get($entry->fields,'collections','');
if(is_array($collections)) {
$ecs = $collections;
} else {
$ecs = array_map(
function($c) {
return trim($c);
},
explode(",",get($entry->fields,'collections',''))
);
}
$ecs = array_filter($ecs,function($x){return $x!='';});
$ecs = array_map(function($collection) {
$slugify = new Slugify();
$slug = $slugify->slugify($collection);
if(!isset($this->collections[$slug])) {
$this->collections[$slug] = new Collection($collection,$slug);
}
return $this->collections[$slug];
},$ecs);
return $ecs;
}
function authenticate($password) {
$hash = crypt($password,'bibbo');
if($hash==$this->password) {
$_SESSION['logged_in'] = true;
return true;
} else {
return false;
}
}
function logged_in() {
return $_SESSION['logged_in'] === true;
}
function save_database() {
$now = date('Y-m-d-H-i-s');
copy($this->bibfile,"backups/{$now}-{$this->bibfile}");
file_put_contents($this->bibfile,$this->db->as_bib());
}
function entry_json($entry) {
$collections = $this->entry_collections($entry);
$collections_data = array();
foreach($collections as $collection) {
$collections_data[] = $collection->name;
}
$data = $entry->as_json();
$data = array_merge($data,[
'pdf' => $entry->pdf(),
'nicetype' => $this->type_options[$entry->type],
'collections' => $collections_data,
'url' => $entry->urls[0],
'view' => url_origin($_SERVER).reverse('view_entry',array('key'=>$entry->key))
]);
return $data;
}
}
class Collection {
function __construct($name,$slug) {
$this->name = $name;
$this->slug = $slug;
$this->entries = array();
}
}
$config = json_decode(file_get_contents("config.json"));
$BIB = new BibSite($config);