Dotclear

source: inc/core/class.dc.log.php @ 0:54703be25dd6

Revision 0:54703be25dd6, 4.5 KB checked in by Dsls <dsls@…>, 14 years ago (diff)

2.3 branch (trunk) first checkin

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2010 Olivier Meunier & Association Dotclear
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 -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14class dcLog
15{
16     protected $core;
17     protected $prefix;
18     
19     /**
20     Object constructor.
21     
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     }
29     
30     /**
31     Retrieves logs. <b>$params</b> is an array taking the following
32     optionnal parameters:
33     
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
40     
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          }
56         
57          $strReq = 'SELECT '.$f.' FROM '.$this->prefix.'log L ';
58         
59          if (!$count_only) {
60               $strReq .=
61               'LEFT JOIN '.$this->prefix.'user U '.
62               'ON U.user_id = L.user_id ';
63          }
64         
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          }
76         
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          }
86         
87          if (!empty($params['order']) && !$count_only) {
88               $strReq .= 'ORDER BY '.$this->core->con->escape($params['order']).' ';
89          } else {
90               $strReq .= 'ORDER BY log_dt DESC ';
91          }
92         
93          if (!empty($params['limit'])) {
94               $strReq .= $this->core->con->limit($params['limit']);
95          }
96         
97          $rs = $this->core->con->select($strReq);
98          $rs->extend('rsExtLog');
99         
100          return $rs;
101     }
102     
103     /**
104     Creates a new log. Takes a cursor as input and returns the new log
105     ID.
106     
107     @param    cur       <b>cursor</b>       Log cursor
108     @return   <b>integer</b>      New log ID
109     */
110     public function addLog($cur)
111     {
112          $this->core->con->writeLock($this->prefix.'log');
113         
114          try
115          {
116               # Get ID
117               $rs = $this->core->con->select(
118                    'SELECT MAX(log_id) '.
119                    'FROM '.$this->prefix.'log ' 
120               );
121               
122               $cur->log_id = (integer) $rs->f(0) + 1;
123               $cur->blog_id = (string) $this->core->blog->id;
124               $cur->log_dt = date('Y-m-d H:i:s');
125               
126               $this->getLogCursor($cur,$cur->log_id);
127               
128               # --BEHAVIOR-- coreBeforeLogCreate
129               $this->core->callBehavior('coreBeforeLogCreate',$this,$cur);
130               
131               $cur->insert();
132               $this->core->con->unlock();
133          }
134          catch (Exception $e)
135          {
136               $this->core->con->unlock();
137               throw $e;
138          }
139         
140          # --BEHAVIOR-- coreAfterLogCreate
141          $this->core->callBehavior('coreAfterLogCreate',$this,$cur);
142         
143          return $cur->log_id;
144     }
145     
146     /**
147     Deletes a log.
148     
149     @param    id        <b>integer</b>      Log ID
150     */
151     public function delLogs($id,$all = false)
152     {
153          $strReq = $all ?
154          'TRUNCATE TABLE '.$this->prefix.'log' :
155          'DELETE FROM '.$this->prefix.'log WHERE log_id'.$this->core->con->in($id);
156         
157          $this->core->con->execute($strReq);
158     }
159     
160     private function getLogCursor($cur,$log_id = null)
161     {
162          if ($cur->log_msg === '') {
163               throw new Exception(__('No log message'));
164          }
165         
166          if ($cur->log_table === null) {
167               $cur->log_table = 'none';
168          }
169         
170          if ($cur->user_id === null) {
171               $cur->user_id = 'unknown';
172          }
173         
174          if ($cur->log_dt === '' || $cur->log_dt === null) {
175               $cur->log_dt = date('Y-m-d H:i:s');
176          }
177         
178          if ($cur->log_ip === null) {
179               $cur->log_ip = http::realIP();
180          }
181         
182          $log_id = is_int($log_id) ? $log_id : $cur->log_id;
183     }
184}
185
186class rsExtLog
187{
188     public static function getUserCN($rs)
189     {
190          $user = dcUtils::getUserCN($rs->user_id, $rs->user_name,
191          $rs->user_firstname, $rs->user_displayname);
192         
193          if ($user === 'unknown') {
194               $user = __('unknown');
195          }
196         
197          return $user;
198     }
199}
200
201?>
Note: See TracBrowser for help on using the repository browser.

Sites map