| @@ -1,115 +1,116 @@ |
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | | $full_domain = 'https://hn.cleberg.net'; |
| 4 | | $path = ltrim($_SERVER['REQUEST_URI'], '/'); |
| 5 | | $elements = explode('/', $path); |
| 3 | global $full_domain = 'https://hn.cleberg.net', |
| 4 | $path = ltrim($_SERVER['REQUEST_URI'], '/'), |
| 5 | $elements = explode('/', $path); |
| 6 | 6 | |
| 7 | 7 | if (empty($elements[0])) { |
| 8 | | $html_output = get_stories( |
| 9 | | 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"', |
| 10 | | 'Top' |
| 11 | | ); |
| 12 | | echo_html( |
| 13 | | $GLOBALS['full_domain'] . '/top/', |
| 8 | $html_output = get_stories( |
| 9 | 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"', |
| 10 | 'Top' |
| 11 | ); |
| 12 | echo_html( |
| 13 | $GLOBALS['full_domain'] . '/top/', |
| 14 | 'The top stories from Hacker News, proxied by hn.', |
| 14 | 15 | 'hn', |
| 15 | 16 | $html_output |
| 16 | | ); |
| 17 | ); |
| 17 | 18 | } else { |
| 18 | 19 | switch (array_shift($elements)) { |
| 19 | 20 | case 'top': |
| 20 | 21 | $html_output = get_stories( |
| 21 | | 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"', |
| 22 | | 'Top' |
| 23 | | ); |
| 24 | | echo_html( |
| 25 | | $GLOBALS['full_domain'] . '/top/', |
| 26 | | 'The top stories from Hacker News, proxied by hn.', |
| 27 | | 'hn', |
| 28 | | $html_output |
| 29 | | ); |
| 30 | | break; |
| 22 | 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=30&orderBy="$key"', |
| 23 | 'Top' |
| 24 | ); |
| 25 | echo_html( |
| 26 | $GLOBALS['full_domain'] . '/top/', |
| 27 | 'The top stories from Hacker News, proxied by hn.', |
| 28 | 'hn', |
| 29 | $html_output |
| 30 | ); |
| 31 | break; |
| 31 | 32 | |
| 32 | 33 | case 'best': |
| 33 | 34 | $html_output = get_stories( |
| 34 | | 'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=30&orderBy="$key"', |
| 35 | | 'Best' |
| 36 | | ); |
| 37 | | echo_html( |
| 38 | | $GLOBALS['full_domain'] . '/best/', |
| 39 | | 'The best 30 stories from Hacker News, proxied by hn.', |
| 40 | | 'hn', |
| 41 | | $html_output |
| 42 | | ); |
| 43 | | break; |
| 35 | 'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=30&orderBy="$key"', |
| 36 | 'Best' |
| 37 | ); |
| 38 | echo_html( |
| 39 | $GLOBALS['full_domain'] . '/best/', |
| 40 | 'The best 30 stories from Hacker News, proxied by hn.', |
| 41 | 'hn', |
| 42 | $html_output |
| 43 | ); |
| 44 | break; |
| 44 | 45 | |
| 45 | 46 | case 'new': |
| 46 | 47 | $html_output = get_stories( |
| 47 | | 'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=30&orderBy="$key"', |
| 48 | | 'New' |
| 49 | | ); |
| 50 | | echo_html( |
| 51 | | $GLOBALS['full_domain'] . '/new/', |
| 52 | | 'The newest 30 stories from Hacker News, proxied by hn.', |
| 53 | | 'hn', |
| 54 | | $html_output |
| 55 | | ); |
| 56 | | break; |
| 48 | 'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=30&orderBy="$key"', |
| 49 | 'New' |
| 50 | ); |
| 51 | echo_html( |
| 52 | $GLOBALS['full_domain'] . '/new/', |
| 53 | 'The newest 30 stories from Hacker News, proxied by hn.', |
| 54 | 'hn', |
| 55 | $html_output |
| 56 | ); |
| 57 | break; |
| 57 | 58 | |
| 58 | 59 | case 'ask': |
| 59 | 60 | $html_output = get_stories( |
| 60 | | 'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=30&orderBy="$key"', |
| 61 | | 'Ask' |
| 62 | | ); |
| 63 | | echo_html( |
| 64 | | $GLOBALS['full_domain'] . '/ask/', |
| 65 | | 'The latest 30 asks from Hacker News, proxied by hn.', |
| 66 | | 'hn', |
| 67 | | $html_output |
| 68 | | ); |
| 69 | | break; |
| 61 | 'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=30&orderBy="$key"', |
| 62 | 'Ask' |
| 63 | ); |
| 64 | echo_html( |
| 65 | $GLOBALS['full_domain'] . '/ask/', |
| 66 | 'The latest 30 asks from Hacker News, proxied by hn.', |
| 67 | 'hn', |
| 68 | $html_output |
| 69 | ); |
| 70 | break; |
| 70 | 71 | |
| 71 | 72 | case 'show': |
| 72 | 73 | $html_output = get_stories( |
| 73 | | 'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=30&orderBy="$key"', |
| 74 | | 'Show' |
| 75 | | ); |
| 76 | | echo_html( |
| 77 | | $GLOBALS['full_domain'] . '/show/', |
| 78 | | 'The latest 30 show stories from Hacker News, proxied by hn.', |
| 79 | | 'hn', |
| 80 | | $html_output |
| 81 | | ); |
| 82 | | break; |
| 74 | 'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=30&orderBy="$key"', |
| 75 | 'Show' |
| 76 | ); |
| 77 | echo_html( |
| 78 | $GLOBALS['full_domain'] . '/show/', |
| 79 | 'The latest 30 show stories from Hacker News, proxied by hn.', |
| 80 | 'hn', |
| 81 | $html_output |
| 82 | ); |
| 83 | break; |
| 83 | 84 | |
| 84 | 85 | case 'job': |
| 85 | 86 | $html_output = get_stories( |
| 86 | | 'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=30&orderBy="$key"', |
| 87 | | 'Job' |
| 88 | | ); |
| 89 | | echo_html( |
| 90 | | $GLOBALS['full_domain'] . '/job/', |
| 91 | | 'The latest 30 job posts from Hacker News, proxied by hn.', |
| 92 | | 'hn', |
| 93 | | $html_output |
| 94 | | ); |
| 95 | | break; |
| 87 | 'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=30&orderBy="$key"', |
| 88 | 'Job' |
| 89 | ); |
| 90 | echo_html( |
| 91 | $GLOBALS['full_domain'] . '/job/', |
| 92 | 'The latest 30 job posts from Hacker News, proxied by hn.', |
| 93 | 'hn', |
| 94 | $html_output |
| 95 | ); |
| 96 | break; |
| 96 | 97 | |
| 97 | 98 | default: |
| 98 | 99 | header('HTTP/1.1 404 Not Found'); |
| 99 | | } |
| 100 | } |
| 100 | 101 | } |
| 101 | 102 | |
| 102 | 103 | /** |
| 103 | | * Extract a set of stories from Hacker News API and format in HTML |
| 104 | | * |
| 105 | | * @access public |
| 106 | | * @author cmc <hello@cleberg.net> |
| 107 | | * @param string $api_url The API endpoint to use for extraction |
| 108 | | * @param string $inline_title The <h1> title to use in the HTML |
| 109 | | * @return string $html_output The formatted HTML result of stories from the API |
| 110 | | */ |
| 104 | * Extract a set of stories from Hacker News API and format in HTML |
| 105 | * |
| 106 | * @access public |
| 107 | * @author cmc <hello@cleberg.net> |
| 108 | * @param string $api_url The API endpoint to use for extraction |
| 109 | * @param string $inline_title The <h1> title to use in the HTML |
| 110 | * @return string $html_output The formatted HTML result of stories from the API |
| 111 | */ |
| 111 | 112 | function get_stories($api_url, $inline_title) { |
| 112 | | $response_raw = file_get_contents($api_url); |
| 113 | $response_raw = file_get_contents($api_url); |
| 113 | 114 | $response = json_decode($response_raw, true); |
| 114 | 115 | |
| 115 | 116 | $html_output = '<h1>' . $inline_title . '</h1>'; |
| @@ -130,23 +131,23 @@ function get_stories($api_url, $inline_title) { |
| 130 | 131 | } |
| 131 | 132 | |
| 132 | 133 | /** |
| 133 | | * Send formatted HTML results to the user via a template |
| 134 | | * |
| 135 | | * @access public |
| 136 | | * @author cmc <hello@cleberg.net> |
| 137 | | * @param string $page_url Canoncial URL for HTML header |
| 138 | | * @param string $page_description Page description for HTML header |
| 139 | | * @param string $page_title Page title for HTML header |
| 140 | | * @param string $page_content Page content to display in <main> |
| 141 | | */ |
| 134 | * Send formatted HTML results to the user via a template |
| 135 | * |
| 136 | * @access public |
| 137 | * @author cmc <hello@cleberg.net> |
| 138 | * @param string $page_url Canoncial URL for HTML header |
| 139 | * @param string $page_description Page description for HTML header |
| 140 | * @param string $page_title Page title for HTML header |
| 141 | * @param string $page_content Page content to display in <main> |
| 142 | */ |
| 142 | 143 | function echo_html(string $page_url, string $page_description, string $page_title, string $page_content) { |
| 143 | 144 | include_once 'src/View/class-template.php'; |
| 144 | 145 | |
| 145 | 146 | $template = new HN\View\Template( |
| 146 | | $page_url, |
| 147 | | $page_description, |
| 148 | | $page_title, |
| 149 | | $page_content |
| 147 | $page_url, |
| 148 | $page_description, |
| 149 | $page_title, |
| 150 | $page_content |
| 150 | 151 | ); |
| 151 | 152 | |
| 152 | 153 | $template->echo_template(); |