Dotclear

source: inc/core/class.dc.log.php @ 3731:3770620079d4

Revision 3731:3770620079d4, 5.3 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Simplify licence block at the beginning of each file

Line 
1<?php
2/**
3 * @package Dotclear
4 * @subpackage Core
5 *
6 * @copyright Olivier Meunier & Association Dotclear
7 * @copyright GPL-2.0-only
8 */
9
10if (!defined('DC_RC_PATH')) {return;}
11
12class dcLog
13{
14    protected $core;
15    protected $prefix;
16
17    /**
18    Object constructor.
19
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    }
27
28    /**
29    Retrieves logs. <b>$params</b> is an array taking the following
30    optionnal parameters:
31
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
38
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     */
43    public function getLogs($params = array(), $count_only = false)
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        }
53
54        $strReq = 'SELECT ' . $f . ' FROM ' . $this->prefix . 'log L ';
55
56        if (!$count_only) {
57            $strReq .=
58            'LEFT JOIN ' . $this->prefix . 'user U ' .
59                'ON U.user_id = L.user_id ';
60        }
61
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        }
71
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        }
81
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        }
89
90        if (!empty($params['limit'])) {
91            $strReq .= $this->core->con->limit($params['limit']);
92        }
93
94        $rs = $this->core->con->select($strReq);
95        $rs->extend('rsExtLog');
96
97        return $rs;
98    }
99
100    /**
101    Creates a new log. Takes a cursor as input and returns the new log
102    ID.
103
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');
110
111        try
112        {
113            # Get ID
114            $rs = $this->core->con->select(
115                'SELECT MAX(log_id) ' .
116                'FROM ' . $this->prefix . 'log '
117            );
118
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');
122
123            $this->getLogCursor($cur, $cur->log_id);
124
125            # --BEHAVIOR-- coreBeforeLogCreate
126            $this->core->callBehavior('coreBeforeLogCreate', $this, $cur);
127
128            $cur->insert();
129            $this->core->con->unlock();
130        } catch (Exception $e) {
131            $this->core->con->unlock();
132            throw $e;
133        }
134
135        # --BEHAVIOR-- coreAfterLogCreate
136        $this->core->callBehavior('coreAfterLogCreate', $this, $cur);
137
138        return $cur->log_id;
139    }
140
141    /**
142    Deletes a log.
143
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);
151
152        $this->core->con->execute($strReq);
153    }
154
155    private function getLogCursor($cur, $log_id = null)
156    {
157        if ($cur->log_msg === '') {
158            throw new Exception(__('No log message'));
159        }
160
161        if ($cur->log_table === null) {
162            $cur->log_table = 'none';
163        }
164
165        if ($cur->user_id === null) {
166            $cur->user_id = 'unknown';
167        }
168
169        if ($cur->log_dt === '' || $cur->log_dt === null) {
170            $cur->log_dt = date('Y-m-d H:i:s');
171        }
172
173        if ($cur->log_ip === null) {
174            $cur->log_ip = http::realIP();
175        }
176
177        $log_id = is_int($log_id) ? $log_id : $cur->log_id;
178    }
179}
180
181class rsExtLog
182{
183    public static function getUserCN($rs)
184    {
185        $user = dcUtils::getUserCN($rs->user_id, $rs->user_name,
186            $rs->user_firstname, $rs->user_displayname);
187
188        if ($user === 'unknown') {
189            $user = __('unknown');
190        }
191
192        return $user;
193    }
194}
Note: See TracBrowser for help on using the repository browser.

Sites map