summaryrefslogtreecommitdiff
path: root/lib/BridgeList.php
blob: d79d72f3bd15538dfe3a4dbe2b1fd0da6c9ba957 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
 * This file is part of RSS-Bridge, a PHP project capable of generating RSS and
 * Atom feeds for websites that don't have one.
 *
 * For the full license information, please view the UNLICENSE file distributed
 * with this source code.
 *
 * @package	Core
 * @license	http://unlicense.org/ UNLICENSE
 * @link	https://github.com/rss-bridge/rss-bridge
 */

/**
 * A generator class for the home page of RSS-Bridge.
 *
 * This class generates the HTML content for displaying all bridges on the home
 * page of RSS-Bridge.
 *
 * @todo Return error if a caller creates an object of this class.
 */
final class BridgeList {
	/**
	 * Get the document head
	 *
	 * @return string The document head
	 */
	private static function getHead() {
		return <<<EOD
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta name="description" content="RSS-Bridge" />
	<title>RSS-Bridge</title>
	<link href="static/style.css" rel="stylesheet">
	<script src="static/search.js"></script>
	<script src="static/select.js"></script>
	<noscript>
		<style>
			.searchbar {
				display: none;
			}
		</style>
	</noscript>
</head>
EOD;
	}

	/**
	 * Get the document body for all bridge cards
	 *
	 * @param bool $showInactive Inactive bridges are visible on the home page if
	 * enabled.
	 * @param int $totalBridges (ref) Returns the total number of bridges.
	 * @param int $totalActiveBridges (ref) Returns the number of active bridges.
	 * @return string The document body for all bridge cards.
	 */
	private static function getBridges($showInactive, &$totalBridges, &$totalActiveBridges) {

		$body = '';
		$totalActiveBridges = 0;
		$inactiveBridges = '';

		$bridgeList = Bridge::getBridgeNames();
		$formats = Format::getFormatNames();

		$totalBridges = count($bridgeList);

		foreach($bridgeList as $bridgeName) {

			if(Bridge::isWhitelisted($bridgeName)) {

				$body .= BridgeCard::displayBridgeCard($bridgeName, $formats);
				$totalActiveBridges++;

			} elseif($showInactive) {

				// inactive bridges
				$inactiveBridges .= BridgeCard::displayBridgeCard($bridgeName, $formats, false) . PHP_EOL;

			}

		}

		$body .= $inactiveBridges;

		return $body;
	}

	/**
	 * Get the document header
	 *
	 * @return string The document header
	 */
	private static function getHeader() {
		$warning = '';

		if(Debug::isEnabled()) {
			if(!Debug::isSecure()) {
				$warning .= <<<EOD
<section class="critical-warning">Warning : Debug mode is active from any location,
 make sure only you can access RSS-Bridge.</section>
EOD;
			} else {
				$warning .= <<<EOD
<section class="warning">Warning : Debug mode is active from your IP address,
 your requests will bypass the cache.</section>
EOD;
			}
		}

		return <<<EOD
<header>
	<h1>RSS-Bridge</h1>
	<h2>Reconnecting the Web</h2>
	{$warning}
</header>
EOD;
	}

	/**
	 * Get the searchbar
	 *
	 * @return string The searchbar
	 */
	private static function getSearchbar() {
		$query = filter_input(INPUT_GET, 'q');

		return <<<EOD
<section class="searchbar">
	<h3>Search</h3>
	<input type="text" name="searchfield"
		id="searchfield" placeholder="Enter the bridge you want to search for"
		onchange="search()" onkeyup="search()" value="{$query}">
</section>
EOD;
	}

	/**
	 * Get the document footer
	 *
	 * @param int $totalBridges The total number of bridges, shown in the footer
	 * @param int $totalActiveBridges The total number of active bridges, shown
	 * in the footer.
	 * @param bool $showInactive Sets the 'Show active'/'Show inactive' text in
	 * the footer.
	 * @return string The document footer
	 */
	private static function getFooter($totalBridges, $totalActiveBridges, $showInactive) {
		$version = Configuration::getVersion();

		$email = Configuration::getConfig('admin', 'email');
		$admininfo = '';
		if (!empty($email)) {
			$admininfo = <<<EOD
<br />
<span>
   You may email the administrator of this RSS-Bridge instance
   at <a href="mailto:{$email}">{$email}</a>
</span>
EOD;
		}

		$inactive = '';

		if($totalActiveBridges !== $totalBridges) {

			if(!$showInactive) {
				$inactive = '<a href="?show_inactive=1"><button class="small">Show inactive bridges</button></a><br>';
			} else {
				$inactive = '<a href="?show_inactive=0"><button class="small">Hide inactive bridges</button></a><br>';
			}

		}

		return <<<EOD
<section class="footer">
	<a href="https://github.com/rss-bridge/rss-bridge">RSS-Bridge ~ Public Domain</a><br>
	<p class="version">{$version}</p>
	{$totalActiveBridges}/{$totalBridges} active bridges.<br>
	{$inactive}
	{$admininfo}
</section>
EOD;
	}

	/**
	 * Create the entire home page
	 *
	 * @param bool $showInactive Inactive bridges are displayed on the home page,
	 * if enabled.
	 * @return string The home page
	 */
	static function create($showInactive = true) {

		$totalBridges = 0;
		$totalActiveBridges = 0;

		return '<!DOCTYPE html><html lang="en">'
		. BridgeList::getHead()
		. '<body onload="search()">'
		. BridgeList::getHeader()
		. BridgeList::getSearchbar()
		. BridgeList::getBridges($showInactive, $totalBridges, $totalActiveBridges)
		. BridgeList::getFooter($totalBridges, $totalActiveBridges, $showInactive)
		. '</body></html>';

	}
}