3 trait Pman_Core_RooTrait {
5 var $validTables = false;
11 var $debugEnabled = true;
15 if (isset($this->_hasInit)) {
19 $this->_hasInit = true;
21 $boot = HTML_FlexyFramework::get();
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'];
32 if (!empty($ff->Pman['local_autoauth']) &&
33 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
34 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
41 function checkDebug($req = false)
43 $req = $req === false ? $_REQUEST : $req;
44 if (isset($req['_debug'])
50 method_exists($this->authUser,'canDebug')
52 $this->authUser->canDebug()
57 method_exists($this->authUser,'groups')
59 is_a($this->authUser, 'Pman_Core_DataObjects_Person')
61 in_array('Administrators', $this->authUser->groups('name'))
66 DB_DAtaObject::debuglevel((int)$req['_debug']);
71 function dataObject($tab)
73 if (is_array($this->validTables) && !in_array($tab, $this->validTables)) {
74 $this->jerr("Invalid url - not listed in validTables");
77 $tab = str_replace('/', '',$tab); // basic protection??
79 $x = DB_DataObject::factory($tab);
81 if (!is_a($x, 'DB_DataObject')) {
82 $this->jerr('invalid url - no dataobject');
88 function selectSingle($x, $id, $req=false)
90 $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
92 if (!is_array($id) && empty($id)) {
94 if (method_exists($x, 'toRooSingleArray')) {
95 $this->jok($x->toRooSingleArray($this->getAuthUser(), $req));
98 if (method_exists($x, 'toRooArray')) {
99 $this->jok($x->toRooArray($req));
102 $this->jok($x->toArray());
105 $this->loadMap($x, array(
106 'columns' => $_columns,
109 if ($req !== false) {
110 $this->setFilters($x, $req);
115 $x->setFrom($req['lookup'] );
117 if (!$x->find(true)) {
118 if (!empty($id['_id'])) {
120 $this->jok($x->toArray());
125 } else if (!$x->get($id)) {
126 $this->jerr("selectSingle: no such record ($id)");
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");
133 // different symantics on all these calls??
134 if (method_exists($x, 'toRooSingleArray')) {
135 $this->jok($x->toRooSingleArray($this->getAuthUser(), $req));
137 if (method_exists($x, 'toRooArray')) {
138 $this->jok($x->toRooArray($req));
141 $this->jok($x->toArray());
148 function loadMap($do, $cfg =array())
150 $onlycolumns = !empty($cfg['columns']) ? $cfg['columns'] : false;
151 $distinct = !empty($cfg['distinct']) ? $cfg['distinct'] : false;
152 $excludecolumns = !empty($cfg['exclude']) ? $cfg['exclude'] : array();
154 $excludecolumns[] = 'passwd'; // we never expose passwords
158 $ret = $do->autoJoin(array(
159 'include' => $onlycolumns,
160 'exclude' => $excludecolumns,
161 'distinct' => $distinct
164 $this->countWhat = $ret['count'];
165 $this->cols = $ret['cols'];
166 $this->colsJname = $ret['join_names'];
172 function setFilters($x, $q)
174 if (method_exists($x, 'applyFilters')) {
175 // DB_DataObject::debugLevel(1);
176 if (false === $x->applyFilters($q, $this->getAuthUser(), $this)) {
180 $q_filtered = array();
183 // var_dump($keys);exit;
184 foreach($q as $key=>$val) {
186 if (in_array($key,$keys) && !is_array($val)) {
191 // handles name[]=fred&name[]=brian => name in ('fred', 'brian').
192 // value is an array..
193 if (is_array($val) ) {
197 if ($key[0] == '!') {
199 $key = substr($key,1);
202 if (!in_array( $key, array_keys($this->cols))) {
206 // support a[0] a[1] ..... => whereAddIn(
209 foreach($val as $k=>$v) {
210 if (!is_numeric($k)) {
214 // FIXME: note this is not typesafe for anything other than mysql..
216 if (!is_numeric($v) || !is_long($v)) {
225 $x->whereAddIn($pref . (
226 isset($this->colsJname[$key]) ?
227 $this->colsJname[$key] :
228 ($x->tableName(). '.'.$key)),
229 $ar, $quote ? 'string' : 'int');
236 // handles !name=fred => name not equal fred.
237 if ($key[0] == '!' && in_array(substr($key, 1), array_keys($this->cols))) {
239 $key = substr($key, 1) ;
242 isset($this->colsJname[$key]) ?
243 $this->colsJname[$key] :
244 $x->tableName(). '.'.$key ) . ' != ' .
245 (is_numeric($val) ? $val : "'". $x->escape($val) . "'")
253 // Events and remarks -- fixme - move to events/remarsk...
254 case 'on_id': // where TF is this used...
255 if (!empty($q['query']['original'])) {
256 // DB_DataObject::debugLevel(1);
257 $o = (int) $q['query']['original'];
259 $x->whereAdd("(on_id = $oid OR
260 on_id IN ( SELECT distinct(id) FROM Documents WHERE original = $o )
269 if (strlen($val) && $key[0] != '_') {
270 $q_filtered[$key] = $val;
273 // subjoined columns = check the values.
274 // note this is not typesafe for anything other than mysql..
276 if (isset($this->colsJname[$key])) {
278 if (!is_numeric($val) || !is_long($val)) {
281 $x->whereAdd( "{$this->colsJname[$key]} = " . ($quote ? "'". $x->escape($val) ."'" : $val));
289 if (!empty($q_filtered)) {
290 $x->setFrom($q_filtered);
293 if (!empty($q['query']['name'])) {
294 if (in_array( 'name', array_keys($x->table()))) {
295 $x->whereAdd($x->tableName().".name LIKE '". $x->escape($q['query']['name']) . "%'");
306 static $permitError = false;
308 function onPearError($err)
310 static $reported = false;
315 if (Pman::$permitError) {
322 $out = $err->toString();
327 foreach($err->backtrace as $b) {
328 $ret[] = @$b['file'] . '(' . @$b['line'] . ')@' . @$b['class'] . '::' . @$b['function'];
334 //convert the huge backtrace into something that is readable..
335 $out .= "\n" . implode("\n", $ret);
343 function addEvent($act, $obj = false, $remarks = '')
345 if (!empty(HTML_FlexyFramework::get()->Pman['disable_events'])) {
349 $e = DB_DataObject::factory('Events');
350 $e->init($act,$obj,$remarks);
352 $e->event_when = date('Y-m-d H:i:s');
356 // fixme - this should be in onInsert..
357 $wa = DB_DataObject::factory('core_watch');
358 if (method_exists($wa,'notifyEvent')) {
359 $wa->notifyEvent($e); // trigger any actions..
362 $e->onInsert(isset($_REQUEST) ? $_REQUEST : array() , $this);
368 function checkPerm($obj, $lvl, $req= null)
370 if (!method_exists($obj, 'checkPerm')) {
373 if ($obj->checkPerm($lvl, $this->getAuthUser(), $req)) {
380 function hasPerm($name, $lvl) // do we have a permission
382 static $pcache = array();
383 $au = $this->getAuthUser();
384 return $au && $au->hasPerm($name, $lvl);
388 function getAuthUser()
390 die('Get auth user is not implement.');