[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
[1179] | 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
[0] | 7 | # Licensed under the GPL version 2.0 license. |
---|
| 8 | # See LICENSE file or |
---|
| 9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
| 10 | # |
---|
| 11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
| 12 | if (!defined('DC_RC_PATH')) { return; } |
---|
| 13 | |
---|
| 14 | class dcLog |
---|
| 15 | { |
---|
| 16 | protected $core; |
---|
| 17 | protected $prefix; |
---|
[2566] | 18 | |
---|
[0] | 19 | /** |
---|
| 20 | Object constructor. |
---|
[2566] | 21 | |
---|
[0] | 22 | @param core <b>dcCore</b> dcCore instance |
---|
| 23 | */ |
---|
| 24 | public function __construct($core) |
---|
| 25 | { |
---|
| 26 | $this->core =& $core; |
---|
| 27 | $this->prefix = $core->prefix; |
---|
| 28 | } |
---|
[2566] | 29 | |
---|
[0] | 30 | /** |
---|
| 31 | Retrieves logs. <b>$params</b> is an array taking the following |
---|
| 32 | optionnal parameters: |
---|
[2566] | 33 | |
---|
[0] | 34 | - blog_id: Get logs belonging to given blog ID |
---|
| 35 | - user_id: Get logs belonging to given user ID |
---|
| 36 | - log_ip: Get logs belonging to given IP address |
---|
| 37 | - log_table: Get logs belonging to given log table |
---|
| 38 | - order: Order of results (default "ORDER BY log_dt DESC") |
---|
| 39 | - limit: Limit parameter |
---|
[2566] | 40 | |
---|
[0] | 41 | @param params <b>array</b> Parameters |
---|
| 42 | @param count_only <b>boolean</b> Only counts results |
---|
| 43 | @return <b>record</b> A record with some more capabilities |
---|
| 44 | */ |
---|
| 45 | public function getLogs($params = array(),$count_only = false) |
---|
| 46 | { |
---|
| 47 | if ($count_only) { |
---|
| 48 | $f = 'COUNT(log_id)'; |
---|
| 49 | } |
---|
| 50 | else { |
---|
| 51 | $f = |
---|
| 52 | 'L.log_id, L.user_id, L.log_table, L.log_dt, '. |
---|
| 53 | 'L.log_ip, L.log_msg, L.blog_id, U.user_name, '. |
---|
| 54 | 'U.user_firstname, U.user_displayname, U.user_url'; |
---|
| 55 | } |
---|
[2566] | 56 | |
---|
[0] | 57 | $strReq = 'SELECT '.$f.' FROM '.$this->prefix.'log L '; |
---|
[2566] | 58 | |
---|
[0] | 59 | if (!$count_only) { |
---|
| 60 | $strReq .= |
---|
| 61 | 'LEFT JOIN '.$this->prefix.'user U '. |
---|
| 62 | 'ON U.user_id = L.user_id '; |
---|
| 63 | } |
---|
[2566] | 64 | |
---|
[0] | 65 | if (!empty($params['blog_id'])) { |
---|
| 66 | if ($params['blog_id'] === 'all') { |
---|
| 67 | $strReq .= "WHERE NULL IS NULL "; |
---|
| 68 | } |
---|
| 69 | else { |
---|
| 70 | $strReq .= "WHERE L.blog_id = '".$this->core->con->escape($params['blog_id'])."' "; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | else { |
---|
| 74 | $strReq .= "WHERE L.blog_id = '".$this->core->blog->id."' "; |
---|
| 75 | } |
---|
[2566] | 76 | |
---|
[0] | 77 | if (!empty($params['user_id'])) { |
---|
| 78 | $strReq .= 'AND L.user_id'.$this->core->con->in($params['user_id']); |
---|
| 79 | } |
---|
| 80 | if (!empty($params['log_ip'])) { |
---|
| 81 | $strReq .= 'AND log_ip'.$this->core->con->in($params['log_ip']); |
---|
| 82 | } |
---|
| 83 | if (!empty($params['log_table'])) { |
---|
| 84 | $strReq .= 'AND log_table'.$this->core->con->in($params['log_table']); |
---|
| 85 | } |
---|
[2566] | 86 | |
---|
[126] | 87 | if (!$count_only) |
---|
| 88 | { |
---|
| 89 | if (!empty($params['order'])) { |
---|
| 90 | $strReq .= 'ORDER BY '.$this->core->con->escape($params['order']).' '; |
---|
| 91 | } else { |
---|
| 92 | $strReq .= 'ORDER BY log_dt DESC '; |
---|
| 93 | } |
---|
[0] | 94 | } |
---|
[2566] | 95 | |
---|
[0] | 96 | if (!empty($params['limit'])) { |
---|
| 97 | $strReq .= $this->core->con->limit($params['limit']); |
---|
| 98 | } |
---|
[2566] | 99 | |
---|
[0] | 100 | $rs = $this->core->con->select($strReq); |
---|
| 101 | $rs->extend('rsExtLog'); |
---|
[2566] | 102 | |
---|
[0] | 103 | return $rs; |
---|
| 104 | } |
---|
[2566] | 105 | |
---|
[0] | 106 | /** |
---|
| 107 | Creates a new log. Takes a cursor as input and returns the new log |
---|
| 108 | ID. |
---|
[2566] | 109 | |
---|
[0] | 110 | @param cur <b>cursor</b> Log cursor |
---|
| 111 | @return <b>integer</b> New log ID |
---|
| 112 | */ |
---|
| 113 | public function addLog($cur) |
---|
| 114 | { |
---|
| 115 | $this->core->con->writeLock($this->prefix.'log'); |
---|
[2566] | 116 | |
---|
[0] | 117 | try |
---|
| 118 | { |
---|
| 119 | # Get ID |
---|
| 120 | $rs = $this->core->con->select( |
---|
| 121 | 'SELECT MAX(log_id) '. |
---|
[2566] | 122 | 'FROM '.$this->prefix.'log ' |
---|
[0] | 123 | ); |
---|
[2566] | 124 | |
---|
[0] | 125 | $cur->log_id = (integer) $rs->f(0) + 1; |
---|
| 126 | $cur->blog_id = (string) $this->core->blog->id; |
---|
| 127 | $cur->log_dt = date('Y-m-d H:i:s'); |
---|
[2566] | 128 | |
---|
[0] | 129 | $this->getLogCursor($cur,$cur->log_id); |
---|
[2566] | 130 | |
---|
[0] | 131 | # --BEHAVIOR-- coreBeforeLogCreate |
---|
| 132 | $this->core->callBehavior('coreBeforeLogCreate',$this,$cur); |
---|
[2566] | 133 | |
---|
[0] | 134 | $cur->insert(); |
---|
| 135 | $this->core->con->unlock(); |
---|
| 136 | } |
---|
| 137 | catch (Exception $e) |
---|
| 138 | { |
---|
| 139 | $this->core->con->unlock(); |
---|
| 140 | throw $e; |
---|
| 141 | } |
---|
[2566] | 142 | |
---|
[0] | 143 | # --BEHAVIOR-- coreAfterLogCreate |
---|
| 144 | $this->core->callBehavior('coreAfterLogCreate',$this,$cur); |
---|
[2566] | 145 | |
---|
[0] | 146 | return $cur->log_id; |
---|
| 147 | } |
---|
[2566] | 148 | |
---|
[0] | 149 | /** |
---|
| 150 | Deletes a log. |
---|
[2566] | 151 | |
---|
[0] | 152 | @param id <b>integer</b> Log ID |
---|
| 153 | */ |
---|
| 154 | public function delLogs($id,$all = false) |
---|
| 155 | { |
---|
| 156 | $strReq = $all ? |
---|
| 157 | 'TRUNCATE TABLE '.$this->prefix.'log' : |
---|
| 158 | 'DELETE FROM '.$this->prefix.'log WHERE log_id'.$this->core->con->in($id); |
---|
[2566] | 159 | |
---|
[0] | 160 | $this->core->con->execute($strReq); |
---|
| 161 | } |
---|
[2566] | 162 | |
---|
[0] | 163 | private function getLogCursor($cur,$log_id = null) |
---|
| 164 | { |
---|
| 165 | if ($cur->log_msg === '') { |
---|
| 166 | throw new Exception(__('No log message')); |
---|
| 167 | } |
---|
[2566] | 168 | |
---|
[0] | 169 | if ($cur->log_table === null) { |
---|
| 170 | $cur->log_table = 'none'; |
---|
| 171 | } |
---|
[2566] | 172 | |
---|
[0] | 173 | if ($cur->user_id === null) { |
---|
| 174 | $cur->user_id = 'unknown'; |
---|
| 175 | } |
---|
[2566] | 176 | |
---|
[0] | 177 | if ($cur->log_dt === '' || $cur->log_dt === null) { |
---|
| 178 | $cur->log_dt = date('Y-m-d H:i:s'); |
---|
| 179 | } |
---|
[2566] | 180 | |
---|
[0] | 181 | if ($cur->log_ip === null) { |
---|
| 182 | $cur->log_ip = http::realIP(); |
---|
| 183 | } |
---|
[2566] | 184 | |
---|
[0] | 185 | $log_id = is_int($log_id) ? $log_id : $cur->log_id; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | class rsExtLog |
---|
| 190 | { |
---|
| 191 | public static function getUserCN($rs) |
---|
| 192 | { |
---|
| 193 | $user = dcUtils::getUserCN($rs->user_id, $rs->user_name, |
---|
| 194 | $rs->user_firstname, $rs->user_displayname); |
---|
[2566] | 195 | |
---|
[0] | 196 | if ($user === 'unknown') { |
---|
| 197 | $user = __('unknown'); |
---|
| 198 | } |
---|
[2566] | 199 | |
---|
[0] | 200 | return $user; |
---|
| 201 | } |
---|
| 202 | } |
---|