-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_helper.php
More file actions
executable file
·99 lines (86 loc) · 4.01 KB
/
php_helper.php
File metadata and controls
executable file
·99 lines (86 loc) · 4.01 KB
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
#!/usr/local/bin/php
<?php
header('Content-Type: text/plain; charset=utf-8');
if(isset($_FILES['image'])) {
$queue_dir = __DIR__ . "/queue";
$filename = strval(time()) . ".png";
$new_filename = $queue_dir . "/" . $filename;
$success = move_uploaded_file($_FILES['image']['tmp_name'], $new_filename);
try {
$drawingsdb = new SQLite3('drawings.db');
$statement = "CREATE TABLE IF NOT EXISTS queue (filename TEXT UNIQUE, caption TEXT, artist TEXT);";
$run = $drawingsdb->exec($statement);
$artist = SQLite3::escapeString($_POST['name']);
$statement = "INSERT OR IGNORE INTO queue (filename, artist) VALUES ('$filename', '$artist');";
$run = $drawingsdb->exec($statement);
} catch (Exception $ex) {
echo $ex->getMessage();
}
if ($success) {
echo "File was uploaded sucessfully.";
} else {
echo "File upload failed.";
}
} else if($_SERVER['REQUEST_METHOD'] === 'GET') {
if(isset($_GET['submissions'])) {
$saved_dir = __DIR__ . "/saved";
$files = array_diff(scandir($saved_dir), array('..', '.'));
$files_string = implode(",", $files);
echo "$files_string";
} else if (isset($_GET['drawings'])) {
// We expect the images folder to contain only folders of images & captions.txt
$img_folders = array_diff(scandir(__DIR__ . "/images"), array('..', '.', 'captions.txt'));
foreach($img_folders as $folder) {
$folder_dir = __DIR__ . "/images/" . $folder;
$folder_files = array_diff(scandir($folder_dir), array('..', '.'));
$folder_string = implode(',', $folder_files);
echo "$folder_string" . "\n";
}
} else if (isset($_GET['captions'])) {
try {
$drawingsdb = new SQLite3('drawings.db');
$statement = "CREATE TABLE IF NOT EXISTS drawings (filename TEXT UNIQUE, caption TEXT, artist TEXT);";
$run = $drawingsdb->exec($statement);
$captions_path = __DIR__ . "/images/captions.txt";
$captions_content = file_get_contents($captions_path);
$captions_arr = preg_split('/\r\n|\r|\n/', $captions_content);
foreach($captions_arr as $caption) {
$current_line = explode(',', $caption);
$filename = $current_line[0];
$caption_text = SQLite3::escapeString($current_line[1]);
$artist = $current_line[2];
$statement = "INSERT OR IGNORE INTO drawings (filename, caption, artist) VALUES ('$filename', '$caption_text', '$artist');";
$run = $drawingsdb->exec($statement);
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
} else if (isset($_GET['caption'])) {
$filename = $_GET['caption'];
try {
$drawingsdb = new SQLite3('drawings.db');
$statement = "SELECT caption FROM drawings WHERE filename='$filename';";
$run = $drawingsdb->query($statement);
if($run) {
$row = $run->fetchArray();
echo $row['caption'];
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
} else if (isset($_GET['artist'])) {
$filename = $_GET['artist'];
try {
$drawingsdb = new SQLite3('drawings.db');
$statement = "SELECT artist FROM drawings WHERE filename='$filename';";
$run = $drawingsdb->query($statement);
if($run) {
$row = $run->fetchArray();
echo $row['artist'];
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
}
?>