diogenes.logger-view.inc.php

00001 <?php
00002 /*
00003  * Copyright (C) 2003-2004 Polytechnique.org
00004  * http://opensource.polytechnique.org/
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 
00029 class DiogenesLoggerView {
00032   var $dbh;
00033 
00036   function DiogenesLoggerView()
00037   {
00038     global $globals;
00039     if (!is_object($globals->db))
00040       die("\$globals->db is not an object!");
00041     $this->dbh =& $globals->db;
00042   }
00043 
00044 
00053   function _getAuths()
00054   {
00055     global $globals;
00056 
00057     // give a 'no filter' option
00058     $auths[0] = __("all");
00059 
00060     $res = $this->dbh->query("describe {$globals->table_log_sessions} auth");
00061     list(,$type) = mysql_fetch_row($res);
00062     mysql_free_result($res);
00063     $types = preg_split("/(enum\\('|','|'\\))/", $type, -1, PREG_SPLIT_NO_EMPTY);
00064     foreach ($types as $type)
00065       $auths[$type] = isset($globals->tlabel[$type]) ? $globals->tlabel[$type] : $type;
00066     return $auths;
00067   }
00068 
00069 
00079   function _getDays($year,$month)
00080   {
00081     global $globals;
00082 
00083     // give a 'no filter' option
00084     $days[0] = __("all");
00085 
00086     if ($year && $month) {
00087       $day_max = Array(-1, 31,checkdate(2,29,$year) ? 29 : 28 ,31,30,31,30,31,31,30,31,30,31);
00088       $res = $this->dbh->query("SELECT YEAR (MAX(start)), YEAR (MIN(start)),
00089                                        MONTH(MAX(start)), MONTH(MIN(start)),
00090                                        DAYOFMONTH(MAX(start)),
00091                                        DAYOFMONTH(MIN(start))
00092                                 FROM {$globals->table_log_sessions}");
00093       list($ymax,$ymin,$mmax,$mmin,$dmax,$dmin) = mysql_fetch_row($res);
00094       mysql_free_result($res);
00095 
00096       if (($year < $ymin) || ($year == $ymin && $month < $mmin))
00097       {
00098         return array();
00099       }
00100       
00101       if (($year > $ymax) || ($year == $ymax && $month > $mmax))
00102       {
00103         return array();
00104       }
00105       
00106       $min = ($year==$ymin && $month==$mmin) ? intval($dmin) : 1;
00107       $max = ($year==$ymax && $month==$mmax) ? intval($dmax) : $day_max[$month];
00108 
00109       for($i = $min; $i<=$max; $i++)
00110         $days[$i] = $i;
00111     }
00112     return $days;
00113   }
00114 
00115 
00124   function _getMonths($year)
00125   {
00126     global $globals;
00127 
00128     // give a 'no filter' option
00129     $months[0] = __("all");
00130 
00131     if ($year) {
00132       $res = $this->dbh->query("SELECT YEAR (MAX(start)), YEAR (MIN(start)),
00133                                        MONTH(MAX(start)), MONTH(MIN(start))
00134                                 FROM {$globals->table_log_sessions}");
00135       list($ymax,$ymin,$mmax,$mmin) = mysql_fetch_row($res);
00136       mysql_free_result($res);
00137 
00138       if (($year < $ymin) || ($year > $ymax))
00139       {
00140         return array();
00141       }
00142 
00143       $min = $year == $ymin ? intval($mmin) : 1;
00144       $max = $year == $ymax ? intval($mmax) : 12;
00145 
00146       for($i = $min; $i<=$max; $i++)
00147         $months[$i] = $i;
00148     }
00149     return $months;
00150   }
00151 
00152 
00160   function _getUsername($auth, $uid) {
00161     global $globals;
00162     static $cache;
00163 
00164     if (!isset($cache[$auth][$uid]))
00165       $cache[$auth][$uid] = call_user_func(array($globals->session,'getUsername'),$auth,$uid);
00166       
00167     return $cache[$auth][$uid];
00168   }
00169 
00170 
00177   function _getYears()
00178   {
00179     global $globals;
00180 
00181     // give a 'no filter' option
00182     $years[0] = __("all");
00183 
00184     // retrieve available years
00185     $res = $this->dbh->query("select YEAR(MAX(start)),YEAR(MIN(start)) FROM {$globals->table_log_sessions}");
00186     list($max,$min) = mysql_fetch_row($res);
00187     mysql_free_result($res);
00188 
00189     for($i = intval($min); $i<=$max; $i++)
00190         $years[$i] = $i;
00191     return $years;
00192   }
00193 
00194 
00207   function _makeWhere($year,$month,$day,$auth,$uid)
00208   {
00209     global $globals;
00210 
00211     // start constructing the "where" clause
00212     $where = array();
00213 
00214     if ($auth)
00215       array_push($where, "auth='$auth'");
00216 
00217     if ($uid)
00218       array_push($where, "uid='$uid'");
00219 
00220     // we were given at least a year
00221     if ($year) {
00222       if ($day) {
00223         $dmin = mktime(0, 0, 0, $month, $day, $year);
00224         $dmax = mktime(0, 0, 0, $month, $day+1,$year);
00225       } elseif ($month) {
00226         $dmin = mktime(0, 0, 0, $month, 1, $year);
00227         $dmax = mktime(0, 0, 0, $month+1, 1, $year);
00228       } else {
00229         $dmin = mktime(0, 0, 0, 1, 1, $year);
00230         $dmax = mktime(0, 0, 0, 1, 1, $year+1);
00231       }
00232       $where[] = "start >= " . date("Ymd000000",$dmin);
00233       $where[] = "start < " . date("Ymd000000", $dmax);
00234     }
00235 
00236     if (!empty($where))
00237       return ' WHERE ' . implode($where," AND ");
00238     else
00239       return '';
00240     // WE know it's totally reversed, so better use array_reverse than a SORT BY start DESC
00241   }
00242 
00243 
00250   function run(&$page,$outputvar='',$template='')
00251   {
00252     global $globals;
00253 
00254     if (isset($_REQUEST['logsess'])) {
00255 
00256       // we are viewing a session
00257       $res=$this->dbh->query("SELECT  host,ip,browser,auth,uid,sauth,suid
00258                                 FROM  {$globals->table_log_sessions}
00259                                WHERE  id =".$_REQUEST['logsess']);
00260       $sarr = mysql_fetch_assoc($res);
00261       
00262       $sarr['username'] = $this->_getUsername($sarr['auth'],$sarr['uid']);
00263       if ($sarr['suid'])
00264         $sarr['suer'] = $this->_getUsername($sarr['sauth'],$sarr['suid']);
00265       $page->assign('session',$sarr);
00266 
00267       $res=$this->dbh->query("SELECT  a.text,e.data,UNIX_TIMESTAMP(e.stamp) AS stamp
00268                                FROM  {$globals->table_log_events}  AS e
00269                            LEFT JOIN  {$globals->table_log_actions} AS a ON e.action=a.id
00270                                WHERE  e.session='{$_REQUEST['logsess']}'");
00271       while ($myarr = mysql_fetch_assoc($res))
00272         $page->append('events',$myarr);
00273       mysql_free_result($res);
00274 
00275     } else {
00276 
00277       // we are browsing the available sessions
00278       $logauth = isset($_REQUEST['logauth']) ? $_REQUEST['logauth'] : '';
00279       $loguser = isset($_REQUEST['loguser']) ? $_REQUEST['loguser'] : '';
00280       $loguid = ($logauth && $loguser)? call_user_func(array($globals->session,'getUserId'),$logauth,$_REQUEST['loguser']) : '';
00281       if (!$loguid) $loguser = '';
00282 
00283       if ($loguid) {
00284         $year = isset($_REQUEST['year']) ? $_REQUEST['year'] : 0;
00285         $month = isset($_REQUEST['month']) ? $_REQUEST['month'] : 0;
00286         $day = isset($_REQUEST['day']) ? $_REQUEST['day'] : 0;
00287       } else {
00288         $year = isset($_REQUEST['year']) ? $_REQUEST['year'] : date("Y");
00289         $month = isset($_REQUEST['month']) ? $_REQUEST['month'] : date("m");
00290         $day = isset($_REQUEST['day']) ? $_REQUEST['day'] : date("d");
00291       }
00292 
00293       if (!$year)  $month = 0;
00294       if (!$month) $day = 0;
00295 
00296     // smarty assignments
00297       // retrieve available years
00298       $page->assign('years',$this->_getYears());
00299       $page->assign('year',$year);
00300 
00301       // retrieve available months for the current year
00302       $page->assign('months',$this->_getMonths($year));
00303       $page->assign('month',$month);
00304 
00305       // retrieve available days for the current year and month
00306       $page->assign('days',$this->_getDays($year,$month));
00307       $page->assign('day',$day);
00308 
00309       // retrieve available auths
00310       $auths = $this->_getAuths();
00311       $page->assign('auths',$auths);
00312 
00313       $page->assign('logauth',$logauth);
00314       $page->assign('loguser',$loguser);
00315     // smarty assignments
00316 
00317       if($loguid || $year) {
00318 
00319         // get the requested sessions
00320         $where  = $this->_makeWhere($year,$month,$day,$logauth,$loguid);
00321         $select = "SELECT id,UNIX_TIMESTAMP(start) as start,auth,uid
00322                    FROM {$globals->table_log_sessions} AS s
00323                    $where
00324                   ORDER BY start DESC";
00325         $res = $globals->db->query($select);
00326        
00327         $sessions = array();
00328         while ( $mysess = mysql_fetch_assoc($res) ) {
00329           $mysess['username'] = $this->_getUsername($mysess['auth'],$mysess['uid']);
00330           // pretty label for auth method
00331           $mysess['lauth'] = $auths[$mysess['auth']];
00332           // summary of events
00333           $mysess['events'] = array();
00334           // actions
00335           $mysess['actions'] = array(
00336              array(__("view session"),"?logsess={$mysess['id']}"),
00337              array(__("user's log"),"?logauth={$mysess['auth']}&amp;loguser={$mysess['username']}")
00338           );
00339 
00340           $sessions[$mysess['id']] = $mysess;
00341         }
00342         mysql_free_result($res);
00343         array_reverse($sessions);
00344 
00345         // attach events
00346         $sql = "SELECT s.id, a.text
00347                 FROM       {$globals->table_log_sessions} AS s
00348                 LEFT  JOIN {$globals->table_log_events}   AS e ON(e.session=s.id)
00349                 INNER JOIN {$globals->table_log_actions}  AS a ON(a.id=e.action)
00350                 $where";
00351 
00352         $res = $globals->db->query($sql);
00353         while( $event = mysql_fetch_assoc($res) ) {
00354             array_push($sessions[$event['id']]['events'],$event['text']);
00355         }
00356         mysql_free_result($res);
00357         $page->assign_by_ref('sessions',$sessions);
00358       } else {
00359         $page->assign('msg_nofilters', __("Please select a year and/or a user."));
00360       }
00361     }
00362 
00363     // translations
00364     $page->assign('msg_session_properties', __("session properties"));
00365     $page->assign('msg_user', __("user"));
00366     $page->assign('msg_host', __("host"));
00367     $page->assign('msg_browser', __("browser"));
00368     $page->assign('msg_date', __("date"));
00369     $page->assign('msg_action', __("action"));
00370     $page->assign('msg_data', __("data"));
00371     $page->assign('msg_filter_by', __("filter by"));
00372     $page->assign('msg_start', __("start"));
00373     $page->assign('msg_summary', __("summary"));
00374     $page->assign('msg_actions', __("actions"));
00375     $page->assign('msg_year', __("year"));
00376     $page->assign('msg_month', __("month"));
00377     $page->assign('msg_day', __("day"));
00378     $page->assign('msg_submit', __("Submit"));
00379 
00380     // if requested, assign the content to be displayed
00381     if (!empty($outputvar)) {
00382       if (empty($template))
00383         $template = $globals->libroot."/templates/logger-view.tpl";
00384       $page->assign($outputvar, $page->fetch($template));
00385     }
00386   }
00387 
00388 }
00389 
00390 ?>

Generated on Fri Jan 11 01:20:08 2008 for Diogenes by  doxygen 1.5.1