summaryrefslogtreecommitdiff
path: root/lib/Exceptions.php
blob: ac452d021b3af1b439756f2cb25eb9d8f9ddc2f0 (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
<?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
 */

/**
 * Returns an URL that automatically populates a new issue on GitHub based
 * on the information provided
 *
 * @param string $title string Sets the title of the issue
 * @param string $body string Sets the body of the issue (GitHub markdown applies)
 * @param string $labels mixed (optional) Specifies labels to add to the issue
 * @param string $maintainer string (optional) Specifies the maintainer for the issue.
 * The maintainer only applies if part of the development team!
 * @return string|null A qualified URL to a new issue with populated conent or null.
 *
 * @todo This function belongs inside a class
 */
function buildGitHubIssueQuery($title, $body, $labels = null, $maintainer = null){
	if(!isset($title) || !isset($body) || empty($title) || empty($body)) {
		return null;
	}

	// Add title and body
	$uri = REPOSITORY
		. 'issues/new?title='
		. urlencode($title)
		. '&body='
		. urlencode($body);

	// Add labels
	if(!is_null($labels) && is_array($labels) && count($labels) > 0) {
		if(count($lables) === 1) {
			$uri .= '&labels=' . urlencode($labels[0]);
		} else {
			foreach($labels as $label) {
				$uri .= '&labels[]=' . urlencode($label);
			}
		}
	} elseif(!is_null($labels) && is_string($labels)) {
		$uri .= '&labels=' . urlencode($labels);
	}

	// Add maintainer
	if(!empty($maintainer)) {
		$uri .= '&assignee=' . urlencode($maintainer);
	}

	return $uri;
}

/**
 * Returns the exception message as HTML string
 *
 * @param object $e Exception The exception to show
 * @param object $bridge object The bridge object
 * @return string|null Returns the exception as HTML string or null.
 *
 * @todo This function belongs inside a class
 */
function buildBridgeException($e, $bridge){
	if(( !($e instanceof \Exception) && !($e instanceof \Error)) || !($bridge instanceof \BridgeInterface)) {
		return null;
	}

	$title = $bridge->getName() . ' failed with error ' . $e->getCode();

	// Build a GitHub compatible message
	$body = 'Error message: `'
	. $e->getMessage()
	. "`\nQuery string: `"
	. (isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '')
	. "`\nVersion: `"
	. Configuration::getVersion()
	. '`';

	$body_html = nl2br($body);
	$link = buildGitHubIssueQuery($title, $body, 'bug report', $bridge->getMaintainer());

	$header = buildHeader($e, $bridge);
	$message = <<<EOD
<strong>{$bridge->getName()}</strong> was unable to receive or process the
remote website's content!<br>
{$body_html}
EOD;
	$section = buildSection($e, $bridge, $message, $link);

	return $section;
}

/**
 * Returns the exception message as HTML string
 *
 * @param object $e Exception The exception to show
 * @param object $bridge object The bridge object
 * @return string|null Returns the exception as HTML string or null.
 *
 * @todo This function belongs inside a class
 */
function buildTransformException($e, $bridge){
	if(( !($e instanceof \Exception) && !($e instanceof \Error)) || !($bridge instanceof \BridgeInterface)) {
		return null;
	}

	$title = $bridge->getName() . ' failed with error ' . $e->getCode();

	// Build a GitHub compatible message
	$body = 'Error message: `'
	. $e->getMessage()
	. "`\nQuery string: `"
	. (isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '')
	. '`';

	$link = buildGitHubIssueQuery($title, $body, 'bug report', $bridge->getMaintainer());
	$header = buildHeader($e, $bridge);
	$message = "RSS-Bridge was unable to transform the contents returned by
<strong>{$bridge->getName()}</strong>!";
	$section = buildSection($e, $bridge, $message, $link);

	return buildPage($title, $header, $section);
}

/**
 * Builds a new HTML header with data from a exception an a bridge
 *
 * @param object $e The exception object
 * @param object $bridge The bridge object
 * @return string The HTML header
 *
 * @todo This function belongs inside a class
 */
function buildHeader($e, $bridge){
	return <<<EOD
<header>
	<h1>Error {$e->getCode()}</h1>
	<h2>{$e->getMessage()}</h2>
	<p class="status">{$bridge->getName()}</p>
</header>
EOD;
}

/**
 * Builds a new HTML section
 *
 * @param object $e The exception object
 * @param object $bridge The bridge object
 * @param string $message The message to display
 * @param string $link The link to include in the anchor
 * @return string The HTML section
 *
 * @todo This function belongs inside a class
 */
function buildSection($e, $bridge, $message, $link){
	return <<<EOD
<section>
	<p class="exception-message">{$message}</p>
	<div class="advice">
		<ul class="advice">
			<li>Press Return to check your input parameters</li>
			<li>Press F5 to retry</li>
			<li>Open a <a href="{$link}">GitHub Issue</a> if this error persists</li>
		</ul>
	</div>
	<a href="{$link}" title="After clicking this button you can review
	the issue before submitting it"><button>Open GitHub Issue</button></a>
	<p class="maintainer">{$bridge->getMaintainer()}</p>
</section>
EOD;
}

/**
 * Builds a new HTML page
 *
 * @param string $title The HTML title
 * @param string $header The HTML header
 * @param string $section The HTML section
 * @return string The HTML page
 *
 * @todo This function belongs inside a class
 */
function buildPage($title, $header, $section){
	return <<<EOD
<!DOCTYPE html>
<html lang="en">
<head>
	<title>{$title}</title>
	<link href="static/style.css" rel="stylesheet">
</head>
<body>
	{$header}
	{$section}
</body>
</html>
EOD;
}