OSDN Git Service

MERGE: リビジョン1721をマージ。主要なクラス名をUpperCamelCaseに統一。
[nucleus-jp/nucleus-next.git] / nucleus / libs / ACTIONLOG.php
1 <?php\r
2 /*\r
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
4  * Copyright (C) 2002-2009 The Nucleus Group\r
5  *\r
6  * This program is free software; you can redistribute it and/or\r
7  * modify it under the terms of the GNU General Public License\r
8  * as published by the Free Software Foundation; either version 2\r
9  * of the License, or (at your option) any later version.\r
10  * (see nucleus/documentation/index.html#license for more info)\r
11  */\r
12 /**\r
13  * Actionlog class for Nucleus\r
14  *\r
15  * @license http://nucleuscms.org/license.txt GNU General Public License\r
16  * @copyright Copyright (C) 2002-2009 The Nucleus Group\r
17  * @version $Id: ACTIONLOG.php 1470 2010-11-29 22:10:16Z ftruscot $\r
18  */\r
19 define('ERROR',1);              // only errors\r
20 define('WARNING',2);    // errors and warnings\r
21 define('INFO',3);               // info, errors and warnings\r
22 define('DEBUG',4);              // everything\r
23 $CONF['LogLevel'] = INFO;\r
24 \r
25 class ActionLog\r
26 {\r
27         /**\r
28          * ActionLog::add()\r
29          * Method to add a message to the action log\r
30          * \r
31          * @static\r
32          * @param       Integer $level  log level\r
33          * @param       String  $message        log message\r
34          * @return      \r
35          * \r
36          */\r
37         function add($level, $message)\r
38         {\r
39                 global $member, $CONF;\r
40                 \r
41                 if ( $CONF['LogLevel'] < $level )\r
42                 {\r
43                         return;\r
44                 }\r
45                 \r
46                 if ( $member && $member->isLoggedIn() )\r
47                 {\r
48                         $message = "[" . $member->getDisplayName() . "] " . $message;\r
49                 }\r
50                 \r
51                 $message = sql_real_escape_string($message);            // add slashes\r
52                 $timestamp = date("Y-m-d H:i:s",time());        // format timestamp\r
53                 $query = "INSERT INTO %s (timestamp, message) VALUES ('%s', '%s')";\r
54                 $query = sprintf($query, sql_table('actionlog'), $timestamp, $message);\r
55                 sql_query($query);\r
56                 \r
57                 self::trimLog();\r
58                 return;\r
59         }\r
60         \r
61         /**\r
62           * (Static) Method to clear the whole action log\r
63           */\r
64         function clear() {\r
65                 global $manager;\r
66 \r
67                 $query = 'DELETE FROM ' . sql_table('actionlog');\r
68 \r
69                 $manager->notify('ActionLogCleared',array());\r
70 \r
71                 return sql_query($query);\r
72         }\r
73 \r
74         /**\r
75           * (Static) Method to trim the action log (from over 500 back to 250 entries)\r
76           */\r
77         function trimLog() {\r
78                 static $checked = 0;\r
79 \r
80                 // only check once per run\r
81                 if ($checked) return;\r
82 \r
83                 // trim\r
84                 $checked = 1;\r
85 \r
86                 $iTotal = quickQuery('SELECT COUNT(*) AS result FROM ' . sql_table('actionlog'));\r
87 \r
88                 // if size > 500, drop back to about 250\r
89                 $iMaxSize = 500;\r
90                 $iDropSize = 250;\r
91                 if ($iTotal > $iMaxSize) {\r
92                         $tsChop = quickQuery('SELECT timestamp as result FROM ' . sql_table('actionlog') . ' ORDER BY timestamp DESC LIMIT '.$iDropSize.',1');\r
93                         sql_query('DELETE FROM ' . sql_table('actionlog') . ' WHERE timestamp < \'' . $tsChop . '\'');\r
94                 }\r
95 \r
96         }\r
97 \r
98 }\r
99 \r
100 ?>\r