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