6 * - base application setup (variables etc to javascript)
8 * - authentication and permission info about user / application
9 * - json output methods.
10 * - file upload error checking - checkFileUploadError
11 * - logging to event table
12 * - sendTemplate code (normally use the Person version for sending to specific people..)
14 * - doc managment code?? - remarks and tracking??? - MOVEME
15 * - authentication link checking?? MOVEME?
16 * - authentication reset password ?? MOVEME?
17 * ?? arrayClean.. what's it doing here?!? ;)
20 * DB_DataObject*:*toEventString (for logging - this is generically prefixed to all database operations.)
21 * - any data object where this method exists, the result will get prefixed to the log remarks
24 class Pman extends HTML_FlexyFramework_Page
28 var $appShortName= "";
29 var $appVersion = "1.8";
36 var $appDisabled = array(); // array of disabled modules..
37 // (based on config option disable)
39 var $authUser; // always contains the authenticated user..
41 var $disable_jstemplate = false; /// disable inclusion of jstemplate code..
45 * ------------- Standard getAuth/get/post methods of framework.
50 function getAuth() // everyone allowed in!!!!!
52 $this->loadOwnerCompany();
60 if (isset($this->_hasInit)) {
63 $this->_hasInit = true;
64 // move away from doing this ... you can access bootLoader.XXXXXX in the master template..
65 $boot = HTML_FlexyFramework::get();
66 // echo'<PRE>';print_R($boot);exit;
67 $this->appName= $boot->appName;
68 $this->appNameShort= $boot->appNameShort;
71 $this->appModules= $boot->enable;
72 $this->isDev = empty($boot->Pman['isDev']) ? false : $boot->Pman['isDev'];
73 $this->appDisable = $boot->disable;
74 $this->appDisabled = explode(',', $boot->disable);
75 $this->version = $boot->version;
76 $this->uiConfig = empty($boot->Pman['uiConfig']) ? false : $boot->Pman['uiConfig'];
78 if (!empty($ff->Pman['local_autoauth']) &&
79 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
80 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
84 //var_dump($this->appModules);
85 foreach(explode(',',$this->appModules) as $m) {
86 $cls = 'Pman_'. $m . '_Pman';
88 //echo $this->rootDir . '/'.str_replace('_','/', $cls). '.php';
90 if (!file_exists($this->rootDir . '/'.str_replace('_','/', $cls). '.php')) {
93 require_once str_replace('_','/', $cls). '.php';
95 if (method_exists($c,'init')) {
106 //$this->allowSignup= empty($opts['allowSignup']) ? 0 : 1;
107 $bits = explode('/', $base);
109 if ($bits[0] == 'Link') {
110 $this->linkFail = $this->linkAuth(@$bits[1],@$bits[2]);
111 header('Content-type: text/html; charset=utf-8');
115 // should really be moved to Login...
117 if ($bits[0] == 'PasswordReset') {
118 $this->linkFail = $this->resetPassword(@$bits[1],@$bits[2],@$bits[3]);
119 header('Content-type: text/html; charset=utf-8');
123 $au = $this->getAuthUser();
125 $ff= HTML_FlexyFramework::get();
127 if (!empty($ff->Pman['auth_comptype']) && $au->id > 0 &&
128 ( !$au->company_id || ($ff->Pman['auth_comptype'] != $au->company()->comptype))) {
132 $this->jerr("Login not permited to outside companies - please reload");
134 $this->addEvent("RELOAD");
139 $this->addEvent("BADURL", false, $base);
140 $this->jerr("invalid url");
143 if (isset($_GET['onloadTrack'])) {
144 $this->onloadTrack = (int)$_GET['onloadTrack'];
146 // getting this to work with xhtml is a nightmare
147 // = nbsp / <img> issues screw everyting up.
148 //var_dump($this->isDev);
149 // force regeneration on load for development enviroments..
151 HTML_FlexyFramework::get()->generateDataobjectsCache($this->isDev);
153 //header('Content-type: application/xhtml+xml; charset=utf-8');
157 if ($this->company && $this->company->logo_id) {
158 $im = DB_DataObject::Factory('Images');
159 $im->get($this->company->logo_id);
160 $this->appLogo = $this->baseURL . '/Images/Thumb/300x100/'. $this->company->logo_id .'/' . $im->filename;
163 header('Content-type: text/html; charset=utf-8');
166 function post($base) {
167 return $this->get($base);
171 // --------------- AUTHENTICATION or system information
174 * finds the compay with comptype=='OWNER'
176 * @return {Pman_Core_DataObjects_Companies} the owner company
178 function loadOwnerCompany()
180 // only applies if authtable is person..
181 $ff = HTML_FlexyFramework::get();
182 if (!empty($ff->Pman['authTable']) && $ff->Pman['authTable'] != 'Person') {
186 $this->company = DB_DataObject::Factory('Companies');
187 if (!is_a($this->company, 'DB_DataObject')) { // non-core pman projects
190 $this->company->get('comptype', 'OWNER');
191 return $this->company;
197 * getAuthUser: - get the authenticated user..
199 * @return {DB_DataObject} of type Pman[authTable] if authenticated.
202 function getAuthUser()
204 if (!empty($this->authUser)) {
205 return $this->authUser;
207 $ff = HTML_FlexyFramework::get();
208 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
210 $u = DB_DataObject::factory( $tbl );
214 $this->authUser =$u->getAuthUser();
215 return $this->authUser ;
219 * wrapper arround authuser->hasPerm
220 * @see Pman_Core_DataObjects_User::hasPerm
222 * @param {String} $name The permission name (eg. Projects.List)
223 * @param {String} $lvl eg. (C)reate (E)dit (D)elete ... etc.
226 function hasPerm($name, $lvl) // do we have a permission
228 static $pcache = array();
229 $au = $this->getAuthUser();
230 return $au && $au->hasPerm($name,$lvl);
235 * modulesList: List the modules in the application
237 * @return {Array} list of modules
239 function modulesList()
241 $boot = HTML_FlexyFramework::get();
242 // echo'<PRE>';print_R($boot);exit;
245 $mods = explode(',', $boot->enable);
246 if (in_array('Core',$mods)) { // core has to be the first modules loaded as it contains Pman.js
247 array_unshift($mods, 'Core');
250 if (in_array($boot->appNameShort,$mods)) { // Project has to be the last modules loaded as it contains Pman.js
251 unset($mods[array_search($boot->appNameShort, $mods)]);
252 $mods[] = $boot->appNameShort;
255 $mods = array_unique($mods);
257 $disabled = explode(',', $boot->disable ? $boot->disable : '');
259 foreach($mods as $mod) {
260 // add the css file..
261 if (in_array($mod, $disabled)) {
272 function hasModule($name)
275 if (!strpos( $name,'.') ) {
276 // use enable / disable..
277 return in_array($name, $this->modules());
280 $x = DB_DataObject::factory('Group_Rights');
281 $ar = $x->defaultPermData();
282 if (empty($ar[$name]) || empty($ar[$name][0])) {
297 * ---------------- Global Tools ---------------
299 function checkFileUploadError() // check for file upload errors.
302 empty($_FILES['File'])
303 || empty($_FILES['File']['name'])
304 || empty($_FILES['File']['tmp_name'])
305 || empty($_FILES['File']['type'])
306 || !empty($_FILES['File']['error'])
307 || empty($_FILES['File']['size'])
309 $this->jerr("File upload error: <PRE>" . print_r($_FILES,true) . print_r($_POST,true) . "</PRE>");
315 * generate a tempory file with an extension (dont forget to delete it)
318 function tempName($ext)
320 $x = tempnam(ini_get('session.save_path'), HTML_FlexyFramework::get()->appNameShort.'TMP');
322 return $x .'.'. $ext;
325 * ------------- Authentication testing ------ ??? MOVEME?
329 function linkAuth($trid, $trkey)
331 $tr = DB_DataObject::factory('Documents_Tracking');
332 if (!$tr->get($trid)) {
333 return "Invalid URL";
335 if (strtolower($tr->authkey) != strtolower($trkey)) {
336 $this->AddEvent("ERROR-L", false, "Invalid Key");
337 return "Invalid KEY";
340 $this->onloadTrack = (int) $tr->doc_id;
341 if (strtotime($tr->date_sent) < strtotime("NOW - 14 DAYS")) {
342 $this->AddEvent("ERROR-L", false, "Key Expired");
343 return "Key Expired";
345 // user logged in and not
346 $au = $this->getAuthUser();
347 if ($au && $au->id && $au->id != $tr->person_id) {
350 return "Logged Out existing Session\n - reload to log in with correct key";
352 if ($au) { // logged in anyway..
353 $this->AddEvent("LOGIN", false, "With Key (ALREADY)");
354 header('Location: ' . $this->baseURL.'?onloadTrack='.$this->onloadTrack);
359 // authenticate the user...
361 $u = DB_DataObject::factory('Person');
363 $u->get($tr->person_id);
365 $this->AddEvent("LOGIN", false, "With Key");
367 // we need to redirect out - otherwise refererer url will include key!
368 header('Location: ' . $this->baseURL.'?onloadTrack='.$this->onloadTrack);
380 * ------------- Authentication password reset ------ ??? MOVEME?
386 function resetPassword($id,$t, $key)
389 $au = $this->getAuthUser();
391 return "Already Logged in - no need to use Password Reset";
394 $u = DB_DataObject::factory('Person');
395 //$u->company_id = $this->company->id;
397 if (!$u->get($id) || !strlen($u->passwd)) {
402 if ($key != $u->genPassKey($t)) {
403 return "invalid key";
406 $u->no_reset_sent = 0;
409 if ($t < strtotime("NOW - 1 DAY")) {
412 $this->showNewPass = implode("/", array($id,$t,$key));
417 * jerrAuth: standard auth failure - with data that let's the UI know..
421 $au = $this->authUser();
423 // is it an authfailure?
424 $this->jerr("Permission denied to view this resource", array('authFailure' => true));
426 $this->jerr("Not authenticated", array('authFailure' => true));
432 * ---------------- Standard JSON outputers. - used everywhere
435 function jerr($str, $errors=array(), $content_type = false) // standard error reporting..
437 $this->addEvent("ERROR", false, $str);
439 $cli = HTML_FlexyFramework::get()->cli;
441 echo "ERROR: " .$str . "\n";
446 if ($content_type == 'text/plain') {
447 header('Content-Disposition: attachment; filename="error.txt"');
448 header('Content-type: '. $content_type);
449 echo "ERROR: " .$str . "\n";
455 require_once 'Services/JSON.php';
456 $json = new Services_JSON();
461 if (!empty($_REQUEST['returnHTML']) ||
462 (isset($_SERVER['CONTENT_TYPE']) && preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']))
464 header('Content-type: text/html');
465 echo "<HTML><HEAD></HEAD><BODY>";
466 echo $json->encodeUnsafe(array(
469 'message' => $str, // compate with exeption / loadexception.
471 'errors' => $errors ? $errors : true, // used by forms to flag errors.
472 'authFailure' => !empty($errors['authFailure']),
474 echo "</BODY></HTML>";
478 if (isset($_REQUEST['_debug'])) {
479 echo '<PRE>'.htmlspecialchars(print_r(array(
483 'message' => $str, // compate with exeption / loadexception.
484 'errors' => $errors ? $errors : true, // used by forms to flag errors.
485 'authFailure' => !empty($errors['authFailure']),
491 echo $json->encode(array(
495 'message' => $str, // compate with exeption / loadexception.
496 'errors' => $errors ? $errors : true, // used by forms to flag errors.
497 'authFailure' => !empty($errors['authFailure']),
506 $cli = HTML_FlexyFramework::get()->cli;
508 echo "OK: " .$str . "\n";
511 require_once 'Services/JSON.php';
512 $json = new Services_JSON();
514 if (!empty($_REQUEST['returnHTML']) ||
515 (isset($_SERVER['CONTENT_TYPE']) && preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']))
518 header('Content-type: text/html');
519 echo "<HTML><HEAD></HEAD><BODY>";
520 // encode html characters so they can be read..
521 echo str_replace(array('<','>'), array('\u003c','\u003e'),
522 $json->encodeUnsafe(array('success'=> true, 'data' => $str)));
523 echo "</BODY></HTML>";
528 echo $json->encode(array('success'=> true, 'data' => $str));
534 * output data for grids or tree
535 * @ar {Array} ar Array of data
536 * @total {Number|false} total number of records (or false to return count(ar)
537 * @extra {Array} extra key value list of data to pass as extra data.
540 function jdata($ar,$total=false, $extra=array(), $cachekey = false)
542 // should do mobile checking???
543 if ($total == false) {
546 $extra= $extra ? $extra : array();
547 require_once 'Services/JSON.php';
548 $json = new Services_JSON();
549 if (isset($_SERVER['CONTENT_TYPE']) && preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE'])) {
551 header('Content-type: text/html');
552 echo "<HTML><HEAD></HEAD><BODY>";
553 // encode html characters so they can be read..
554 echo str_replace(array('<','>'), array('\u003c','\u003e'),
555 $json->encodeUnsafe(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra));
556 echo "</BODY></HTML>";
561 // see if trimming will help...
562 if (!empty($_REQUEST['_pman_short'])) {
565 foreach($ar as $as) {
567 foreach($as as $k=>$v) {
568 if (is_string($v) && !strlen(trim($v))) {
580 $ret = $json->encode(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra);
582 if (!empty($cachekey)) {
584 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
585 if (!file_exists(dirname($fn))) {
586 mkdir(dirname($fn), 0777,true);
588 file_put_contents($fn, $ret);
596 /** a daily cache **/
597 function jdataCache($cachekey)
599 $fn = ini_get('session.save_path') . '/json-cache'.date('/Y/m/d').'.'. $cachekey . '.cache.json';
600 if (file_exists($fn)) {
601 header('Content-type: application/json');
602 echo file_get_contents($fn);
612 * ---------------- OUTPUT
614 function hasBg($fn) // used on front page to check if logos exist..
616 return file_exists($this->rootDir.'/Pman/'.$this->appNameShort.'/templates/images/'. $fn);
619 * outputJavascriptIncludes:
621 * output <script....> for all the modules in the applcaiton
624 function outputJavascriptIncludes()
627 $mods = $this->modulesList();
629 foreach($mods as $mod) {
630 // add the css file..
633 $files = $this->moduleJavascriptList($mod.'/widgets');
634 foreach($files as $f) {
635 echo '<script type="text/javascript" src="'. $f. '"></script>'."\n";
638 $files = $this->moduleJavascriptList($mod);
639 foreach($files as $f) {
640 echo '<script type="text/javascript" src="'. $f. '"></script>'."\n";
644 if (empty($this->disable_jstemplate)) {
645 // and finally the JsTemplate...
646 echo '<script type="text/javascript" src="'. $this->baseURL. '/Core/JsTemplate"></script>'."\n";
653 * output <link rel=stylesheet......> for all the modules in the applcaiton
656 * This could css minify as well.
658 function outputCSSIncludes() // includes on CSS links.
661 $mods = $this->modulesList();
664 foreach($mods as $mod) {
665 // add the css file..
666 $dir = $this->rootDir.'/Pman/'.$mod;
667 $ar = glob($dir . '/*.css');
668 foreach($ar as $fn) {
669 $css = $this->rootURL .'/Pman/'.$mod.'/'.basename($fn) . '?ts=' . filemtime($fn);
670 echo '<link rel="stylesheet" type="text/css" href="'.$css.'" />'."\n";
679 * Gather infor for javascript files..
681 * @param {String} $mod the module to get info about.
682 * @return {StdClass} details about module.
684 function moduleJavascriptFilesInfo($mod)
687 static $cache = array();
689 if (isset($cache[$mod])) {
694 $ff = HTML_FlexyFramework::get();
696 $base = dirname($_SERVER['SCRIPT_FILENAME']);
697 $dir = $this->rootDir.'/Pman/'. $mod;
698 $path = $this->rootURL ."/Pman/$mod/";
700 $ar = glob($dir . '/*.js');
706 foreach($ar as $fn) {
708 // got the 'module file..'
709 $mtime = filemtime($dir . '/'. $f);
710 $maxtime = max($mtime, $maxtime);
711 $arfiles[$fn] = $mtime;
712 $files[] = $path . $f . '?ts='.$mtime;
715 ksort($arfiles); // just sort by name so it's consistant for serialize..
717 $compile = empty($ff->Pman['public_cache_dir']) ? 0 : 1;
718 $basedir = $compile ? $ff->Pman['public_cache_dir'] : false;
719 $baseurl = $compile ? $ff->Pman['public_cache_url'] : false;
721 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
722 usort($files, $lsort);
724 $smod = str_replace('/','.',$mod);
726 $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.js';
729 // why are translations done like this - we just build them on the fly frmo the database..
730 $tmtime = file_exists($this->rootDir.'/_translations_/'. $smod.'.js')
731 ? filemtime($this->rootDir.'/_translations_/'. $smod.'.js') : 0;
733 $cache[$mod] = (object) array(
734 'smod' => $smod, // module name without '/'
735 'files' => $files, // list of all files.
736 'filesmtime' => $arfiles, // map of mtime=>file
737 'maxtime' => $maxtime, // max mtime
738 'compile' => $this->isDev ? false : $compile,
739 'translation_file' => $base .'/_translations_/' . $smod . '.js',
740 'translation_mtime' => $tmtime,
742 'translation_data' => preg_replace('/\.js$/', '.__translation__.js', $output),
743 'translation_base' => $dir .'/', //prefix of filename (without moudle name))
744 'basedir' => $basedir,
745 'baseurl' => $baseurl,
746 'module_dir' => $dir,
753 * moduleJavascriptList: list the javascript files in a module
755 * The original version of this.. still needs more thought...
757 * Compiled is in Pman/_compiled_/{$mod}/{LATEST...}.js
758 * Translations are in Pman/_translations_/{$mod}.js
760 * if that stuff does not exist just list files in Pman/{$mod}/*.js
762 * Compiled could be done on the fly..
766 * @param {String} $mod the module to look at - eg. Pman/{$mod}/*.js
767 * @return {Array} list of include paths (either compiled or raw)
773 function moduleJavascriptList($mod)
777 $dir = $this->rootDir.'/Pman/'. $mod;
780 if (!file_exists($dir)) {
781 echo '<!-- missing directory '. htmlspecialchars($dir) .' -->';
785 $info = $this->moduleJavascriptFilesInfo($mod);
789 if (empty($info->files)) {
792 // finally sort the files, so they are in the right order..
794 // only compile this stuff if public_cache is set..
798 // public_cache_dir = /var/www/myproject_cache
799 // public_cache_url = /myproject_cache (with Alias apache /myproject_cache/ /var/www/myproject_cache/)
802 if (!$info->compile) {
803 echo "<!-- Javascript compile turned off (isDev on, or public_cache_dir not set) -->\n";
807 // where are we going to write all of this..
808 // This has to be done via a
809 if (!file_exists($info->basedir.'/'.$info->output) || !filesize($info->basedir.'/'.$info->output)) {
810 require_once 'Pman/Core/JsCompile.php';
811 $x = new Pman_Core_JsCompile();
813 $x->pack($info->filesmtime,$info->basedir.'/'.$info->output, $info->translation_base);
815 echo "<!-- file exists not exist: {$info->basedir}/{$info->output} -->\n";
818 if (file_exists($info->basedir.'/'.$info->output) &&
819 filesize($info->basedir.'/'.$info->output)) {
822 $info->baseurl.'/'. $info->output,
825 // output all the ava
826 // fixme - this needs the max datetime for the translation file..
827 $ret[] = $this->baseURL."/Admin/InterfaceTranslations/".$mod.".js"; //?ts=".$info->translation_mtime;
829 //if ($info->translation_mtime) {
830 // $ret[] = $this->rootURL."/_translations_/". $info->smod.".js?ts=".$info->translation_mtime;
837 // give up and output original files...
847 * PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
850 static $permitError = false;
852 function onPearError($err)
854 static $reported = false;
859 if (Pman::$permitError) {
867 $out = $err->toString();
870 //print_R($bt); exit;
873 foreach($err->backtrace as $b) {
874 $ret[] = @$b['file'] . '(' . @$b['line'] . ')@' . @$b['class'] . '::' . @$b['function'];
880 //convert the huge backtrace into something that is readable..
881 $out .= "\n" . implode("\n", $ret);
892 * ---------------- Logging ---------------
897 * Log an action (only if it has not been logged already.
899 * @param {String} action - group/name of event
900 * @param {DataObject|false} obj - dataobject action occured on.
901 * @param {String} any remarks
902 * @return {false|DB_DataObject} Event object.,
905 function addEventOnce($act, $obj = false, $remarks = '')
907 if (!empty(HTML_FlexyFramework::get()->Pman['disable_events'])) {
910 $e = DB_DataObject::factory('Events');
911 $e->init($act,$obj,$remarks);
912 if ($e->find(true)) {
915 return $this->addEvent($act, $obj, $remarks);
921 * @param {String} action - group/name of event
922 * @param {DataObject|false} obj - dataobject action occured on.
923 * @param {String} any remarks
924 * @return {DB_DataObject} Event object.,
927 function addEvent($act, $obj = false, $remarks = '')
930 if (!empty(HTML_FlexyFramework::get()->Pman['disable_events'])) {
933 $au = $this->getAuthUser();
935 $e = DB_DataObject::factory('Events');
936 $e->init($act,$obj,$remarks);
938 $e->event_when = date('Y-m-d H:i:s');
942 // fixme - this should be in onInsert..
943 $wa = DB_DataObject::factory('core_watch');
944 if (method_exists($wa,'notifyEvent')) {
945 $wa->notifyEvent($e); // trigger any actions..
949 $e->onInsert(isset($_REQUEST) ? $_REQUEST : array() , $this);
955 // ------------------ DEPERCIATED ----------------------------
957 // DEPRECITAED - use moduleslist
958 function modules() { return $this->modulesList(); }
960 // DEPRECIATED.. - use getAuthUser...
961 function staticGetAuthUser() { $x = new Pman(); return $x->getAuthUser(); }
964 // DEPRICATED USE Pman_Core_Mailer
966 function emailTemplate($templateFile, $args)
969 require_once 'Pman/Core/Mailer.php';
970 $r = new Pman_Core_Mailer(array(
971 'template'=>$templateFile,
978 // DEPRICATED - USE Pman_Core_Mailer
979 // WHAT Part about DEPRICATED Does no one understand??
980 function sendTemplate($templateFile, $args)
982 require_once 'Pman/Core/Mailer.php';
983 $r = new Pman_Core_Mailer(array(
984 'template'=>$templateFile,
985 'contents' => array(),