Changeset 813


Ignore:
Timestamp:
Mar 9, 2016, 9:16:11 PM (9 years ago)
Author:
chronos
Message:
  • Modified: Network interface state notification made registrated as callback instead of fixed method in Notify class.
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Application/Version.php

    r810 r813  
    11<?php
    22
    3 $Revision = 810; // Subversion revision
     3$Revision = 813; // Subversion revision
    44$DatabaseRevision = 808; // SQL structure revision
    5 $ReleaseTime = strtotime('2016-03-06');
     5$ReleaseTime = strtotime('2016-03-09');
  • trunk/Modules/Network/Network.php

    r795 r813  
    107107    $this->License = 'GNU/GPLv3';
    108108    $this->Description = 'Networking related tools';
    109     $this->Dependencies = array();
     109    $this->Dependencies = array('Notify');
    110110  }
    111111
     
    120120  function DoStart()
    121121  {
     122    $this->System->ModuleManager->Modules['Notify']->RegisterCheck('NetworkReachability', array($this, 'ReachabilityCheck'));
     123   
    122124    $this->System->RegisterPage('network', 'PageNetwork');
    123125    $this->System->RegisterPage(array('network', 'administration'), 'PageNetworkAdministration');
     
    593595  {
    594596  }
     597 
     598  function OnlineList($Title, $OnlineNow, $OnlinePrevious, $MinDuration)
     599  {
     600    $Time = time();
     601    $OnlineText = array('<span style="color:red;">Nedostupný</span>', '<span style="color:green;">Dostupný</span>');
     602    $Output = '';
     603    $Condition = 'WHERE (`NetworkInterface`.`Online` = '.$OnlineNow.') AND (`NetworkInterface`.`OnlineNotify` = '.$OnlinePrevious.') '.
     604      'AND (`NetworkInterface`.`LastOnline` <= "'.TimeToMysqlDateTime($Time - $MinDuration).'")';
     605    $DbResult3 = $this->Database->query('SELECT CONCAT_WS("-", `NetworkDevice`.`Name`, NULLIF(`NetworkInterface`.`Name`, "")) AS `Name`, '.
     606      '`NetworkInterface`.`Online`, `NetworkInterface`.`LastOnline` FROM `NetworkInterface` '.
     607      'LEFT JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` '.
     608      $Condition.' AND (`NetworkDevice`.`PermanentOnline`=1) AND (`NetworkDevice`.`Used`=1) AND '.
     609      '(`NetworkInterface`.`LocalIP` != "") '.
     610      'ORDER BY `Name` ASC');
     611    if($DbResult3->num_rows > 0)
     612    {
     613      $Output .= $Title.'<br/>';
     614      $Output .= '<table>'.
     615        '<tr><th align="center">Jméno</th><th align="center">Stav</th><th align="center">Čas</th><th align="center">Trvání</th></tr>'."\n";
     616      while($Item = $DbResult3->fetch_assoc())
     617      {
     618        $Duration = $Time - MysqlDateTimeToTime($Item['LastOnline']);
     619        $DurationText = sprintf('%02d', floor($Duration / 3600 % 24)).':'.
     620          sprintf('%02d', floor($Duration / 60 % 60)).':'.
     621          sprintf('%02d', floor($Duration % 60));
     622        $Days = floor($Duration / (60 * 60 * 24));
     623        if($Days > 0) $DurationText = $Days.' dnů '.$DurationText;
     624
     625        $Output .= '<tr><td>'.$Item['Name'].'</td><td>'.$OnlineText[$Item['Online']].
     626          '</td><td>'.$Item['LastOnline'].'</td><td>'.$DurationText.'</td></tr>'."\n";
     627      }
     628      $Output .= '</table><br/>'."\n";
     629    }
     630    $this->Database->query('UPDATE `NetworkInterface` SET `OnlineNotify` = `Online` '.
     631      $Condition);
     632    return $Output;
     633  }
     634
     635  function ReachabilityCheck()
     636  {
     637    $Output = '';
     638    $Output = $this->OnlineList('Nově online', 1, 0, 0);
     639    $Output .= $this->OnlineList('Nově offline', 0, 1, 5 * 60);
     640    if($Output != '') $Output .= $this->OnlineList('Stále offline', 0, 0, 0);
     641    return($Output);
     642  }
     643 
     644  function CheckPortStatus($Ip, $Port)
     645  {
     646    $Timeout = 1;
     647    if($Socket = @fsockopen($Ip, $Port, $ErrorNumber, $ErrorString, $Timeout))
     648    {
     649      fclose($Socket);
     650      return(1);
     651    } else
     652    {
     653      return(0);
     654    }
     655  }
    595656}
  • trunk/Modules/Notify/Notify.php

    r772 r813  
    11<?php
     2
     3define('CONTACT_CATEGORY_EMAIL', 4);
    24
    35class ModuleNotify extends AppModule
    46{
     7  var $Checks;
     8 
    59  function __construct($System)
    610  {
     
    1115    $this->License = 'GNU/GPL';
    1216    $this->Description = 'Send notification messages to selected users';
    13     $this->Dependencies = array('User', 'Subject');
     17    $this->Dependencies = array('User');
     18    $this->Checks = array();
    1419  }
    1520
     
    3439    ));
    3540  }
    36 
    37   function OnlineList($Title, $OnlineNow, $OnlinePrevious, $MinDuration)
     41 
     42  function RegisterCheck($Name, $Callback)
    3843  {
    39     $Time = time();
    40     $OnlineText = array('<span style="color:red;">Nedostupný</span>', '<span style="color:green;">Dostupný</span>');
    41     $Output = '';
    42     $Condition = 'WHERE (`NetworkInterface`.`Online` = '.$OnlineNow.') AND (`NetworkInterface`.`OnlineNotify` = '.$OnlinePrevious.') '.
    43       'AND (`NetworkInterface`.`LastOnline` <= "'.TimeToMysqlDateTime($Time - $MinDuration).'")';
    44     $DbResult3 = $this->Database->query('SELECT CONCAT_WS("-", `NetworkDevice`.`Name`, NULLIF(`NetworkInterface`.`Name`, "")) AS `Name`, '.
    45       '`NetworkInterface`.`Online`, `NetworkInterface`.`LastOnline` FROM `NetworkInterface` '.
    46       'LEFT JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` '.
    47       $Condition.' AND (`NetworkDevice`.`PermanentOnline`=1) AND (`NetworkDevice`.`Used`=1) AND '.
    48       '(`NetworkInterface`.`LocalIP` != "") '.
    49       'ORDER BY `Name` ASC');
    50     if($DbResult3->num_rows > 0) {
    51       $Output .= $Title.'<br/>';
    52       $Output .= '<table>'.
    53       '<tr><th align="center">Jméno</th><th align="center">Stav</th><th align="center">Čas</th><th align="center">Trvání</th></tr>'."\n";
    54       while($Item = $DbResult3->fetch_assoc())
    55       {
    56         $Duration = $Time - MysqlDateTimeToTime($Item['LastOnline']);
    57         $DurationText = sprintf('%02d', floor($Duration / 3600 % 24)).':'.
    58           sprintf('%02d', floor($Duration / 60 % 60)).':'.
    59           sprintf('%02d', floor($Duration % 60));
    60         $Days = floor($Duration / (60 * 60 * 24));
    61         if($Days > 0) $DurationText = $Days.' dnů '.$DurationText;
    62 
    63         $Output .= '<tr><td>'.$Item['Name'].'</td><td>'.$OnlineText[$Item['Online']].
    64         '</td><td>'.$Item['LastOnline'].'</td><td>'.$DurationText.'</td></tr>'."\n";
    65       }
    66       $Output .= '</table><br/>'."\n";
    67     }
    68     $this->Database->query('UPDATE `NetworkInterface` SET `OnlineNotify` = `Online` '.
    69       $Condition);
    70     return $Output;
     44    if(array_key_exists($Name, $this->Checks))
     45      throw new Exception('Check function "'.$Name.'" already registered.');
     46    $this->Checks[$Name] = array('Callback' => $Callback);
     47  }
     48 
     49  function UnregisterCheck($Name)
     50  {
     51    if(!array_key_exists($Name, $this->Checks))
     52      throw new Exception('Check function "'.$Name.'" not registered.');
     53    unset($this->Checks[$Name]);
    7154  }
    7255
     
    7457  {
    7558    $Output = '';
     59    $Content = '';
     60
     61    // Get output from checks
     62    $DbResult2 = $this->Database->query('SELECT `Id`, `Name`, `SysName` FROM `NotifyCategory`');
     63    while($Category = $DbResult2->fetch_assoc())
     64    {
     65      if(array_key_exists($Category['SysName'], $this->Checks))
     66      {
     67        $List = call_user_func($this->Checks[$Category['SysName']]['Callback']);
     68        if($List != '') {
     69          $Output .= 'Kategorie: '.$Category['Name'].":\n";
     70          $Content .= 'Kategorie: '.$Category['Name']."<br>\n";
     71          $Content .= $List;
     72        }
     73      }
     74    }
     75   
     76    // Send content to users
    7677    $DbResult = $this->Database->query('SELECT `NotifyUser`.`Id`, `NotifyUser`.`LastTime`, `User`.`Name`, `Contact`.`Value`, `Contact`.`Category` FROM `NotifyUser` '.
    7778      'LEFT JOIN `User` ON `User`.`Id` = `NotifyUser`.`User` '.
     
    8081    {
    8182      $Time = time();
    82       $Content = '';
    8383      $Output .= 'User '.$User['Name'].'<br/>';
    84       $DbResult2 = $this->Database->query('SELECT `Id`, `Name`, `SysName` FROM `NotifyCategory`');
    85       while($Category = $DbResult2->fetch_assoc())
    86       {
    87         if($Category['SysName'] == 'NetworkReachability') {
    88           $List = $this->OnlineList('Nově online', 1, 0, 0);
    89           $List .= $this->OnlineList('Nově offline', 0, 1, 5 * 60);
    90           if($List != '') $List .= $this->OnlineList('Stále offline', 0, 0, 0);
    91           if($List != '') {
    92             $Output .= 'Kategorie: '.$Category['Name'].":\n";
    93             $Content .= 'Kategorie: '.$Category['Name']."<br>\n";
    94             $Content .= $List;
    95           }
    96         }
    97       }
     84     
    9885      $this->Database->update('NotifyUser', '`Id`='.$User['Id'], array('LastTime' => TimeToMysqlDateTime($Time)));
    9986
    100       if(($User['Category'] == 4) and ($Content !='')) {
     87      if(($User['Category'] == CONTACT_CATEGORY_EMAIL) and ($Content !='')) {
    10188        $Mail = new Mail();
    10289        $Mail->Subject = 'Notification';
    10390        $Mail->AddTo($User['Value'], $User['Name']);
    104         $Mail->AddBody(strip_tags($Content), 'text/plain');
     91        //$Mail->AddBody(strip_tags($Content), 'text/plain');
    10592        $Mail->AddBody('<style>
    10693table { border-collapse: collapse; }
     
    11097        $Mail->Send();
    11198        $Output .= 'Sent email to '.$User['Value'].' ('.$User['Name'].')<br/>';
    112         $Output .= $Content;
     99        $Output .= strip_tags(str_replace("</", " </", str_replace('<br/>', "\n", $Content)));
    113100      }
    114101
     
    147134ADD CONSTRAINT `NotifyUser_ibfk_1` FOREIGN KEY (`User`) REFERENCES `User` (`Id`),
    148135ADD CONSTRAINT `NotifyUser_ibfk_2` FOREIGN KEY (`Contact`) REFERENCES `Contact` (`Id`);');
    149 
    150136  }
    151137
Note: See TracChangeset for help on using the changeset viewer.