forked from opendcim/openDCIM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.inc.php
More file actions
154 lines (124 loc) · 4.14 KB
/
logging.inc.php
File metadata and controls
154 lines (124 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/*
openDCIM
This is the main class library for the openDCIM application, which
is a PHP/Web based data center infrastructure management system.
This application was originally written by Scott A. Milliken while
employed at Vanderbilt University in Nashville, TN, as the
Data Center Manager, and released under the GNU GPL.
Copyright (C) 2011 Scott A. Milliken
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For further details on the license, see http://www.gnu.org/licenses
=====================================================================
Logging is designed to be object agnostic. On any function you want
logged add the following line to the function.
LogActions::LogThis($this);
*/
class LogActions {
var $UserID;
var $Class;
var $ObjectID;
var $Action;
var $Property;
var $OldVal;
var $NewVal;
var $Time;
function __construct(){
$this->UserID=$_SERVER['REMOTE_USER'];
}
function query($sql){
global $dbh;
return $dbh->query($sql);
}
function exec($sql){
global $dbh;
return $dbh->exec($sql);
}
// Generic catch all logging function
static function LogThis($object,$originalobject=null){
$log=new LogActions();
$trace=debug_backtrace();
// we're only concerned with the 2nd record $trace can be read for a full debug if something calls for it
$caller=$trace[1];
$action=$caller['function'];
if(preg_match("/create/i", $caller['function'])){$action='1';}
if(preg_match("/delete/i", $caller['function'])){$action='2';}
if(preg_match("/update/i", $caller['function'])){$action='3';}
// Move the action onto the object
$log->Action=$action;
$log->Class=get_class($object);
// Will return true/false for key and value comparison
function key_comp($v1, $v2) {
return ($v1 == $v2)?0:1;
}
function val_comp($v1, $v2) {
return ($v1 == $v2)?0:1;
}
$diff=array();
// Find the difference between the original object and the altered object, if present
if(!is_null($originalobject)){
$diff=(array_udiff_uassoc((array)$object,(array)$originalobject, "key_comp", "val_comp"));
// Note the changed values
foreach($diff as $key => $value){
$diff[$key]=$key.": ".$originalobject->$key." => ".$object->$key;
}
}
switch($log->Class){
case "Device":
$log->ObjectID=$object->DeviceID;
break;
case "Cabinet":
$log->ObjectID=$object->CabinetID;
break;
case "SupplyBin":
case "Supplies":
case "Config":
// do we want to track when the default system config has been updated?
case "Contact":
case "PowerDistribution":
case "PowerConnection":
// similar questions as to the switch connections. are we going to track this?
case "CDUTemplate":
case "PowerPanel":
// only has create and update. should changes here be logged or figure out what changed and log that?
case "PowerSource":
// same as PowerPanel
case "DeviceTemplate":
default:
}
$return=true;
// If there are any differences then we are upating an object otherwise
// this is a new object so just log the action as a create
if(count($diff)){
foreach($diff as $key => $value){
$log->Property=$key;
$log->OldVal=$originalobject->$key;
$log->NewVal=$object->$key;
$return=($log->WriteToDB())?$return:false;
}
}else{
$return=$log->WriteToDB();
}
return $return;
}
function WriteToDB(){
$sql="INSERT INTO fac_GenericLog set UserID=\"$this->UserID\",
Class=\"$this->Class\", ObjectID=\"$this->ObjectID\", Action=\"$this->Action\",
Property=\"$this->Property\", OldVal=\"$this->OldVal\", NewVal=\"$this->NewVal\";";
if(!$this->exec($sql)){
global $dbh;
$info=$dbh->errorInfo();
error_log("PDO Error::LogActions:WriteToDB {$info[2]} SQL=$sql");
return false;
}
return true;
}
// Add in functions here for actions lookup by device, user, date, etc
}
?>