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 checkDebugPost()
73 return (!empty($_GET['_post']) || !empty($_GET['_debug_post'])) &&
75 method_exists($this->authUser,'groups') &&
76 in_array('Administrators', $this->authUser->groups('name'));
80 function dataObject($tab)
82 if (is_array($this->validTables) && !in_array($tab, $this->validTables)) {
83 $this->jerr("Invalid url - not listed in validTables");
86 $tab = str_replace('/', '',$tab); // basic protection??
88 $x = DB_DataObject::factory($tab);
90 if (!is_a($x, 'DB_DataObject')) {
91 $this->jerr('invalid url - no dataobject');
101 static $permitError = false;
103 function onPearError($err)
105 static $reported = false;
110 if (Pman::$permitError) {
117 $out = $err->toString();
122 foreach($err->backtrace as $b) {
123 $ret[] = @$b['file'] . '(' . @$b['line'] . ')@' . @$b['class'] . '::' . @$b['function'];
129 //convert the huge backtrace into something that is readable..
130 $out .= "\n" . implode("\n", $ret);
140 if ($this->transObj ) {
141 $this->transObj->query( connection_aborted() ? 'ROLLBACK' : 'COMMIT');
144 $cli = HTML_FlexyFramework::get()->cli;
147 echo "OK: " .$str . "\n";
150 require_once 'Services/JSON.php';
151 $json = new Services_JSON();
153 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
154 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
157 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
161 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
165 header('Content-type: text/html');
166 echo "<HTML><HEAD></HEAD><BODY>";
167 // encode html characters so they can be read..
168 echo str_replace(array('<','>'), array('\u003c','\u003e'),
169 $json->encodeUnsafe(array('success'=> true, 'data' => $str)));
170 echo "</BODY></HTML>";
175 echo $json->encode(array('success'=> true, 'data' => $str));
181 function jerr($str, $errors=array(), $content_type = false)
183 if ($this->transObj) {
184 $this->transObj->query('ROLLBACK');
187 return $this->jerror('ERROR', $str,$errors,$content_type);
190 function jerror($type, $str, $errors=array(), $content_type = false) // standard error reporting..
192 if ($type !== false) {
193 $this->addEvent($type, false, $str);
196 $cli = HTML_FlexyFramework::get()->cli;
198 echo "ERROR: " .$str . "\n";
203 if ($content_type == 'text/plain') {
204 header('Content-Disposition: attachment; filename="error.txt"');
205 header('Content-type: '. $content_type);
206 echo "ERROR: " .$str . "\n";
210 require_once 'Services/JSON.php';
211 $json = new Services_JSON();
213 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
214 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
217 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
221 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
225 header('Content-type: text/html');
226 echo "<HTML><HEAD></HEAD><BODY>";
227 echo $json->encodeUnsafe(array(
230 'message' => $str, // compate with exeption / loadexception.
232 'errors' => $errors ? $errors : true, // used by forms to flag errors.
233 'authFailure' => !empty($errors['authFailure']),
235 echo "</BODY></HTML>";
239 if (isset($_REQUEST['_debug'])) {
240 echo '<PRE>'.htmlspecialchars(print_r(array(
244 'message' => $str, // compate with exeption / loadexception.
245 'errors' => $errors ? $errors : true, // used by forms to flag errors.
246 'authFailure' => !empty($errors['authFailure']),
252 echo $json->encode(array(
256 'message' => $str, // compate with exeption / loadexception.
257 'errors' => $errors ? $errors : true, // used by forms to flag errors.
258 'authFailure' => !empty($errors['authFailure']),
265 function jdata($ar,$total=false, $extra=array(), $cachekey = false)
267 // should do mobile checking???
268 if ($total == false) {
271 $extra= $extra ? $extra : array();
272 require_once 'Services/JSON.php';
273 $json = new Services_JSON();
275 $retHTML = isset($_SERVER['CONTENT_TYPE']) &&
276 preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']);
279 if (isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] == 'NO') {
283 $retHTML = isset($_REQUEST['returnHTML']) && $_REQUEST['returnHTML'] !='NO';
288 header('Content-type: text/html');
289 echo "<HTML><HEAD></HEAD><BODY>";
290 // encode html characters so they can be read..
291 echo str_replace(array('<','>'), array('\u003c','\u003e'),
292 $json->encodeUnsafe(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra));
293 echo "</BODY></HTML>";
298 // see if trimming will help...
299 if (!empty($_REQUEST['_pman_short'])) {
302 foreach($ar as $as) {
304 foreach($as as $k=>$v) {
305 if (is_string($v) && !strlen(trim($v))) {
317 $ret = $json->encode(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra);
319 if (!empty($cachekey)) {
321 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
322 if (!file_exists(dirname($fn))) {
323 mkdir(dirname($fn), 0777,true);
325 file_put_contents($fn, $ret);
331 /** a daily cache **/
332 function jdataCache($cachekey)
334 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
335 if (file_exists($fn)) {
336 header('Content-type: application/json');
337 echo file_get_contents($fn);
344 function addEvent($act, $obj = false, $remarks = '')
347 if (!empty(HTML_FlexyFramework::get()->Pman['disable_events'])) {
351 $au = $this->getAuthUser();
353 $e = DB_DataObject::factory('Events');
354 $e->init($act,$obj,$remarks);
356 $e->event_when = date('Y-m-d H:i:s');
360 // fixme - this should be in onInsert..
361 $wa = DB_DataObject::factory('core_watch');
362 if (method_exists($wa,'notifyEvent')) {
363 $wa->notifyEvent($e); // trigger any actions..
367 $e->onInsert(isset($_REQUEST) ? $_REQUEST : array() , $this);
373 function getAuthUser()
375 if (!empty($this->authUser)) {
376 return $this->authUser;
378 $ff = HTML_FlexyFramework::get();
379 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
381 $u = DB_DataObject::factory( $tbl );
385 $this->authUser =$u->getAuthUser();
386 return $this->authUser ;