Elgg  Version 1.11
system_log.php
Go to the documentation of this file.
1 <?php
30 function get_system_log($by_user = "", $event = "", $class = "", $type = "", $subtype = "", $limit = null,
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  if ($limit === null) {
51  $limit = elgg_get_config('default_limit');
52  }
53  $limit = (int)$limit;
54  $offset = (int)$offset;
55 
56  $where = array();
57 
58  if ($by_user_orig !== "" && $by_user_orig !== false && $by_user_orig !== null) {
59  if (is_int($by_user)) {
60  $where[] = "performed_by_guid=$by_user";
61  } else if (is_array($by_user)) {
62  $where [] = "performed_by_guid in (" . implode(",", $by_user) . ")";
63  }
64  }
65  if ($event != "") {
66  $where[] = "event='$event'";
67  }
68  if ($class !== "") {
69  $where[] = "object_class='$class'";
70  }
71  if ($type != "") {
72  $where[] = "object_type='$type'";
73  }
74  if ($subtype !== "") {
75  $where[] = "object_subtype='$subtype'";
76  }
77 
78  if ($timebefore) {
79  $where[] = "time_created < " . ((int) $timebefore);
80  }
81  if ($timeafter) {
82  $where[] = "time_created > " . ((int) $timeafter);
83  }
84  if ($object_id) {
85  $where[] = "object_id = " . ((int) $object_id);
86  }
87  if ($ip_address) {
88  $where[] = "ip_address = '$ip_address'";
89  }
90 
91  $select = "*";
92  if ($count) {
93  $select = "count(*) as count";
94  }
95  $query = "SELECT $select from {$CONFIG->dbprefix}system_log where 1 ";
96  foreach ($where as $w) {
97  $query .= " and $w";
98  }
99 
100  if (!$count) {
101  $query .= " order by time_created desc";
102  $query .= " limit $offset, $limit"; // Add order and limit
103  }
104 
105  if ($count) {
106  $numrows = get_data_row($query);
107  if ($numrows) {
108  return $numrows->count;
109  }
110  } else {
111  return get_data($query);
112  }
113 
114  return false;
115 }
116 
124 function get_log_entry($entry_id) {
125  global $CONFIG;
126 
127  $entry_id = (int)$entry_id;
128 
129  return get_data_row("SELECT * from {$CONFIG->dbprefix}system_log where id=$entry_id");
130 }
131 
139 function get_object_from_log_entry($entry) {
140  if (is_numeric($entry)) {
141  $entry = get_log_entry($entry);
142  if (!$entry) {
143  return false;
144  }
145  }
146 
147  $class = $entry->object_class;
148  $id = $entry->object_id;
149 
150  if (!class_exists($class)) {
151  // failed autoload
152  return false;
153  }
154 
155  $getters = array(
156  'ElggAnnotation' => 'elgg_get_annotation_from_id',
157  'ElggMetadata' => 'elgg_get_metadata_from_id',
158  'ElggRelationship' => 'get_relationship',
159  );
160 
161  if (isset($getters[$class]) && is_callable($getters[$class])) {
162  $object = call_user_func($getters[$class], $id);
163  } elseif (preg_match('~^Elgg[A-Z]~', $class)) {
165  } else {
166  // surround with try/catch because object could be disabled
167  try {
168  $object = new $class($entry->object_id);
169  return $object;
170  } catch (Exception $e) {
171 
172  }
173  }
174 
175  if (!is_object($object) || get_class($object) !== $class) {
176  return false;
177  }
178 
179  return $object;
180 }
181 
191 function system_log($object, $event) {
192  global $CONFIG;
193  static $log_cache;
194  static $cache_size = 0;
195 
196  if ($object instanceof Loggable) {
197 
198  /* @var \ElggEntity|\ElggExtender $object */
199  if (datalist_get('version') < 2012012000) {
200  // this is a site that doesn't have the ip_address column yet
201  return;
202  }
203 
204  // reset cache if it has grown too large
205  if (!is_array($log_cache) || $cache_size > 500) {
206  $log_cache = array();
207  $cache_size = 0;
208  }
209 
210  // Has loggable interface, extract the necessary information and store
211  $object_id = (int)$object->getSystemLogID();
212  $object_class = get_class($object);
213  $object_type = $object->getType();
214  $object_subtype = $object->getSubtype();
215  $event = sanitise_string($event);
216  $time = time();
217  $ip_address = sanitize_string(_elgg_services()->request->getClientIp());
218  if (!$ip_address) {
219  $ip_address = '0.0.0.0';
220  }
221  $performed_by = elgg_get_logged_in_user_guid();
222 
223  if (isset($object->access_id)) {
224  $access_id = $object->access_id;
225  } else {
226  $access_id = ACCESS_PUBLIC;
227  }
228  if (isset($object->enabled)) {
229  $enabled = $object->enabled;
230  } else {
231  $enabled = 'yes';
232  }
233 
234  if (isset($object->owner_guid)) {
235  $owner_guid = $object->owner_guid;
236  } else {
237  $owner_guid = 0;
238  }
239 
240  // Create log if we haven't already created it
241  if (!isset($log_cache[$time][$object_id][$event])) {
242  $query = "INSERT DELAYED into {$CONFIG->dbprefix}system_log
243  (object_id, object_class, object_type, object_subtype, event,
244  performed_by_guid, owner_guid, access_id, enabled, time_created, ip_address)
245  VALUES
246  ('$object_id','$object_class','$object_type', '$object_subtype', '$event',
247  $performed_by, $owner_guid, $access_id, '$enabled', '$time', '$ip_address')";
248 
249  insert_data($query);
250 
251  $log_cache[$time][$object_id][$event] = true;
252  $cache_size += 1;
253  }
254  }
255 }
256 
264 function archive_log($offset = 0) {
265  global $CONFIG;
266 
267  $offset = (int)$offset;
268  $now = time(); // Take a snapshot of now
269 
270  $ts = $now - $offset;
271 
272  // create table
273  $query = "CREATE TABLE {$CONFIG->dbprefix}system_log_$now as
274  SELECT * from {$CONFIG->dbprefix}system_log WHERE time_created<$ts";
275 
276  if (!update_data($query)) {
277  return false;
278  }
279 
280  // delete
281  // Don't delete on time since we are running in a concurrent environment
282  if (delete_data("DELETE from {$CONFIG->dbprefix}system_log WHERE time_created<$ts") === false) {
283  return false;
284  }
285 
286  // alter table to engine
287  if (!update_data("ALTER TABLE {$CONFIG->dbprefix}system_log_$now engine=archive")) {
288  return false;
289  }
290 
291  return true;
292 }
293 
303 function system_log_default_logger($event, $object_type, $object) {
304  system_log($object['object'], $object['event']);
305 
306  return true;
307 }
308 
320 function system_log_listener($event, $object_type, $object) {
321  if (($object_type != 'systemlog') && ($event != 'log')) {
322  elgg_trigger_event('log', 'systemlog', array('object' => $object, 'event' => $event));
323  }
324 
325  return true;
326 }
327 
328 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
330  $events->registerHandler('all', 'all', 'system_log_listener', 400);
331 
333  $events->registerHandler('log', 'systemlog', 'system_log_default_logger', 999);
334 };
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
get_data_row($query, $callback="")
Retrieve a single row from the database.
Definition: database.php:66
$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:303
if(!$count) $offset
Definition: pagination.php:25
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:320
$owner_guid
$limit
Definition: userpicker.php:31
$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:264
get_system_log($by_user="", $event="", $class="", $type="", $subtype="", $limit=null, $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
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
get_data($query, $callback="")
Retrieve rows from the database.
Definition: database.php:50
const ACCESS_PUBLIC
Definition: elgglib.php:1956
system_log($object, $event)
Log a system event related to a specific object.
Definition: system_log.php:191
get_object_from_log_entry($entry)
Return the object referred to by a given log entry.
Definition: system_log.php:139
if(elgg_in_context('widget')) $count
Definition: pagination.php:20
elgg_trigger_event($event, $object_type, $object=null)
Definition: elgglib.php:570
$subtype
Definition: river.php:12
get_log_entry($entry_id)
Return a specific log entry.
Definition: system_log.php:124
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:382