summaryrefslogtreecommitdiff
path: root/lib/helper.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/helper.php')
-rw-r--r--lib/helper.php195
1 files changed, 195 insertions, 0 deletions
diff --git a/lib/helper.php b/lib/helper.php
new file mode 100644
index 0000000..8296cd9
--- /dev/null
+++ b/lib/helper.php
@@ -0,0 +1,195 @@
+<?php
+
+# do not include twice
+if (function_exists("format_time_duration"))
+ return;
+
+require_once "../init.php";
+
+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)) {
+ $memcache = new Memcache;
+ $memcache->connect('localhost', 11211) or die ('Memcached Connection Error');
+ $git_available = $memcache->get('git_available');
+ if ($git_available === false) {
+ $git_available =
+ preg_match(
+ "/ 200 OK$/",
+ get_headers("https://git.archlinux32.org/archlinux32/packages")[0]
+ );
+ $memcache->set('git_available',$git_available,0,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) {
+ if (isset($line))
+ $line = "#L" . $line;
+ else
+ $line = "";
+ if ($commit_is_hash)
+ $commit = "commit/" . $commit;
+ else
+ $commit = "branch/" . $commit;
+ switch ($type) {
+ case "tree":
+ return
+ "https://git.archlinux32.org/archlinux32/" .
+ $repository .
+ "/src/" .
+ $commit .
+ "/" .
+ $path .
+ $line;
+ case "log":
+ return
+ "https://git.archlinux32.org/archlinux32/" .
+ $repository .
+ "/commits/" .
+ $commit .
+ "/" .
+ $path .
+ $line;
+ }
+
+ } else {
+ 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://git2.archlinux32.org/Archlinux32/" .
+ $repository .
+ "/tree/" .
+ $path .
+ $commit .
+ $line;
+ case "log":
+ return
+ "https://git2.archlinux32.org/Archlinux32/" .
+ $repository .
+ "/log/" .
+ $path .
+ $commit .
+ $line;
+ }
+ };
+};
+
+function if_unset($array, $index, $default) {
+ if (isset($array[$index]))
+ return $array[$index];
+ else
+ return $default;
+};
+
+function site_is_reachable($url) {
+ $scd = stream_context_get_default();
+ stream_context_set_default(array('timeout' => 10));
+ $headers = get_headers($url);
+ stream_context_set_default($scd);
+ 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);
+}