<?php

if(isset($_SERVER['REMOTE_ADDR'])) die();
include('../../global.php');

$Now = time();
$DbResult = $Database->select('NetworkConfiguration', '*, UNIX_TIMESTAMP(LastTime)', '(Changed = 1) AND (Enabled = 1)');
while($Service = $DbResult->fetch_assoc())
{
  if($Service['UNIX_TIMESTAMP(LastTime)'] < (time() - $Service['Period']))
  {
    $Database->update('NetworkConfiguration', 'Id='.$Service['Id'], array('Changed' => 2));
    $ExecutionStart = time();
    //echo($Service['Caption'].'...');
    $ExecuteParts = explode('|', $Service['Execute']);
    $Output = array();
    foreach($ExecuteParts as $Command)
      if($Command != '') 
      {
        exec($Command, $Output);        
        //echo($Command."\n");
        //sleep(5);
      }
    //echo("Done\n");
    $ExecutionTime = time() - $ExecutionStart;
    $Database->update('NetworkConfiguration', 'Id='.$Service['Id'], array('Changed' => 0, 'LastTime' => 'NOW()', 'ExecutionTime' => $ExecutionTime));
    $Database->update('NetworkConfiguration', 'Id='.$Service['Id'], array('Log' => implode("\n", $Output)));
  }
}

?>
