Skip to content

Commit

Permalink
5.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lbr38 committed Nov 5, 2024
1 parent 56adfba commit ae9a499
Show file tree
Hide file tree
Showing 18 changed files with 524 additions and 449 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# motion-UI

A web responsive interface to manage <a href="https://motion-project.github.io/"><b>motion</b></a> (an open-source motion detection software) and visualize live stream from http cameras.
A web responsive interface to manage <a href="https://motion-project.github.io/"><b>motion</b></a> (an open-source motion detection software) and visualize cameras live stream.

<div align="center">
<img src="https://github.com/user-attachments/assets/870aef98-5e5c-42e0-8387-261f6981561e" width=25% align="top">
Expand Down
137 changes: 79 additions & 58 deletions www/controllers/Motion/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ public function edit(string $file, array $params)
* Edit and overwrite current params with new params
*/
foreach ($params as $param => $details) {
// Ignore if param is empty
if (empty($param)) {
continue;
}

// Ignore if param value is empty
if (empty($details['value'])) {
continue;
}

$status = $details['status'];
$value = $details['value'];

Expand Down Expand Up @@ -194,17 +204,48 @@ private function write(string $file, array $params)
{
$content = '';

foreach ($params as $param => $details) {
foreach ($params as $name => $details) {
$name = \Controllers\Common::validateData($name);
$status = $details['status'];
$value = $details['value'];

/**
* Check that parameter name is valid and does not contains invalid caracters
*/
if (\Controllers\Common::isAlphanumDash($name) === false) {
throw new Exception($name . ' parameter name contains invalid caracter(s)');
}

/**
* Case the option is 'netcam_url' or 'netcam_high_url'
*/
if ($status == 'enabled' and ($name == 'netcam_url' or $name == 'netcam_high_url')) {
/**
* Check that URL starts with http:// or https:// or rtsp://
*/
if (!preg_match('#((^https?|rtsp)://)#', $value)) {
throw new Exception('<b>' . $name . '</b> parameter value must start with http:// or https:// or rtsp://');
}

if (\Controllers\Common::isAlphanumDash($value, array('.', '/', ':', '=', '?', '&', '@')) === false) {
throw new Exception('<b>' . $name . '</b> parameter value contains invalid caracter(s)');
}
/**
* All other options
*/
} else {
if (\Controllers\Common::isAlphanumDash($value, array('.', ' ', ',', ':', '/', '%Y', '%m', '%d', '%H', '%M', '%S', '%q', '%v', '%t', '%w', '%h', '%D', '%f', '%{eventid}', '%{fps}', '(', ')', '=', '\'', '[', ']', '@')) === false) {
throw new Exception('<b>' . $name . '</b> parameter value contains invalid caracter(s)');
}
}

if ($status == 'enabled') {
$status = '';
} else {
$status = ';';
}

$content .= $status . $param . " " . $value . PHP_EOL . PHP_EOL;
$content .= $status . $name . " " . $value . PHP_EOL . PHP_EOL;
}

/**
Expand All @@ -217,78 +258,58 @@ private function write(string $file, array $params)
unset($content);
}

// TODO : fait doublon avec la fonction edit() ci-dessus
/**
* Edit motion configuration file (in /var/lib/motionui/cameras/)
*/
public function configure(string $cameraId, array $options)
public function configure(string $cameraId, array $params)
{
$filename = CAMERAS_MOTION_CONF_AVAILABLE_DIR . '/camera-' . $cameraId . '.conf';
$file = CAMERAS_MOTION_CONF_AVAILABLE_DIR . '/camera-' . $cameraId . '.conf';

if (!file_exists($filename)) {
throw new Exception('Camera configuration file does not exist: ' . $filename);
if (!file_exists($file)) {
throw new Exception('Camera configuration file does not exist: ' . $file);
}

$content = '';

foreach ($options as $option) {
/**
* Comment the parameter with a semicolon in the final file if status sent is not 'enabled'
*/
if ($option['status'] == 'enabled') {
$optionStatus = '';
} else {
$optionStatus = ';';
}
/**
* Edit and overwrite current params with new params
*/
$this->edit($file, $params);

/**
* Check that option name is valid and does not contains invalid caracters
*/
if (\Controllers\Common::isAlphanumDash($option['name']) === false) {
throw new Exception('<b>' . $option['name'] . '</b> parameter name contains invalid caracter(s)');
/**
* Restart motion service if running
*/
if ($this->motionServiceController->isRunning()) {
if (!file_exists(DATA_DIR . '/motion.restart')) {
touch(DATA_DIR . '/motion.restart');
}
}
}

/**
* Case the option is 'netcam_url' or 'netcam_high_url'
*/
if ($option['status'] == 'enabled' and ($option['name'] == 'netcam_url' or $option['name'] == 'netcam_high_url')) {
/**
* Check that URL starts with http:// or https:// or rtsp://
*/
if (!preg_match('#((^https?|rtsp)://)#', $option['value'])) {
throw new Exception('<b>' . $option['name'] . '</b> parameter value must start with http:// or https:// or rtsp://');
}

if (\Controllers\Common::isAlphanumDash($option['value'], array('.', '/', ':', '=', '?', '&', '@')) === false) {
throw new Exception('<b>' . $option['name'] . '</b> parameter value contains invalid caracter(s)');
}
/**
* All other options
*/
} else {
if (\Controllers\Common::isAlphanumDash($option['value'], array('.', ' ', ',', ':', '/', '%Y', '%m', '%d', '%H', '%M', '%S', '%q', '%v', '%t', '%w', '%h', '%D', '%f', '%{eventid}', '%{fps}', '(', ')', '=', '\'', '[', ']', '@')) === false) {
throw new Exception('<b>' . $option['name'] . '</b> parameter value contains invalid caracter(s)');
}
}
/**
* Delete parameter from motion configuration file
*/
public function deleteParameter(int $id, string $param)
{
$file = CAMERAS_MOTION_CONF_AVAILABLE_DIR . '/camera-' . $id . '.conf';

$optionName = \Controllers\Common::validateData($option['name']);
$optionValue = $option['value'];
/**
* First get current params
*/
$currentParams = $this->getConfig($file);

/**
* If there is no error then forge the parameter line with its name and value, separated by a space ' '
* Else forge the same line but leave the value empty so that the user can re-enter it
*/
$content .= $optionStatus . $optionName . " " . $optionValue . PHP_EOL . PHP_EOL;
}
/**
* Delete param from current params
*/
unset($currentParams[$param]);

/**
* Write to file
* Order params
*/
if (file_exists($filename)) {
file_put_contents($filename, trim($content));
}
ksort($currentParams);

unset($content);
/**
* Write new configuration
*/
$this->write($file, $currentParams);

/**
* Restart motion service if running
Expand Down
17 changes: 16 additions & 1 deletion www/controllers/ajax/motion.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
/**
* Configure motion
*/
if ($_POST['action'] == "configureMotion" and !empty($_POST['cameraId']) and !empty($_POST['params'])) {
if ($_POST['action'] == "configure-motion" and !empty($_POST['cameraId']) and !empty($_POST['params'])) {
$mymotionConfig = new \Controllers\Motion\Config();

try {
Expand All @@ -282,4 +282,19 @@
response(HTTP_OK, 'Configuration saved.');
}

/**
* Delete motion parameter from configuration file
*/
if ($_POST['action'] == 'delete-param-from-config' and !empty($_POST['cameraId']) and !empty($_POST['name'])) {
$mymotionConfig = new \Controllers\Motion\Config();

try {
$mymotionConfig->deleteParameter($_POST['cameraId'], $_POST['name']);
} catch (\Exception $e) {
response(HTTP_BAD_REQUEST, $e->getMessage());
}

response(HTTP_OK, 'Parameter deleted');
}

response(HTTP_BAD_REQUEST, 'Invalid action');
65 changes: 36 additions & 29 deletions www/public/assets/icons/cpu.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ae9a499

Please sign in to comment.