Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Failed Conditions
Pull Request — main (#1508)
by Dan
04:48
created

isUrlReachable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 0
loc 17
rs 9.8333
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Smr\Pages\Account;
4
5
use Exception;
6
use Smr\Database;
7
use Smr\Epoch;
8
use Smr\Page\AccountPageProcessor;
9
use Smr\Request;
10
use SmrAccount;
11
12
/**
13
 * Determine whether a URL is reachable based on HTTP status code class.
14
 */
15
function isUrlReachable(string $url): bool {
16
	$ch = curl_init($url);
17
	if ($ch === false) {
18
		throw new Exception('Failed to initialize curl');
19
	}
20
	curl_setopt_array($ch, [
21
		CURLOPT_HEADER => true,
22
		CURLOPT_NOBODY => true, // headers only
23
		CURLOPT_RETURNTRANSFER => true, // don't print output
24
		CURLOPT_TIMEOUT => 5, // in seconds
25
	]);
26
	curl_exec($ch);
27
	$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
28
	curl_close($ch);
29
30
	$statusClass = floor($statusCode / 100);
31
	return $statusClass == 2 || $statusClass == 3;
32
}
33
34
class AlbumEditProcessor extends AccountPageProcessor {
35
36
	public function build(SmrAccount $account): never {
37
		$location = Request::get('location');
38
		$email = Request::get('email');
39
40
		// get website (and validate it)
41
		$website = Request::get('website');
42
		if ($website != '') {
43
			// add http:// if missing
44
			if (!preg_match('=://=', $website)) {
45
				$website = 'http://' . $website;
46
			}
47
48
			// validate
49
			if (!isUrlReachable($website)) {
50
				create_error('The website you entered is invalid!');
51
			}
52
		}
53
54
		$other = Request::get('other');
55
56
		$day = Request::getInt('day');
57
		$month = Request::getInt('month');
58
		$year = Request::getInt('year');
59
60
		// check if we have an image
61
		$noPicture = true;
62
		if ($_FILES['photo']['error'] == UPLOAD_ERR_OK) {
63
			$noPicture = false;
64
			// get dimensions
65
			$size = getimagesize($_FILES['photo']['tmp_name']);
66
			if ($size === false) {
67
				create_error('Uploaded file must be an image!');
68
			}
69
70
			$allowed_types = [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG];
71
			if (!in_array($size[2], $allowed_types)) {
72
				create_error('Only gif, jpg or png-image allowed!');
73
			}
74
75
			// check if width > 500
76
			if ($size[0] > 500) {
77
				create_error('Image is wider than 500 pixels!');
78
			}
79
80
			// check if height > 500
81
			if ($size[1] > 500) {
82
				create_error('Image is higher than 500 pixels!');
83
			}
84
85
			if (!move_uploaded_file($_FILES['photo']['tmp_name'], UPLOAD . $account->getAccountID())) {
86
				create_error('Failed to upload image!');
87
			}
88
		}
89
90
		// check if we had a album entry so far
91
		$db = Database::getInstance();
92
		$dbResult = $db->read('SELECT 1 FROM album WHERE account_id = ' . $db->escapeNumber($account->getAccountID()));
93
		if ($dbResult->hasRecord()) {
94
			if (!$noPicture) {
95
				$comment = '<span class="green">*** Picture changed</span>';
96
			}
97
98
			// change album entry
99
			$db->write('UPDATE album
100
						SET location = ' . $db->escapeString($location) . ',
101
							email = ' . $db->escapeString($email) . ',
102
							website= ' . $db->escapeString($website) . ',
103
							day = ' . $db->escapeNumber($day) . ',
104
							month = ' . $db->escapeNumber($month) . ',
105
							year = ' . $db->escapeNumber($year) . ',
106
							other = ' . $db->escapeString($other) . ',
107
							last_changed = ' . $db->escapeNumber(Epoch::time()) . ',
108
							approved = \'TBC\',
109
							disabled = \'FALSE\'
110
						WHERE account_id = ' . $db->escapeNumber($account->getAccountID()));
111
		} else {
112
			// if he didn't upload a picture before
113
			// we kick him out here
114
			if ($noPicture) {
115
				create_error('What is it worth if you don\'t upload an image?');
116
			}
117
118
			$comment = '<span class="green">*** Picture added</span>';
119
120
			// add album entry
121
			$db->insert('album', [
122
				'account_id' => $db->escapeNumber($account->getAccountID()),
123
				'location' => $db->escapeString($location),
124
				'email' => $db->escapeString($email),
125
				'website' => $db->escapeString($website),
126
				'day' => $db->escapeNumber($day),
127
				'month' => $db->escapeNumber($month),
128
				'year' => $db->escapeNumber($year),
129
				'other' => $db->escapeString($other),
130
				'created' => $db->escapeNumber(Epoch::time()),
131
				'last_changed' => $db->escapeNumber(Epoch::time()),
132
				'approved' => $db->escapeString('TBC'),
133
			]);
134
		}
135
136
		if (!empty($comment)) {
137
			// check if we have comments for this album already
138
			$db->lockTable('album_has_comments');
139
140
			$dbResult = $db->read('SELECT IFNULL(MAX(comment_id)+1, 0) AS next_comment_id FROM album_has_comments WHERE album_id = ' . $db->escapeNumber($account->getAccountID()));
141
			$comment_id = $dbResult->record()->getInt('next_comment_id');
142
143
			$db->insert('album_has_comments', [
144
				'album_id' => $db->escapeNumber($account->getAccountID()),
145
				'comment_id' => $db->escapeNumber($comment_id),
146
				'time' => $db->escapeNumber(Epoch::time()),
147
				'post_id' => 0,
148
				'msg' => $db->escapeString($comment),
149
			]);
150
			$db->unlock();
151
		}
152
153
		$successMsg = 'SUCCESS: Your information has been updated!';
154
		$container = new AlbumEdit($successMsg);
155
		$container->go();
156
	}
157
158
}
159