1
0
moha/daemons/presenceDaemon.php

109 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2022-09-01 18:48:20 +02:00
#!/usr/bin/php
<?php
define("EOL", "\n");
$title = "PresenceDaemon";
cli_set_process_title($title);
file_put_contents("/proc/".getmypid()."/comm",$title);
declare(ticks = 1);
require "/etc/moha/liste_telephones.php";
$msg = "";
$result = array();
$presence = array();
$curlErr = 0;
$command = "sudo nmap -n -sP 192.168.1.*"; //need to configure /etc/sudo/sudoers to permit execution of nmap as superuser
2022-09-09 16:52:42 +02:00
$presenceTemp = array();
2022-09-03 21:10:38 +02:00
2022-09-01 18:48:20 +02:00
echo "presenceDaemon is starting\n";
2022-09-03 21:10:38 +02:00
// init du tableau des présences à -1
2022-09-05 13:47:41 +02:00
foreach($macAddresses as $mac => $nom)
2022-09-03 21:10:38 +02:00
{
$presence[$nom] = -1;
}
2022-09-01 18:48:20 +02:00
while (1)
{
2022-09-09 16:52:42 +02:00
echo "presenceDaemon: executing nmap" . EOL;
2022-09-01 18:48:20 +02:00
exec($command, $result);
2022-09-09 16:52:42 +02:00
$presenceTemp = array();
2022-09-01 18:48:20 +02:00
2022-09-03 21:10:38 +02:00
foreach ($result as $value)
2022-09-01 18:48:20 +02:00
{
2022-09-03 21:10:38 +02:00
//echo $value;
if (str_contains($value, "MAC Address:"))
2022-09-01 18:48:20 +02:00
{
2022-09-03 21:10:38 +02:00
//echo " contient une adresse MAC" . EOL;
2022-09-09 16:52:42 +02:00
$presenceTemp = array_merge($presenceTemp, search($value));
2022-09-01 18:48:20 +02:00
}
2022-09-03 21:10:38 +02:00
}
2022-09-09 16:52:42 +02:00
var_dump($presenceTemp);
2022-09-03 21:10:38 +02:00
foreach($presenceTemp as $nom => $status)
{
$msg = "";
if ($presence[$nom] != (int)$status)
2022-09-01 18:48:20 +02:00
{
2022-09-09 16:52:42 +02:00
$presence[$nom] = $status;
2022-09-03 21:10:38 +02:00
$msg .= "nom=" . $nom . "&presence=" . $status;
2022-09-01 18:48:20 +02:00
send($msg);
2022-09-09 16:52:42 +02:00
echo "presenceDaemon:" . $nom . " est modifié dans tableau à " . $status . EOL;
2022-09-01 18:48:20 +02:00
}
}
2022-09-05 13:47:41 +02:00
$result = array();
2022-12-05 12:23:43 +01:00
//sleep(1);
2022-09-01 18:48:20 +02:00
}
2022-09-03 21:10:38 +02:00
function search($string)
2022-09-01 18:48:20 +02:00
{
2022-09-09 16:52:42 +02:00
global $macAddresses;
$presenceTemp = array();
$flag = false;
2022-09-03 21:10:38 +02:00
//echo "searching in " . $string . EOL;
2022-09-05 13:47:41 +02:00
foreach ($macAddresses as $needle => $nom)
2022-09-01 18:48:20 +02:00
{
2022-12-05 12:23:43 +01:00
//echo $string . " ==> presenceD:" . $nom ." => " . $needle . EOL;
2022-09-01 18:48:20 +02:00
if (str_contains($string, $needle))
{
2022-12-05 12:23:43 +01:00
//echo "presenceD: found " . $needle . EOL;
2022-09-03 21:10:38 +02:00
$presenceTemp[$nom] = true;
2022-09-09 16:52:42 +02:00
$flag = true;
2022-09-01 18:48:20 +02:00
}
}
2022-12-05 12:23:43 +01:00
//var_dump($presenceTemp);
2022-09-09 16:52:42 +02:00
return $presenceTemp;
2022-09-01 18:48:20 +02:00
}
function send($msg)
{
global $curlErr;
$ch = curl_init();
$msg = "http://localhost:1025/present&" . $msg;
// set url
curl_setopt($ch, CURLOPT_URL, $msg );
// return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $result contains the output string
2022-12-05 12:23:43 +01:00
//echo _("presenceDaemon: Curl sending message -") . $msg . __FILE__ . ":" . __LINE__ . EOL;
2022-09-01 18:48:20 +02:00
if ($curlErr <= 5)
{
curl_exec($ch);
if (curl_errno($ch) != 0)
{
$curlErr += 1;
2022-09-09 16:52:42 +02:00
echo sprintf( _("presenceDaemon: Sleeping 1 minute because Curl return error %d: %s when sending notification - "), curl_errno($ch), curl_error($ch)) . __FILE__ . ":" . __LINE__ . EOL;
2022-09-01 18:48:20 +02:00
sleep (60);
}else
{
2022-09-09 16:52:42 +02:00
echo _("presenceDaemon: Curl return no error - ") . __FILE__ . ":" . __LINE__ . EOL;
2022-09-01 18:48:20 +02:00
$curlErr = 0;
}
}
}
?>