aboutsummaryrefslogtreecommitdiffstats
path: root/misc/tools.php
blob: 0fec976e2c8874e38d26d81ef03b89163bcc5234 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
  include_once('scrapers/wallhaven.php');
  $config = require(__DIR__.'/../config.php');

  function timesince($time) {
    $time = time() - $time;
    $timeunits = [
      31536000 => 'year',
      2592000 => 'month',
      604800 => 'week',
      86400 => 'day',
      3600 => 'hour',
      60 => 'minute',
    ];

    foreach ($timeunits as $unit => $text) {
      if ($time < $unit)
        continue;
      $numunits = floor($time / $unit);
      return ' '.$numunits.' '.$text.(($numunits > 1) ? 's' : '');
    }
  }

  function humanfilesize($size, $unit = "") {
    if ((!$unit && $size >= 1 << 30) || $unit == "GB")
      return number_format($size / (1 << 30),2)."GB";
    if ((!$unit && $size >= 1 << 20) || $unit == "MB")
      return number_format($size / (1 << 20),2)."MB";
    if ((!$unit && $size >= 1 << 10) || $unit == "KB")
      return number_format($size / (1 << 10),2)."KB";
    return number_format($size)." bytes";
  }

  function toupperpurity($str) {
    if ($str == "sfw")
      return strtoupper($str);
    if ($str == "sketchy")
      return ucfirst($str);
    if ($str == "nsfw")
      return strtoupper($str);
    return $str;
  }

  function humanquery($q, $nsfw = false) {
    global $config;
    if ($nsfw)
      $apikey = $config["frontends"]["wallhaven"]["apikey"];
    else
      $apikey = "";

    if (preg_match("/id:[0-9+]/", $q)) {
      $info = gettaginfo("https://wallhaven.cc/api/v1/tag/".preg_replace("/id:/", "", $q));
      $q = preg_replace("/id:[0-9]+/", $info["name"], $q);
  }
    if (preg_match("/like:([A-Za-z0-9]+)/", $q, $matches)) {
      $id = preg_replace("/like:/", "", $matches[0]);
      $wall = getwallpaper("https://wallhaven.cc/api/v1/w/$id?apikey=$apikey");
      $q = $q."<span id='triangle'></span><img src='/proxy.php?i=".$wall["thumbs"]["orig"]."'>";
    }

    return $q;
  }
?>