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..
44 * ------------- Standard getAuth/get/post methods of framework.
49 function getAuth() // everyone allowed in!!!!!
51 $this->loadOwnerCompany();
59 if (isset($this->_hasInit)) {
62 $this->_hasInit = true;
64 $boot = HTML_FlexyFramework::get();
65 // echo'<PRE>';print_R($boot);exit;
66 $this->appName= $boot->appName;
67 $this->appNameShort= $boot->appNameShort;
70 $this->appModules= $boot->enable;
71 $this->isDev = empty($boot->Pman['isDev']) ? false : $boot->Pman['isDev'];
72 $this->appDisable = $boot->disable;
73 $this->appDisabled = explode(',', $boot->disable);
74 $this->version = $boot->version;
76 if (!empty($ff->Pman['local_autoauth']) &&
77 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
78 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
82 if ($this->company->logo_id) {
83 $im = DB_DataObject::Factory('Image');
84 $im->get($this->company->logo_id);
85 $this->appLogo = $this->baseURL . '/Images/'. $this->company->logo_id .'/' . $im->filename;
95 //$this->allowSignup= empty($opts['allowSignup']) ? 0 : 1;
96 $bits = explode('/', $base);
98 if ($bits[0] == 'Link') {
99 $this->linkFail = $this->linkAuth(@$bits[1],@$bits[2]);
100 header('Content-type: text/html; charset=utf-8');
103 if ($bits[0] == 'PasswordReset') {
104 $this->linkFail = $this->resetPassword(@$bits[1],@$bits[2],@$bits[3]);
105 header('Content-type: text/html; charset=utf-8');
110 if ($this->getAuthUser()) {
111 $this->addEvent("RELOAD");
116 $this->addEvent("BADURL", false, $base);
117 $this->jerr("invalid url");
120 if (isset($_GET['onloadTrack'])) {
121 $this->onloadTrack = (int)$_GET['onloadTrack'];
123 // getting this to work with xhtml is a nightmare
124 // = nbsp / <img> issues screw everyting up.
125 //var_dump($this->isDev);
126 // force regeneration on load for development enviroments..
128 HTML_FlexyFramework::get()->generateDataobjectsCache($this->isDev);
130 //header('Content-type: application/xhtml+xml; charset=utf-8');
131 header('Content-type: text/html; charset=utf-8');
134 function post($base) {
135 return $this->get($base);
139 // --------------- AUTHENTICATION or system information
142 * finds the compay with comptype=='OWNER'
144 * @return {Pman_Core_DataObjects_Companies} the owner company
146 function loadOwnerCompany()
149 $this->company = DB_DataObject::Factory('Companies');
150 if (!is_a($this->company, 'DB_DataObject')) { // non-core pman projects
153 $this->company->get('comptype', 'OWNER');
154 return $this->company;
160 * getAuthUser: - get the authenticated user..
162 * @return {DB_DataObject} of type Pman[authTable] if authenticated.
165 function getAuthUser()
167 if (!empty($this->authUser)) {
168 return $this->authUser;
170 $ff = HTML_FlexyFramework::get();
171 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
173 $u = DB_DataObject::factory( $tbl );
177 $this->authUser =$u->getAuthUser();
178 return $this->authUser ;
182 * wrapper arround authuser->hasPerm
183 * @see Pman_Core_DataObjects_User::hasPerm
185 * @param {String} $name The permission name (eg. Projects.List)
186 * @param {String} $lvl eg. (C)reate (E)dit (D)elete ... etc.
189 function hasPerm($name, $lvl) // do we have a permission
191 static $pcache = array();
192 $au = $this->getAuthUser();
193 return $au && $au->hasPerm($name,$lvl);
198 * modulesList: List the modules in the application
200 * @return {Array} list of modules
202 function modulesList()
206 $mods = explode(',', $this->appModules);
207 if (in_array('Core',$mods)) { // core has to be the first modules loaded as it contains Pman.js
208 array_unshift($mods, 'Core');
211 $mods = array_unique($mods);
213 $disabled = explode(',', $this->appDisable ? $this->appDisable: '');
215 foreach($mods as $mod) {
216 // add the css file..
217 if (in_array($mod, $disabled)) {
228 function hasModule($name)
231 if (!strpos( $name,'.') ) {
232 // use enable / disable..
233 return in_array($name, $this->modules());
236 $x = DB_DataObject::factory('Group_Rights');
237 $ar = $x->defaultPermData();
238 if (empty($ar[$name]) || empty($ar[$name][0])) {
249 * ---------------- Global Tools ---------------
255 * send a template to the user
256 * rcpts are read from the resulting template.
258 * @arg $templateFile - the file in mail/XXXXXX.txt
259 * @arg $args - variables available to the form as {t.*} over and above 'this'
264 function sendTemplate($templateFile, $args)
269 $content = clone($this);
271 foreach((array)$args as $k=>$v) {
274 $content->msgid = md5(time() . rand());
276 $content->HTTP_HOST = $_SERVER["HTTP_HOST"];
277 /* use the regex compiler, as it doesnt parse <tags */
278 require_once 'HTML/Template/Flexy.php';
279 $template = new HTML_Template_Flexy( array(
280 'compiler' => 'Regex',
281 'filters' => array('SimpleTags','Mail'),
285 // this should be done by having multiple template sources...!!!
287 $template->compile('mail/'. $templateFile.'.txt');
289 /* use variables from this object to ouput data. */
290 $mailtext = $template->bufferedOutputObject($content);
291 //echo "<PRE>";print_R($mailtext);
293 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
294 require_once 'Mail/mimeDecode.php';
295 require_once 'Mail.php';
297 $decoder = new Mail_mimeDecode($mailtext);
298 $parts = $decoder->getSendArray();
299 if (PEAR::isError($parts)) {
301 //echo "PROBLEM: {$parts->message}";
304 list($recipents,$headers,$body) = $parts;
305 ///$recipents = array($this->email);
306 $mailOptions = PEAR::getStaticProperty('Mail','options');
307 $mail = Mail::factory("SMTP",$mailOptions);
308 $headers['Date'] = date('r');
309 if (PEAR::isError($mail)) {
312 $oe = error_reporting(E_ALL ^ E_NOTICE);
313 $ret = $mail->send($recipents,$headers,$body);
314 error_reporting($oe);
320 function checkFileUploadError() // check for file upload errors.
323 empty($_FILES['File'])
324 || empty($_FILES['File']['name'])
325 || empty($_FILES['File']['tmp_name'])
326 || empty($_FILES['File']['type'])
327 || !empty($_FILES['File']['error'])
328 || empty($_FILES['File']['size'])
330 $this->jerr("File upload error: <PRE>" . print_r($_FILES,true) . print_r($_POST,true) . "</PRE>");
336 * generate a tempory file with an extension (dont forget to delete it)
339 function tempName($ext)
341 $x = tempnam(ini_get('session.save_path'), HTML_FlexyFramework::get()->appNameShort.'TMP');
343 return $x .'.'. $ext;
346 * ------------- Authentication testing ------ ??? MOVEME?
350 function linkAuth($trid, $trkey)
352 $tr = DB_DataObject::factory('Documents_Tracking');
353 if (!$tr->get($trid)) {
354 return "Invalid URL";
356 if (strtolower($tr->authkey) != strtolower($trkey)) {
357 $this->AddEvent("ERROR-L", false, "Invalid Key");
358 return "Invalid KEY";
361 $this->onloadTrack = (int) $tr->doc_id;
362 if (strtotime($tr->date_sent) < strtotime("NOW - 14 DAYS")) {
363 $this->AddEvent("ERROR-L", false, "Key Expired");
364 return "Key Expired";
366 // user logged in and not
367 $au = $this->getAuthUser();
368 if ($au && $au->id && $au->id != $tr->person_id) {
371 return "Logged Out existing Session\n - reload to log in with correct key";
373 if ($au) { // logged in anyway..
374 $this->AddEvent("LOGIN", false, "With Key (ALREADY)");
375 header('Location: ' . $this->baseURL.'?onloadTrack='.$this->onloadTrack);
380 // authenticate the user...
382 $u = DB_DataObject::factory('Person');
384 $u->get($tr->person_id);
386 $this->AddEvent("LOGIN", false, "With Key");
388 // we need to redirect out - otherwise refererer url will include key!
389 header('Location: ' . $this->baseURL.'?onloadTrack='.$this->onloadTrack);
401 * ------------- Authentication password reset ------ ??? MOVEME?
407 function resetPassword($id,$t, $key)
410 $au = $this->getAuthUser();
412 return "Already Logged in - no need to use Password Reset";
415 $u = DB_DataObject::factory('Person');
416 //$u->company_id = $this->company->id;
418 if (!$u->get($id) || !strlen($u->passwd)) {
423 if ($key != $u->genPassKey($t)) {
424 return "invalid key";
427 $u->no_reset_sent = 0;
430 if ($t < strtotime("NOW - 1 DAY")) {
433 $this->showNewPass = implode("/", array($id,$t,$key));
438 * jerrAuth: standard auth failure - with data that let's the UI know..
442 $au = $this->authUser();
444 // is it an authfailure?
445 $this->jerr("Permission denied to view this resource", array('authFailure' => true));
447 $this->jerr("Not authenticated", array('authFailure' => true));
453 * ---------------- Standard JSON outputers. - used everywhere
456 function jerr($str, $errors=array()) // standard error reporting..
459 $cli = HTML_FlexyFramework::get()->cli;
461 echo "ERROR:\n" .$str . "\n";
465 require_once 'Services/JSON.php';
466 $json = new Services_JSON();
469 $this->addEvent("ERROR", false, $str);
471 if (!empty($_REQUEST['returnHTML']) ||
472 (isset($_SERVER['CONTENT_TYPE']) && preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']))
474 header('Content-type: text/html');
475 echo "<HTML><HEAD></HEAD><BODY>";
476 echo $json->encodeUnsafe(array(
479 'message' => $str, // compate with exeption / loadexception.
481 'errors' => $errors ? $errors : true, // used by forms to flag errors.
482 'authFailure' => !empty($errors['authFailure']),
484 echo "</BODY></HTML>";
488 echo $json->encode(array(
492 'message' => $str, // compate with exeption / loadexception.
493 'errors' => $errors ? $errors : true, // used by forms to flag errors.
494 'authFailure' => !empty($errors['authFailure']),
502 require_once 'Services/JSON.php';
503 $json = new Services_JSON();
505 if (!empty($_REQUEST['returnHTML']) ||
506 (isset($_SERVER['CONTENT_TYPE']) && preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE']))
509 header('Content-type: text/html');
510 echo "<HTML><HEAD></HEAD><BODY>";
511 // encode html characters so they can be read..
512 echo str_replace(array('<','>'), array('\u003c','\u003e'),
513 $json->encodeUnsafe(array('success'=> true, 'data' => $str)));
514 echo "</BODY></HTML>";
519 echo $json->encode(array('success'=> true, 'data' => $str));
524 * output data for grids or tree
525 * @ar {Array} ar Array of data
526 * @total {Number|false} total number of records (or false to return count(ar)
527 * @extra {Array} extra key value list of data to pass as extra data.
530 function jdata($ar,$total=false, $extra=array())
532 // should do mobile checking???
533 if ($total == false) {
536 $extra= $extra ? $extra : array();
537 require_once 'Services/JSON.php';
538 $json = new Services_JSON();
539 if (isset($_SERVER['CONTENT_TYPE']) && preg_match('#multipart/form-data#i', $_SERVER['CONTENT_TYPE'])) {
541 header('Content-type: text/html');
542 echo "<HTML><HEAD></HEAD><BODY>";
543 // encode html characters so they can be read..
544 echo str_replace(array('<','>'), array('\u003c','\u003e'),
545 $json->encodeUnsafe(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra));
546 echo "</BODY></HTML>";
556 echo $json->encode(array('success' => true, 'total'=> $total, 'data' => $ar) + $extra);
566 * ---------------- OUTPUT
568 function hasBg($fn) // used on front page to check if logos exist..
570 return file_exists($this->rootDir.'/Pman/'.$this->appNameShort.'/templates/images/'. $fn);
573 * outputJavascriptIncludes:
575 * output <script....> for all the modules in the applcaiton
578 function outputJavascriptIncludes() // includes on devel version..
581 $mods = $this->modulesList();
583 foreach($mods as $mod) {
584 // add the css file..
587 $files = $this->moduleJavascriptList($mod.'/widgets');
588 foreach($files as $f) {
589 echo '<script type="text/javascript" src="'. $f. '"></script>'."\n";
592 $files = $this->moduleJavascriptList($mod);
593 foreach($files as $f) {
594 echo '<script type="text/javascript" src="'. $f. '"></script>'."\n";
603 * output <link rel=stylesheet......> for all the modules in the applcaiton
606 function outputCSSIncludes() // includes on CSS links.
609 $mods = $this->modulesList();
612 foreach($mods as $mod) {
613 // add the css file..
614 $css = $this->rootDir.'/Pman/'.$mod.'/'.strtolower($mod).'.css';
615 if (file_exists( $css)){
616 $css = $this->rootURL .'/Pman/'.$mod.'/'.strtolower($mod).'.css';
617 echo '<link rel="stylesheet" type="text/css" href="'.$css.'" />'."\n";
626 * Gather infor for javascript files..
628 * @param {String} $mod the module to get info about.
629 * @return {StdClass} details about module.
631 function moduleJavascriptFilesInfo($mod)
634 static $cache = array();
636 if (isset($cache[$mod])) {
641 $ff = HTML_FlexyFramework::get();
643 $base = dirname($_SERVER['SCRIPT_FILENAME']);
644 $dir = $this->rootDir.'/Pman/'. $mod;
645 $path = $this->rootURL ."/Pman/$mod/";
647 $ar = glob($dir . '/*.js');
653 foreach($ar as $fn) {
655 // got the 'module file..'
656 $mtime = filemtime($dir . '/'. $f);
657 $maxtime = max($mtime, $maxtime);
658 $arfiles[$fn] = $mtime;
659 $files[] = $path . $f . '?ts='.$mtime;
662 ksort($arfiles); // just sort by name so it's consistant for serialize..
664 $compile = empty($ff->Pman['public_cache_dir']) ? 0 : 1;
665 $basedir = $ff->Pman['public_cache_dir'];
666 $baseurl = $ff->Pman['public_cache_url'];
668 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
669 usort($files, $lsort);
671 $smod = str_replace('/','.',$mod);
673 $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.js';
675 $tmtime = file_exists($this->rootDir.'/_translations_/'. $smod.'.js')
676 ? filemtime($this->rootDir.'/_translations_/'. $smod.'.js') : 0;
678 $cache[$mod] = (object) array(
679 'smod' => $smod, // module name without '/'
680 'files' => $files, // list of all files.
681 'filesmtime' => $arfiles, // map of mtime=>file
682 'maxtime' => $maxtime, // max mtime
683 'compile' => $this->isDev ? false : $compile,
684 'translation_file' => $base .'/_translations_/' . $smod . '.js',
685 'translation_mtime' => $tmtime,
687 'translation_data' => preg_replace('/\.js$/', '.__translation__.js', $output),
688 'translation_base' => $dir .'/', //prefix of filename (without moudle name))
689 'basedir' => $basedir,
690 'baseurl' => $baseurl,
691 'module_dir' => $dir,
698 * moduleJavascriptList: list the javascript files in a module
700 * The original version of this.. still needs more thought...
702 * Compiled is in Pman/_compiled_/{$mod}/{LATEST...}.js
703 * Translations are in Pman/_translations_/{$mod}.js
705 * if that stuff does not exist just list files in Pman/{$mod}/*.js
707 * Compiled could be done on the fly..
711 * @param {String} $mod the module to look at - eg. Pman/{$mod}/*.js
712 * @return {Array} list of include paths (either compiled or raw)
718 function moduleJavascriptList($mod)
722 $dir = $this->rootDir.'/Pman/'. $mod;
725 if (!file_exists($dir)) {
726 echo '<!-- missing directory '. htmlspecialchars($dir) .' -->';
730 $info = $this->moduleJavascriptFilesInfo($mod);
734 if (empty($info->files)) {
737 // finally sort the files, so they are in the right order..
739 // only compile this stuff if public_cache is set..
743 // public_cache_dir = /var/www/myproject_cache
744 // public_cache_url = /myproject_cache (with Alias apache /myproject_cache/ /var/www/myproject_cache/)
747 if (!$info->compile) {
748 echo "<!-- Javascript compile turned off (isDev on, or public_cache_dir not set) -->\n";
751 // where are we going to write all of this..
752 // This has to be done via a
753 if (!file_exists($info->basedir.'/'.$info->output)) {
754 require_once 'Pman/Core/JsCompile.php';
755 $x = new Pman_Core_JsCompile();
757 $x->pack($info->filesmtime,$info->basedir.'/'.$info->output, $info->translation_base);
760 if (file_exists($info->basedir.'/'.$info->output) &&
761 filesize($info->basedir.'/'.$info->output)) {
764 $info->baseurl.'/'. $info->output,
767 if ($info->translation_mtime) {
768 $ret[] = $this->rootURL."/_translations_/". $info->smod.".js?ts=".$info->translation_mtime;
775 // give up and output original files...
786 * ---------------- Logging ---------------
791 * Log an action (only if it has not been logged already.
793 * @param {String} action - group/name of event
794 * @param {DataObject|false} obj - dataobject action occured on.
795 * @param {String} any remarks
796 * @return {false|DB_DataObject} Event object.,
799 function addEventOnce($act, $obj = false, $remarks = '')
802 $e = DB_DataObject::factory('Events');
803 $e->init($act,$obj,$remarks);
804 if ($e->find(true)) {
807 return $this->addEvent($act, $obj, $remarks);
813 * @param {String} action - group/name of event
814 * @param {DataObject|false} obj - dataobject action occured on.
815 * @param {String} any remarks
816 * @return {DB_DataObject} Event object.,
819 function addEvent($act, $obj = false, $remarks = '')
821 $au = $this->getAuthUser();
823 $e = DB_DataObject::factory('Events');
824 $e->init($act,$obj,$remarks);
826 $e->event_when = date('Y-m-d H:i:s');
830 $wa = DB_DataObject::factory('core_watch');
831 $wa->notifyEvent($e); // trigger any actions..
834 $ff = HTML_FlexyFramework::get();
835 if (empty($ff->Pman['event_log_dir'])) {
838 $file = $ff->Pman['event_log_dir']. date('/Y/m/d/'). $eid . ".php";
839 if (!file_exists(dirname($file))) {
840 mkdir(dirname($file),0700,true);
842 file_put_contents($file, var_export(array(
843 'REQUEST_URI' => empty($_SERVER['REQUEST_URI']) ? 'cli' : $_SERVER['REQUEST_URI'],
844 'GET' => empty($_GET) ? array() : $_GET,
845 'POST' => empty($_POST) ? array() : $_POST,
851 // ------------------ DEPERCIATED ---
853 function modules() // DEPRECITAED
855 return $this->modulesList();
857 function staticGetAuthUser() // DEPRECIATED..
861 return $x->getAuthUser();