1
0
moha/daemons/presenceDaemon.php

109 lines
2.4 KiB
PHP
Executable File

#!/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
$presenceTemp = array();
echo "presenceDaemon is starting\n";
// init du tableau des présences à -1
foreach($macAddresses as $mac => $nom)
{
$presence[$nom] = -1;
}
while (1)
{
echo "presenceDaemon: executing nmap" . EOL;
exec($command, $result);
$presenceTemp = array();
foreach ($result as $value)
{
//echo $value;
if (str_contains($value, "MAC Address:"))
{
//echo " contient une adresse MAC" . EOL;
$presenceTemp = array_merge($presenceTemp, search($value));
}
}
var_dump($presenceTemp);
foreach($presenceTemp as $nom => $status)
{
$msg = "";
if ($presence[$nom] != (int)$status)
{
$presence[$nom] = $status;
$msg .= "nom=" . $nom . "&presence=" . $status;
send($msg);
echo "presenceDaemon:" . $nom . " est modifié dans tableau à " . $status . EOL;
}
}
$result = array();
//ésleep(1);
}
function search($string)
{
global $macAddresses;
$presenceTemp = array();
$flag = false;
//echo "searching in " . $string . EOL;
foreach ($macAddresses as $needle => $nom)
{
echo $string . " ==> presenceD:" . $nom ." => " . $needle . EOL;
if (str_contains($string, $needle))
{
echo "presenceD: found " . $needle . EOL;
$presenceTemp[$nom] = true;
$flag = true;
}
}
var_dump($presenceTemp);
return $presenceTemp;
}
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
echo _("presenceDaemon: Curl sending message -") . $msg . __FILE__ . ":" . __LINE__ . EOL;
if ($curlErr <= 5)
{
curl_exec($ch);
if (curl_errno($ch) != 0)
{
$curlErr += 1;
echo sprintf( _("presenceDaemon: Sleeping 1 minute because Curl return error %d: %s when sending notification - "), curl_errno($ch), curl_error($ch)) . __FILE__ . ":" . __LINE__ . EOL;
sleep (60);
}else
{
echo _("presenceDaemon: Curl return no error - ") . __FILE__ . ":" . __LINE__ . EOL;
$curlErr = 0;
}
}
}
?>