[Mvblog-commits] r770 - in trunk: admin common upgrades/mysql upgrades/pgsql upgrades/sqlite

mvblog-commits at lists.three-dimensional.net mvblog-commits at lists.three-dimensional.net
Sun Dec 30 18:31:20 CET 2007


Author: michiel
Date: 2007-12-30 18:31:19 +0100 (Sun, 30 Dec 2007)
New Revision: 770

Added:
   trunk/common/mvblog_log.php
   trunk/upgrades/mysql/2007123000.php
   trunk/upgrades/pgsql/2007123000.php
   trunk/upgrades/sqlite/2007123000.php
Modified:
   trunk/admin/index.php
   trunk/common/mvblog_admin.php
   trunk/common/mvblog_common.php
Log:
introduce a logging class.
For now it only logs stuff in the admin backend.
More work to be done.

Re #161


Modified: trunk/admin/index.php
===================================================================
--- trunk/admin/index.php	2007-12-29 19:22:51 UTC (rev 769)
+++ trunk/admin/index.php	2007-12-30 17:31:19 UTC (rev 770)
@@ -172,6 +172,9 @@
 				case "show_about" :
 					$admin->show_about();
 					break;
+				case "show_userlog" :
+					$admin->show_userlog();
+					break;
 				default : 
 					$admin->show_index();
 					break;

Modified: trunk/common/mvblog_admin.php
===================================================================
--- trunk/common/mvblog_admin.php	2007-12-29 19:22:51 UTC (rev 769)
+++ trunk/common/mvblog_admin.php	2007-12-30 17:31:19 UTC (rev 770)
@@ -46,6 +46,7 @@
 		/* first do the common construct tasks */
 		parent::__construct($basedir."plugins/", 1);
 		$this->webroot = $this->webroot."admin/";
+		$this->log = new MvBlog_log($basedir, 1);
 
 		if (array_key_exists("action", $_POST))
 			$action = $_POST["action"];
@@ -67,6 +68,10 @@
 				$this->_selected_menuitem = "manage";
 				$this->_selected_submenuitem = "dossiers";
 				break;
+			case "show_userlog":
+				$this->_selected_menuitem = "users";
+				$this->_selected_submenuitem = "log";
+				break;
 			case "show_authors" :
 			case "edit_author" :
 				$this->_selected_menuitem = "users";
@@ -216,6 +221,7 @@
 			$_SESSION["author_email"]    = $row["email"];
 			$_SESSION["author_website"]  = $row["website"];
 			$_SESSION["blog_user"]       = 1;
+			$this->log->add_log(mktime(), $row["id"], 1, "Admin logged in");
 			header("Location: index.php");
 		} else {
 			$this->show_login();
@@ -227,6 +233,7 @@
 	 * Logout user
 	 */
 	public function logout() {
+		$this->log->add_log(mktime(), $_SESSION["author_id"], 1, "Admin logged off");
 		session_destroy();
 		header("Location: index.php");
 	}
@@ -357,7 +364,7 @@
 				<a class="if_menu_item" href="../index.php">Site</a>&nbsp;
 				<a class="if_menu_item<?php if ($this->_selected_menuitem == "index")      { echo "_act"; } ?>" href="./index.php"><?php echo gettext("Main"); ?></a>&nbsp;
 				<a class="if_menu_item<?php if ($this->_selected_menuitem == "manage")     { echo "_act"; } ?>" href="./index.php?action=show_posts"><?php echo gettext("Manage"); ?></a>&nbsp;
-				<a class="if_menu_item<?php if ($this->_selected_menuitem == "users")      { echo "_act"; } ?>" href="./index.php?action=show_authors"><?php echo gettext("Users"); ?></a>&nbsp;
+				<a class="if_menu_item<?php if ($this->_selected_menuitem == "users")      { echo "_act"; } ?>" href="./index.php?action=show_userlog"><?php echo gettext("Users"); ?></a>&nbsp;
 				<a class="if_menu_item<?php if ($this->_selected_menuitem == "settings")   { echo "_act"; } ?>" href="./index.php?action=show_settings"><?php echo gettext("Settings"); ?></a>&nbsp;
 				<a class="if_menu_item<?php if ($this->_selected_menuitem == "plugins")    { echo "_act"; } ?>" href="./index.php?action=show_plugins"><?php echo gettext("Plugins"); ?></a>&nbsp;
 				<a class="if_menu_item<?php if ($this->_selected_menuitem == "import")     { echo "_act"; } ?>" href="./index.php?action=show_import"><?php echo gettext("Import"); ?></a>&nbsp;
@@ -550,6 +557,7 @@
 				$this->db_quote("icon"),   $cat["icon"],
 				$cat["id"]
 			);
+			$logmsg = sprintf("Category %d:%s updated", $cat["id"], $cat["name"]);
 		} else {
 			$query  = sprintf("INSERT INTO categories (%s, %s, %s, %s, %s) VALUES ('%s', '%s', %d, %d, '%s')",
 				$this->db_quote("name"), $this->db_quote("desc"), $this->db_quote("active"), $this->db_quote("public"), $this->db_quote("icon"),
@@ -559,11 +567,13 @@
 				$cat["public"],
 				$cat["icon"]
 			);
+			$logmsg = sprintf("Category %s created", $cat["name"]);
 		}
 		$res =& $this->db->exec($query);
 		if (PEAR::isError($res)) {
 			die($res->getUserInfo());
 		}
+		$this->log->add_log(mktime(), $_SESSION["author_id"], 1, $logmsg);
 		header("Location: index.php?action=show_cats");
 	}
 	/* }}} */
@@ -579,6 +589,7 @@
 		if (PEAR::isError($res)) {
 			die($res->getUserInfo());
 		}
+		$this->log->add_log(mktime(), $_SESSION["author_id"], 1, sprintf("Category %d deleted", $id));
 		header("Location: index.php?action=show_cats");
 	}
 	/* }}} */
@@ -676,6 +687,7 @@
 				$this->db_quote("public"), $dossier["public"],
 				$dossier["id"]
 			);
+			$logmsg = sprintf("Dossier %d:%s updated", $dossier["id"], $dossier["name"]);
 		} else {
 			$query  = sprintf("INSERT INTO dossiers (%s, %s, %s, %s) VALUES ('%s', '%s', %d, %d)",
 				$this->db_quote("name"), $this->db_quote("desc"), $this->db_quote("active"), $this->db_quote("public"),
@@ -684,11 +696,13 @@
 				$dossier["active"],
 				$dossier["public"]
 			);
+			$logmsg = sprintf("Dossier %s created", $dossier["name"]);
 		}
 		$res =& $this->db->exec($query);
 		if (PEAR::isError($res)) {
 			die($res->getUserInfo());
 		}
+		$this->log->add_log(mktime(), $_SESSION["author_id"], 1, $logmsg);
 		header("Location: index.php?action=show_dossiers");
 	}
 	/* }}} */
@@ -704,6 +718,7 @@
 		if (PEAR::isError($res)) {
 			die($res->getUserInfo());
 		}
+		$this->log->add_log(mktime(), $_SESSION["author_id"], 1, sprintf("Dossier %d deleted", $id));
 		header("Location: index.php?action=show_dossiers");
 	}
 	/* }}} */
@@ -836,6 +851,7 @@
 					$query .= sprintf(", %s = '%s'", $this->db_quote("password"), trim(preg_quote(strip_tags($author["password"]), "'")));
 				}
 				$query .= sprintf(" WHERE id = %d", $author["id"]);
+				$logmsg = sprintf("Author %d:%s updated", $author["id"], $author["login"]);
 			} else {
 				$query = sprintf("INSERT INTO authors (%s, %s, %s, %s, %s, %s) VALUES ('%s', '%s', '%s', '%s', '%s', %d)",
 					$this->db_quote("password"), $this->db_quote("fullname"), $this->db_quote("email"), $this->db_quote("website"), $this->db_quote("login"), $this->db_quote("active"),
@@ -846,11 +862,13 @@
 					preg_quote(strip_tags($author["login"]), "'"),
 					$author["active"]
 				);
+				$logmsg = sprintf("Author %s created", $author["login"]);
 			}
 			$res =& $this->db->exec($query);
 			if (PEAR::isError($res)) {
 				die($res->getUserInfo());
 			}
+			$this->log->add_log(mktime(), $_SESSION["author_id"], 1, $logmsg);
 			header("Location: index.php?action=show_authors");
 		} else {
 			header("Location: index.php?action=edit_author&id=".$author["id"]."&error=$error");
@@ -1035,6 +1053,7 @@
 					$query .= sprintf(", %s = '%s'", $this->db_quote("password"), trim(preg_quote(strip_tags($user["password"]), "'")));
 				}
 				$query .= sprintf(" WHERE id = %d", $user["id"]);
+				$logmsg = sprintf("User %d:%s updated", $user["id"], $user["username"]);
 			} else {
 				$query = sprintf("INSERT INTO blog_users (%s, %s, %s, %s, %s, %s) VALUES ('%s', '%s', '%s', '%s', '%s', %d)",
 					$this->db_quote("password"), $this->db_quote("realname"), $this->db_quote("email"), $this->db_quote("website"), $this->db_quote("username"), $this->db_quote("active"),
@@ -1045,11 +1064,13 @@
 					preg_quote(strip_tags($user["username"]), "'"),
 					$user["active"]
 				);
+				$logmsg = sprintf("User %s created", $user["username"]);
 			}
 			$res =& $this->db->exec($query);
 			if (PEAR::isError($res)) {
 				die($res->getUserInfo());
 			}
+			$this->log->add_log(mktime(), $_SESSION["author_id"], 1, $logmsg);
 			header("Location: index.php?action=show_users");
 		} else {
 			header("Location: index.php?action=edit_user&id=".$user["id"]."&error=$error");
@@ -1086,6 +1107,7 @@
 				$res = $this->db->exec($sql);
 			}
 		}
+		$this->log->add_log(mktime(), $_SESSION["author_id"], 1, sprintf("User %d deleted", $userid));
 		header("Location: index.php?action=show_users");
 	}
 	/* }}} */
@@ -1450,6 +1472,7 @@
 				$tb_uri = "";
 			$query .= sprintf(", ping_sent = 1, tb_uri = '%s'", preg_quote($tb_uri, "'"));
 			$query .= sprintf(" WHERE id = %d", $post["id"]);
+			$logmsg = sprintf("Article %d:%s updated", $post["id"], $post["title"]);
 		} else {
 			$query  = sprintf("INSERT INTO articles (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
 				$this->db_quote("title"), $this->db_quote("body"), $this->db_quote("authors_id"), $this->db_quote("categories_ids"), $this->db_quote("date"), $this->db_quote("active"),
@@ -1466,11 +1489,13 @@
 				(array_key_exists("dossier_id", $post))?$post["dossier_id"]:0,
 				$post["postformat"]
 			);
+			$logmsg = sprintf("Article %s created", $post["title"]);
 		}
 		$res =& $this->db->exec($query);
 		if (PEAR::isError($res)) {
 			die($res->getUserInfo());
 		}
+		$this->log->add_log(mktime(), $_SESSION["author_id"], 1, $logmsg);
 		if (!$post["id"]) {
 			/* this is a new post. fetch old data */
 			$sql = sprintf("SELECT id FROM articles WHERE %s = '%s' AND %s = '%s' AND authors_id = %d AND categories_ids = '%s'",
@@ -1498,6 +1523,7 @@
 			} else {
 				die("error");
 			}
+			$this->log->add_log(mktime(), $_SESSION["author_id"], 1, sprintf("Trackback for article %s sent to %s", $post["title"], $post["tb_uri"]));
 			$sql = sprintf("UPDATE articles SET ping_sent = 1 WHERE id = %d", $post["id"]);
 			$res =& $db->exec($sql);
 		}
@@ -1522,6 +1548,7 @@
 		if (PEAR::isError($res)) {
 			die($res->getUserInfo());
 		}
+		$this->log->add_log(mktime(), $_SESSION["author_id"], 1, sprintf("Article %d deleted", $id));
 		header("Location: index.php?action=show_posts");
 	}
 	/* }}} */
@@ -1600,6 +1627,7 @@
 	public function delete_comment($id) {
 		$sql = sprintf("DELETE FROM comments WHERE id = %d", $id);
 		$res = $this->db->exec($sql);
+		$this->log->add_log(mktime(), $_SESSION["author_id"], 1, sprintf("Comment %d deleted", $id));
 		header("Location: index.php?action=show_comments");
 	}
 	/* }}} */
@@ -1697,6 +1725,7 @@
 		$this->plugman->activate_plugin($plugin);
 		$active_plugins = serialize($this->active_plugins);
 		$this->save_settings(array("active_plugins" => $active_plugins));
+		$this->log->add_log(mktime(), $_SESSION["author_id"], 1, sprintf("Plugin %s activated", $plugin));
 		return true;
 	}
 	/* }}} */
@@ -1712,6 +1741,7 @@
 		$this->plugman->deactivate_plugin($plugin);
 		$active_plugins = serialize($this->active_plugins);
 		$this->save_settings(array("active_plugins" => $active_plugins));
+		$this->log->add_log(mktime(), $_SESSION["author_id"], 1, sprintf("Plugin %s de-activated", $plugin));
 		return true;
 	}
 	/* }}} */
@@ -2010,5 +2040,13 @@
 		$import->run_module($type, $options);
 	}
 	/* }}} */
+	/* show_userlog {{{ */
+	/**
+	 * Show a log of what happened
+	 */
+	public function show_userlog() {
+		$this->log->show_log();
+	}
+	/* }}} */
 }
 ?>

Modified: trunk/common/mvblog_common.php
===================================================================
--- trunk/common/mvblog_common.php	2007-12-29 19:22:51 UTC (rev 769)
+++ trunk/common/mvblog_common.php	2007-12-30 17:31:19 UTC (rev 770)
@@ -136,7 +136,8 @@
 	 */
 	public function __construct($plugindir="plugins/", $adminmode=0, $upgrade=0) {
 		/* start session and output buffering */
-		session_start();
+		if (session_id() == "")
+			session_start();
 		ob_start();
 
 		/* handle php bugs with globals overwrite */

Added: trunk/common/mvblog_log.php
===================================================================
--- trunk/common/mvblog_log.php	                        (rev 0)
+++ trunk/common/mvblog_log.php	2007-12-30 17:31:19 UTC (rev 770)
@@ -0,0 +1,102 @@
+<?php
+/**
+ * MvBlog -- An open source no-nosense blogtool
+ *
+ * Copyright (C) 2005-2007, Michiel van Baak
+ * Michiel van Baak <mvanbaak at users.sourceforge.net>
+ *
+ * See http://dev.mvblog.org for more information on MvBlog.
+ * That page also provides Bugtrackers, Filereleases etc.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ *
+ * @package MvBlog
+ * @author Michiel van Baak
+ * @version %%VERSION%%
+ * @copyright 2005-2007 Michiel van Baak
+ */
+
+/**
+ * Class that holds methods to create/read action log.
+ * @package MvBlog
+ */
+Class MvBlog_log extends MvBlog_common {
+	/* constants */
+	/* variables */
+	/* methods */
+	/* __construct {{{ */
+	/**
+	 * Class constructor.
+	 */
+	public function __construct($basedir = "", $adminmode) {
+		parent::__construct($basedir."plugins/", $adminmode);
+	}
+	/* }}} */
+	/* data setters */
+	/* add_log {{{ */
+	/**
+	 * Add a logentry to the database
+	 *
+	 * @param int $datetime Entry timestamp
+	 * @param int $user_id The userid who generated event
+	 * @param int $user_type The type of user. 1 for admin, 2 for bloguser
+	 * @param string $msg The log message
+	 * @return bool true on success, false on failure
+	 */
+	public function add_log($datetime, $user_id, $user_type, $msg) {
+		$sql = sprintf("INSERT INTO log VALUES (%d, %d, %d, '%s');",
+			$datetime, $user_id, $user_type, $msg);
+		$res = $this->db->query($sql);
+		if (PEAR::isError($res))
+			return false;
+		return true;
+
+	}
+	/* }}} */
+	/* data getters */
+	/* get_log {{{ */
+	/**
+	 * Get logrecords
+	 *
+	 * @param int $user_type If 1 only show author actions, if 2 only show user actions, if 0 show all actions.
+	 * @param int $count If set only show this many logrecords
+	 * @param int $ts_start If set, start showing records from this time
+	 * @param int $ts_end If set, stop showing records at this time
+	 */
+	public function get_log($user_type = 0, $count = 25, $ts_start = 0, $ts_end = 0) {
+		$logrecords = array();
+		$sql = "SELECT * FROM log ORDER BY time DESC";
+		$res = $this->db->query($sql);
+		while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
+			$row["human_time"] = date("Y-m-d H:i:s", $row["time"]);
+			$row["username"] = $this->authors[$row["user_id"]]["login"];
+			$row["fullname"] = $this->authors[$row["user_id"]]["fullname"];
+			$logrecords[] = $row;
+		}
+		return $logrecords;
+	}
+	/* }}} */
+	/* output functions */
+	/* show_log {{{ */
+	/**
+	 * Show log overview
+	 */
+	public function show_log() {
+		$logrecords = $this->get_log();
+		echo "<table style=\"border: 1px solid black;\"><tr>\n";
+		echo "\t<td style=\"border: 1px solid black;\">date</td><td style=\"border: 1px solid black;\">login</td><td style=\"border: 1px solid black;\">fullname</td>\n";
+		echo "</tr>";
+		foreach ($logrecords as $logrecord) {
+			echo "<tr>\n";
+			echo sprintf("\t<td style=\"border: 1px solid black;\">%s</td><td style=\"border: 1px solid black;\">%s</td><td style=\"border: 1px solid black;\">%s</td>\n", $logrecord["human_time"],
+				$logrecord["username"], $logrecord["fullname"]);
+			echo "</tr><tr>\n";
+			echo sprintf("\t<td colspan=\"3\" style=\"border: 1px solid black;\">%s</td>\n", $logrecord["msg"]);
+			echo "</tr>";
+		}
+		echo "</table>";
+	}
+	/* }}} */
+}

Added: trunk/upgrades/mysql/2007123000.php
===================================================================
--- trunk/upgrades/mysql/2007123000.php	                        (rev 0)
+++ trunk/upgrades/mysql/2007123000.php	2007-12-30 17:31:19 UTC (rev 770)
@@ -0,0 +1,8 @@
+<?php
+$sql[] = "CREATE TABLE log (
+	time int(11),
+	user_id int(11),
+	user_type smallint(3),
+	msg text
+);";
+?>

Added: trunk/upgrades/pgsql/2007123000.php
===================================================================
--- trunk/upgrades/pgsql/2007123000.php	                        (rev 0)
+++ trunk/upgrades/pgsql/2007123000.php	2007-12-30 17:31:19 UTC (rev 770)
@@ -0,0 +1,8 @@
+<?php
+$sql[] = "CREATE TABLE log (
+	time integer,
+	user_id integer,
+	user_type integer,
+	msg text
+);";
+?>

Added: trunk/upgrades/sqlite/2007123000.php
===================================================================
--- trunk/upgrades/sqlite/2007123000.php	                        (rev 0)
+++ trunk/upgrades/sqlite/2007123000.php	2007-12-30 17:31:19 UTC (rev 770)
@@ -0,0 +1,8 @@
+<?php
+$sql[] = "CREATE TABLE log (
+	time INTEGER,
+	user_id INTEGER,
+	user_type smallint(3),
+	msg text
+);";
+?>



More information about the Mvblog-commits mailing list