| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (C) 2003-2004 Polytechnique.org |
| 4 | * http://opensource.polytechnique.org/ |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | class CoreLogger { |
| 23 | /** user id */ |
| 24 | var $uid; |
| 25 | /** id of the session */ |
| 26 | var $session; |
| 27 | /** list of available actions */ |
| 28 | var $actions; |
| 29 | |
| 30 | /** The constructor, creates a new entry in the sessions table |
| 31 | * |
| 32 | * @param $uid the id of the logged user |
| 33 | * @param $suid the id of the administrator who has just su'd to the user |
| 34 | * @return VOID |
| 35 | */ |
| 36 | function CoreLogger($uid, $suid='') { |
| 37 | // write the session entry |
| 38 | $this->uid = $uid; |
| 39 | $this->session = $this->writeSession($uid, $suid); |
| 40 | |
| 41 | // retrieve available actions |
| 42 | $res = XDB::iterRow("SELECT id, text FROM logger.actions"); |
| 43 | |
| 44 | while (list($action_id, $action_text) = $res->next()) { |
| 45 | $this->actions[$action_text] = $action_id; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** Creates a new session entry in database and return its ID. |
| 50 | * |
| 51 | * @param $uid the id of the logged user |
| 52 | * @param $suid the id of the administrator who has just su'd to the user |
| 53 | * @return session the session id |
| 54 | */ |
| 55 | function writeSession($uid, $suid = null) { |
| 56 | $ip = $_SERVER['REMOTE_ADDR']; |
| 57 | $host = strtolower(gethostbyaddr($_SERVER['REMOTE_ADDR'])); |
| 58 | $browser = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''); |
| 59 | |
| 60 | XDB::execute("INSERT INTO logger.sessions |
| 61 | SET uid={?}, host={?}, ip={?}, browser={?}, suid={?}", |
| 62 | $uid, $host, $ip, $browser, $suid); |
| 63 | |
| 64 | return XDB::insertId(); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /** Logs an action and its related data. |
| 69 | * |
| 70 | * @param $action le type d'action |
| 71 | * @param $data les données (id de liste, etc.) |
| 72 | * @return VOID |
| 73 | */ |
| 74 | function log($action, $data = null) { |
| 75 | if (isset($this->actions[$action])) { |
| 76 | XDB::execute("INSERT INTO logger.events |
| 77 | SET session={?}, action={?}, data={?}", |
| 78 | $this->session, $this->actions[$action], $data); |
| 79 | } else { |
| 80 | echo "unknown action : $action<br />"; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | ?> |