OSDN Git Service

Subversion由来のタグを削除
[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-2012 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  * Actionlog class for Nucleus
13  */
14
15 define('ERROR',1);              // only errors
16 define('WARNING',2);    // errors and warnings
17 define('INFO',3);               // info, errors and warnings
18 define('DEBUG',4);              // everything
19 $CONF['LogLevel'] = INFO;
20
21 class ACTIONLOG {
22
23         /**
24           * (Static) Method to add a message to the action log
25           */
26         function add($level, $message) {
27                 global $member, $CONF;
28
29                 if ($CONF['LogLevel'] < $level)
30                         return;
31
32                 if ($member && $member->isLoggedIn())
33                         $message = "[" . $member->getDisplayName() . "] " . $message;
34
35                 $message = sql_real_escape_string($message);            // add slashes
36                 $timestamp = date("Y-m-d H:i:s",time());        // format timestamp
37                 $query = "INSERT INTO " . sql_table('actionlog') . " (timestamp, message) VALUES ('$timestamp', '$message')";
38
39                 sql_query($query);
40
41                 ACTIONLOG::trimLog();
42         }
43
44         /**
45           * (Static) Method to clear the whole action log
46           */
47         function clear() {
48                 global $manager;
49
50                 $query = 'DELETE FROM ' . sql_table('actionlog');
51                 
52                 $param = array();
53                 $manager->notify('ActionLogCleared', $param);
54
55                 return sql_query($query);
56         }
57
58         /**
59           * (Static) Method to trim the action log (from over 500 back to 250 entries)
60           */
61         function trimLog() {
62                 static $checked = 0;
63
64                 // only check once per run
65                 if ($checked) return;
66
67                 // trim
68                 $checked = 1;
69
70                 $iTotal = quickQuery('SELECT COUNT(*) AS result FROM ' . sql_table('actionlog'));
71
72                 // if size > 500, drop back to about 250
73                 $iMaxSize = 500;
74                 $iDropSize = 250;
75                 if ($iTotal > $iMaxSize) {
76                         $tsChop = quickQuery('SELECT timestamp as result FROM ' . sql_table('actionlog') . ' ORDER BY timestamp DESC LIMIT '.$iDropSize.',1');
77                         sql_query('DELETE FROM ' . sql_table('actionlog') . ' WHERE timestamp < \'' . $tsChop . '\'');
78                 }
79
80         }
81
82 }
83
84 ?>