1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8//this script may only be included - so its better to die if called directly. 9if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) { 10 header("location: index.php"); 11 exit; 12} 13 14class LogsQueryLib 15{ 16 public $type = ""; 17 public $id = ""; 18 public $action = ""; 19 public $start = ""; 20 public $end = ""; 21 public $client = ""; 22 public $groupType = null; 23 public $limit = null; 24 public $desc = true; 25 26 static function type($type = "") 27 { 28 $me = new self(); 29 $me->type = $type; 30 return $me; 31 } 32 33 static function wikiPage($id = "") 34 { 35 return LogsQueryLib::type("wiki page")->id($id); 36 } 37 38 static function wikiPagesFrom($user = "") 39 { 40 return LogsQueryLib::type("wiki page")->id($id); 41 } 42 43 static function forum($id = "") 44 { 45 return LogsQueryLib::type("forum")->id($id); 46 } 47 48 static function fileGallery($id = "") 49 { 50 return LogsQueryLib::type("file gallery")->id($id); 51 } 52 53 static function imageGallery($id = "") 54 { 55 return LogsQueryLib::type("image gallery")->id($id); 56 } 57 58 static function category($id = "") 59 { 60 return LogsQueryLib::type("category")->id($id); 61 } 62 63 static function system($id = "") 64 { 65 return LogsQueryLib::type("system")->id($id); 66 } 67 68 static function message($id = "") 69 { 70 return LogsQueryLib::type("message")->id($id); 71 } 72 73 static function comment($id = "") 74 { 75 return LogsQueryLib::type("comment")->id($id); 76 } 77 78 static function sheet($id = "") 79 { 80 return LogsQueryLib::type("sheet")->id($id); 81 } 82 83 static function blog($id = "") 84 { 85 return LogsQueryLib::type("blog")->id($id); 86 } 87 88 static function file($id = "") 89 { 90 return LogsQueryLib::type("file")->id($id); 91 } 92 93 static function article($id = "") 94 { 95 return LogsQueryLib::type("article")->id($id); 96 } 97 98 static function trackerItem($id = "") 99 { 100 return LogsQueryLib::type("trackeritem")->id($id); 101 } 102 103 static function wikiPageAttachment($id = "") 104 { 105 return LogsQueryLib::type("wiki page attachment")->id($id); 106 } 107 108 static function listTypes() 109 { 110 $tikilib = TikiLib::lib('tiki'); 111 $result = []; 112 113 foreach ($tikilib->fetchAll("SELECT objectType FROM tiki_actionlog GROUP By objectType") as $row) { 114 $result[] = $row['objectType']; 115 } 116 117 return $result; 118 } 119 120 static function listActions() 121 { 122 $tikilib = TikiLib::lib('tiki'); 123 $result = []; 124 125 foreach ($tikilib->fetchAll("SELECT action FROM tiki_actionlog GROUP By action") as $row) { 126 $result[] = $row['action']; 127 } 128 129 return $result; 130 } 131 132 static function url($id = "") 133 { 134 return LogsQueryLib::type("url")->id($id); 135 } 136 137 function id($id = "") 138 { 139 $this->id = $id; 140 return $this; 141 } 142 143 function viewed() 144 { 145 return $this->action("viewed"); 146 } 147 148 function action($action) 149 { 150 $this->action = $action; 151 return $this; 152 } 153 154 function start($start) 155 { 156 $this->start = $start; 157 return $this; 158 } 159 160 function end($end) 161 { 162 $this->end = $end; 163 return $this; 164 } 165 166 function client($client) 167 { 168 $this->client = $client; 169 return $this; 170 } 171 172 function count() 173 { 174 $this->groupType = "count"; 175 return $this->fetchAll(); 176 } 177 178 function countByDate() 179 { 180 $this->groupType = "countByDate"; 181 return $this->fetchAll(); 182 } 183 184 function limit($limit) 185 { 186 $this->limit = $limit; 187 return $this; 188 } 189 190 function desc() 191 { 192 $this->desc = true; 193 return $this; 194 } 195 196 function asc() 197 { 198 $this->desc = false; 199 return $this; 200 } 201 202 function countByDateFilterId($ids = []) 203 { 204 $tikilib = TikiLib::lib('tiki'); 205 206 $this->countByDate(); 207 208 $result = []; 209 210 foreach ($ids as $id) { 211 foreach ($this->id($id)->fetchAll() as $log) { 212 if (empty($result[$log['date']])) { 213 $result[$log['date']] = 0; 214 } 215 $result[$log['date']] += $log['count']; 216 } 217 } 218 219 return $result; 220 } 221 222 function countUsersFilterId($ids = []) 223 { 224 $tikilib = TikiLib::lib('tiki'); 225 226 $this->groupType = ""; 227 228 $result = []; 229 230 foreach ($ids as $id) { 231 foreach ($this->id($id)->fetchAll() as $log) { 232 if (empty($result[$log['user']])) { 233 $result[$log['user']] = 0; 234 } 235 236 $result[$log['user']]++; 237 } 238 } 239 240 return $result; 241 } 242 243 function countUsersIPFilterId($ids = []) 244 { 245 $tikilib = TikiLib::lib('tiki'); 246 247 $this->groupType = ""; 248 249 $result = []; 250 251 foreach ($ids as $id) { 252 foreach ($this->id($id)->fetchAll() as $log) { 253 $result[json_encode(["ip" => $log['ip'],"user" => $log['user']])]++; 254 } 255 } 256 257 return $result; 258 } 259 260 function fetchAll() 261 { 262 $tikilib = TikiLib::lib('tiki'); 263 264 if (empty($this->type)) { 265 return []; 266 } 267 268 269 $query = " 270 SELECT 271 " . ($this->groupType == "count" ? " COUNT(actionId) as count " : "") . " 272 " . ($this->groupType == "countByDate" ? " COUNT(actionId) AS count, DATE_FORMAT(FROM_UNIXTIME(lastModif), '%m/%d/%Y') as date " : "") . " 273 " . (empty($this->groupType) ? " * " : "") . " 274 FROM 275 tiki_actionlog 276 WHERE 277 objectType = ? 278 " . ( 279 ! empty($this->id) ? " AND object = ? " : "" 280 ) . " 281 " . ( 282 ! empty($this->action) ? " AND action = ? " : "" 283 ) . " 284 " . ( 285 ! empty($this->start) ? " AND lastModif > ? " : "" 286 ) . " 287 " . ( 288 ! empty($this->end) ? " AND lastModif < ? " : "" 289 ) . " 290 " . ( 291 ! empty($this->client) ? " AND client = ? " : "" 292 ) . " 293 294 " . ($this->groupType == "countByDate" ? " GROUP BY DATE_FORMAT(FROM_UNIXTIME(lastModif), '%Y%m%d') " : "") . " 295 296 ORDER BY lastModif " . ($this->desc == true ? "DESC" : "ASC") . " 297 298 " . (! empty($this->limit) ? 299 " LIMIT " . $this->limit 300 : "" 301 ) . " 302 "; 303 304 $params = [$this->type]; 305 306 if (! empty($this->id)) { 307 $params[] = $this->id; 308 } 309 if (! empty($this->action)) { 310 $params[] = $this->action; 311 } 312 if (! empty($this->start)) { 313 $params[] = $this->start; 314 } 315 if (! empty($this->end)) { 316 $params[] = $this->end; 317 } 318 if (! empty($this->client)) { 319 $params[] = $this->client; 320 } 321 322 if ($this->groupType == "count") { 323 return $tikilib->getOne($query, $params); 324 } else { 325 return $tikilib->fetchAll($query, $params); 326 } 327 } 328} 329