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('http' => 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 get_news($name, $url, $ttl) { // clear cache if you have to push out new news fast, otherwise wait for $ttl seconds //apcu_clear_cache(); $news = apcu_fetch($name, $apcu_success); if( $apcu_success == false ) { $news_reachable = apcu_fetch($name."_reachable", $apcu_success); if ($apcu_success == false) { if (site_is_reachable($url)) { $news_reachable = 'YES'; } else { $news_reachable = 'NO'; } apcu_store($name."_reachable", $news_reachable, $ttl); } if ($news_reachable == 'YES') { $ch = curl_init($url); if( $ch != null && $ch != false ) { curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_VERBOSE,1); $news = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if( $httpcode >= 200 && $httpcode < 300 ) { apcu_store($name, $news, $ttl); } else { $news = false; } } else { $news = false; } } else { $news = false; } } if ($news != false) { $news = simplexml_load_string($news); } return $news; } 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); }