Move code of PlLogger out of the core, keep logging class, but not its
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 16 Nov 2008 20:45:08 +0000 (21:45 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 16 Nov 2008 20:45:08 +0000 (21:45 +0100)
implementation that is db dependent.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/pllogger.php
classes/s.php

index e88fd4d..6505a4d 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-
-class PlLogger
+abstract class PlLogger
 {
-    /** user id */
-    public $uid;
-    /** id of the session */
-    private $session;
-    /** list of available actions */
-    private $actions;
-
-    public $ip;
-    public $host;
-    public $proxy_ip;
-    public $proxy_host;
-
     /** The constructor, creates a new entry in the sessions table
      *
      * @param $uid the id of the logged user
      * @param $suid the id of the administrator who has just su'd to the user
      * @return VOID
      */
-    public function __construct($uid, $suid = 0)
-    {
-        // write the session entry
-        $this->uid     = $uid;
-        $this->session = $this->writeSession($uid, $suid);
-
-        // retrieve available actions
-        $res = XDB::iterRow("SELECT id, text FROM logger.actions");
-
-        while (list($action_id, $action_text) = $res->next()) {
-            $this->actions[$action_text] = $action_id;
-        }
-    }
+    abstract public function __construct($uid, $suid = 0);
 
-    /** Creates a new session entry in database and return its ID.
+    /** Logs an action and its related data.
      *
-     * @param $uid the id of the logged user
-     * @param $suid the id of the administrator who has just su'd to the user
-     * @return session the session id
+     * @param $action le type d'action
+     * @param $data les donnĂ©es (id de liste, etc.)
+     * @return VOID
      */
-    private function writeSession($uid, $suid = 0)
-    {
-        $ip      = $_SERVER['REMOTE_ADDR'];
-        $host    = strtolower(gethostbyaddr($_SERVER['REMOTE_ADDR']));
-        $browser = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '');
-
-        @list($forward_ip,) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
-        $forward_host = $forward_ip;
-        if ($forward_host) {
-            $forward_host = strtolower(gethostbyaddr($forward_host));
-        }
-        $proxy = '';
-        if ($forward_ip || @$_SERVER['HTTP_VIA']) {
-            $proxy = 'proxy';
-        }
+    abstract public function log($action, $data = null);
 
-        XDB::execute("INSERT INTO logger.sessions
-                         SET uid={?}, host={?}, ip={?}, forward_ip={?}, forward_host={?}, browser={?}, suid={?}, flags={?}",
-                     $uid, $host, ip_to_uint($ip), ip_to_uint($forward_ip), $forward_host, $browser, $suid, $proxy);
-        if ($forward_ip) {
-            $this->proxy_ip = $ip;
-            $this->proxy_host = $host;
-            $this->ip = $forward_ip;
-            $this->host = $forward_host;
+    /** Build a logger.
+     */
+    public static function get($uid, $suid = 0)
+    {
+        if (defined('PL_LOGGER_CLASS')) {
+            $class = PL_LOGGER_CLASS;
+            return new $class($uid, $suid);
         } else {
-            $this->ip = $ip;
-            $this->host = $host;
-        }
-
-        $id = XDB::insertId();
-        if ($uid and !$suid) {
-            XDB::execute('REPLACE INTO  logger.last_sessions (uid, id)
-                                VALUES  ({?}, {?})',
-                         $uid, $id);
+            return new DummyLogger($uid, $suid);
         }
-        return $id;
     }
+}
 
+class DummyLogger extends PlLogger
+{
+    public function __construct($uid, $suid = 0)
+    {
+    }
 
-    /** Logs an action and its related data.
-     *
-     * @param $action le type d'action
-     * @param $data les donnĂ©es (id de liste, etc.)
-     * @return VOID
-     */
     public function log($action, $data = null)
     {
-        if (isset($this->actions[$action])) {
-            XDB::execute("INSERT INTO logger.events
-                             SET session={?}, action={?}, data={?}",
-                         $this->session, $this->actions[$action], $data);
-        } else {
-            trigger_error("PlLogger: unknown action, $action", E_USER_WARNING);
-        }
     }
 }
 
index 9cc1637..0a9c1d7 100644 (file)
@@ -77,9 +77,9 @@ class S
         if (!S::has('log')) {
             if (S::has('suid')) {
                 $suid = S::v('suid');
-                S::set('log', new PlLogger(S::v('uid', $uid), $suid['uid']));
+                S::set('log', PlLogger::get(S::v('uid', $uid), $suid['uid']));
             } else if (S::has('uid') || $uid) {
-                S::set('log', new PlLogger(S::v('uid', $uid)));
+                S::set('log', PlLogger::get(S::v('uid', $uid)));
             }
         }
         return S::v('log');