<?php

if (isset($_SERVER['REMOTE_ADDR'])) die();
include_once(dirname(__FILE__).'/../../Application/Core.php');
$System = new Core();
$System->ShowPage = false;
$System->Run();

$Now = time();
$DbResult = $System->Database->select('NetworkConfiguration', '*, UNIX_TIMESTAMP(`LastTime`)', '(`Changed` = 1) AND (`Enabled` = 1)');
while ($Service = $DbResult->fetch_assoc())
{
  if ($Service['UNIX_TIMESTAMP(LastTime)'] < (time() - $Service['Period']))
  {
    $System->Database->update('NetworkConfiguration', '`Id`='.$Service['Id'], array('Changed' => 2));
    $ExecutionStart = time();

    $ExecuteParts = explode('|', $Service['Execute']);
    $Output = array();
    foreach ($ExecuteParts as $Command)
    {
      if ($Command != '')
      {
        exec($Command, $Output);
      }
    }
    $ExecutionTime = time() - $ExecutionStart;
    $System->Database->update('NetworkConfiguration', '`Id`='.$Service['Id'], array('Changed' => 0, 'LastTime' => 'NOW()', 'ExecutionTime' => $ExecutionTime));
    $System->Database->update('NetworkConfiguration', '`Id`='.$Service['Id'], array('Log' => implode("\n", $Output)));
  }
}
