-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUnitySite.php
74 lines (65 loc) · 2.06 KB
/
UnitySite.php
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
<?php
namespace UnityWebPortal\lib;
use phpseclib3\Crypt\PublicKeyLoader;
class UnitySite
{
public static function redirect($destination)
{
if ($_SERVER["PHP_SELF"] != $destination) {
header("Location: $destination");
die("Redirect failed, click <a href='$destination'>here</a> to continue.");
}
}
public static function removeTrailingWhitespace($arr)
{
$out = array();
foreach ($arr as $str) {
$new_string = rtrim($str);
array_push($out, $new_string);
}
return $out;
}
public static function getGithubKeys($username)
{
$url = "https://api.github.com/users/$username/keys";
$headers = array(
"User-Agent: Unity Cluster User Portal"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$keys = json_decode(curl_exec($curl), false);
curl_close($curl);
// normally returns array of objects each with a ->key attribute
// if bad URL or no such user, returns status=404 object
// if no keys, returns []
if ((!is_array($keys)) || (count($keys) == 0)) {
return [];
}
// phpcs:disable
return array_map(function($x){return $x->key;}, $keys);
// phpcs:enable
}
public static function testValidSSHKey($key_str)
{
$key_str = trim($key_str);
if ($key_str == "") {
return false;
}
// PHP warning when key_str is digits: Attempt to read property "keys" on int
if (preg_match("/^[0-9]+$/", $key_str)) {
return false;
}
// PHP warning when key_str is JSON: Undefined property: stdClass::$keys
if (!is_null(@json_decode($key_str))) {
return false;
}
try {
PublicKeyLoader::load($key_str);
return true;
} catch (\Exception $e) {
return false;
}
}
}