forked from jonathanlking/Instagram-Printer-Server
/
service.php
102 lines (71 loc) · 2.16 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() {
$db = new SQLite3('data.db');
if (!$db) die ();
// Create the table
$command = "CREATE TABLE Queue(
id INTEGER PRIMARY KEY,
URL TEXT NOT NULL
)";
$ok = $db->exec($command);
if (!$ok) die("Cannot create database. </br></br>");
else echo "Database created successfully</br></br>";
$db->close();
}
function tableExists() {
$db = new SQLite3('data.db');
if (!$db) die ();
$check = "SELECT name FROM sqlite_master WHERE type='table' AND name='Queue'";
$response = $db->query($check);
$contents = $response->fetchArray();
return $contents;
}
function requestPrintURL() {
$db = new SQLite3('data.db');
if (!$db) die ();
$query = "SELECT * FROM Queue ORDER BY ROWID ASC LIMIT 1";
$result = $db->query($query);
$row = $result->fetchArray();
// If the table contains a row
if ($row) {
echo($row['URL']);
$query = "DELETE FROM Queue WHERE id=".$row['id'];
$result = $db->query($query);
}
/* else echo("Nothing to print."); */
$db->close();
}
function printDatabase() {
$db = new SQLite3('data.db');
if (!$db) die ();
//Do the query
$query = "SELECT * FROM Queue";
$result = $db->query($query);
//iterate over all the rows
while($row = $result->fetchArray()){
//iterate over all the fields
foreach($row as $key => $val){
//generate output
echo $key . ": " . $val . "<BR />";
}
}
$db->close();
}
function addPrintURL($printURL) {
$db = new SQLite3('data.db');
if (!$db) die ();
$command = "INSERT INTO Queue VALUES(NULL,'$printURL')";
$response = $db->exec($command);
if (!$response) die("Cannot add print to queue.");
else echo "Print added to queue.";
$db->close();
}
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();
?>