summaryrefslogtreecommitdiff
path: root/search.php
blob: f2acce8f22587aabd46f852ca323cb23c0e3fd1c (plain)
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
<?php

    $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
    $hash = hash("sha256", $config['password']);
    $curl = curl_init($config["url"]. "?format=json");
    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);

    // Really slow search
    $results = [];
    foreach ($output as $upload) {
        $match = false;
        foreach ($args as $arg) { 
            if (strpos($upload->filename, $arg) === false) {
                $match = false;
                break;
            }
            $match = true;
        }
        if ($match) {
            $results[] = $config["url"] . "/" . $upload->filename;
        }
    }

    // Return results
    if (empty($results)) {
        echo "No results found\n";
    } else {
        foreach($results as $result) {
            echo $result . "\n";
        }
    }

} 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
}