OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / ACTIONLOG.php
1 <<<<<<< HEAD
2 <?php\r
3 /*\r
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
5  * Copyright (C) 2002-2012 The Nucleus Group\r
6  *\r
7  * This program is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License\r
9  * as published by the Free Software Foundation; either version 2\r
10  * of the License, or (at your option) any later version.\r
11  * (see nucleus/documentation/index.html#license for more info)\r
12  */\r
13 /**\r
14  * Actionlog class for Nucleus\r
15  *\r
16  * @license http://nucleuscms.org/license.txt GNU General Public License\r
17  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
18  * @version $Id: ACTIONLOG.php 1470 2010-11-29 22:10:16Z ftruscot $\r
19  */\r
20 define('ERROR',1);              // only errors\r
21 define('WARNING',2);    // errors and warnings\r
22 define('INFO',3);               // info, errors and warnings\r
23 define('DEBUG',4);              // everything\r
24 $CONF['LogLevel'] = INFO;\r
25 \r
26 class ActionLog\r
27 {\r
28         /**\r
29          * ActionLog::add()\r
30          * Method to add a message to the action log\r
31          * \r
32          * @static\r
33          * @param       Integer $level  log level\r
34          * @param       String  $message        log message\r
35          * @return      \r
36          * \r
37          */\r
38         function add($level, $message)\r
39         {\r
40                 global $member, $CONF;\r
41                 \r
42                 if ( $CONF['LogLevel'] < $level )\r
43                 {\r
44                         return;\r
45                 }\r
46                 \r
47                 if ( $member && $member->isLoggedIn() )\r
48                 {\r
49                         $message = "[" . $member->getDisplayName() . "] " . $message;\r
50                 }\r
51                 \r
52                 $query = "INSERT INTO %s (timestamp, message) VALUES (%s, %s)";\r
53                 $query = sprintf($query, sql_table('actionlog'), DB::formatDateTime(), DB::quoteValue($message));\r
54                 DB::execute($query);\r
55                 \r
56                 self::trimLog();\r
57                 return;\r
58         }\r
59         \r
60         /**\r
61           * (Static) Method to clear the whole action log\r
62           */\r
63         function clear() {\r
64                 global $manager;\r
65 \r
66                 $query = sprintf('DELETE FROM %s', sql_table('actionlog'));\r
67 \r
68                 $manager->notify('ActionLogCleared',array());\r
69 \r
70                 return DB::execute($query) !== FALSE;\r
71         }\r
72 \r
73         /**\r
74           * (Static) Method to trim the action log (from over 500 back to 250 entries)\r
75           */\r
76         function trimLog() {\r
77                 static $checked = 0;\r
78 \r
79                 // only check once per run\r
80                 if ($checked) return;\r
81 \r
82                 // trim\r
83                 $checked = 1;\r
84 \r
85                 $query = sprintf('SELECT COUNT(*) AS result FROM %s', sql_table('actionlog'));\r
86                 $iTotal = DB::getValue($query);\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                         $query = sprintf('SELECT timestamp as result FROM %s ORDER BY timestamp DESC LIMIT %d,1',\r
93                                 sql_table('actionlog'), intval($iDropSize));\r
94                         $tsChop = DB::getValue($query);\r
95                         $query = sprintf("DELETE FROM %s WHERE timestamp < '%s'", sql_table('actionlog'), $tsChop);\r
96                         DB::execute($query);\r
97                 }\r
98 \r
99         }\r
100 \r
101 }\r
102 \r
103 ?>\r
104 =======
105 <?php
106 /*
107  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
108  * Copyright (C) 2002-2009 The Nucleus Group
109  *
110  * This program is free software; you can redistribute it and/or
111  * modify it under the terms of the GNU General Public License
112  * as published by the Free Software Foundation; either version 2
113  * of the License, or (at your option) any later version.
114  * (see nucleus/documentation/index.html#license for more info)
115  */
116 /**
117  * Actionlog class for Nucleus
118  *
119  * @license http://nucleuscms.org/license.txt GNU General Public License
120  * @copyright Copyright (C) 2002-2009 The Nucleus Group
121  * @version $Id: ACTIONLOG.php 1822 2012-05-04 13:47:22Z sakamocchi $
122  */
123 define('ERROR',1);              // only errors
124 define('WARNING',2);    // errors and warnings
125 define('INFO',3);               // info, errors and warnings
126 define('DEBUG',4);              // everything
127 $CONF['LogLevel'] = INFO;
128
129 class ActionLog
130 {
131         /**
132          * ActionLog::add()
133          * Method to add a message to the action log
134          * 
135          * @static
136          * @param       Integer $level  log level
137          * @param       String  $message        log message
138          * @return      
139          * 
140          */
141         function add($level, $message)
142         {
143                 global $member, $CONF;
144                 
145                 if ( $CONF['LogLevel'] < $level )
146                 {
147                         return;
148                 }
149                 
150                 if ( $member && $member->isLoggedIn() )
151                 {
152                         $message = "[" . $member->getDisplayName() . "] " . $message;
153                 }
154                 
155                 $query = "INSERT INTO %s (timestamp, message) VALUES (%s, %s)";
156                 $query = sprintf($query, sql_table('actionlog'), DB::formatDateTime(), DB::quoteValue($message));
157                 DB::execute($query);
158                 
159                 self::trimLog();
160                 return;
161         }
162         
163         /**
164           * (Static) Method to clear the whole action log
165           */
166         function clear() {
167                 global $manager;
168
169                 $query = sprintf('DELETE FROM %s', sql_table('actionlog'));
170
171                 $data = array();
172                 $manager->notify('ActionLogCleared', $data);
173
174                 return DB::execute($query) !== FALSE;
175         }
176
177         /**
178           * (Static) Method to trim the action log (from over 500 back to 250 entries)
179           */
180         function trimLog() {
181                 static $checked = 0;
182
183                 // only check once per run
184                 if ($checked) return;
185
186                 // trim
187                 $checked = 1;
188
189                 $query = sprintf('SELECT COUNT(*) AS result FROM %s', sql_table('actionlog'));
190                 $iTotal = DB::getValue($query);
191
192                 // if size > 500, drop back to about 250
193                 $iMaxSize = 500;
194                 $iDropSize = 250;
195                 if ($iTotal > $iMaxSize) {
196                         $query = sprintf('SELECT timestamp as result FROM %s ORDER BY timestamp DESC LIMIT %d,1',
197                                 sql_table('actionlog'), intval($iDropSize));
198                         $tsChop = DB::getValue($query);
199                         $query = sprintf("DELETE FROM %s WHERE timestamp < '%s'", sql_table('actionlog'), $tsChop);
200                         DB::execute($query);
201                 }
202
203         }
204
205 }
206
207 ?>
208 >>>>>>> skinnable-master