From c4271d388f03a6c7a9e6437908e206f98ce1354a Mon Sep 17 00:00:00 2001 From: x2000habouzit Date: Mon, 31 Jul 2006 06:50:08 +0000 Subject: [PATCH] import some diogenes thingies into x.org, rough import, needs more work git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@743 839d8a87-29fc-0310-9880-83ba4fa771e5 --- classes/CoreLogger.php | 124 ++++++++++++++ classes/LoggerView.php | 352 ++++++++++++++++++++++++++++++++++++++ classes/Session.php | 1 - htdocs/xnet.php | 5 +- htdocs/xorg.php | 5 +- include/platal/globals.inc.php.in | 1 - include/xorg/session.inc.php | 4 +- modules/admin.php | 7 +- modules/auth.php | 3 +- modules/platal.php | 4 +- modules/register.php | 2 +- templates/logger-view.tpl | 6 +- templates/table-editor.tpl | 5 +- 13 files changed, 494 insertions(+), 25 deletions(-) create mode 100644 classes/CoreLogger.php create mode 100644 classes/LoggerView.php diff --git a/classes/CoreLogger.php b/classes/CoreLogger.php new file mode 100644 index 0000000..49d2ce5 --- /dev/null +++ b/classes/CoreLogger.php @@ -0,0 +1,124 @@ +table_actions = $globals->table_log_actions; + $this->table_events = $globals->table_log_events; + $this->table_sessions = $globals->table_log_sessions; + + // write the session entry + $this->uid = $uid; + $this->session = $this->writeSession($uid, $suid, $auth, $sauth); + + // retrieve available actions + $this->actions = $this->readActions(); + } + + + /** Creates a new session entry in database and return its ID. + * + * @param $uid the id of the logged user + * @param $suid the id of the administrator who has just su'd to the user + * @param $auth authentication method for the logged user + * @param $sauth authentication method for the su'er + * @return session the session id + */ + function writeSession($uid, $suid, $auth, $sauth) { + $ip = $_SERVER['REMOTE_ADDR']; + $host = strtolower(gethostbyaddr($_SERVER['REMOTE_ADDR'])); + $browser = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''); + $sql = "insert into {$this->table_sessions} set uid='$uid', host='$host', ip='$ip', browser='$browser'"; + // optional parameters + if ($suid) + $sql .= ", suid='$suid'"; + if ($auth) + $sql .= ", auth='$auth'"; + if ($sauth) + $sql .= ", sauth='$sauth'"; + + XDB::execute($sql); + + return XDB::insertId(); + } + + + /** Reads available actions from database. + * + * @return actions the available actions + */ + function readActions() { + $res = XDB::iterRow("select id, text from {$this->table_actions}"); + + while (list($action_id, $action_text) = $res->next()) { + $actions[$action_text] = $action_id; + } + + return $actions; + } + + + /** Logs an action and its related data. + * + * @param $action le type d'action + * @param $data les données (id de liste, etc.) + * @return VOID + */ + function log($action, $data="") { + if (isset($this->actions[$action])) { + XDB::execute("insert into {$this->table_events} + set session={?}, action={?}, data={?}", + $this->session, $this->actions[$action], $data); + } else { + echo "unknown action : $action
"; + } + } +} + +?> diff --git a/classes/LoggerView.php b/classes/LoggerView.php new file mode 100644 index 0000000..59f8d90 --- /dev/null +++ b/classes/LoggerView.php @@ -0,0 +1,352 @@ +table_log_sessions}"); + list($ymax, $ymin, $mmax, $mmin, $dmax, $dmin) = $res->fetchOneRow(); + + if (($year < $ymin) || ($year == $ymin && $month < $mmin)) { + return array(); + } + + if (($year > $ymax) || ($year == $ymax && $month > $mmax)) { + return array(); + } + + $min = ($year==$ymin && $month==$mmin) ? intval($dmin) : 1; + $max = ($year==$ymax && $month==$mmax) ? intval($dmax) : $day_max[$month]; + + for($i = $min; $i<=$max; $i++) { + $days[$i] = $i; + } + } + return $days; + } + + + /** Retrieves the available months for a given year. + * Obtains a list of month numbers that are within the timeframe that + * we have log entries for. + * + * @param integer year + * @return array List of month numbers we have log info for. + * @private + */ + function _getMonths($year) + { + global $globals; + + // give a 'no filter' option + $months[0] = __("all"); + + if ($year) { + $res = XDB::query("SELECT YEAR (MAX(start)), YEAR (MIN(start)), + MONTH(MAX(start)), MONTH(MIN(start)) + FROM {$globals->table_log_sessions}"); + list($ymax, $ymin, $mmax, $mmin) = $res->fetchOneRow(); + + if (($year < $ymin) || ($year > $ymax)) { + return array(); + } + + $min = $year == $ymin ? intval($mmin) : 1; + $max = $year == $ymax ? intval($mmax) : 12; + + for($i = $min; $i<=$max; $i++) { + $months[$i] = $i; + } + } + return $months; + } + + + /** Retrieves the username for a given authentication method and user id. + * This function caches the results of the lookups to avoid uncecessary + * database requests. + * + * @return the matching username. + * @private + */ + function _getUsername($auth, $uid) { + global $globals; + static $cache; + + if (!isset($cache[$auth][$uid])) { + $res = XDB::query('SELECT alias FROM aliases + WHERE id = {?} AND type="a_vie"', $uid); + $cache[$auth][$uid] = $res->fetchOneCell(); + } + + return $cache[$auth][$uid]; + } + + + /** Retrieves the available years. + * Obtains a list of years that we have log entries covering. + * + * @return array years we have log entries for. + * @private + */ + function _getYears() + { + global $globals; + + // give a 'no filter' option + $years[0] = __("all"); + + // retrieve available years + $res = XDB::query("select YEAR(MAX(start)), YEAR(MIN(start)) FROM {$globals->table_log_sessions}"); + list($max, $min) = $res->fetchOneRow(); + + for($i = intval($min); $i<=$max; $i++) { + $years[$i] = $i; + } + return $years; + } + + + /** Make a where clause to get a user's sessions. + * Prepare the where clause request that will retrieve the sessions. + * + * @param $year INTEGER Only get log entries made during the given year. + * @param $month INTEGER Only get log entries made during the given month. + * @param $day INTEGER Only get log entries made during the given day. + * @param $auth INTEGER Only get log entries with the given authentication type. + * @param $uid INTEGER Only get log entries referring to the given user ID. + * + * @return STRING the WHERE clause of a query, including the 'WHERE' keyword + * @private + */ + function _makeWhere($year, $month, $day, $auth, $uid) + { + // start constructing the "where" clause + $where = array(); + + if ($auth) + array_push($where, "auth='$auth'"); + + if ($uid) + array_push($where, "uid='$uid'"); + + // we were given at least a year + if ($year) { + if ($day) { + $dmin = mktime(0, 0, 0, $month, $day, $year); + $dmax = mktime(0, 0, 0, $month, $day+1, $year); + } elseif ($month) { + $dmin = mktime(0, 0, 0, $month, 1, $year); + $dmax = mktime(0, 0, 0, $month+1, 1, $year); + } else { + $dmin = mktime(0, 0, 0, 1, 1, $year); + $dmax = mktime(0, 0, 0, 1, 1, $year+1); + } + $where[] = "start >= " . date("Ymd000000", $dmin); + $where[] = "start < " . date("Ymd000000", $dmax); + } + + if (!empty($where)) { + return ' WHERE ' . implode($where, " AND "); + } else { + return ''; + } + // WE know it's totally reversed, so better use array_reverse than a SORT BY start DESC + } + + + /** Run the log viewer and fill out the Smarty variables for display. + * + * @param page the page that will display the viewer's data + * @param outputvar the Smarty variable to which we should assign the output + * @param template the template to use for display + */ + function run(&$page, $outputvar='', $template='') + { + global $globals; + + if (isset($_REQUEST['logsess'])) { + + // we are viewing a session + $res = XDB::query("SELECT host, ip, browser, auth, uid, sauth, suid + FROM {$globals->table_log_sessions} + WHERE id =".$_REQUEST['logsess']); + + $sarr = $res->fetchOneAssoc(); + + $sarr['username'] = $this->_getUsername($sarr['auth'], $sarr['uid']); + if ($sarr['suid']) { + $sarr['suer'] = $this->_getUsername($sarr['sauth'], $sarr['suid']); + } + $page->assign('session', $sarr); + + $res = XDB::iterator("SELECT a.text, e.data, UNIX_TIMESTAMP(e.stamp) AS stamp + FROM {$globals->table_log_events} AS e + LEFT JOIN {$globals->table_log_actions} AS a ON e.action=a.id + WHERE e.session='{$_REQUEST['logsess']}'"); + while ($myarr = $res->next()) { + $page->append('events', $myarr); + } + + } else { + + // we are browsing the available sessions + $logauth = isset($_REQUEST['logauth']) ? $_REQUEST['logauth'] : ''; + $loguser = isset($_REQUEST['loguser']) ? $_REQUEST['loguser'] : ''; + + $res = XDB::query('SELECT id FROM aliases WHERE alias={?}', + Env::v('loguser')); + $loguid = $res->fetchOneCell(); + + if ($loguid) { + $year = isset($_REQUEST['year']) ? $_REQUEST['year'] : 0; + $month = isset($_REQUEST['month']) ? $_REQUEST['month'] : 0; + $day = isset($_REQUEST['day']) ? $_REQUEST['day'] : 0; + } else { + $year = isset($_REQUEST['year']) ? $_REQUEST['year'] : date("Y"); + $month = isset($_REQUEST['month']) ? $_REQUEST['month'] : date("m"); + $day = isset($_REQUEST['day']) ? $_REQUEST['day'] : date("d"); + } + + if (!$year) $month = 0; + if (!$month) $day = 0; + + // smarty assignments + // retrieve available years + $page->assign('years', $this->_getYears()); + $page->assign('year', $year); + + // retrieve available months for the current year + $page->assign('months', $this->_getMonths($year)); + $page->assign('month', $month); + + // retrieve available days for the current year and month + $page->assign('days', $this->_getDays($year, $month)); + $page->assign('day', $day); + + // retrieve available auths + $auths = array('all', 'native' => 'X.org'); + $page->assign('auths', $auths); + + $page->assign('logauth', $logauth); + $page->assign('loguser', $loguser); + // smarty assignments + + if ($loguid || $year) { + + // get the requested sessions + $where = $this->_makeWhere($year, $month, $day, $logauth, $loguid); + $select = "SELECT id, UNIX_TIMESTAMP(start) as start, auth, uid + FROM {$globals->table_log_sessions} AS s + $where + ORDER BY start DESC"; + $res = XDB::iterator($select); + + $sessions = array(); + $odd = false; + while ($mysess = $res->next()) { + $mysess['username'] = $this->_getUsername($mysess['auth'], $mysess['uid']); + // pretty label for auth method + $mysess['lauth'] = $auths[$mysess['auth']]; + // summary of events + $mysess['events'] = array(); + // actions + $mysess['actions'] = array( + array(__("view session"), "admin/logger?logsess={$mysess['id']}"), + array(__("user's log"), "admin/logger?logauth={$mysess['auth']}&loguser={$mysess['username']}") + ); + + $mysess['class'] = $odd ? "odd" : "even"; + $sessions[$mysess['id']] = $mysess; + $odd = !$odd; + } + array_reverse($sessions); + + // attach events + $sql = "SELECT s.id, a.text + FROM {$globals->table_log_sessions} AS s + LEFT JOIN {$globals->table_log_events} AS e ON(e.session=s.id) + INNER JOIN {$globals->table_log_actions} AS a ON(a.id=e.action) + $where"; + + $res = XDB::iterator($sql); + while ($event = $res->next()) { + array_push($sessions[$event['id']]['events'], $event['text']); + } + $page->assign_by_ref('sessions', $sessions); + } else { + $page->assign('msg_nofilters', __("Please select a year and/or a user.")); + } + } + + // translations + $page->assign('msg_session_properties', __("session properties")); + $page->assign('msg_user', __("user")); + $page->assign('msg_host', __("host")); + $page->assign('msg_browser', __("browser")); + $page->assign('msg_date', __("date")); + $page->assign('msg_action', __("action")); + $page->assign('msg_data', __("data")); + $page->assign('msg_filter_by', __("filter by")); + $page->assign('msg_start', __("start")); + $page->assign('msg_summary', __("summary")); + $page->assign('msg_actions', __("actions")); + $page->assign('msg_year', __("year")); + $page->assign('msg_month', __("month")); + $page->assign('msg_day', __("day")); + $page->assign('msg_submit', __("Submit")); + + // if requested, assign the content to be displayed + if (!empty($outputvar)) { + $page->assign($outputvar, $page->fetch($template)); + } + + $page->changeTpl('logger-view.tpl'); + } + +} + +?> diff --git a/classes/Session.php b/classes/Session.php index ca2b7a7..c6d1b34 100644 --- a/classes/Session.php +++ b/classes/Session.php @@ -20,7 +20,6 @@ ***************************************************************************/ require_once 'diogenes/diogenes.misc.inc.php'; -require_once 'diogenes/diogenes.core.logger.inc.php'; class Session { diff --git a/htdocs/xnet.php b/htdocs/xnet.php index 93c1552..53ecd71 100644 --- a/htdocs/xnet.php +++ b/htdocs/xnet.php @@ -21,10 +21,11 @@ $GLOBALS['IS_XNET_SITE'] = true; -require 'xnet.inc.php'; - require_once dirname(__FILE__).'/../classes/Xnet.php'; require_once dirname(__FILE__).'/../classes/PLModule.php'; +require_once dirname(__FILE__).'/../classes/CoreLogger.php'; + +require 'xnet.inc.php'; if (!($path = Env::v('n')) || substr($path, 0, 4) != 'Xnet') { $platal = new Xnet('xnet', 'xnetgrp', 'xnetlists', 'xnetevents', 'geoloc'); diff --git a/htdocs/xorg.php b/htdocs/xorg.php index f9ad7f9..e966de3 100644 --- a/htdocs/xorg.php +++ b/htdocs/xorg.php @@ -21,10 +21,11 @@ // $GLOBALS['IS_XNET_SITE'] = true; -require_once 'xorg.inc.php'; - require_once dirname(__FILE__).'/../classes/Platal.php'; require_once dirname(__FILE__).'/../classes/PLModule.php'; +require_once dirname(__FILE__).'/../classes/CoreLogger.php'; + +require_once 'xorg.inc.php'; if (!($path = Env::v('n')) || ($path{0} < 'A' || $path{0} > 'Z')) { diff --git a/include/platal/globals.inc.php.in b/include/platal/globals.inc.php.in index 377869c..e539595 100644 --- a/include/platal/globals.inc.php.in +++ b/include/platal/globals.inc.php.in @@ -45,7 +45,6 @@ class PlatalGlobals extends DiogenesCoreGlobals var $dbuser = 'x4dat'; var $dbpwd = 'x4dat'; - var $table_auth = 'auth_user_md5'; var $table_log_actions = 'logger.actions'; var $table_log_sessions = 'logger.sessions'; var $table_log_events = 'logger.events'; diff --git a/include/xorg/session.inc.php b/include/xorg/session.inc.php index 8b041a1..6c20f39 100644 --- a/include/xorg/session.inc.php +++ b/include/xorg/session.inc.php @@ -224,11 +224,11 @@ function start_connexion ($uid, $identified) $suid = S::v('suid'); if ($suid) { - $logger = new DiogenesCoreLogger($uid, $suid); + $logger = new CoreLogger($uid, $suid); $logger->log("suid_start", S::v('forlife')." by {$suid['uid']}"); $sess['suid'] = $suid; } else { - $logger = S::v('log', new DiogenesCoreLogger($uid)); + $logger = S::v('log', new CoreLogger($uid)); $logger->log("connexion", $_SERVER['PHP_SELF']); setcookie('ORGuid', $uid, (time()+25920000), '/', '', 0); } diff --git a/modules/admin.php b/modules/admin.php index 3f72157..3259b9d 100644 --- a/modules/admin.php +++ b/modules/admin.php @@ -94,18 +94,17 @@ class AdminModule extends PLModule } function handler_logger(&$page) { - $page->changeTpl('logger-view.tpl'); - $page->assign('xorg_title','Polytechnique.org - Administration - Logs des sessions'); - require_once('diogenes/diogenes.logger-view.inc.php'); + require_once dirname(__FILE__).'/../classes/LoggerView.php'; if (!Env::has('logauth')) { $_REQUEST['logauth'] = 'native'; } - $logview = new DiogenesLoggerView; + $logview = new LoggerView; $logview->run($page); $page->fakeDiogenes(); + $page->assign('xorg_title','Polytechnique.org - Administration - Logs des sessions'); } function handler_user(&$page, $login = false) { diff --git a/modules/auth.php b/modules/auth.php index d1ac68b..31a6cb3 100644 --- a/modules/auth.php +++ b/modules/auth.php @@ -143,8 +143,7 @@ class AuthModule extends PLModule // mise à jour de l'heure et de la machine de dernier login sauf quand on est en suid if (!isset($_SESSION['suid'])) { $logger = (isset($_SESSION['log']) && $_SESSION['log']->uid == $uid) - ? $_SESSION['log'] - : new DiogenesCoreLogger($uid); + ? $_SESSION['log'] : new CoreLogger($uid); $logger->log('connexion_auth_ext', $_SERVER['PHP_SELF']); } diff --git a/modules/platal.php b/modules/platal.php index 1fe93ce..9949ccc 100644 --- a/modules/platal.php +++ b/modules/platal.php @@ -284,7 +284,7 @@ Mail envoy $mymail->send(); // on cree un objet logger et on log l'evenement - $logger = $_SESSION['log'] = new DiogenesCoreLogger($uid); + $logger = $_SESSION['log'] = new CoreLogger($uid); $logger->log('recovery', $emails); } else { $page->trig('Pas de résultat correspondant aux champs entrés dans notre base de données.'); @@ -306,7 +306,7 @@ Mail envoy $uid = $ligne["uid"]; if (Post::has('response2')) { $password = Post::v('response2'); - $logger = new DiogenesCoreLogger($uid); + $logger = new CoreLogger($uid); XDB::query('UPDATE auth_user_md5 SET password={?} WHERE user_id={?} AND perms IN("admin","user")', $password, $uid); diff --git a/modules/register.php b/modules/register.php index c7785b3..7acd2b9 100644 --- a/modules/register.php +++ b/modules/register.php @@ -249,7 +249,7 @@ class RegisterModule extends PLModule $redirect->add_email($email); // on cree un objet logger et on log l'inscription - $logger = new DiogenesCoreLogger($uid); + $logger = new CoreLogger($uid); $logger->log('inscription', $email); XDB::execute('UPDATE register_pending SET hash="INSCRIT" WHERE uid={?}', $uid); diff --git a/templates/logger-view.tpl b/templates/logger-view.tpl index fcc09c0..f20a2d3 100644 --- a/templates/logger-view.tpl +++ b/templates/logger-view.tpl @@ -20,11 +20,7 @@ {* *} {**************************************************************************} - -

- Parcours des logs du site -

- +

Parcours des logs du site

{if $smarty.request.logsess} diff --git a/templates/table-editor.tpl b/templates/table-editor.tpl index 2d1c2ae..dd6704c 100644 --- a/templates/table-editor.tpl +++ b/templates/table-editor.tpl @@ -21,7 +21,6 @@ {**************************************************************************} -

{$title}

{if $list} @@ -72,8 +71,8 @@ {if ($p_prev > -1) || ($p_next > -1)} {/if} -- 2.1.4