aboutsummaryrefslogtreecommitdiff
path: root/index.php
diff options
context:
space:
mode:
authorChristian Cleberg <hello@cleberg.net>2023-05-31 21:36:23 -0500
committerChristian Cleberg <hello@cleberg.net>2023-05-31 21:36:23 -0500
commit6c65601cc4781a4fac00f4ae2a7a8def24c28e72 (patch)
tree873f6a066dcd47ce42c1370017792d2fdb0cdff7 /index.php
parentcac7a38b16f0f6d08ba7176d52f475cb14036b0a (diff)
downloadhn-6c65601cc4781a4fac00f4ae2a7a8def24c28e72.tar.gz
hn-6c65601cc4781a4fac00f4ae2a7a8def24c28e72.tar.bz2
hn-6c65601cc4781a4fac00f4ae2a7a8def24c28e72.zip
formatting
Diffstat (limited to 'index.php')
-rw-r--r--index.php187
1 files changed, 94 insertions, 93 deletions
diff --git a/index.php b/index.php
index a61df1e..28a2cfe 100644
--- a/index.php
+++ b/index.php
@@ -1,115 +1,116 @@
<?php
-$full_domain = 'https://hn.cleberg.net';
-$path = ltrim($_SERVER['REQUEST_URI'], '/');
-$elements = explode('/', $path);
+global $full_domain = 'https://hn.cleberg.net',
+ $path = ltrim($_SERVER['REQUEST_URI'], '/'),
+ $elements = explode('/', $path);
if (empty($elements[0])) {
- $html_output = get_stories(
- 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
- 'Top'
- );
- echo_html(
- $GLOBALS['full_domain'] . '/top/',
+ $html_output = get_stories(
+ 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
+ 'Top'
+ );
+ echo_html(
+ $GLOBALS['full_domain'] . '/top/',
+ 'The top stories from Hacker News, proxied by hn.',
'hn',
$html_output
- );
+ );
} else {
switch (array_shift($elements)) {
case 'top':
$html_output = get_stories(
- 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
- 'Top'
- );
- echo_html(
- $GLOBALS['full_domain'] . '/top/',
- 'The top stories from Hacker News, proxied by hn.',
- 'hn',
- $html_output
- );
- break;
+ 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"',
+ 'Top'
+ );
+ echo_html(
+ $GLOBALS['full_domain'] . '/top/',
+ 'The top stories from Hacker News, proxied by hn.',
+ 'hn',
+ $html_output
+ );
+ break;
case 'best':
$html_output = get_stories(
- 'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=30&orderBy="$key"',
- 'Best'
- );
- echo_html(
- $GLOBALS['full_domain'] . '/best/',
- 'The best 30 stories from Hacker News, proxied by hn.',
- 'hn',
- $html_output
- );
- break;
+ 'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=30&orderBy="$key"',
+ 'Best'
+ );
+ echo_html(
+ $GLOBALS['full_domain'] . '/best/',
+ 'The best 30 stories from Hacker News, proxied by hn.',
+ 'hn',
+ $html_output
+ );
+ break;
case 'new':
$html_output = get_stories(
- 'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=30&orderBy="$key"',
- 'New'
- );
- echo_html(
- $GLOBALS['full_domain'] . '/new/',
- 'The newest 30 stories from Hacker News, proxied by hn.',
- 'hn',
- $html_output
- );
- break;
+ 'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=30&orderBy="$key"',
+ 'New'
+ );
+ echo_html(
+ $GLOBALS['full_domain'] . '/new/',
+ 'The newest 30 stories from Hacker News, proxied by hn.',
+ 'hn',
+ $html_output
+ );
+ break;
case 'ask':
$html_output = get_stories(
- 'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=30&orderBy="$key"',
- 'Ask'
- );
- echo_html(
- $GLOBALS['full_domain'] . '/ask/',
- 'The latest 30 asks from Hacker News, proxied by hn.',
- 'hn',
- $html_output
- );
- break;
+ 'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=30&orderBy="$key"',
+ 'Ask'
+ );
+ echo_html(
+ $GLOBALS['full_domain'] . '/ask/',
+ 'The latest 30 asks from Hacker News, proxied by hn.',
+ 'hn',
+ $html_output
+ );
+ break;
case 'show':
$html_output = get_stories(
- 'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=30&orderBy="$key"',
- 'Show'
- );
- echo_html(
- $GLOBALS['full_domain'] . '/show/',
- 'The latest 30 show stories from Hacker News, proxied by hn.',
- 'hn',
- $html_output
- );
- break;
+ 'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=30&orderBy="$key"',
+ 'Show'
+ );
+ echo_html(
+ $GLOBALS['full_domain'] . '/show/',
+ 'The latest 30 show stories from Hacker News, proxied by hn.',
+ 'hn',
+ $html_output
+ );
+ break;
case 'job':
$html_output = get_stories(
- 'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=30&orderBy="$key"',
- 'Job'
- );
- echo_html(
- $GLOBALS['full_domain'] . '/job/',
- 'The latest 30 job posts from Hacker News, proxied by hn.',
- 'hn',
- $html_output
- );
- break;
+ 'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=30&orderBy="$key"',
+ 'Job'
+ );
+ echo_html(
+ $GLOBALS['full_domain'] . '/job/',
+ 'The latest 30 job posts from Hacker News, proxied by hn.',
+ 'hn',
+ $html_output
+ );
+ break;
default:
header('HTTP/1.1 404 Not Found');
- }
+ }
}
/**
- * Extract a set of stories from Hacker News API and format in HTML
- *
- * @access public
- * @author cmc <hello@cleberg.net>
- * @param string $api_url The API endpoint to use for extraction
- * @param string $inline_title The <h1> title to use in the HTML
- * @return string $html_output The formatted HTML result of stories from the API
- */
+* Extract a set of stories from Hacker News API and format in HTML
+*
+* @access public
+* @author cmc <hello@cleberg.net>
+* @param string $api_url The API endpoint to use for extraction
+* @param string $inline_title The <h1> title to use in the HTML
+* @return string $html_output The formatted HTML result of stories from the API
+*/
function get_stories($api_url, $inline_title) {
- $response_raw = file_get_contents($api_url);
+ $response_raw = file_get_contents($api_url);
$response = json_decode($response_raw, true);
$html_output = '<h1>' . $inline_title . '</h1>';
@@ -130,23 +131,23 @@ function get_stories($api_url, $inline_title) {
}
/**
- * Send formatted HTML results to the user via a template
- *
- * @access public
- * @author cmc <hello@cleberg.net>
- * @param string $page_url Canoncial URL for HTML header
- * @param string $page_description Page description for HTML header
- * @param string $page_title Page title for HTML header
- * @param string $page_content Page content to display in <main>
- */
+* Send formatted HTML results to the user via a template
+*
+* @access public
+* @author cmc <hello@cleberg.net>
+* @param string $page_url Canoncial URL for HTML header
+* @param string $page_description Page description for HTML header
+* @param string $page_title Page title for HTML header
+* @param string $page_content Page content to display in <main>
+*/
function echo_html(string $page_url, string $page_description, string $page_title, string $page_content) {
include_once 'src/View/class-template.php';
$template = new HN\View\Template(
- $page_url,
- $page_description,
- $page_title,
- $page_content
+ $page_url,
+ $page_description,
+ $page_title,
+ $page_content
);
$template->echo_template();