Changeset 3730:5c45a5df9a59 for inc/core/class.dc.log.php
- Timestamp:
- 03/08/18 17:58:39 (8 years ago)
- Branch:
- default
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.log.php
r2566 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return;}12 if (!defined('DC_RC_PATH')) {return;} 13 13 14 14 class dcLog 15 15 { 16 17 16 protected $core; 17 protected $prefix; 18 18 19 20 19 /** 20 Object constructor. 21 21 22 @param core <b>dcCore</b>dcCore instance23 24 25 26 $this->core =&$core;27 28 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 29 30 31 32 30 /** 31 Retrieves logs. <b>$params</b> is an array taking the following 32 optionnal parameters: 33 33 34 35 36 37 38 39 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 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 } 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 } else { 50 $f = 51 'L.log_id, L.user_id, L.log_table, L.log_dt, ' . 52 'L.log_ip, L.log_msg, L.blog_id, U.user_name, ' . 53 'U.user_firstname, U.user_displayname, U.user_url'; 54 } 56 55 57 $strReq = 'SELECT '.$f.' FROM '.$this->prefix.'log L ';56 $strReq = 'SELECT ' . $f . ' FROM ' . $this->prefix . 'log L '; 58 57 59 60 61 'LEFT JOIN '.$this->prefix.'user U '.62 63 58 if (!$count_only) { 59 $strReq .= 60 'LEFT JOIN ' . $this->prefix . 'user U ' . 61 'ON U.user_id = L.user_id '; 62 } 64 63 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 } 64 if (!empty($params['blog_id'])) { 65 if ($params['blog_id'] === 'all') { 66 $strReq .= "WHERE NULL IS NULL "; 67 } else { 68 $strReq .= "WHERE L.blog_id = '" . $this->core->con->escape($params['blog_id']) . "' "; 69 } 70 } else { 71 $strReq .= "WHERE L.blog_id = '" . $this->core->blog->id . "' "; 72 } 76 73 77 78 $strReq .= 'AND L.user_id'.$this->core->con->in($params['user_id']);79 80 81 $strReq .= 'AND log_ip'.$this->core->con->in($params['log_ip']);82 83 84 $strReq .= 'AND log_table'.$this->core->con->in($params['log_table']);85 74 if (!empty($params['user_id'])) { 75 $strReq .= 'AND L.user_id' . $this->core->con->in($params['user_id']); 76 } 77 if (!empty($params['log_ip'])) { 78 $strReq .= 'AND log_ip' . $this->core->con->in($params['log_ip']); 79 } 80 if (!empty($params['log_table'])) { 81 $strReq .= 'AND log_table' . $this->core->con->in($params['log_table']); 82 } 86 83 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 } 94 } 84 if (!$count_only) { 85 if (!empty($params['order'])) { 86 $strReq .= 'ORDER BY ' . $this->core->con->escape($params['order']) . ' '; 87 } else { 88 $strReq .= 'ORDER BY log_dt DESC '; 89 } 90 } 95 91 96 97 98 92 if (!empty($params['limit'])) { 93 $strReq .= $this->core->con->limit($params['limit']); 94 } 99 95 100 101 96 $rs = $this->core->con->select($strReq); 97 $rs->extend('rsExtLog'); 102 98 103 104 99 return $rs; 100 } 105 101 106 107 108 102 /** 103 Creates a new log. Takes a cursor as input and returns the new log 104 ID. 109 105 110 @param cur <b>cursor</b>Log cursor111 @return <b>integer</b>New log ID112 113 114 115 $this->core->con->writeLock($this->prefix.'log');106 @param cur <b>cursor</b> Log cursor 107 @return <b>integer</b> New log ID 108 */ 109 public function addLog($cur) 110 { 111 $this->core->con->writeLock($this->prefix . 'log'); 116 112 117 118 119 120 121 'SELECT MAX(log_id) '.122 'FROM '.$this->prefix.'log '123 113 try 114 { 115 # Get ID 116 $rs = $this->core->con->select( 117 'SELECT MAX(log_id) ' . 118 'FROM ' . $this->prefix . 'log ' 119 ); 124 120 125 $cur->log_id= (integer) $rs->f(0) + 1;126 127 $cur->log_dt= date('Y-m-d H:i:s');121 $cur->log_id = (integer) $rs->f(0) + 1; 122 $cur->blog_id = (string) $this->core->blog->id; 123 $cur->log_dt = date('Y-m-d H:i:s'); 128 124 129 $this->getLogCursor($cur,$cur->log_id);125 $this->getLogCursor($cur, $cur->log_id); 130 126 131 132 $this->core->callBehavior('coreBeforeLogCreate',$this,$cur);127 # --BEHAVIOR-- coreBeforeLogCreate 128 $this->core->callBehavior('coreBeforeLogCreate', $this, $cur); 133 129 134 $cur->insert(); 135 $this->core->con->unlock(); 136 } 137 catch (Exception $e) 138 { 139 $this->core->con->unlock(); 140 throw $e; 141 } 130 $cur->insert(); 131 $this->core->con->unlock(); 132 } catch (Exception $e) { 133 $this->core->con->unlock(); 134 throw $e; 135 } 142 136 143 144 $this->core->callBehavior('coreAfterLogCreate',$this,$cur);137 # --BEHAVIOR-- coreAfterLogCreate 138 $this->core->callBehavior('coreAfterLogCreate', $this, $cur); 145 139 146 147 140 return $cur->log_id; 141 } 148 142 149 150 143 /** 144 Deletes a log. 151 145 152 @param id <b>integer</b>Log ID153 154 public function delLogs($id,$all = false)155 156 157 'TRUNCATE TABLE '.$this->prefix.'log' :158 'DELETE FROM '.$this->prefix.'log WHERE log_id'.$this->core->con->in($id);146 @param id <b>integer</b> Log ID 147 */ 148 public function delLogs($id, $all = false) 149 { 150 $strReq = $all ? 151 'TRUNCATE TABLE ' . $this->prefix . 'log' : 152 'DELETE FROM ' . $this->prefix . 'log WHERE log_id' . $this->core->con->in($id); 159 153 160 161 154 $this->core->con->execute($strReq); 155 } 162 156 163 private function getLogCursor($cur,$log_id = null)164 165 166 167 157 private function getLogCursor($cur, $log_id = null) 158 { 159 if ($cur->log_msg === '') { 160 throw new Exception(__('No log message')); 161 } 168 162 169 170 171 163 if ($cur->log_table === null) { 164 $cur->log_table = 'none'; 165 } 172 166 173 174 175 167 if ($cur->user_id === null) { 168 $cur->user_id = 'unknown'; 169 } 176 170 177 178 179 171 if ($cur->log_dt === '' || $cur->log_dt === null) { 172 $cur->log_dt = date('Y-m-d H:i:s'); 173 } 180 174 181 182 183 175 if ($cur->log_ip === null) { 176 $cur->log_ip = http::realIP(); 177 } 184 178 185 186 179 $log_id = is_int($log_id) ? $log_id : $cur->log_id; 180 } 187 181 } 188 182 189 183 class rsExtLog 190 184 { 191 192 193 194 185 public static function getUserCN($rs) 186 { 187 $user = dcUtils::getUserCN($rs->user_id, $rs->user_name, 188 $rs->user_firstname, $rs->user_displayname); 195 189 196 197 198 190 if ($user === 'unknown') { 191 $user = __('unknown'); 192 } 199 193 200 201 194 return $user; 195 } 202 196 }
Note: See TracChangeset
for help on using the changeset viewer.