summaryrefslogtreecommitdiff
path: root/lib/BridgeCard.php
blob: c6f382217d4901fcdbdfdd56b060514d1b225c9f (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?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 a single bridge card on the home page of RSS-Bridge.
 *
 * This class generates the HTML content for a single bridge card for the home
 * page of RSS-Bridge.
 *
 * @todo Return error if a caller creates an object of this class.
 */
final class BridgeCard {
	/**
	 * Build a HTML document string of buttons for each of the provided formats
	 *
	 * @param array $formats A list of format names
	 * @return string The document string
	 */
	private static function buildFormatButtons($formats) {
		$buttons = '';

		foreach($formats as $name) {
			$buttons .= '<button type="submit" name="format" value="'
			. $name
			. '">'
			. $name
			. '</button>'
			. PHP_EOL;
		}

		return $buttons;
	}

	/**
	 * Get the form header for a bridge card
	 *
	 * @param string $bridgeName The bridge name
	 * @param bool $isHttps If disabled, adds a warning to the form
	 * @return string The form header
	 */
	private static function getFormHeader($bridgeName, $isHttps = false, $parameterName = '') {
		$form = <<<EOD
			<form method="GET" action="?">
				<input type="hidden" name="action" value="display" />
				<input type="hidden" name="bridge" value="{$bridgeName}" />
EOD;

		if(!empty($parameterName)) {
			$form .= <<<EOD
				<input type="hidden" name="context" value="{$parameterName}" />
EOD;
		}

		if(!$isHttps) {
			$form .= '<div class="secure-warning">Warning :
This bridge is not fetching its content through a secure connection</div>';
		}

		return $form;
	}

	/**
	 * Get the form body for a bridge
	 *
	 * @param string $bridgeName The bridge name
	 * @param array $formats A list of supported formats
	 * @param bool $isActive Indicates if a bridge is enabled or not
	 * @param bool $isHttps Indicates if a bridge uses HTTPS or not
	 * @param string $parameterName Sets the bridge context for the current form
	 * @param array $parameters The bridge parameters
	 * @return string The form body
	 */
	private static function getForm($bridgeName,
	$formats,
	$isActive = false,
	$isHttps = false,
	$parameterName = '',
	$parameters = array()) {
		$form = self::getFormHeader($bridgeName, $isHttps, $parameterName);

		if(count($parameters) > 0) {

			$form .= '<div class="parameters">';

			foreach($parameters as $id => $inputEntry) {
				if(!isset($inputEntry['exampleValue']))
					$inputEntry['exampleValue'] = '';

				if(!isset($inputEntry['defaultValue']))
					$inputEntry['defaultValue'] = '';

				$idArg = 'arg-'
					. urlencode($bridgeName)
					. '-'
					. urlencode($parameterName)
					. '-'
					. urlencode($id);

				$form .= '<label for="'
					. $idArg
					. '">'
					. filter_var($inputEntry['name'], FILTER_SANITIZE_STRING)
					. '</label>'
					. PHP_EOL;

				if(!isset($inputEntry['type']) || $inputEntry['type'] === 'text') {
					$form .= self::getTextInput($inputEntry, $idArg, $id);
				} elseif($inputEntry['type'] === 'number') {
					$form .= self::getNumberInput($inputEntry, $idArg, $id);
				} else if($inputEntry['type'] === 'list') {
					$form .= self::getListInput($inputEntry, $idArg, $id);
				} elseif($inputEntry['type'] === 'checkbox') {
					$form .= self::getCheckboxInput($inputEntry, $idArg, $id);
				}
			}

			$form .= '</div>';

		}

		if($isActive) {
			$form .= self::buildFormatButtons($formats);
		} else {
			$form .= '<span style="font-weight: bold;">Inactive</span>';
		}

		return $form . '</form>' . PHP_EOL;
	}

	/**
	 * Get input field attributes
	 *
	 * @param array $entry The current entry
	 * @return string The input field attributes
	 */
	private static function getInputAttributes($entry) {
		$retVal = '';

		if(isset($entry['required']) && $entry['required'] === true)
			$retVal .= ' required';

		if(isset($entry['pattern']))
			$retVal .= ' pattern="' . $entry['pattern'] . '"';

		if(isset($entry['title']))
			$retVal .= ' title="' . filter_var($entry['title'], FILTER_SANITIZE_STRING) . '"';

		return $retVal;
	}

	/**
	 * Get text input
	 *
	 * @param array $entry The current entry
	 * @param string $id The field ID
	 * @param string $name The field name
	 * @return string The text input field
	 */
	private static function getTextInput($entry, $id, $name) {
		return '<input '
		. self::getInputAttributes($entry)
		. ' id="'
		. $id
		. '" type="text" value="'
		. filter_var($entry['defaultValue'], FILTER_SANITIZE_STRING)
		. '" placeholder="'
		. filter_var($entry['exampleValue'], FILTER_SANITIZE_STRING)
		. '" name="'
		. $name
		. '" />'
		. PHP_EOL;
	}

	/**
	 * Get number input
	 *
	 * @param array $entry The current entry
	 * @param string $id The field ID
	 * @param string $name The field name
	 * @return string The number input field
	 */
	private static function getNumberInput($entry, $id, $name) {
		return '<input '
		. self::getInputAttributes($entry)
		. ' id="'
		. $id
		. '" type="number" value="'
		. filter_var($entry['defaultValue'], FILTER_SANITIZE_NUMBER_INT)
		. '" placeholder="'
		. filter_var($entry['exampleValue'], FILTER_SANITIZE_NUMBER_INT)
		. '" name="'
		. $name
		. '" />'
		. PHP_EOL;
	}

	/**
	 * Get list input
	 *
	 * @param array $entry The current entry
	 * @param string $id The field ID
	 * @param string $name The field name
	 * @return string The list input field
	 */
	private static function getListInput($entry, $id, $name) {
		if(isset($entry['required']) && $entry['required'] === true) {
			Debug::log('The "required" attribute is not supported for lists.');
			unset($entry['required']);
		}

		$list = '<select '
		. self::getInputAttributes($entry)
		. ' id="'
		. $id
		. '" name="'
		. $name
		. '" >';

		foreach($entry['values'] as $name => $value) {
			if(is_array($value)) {
				$list .= '<optgroup label="' . htmlentities($name) . '">';
				foreach($value as $subname => $subvalue) {
					if($entry['defaultValue'] === $subname
						|| $entry['defaultValue'] === $subvalue) {
						$list .= '<option value="'
							. $subvalue
							. '" selected>'
							. $subname
							. '</option>';
					} else {
						$list .= '<option value="'
							. $subvalue
							. '">'
							. $subname
							. '</option>';
					}
				}
				$list .= '</optgroup>';
			} else {
				if($entry['defaultValue'] === $name
					|| $entry['defaultValue'] === $value) {
					$list .= '<option value="'
						. $value
						. '" selected>'
						. $name
						. '</option>';
				} else {
					$list .= '<option value="'
						. $value
						. '">'
						. $name
						. '</option>';
				}
			}
		}

		$list .= '</select>';

		return $list;
	}

	/**
	 * Get checkbox input
	 *
	 * @param array $entry The current entry
	 * @param string $id The field ID
	 * @param string $name The field name
	 * @return string The checkbox input field
	 */
	private static function getCheckboxInput($entry, $id, $name) {
		if(isset($entry['required']) && $entry['required'] === true) {
			Debug::log('The "required" attribute is not supported for checkboxes.');
			unset($entry['required']);
		}

		return '<input '
		. self::getInputAttributes($entry)
		. ' id="'
		. $id
		. '" type="checkbox" name="'
		. $name
		. '" '
		. ($entry['defaultValue'] === 'checked' ? 'checked' : '')
		. ' />'
		. PHP_EOL;
	}

	/**
	 * Gets a single bridge card
	 *
	 * @param string $bridgeName The bridge name
	 * @param array $formats A list of formats
	 * @param bool $isActive Indicates if the bridge is active or not
	 * @return string The bridge card
	 */
	static function displayBridgeCard($bridgeName, $formats, $isActive = true){

		$bridgeFac = new \BridgeFactory();
		$bridgeFac->setWorkingDir(PATH_LIB_BRIDGES);

		$bridge = $bridgeFac->create($bridgeName);

		if($bridge == false)
			return '';

		$isHttps = strpos($bridge->getURI(), 'https') === 0;

		$uri = $bridge->getURI();
		$name = $bridge->getName();
		$icon = $bridge->getIcon();
		$description = $bridge->getDescription();
		$parameters = $bridge->getParameters();

		if(defined('PROXY_URL') && PROXY_BYBRIDGE) {
			$parameters['global']['_noproxy'] = array(
				'name' => 'Disable proxy (' . ((defined('PROXY_NAME') && PROXY_NAME) ? PROXY_NAME : PROXY_URL) . ')',
				'type' => 'checkbox'
			);
		}

		if(CUSTOM_CACHE_TIMEOUT) {
			$parameters['global']['_cache_timeout'] = array(
				'name' => 'Cache timeout in seconds',
				'type' => 'number',
				'defaultValue' => $bridge->getCacheTimeout()
			);
		}

		$card = <<<CARD
			<section id="bridge-{$bridgeName}" data-ref="{$bridgeName}">
				<h2><a href="{$uri}">{$name}</a></h2>
				<p class="description">{$description}</p>
				<input type="checkbox" class="showmore-box" id="showmore-{$bridgeName}" />
				<label class="showmore" for="showmore-{$bridgeName}">Show more</label>
CARD;

		// If we don't have any parameter for the bridge, we print a generic form to load it.
		if(count($parameters) === 0
		|| count($parameters) === 1 && array_key_exists('global', $parameters)) {

			$card .= self::getForm($bridgeName, $formats, $isActive, $isHttps);

		} else {

			foreach($parameters as $parameterName => $parameter) {
				if(!is_numeric($parameterName) && $parameterName === 'global')
					continue;

				if(array_key_exists('global', $parameters))
					$parameter = array_merge($parameter, $parameters['global']);

				if(!is_numeric($parameterName))
					$card .= '<h5>' . $parameterName . '</h5>' . PHP_EOL;

				$card .= self::getForm($bridgeName, $formats, $isActive, $isHttps, $parameterName, $parameter);
			}

		}

		$card .= '<label class="showless" for="showmore-' . $bridgeName . '">Show less</label>';
		$card .= '<p class="maintainer">' . $bridge->getMaintainer() . '</p>';
		$card .= '</section>';

		return $card;
	}
}