summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-12-21 03:50:11 +0000
committerBharat Mediratta <bharat@menalto.com>2008-12-21 03:50:11 +0000
commitf0b633334416ecb86de0356a06932fcf78efbfb7 (patch)
tree2adb3e18cd09d04cd63f3be65c4e12ef26aa49ff
parenta19a4729b55ac72d487def1bc5da9f427c084ba9 (diff)
Add a logging facility, and instrument login/logout to use it.
-rw-r--r--core/helpers/MY_url.php7
-rw-r--r--core/helpers/core_installer.php28
-rw-r--r--core/helpers/log.php44
-rw-r--r--core/models/log.php21
-rw-r--r--modules/user/controllers/logout.php7
-rw-r--r--modules/user/helpers/user.php16
6 files changed, 107 insertions, 16 deletions
diff --git a/core/helpers/MY_url.php b/core/helpers/MY_url.php
index 1c40c2bc..12c51f8d 100644
--- a/core/helpers/MY_url.php
+++ b/core/helpers/MY_url.php
@@ -33,7 +33,10 @@ class url extends url_Core {
return url::site($path, "http");
}
- public static function abs_current() {
- return self::abs_site(url::current());
+ /**
+ * Just like url::current except that it returns an absolute URI
+ */
+ public static function abs_current($qs=false) {
+ return self::abs_site(url::current($qs));
}
}
diff --git a/core/helpers/core_installer.php b/core/helpers/core_installer.php
index 399b6bf5..53b5bfe6 100644
--- a/core/helpers/core_installer.php
+++ b/core/helpers/core_installer.php
@@ -45,13 +45,13 @@ class core_installer {
$db->query("CREATE TABLE `items` (
`created` int(9) default NULL,
- `description` char(255) default NULL,
+ `description` varchar(255) default NULL,
`height` int(9) default NULL,
`id` int(9) NOT NULL auto_increment,
`left` int(9) NOT NULL,
`level` int(9) NOT NULL,
- `mime_type` char(64) default NULL,
- `name` char(255) default NULL,
+ `mime_type` varchar(64) default NULL,
+ `name` varchar(255) default NULL,
`owner_id` int(9) default NULL,
`parent_id` int(9) NOT NULL,
`resize_height` int(9) default NULL,
@@ -59,8 +59,8 @@ class core_installer {
`right` int(9) NOT NULL,
`thumb_height` int(9) default NULL,
`thumb_width` int(9) default NULL,
- `title` char(255) default NULL,
- `type` char(32) NOT NULL,
+ `title` varchar(255) default NULL,
+ `type` varchar(32) NOT NULL,
`updated` int(9) default NULL,
`view_count` int(9) default 0,
`width` int(9) default NULL,
@@ -69,9 +69,21 @@ class core_installer {
KEY `type` (`type`))
ENGINE=InnoDB DEFAULT CHARSET=utf8;");
+ $db->query("CREATE TABLE `logs` (
+ `id` int(9) NOT NULL auto_increment,
+ `category` varchar(64) default NULL,
+ `html` varchar(255) default NULL,
+ `message` text default NULL,
+ `referer` varchar(255) default NULL,
+ `severity` int(9) default 0,
+ `timestamp` int(9) default 0,
+ `url` varchar(255) default NULL,
+ PRIMARY KEY (`id`))
+ ENGINE=InnoDB DEFAULT CHARSET=utf8;");
+
$db->query("CREATE TABLE `modules` (
`id` int(9) NOT NULL auto_increment,
- `name` char(255) default NULL,
+ `name` varchar(255) default NULL,
`version` int(9) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY(`name`))
@@ -79,7 +91,7 @@ class core_installer {
$db->query("CREATE TABLE `permissions` (
`id` int(9) NOT NULL auto_increment,
- `name` char(255) default NULL,
+ `name` varchar(255) default NULL,
`version` int(9) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY(`name`))
@@ -95,7 +107,7 @@ class core_installer {
$db->query("CREATE TABLE `vars` (
`id` int(9) NOT NULL auto_increment,
`module_id` int(9),
- `name` char(255) NOT NULL,
+ `name` varchar(255) NOT NULL,
`value` text,
PRIMARY KEY (`id`),
UNIQUE KEY(`module_id`, `name`))
diff --git a/core/helpers/log.php b/core/helpers/log.php
new file mode 100644
index 00000000..fece9de3
--- /dev/null
+++ b/core/helpers/log.php
@@ -0,0 +1,44 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class log_Core {
+ const INFO = 1;
+ const WARNING = 2;
+ const ERROR = 3;
+
+ /**
+ * Add a log entry.
+ *
+ * @param string $category an arbitrary category we can use to filter log messages
+ * @param string $message a detailed log message
+ * @param integer $severity INFO, WARNING or ERROR
+ * @param string $html an html snippet presented alongside the log message to aid the admin
+ */
+ function add($category, $message, $severity=INFO, $html) {
+ $log = ORM::factory("log");
+ $log->category = $category;
+ $log->message = $message;
+ $log->severity = $severity;
+ $log->html = $html;
+ $log->url = url::abs_current(true);
+ $log->referer = request::referrer(null);
+ $log->timestamp = time();
+ $log->save();
+ }
+}
diff --git a/core/models/log.php b/core/models/log.php
new file mode 100644
index 00000000..6803d41d
--- /dev/null
+++ b/core/models/log.php
@@ -0,0 +1,21 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Log_Model extends ORM {
+}
diff --git a/modules/user/controllers/logout.php b/modules/user/controllers/logout.php
index 19a8450b..60f9ee50 100644
--- a/modules/user/controllers/logout.php
+++ b/modules/user/controllers/logout.php
@@ -19,12 +19,7 @@
*/
class Logout_Controller extends Controller {
public function index() {
- try {
- Session::instance()->destroy();
- module::event("user_logout", $user);
- } catch (Exception $e) {
- Kohana::log("error", $e);
- }
+ user::logout();
if ($this->input->get("continue")) {
url::redirect($this->input->get("continue"));
}
diff --git a/modules/user/helpers/user.php b/modules/user/helpers/user.php
index 387e0e3e..962db8a7 100644
--- a/modules/user/helpers/user.php
+++ b/modules/user/helpers/user.php
@@ -187,6 +187,22 @@ class user_Core {
user::set_active($user);
module::event("user_login", $user);
+ log::add("user", "User $user->name logged in",
+ log::INFO, html::anchor("user/$user->id", $user->name));
+ }
+
+ public static function logout() {
+ $user = user::active();
+ if (!$user->guest) {
+ try {
+ Session::instance()->destroy();
+ } catch (Exception $e) {
+ Kohana::log("error", $e);
+ }
+ module::event("user_logout", $user);
+ log::add("user", "User $user->name logged out",
+ log::INFO, html::anchor("user/$user->id", $user->name));
+ }
}
/**