Elgg  Version 1.9
system_log.php
Go to the documentation of this file.
1 <?php
30 function get_system_log($by_user = "", $event = "", $class = "", $type = "", $subtype = "", $limit = 10,
31  $offset = 0, $count = false, $timebefore = 0, $timeafter = 0, $object_id = 0,
32  $ip_address = "") {
33 
35 
36  $by_user_orig = $by_user;
37  if (is_array($by_user) && sizeof($by_user) > 0) {
38  foreach ($by_user as $key => $val) {
39  $by_user[$key] = (int) $val;
40  }
41  } else {
42  $by_user = (int)$by_user;
43  }
44 
45  $event = sanitise_string($event);
49  $ip_address = sanitise_string($ip_address);
50  $limit = (int)$limit;
51  $offset = (int)$offset;
52 
53  $where = array();
54 
55  if ($by_user_orig !== "" && $by_user_orig !== false && $by_user_orig !== null) {
56  if (is_int($by_user)) {
57  $where[] = "performed_by_guid=$by_user";
58  } else if (is_array($by_user)) {
59  $where [] = "performed_by_guid in (" . implode(",", $by_user) . ")";
60  }
61  }
62  if ($event != "") {
63  $where[] = "event='$event'";
64  }
65  if ($class !== "") {
66  $where[] = "object_class='$class'";
67  }
68  if ($type != "") {
69  $where[] = "object_type='$type'";
70  }
71  if ($subtype !== "") {
72  $where[] = "object_subtype='$subtype'";
73  }
74 
75  if ($timebefore) {
76  $where[] = "time_created < " . ((int) $timebefore);
77  }
78  if ($timeafter) {
79  $where[] = "time_created > " . ((int) $timeafter);
80  }
81  if ($object_id) {
82  $where[] = "object_id = " . ((int) $object_id);
83  }
84  if ($ip_address) {
85  $where[] = "ip_address = '$ip_address'";
86  }
87 
88  $select = "*";
89  if ($count) {
90  $select = "count(*) as count";
91  }
92  $query = "SELECT $select from {$CONFIG->dbprefix}system_log where 1 ";
93  foreach ($where as $w) {
94  $query .= " and $w";
95  }
96 
97  if (!$count) {
98  $query .= " order by time_created desc";
99  $query .= " limit $offset, $limit"; // Add order and limit
100  }
101 
102  if ($count) {
103  $numrows = get_data_row($query);
104  if ($numrows) {
105  return $numrows->count;
106  }
107  } else {
108  return get_data($query);
109  }
110 
111  return false;
112 }
113 
121 function get_log_entry($entry_id) {
122  global $CONFIG;
123 
124  $entry_id = (int)$entry_id;
125 
126  return get_data_row("SELECT * from {$CONFIG->dbprefix}system_log where id=$entry_id");
127 }
128 
136 function get_object_from_log_entry($entry) {
137  if (is_numeric($entry)) {
138  $entry = get_log_entry($entry);
139  if (!$entry) {
140  return false;
141  }
142  }
143 
144  $class = $entry->object_class;
145  $id = $entry->object_id;
146 
147  if (!class_exists($class)) {
148  // failed autoload
149  return false;
150  }
151 
152  $getters = array(
153  'ElggAnnotation' => 'elgg_get_annotation_from_id',
154  'ElggMetadata' => 'elgg_get_metadata_from_id',
155  'ElggRelationship' => 'get_relationship',
156  );
157 
158  if (isset($getters[$class]) && is_callable($getters[$class])) {
159  $object = call_user_func($getters[$class], $id);
160  } elseif (preg_match('~^Elgg[A-Z]~', $class)) {
162  } else {
163  // surround with try/catch because object could be disabled
164  try {
165  $object = new $class($entry->object_id);
166  return $object;
167  } catch (Exception $e) {
168 
169  }
170  }
171 
172  if (!is_object($object) || get_class($object) !== $class) {
173  return false;
174  }
175 
176  return $object;
177 }
178 
188 function system_log($object, $event) {
189  global $CONFIG;
190  static $log_cache;
191  static $cache_size = 0;
192 
193  if ($object instanceof Loggable) {
194 
195  /* @var ElggEntity|ElggExtender $object */
196  if (datalist_get('version') < 2012012000) {
197  // this is a site that doesn't have the ip_address column yet
198  return;
199  }
200 
201  // reset cache if it has grown too large
202  if (!is_array($log_cache) || $cache_size > 500) {
203  $log_cache = array();
204  $cache_size = 0;
205  }
206 
207  // Has loggable interface, extract the necessary information and store
208  $object_id = (int)$object->getSystemLogID();
209  $object_class = get_class($object);
210  $object_type = $object->getType();
211  $object_subtype = $object->getSubtype();
212  $event = sanitise_string($event);
213  $time = time();
214  $ip_address = sanitize_string(_elgg_services()->request->getClientIp());
215  if (!$ip_address) {
216  $ip_address = '0.0.0.0';
217  }
218  $performed_by = elgg_get_logged_in_user_guid();
219 
220  if (isset($object->access_id)) {
221  $access_id = $object->access_id;
222  } else {
223  $access_id = ACCESS_PUBLIC;
224  }
225  if (isset($object->enabled)) {
226  $enabled = $object->enabled;
227  } else {
228  $enabled = 'yes';
229  }
230 
231  if (isset($object->owner_guid)) {
232  $owner_guid = $object->owner_guid;
233  } else {
234  $owner_guid = 0;
235  }
236 
237  // Create log if we haven't already created it
238  if (!isset($log_cache[$time][$object_id][$event])) {
239  $query = "INSERT DELAYED into {$CONFIG->dbprefix}system_log
240  (object_id, object_class, object_type, object_subtype, event,
241  performed_by_guid, owner_guid, access_id, enabled, time_created, ip_address)
242  VALUES
243  ('$object_id','$object_class','$object_type', '$object_subtype', '$event',
244  $performed_by, $owner_guid, $access_id, '$enabled', '$time', '$ip_address')";
245 
246  insert_data($query);
247 
248  $log_cache[$time][$object_id][$event] = true;
249  $cache_size += 1;
250  }
251  }
252 }
253 
261 function archive_log($offset = 0) {
262  global $CONFIG;
263 
264  $offset = (int)$offset;
265  $now = time(); // Take a snapshot of now
266 
267  $ts = $now - $offset;
268 
269  // create table
270  $query = "CREATE TABLE {$CONFIG->dbprefix}system_log_$now as
271  SELECT * from {$CONFIG->dbprefix}system_log WHERE time_created<$ts";
272 
273  if (!update_data($query)) {
274  return false;
275  }
276 
277  // delete
278  // Don't delete on time since we are running in a concurrent environment
279  if (delete_data("DELETE from {$CONFIG->dbprefix}system_log WHERE time_created<$ts") === false) {
280  return false;
281  }
282 
283  // alter table to engine
284  if (!update_data("ALTER TABLE {$CONFIG->dbprefix}system_log_$now engine=archive")) {
285  return false;
286  }
287 
288  return true;
289 }
290 
300 function system_log_default_logger($event, $object_type, $object) {
301  system_log($object['object'], $object['event']);
302 
303  return true;
304 }
305 
317 function system_log_listener($event, $object_type, $object) {
318  if (($object_type != 'systemlog') && ($event != 'log')) {
319  elgg_trigger_event('log', 'systemlog', array('object' => $object, 'event' => $event));
320  }
321 
322  return true;
323 }
324 
326 elgg_register_event_handler('all', 'all', 'system_log_listener', 400);
327 
329 elgg_register_event_handler('log', 'systemlog', 'system_log_default_logger', 999);
get_data_row($query, $callback="")
Retrieve a single row from the database.
Definition: database.php:66
get_system_log($by_user="", $event="", $class="", $type="", $subtype="", $limit=10, $offset=0, $count=false, $timebefore=0, $timeafter=0, $object_id=0, $ip_address="")
Retrieve the system log based on a number of parameters.
Definition: system_log.php:30
$e
Definition: metadata.php:12
$object
Definition: upgrade.php:12
system_log_default_logger($event, $object_type, $object)
Default system log handler, allows plugins to override, extend or disable logging.
Definition: system_log.php:300
if(elgg_in_context('widget')) $offset
Definition: pagination.php:20
delete_data($query)
Remove a row from the database.
Definition: database.php:106
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
update_data($query)
Update a row in the database.
Definition: database.php:93
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
insert_data($query)
Insert a row into the database.
Definition: database.php:80
system_log_listener($event, $object_type, $object)
System log listener.
Definition: system_log.php:317
$owner_guid
$limit
Definition: userpicker.php:33
$key
Definition: summary.php:34
datalist_get($name)
Get the value of a datalist element.
_elgg_services()
Definition: autoloader.php:14
global $CONFIG
$enabled
Sample cli installer script.
sanitise_string($string)
Wrapper function for alternate English spelling (.
Definition: database.php:150
archive_log($offset=0)
This function creates an archive copy of the system log.
Definition: system_log.php:261
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Register a callback as an Elgg event handler.
Definition: elgglib.php:669
get_data($query, $callback="")
Retrieve rows from the database.
Definition: database.php:50
const ACCESS_PUBLIC
Definition: elgglib.php:2123
$count
Definition: tools.php:19
system_log($object, $event)
Log a system event related to a specific object.
Definition: system_log.php:188
get_object_from_log_entry($entry)
Return the object referred to by a given log entry.
Definition: system_log.php:136
elgg_trigger_event($event, $object_type, $object=null)
Trigger an Elgg Event and attempt to run all handler callbacks registered to that event...
Definition: elgglib.php:720
$subtype
Definition: river.php:10
get_log_entry($entry_id)
Return a specific log entry.
Definition: system_log.php:121
if(!$collection_name) $id
Definition: add.php:17
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:42
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:604