Skip to content

Commit 9f7e15f

Browse files
committed
Re #11 - start working on an api
Also some locale negotiator bugfixes & it is now possible to serialize Service and Incident classess to JSON
1 parent 416070b commit 9f7e15f

File tree

7 files changed

+88
-8
lines changed

7 files changed

+88
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Server status page
22
![screenshot](https://status.trucksbook.eu/img/screenshot.png)
33

4-
Very simple server status page written in PHP that can run on **PHP 5.3+** - even on **shared webhosting**. Because why waste your money on another server (or host on a server that you might want to do maintenance on), when you can use cheap webhosting?
4+
Very simple server status page written in PHP that can run on **PHP 5.4+** - even on **shared webhosting**. Because why waste your money on another server (or host on a server that you might want to do maintenance on), when you can use cheap webhosting?
55

66
## How do I install this thing?
77
Simply put the files on your server and access it from your browser. There will be a simple install dialog waiting for you.

api/incidents.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
if (!file_exists("../config.php"))
4+
{
5+
header("Location: ../");
6+
}
7+
else{
8+
require_once("../config.php");
9+
require_once("../classes/constellation.php");
10+
11+
}

api/status.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
if (!file_exists("../config.php"))
4+
{
5+
header("Location: ../");
6+
}
7+
else{
8+
require_once("../config.php");
9+
require_once("../classes/constellation.php");
10+
11+
if (!isset($_GET['id']))
12+
{
13+
$array = $constellation->render_status(true, false);
14+
echo json_encode($array);
15+
}else{
16+
$query = $mysqli->prepare("SELECT name FROM services WHERE id=?");
17+
$query->bind_param("i", $_GET['id']);
18+
$query->execute();
19+
$result = $query->get_result()->fetch_assoc();
20+
if (!count($result))
21+
{
22+
die(json_encode(["error" => _("Service does not exist!")]));
23+
}
24+
25+
$sql = $mysqli->prepare("SELECT type FROM services_status INNER JOIN status ON services_status.status_id = status.id WHERE service_id = ? AND `time` <= ? AND (`end_time` >= ? OR `end_time`=0) ORDER BY `time` DESC LIMIT 1");
26+
27+
$sql->bind_param("iii", $id, $timestamp, $timestamp);
28+
$sql->execute();
29+
$tmp = $sql->get_result();
30+
if ($tmp->num_rows)
31+
{
32+
$service = new Service($_GET['id'], $result['name'], $tmp->fetch_assoc()['type']);
33+
}
34+
else{
35+
$service = new Service($_GET['id'], $result['name']);
36+
}
37+
38+
echo json_encode($service);
39+
}
40+
}

classes/constellation.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function render_incidents($future=false, $offset=0, $limit = 5, $admin =
6969
* @param boolean $admin
7070
* @return array of services
7171
*/
72-
public function render_status($admin = 0){
72+
public function render_status($admin = false, $heading = true){
7373
global $mysqli;
7474

7575
$query = $mysqli->query("SELECT id, name FROM services");
@@ -93,8 +93,10 @@ public function render_status($admin = 0){
9393
$array[] = new Service($result['id'], $result['name']);
9494
}
9595
}
96-
97-
echo Service::current_status($array);
96+
if ($heading)
97+
{
98+
echo Service::current_status($array);
99+
}
98100
}
99101
else{
100102
$array[] = new Service(0, _("No services"), -1);

classes/incident.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Class for creating and rendering an incident
44
*/
5-
class Incident
5+
class Incident implements JsonSerializable
66
{
77
private $id;
88
private $date;
@@ -183,4 +183,16 @@ public function render($admin=0){
183183
</article>
184184
<?php
185185
}
186+
187+
public function jsonSerialize() {
188+
return [
189+
"id" => $this->id,
190+
"date" => $this->date,
191+
"end_date" => $this->end_date,
192+
"text" => $this->text,
193+
"type" => $this->type,
194+
"title" => $this->title,
195+
"username" => $this->username
196+
];
197+
}
186198
}

classes/locale-negotiator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ public function negotiate($override = null){
279279
$best_match = false;
280280
//So we have also lang code as value
281281
$accepted_langs = array_flip($this->accepted_langs);
282+
283+
global $lang;
282284
foreach ($langs as $lang) {
283285
if (strlen($lang)>2){
284286
if (in_array($lang, $accepted_langs)){
@@ -292,7 +294,9 @@ public function negotiate($override = null){
292294
});
293295

294296
if (count($possible)){
295-
$best_match = $possible[0];
297+
foreach ($possible as $value) {
298+
$best_match = $value;
299+
}
296300
break;
297301
}
298302
}

classes/service.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Class for managing services
44
*/
5-
class Service
5+
class Service implements JsonSerializable
66
{
77
private $id;
88
private $name;
@@ -167,4 +167,15 @@ public function render(){
167167
</div>
168168
<?php
169169
}
170-
}
170+
171+
public function jsonSerialize() {
172+
global $statuses;
173+
return [
174+
"id" => $this->id,
175+
"name" => $this->name,
176+
"status" => $this->status,
177+
"status_string" => $statuses[$this->status]
178+
];
179+
}
180+
181+
}

0 commit comments

Comments
 (0)