-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.php
102 lines (71 loc) · 2.44 KB
/
service.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
<?php
function createTable() {
$handle = sqlite_open('data.db', 0666, $error);
if (!$handle) die ($error);
// Create the table
$command = "CREATE TABLE Queue(
id INTEGER PRIMARY KEY,
URL TEXT NOT NULL
)";
$ok = sqlite_exec($handle, $command, $error);
if (!$ok) die("Cannot create database. $error. </br></br>");
else echo "Database created successfully</br></br>";
sqlite_close($handle);
}
function tableExists() {
$handle = sqlite_open('data.db', 0666, $error);
if (!$handle) die ($error);
$check = "SELECT name FROM sqlite_master WHERE type='table' AND name='Queue'";
$responce = sqlite_query($handle, $check);
$contents = sqlite_fetch_array($responce, SQLITE_ASSOC);
return $contents;
}
function requestPrintURL() {
$handle = sqlite_open('data.db', 0666, $error);
if (!$handle) die ($error);
$query = "SELECT * FROM Queue ORDER BY ROWID ASC LIMIT 1";
$result = sqlite_query($handle, $query);
$row = sqlite_fetch_array($result, SQLITE_ASSOC);
// If the table contains a row
if ($row) {
echo($row['URL']);
$query = "DELETE FROM Queue WHERE id=".$row['id'];
$result = sqlite_query($handle, $query);
}
/* else echo("Nothing to print."); */
sqlite_close($handle);
}
function printDatabase() {
$handle = sqlite_open('data.db', 0666, $error);
if (!$handle) die ($error);
//Do the query
$query = "SELECT * FROM Queue";
$result = sqlite_query($handle, $query);
//iterate over all the rows
while($row = sqlite_fetch_array($result, SQLITE_ASSOC)){
//iterate over all the fields
foreach($row as $key => $val){
//generate output
echo $key . ": " . $val . "<BR />";
}
}
sqlite_close($handle);
}
function addPrintURL($printURL) {
$handle = sqlite_open('data.db', 0666, $error);
if (!$handle) die ($error);
$command = "INSERT INTO Queue VALUES(NULL,'$printURL')";
$responce = sqlite_exec($handle, $command);
if (!$responce) die("Cannot add print to queue.");
else echo "Print added to queue.";
sqlite_close($handle);
}
function usage() {
echo("Usage:</br></br>Request print URL = ?requestPrintURL</br>List items in database = ?printDatabase</br>Add URL of a photo to print = ?addPrintURL&url=<i>urlOfPhoto</i>");
}
if (!tableExists()) createTable();
if(isset($_GET['requestPrintURL'])) die(requestPrintURL());
if(isset($_GET['printDatabase'])) die(printDatabase());
if(isset($_GET['addPrintURL']) && isset($_GET['url'])) die(addPrintURL($_GET['url']));
else usage();
?>