-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.cls.php
218 lines (203 loc) · 7.57 KB
/
library.cls.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
<?php
require_once(__DIR__ . "/config.php");
class library{
private $db;
public function __construct($db = null) {
if (!$db) {
$db = LIBRARY;
}
$this->db = $this->getdb($db);
$this->checkTables();
}
private function getdb($dbname = "library.sqlite") {
$base=new SQLite3($dbname);
if (!$base)
{
echo "SQLite NOT supported.\n";
exit($err);
}
else
{
return $base;
}
}
private function checkTables() {
$q=$this->db->query("PRAGMA table_info(books)");
if ($q->fetchArray() < 1) {
if (!$this->db->exec("
CREATE TABLE books (
id INTEGER NOT NULL PRIMARY KEY,
title VARCHAR ( 255 ) NOT NULL,
author VARCHAR(255) NOT NULL,
sortauthor VARCHAR(255) NOT NULL,
file VARCHAR(255) NOT NULL,
summary TEXT,
md5id varchar(255) NOT NULL UNIQUE,
added timestamp NOT NULL
)")
) exit ("Create SQLite Database Error\n");
}
$q=$this->db->query("PRAGMA table_info(tags)");
if ($q->fetchArray() < 1) {
if (!$this->db->exec("
CREATE TABLE tags (
id INTEGER NOT NULL PRIMARY KEY,
tag VARCHAR ( 255 ) NOT NULL UNIQUE
)")
) exit ("Create SQLite Database Error\n");
}
$q=$this->db->query("PRAGMA table_info(taggedbooks)");
if ($q->fetchArray() < 1) {
if (!$this->db->exec("
CREATE TABLE taggedbooks (
bookid INTEGER NOT NULL,
tagid INTEGER NOT NULL
)")
) exit ("Create SQLite Database Error\n");
}
}
public function insertBook($ebook) {
$qry = "insert into books (title,
author,
sortauthor,
file,
summary,
md5id,
added)
values
('".SQLite3::escapeString($ebook->title)."',
'".SQLite3::escapeString($ebook->author)."',
'".SQLite3::escapeString($ebook->sortauthor)."',
'".SQLite3::escapeString($ebook->file)."',
'".SQLite3::escapeString($ebook->summary)."',
'".SQLite3::escapeString($ebook->id)."',
'".time()."')";
$this->db->exec($qry);
$qry = "select * from books where md5id = '".$ebook->id."'";
$res = $this->db->query($qry);
$row = $res->fetcharray();
$bookid = $row['id'];
$this->db->exec("DELETE FROM taggedbooks WHERE bookid = '$bookid'");
foreach($ebook->tags as $id => $tag) {
$qry = "select id from tags where tag = '$tag'";
$tagid = $this->db->querySingle($qry);
if (!$tagid) {
$this->db->exec("insert into tags (tag) values ('$tag')");
$tagid = $this->db->querySingle($qry);
}
$this->db->exec("INSERT INTO taggedbooks (bookid, tagid) values ('$bookid', '$tagid')");
}
}
public function updateBook($ebook) {
$qry = "update books
SET title = '".SQLite3::escapeString($ebook->title)."',
author = '".SQLite3::escapeString($ebook->author)."',
sortauthor = '".SQLite3::escapeString($ebook->sortauthor)."',
summary = '".SQLite3::escapeString($ebook->summary)."'
WHERE md5id = '".SQLite3::escapeString($ebook->id)."'";
$this->db->exec($qry);
$qry = "select * from books where md5id = '".$ebook->id."'";
$res = $this->db->query($qry);
$row = $res->fetcharray();
$bookid = $row['id'];
$this->db->exec("DELETE FROM taggedbooks WHERE bookid = '$bookid'");
foreach($ebook->tags as $id => $tag) {
$qry = "select id from tags where tag = '$tag'";
$tagid = $this->db->querySingle($qry);
if (!$tagid) {
$this->db->exec("insert into tags (tag) values ('$tag')");
$tagid = $this->db->querySingle($qry);
}
$this->db->exec("INSERT INTO taggedbooks (bookid, tagid) values ('$bookid', '$tagid')");
}
}
public function getBook($md5id) {
$qry = "select * from books where md5id = '".$md5id."'";
$res = $this->db->query($qry);
$row = $res->fetcharray();
$book = new ebook();
$book->title = $row['title'];
$book->author = $row['author'];
$book->sortauthor = $row['sortauthor'];
$book->file = $row['file'];
$book->summary = $row['summary'];
$book->id = $row['md5id'];
$tagquery = "select tag from tags join taggedbooks on taggedbooks.tagid = tags.id where taggedbooks.bookid = '" . $row['id'] . "'";
$tagres = $this->db->query($tagquery);
while($tagrow = $tagres->fetchArray()) {
$book->tags[] = $tagrow['tag'];
}
return $book;
}
public function getBooklist($order = 'added desc', $where = '', $limit = false) {
$lwhere = urldecode($where);
$booklist = array();
$limstr = ($limit) ? " LIMIT 30": '';
$qry = "select * from books $lwhere order by $order $limstr";
$res = $this->db->query($qry);
while ($row = $res->fetchArray()) {
$book = new ebook();
$book->title = $row['title'];
$book->author = $row['author'];
$book->sortauthor = $row['sortauthor'];
$book->file = $row['file'];
$book->summary = $row['summary'];
$book->id = $row['md5id'];
$book->updated = $row['added'];
$booklist[$book->sortauthor.$book->title] = $book;
}
return $booklist;
}
public function getAuthorlist($order = 'sortauthor asc') {
$booklist = array();
$qry = "select author, title, sortauthor from books order by $order";
$res = $this->db->query($qry);
while ($row = $res->fetchArray()) {
if(strlen($row['title']) > 0) {
$booklist[$row['author']]['name'] = $row['author'];
$booklist[$row['author']]['books'][] = $row['title'];
}
}
return $booklist;
}
public function getTagList($updatedtags = true) {
$booklist = array();
if(!$updatedtags) {
$where = " where tag not like 'last update%'";
}
$qry = "select * from tags $where order by tag asc";
$res = $this->db->query($qry);
while ($row = $res->fetchArray()) {
if(strlen($row['tag']) > 0) {
$count = $this->db->querySingle("select count(bookid) from taggedbooks where tagid = '".$row['id']."'");
$booklist[$row['tag']]['name'] = $row['tag'];
$booklist[$row['tag']]['books'][] = $count;
}
}
return $booklist;
}
public function getTaggedBooks($tag, $order = 'added desc') {
$booklist = array();
$qry = "select * from books join taggedbooks on taggedbooks.bookid = books.id join tags on tags.id = taggedbooks.tagid where tag = '$tag' order by $order";
$res = $this->db->query($qry);
while ($row = $res->fetchArray()) {
$book = new ebook();
$book->title = $row['title'];
$book->author = $row['author'];
$book->sortauthor = $row['sortauthor'];
$book->file = $row['file'];
$book->summary = $row['summary'];
$book->id = $row['md5id'];
$book->updated = $row['added'];
$booklist[$book->sortauthor.$book->title] = $book;
}
return $booklist;
}
public function deleteBook($book) {
$bookid = $this->db->querySingle('select id from books where md5id =\''.$book->id."'");
$this->db->exec("delete from books where id = '$bookid'");
$this->db->exec("delete from taggedbooks where bookid = '$bookid'");
rename(dirname($book->file), "/Users/".USER."/.Trash/".basename(dirname($book->file)));
}
}
?>