DataObjects/Events.php
[Pman.Core] / DataObjects / Events.php
1 <?php
2 /**
3  * Table Definition for Events
4  *
5  * objects can implement relatedWhere(), which should return
6  *    'tablename' => array of ids
7  *
8  * 
9  */
10 require_once 'DB/DataObject.php';
11
12 class Pman_Core_DataObjects_Events extends DB_DataObject 
13 {
14     ###START_AUTOCODE
15     /* the code below is auto generated do not remove the above tag */
16
17     public $__table = 'Events';                          // table name
18     public $id;                              // int(11)  not_null primary_key auto_increment
19     public $person_name;                     // string(128)  
20     public $event_when;                      // datetime(19)  binary
21     public $action;                          // string(32)  
22     public $ipaddr;                          // string(16)  
23     public $on_id;                           // int(11)  
24     public $on_table;                        // string(64)  
25     public $person_id;                       // int(11)  
26     public $remarks;                         // blob(65535)  blob
27     public $person_table;                    // string(64)
28     
29     /* the code above is auto generated do not remove the tag below */
30     ###END_AUTOCODE
31     
32     
33     
34     
35     //  ------------ROO HOOKS------------------------------------
36     function applyFilters($q, $au ,$roo)
37     {
38         
39         
40             
41     }
42       
43     
44     
45     /**
46      * check who is trying to access this. false == access denied..
47      * @return {boolean} true if access is allowed.
48      */
49     function checkPerm($lvl, $au) 
50     {
51         if ($lvl == 'S') {
52             return true;
53         }
54         // listing is controleed by applyfilters..
55         return $au->hasPerm("Admin.Admin_Tab", 'S');
56     }
57     /**
58      * object :
59      * return the object that this relates to.
60      * 
61      * @return {DB_DataObject} related object
62      */
63     function object()
64     {
65         $o = DB_DataObject::factory($this->on_table);
66         $o->get($this->on_id);
67         return $o;
68         
69     }
70     
71     
72     /**
73      * init:
74      * Initialize an event - ready to insert..
75      * 
76      * @param {String} action  - group/name of event
77      * @param {DataObject|false} obj - dataobject action occured on.
78      * @param {String} any remarks 
79      */
80     
81     function init($act, $obj, $remarks)
82     {
83         $ff = HTML_FlexyFramework::get();
84         $pg = $ff->page;
85         $au = $pg->getAuthUser();
86         
87         if ($ff->cli) { // && empty($au) && isset($obj->person_id)) {
88             $au = false;
89            // $au = DB_DataObject::Factory('Person'); // not always a person..
90            // $au->get($obj->person_id);
91         } 
92           
93         $this->person_name = $au && !empty($au->name) ? $au->name : '';
94         $this->person_id = $au ? $au->id : -1;
95         $this->person_table = $au ? $au->tableName() : '';
96         $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
97         $this->action = $act;
98         $this->on_table = $obj ? $obj->tableName() : '';
99         $pk = $obj ? $obj->keys()  : false;
100         $this->on_id  = $obj && $pk ? $obj->{$pk[0]}: 0;
101         $rem  = array();
102         // should this really go in remarks? - 
103         if ($obj && method_exists($obj,'toEventString')) {
104             $rem[] = $obj->toEventString() ;
105         }
106         $rem[] = $remarks;
107         $this->remarks = implode(' : ', $rem);
108     }
109     
110     /**
111      * Generate an audit for this field.
112      *
113      * @param {DB_DataObject} new data
114      * @param {DB_DataObject} old data
115      * 
116      * @return {int} number of entries logged.
117      */
118     
119     function audit($new, $old = false)
120     {
121         if ($old == $new) {
122             return 0; // they are the same...
123         }
124          
125         $ret = 0;
126         foreach(array_keys($new->table()) as $k) {
127             // should we JSON serialize this?
128             $n = empty($new->$k) ? '' : $new->$k;
129             $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
130             if ($n == $o) {
131                 continue;
132             }
133             $this->auditField($k, $o, $n, $old);
134             $ret++;
135         }
136         return $ret;
137     }
138     /**
139      * Record an audited change, in theory so we can audit data that is not just
140      * database Fields...
141      *
142      * @param {string} $name    table field anme
143      * @param {mixed} $ov  old value
144      * @param {mixed} $onv  new value
145      * @param {mixed} $old  old object (false if we are creating..)
146      */
147     function auditField($name, $ov, $nv, $old=false )
148     {
149         // hack..
150         if (is_object($nv)) {
151             return;
152         
153         }
154         
155         $x = DB_DataObject::factory('core_event_audit');
156         $x->setFrom(array(
157             'event_id' => $this->id,
158             'name' => $name,
159             'old_audit_id' => $old ? $x->findLast($this, $name) : 0,
160             'newvalue' => $nv
161
162         ));
163         $x->insert();
164     
165     }
166     
167     
168     
169     function onInsert($request,$roo)
170     {
171         $this->writeEventLog();
172     }
173     
174     function writeEventLog()
175     {
176         $ff  = HTML_FlexyFramework::get();
177         if (empty($ff->Pman['event_log_dir'])) {
178             return false;
179         }
180         
181         // add user (eg. www-data or local user if not..)
182         if (function_exists('posix_getpwuid')) {
183             $uinfo = posix_getpwuid( posix_getuid () ); 
184          
185             $user = $uinfo['name'];
186         } else {
187             $user = getenv('USERNAME'); // windows.
188         }
189         //print_r($this);
190         $file = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/'). $this->id . ".json";
191         if (!file_exists(dirname($file))) {
192             mkdir(dirname($file),0700,true);
193         }
194         
195         // Remove all the password from logs...
196         $p =  empty($_POST) ? array() : $_POST;
197         foreach(array('passwd', 'password', 'passwd2', 'password2') as $rm) {
198             if (isset($p[$rm])) {
199                 $p['passwd'] = '******';
200             }
201         }
202         $i=0;
203         $files = array();
204         foreach ($_FILES as $k=>$f){
205             if (empty($f['tmp_name']) || !file_exists($f['tmp_name'])) {
206                 continue;
207             }
208             $i++;
209             $files[$k] = $f;
210             $files[$k]['tmp_name'] = $this->id . '.file_'. $i.'.jpg';
211             $nf = $ff->Pman['event_log_dir']. '/'. $this->id . ".file_$i.jpg";
212             if (!copy($f['tmp_name'], $nf)) {
213                 print_r("failed to copy {$f['tmp_name']}...\n");
214             }
215         }
216         
217         file_put_contents($file, json_encode(array(
218             'REQUEST_URI' => empty($_SERVER['REQUEST_URI']) ? 'cli' : $_SERVER['REQUEST_URI'],
219             'GET' => empty($_GET) ? array() : $_GET,
220             'POST' =>$p,
221             'FILES' => $files,
222         )));
223         
224     }
225     
226     
227 }