typo
[Pman.Core] / RooTrait.php
1 <?php
2
3 trait Pman_Core_RooTrait {
4     
5     var $validTables = false; 
6     
7     var $key;
8     
9     var $transObj = false;
10     
11     var $debugEnabled = true;
12     
13     function init() 
14     {
15         if (isset($this->_hasInit)) {
16             return;
17         }
18         
19         $this->_hasInit = true;
20         
21         $boot = HTML_FlexyFramework::get();
22         
23         $this->appName= $boot->appName;
24         $this->appNameShort= $boot->appNameShort;
25         $this->appModules= $boot->enable;
26         $this->isDev = empty($boot->Pman['isDev']) ? false : $boot->Pman['isDev'];
27         $this->appDisable = $boot->disable;
28         $this->appDisabled = explode(',', $boot->disable);
29         $this->version = $boot->version; 
30         $this->uiConfig = empty($boot->Pman['uiConfig']) ? false : $boot->Pman['uiConfig']; 
31         
32         if (!empty($ff->Pman['local_autoauth']) && 
33             ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
34             ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') 
35         ) {
36             $this->isDev = true;
37         }
38         
39     }
40     
41     function checkDebug($req = false)
42     {
43         $req =  $req === false  ? $_REQUEST : $req;
44         if (isset($req['_debug']) 
45                 && 
46                 $this->authUser
47                 &&
48                 (
49                     (
50                         method_exists($this->authUser,'canDebug')
51                         &&
52                         $this->authUser->canDebug()
53                     )
54                 ||
55                     (  
56                     
57                         method_exists($this->authUser,'groups') 
58                         &&
59                         is_a($this->authUser, 'Pman_Core_DataObjects_Person')
60                         &&
61                         in_array('Administrators', $this->authUser->groups('name'))
62                     )
63                 )
64                 
65             ){
66             DB_DAtaObject::debuglevel((int)$req['_debug']);
67         }
68         
69     }
70     
71     function dataObject($tab)
72     {
73         if (is_array($this->validTables) &&  !in_array($tab, $this->validTables)) {
74             $this->jerr("Invalid url - not listed in validTables");
75         }
76         
77         $tab = str_replace('/', '',$tab); // basic protection??
78         
79         $x = DB_DataObject::factory($tab);
80         
81         if (!is_a($x, 'DB_DataObject')) {
82             $this->jerr('invalid url - no dataobject');
83         }
84     
85         return $x;
86     }
87     
88     function selectSingle($x, $id, $req=false)
89     {
90         $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
91
92         if (!is_array($id) && empty($id)) {
93             
94             if (method_exists($x, 'toRooSingleArray')) {
95                 $this->jok($x->toRooSingleArray($this->authUser, $req));
96             }
97             
98             if (method_exists($x, 'toRooArray')) {
99                 $this->jok($x->toRooArray($req));
100             }
101             
102             $this->jok($x->toArray());
103         }
104         
105         $this->loadMap($x, array(
106             'columns' => $_columns,
107         ));
108         
109         if ($req !== false) { 
110             $this->setFilters($x, $req);
111         }
112         
113         if (is_array($id)) {
114             // lookup...
115             $x->setFrom($req['lookup'] );
116             $x->limit(1);
117             if (!$x->find(true)) {
118                 if (!empty($id['_id'])) {
119                     // standardize this?
120                     $this->jok($x->toArray());
121                 }
122                 $this->jok(false);
123             }
124             
125         } else if (!$x->get($id)) {
126             $this->jerr("selectSingle: no such record ($id)");
127         }
128         
129         // ignore perms if comming from update/insert - as it's already done...
130         if ($req !== false && !$this->checkPerm($x,'S'))  {
131             $this->jerr("PERMISSION DENIED - si");
132         }
133         // different symantics on all these calls??
134         if (method_exists($x, 'toRooSingleArray')) {
135             $this->jok($x->toRooSingleArray($this->authUser, $req));
136         }
137         if (method_exists($x, 'toRooArray')) {
138             $this->jok($x->toRooArray($req));
139         }
140         
141         $this->jok($x->toArray());
142         
143         
144     }
145     
146     var $cols = array();
147     
148     function loadMap($do, $cfg =array())
149     {
150         $onlycolumns    = !empty($cfg['columns']) ? $cfg['columns'] : false;
151         $distinct       = !empty($cfg['distinct']) ? $cfg['distinct'] : false;
152         $excludecolumns = !empty($cfg['exclude']) ? $cfg['exclude'] : array();
153           
154         $excludecolumns[] = 'passwd'; // we never expose passwords
155         
156         $ret = $do->autoJoin(array(
157             'include' => $onlycolumns,
158             'exclude' => $excludecolumns,
159             'distinct' => $distinct
160         ));
161         
162         $this->countWhat = $ret['count'];
163         $this->cols = $ret['cols'];
164         $this->colsJname = $ret['join_names'];
165         
166         return;
167         
168     }
169     
170     function setFilters($x, $q)
171     {
172         if (method_exists($x, 'applyFilters')) {
173            // DB_DataObject::debugLevel(1);
174             if (false === $x->applyFilters($q, $this->authUser, $this)) {
175                 return; 
176             } 
177         }
178         $q_filtered = array();
179         
180         $keys = $x->keys();
181         // var_dump($keys);exit;
182         foreach($q as $key=>$val) {
183             
184             if (in_array($key,$keys) && !is_array($val)) {
185                
186                 $x->$key  = $val;
187             }
188             
189              // handles name[]=fred&name[]=brian => name in ('fred', 'brian').
190             // value is an array..
191             if (is_array($val) ) {
192                 
193                 $pref = '';
194                 
195                 if ($key[0] == '!') {
196                     $pref = '!';
197                     $key = substr($key,1);
198                 }
199                 
200                 if (!in_array( $key,  array_keys($this->cols))) {
201                     continue;
202                 }
203                 
204                 // support a[0] a[1] ..... => whereAddIn(
205                 $ar = array();
206                 $quote = false;
207                 foreach($val as $k=>$v) {
208                     if (!is_numeric($k)) {
209                         $ar = array();
210                         break;
211                     }
212                     // FIXME: note this is not typesafe for anything other than mysql..
213                     
214                     if (!is_numeric($v) || !is_long($v)) {
215                         $quote = true;
216                     }
217                     $ar[] = $v;
218                     
219                 }
220                 if (count($ar)) {
221                     
222                     
223                     $x->whereAddIn($pref . (
224                         isset($this->colsJname[$key]) ? 
225                             $this->colsJname[$key] :
226                             ($x->tableName(). '.'.$key)),
227                         $ar, $quote ? 'string' : 'int');
228                 }
229                 
230                 continue;
231             }
232             
233             
234             // handles !name=fred => name not equal fred.
235             if ($key[0] == '!' && in_array(substr($key, 1), array_keys($this->cols))) {
236                 
237                 $key  = substr($key, 1) ;
238                 
239                 $x->whereAdd(   (
240                         isset($this->colsJname[$key]) ? 
241                             $this->colsJname[$key] :
242                             $x->tableName(). '.'.$key ) . ' != ' .
243                     (is_numeric($val) ? $val : "'".  $x->escape($val) . "'")
244                 );
245                 continue;
246                 
247             }
248
249             switch($key) {
250                     
251                 // Events and remarks -- fixme - move to events/remarsk...
252                 case 'on_id':  // where TF is this used...
253                     if (!empty($q['query']['original'])) {
254                       //  DB_DataObject::debugLevel(1);
255                         $o = (int) $q['query']['original'];
256                         $oid = (int) $val;
257                         $x->whereAdd("(on_id = $oid  OR 
258                                 on_id IN ( SELECT distinct(id) FROM Documents WHERE original = $o ) 
259                             )");
260                         continue 2;
261                                 
262                     }
263                     $x->on_id = $val;
264                 
265                 
266                 default:
267                     if (strlen($val) && $key[0] != '_') {
268                         $q_filtered[$key] = $val;
269                     }
270                     
271                     // subjoined columns = check the values.
272                     // note this is not typesafe for anything other than mysql..
273                     
274                     if (isset($this->colsJname[$key])) {
275                         $quote = false;
276                         if (!is_numeric($val) || !is_long($val)) {
277                             $quote = true;
278                         }
279                         $x->whereAdd( "{$this->colsJname[$key]} = " . ($quote ? "'". $x->escape($val) ."'" : $val));
280                         
281                     }
282                     
283                     
284                     continue 2;
285             }
286         }
287         if (!empty($q_filtered)) {
288             $x->setFrom($q_filtered);
289         }
290         
291         if (!empty($q['query']['name'])) {
292             if (in_array( 'name',  array_keys($x->table()))) {
293                 $x->whereAdd($x->tableName().".name LIKE '". $x->escape($q['query']['name']) . "%'");
294             }
295         }
296         
297     }
298     
299     
300     /*
301      * From Pman.php
302      */
303     
304     static $permitError = false;
305     
306     function onPearError($err)
307     {
308         static $reported = false;
309         if ($reported) {
310             return;
311         }
312         
313         if (self::$permitError) {
314              
315             return;
316             
317         }
318         
319         $reported = true;
320         $out = $err->toString();
321         
322         $ret = array();
323         $n = 0;
324         
325         foreach($err->backtrace as $b) {
326             $ret[] = @$b['file'] . '(' . @$b['line'] . ')@' .   @$b['class'] . '::' . @$b['function'];
327             if ($n > 20) {
328                 break;
329             }
330             $n++;
331         }
332         //convert the huge backtrace into something that is readable..
333         $out .= "\n" . implode("\n",  $ret);
334      
335         print_R($out);exit;
336         
337         $this->jerr($out);
338         
339     }
340     
341     function addEvent($act, $obj = false, $remarks = '') 
342     {
343         if (!empty(HTML_FlexyFramework::get()->Pman['disable_events'])) {
344             return;
345         }
346         
347         $e = DB_DataObject::factory('Events');
348         $e->init($act,$obj,$remarks); 
349          
350         $e->event_when = date('Y-m-d H:i:s');
351         
352         $eid = $e->insert();
353         
354         // fixme - this should be in onInsert..
355         $wa = DB_DataObject::factory('core_watch');
356         if (method_exists($wa,'notifyEvent')) {
357             $wa->notifyEvent($e); // trigger any actions..
358         }
359         
360         $e->onInsert(isset($_REQUEST) ? $_REQUEST : array() , $this);
361         
362         return $e;
363         
364     }
365     
366     function checkPerm($obj, $lvl, $req= null)
367     {
368         if (!method_exists($obj, 'checkPerm')) {
369             return true;
370         }
371         if ($obj->checkPerm($lvl, $this->authUser, $req))  {
372             return true;
373         }
374         
375         return false;
376     }
377     
378     function hasPerm($name, $lvl)  // do we have a permission
379     {
380         static $pcache = array();
381         $au = $this->getAuthUser();
382         return $au && $au->hasPerm($name, $lvl);
383         
384     }
385     
386     function getAuthUser()
387     {
388         die('Get auth user is not implement.');
389     }
390     
391 }