-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher.php
211 lines (190 loc) · 6.66 KB
/
dispatcher.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
<?php
require_once('config.php');
require_once('ebook.cls.php');
require_once('opds.cls.php');
require_once('browser.cls.php');
require_once('library.cls.php');
class Dispatcher {
private $db;
private $display;
public function __construct($odps = false) {
$this->db = new library();
if (!$odps) {
$this->display = new browserdisplay();
} else {
$this->display = new opdsdisplay();
}
}
public function handleRequest($path) {
$handler = 'handle'.$path[0];
$this->$handler($this->db, $path);
}
public function handle($db, $path) {
/**
$items = $this->listdir_by_date($path, $db);
foreach ($items as $id => $item) {
$book = new ebook($path.$item);
$authors[$book->sortauthor][$book->id] = $book;
$allbooks[$book->id] = $book;
}
**/
setcookie('booksel', '');
setcookie('selval', '');
$this->display->printHeader();
switch ($_GET['sort']) {
case 'name':
$list = $this->listdir_by_name($path, $db);
$this->display->printBookList($list, 'bookswide');
break;
case 'author':
$list = $this->listdir_by_author($path, $db);
$this->display->printAuthorList($list, 'author');
/**
$books = $db->getBooklist('title asc');
$this->display->printBookList($books, 'books');
**/
break;
case 'date':
$list = $this->listdir_by_date($path, $db);
$this->display->printBookList($list, 'bookswide');
break;
case 'tags':
$list = $db->getTagList(false);
$this->display->printAuthorList($list, 'tag');
break;
case 'recent':
$list = $this->listdir_by_date($path, $db, true);
$this->display->printBookList($list, 'bookswide');
break;
default:
if (ODPS == PORT) {
$this->display->printNavigation();
} else {
$list = $this->listdir_by_date($path, $db, true);
$this->display->printBookList($list, 'bookswide');
}
}
}
public function handleget($db, $path) {
$book = $db->getBook($path[1]);
#print_r($book);
header("Content-Type: application/epub");
echo file_get_contents($book->file);
exit;
}
public function handleauthor($db, $path) {
setcookie('booksel', 'author', 0, '/');
list($discard,$method, $author) = explode('/', $_SERVER['PATH_INFO']);
setcookie('selval', $author, 0, '/');
$list = $db->getBookList('added desc', 'where author = \'' . SQLite3::escapeString($path[1]) . '\'');
$this->display->printHeader();
$alist = $this->listdir_by_author($path, $db);
$this->display->printAuthorList($alist, 'author', $author);
if ($author) $this->display->printBookList($list, 'books');
$this->display->printFoot();
exit;
}
public function handletag($db, $path) {
setcookie('booksel', 'tag', 0, '/');
setcookie('selval', $path[1], 0, '/');
$list = $db->getTaggedBooks($path[1]);
$this->display->printHeader();
$alist = $db->getTagList(false);
$this->display->printAuthorList($alist, 'tag', $path[1]);
if ($path[1]) $this->display->printBookList($list, 'books');
$this->display->printFoot();
exit;
}
public function handlemeta($db, $path) {
$book = $db->getBook($path[1]);
$newbook = new ebook($book->file);
header("Content-Type: text/plain");
#print_r($newbook);
echo $newbook->allmeta->saveXML();
exit;
}
public function handleshow($db, $path) {
$book = $db->getBook($path[1]);
if($this->getproto() == 'epub') {
header("Content-Type: application/epub");
echo file_get_contents($book->file);
exit;
}
$type = (isset($_COOKIE['booksel']))? $_COOKIE['booksel'] : 'author';
$current = (isset($_COOKIE['selval']))? $_COOKIE['selval'] : $book->author;
setcookie('booksel', '');
setcookie('selval', '');
$list = ($type == 'tag') ? $db->getTagList() : $db->getAuthorlist();
$this->display->printHeader();
$this->display->printAuthorList($list, $type, $current);
$booklist = ($type == 'tag')? $db->getTaggedBooks($current) : $db->getBookList('added desc', 'where author = \'' . SQLite3::escapeString($current) . '\'');
$this->display->printBookList($booklist, 'books', $path[1]);
echo $this->display->showDetails(new ebook($book->file));
$this->display->printFoot();
exit;
}
public function handleedit($db, $path) {
$book = $db->getBook($path[1]);
$realbook = new ebook($book->file);
$realbook->id = $path[1];
$url = $_SERVER['PHP_SELF'];
$realbook->title = (isset($_POST['title'])) ? $_POST['title']:$realbook->title;
$realbook->author = (isset($_POST['author'])) ? $_POST['author']:$realbook->author;
$realbook->sortauthor = (isset($_POST['author'])) ? strtolower($_POST['author']):$realbook->sortauthor;
if (isset($_POST['tags'])) {
$tags = explode(',', $_POST['tags']);
$realbook->tags = array();
foreach($tags as $tag) {
$realbook->tags[] = trim($tag);
}
}
$realbook->summary = (isset($_POST['summary'])) ? $_POST['summary']:$realbook->summary;
$realbook->modify_meta();
$db->updateBook($realbook);
$type = $_COOKIE['booksel'];
$current = $_COOKIE['selval'];
setcookie('booksel', '');
setcookie('selval', '');
$list = ($type == 'tag') ? $db->getTagList() : $db->getAuthorlist();
$this->display->printHeader();
$this->display->printAuthorList($list, $type, $current);
$booklist = ($type == 'tag')? $db->getTaggedBooks($current) : $db->getBookList('added desc', 'where author = \'' . SQLite3::escapeString($current) . '\'');
$this->display->printBookList($booklist, 'books', $path[1]);
echo getEditForm($realbook, $url);
$this->display->printFoot();
exit;
}
public function handleread($db, $path) {
$book = $db->getBook($path[1]);
$realbook = new ebook($book->file);
header("Content-type: text/html");
echo $realbook->getChapter($path[2]);
exit;
}
public function handledelete($db, $path) {
$book = $db->getBook($path[1]);
$db->deleteBook($book);
header("Location: http://".SERVER.BASEURL);
exit;
}
public function getSuffix($file) {
list($name, $suffix) = explode('.', $file);
return $suffix;
}
public function listdir_by_date($path, $db, $limit = false){
return $db->getBooklist('added desc', null, $limit);
}
public function listdir_by_author($path, $db){
return $db->getAuthorlist('sortauthor asc');
}
public function listdir_by_name($path, $db){
return $db->getBooklist('title asc');
}
public function getproto() {
if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== false) {
return "epub";
} else {
return "http";
}
}
}