Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2021-03-19 08:03:43 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2021-03-19 08:03:43 +0100
commit8c5a65bab34da63270e6a4bc8a97df5cc5df7b3f (patch)
tree5aeec0e5f6ebe67dd0c07bc8911a4b4bb1daf96d
parent87bf9ce8cc959104241dbf591a29cb96ced19198 (diff)
index.php: add get_news functions to avoid code duplication
-rw-r--r--index.php65
-rw-r--r--lib/helper.php47
2 files changed, 50 insertions, 62 deletions
diff --git a/index.php b/index.php
index 7f5457a..7c3f54d 100644
--- a/index.php
+++ b/index.php
@@ -73,71 +73,14 @@ print_header("", "home");
<a href="https://bbs.archlinux32.org/extern.php?action=feed&fid=12&type=atom" title="Arch 32 News RSS Feed" class="rss-icon"><img width="16" height="16" src="/static/rss.c5ebdc5318d6.png" alt="RSS Feed" /></a>
<?php
-//apcu_clear_cache();
-$news32 = apcu_fetch('news32', $apcu_success);
-if( $apcu_success == false ) {
- $news32_reachable = apcu_fetch('news32_reachable', $apcu_success);
- if ($apcu_success == false) {
- if (site_is_reachable('https://bbs.archlinux32.org/extern.php?action=feed&fid=12&type=atom'))
- $news32_reachable = 'YES';
- else
- $news32_reachable = 'NO';
- apcu_store('news32_reachable', $news32_reachable, 300);
- }
- if ($news32_reachable == 'YES') {
- $ch = curl_init('https://bbs.archlinux32.org/extern.php?action=feed&fid=12&type=atom');
- if( $ch != null && $ch != false ) {
- curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- $news32 = curl_exec($ch);
- $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- if( $httpcode >= 200 && $httpcode < 300 ) {
- apcu_store('news32', $news32, 300);
- } else {
- $news32 = false;
- }
- } else {
- $news32 = false;
- $news32status = "HTTP return code was $httpcode";
- }
- } else {
- $news = false;
- }
-}
-if( $news32 != false ) {
- $news32 = simplexml_load_string($news32);
-}
-
-$news64 = apcu_fetch('news64', $apcu_success);
-if( $apcu_success == false ) {
- $ch = curl_init("https://archlinux.org/feeds/news/");
- if( $ch != null && $ch != false ) {
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- $news64 = curl_exec($ch);
- $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- if( $httpcode >= 200 && $httpcode < 300 ) {
- apcu_store('news64', $news64, 300);
- } else {
- $news64 = false;
- }
- } else {
- $news64 = false;
- $news64status = "HTTP return code was $httpcode";
- }
-}
-if( $news64 != false ) {
- $news64 = simplexml_load_string($news64);
-}
+$news32 = get_news('news32', 'https://bbs.archlinux32.org/extern.php?action=feed&fid=12&type=atom', 300);
+$news64 = get_news('news64', 'https://archlinux.org/feeds/news/', 300);
$news = array();
if ($news32 === false) {
print " <h4>\n";
- print " sorry, arch 32 news are currently unavailable ($news32status)\n";
+ print " sorry, arch 32 news are currently unavailable\n";
print " </h4>\n";
} else {
foreach($news32 -> {'entry'} as $entry) {
@@ -158,7 +101,7 @@ if ($news32 === false) {
}
if ($news64 === false) {
print " <h4>\n";
- print " sorry, upstream arch news are currently unavailable ($news64status)\n";
+ print " sorry, upstream arch news are currently unavailable\n";
print " </h4>\n";
} else {
foreach($news64 -> {'channel'} -> {'item'} as $entry) {
diff --git a/lib/helper.php b/lib/helper.php
index a81eedf..ad089bf 100644
--- a/lib/helper.php
+++ b/lib/helper.php
@@ -143,7 +143,7 @@ function if_unset($array, $index, $default) {
};
function site_is_reachable($url) {
- $stream_context = stream_context_create(array('timeout' => 10));
+ $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) {
@@ -156,6 +156,51 @@ function site_is_reachable($url) {
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)