aboutsummaryrefslogtreecommitdiffstats
path: root/query.php
blob: 6aa8ee099ac2f67731364d2f0ce9bc684bd5c5a9 (plain) (blame)
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
<?php
  include('scrapers/wallhaven.php');
  include('style/header.php');
  $config = require('config.php');
  $_SERVER['REQUEST_URI'] = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

  /* fix spaces in query */
  $_GET["q"] = preg_replace("/ /", "+", $_GET["q"]);

  /* parse paramaters */
  $_GET["SFW"]     ? $purity = "1"   : $purity = "0";
  $_GET["Sketchy"] ? $purity .= "1" : $purity .= "0";
  if ($_GET["NSFW"])
    /* HACK: to only use the api key when needed we inject it here */
    $purity .= "1&apikey=".$config["frontends"]["wallhaven"]["apikey"];
  else
    $purity .= "0";
  $_GET["General"] ? $categories = "1"       : $categories = "0";
  $_GET["Anime"]   ? $categories .= "1"      : $categories .= "0";
  $_GET["People"]  ? $categories .= "1"      : $categories .= "0";
  $_GET["page"]    ? $page = $_GET["page"] : $page = 1;

  $res = getwallpapers("https://wallhaven.cc/api/v1/search?q=".$_GET["q"]."&purity=".$purity."&categories=".$categories."&page=".$page);
  /* display wallpapers */
  printf("<div id='wallpapers'>\n");
  if (!$res[0])
    printf("No Results Found :(");
  else
    foreach($res[0] as $walls)
      printf("<a href='/view.php?w=%s&p=%s'><img id='%s' width=307 height=206 src=/proxy.php?i=%s loading='lazy'></a>", $walls["id"], $walls["purity"], $walls["purity"], $walls["thumbs"]["small"]);
  printf("</div>\n");

  /* page numbers */
  if (!$res || $res[1]["pages"] == 0 || !$res[0][0])
   return;

  printf("<br>\n<div id='footer'>\n");
  $_SERVER['REQUEST_URI'] = preg_replace("/&page=[0-9]+/i", "",
    $_SERVER['REQUEST_URI']);
  if ($page > 1)
    printf("<a href='%s&page=1'><<</a> <a href='%s&page=%d'><</a> ", 
            $_SERVER['REQUEST_URI'], $_SERVER['REQUEST_URI'], $page - 1);
  for ($i = $page; $i <= ($res[1]["pages"] > $page + 9 ? $page + 9 : $res[1]["pages"]); $i++)
    printf("<a href='%s&page=%d'>%d</a> ", $_SERVER['REQUEST_URI'], $i, $i);
  if ($page != $res[1]["pages"])
    printf("<a href='%s&page=%s'>></a> <a href='%s&page=%s'>>></a> ",
            $_SERVER['REQUEST_URI'], $page + 1, $_SERVER['REQUEST_URI'],
            $res[1]["pages"]);
  printf("</div>\n<br><br>\n");
?>