OSDN Git Service

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/nucleus-jp/trunk@1211 1ca29...
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / libs / ACTIONLOG.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2011 The Nucleus Group
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * (see nucleus/documentation/index.html#license for more info)
11  */
12 /**
13  * Actionlog class for Nucleus
14  *
15  * @license http://nucleuscms.org/license.txt GNU General Public License
16  * @copyright Copyright (C) 2002-2011 The Nucleus Group
17  * @version $Id$
18  * $NucleusJP: ACTIONLOG.php,v 1.7 2007/03/13 05:18:32 shizuki Exp $
19  */
20 define('ERROR',1);              // only errors
21 define('WARNING',2);    // errors and warnings
22 define('INFO',3);               // info, errors and warnings
23 define('DEBUG',4);              // everything
24 $CONF['LogLevel'] = INFO;
25
26 class ACTIONLOG {
27
28         /**
29           * (Static) Method to add a message to the action log
30           */
31         function add($level, $message) {
32                 global $member, $CONF;
33
34                 if ($CONF['LogLevel'] < $level)
35                         return;
36
37                 if ($member && $member->isLoggedIn())
38                         $message = "[" . $member->getDisplayName() . "] " . $message;
39
40                 $message = sql_real_escape_string($message);            // add slashes
41                 $timestamp = date("Y-m-d H:i:s",time());        // format timestamp
42                 $query = "INSERT INTO " . sql_table('actionlog') . " (timestamp, message) VALUES ('$timestamp', '$message')";
43
44                 sql_query($query);
45
46                 ACTIONLOG::trimLog();
47         }
48
49         /**
50           * (Static) Method to clear the whole action log
51           */
52         function clear() {
53                 global $manager;
54
55                 $query = 'DELETE FROM ' . sql_table('actionlog');
56
57                 $manager->notify('ActionLogCleared',array());
58
59                 return sql_query($query);
60         }
61
62         /**
63           * (Static) Method to trim the action log (from over 500 back to 250 entries)
64           */
65         function trimLog() {
66                 static $checked = 0;
67
68                 // only check once per run
69                 if ($checked) return;
70
71                 // trim
72                 $checked = 1;
73
74                 $iTotal = quickQuery('SELECT COUNT(*) AS result FROM ' . sql_table('actionlog'));
75
76                 // if size > 500, drop back to about 250
77                 $iMaxSize = 500;
78                 $iDropSize = 250;
79                 if ($iTotal > $iMaxSize) {
80                         $tsChop = quickQuery('SELECT timestamp as result FROM ' . sql_table('actionlog') . ' ORDER BY timestamp DESC LIMIT '.$iDropSize.',1');
81                         sql_query('DELETE FROM ' . sql_table('actionlog') . ' WHERE timestamp < \'' . $tsChop . '\'');
82                 }
83
84         }
85
86 }
87
88 ?>