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