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
|
<?php
$version = 1.2;
$config = false;
try {
if (!is_readable("config.ini")) {
throw new Exception('File config.ini does not exist');
}
if (!$config = parse_ini_file("config.ini")) {
throw new Exception('Could not parse ini file');
}
// hash the password
if (isset($config['password'])) {
$hash = hash("sha256", $config['password']);
}
$curl = curl_init($config["url"]. "?format=json");
if (isset($hash)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Cookie: password=$hash"));
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = json_decode(curl_exec($curl));
curl_close($curl);
$search = read_stdin();
if (empty($search)) {
throw new Exception("Please enter a search term");
}
$args = explode(" ", $search);
$page = 1;
if ($args[0] == "-v" || $args[0] == "--version") {
echo $version + "\n";
die();
} else if ($args[0] == "-h" || $args[0] == "--help") {
echo "Usage: !blaup [query] #[page] | e.g: !blaup intense | !blaup intense marcus | ";
echo "!blaup intense marcus #2 (returns the second page of results)\n";
die();
}
// Check to see if user specified pagination
$pages = array_filter($args, function($elem) {
if (strpos($elem, "#") === 0) {
return true;
}
return false;
});
// reset the indexes after the array filter
$pages = array_merge($pages);
if (count($pages) > 1) {
throw new Exception("Too many page choices. Enter only one `#<page>`");
}
if (!empty($pages)) {
// Set the page
$page = substr($pages[0], 1);
}
if (!is_int((int)$page) || $page <= 0) {
throw new Exception("Page `" . $page . "` is not valid");
}
// remove the pagination from the search args
$pos = array_search("#" . $page, $args);
if ($pos) {
$args[$pos] = null;
}
// Really slow search
$results = [];
foreach ($output as $upload) {
$match = false;
foreach ($args as $arg) {
if (!is_null($arg) && stripos($upload->filename, $arg) === false) {
$match = false;
break;
}
$match = true;
}
if ($match) {
$results[] = $config["url"] . "/" . rawurlencode($upload->filename);
}
}
// Return results
$out = [];
if (empty($results)) {
echo "No results found\n";
} else {
$count = count($results);
$i = 0;
$start = ($page-1)*3;
$end = $start+2;
if ($start > ($count-1)) {
throw new Exception("No results for this page");
}
foreach($results as $result) {
if ($i < $start || $i > $end) {
$i++;
continue;
}
$out[] = $i+1 . ": " . $result;
$i++;
}
if ($count > 3) {
$out[] = "results (" . $count . ")";
}
}
echo implode(" - ", $out);
} catch (Exception $e) {
echo $e->getMessage() . "\n";die();
}
function read_stdin()
{
$fr=fopen("php://stdin","r"); // open our file pointer to read from stdin
$input = fgets($fr,128); // read a maximum of 128 characters
$input = rtrim($input); // trim any trailing spaces.
fclose ($fr); // close the file handle
return $input; // return the text entered
}
|