Skip to content

Commit 33664c2

Browse files
author
Jared Hendrickson
committed
Converted system API endpoints to OOP, converting services to OOP
1 parent 3c7a934 commit 33664c2

59 files changed

Lines changed: 1207 additions & 227 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
class APIRoutingGateways extends APIBaseModel {
6+
# Create our method constructor
7+
public function __construct() {
8+
parent::__construct();
9+
$this->methods = ["GET"];
10+
$this->privileges = ["page-all", "page-system-gateways"];
11+
}
12+
13+
public function action() {
14+
$gw_array = [];
15+
if (!empty($this->config["gateways"]["gateway_item"])) {
16+
$gw_array = $this->config["gateways"]["gateway_item"];
17+
}
18+
return APIResponse\get(0, $gw_array);
19+
}
20+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
class APIServices extends APIBaseModel {
6+
# Create our method constructor
7+
public function __construct() {
8+
parent::__construct();
9+
$this->methods = ["GET"];
10+
$this->privileges = ["page-all", "page-status-services"];
11+
}
12+
13+
public function action() {
14+
$service_list = get_services(); // Stop our service
15+
// Loop through our service list and add our service status
16+
foreach ($service_list as $key => $srvc) {
17+
$s_status = get_service_status($srvc);
18+
// Check if service is started
19+
if ($s_status === true) {
20+
$service_list[$key]["status"] = "running";
21+
} elseif ($s_status === false) {
22+
$service_list[$key]["status"] = "stopped";
23+
} else {
24+
$service_list[$key]["status"] = "unknown";
25+
}
26+
// Check if user requested only one service, if so remove unmatched services
27+
if (isset($this->validated_data["name"]) and $this->validated_data["name"] !== $srvc["name"]) {
28+
unset($service_list[$key]);
29+
}
30+
}
31+
return APIResponse\get(0, $service_list);
32+
}
33+
34+
public function validate_payload() {
35+
if (isset($this->initial_data['name'])) {
36+
$this->validated_data["name"] = $this->initial_data['name'];
37+
}
38+
}
39+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
class APIServicesNtpdRestart extends APIBaseModel {
6+
# Create our method constructor
7+
public function __construct() {
8+
parent::__construct();
9+
$this->methods = ["POST"];
10+
$this->privileges = ["page-all", "page-status-services"];
11+
}
12+
13+
public function action() {
14+
service_control_restart("ntpd", []); // Start our service
15+
return APIResponse\get(0, ["ntpd" => "restarted"]);
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
class APIServicesNtpdStart extends APIBaseModel {
6+
# Create our method constructor
7+
public function __construct() {
8+
parent::__construct();
9+
$this->methods = ["POST"];
10+
$this->privileges = ["page-all", "page-status-services"];
11+
}
12+
13+
public function action() {
14+
service_control_start("ntpd", []); // Start our service
15+
return APIResponse\get(0, ["ntpd" => "started"]);
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
class APIServicesNtpdStop extends APIBaseModel {
6+
# Create our method constructor
7+
public function __construct() {
8+
parent::__construct();
9+
$this->methods = ["POST"];
10+
$this->privileges = ["page-all", "page-status-services"];
11+
}
12+
13+
public function action() {
14+
service_control_stop("ntpd", []);
15+
return APIResponse\get(0, ["ntpd" => "stopped"]);
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
class APIServicesRestart extends APIBaseModel {
6+
# Create our method constructor
7+
public function __construct() {
8+
parent::__construct();
9+
$this->methods = ["POST"];
10+
$this->privileges = ["page-all", "page-status-services"];
11+
}
12+
13+
public function action() {
14+
$services = get_services();
15+
// Loop through our service list and add our service status
16+
foreach ($services as $key => $srvc) {
17+
service_control_restart($srvc["name"], []); // Start our service
18+
$services[$key]["status"] = "restarted";
19+
}
20+
return APIResponse\get(0, $services);
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
class APIServicesSSHd extends APIBaseModel {
6+
# Create our method constructor
7+
public function __construct() {
8+
parent::__construct();
9+
$this->methods = ["GET"];
10+
$this->privileges = ["page-all", "page-system-advanced-admin"];
11+
}
12+
13+
public function action() {
14+
$ssh_array = [];
15+
if (!empty($this->config["system"]["ssh"])) {
16+
$ssh_array = $this->config["system"]["ssh"];
17+
}
18+
return APIResponse\get(0, $ssh_array);
19+
}
20+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
class APIServicesSSHdModify extends APIBaseModel {
6+
# Create our method constructor
7+
public function __construct() {
8+
parent::__construct();
9+
$this->methods = ["POST"];
10+
$this->privileges = ["page-all", "page-system-advanced-admin"];
11+
$this->change_note = "Modified sshd configuration via API";
12+
}
13+
14+
public function action() {
15+
$this->write_config();
16+
send_event("filter reload");
17+
// Check that something was changed before altering service
18+
if (!empty($this->validated_data)) {
19+
killbyname("sshd"); // Kill SSHD
20+
log_error(gettext("secure shell configuration has changed. Stopping sshd."));
21+
if ($this->config['system']['ssh']['enable']) {
22+
log_error(gettext("secure shell configuration has changed. Restarting sshd."));
23+
send_event("service restart sshd");
24+
}
25+
}
26+
return APIResponse\get(0, $this->config["system"]["ssh"]);
27+
}
28+
29+
public function validate_payload() {
30+
if (isset($this->initial_data['enable'])) {
31+
$enable = $this->initial_data['enable'];
32+
if ($enable === true) {
33+
$this->config["system"]["ssh"]["enable"] = "enabled";
34+
} elseif ($enable === false) {
35+
unset($this->config["system"]["ssh"]["enable"]);
36+
} else {
37+
$this->errors[] = APIResponse\get(2000);
38+
}
39+
}
40+
if (isset($this->initial_data["sshdkeyonly"])) {
41+
$this->validated_data["sshdkeyonly"] = $this->initial_data["sshdkeyonly"];
42+
$allowed_auth_types = array("disabled", "enabled", "both"); // Array of allowed auth types
43+
// Check if our auth type is valid
44+
if (in_array($this->validated_data["sshdkeyonly"], $allowed_auth_types)) {
45+
if ($this->validated_data["sshdkeyonly"] === "disabled") {
46+
unset($this->config["system"]["ssh"]["sshdkeyonly"]);
47+
} else {
48+
$this->config["system"]["ssh"]["sshdkeyonly"] = $this->validated_data["sshdkeyonly"];
49+
}
50+
} else {
51+
$this->errors[] = APIResponse\get(2001);
52+
}
53+
}
54+
if (isset($this->initial_data['sshdagentforwarding'])) {
55+
$this->validated_data["sshdagentforwarding"] = $this->initial_data['sshdagentforwarding'];
56+
if ($this->validated_data["sshdagentforwarding"] === true) {
57+
$this->config["system"]["ssh"]["sshdagentforwarding"] = "enabled";
58+
} elseif ($this->validated_data["sshdagentforwarding"] === false) {
59+
unset($this->config["system"]["ssh"]["sshdagentforwarding"]);
60+
} else {
61+
$this->errors[] = APIResponse\get(2002);
62+
}
63+
}
64+
if (isset($this->initial_data['port'])) {
65+
$this->validated_data["port"] = strval($this->initial_data['port']);
66+
// Convert string to array
67+
if (is_port($this->validated_data["port"])) {
68+
$this->config["system"]["ssh"]["port"] = $this->validated_data["port"];
69+
} else {
70+
$this->errors[] = APIResponse\get(2003);
71+
}
72+
}
73+
}
74+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
class APIServicesSSHdRestart extends APIBaseModel {
6+
# Create our method constructor
7+
public function __construct() {
8+
parent::__construct();
9+
$this->methods = ["POST"];
10+
$this->privileges = ["page-all", "page-status-services"];
11+
}
12+
13+
public function action() {
14+
service_control_restart("sshd", []);
15+
return APIResponse\get(0, ["sshd" => "restarted"]);
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require_once("api/framework/APIBaseModel.inc");
3+
require_once("api/framework/APIResponse.inc");
4+
5+
class APIServicesSSHdStart extends APIBaseModel {
6+
# Create our method constructor
7+
public function __construct() {
8+
parent::__construct();
9+
$this->methods = ["POST"];
10+
$this->privileges = ["page-all", "page-status-services"];
11+
}
12+
13+
public function action() {
14+
service_control_start("sshd", []); // Start our service
15+
return APIResponse\get(0, ["sshd" => "started"]);
16+
}
17+
}

0 commit comments

Comments
 (0)