Ignore:
Timestamp:
Feb 22, 2015, 11:20:50 PM (9 years ago)
Author:
chronos
Message:
  • Modified: Tabs converted to spaces.
  • Modified: Remove spaces from end of lines.
  • Added: Code format script.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/Log/Log.php

    r808 r816  
    33class ModuleLog extends AppModule
    44{
    5         var $Excludes;
    6        
     5  var $Excludes;
     6
    77  function __construct($System)
    88  {
     
    1414    $this->Description = 'Log various application events';
    1515    $this->Dependencies = array('Error', 'News');
    16    
     16
    1717    $this->Excludes = array();
    1818  }
    19  
     19
    2020  function Start()
    2121  {
    22         $this->System->RegisterPage('log.php', 'PageLog');
    23         $this->System->ModuleManager->Modules['Error']->OnError[] = array($this, 'DoAddItem');
    24         $this->System->ModuleManager->Modules['News']->RegisterRSS(array('Title' => T('Logs'),
    25           'Channel' => 'log', 'Callback' => array('PageLog', 'ShowRSS'), 'Permission' => LICENCE_ADMIN));
    26   } 
    27  
     22    $this->System->RegisterPage('log.php', 'PageLog');
     23    $this->System->ModuleManager->Modules['Error']->OnError[] = array($this, 'DoAddItem');
     24    $this->System->ModuleManager->Modules['News']->RegisterRSS(array('Title' => T('Logs'),
     25      'Channel' => 'log', 'Callback' => array('PageLog', 'ShowRSS'), 'Permission' => LICENCE_ADMIN));
     26  }
     27
    2828  function DoAddItem($Message)
    2929  {
    30         $this->WriteLog($Message, LOG_TYPE_ERROR);
     30    $this->WriteLog($Message, LOG_TYPE_ERROR);
    3131  }
    3232
    3333  function WriteLog($Text, $Type)
    3434  {
    35         if(isset($this->System->User) and !is_null($this->System->User->Id))
    36           $UserId = $this->System->User->Id;
    37           else $UserId = 'NULL';
    38         $Query = 'INSERT INTO `Log` ( `User` , `Type` , `Text` , `Date` , `IP`, `URL` ) '.
    39           'VALUES ('.$UserId.', '.$Type.', "'.addslashes($Text).'", NOW(), "'.
    40           GetRemoteAddress().'", "'.GetRequestURI().'")';
    41         $this->System->Database->query($Query);
     35    if(isset($this->System->User) and !is_null($this->System->User->Id))
     36      $UserId = $this->System->User->Id;
     37      else $UserId = 'NULL';
     38    $Query = 'INSERT INTO `Log` ( `User` , `Type` , `Text` , `Date` , `IP`, `URL` ) '.
     39      'VALUES ('.$UserId.', '.$Type.', "'.addslashes($Text).'", NOW(), "'.
     40      GetRemoteAddress().'", "'.GetRequestURI().'")';
     41    $this->System->Database->query($Query);
    4242  }
    4343}
     
    5555define('LOG_TYPE_PAGE_NOT_FOUND', 15);
    5656
    57 // TODO: Change global function to module class local method 
     57// TODO: Change global function to module class local method
    5858function WriteLog($Text, $Type)
    5959{
    60         global $System, $User;
    61        
    62         if(isset($User) and !is_null($User->Id)) $UserId = $User->Id;
    63           else $UserId = 'NULL';
    64         $Query = 'INSERT INTO `Log` ( `User` , `Type` , `Text` , `Date` , `IP`, `URL` ) '.
    65           'VALUES ('.$UserId.', '.$Type.', "'.addslashes($Text).'", NOW(), "'.
     60  global $System, $User;
     61
     62  if(isset($User) and !is_null($User->Id)) $UserId = $User->Id;
     63    else $UserId = 'NULL';
     64  $Query = 'INSERT INTO `Log` ( `User` , `Type` , `Text` , `Date` , `IP`, `URL` ) '.
     65    'VALUES ('.$UserId.', '.$Type.', "'.addslashes($Text).'", NOW(), "'.
    6666      GetRemoteAddress().'", "'.GetRequestURI().'")';
    67         $System->Database->query($Query);
     67  $System->Database->query($Query);
    6868}
    6969
     
    7272  function ShowRSS()
    7373  {
    74         $this->RawPage = true;
    75         $Output = '';
     74    $this->RawPage = true;
     75    $Output = '';
    7676    $Items = array();
    77     if(array_key_exists('type', $_GET)) $Where = ' WHERE `Type` = "'.($_GET['type'] * 1).'"'; 
     77    if(array_key_exists('type', $_GET)) $Where = ' WHERE `Type` = "'.($_GET['type'] * 1).'"';
    7878      else $Where = '';
    7979    $sql = 'SELECT *, UNIX_TIMESTAMP(`Date`) AS `TimeCreate`, (SELECT `User`.`Name` FROM `User` WHERE `User`.`ID` = `Log`.`User`) AS `UserName`, `Date` FROM `Log`'.
    8080      $Where.' ORDER BY `Date` DESC LIMIT 100';
    8181    $DbResult = $this->System->Database->query($sql);
    82     while($Line = $DbResult->fetch_assoc()) 
     82    while($Line = $DbResult->fetch_assoc())
    8383    {
    8484      $DbResult2 = $this->System->Database->query('SELECT * FROM `LogType` WHERE `Id`='.$Line['Type']);
    8585      $LogType = $DbResult2->fetch_assoc();
    86      
     86
    8787      if($Line['Type'] == LOG_TYPE_ERROR) $Line['Text'] = htmlspecialchars($Line['Text']);
    8888      $Line['Text'] = str_replace("\n", '<br>', $Line['Text']);
    89    
     89
    9090      $Items[] = array
    9191      (
     
    9696        'Time' => $Line['TimeCreate'],
    9797      );
    98     } 
     98    }
    9999
    100100    $Output .= GenerateRSS(array
     
    108108    return($Output);
    109109  }
    110  
     110
    111111  function Show()
    112112  {
    113                 if(array_key_exists('a', $_POST)) $Action = $_POST['a'];
    114                   else if(array_key_exists('a', $_GET)) $Action = $_GET['a'];
    115                   else $Action = '';           
    116                 if($Action == 'delerrlog') $Output = $this->DeleteErrorLog();
    117                 else $Output = $this->ShowList();
    118                 return($Output);
    119   }
    120  
     113    if(array_key_exists('a', $_POST)) $Action = $_POST['a'];
     114      else if(array_key_exists('a', $_GET)) $Action = $_GET['a'];
     115      else $Action = '';
     116    if($Action == 'delerrlog') $Output = $this->DeleteErrorLog();
     117    else $Output = $this->ShowList();
     118    return($Output);
     119  }
     120
    121121  function ShowList()
    122122  {
    123123    global $TranslationTree;
    124    
     124
    125125    $this->Title = T('System log');
    126126    $Output = '';
    127     if(array_key_exists('type', $_GET)) $_SESSION['type'] = $_GET['type'] * 1; 
     127    if(array_key_exists('type', $_GET)) $_SESSION['type'] = $_GET['type'] * 1;
    128128    else if(!array_key_exists('type', $_SESSION)) $_SESSION['type'] = '';
    129129
    130130    if(array_key_exists('group', $_GET)) $_SESSION['group'] = $_GET['group'];
    131131
    132     if($_SESSION['type'] != '') $WhereType = ' (`Type`='.$_SESSION['type'].')'; 
     132    if($_SESSION['type'] != '') $WhereType = ' (`Type`='.$_SESSION['type'].')';
    133133      else $WhereType = '1=1';
    134    
     134
    135135    $RSSChannels = array(
    136         array('Title' => 'Záznamy změn', 'Channel' => 'log&amp;type='.$_SESSION['type'])
     136      array('Title' => 'Záznamy změn', 'Channel' => 'log&amp;type='.$_SESSION['type'])
    137137    );
    138  
     138
    139139    // Show category filter
    140140    if($this->System->User->Licence(LICENCE_MODERATOR))
     
    154154     $Output .= '<br /><br />';
    155155
    156     if(array_key_exists('type', $_SESSION)) $Where = ' WHERE '.$WhereType; 
     156    if(array_key_exists('type', $_SESSION)) $Where = ' WHERE '.$WhereType;
    157157    else
    158158    {
    159159      if(array_key_exists('group', $_SESSION)) $Where = ' WHERE `Text` LIKE "%'.$TranslationTree[$_SESSION['group']]['Name'].'%"';
    160160        else $Where = '';
    161     } 
     161    }
    162162    //if(($Where != '') and (array_key_exists('group', $_SESSION))) $Where .= ' AND text LIKE "%'.$TranslationTree[$_SESSION['group']]['Name'].'%"';
    163163
    164164    $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `Log` '.$Where);
    165165    $DbRow = $DbResult->fetch_row();
    166     $PageList = GetPageList($DbRow[0]);   
    167  
     166    $PageList = GetPageList($DbRow[0]);
     167
    168168    $Output .= $PageList['Output'];
    169  
     169
    170170    $TableColumns = array(
    171171      array('Name' => 'Date', 'Title' => 'Čas'),
    172172    );
    173     if($_SESSION['type'] == '') $TableColumns[] = 
     173    if($_SESSION['type'] == '') $TableColumns[] =
    174174      array('Name' => 'LogName', 'Title' => 'Typ');
    175     $TableColumns = array_merge($TableColumns, array( 
    176           array('Name' => 'Text', 'Title' => 'Text'),
    177       array('Name' => 'UserName', 'Title' => 'Uživatel'), 
    178       array('Name' => 'IP', 'Title' => 'Adresa'), 
     175    $TableColumns = array_merge($TableColumns, array(
     176      array('Name' => 'Text', 'Title' => 'Text'),
     177      array('Name' => 'UserName', 'Title' => 'Uživatel'),
     178      array('Name' => 'IP', 'Title' => 'Adresa'),
    179179      array('Name' => 'URL', 'Title' => 'URL'),
    180180    ));
     
    182182    $Output .= '<table width="98%" class="BaseTable">'.
    183183      $Order['Output'];
    184  
     184
    185185    $sql = 'SELECT *, `LogType`.`Color` AS `LogColor`, `LogType`.`Name` AS `LogName`, '.
    186186      '(SELECT `User`.`Name` FROM `User` WHERE `User`.`ID` = `Log`.`User`) AS `UserName` FROM `Log` LEFT JOIN `LogType` ON `LogType`.`Id`=`Log`.`Type` '.$Where.$Order['SQL'].$PageList['SQLLimit'];
    187187    $DbResult = $this->System->Database->query($sql);
    188     while($Line = $DbResult->fetch_assoc()) 
    189     {
    190           if($Line['Type'] == LOG_TYPE_ERROR) $Line['Text'] = htmlspecialchars($Line['Text']);
     188    while($Line = $DbResult->fetch_assoc())
     189    {
     190      if($Line['Type'] == LOG_TYPE_ERROR) $Line['Text'] = htmlspecialchars($Line['Text']);
    191191      $Line['Text'] = str_replace("\n", '<br>', $Line['Text']);
    192           $Output .= '<tr><td>'.$Line['Date'].'</td>';
    193           if($_SESSION['type'] == '') $Output .= '<td>'.$Line['LogName'].'</td>';
     192      $Output .= '<tr><td>'.$Line['Date'].'</td>';
     193      if($_SESSION['type'] == '') $Output .= '<td>'.$Line['LogName'].'</td>';
    194194      $Output .= '<td><span style="color: '.$Line['LogColor'].'">'.$Line['Text'].'</span></td>'.
    195195      '<td><a href="'.$this->System->Link('/user.php?user='.$Line['User']).'">'.$Line['UserName'].'</a></td>'.
    196196      '<td>'.$Line['IP'].'</td>'.
    197           '<td>'.$Line['URL'].'</td></tr>';
     197      '<td>'.$Line['URL'].'</td></tr>';
    198198    }
    199199    $Output .= '</table>'.
     
    203203        $Output .= '<div>Vymazat: <a href="'.$this->System->Link('/log.php?a=delerrlog&amp;type='.LOG_TYPE_ERROR).'">Chybové záznamy</a> '.
    204204          '<a href="'.$this->System->Link('/log.php?a=delerrlog&amp;type='.LOG_TYPE_PAGE_NOT_FOUND).'">Neznámé stránky</a></div>';
    205       }     
     205      }
    206206    } else $Output .= ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
    207  
    208     return($Output); 
    209   }
    210  
     207
     208    return($Output);
     209  }
     210
    211211  function DeleteErrorLog()
    212212  {
    213         if($this->System->User->Licence(LICENCE_ADMIN) and
    214         (($_GET['type'] == LOG_TYPE_ERROR) or ($_GET['type'] == LOG_TYPE_PAGE_NOT_FOUND)))
    215         {
    216                 $DbResult = $this->System->Database->select('LogType', '*', 'Id='.$_GET['type']);
    217                 $LogType = $DbResult->fetch_assoc();
    218           $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `Log` WHERE `Type`='.$_GET['type']);
    219           $DbRow = $DbResult->fetch_row();
    220           $this->System->Database->query('DELETE FROM `Log` WHERE `Type`='.$_GET['type']);
    221           $this->System->ModuleManager->Modules['Log']->WriteLog('Vymazáno záznamů z '.$LogType['Description'].'.', LOG_TYPE_ADMINISTRATION);
    222           $Output = ShowMessage('Smazáno všech '.$DbRow[0].' záznamů z '.$LogType['Description'].'.');
    223           $Output .= $this->ShowList();
    224           return($Output);
    225         } else $Output .= ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
     213    if($this->System->User->Licence(LICENCE_ADMIN) and
     214    (($_GET['type'] == LOG_TYPE_ERROR) or ($_GET['type'] == LOG_TYPE_PAGE_NOT_FOUND)))
     215    {
     216      $DbResult = $this->System->Database->select('LogType', '*', 'Id='.$_GET['type']);
     217      $LogType = $DbResult->fetch_assoc();
     218      $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `Log` WHERE `Type`='.$_GET['type']);
     219      $DbRow = $DbResult->fetch_row();
     220      $this->System->Database->query('DELETE FROM `Log` WHERE `Type`='.$_GET['type']);
     221      $this->System->ModuleManager->Modules['Log']->WriteLog('Vymazáno záznamů z '.$LogType['Description'].'.', LOG_TYPE_ADMINISTRATION);
     222      $Output = ShowMessage('Smazáno všech '.$DbRow[0].' záznamů z '.$LogType['Description'].'.');
     223      $Output .= $this->ShowList();
     224      return($Output);
     225    } else $Output .= ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
    226226  }
    227227}
Note: See TracChangeset for help on using the changeset viewer.