Repository navigation
Expand file tree
/
Copy pathapi.php
More file actions
138 lines (121 loc) · 3.25 KB
/
Copy pathapi.php
File metadata and controls
138 lines (121 loc) · 3.25 KB
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
<?php
/**
* FPP plugin API endpoints for fpp-plugin-showops-agent.
*
* Registered by FPP's /www/api/index.php addPluginEndpoints().
* Function name must match getEndpoints + plugin-dir with hyphens removed.
*/
function getEndpointsfpppluginshowopsagent()
{
return array(
array(
'method' => 'GET',
'endpoint' => 'updates',
'callback' => 'showopsAgentUpdates',
),
);
}
/** @deprecated Legacy install dir name showops-agent; kept for mid-upgrade hosts. */
function getEndpointsshowopsagent()
{
return getEndpointsfpppluginshowopsagent();
}
function showopsAgentUpdates()
{
$currentVersion = showopsAgentDetectCurrentVersion();
$latestVersion = showopsAgentResolveLatestVersion();
$response = array(
'status' => 'ok',
'repo' => 'showops-io/fpp-agent-monitor',
'currentVersion' => $currentVersion,
'latestVersion' => $latestVersion,
);
if ($currentVersion !== null && $latestVersion !== null) {
$response['updateAvailable'] = showopsAgentCompareVersions($currentVersion, $latestVersion) < 0;
}
return json($response);
}
function showopsAgentDetectCurrentVersion()
{
global $settings;
$media = isset($settings['mediaDirectory']) ? $settings['mediaDirectory'] : '/home/fpp/media';
$pluginRoot = dirname(__FILE__);
$versionPaths = array(
$pluginRoot . '/bin/VERSION',
$media . '/plugins/fpp-plugin-showops-agent/bin/VERSION',
$media . '/plugins/showops-agent/bin/VERSION',
'/opt/fpp-monitor-agent/VERSION',
);
foreach ($versionPaths as $path) {
if (!file_exists($path)) {
continue;
}
$raw = @file_get_contents($path);
if ($raw === false) {
continue;
}
$trimmed = trim($raw);
if ($trimmed !== '') {
return $trimmed;
}
}
return null;
}
function showopsAgentResolveLatestVersion()
{
$path = dirname(__FILE__) . '/AGENT_VERSION';
if (!is_readable($path)) {
return null;
}
$raw = file_get_contents($path);
if ($raw === false) {
return null;
}
$trimmed = trim($raw);
if ($trimmed === '') {
return null;
}
return $trimmed;
}
function showopsAgentVersionParts($version)
{
if (!is_string($version)) {
return array();
}
$clean = preg_replace('/^[vV]/', '', trim($version));
if ($clean === '') {
return array();
}
$segments = preg_split('/[^0-9]+/', $clean);
$parts = array();
if (!is_array($segments)) {
return $parts;
}
foreach ($segments as $segment) {
if ($segment === '') {
continue;
}
$parts[] = intval($segment, 10);
}
return $parts;
}
/**
* Returns -1 if $a < $b, 0 if equal, 1 if $a > $b.
*/
function showopsAgentCompareVersions($a, $b)
{
$left = showopsAgentVersionParts($a);
$right = showopsAgentVersionParts($b);
$len = max(count($left), count($right), 3);
for ($i = 0; $i < $len; $i++) {
$lv = isset($left[$i]) ? $left[$i] : 0;
$rv = isset($right[$i]) ? $right[$i] : 0;
if ($lv > $rv) {
return 1;
}
if ($lv < $rv) {
return -1;
}
}
return 0;
}