-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotate.php
179 lines (136 loc) · 7.07 KB
/
annotate.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
<?php
include_once 'data.php';
include_once 'functions.php';
session_write_close();
database_connect($database_path, 'library');
$table1 = $dbHandle->exec("CREATE TABLE IF NOT EXISTS yellowmarkers
(id INTEGER PRIMARY KEY,
userID INTEGER NOT NULL,
filename TEXT NOT NULL,
page INTEGER NOT NULL,
top TEXT NOT NULL,
left TEXT NOT NULL,
width TEXT NOT NULL,
UNIQUE (userID,filename,page,top,left))");
$table2 = $dbHandle->exec("CREATE TABLE IF NOT EXISTS annotations
(id INTEGER PRIMARY KEY,
userID INTEGER NOT NULL,
filename TEXT NOT NULL,
page INTEGER NOT NULL,
top TEXT NOT NULL,
left TEXT NOT NULL,
annotation TEXT NOT NULL,
UNIQUE (userID,filename,page,top,left))");
if (!empty($_GET['delete'])) {
if (!empty($_GET['dbid'])) {
if ($_GET['type'] == 'yellowmarker') {
//DELETE YELLOW MARKER
$dbHandle->exec("DELETE FROM yellowmarkers WHERE id=".intval($_GET['dbid']));
} elseif ($_GET['type'] == 'annotation') {
//DELETE ANNOTATION
$dbHandle->exec("DELETE FROM annotations WHERE id=".intval($_GET['dbid']));
}
}
if ($_GET['delete'] == 'all') {
$filename = preg_replace('/0-9\.pdf/', '', $_GET['filename']);
if ($_GET['type'] == 'yellowmarker') {
//DELETE ALL MARKERS
$dbHandle->exec("DELETE FROM yellowmarkers
WHERE userID=".intval($_SESSION['user_id'])." AND filename='".$filename."'");
} elseif ($_GET['type'] == 'annotation') {
//DELETE ALL ANNOTATIONS
$dbHandle->exec("DELETE FROM annotations
WHERE userID=".intval($_SESSION['user_id'])." AND filename='".$filename."'");
} elseif ($_GET['type'] == 'all') {
$dbHandle->beginTransaction();
//DELETE ALL MARKERS
$dbHandle->exec("DELETE FROM yellowmarkers
WHERE userID=".intval($_SESSION['user_id'])." AND filename='".$filename."'");
//DELETE ALL ANNOTATIONS
$dbHandle->exec("DELETE FROM annotations
WHERE userID=".intval($_SESSION['user_id'])." AND filename='".$filename."'");
$dbHandle->commit();
}
}
die('OK');
} elseif (!empty($_GET['dbid']) && !empty($_GET['edit']) && isset($_GET['annotation'])) {
//EDIT ANNOTATION TEXT
$annotation = $dbHandle->quote($_GET['annotation']);
$update = $dbHandle->exec("UPDATE annotations SET annotation=$annotation WHERE id=".intval($_GET['dbid']));
if ($update) die('OK');
} elseif(!empty($_SESSION['user_id']) && !empty($_GET['filename']) && !empty($_GET['page'])
&& !empty($_GET['top']) && !empty($_GET['left']) && !empty($_GET['save'])) {
$last_id = '';
$userid = $dbHandle->quote($_SESSION['user_id']);
$filename = $dbHandle->quote($_GET['filename']);
$page = $dbHandle->quote($_GET['page']);
$top = $dbHandle->quote($_GET['top']);
$left = $dbHandle->quote($_GET['left']);
if ($_GET['type'] == 'yellowmarker' && !empty($_GET['width'])) {
//SAVE YELLOW MARKER
$width = $dbHandle->quote($_GET['width']);
$dbHandle->beginTransaction();
$update = $dbHandle->exec("INSERT OR IGNORE INTO yellowmarkers (userID,filename,page,top,left,width) VALUES ($userid,$filename,$page,$top,$left,$width)");
} elseif ($_GET['type'] == 'annotation') {
//SAVE ANNOTATION
$annotation = '';
if (!empty($_GET['annotation'])) $annotation = $_GET['annotation'];
$annotation = $dbHandle->quote($annotation);
$dbHandle->beginTransaction();
$update = $dbHandle->exec("INSERT OR IGNORE INTO annotations (userID,filename,page,top,left,annotation) VALUES ($userid,$filename,$page,$top,$left,$annotation)");
}
$last_id = $dbHandle->lastInsertId();
$update = null;
$last_insert = null;
$dbHandle->commit();
die($last_id);
} elseif(!empty($_SESSION['user_id']) && !empty($_GET['filename']) && !empty($_GET['page']) && !empty($_GET['fetch'])) {
$userid = $dbHandle->quote($_SESSION['user_id']);
$filename = $dbHandle->quote($_GET['filename']);
$page = $dbHandle->quote($_GET['page']);
if ($_GET['type'] == 'yellowmarker') {
//READ YELLOW MARKERS
$result = $dbHandle->query("SELECT id,top,left,width FROM yellowmarkers WHERE filename=$filename AND page=$page AND userID=$userid");
} elseif ($_GET['type'] == 'annotation') {
if ($_GET['page'] == 'all') {
if (isset($_GET['user']) && $_GET['user'] == 'all') {
//READ ALL ANNOTATIONS ALL USERS
$result = $dbHandle->query("SELECT id,top,left,annotation,page FROM annotations
WHERE filename=$filename
ORDER BY CAST(page AS INTEGER) ASC, CAST(top AS INTEGER) ASC");
} else {
//READ ALL ANNOTATIONS ONE USER
$result = $dbHandle->query("SELECT id,top,left,annotation,page FROM annotations
WHERE filename=$filename AND userID=$userid
ORDER BY CAST(page AS INTEGER) ASC, CAST(top AS INTEGER) ASC");
}
} else {
//READ ANNOTATIONS FOR ONE PAGE
$result = $dbHandle->query("SELECT id,top,left,annotation FROM annotations
WHERE filename=$filename AND page=$page AND userID=$userid");
}
}
$annotations = $result -> fetchAll(PDO::FETCH_ASSOC);
while (list($key, $value) = each($annotations)) {
if (isset($value['annotation'])) $annotations[$key]['annotation'] = htmlspecialchars($value['annotation']);
}
die(json_encode($annotations));
} elseif(!empty($_SESSION['user_id']) && !empty($_GET['filename']) && !empty($_GET['page']) && !empty($_GET['fetchothers'])) {
$userid = $dbHandle->quote($_SESSION['user_id']);
$filename = $dbHandle->quote($_GET['filename']);
$page = $dbHandle->quote($_GET['page']);
if ($_GET['type'] == 'yellowmarker') {
//READ OTHERS' YELLOW MARKERS
$result = $dbHandle->query("SELECT id,userID,top,left,width FROM yellowmarkers WHERE filename=$filename AND page=$page AND userID!=$userid");
} elseif ($_GET['type'] == 'annotation') {
//READ OTHERS' ANNOTATIONS
$result = $dbHandle->query("SELECT id,userID,top,left,annotation FROM annotations WHERE filename=$filename AND page=$page AND userID!=$userid");
}
$annotations = $result -> fetchAll(PDO::FETCH_ASSOC);
while (list($key, $value) = each($annotations)) {
if (isset($value['annotation'])) $annotations[$key]['annotation'] = htmlspecialchars($value['annotation']);
if (isset($value['userID'])) $annotations[$key]['user'] = get_username($dbHandle, $usersdatabase_path, $value['userID']);
}
die(json_encode($annotations));
}
?>