3 * Table Definition for Events
5 * objects can implement relatedWhere(), which should return
6 * 'tablename' => array of ids
10 require_once 'DB/DataObject.php';
12 class Pman_Core_DataObjects_Events extends DB_DataObject
15 /* the code below is auto generated do not remove the above tag */
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)
29 /* the code above is auto generated do not remove the tag below */
35 // ------------ROO HOOKS------------------------------------
36 function applyFilters($q, $au ,$roo)
46 * check who is trying to access this. false == access denied..
47 * @return {boolean} true if access is allowed.
49 function checkPerm($lvl, $au)
54 // listing is controleed by applyfilters..
55 return $au->hasPerm("Admin.Admin_Tab", 'S');
59 * return the object that this relates to.
61 * @return {DB_DataObject} related object
65 $o = DB_DataObject::factory($this->on_table);
66 $o->get($this->on_id);
74 * Initialize an event - ready to insert..
76 * @param {String} action - group/name of event
77 * @param {DataObject|false} obj - dataobject action occured on.
78 * @param {String} any remarks
81 function init($act, $obj, $remarks)
83 $ff = HTML_FlexyFramework::get();
85 $au = $pg->getAuthUser();
87 if ($ff->cli) { // && empty($au) && isset($obj->person_id)) {
89 // $au = DB_DataObject::Factory('Person'); // not always a person..
90 // $au->get($obj->person_id);
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';
98 $this->on_table = $obj ? $obj->tableName() : '';
99 $pk = $obj ? $obj->keys() : false;
100 $this->on_id = $obj && $pk ? $obj->{$pk[0]}: 0;
102 // should this really go in remarks? -
103 if ($obj && method_exists($obj,'toEventString')) {
104 $rem[] = $obj->toEventString() ;
107 $this->remarks = implode(' : ', $rem);
111 * Generate an audit for this field.
113 * @param {DB_DataObject} new data
114 * @param {DB_DataObject} old data
116 * @return {int} number of entries logged.
119 function audit($new, $old = false)
122 return 0; // they are the same...
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;
133 $this->auditField($k, $o, $n, $old);
139 * Record an audited change, in theory so we can audit data that is not just
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..)
147 function auditField($name, $ov, $nv, $old=false )
150 if (is_object($nv)) {
155 $x = DB_DataObject::factory('core_event_audit');
157 'event_id' => $this->id,
159 'old_audit_id' => $old ? $x->findLast($this, $name) : 0,
169 function onInsert($request,$roo)
171 $this->writeEventLog();
174 function writeEventLog()
176 $ff = HTML_FlexyFramework::get();
177 if (empty($ff->Pman['event_log_dir'])) {
181 // add user (eg. www-data or local user if not..)
182 if (function_exists('posix_getpwuid')) {
183 $uinfo = posix_getpwuid( posix_getuid () );
185 $user = $uinfo['name'];
187 $user = getenv('USERNAME'); // windows.
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);
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'] = '******';
204 foreach ($_FILES as $k=>$f){
205 if (empty($f['tmp_name']) || !file_exists($f['tmp_name'])) {
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");
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,