| 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 | class PlatalLogger extends PlLogger |
| 22 | { |
| 23 | /** user id */ |
| 24 | public $uid; |
| 25 | /** id of the session */ |
| 26 | private $session; |
| 27 | /** list of available actions */ |
| 28 | private $actions; |
| 29 | |
| 30 | public $ip; |
| 31 | public $host; |
| 32 | public $proxy_ip; |
| 33 | public $proxy_host; |
| 34 | |
| 35 | /** The constructor, creates a new entry in the sessions table |
| 36 | * |
| 37 | * @param $uid the id of the logged user |
| 38 | * @param $suid the id of the administrator who has just su'd to the user |
| 39 | * @return VOID |
| 40 | */ |
| 41 | public function __construct($uid, $suid = 0) |
| 42 | { |
| 43 | // write the session entry |
| 44 | $this->uid = $uid; |
| 45 | $this->session = $this->writeSession($uid, $suid); |
| 46 | |
| 47 | // retrieve available actions |
| 48 | $res = XDB::iterRow("SELECT id, text FROM logger.actions"); |
| 49 | |
| 50 | while (list($action_id, $action_text) = $res->next()) { |
| 51 | $this->actions[$action_text] = $action_id; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** Creates a new session entry in database and return its ID. |
| 56 | * |
| 57 | * @param $uid the id of the logged user |
| 58 | * @param $suid the id of the administrator who has just su'd to the user |
| 59 | * @return session the session id |
| 60 | */ |
| 61 | private function writeSession($uid, $suid = 0) |
| 62 | { |
| 63 | $ip = $_SERVER['REMOTE_ADDR']; |
| 64 | $host = strtolower(gethostbyaddr($_SERVER['REMOTE_ADDR'])); |
| 65 | $browser = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''); |
| 66 | |
| 67 | @list($forward_ip,) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); |
| 68 | $forward_host = $forward_ip; |
| 69 | if ($forward_host) { |
| 70 | $forward_host = strtolower(gethostbyaddr($forward_host)); |
| 71 | } |
| 72 | $proxy = ''; |
| 73 | if ($forward_ip || @$_SERVER['HTTP_VIA']) { |
| 74 | $proxy = 'proxy'; |
| 75 | } |
| 76 | |
| 77 | XDB::execute("INSERT INTO logger.sessions |
| 78 | SET uid={?}, host={?}, ip={?}, forward_ip={?}, forward_host={?}, browser={?}, suid={?}, flags={?}", |
| 79 | $uid, $host, ip_to_uint($ip), ip_to_uint($forward_ip), $forward_host, $browser, $suid, $proxy); |
| 80 | if ($forward_ip) { |
| 81 | $this->proxy_ip = $ip; |
| 82 | $this->proxy_host = $host; |
| 83 | $this->ip = $forward_ip; |
| 84 | $this->host = $forward_host; |
| 85 | } else { |
| 86 | $this->ip = $ip; |
| 87 | $this->host = $host; |
| 88 | } |
| 89 | |
| 90 | return XDB::insertId(); |
| 91 | } |
| 92 | |
| 93 | public function saveLastSession() { |
| 94 | XDB::execute('REPLACE INTO logger.last_sessions (uid, id) |
| 95 | VALUES ({?}, {?})', |
| 96 | $this->uid, $this->session); |
| 97 | } |
| 98 | |
| 99 | public function isValid($uid) { |
| 100 | return $uid == $this->uid; |
| 101 | } |
| 102 | |
| 103 | /** Logs an action and its related data. |
| 104 | * |
| 105 | * @param $action le type d'action |
| 106 | * @param $data les données (id de liste, etc.) |
| 107 | * @return VOID |
| 108 | */ |
| 109 | public function log($action, $data = null) |
| 110 | { |
| 111 | if (isset($this->actions[$action])) { |
| 112 | XDB::execute("INSERT INTO logger.events |
| 113 | SET session={?}, action={?}, data={?}", |
| 114 | $this->session, $this->actions[$action], $data); |
| 115 | } else { |
| 116 | trigger_error("PlLogger: unknown action, $action", E_USER_WARNING); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 122 | ?> |