forked from mikespub-org/seblucas-cops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
112 lines (102 loc) · 4.29 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
<?php
/**
* COPS (Calibre OPDS PHP Server) HTML main script
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Sébastien Lucas <[email protected]>
*
*/
use SebLucas\Cops\Calibre\Database;
use SebLucas\Cops\Input\Config;
use SebLucas\Cops\Input\Request;
use SebLucas\Cops\Input\Route;
use SebLucas\Cops\Output\Format;
use SebLucas\Cops\Output\JSONRenderer;
use SebLucas\Cops\Pages\PageId;
use SebLucas\Template\doT;
require_once __DIR__ . '/config.php';
// If we detect that an OPDS reader try to connect try to redirect to feed.php
if (preg_match('/(Librera|MantanoReader|FBReader|Stanza|Marvin|Aldiko|Moon\+ Reader|Chunky|AlReader|EBookDroid|BookReader|CoolReader|PageTurner|books\.ebook\.pdf\.reader|com\.hiwapps\.ebookreader|OpenBook)/', $_SERVER['HTTP_USER_AGENT'])) {
header('location: ' . Config::ENDPOINT["feed"]);
exit();
}
$request = new Request();
$page = $request->get('page');
$database = $request->database();
// Use the configured home page if needed
if (!isset($page)) {
$page = PageId::INDEX;
if (!empty(Config::get('home_page')) && defined('SebLucas\Cops\Pages\PageId::' . Config::get('home_page'))) {
$page = constant('SebLucas\Cops\Pages\PageId::' . Config::get('home_page'));
}
$request->set('page', $page);
}
// Access the database ASAP to be sure it's readable, redirect if that's not the case.
// It has to be done before any header is sent.
Database::checkDatabaseAvailability($database);
if (Config::get('fetch_protect') == '1') {
session_start();
if (!isset($_SESSION['connected'])) {
$_SESSION['connected'] = 0;
}
}
header('Content-Type:text/html;charset=utf-8');
$assets = Route::url(Config::get('assets'));
$data = ['title' => Config::get('title_default'),
'version' => Config::VERSION,
'opds_url' => Route::url(Config::ENDPOINT["feed"]),
'customHeader' => '',
'template' => $request->template(),
'server_side_rendering' => $request->render(),
'current_css' => Route::url($request->style()),
'favico' => Route::url(Config::get('icon')),
'assets' => $assets,
'images' => Route::url('images'),
'resources' => Route::url('resources'),
'templates' => Route::url('templates'),
'basedir' => Route::url('.'),
'getjson_url' => JSONRenderer::getCurrentUrl($request)];
if (preg_match('/Kindle/', $request->agent())) {
$data['customHeader'] = '<style media="screen" type="text/css"> html { font-size: 75%; -webkit-text-size-adjust: 75%; -ms-text-size-adjust: 75%; }</style>';
}
if ($request->template() == 'twigged') {
$loader = new \Twig\Loader\FilesystemLoader('templates/twigged');
$twig = new \Twig\Environment($loader);
$function = new \Twig\TwigFunction('str_format', function ($format, ...$args) {
//return str_format($format, ...$args);
return Format::str_format($format, ...$args);
});
$twig->addFunction($function);
$function = new \Twig\TwigFunction('asset', function ($file) use ($assets) {
return $assets . '/' . $file . '?v=' . Config::VERSION;
});
$twig->addFunction($function);
if ($request->render()) {
// Get the page data
$data['page_it'] = JSONRenderer::getJson($request, true);
if ($data['title'] != $data['page_it']['title']) {
$data['title'] .= ' - ' . $data['page_it']['title'];
}
}
echo $twig->render('index.html', ['it' => $data]);
return;
}
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
$headcontent = file_get_contents('templates/' . $request->template() . '/file.html');
$template = new doT();
$dot = $template->template($headcontent, null);
if ($request->render()) {
// Get the page data
$page_it = JSONRenderer::getJson($request, true);
if ($data['title'] != $page_it['title']) {
$data['title'] .= ' - ' . $page_it['title'];
}
echo($dot($data));
echo "<body>\n";
echo Format::serverSideRender($page_it, $request->template());
echo "</body>\n</html>\n";
return;
}
echo($dot($data));
echo "<body>\n";
echo "</body>\n</html>\n";