Commit aa8202aa authored by Franco Fichtner's avatar Franco Fichtner

services: start to restructure service_control_start()

Improve redirect to avoid removing the savemsg which can be useful
and start to use the service array instead of the plain name.  This
is going to help with moving all the start/restart/stop info into
the service array creation for base services as well as plugin
services.
parent 89179ee7
...@@ -2717,6 +2717,24 @@ function find_service_by_openvpn_vpnid($vpnid) ...@@ -2717,6 +2717,24 @@ function find_service_by_openvpn_vpnid($vpnid)
return array(); return array();
} }
function find_service_by_name($names)
{
if (!is_array($names)) {
$names = array($names);
}
$services = services_get();
foreach ($services as $service) {
foreach ($names as $name) {
if ($service['name'] == $name) {
return $service;
}
}
}
return array();
}
function service_name_compare($a, $b) { function service_name_compare($a, $b) {
if (strtolower($a['name']) == strtolower($b['name'])) { if (strtolower($a['name']) == strtolower($b['name'])) {
......
...@@ -29,25 +29,6 @@ ...@@ -29,25 +29,6 @@
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
function find_service_by_name($names)
{
if (!is_array($names)) {
$names = array($names);
}
$services = services_get();
foreach ($services as $service) {
foreach ($names as $name) {
if ($service['name'] == $name) {
return $service;
}
}
}
return array();
}
/* Determine automated help URL. Should output the page name and /* Determine automated help URL. Should output the page name and
parameters separately */ parameters separately */
$uri_split = ""; $uri_split = "";
......
...@@ -47,7 +47,7 @@ function openvpn_restart_by_vpnid($mode, $vpnid) ...@@ -47,7 +47,7 @@ function openvpn_restart_by_vpnid($mode, $vpnid)
} }
if (!empty($_GET['service'])) { if (!empty($_GET['service'])) {
$service_name = htmlspecialchars($_GET['service']); $service_name = $_GET['service'];
switch ($_GET['mode']) { switch ($_GET['mode']) {
case "restartservice": case "restartservice":
$savemsg = service_control_restart($service_name, $_GET); $savemsg = service_control_restart($service_name, $_GET);
...@@ -59,15 +59,38 @@ if (!empty($_GET['service'])) { ...@@ -59,15 +59,38 @@ if (!empty($_GET['service'])) {
$savemsg = service_control_stop($service_name, $_GET); $savemsg = service_control_stop($service_name, $_GET);
break; break;
} }
sleep(5); if (isset($_SERVER['HTTP_REFERER'])) {
// redirect to the previous page after performing action, removing the action parameters from request. $referer = $_SERVER['HTTP_REFERER'];
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/status_services.php'); if (strpos($referer, $_SERVER['PHP_SELF']) === false) {
header("Location: ".$referer); /* redirect only if launched from somewhere else */
exit; header('Location: '. $referer);
exit;
}
}
} }
function service_control_start($name, $extras) { function service_control_start($name, $extras)
switch($name) { {
/* XXX openvpn is handled special at the moment */
if ($name == 'openvpn') {
$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
if (($vpnmode == "server") || ($vpnmode == "client")) {
$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
$configfile = "/var/etc/openvpn/{$vpnmode}{$id}.conf";
if (file_exists($configfile)) {
openvpn_restart_by_vpnid($vpnmode, $id);
}
}
return sprintf(gettext('%s has been started.'), htmlspecialchars($name));
}
$service = find_service_by_name($name);
if (!isset($service['name'])) {
return sprintf(gettext("Could not start unknown service `%s'"), htmlspecialchars($name));
}
switch ($service['name']) {
case 'radvd': case 'radvd':
services_radvd_configure(); services_radvd_configure();
break; break;
...@@ -107,16 +130,6 @@ function service_control_start($name, $extras) { ...@@ -107,16 +130,6 @@ function service_control_start($name, $extras) {
case 'sshd': case 'sshd':
configd_run("sshd restart"); configd_run("sshd restart");
break; break;
case 'openvpn':
$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
if (($vpnmode == "server") || ($vpnmode == "client")) {
$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
$configfile = "/var/etc/openvpn/{$vpnmode}{$id}.conf";
if (file_exists($configfile)) {
openvpn_restart_by_vpnid($vpnmode, $id);
}
}
break;
case 'relayd': case 'relayd':
relayd_configure(); relayd_configure();
filter_configure(); filter_configure();
...@@ -134,10 +147,10 @@ function service_control_start($name, $extras) { ...@@ -134,10 +147,10 @@ function service_control_start($name, $extras) {
configd_run("captiveportal start"); configd_run("captiveportal start");
break; break;
default: default:
log_error(sprintf(gettext("Could not start unknown service `%s'"), $name)); return sprintf(gettext("Could not launch service `%s'"), htmlspecialchars($name));
break;
} }
return sprintf(gettext("%s has been started."),htmlspecialchars($name));
return sprintf(gettext('%s has been started.'), htmlspecialchars($name));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment