-
Notifications
You must be signed in to change notification settings - Fork 1
/
SignalVersionUpdatesBridge.php
72 lines (58 loc) · 1.86 KB
/
SignalVersionUpdatesBridge.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
<?php
class SignalVersionUpdatesBridge extends BridgeAbstract {
const NAME = 'Signal version updates';
const URI = 'https://signal.org/';
const CACHE_TIMEOUT = 3600;
const DESCRIPTION = 'Returns version updates for Signal';
const MAINTAINER = 'VerifiedJoseph';
const PARAMETERS = array();
private $jsonUrl = 'https://updates.signal.org/android/latest.json';
public function collectData() {
$response = $this->get($this->jsonUrl);;
$data = json_decode($response['body']);
$lastModifiedDate = $response['headers']['last-modified'];
$item['title'] = $data->versionName;
$item['uid'] = $data->sha256sum;
$item['timestamp'] = $lastModifiedDate;
$item['content'] = <<<EOD
<strong>Version code</strong>
<p>{$data->versionCode}</p>
<strong>Date</strong>
<p>{$lastModifiedDate}</p>
<strong>sha256 hash</strong>
<p>{$data->sha256sum}</p>
<strong>Download</strong>
<p><a href="{$data->url}">{$data->url}</a></p>
EOD;
$this->items[] = $item;
}
private function get($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, 1);
$data = curl_exec($curl);
$response = curl_getinfo($curl);
$errorCode = curl_errno($curl);
if ($errorCode !== 0 || $response['http_code'] !== 200) {
returnClientError('Failed to fetch: ' . $url);
}
$body = substr($data, $response['header_size']);
$headers = $this->getHeaders($data, $response['header_size']);
return array(
'headers' => $headers,
'body' => $body
);
}
private function getHeaders($data, $size) {
$headers = array();
$headerText = substr($data, 0, $size);
$body = substr($data, $size);
if(preg_match_all('/([a-zA-Z-0-9]+): ?(.*)\b/', trim($headerText), $matches, PREG_SET_ORDER, 0)) {
foreach ($matches as $header) {
$headers[$header[1]] = trim($header[2]);
}
}
return $headers;
}
}