OSDN Git Service

FIX:DB::executeの戻り値の判定が正しく行えていない不具合の修正
[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-2012 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-2012 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                 $query = "INSERT INTO %s (timestamp, message) VALUES ('%s', %s)";\r
52                 $query = sprintf($query, sql_table('actionlog'), DB::formatDateTime(), DB::quoteValue($message));\r
53                 DB::execute($query);\r
54                 \r
55                 self::trimLog();\r
56                 return;\r
57         }\r
58         \r
59         /**\r
60           * (Static) Method to clear the whole action log\r
61           */\r
62         function clear() {\r
63                 global $manager;\r
64 \r
65                 $query = sprintf('DELETE FROM %s', sql_table('actionlog'));\r
66 \r
67                 $manager->notify('ActionLogCleared',array());\r
68 \r
69                 return DB::execute($query) !== FALSE;\r
70         }\r
71 \r
72         /**\r
73           * (Static) Method to trim the action log (from over 500 back to 250 entries)\r
74           */\r
75         function trimLog() {\r
76                 static $checked = 0;\r
77 \r
78                 // only check once per run\r
79                 if ($checked) return;\r
80 \r
81                 // trim\r
82                 $checked = 1;\r
83 \r
84                 $query = sprintf('SELECT COUNT(*) AS result FROM %s', sql_table('actionlog'));\r
85                 $iTotal = DB::getValue($query);\r
86 \r
87                 // if size > 500, drop back to about 250\r
88                 $iMaxSize = 500;\r
89                 $iDropSize = 250;\r
90                 if ($iTotal > $iMaxSize) {\r
91                         $query = sprintf('SELECT timestamp as result FROM %s ORDER BY timestamp DESC LIMIT %d,1',\r
92                                 sql_table('actionlog'), intval($iDropSize));\r
93                         $tsChop = DB::getValue($query);\r
94                         $query = sprintf("DELETE FROM %s WHERE timestamp < '%s'", sql_table('actionlog'), $tsChop);\r
95                         DB::execute($query);\r
96                 }\r
97 \r
98         }\r
99 \r
100 }\r
101 \r
102 ?>\r