summaryrefslogtreecommitdiff
path: root/lib/helper.php
blob: a81eedf2a652921e7076495abb84f6c599adc133 (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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php

# do not include twice
if (function_exists("format_time_duration"))
  return;

function format_time_duration($val) {
  $val = floor($val);
  $result = "";
  $result =
    sprintf(
      "%02d",
      $val % 60
    );
  $val = floor($val / 60);
  if ($val == 0)
    return $result;
  $result =
    sprintf(
      "%02d:%s",
      $val % 60,
      $result
    );
  $val = floor($val / 60);
  if ($val == 0)
    return $result;
  $result =
    sprintf(
      "%d:%s",
      $val % 24,
      $result
    );
  $val = floor($val / 24);
  if ($val == 0)
    return $result;
  $tmp = $val % 7;
  $printed_conjunction = true;
  if ($tmp > 1)
    $result =
      sprintf(
        "%d days and %s",
        $tmp,
        $result
      );
  elseif ($tmp == 1)
    $result =
      sprintf(
        "%d day and %s",
        $tmp,
        $result
      );
  else
    $printed_conjunction = false;
  $val = floor($val / 7);
  if ($val == 0)
    return $result;
  if ($printed_conjunction)
    $result =
      sprintf(
        ", %s",
        $result
      );
  else
    $result =
      sprintf(
        " and %s",
        $result
      );
  if ($val>1)
    $result =
      sprintf(
        "%d weeks%s",
        $val,
        $result
      );
  else
    $result =
      sprintf(
        "%d week%s",
        $val,
        $result
      );
  return $result;
};

function git_url($repository,$type,$commit,$path,$line = null,$commit_is_hash = null) {
  global $git_available;
  if (!isset($git_available)) {
    $git_available = apcu_fetch('git_available', $apcu_success);
    if ($apcu_success == false) {
      $git_available =
        site_is_reachable(
          'https://git.archlinux32.org/packages'
        );
      apcu_store('git_available', $git_available, 120);
    };
    $git_available = $git_available == 1;
  }
  if (!isset($commit_is_hash))
    $commit_is_hash = preg_match("/^[0-9a-f]{40}$/",$commit)==1;
  if ($git_available)
    $host = "git.archlinux32.org";
  else
    $host = "git2.archlinux32.org";

  if (isset($line))
    $line = "#n" . $line;
  else
    $line = "";
  if ($commit_is_hash)
    $commit = "?id=" . $commit;
  else
    $commit = "?h=" . $commit;
  switch ($type) {
    case "tree":
      return
        "https://" .
        $host .
        "/" .
        $repository .
        "/tree/" .
        $path .
        $commit .
        $line;
    case "log":
      return
        "https://" .
        $host .
        "/" .
        $repository .
        "/log/" .
        $path .
        $commit .
        $line;
  }
};

function if_unset($array, $index, $default) {
  if (array_key_exists($index, $array))
    return $array[$index];
  else
    return $default;
};

function site_is_reachable($url) {
  $stream_context = stream_context_create(array('timeout' => 10));
  $headers = get_headers($url, 0, $stream_context);
  if (is_array($headers))
    foreach ($headers as $header) {
      if (!(strpos($header, 'HTTP/') === 0))
        continue;
      if (explode(' ', $header)[1] == '200')
        return true;
      return false;
    }
  return false;
}

function add_fancy_unit($value, $unit) {
  $suffixes = array("z", "y", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "Y", "Z");
  if ($value==0)
    return $value . " " . $unit;
  $exponent = max(0,min(count($suffixes)-1,round(log(abs($value))/log(1024)-1)));
  return sprintf("%.2f %s%s", $value / pow(1024,$exponent), $suffixes[8 + $exponent], $unit);
}