3 * Table Definition for Person
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Person extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'Person'; // table name
13 public $id; // int(11) not_null primary_key auto_increment
14 public $email; // string(128) not_null
16 public $company_id; // int(11)
17 public $office_id; // int(11)
19 public $name; // string(128) not_null
20 public $firstname; // string(128) not_null
21 public $lastname; // string(128) not_null
22 public $phone; // string(32) not_null
23 public $fax; // string(32) not_null
25 public $role; // string(32) not_null
26 public $remarks; // blob(65535) not_null blob
27 public $passwd; // string(64) not_null
28 public $owner_id; // int(11) not_null
29 public $lang; // string(8)
30 public $no_reset_sent; // int(11)
31 public $action_type; // string(32)
32 public $project_id; // int(11)
35 public $active; // int(11) not_null
36 public $deleted_by; // int(11) not_null
37 public $deleted_dt; // datetime(19) binary
40 /* the code above is auto generated do not remove the tag below */
44 * @param {String} $templateFile (mail/XXXXXXX.txt) exclude the mail and .txt bit.
45 * @param {Array|Object} $args data to send out..
46 * @return {Array|PEAR_Error} array of $recipents, $header, $body
48 function buildMail($templateFile, $args)
51 $args = (array) $args;
52 $content = clone($this);
54 foreach((array)$args as $k=>$v) {
58 $ff = HTML_FlexyFramework::get();
61 //?? is this really the place for this???
64 empty($args['no_auth']) &&
65 !in_array($templateFile, array(
66 // templates that can be sent without authentication.
72 $content->authUser = $this->getAuthUser();
73 if (!$content->authUser) {
74 return PEAR::raiseError("Not authenticated");
78 // should handle x-forwarded...
80 $content->HTTP_HOST = isset($_SERVER["HTTP_HOST"]) ?
81 $_SERVER["HTTP_HOST"] :
82 (isset($ff->HTTP_HOST) ? $ff->HTTP_HOST : 'localhost');
84 /* use the regex compiler, as it doesnt parse <tags */
87 'compiler' => 'Flexy',
89 'filters' => array('SimpleTags','Mail'),
95 if (!empty($args['templateDir'])) {
96 $tops['templateDir'] = $args['templateDir'];
101 require_once 'HTML/Template/Flexy.php';
102 $template = new HTML_Template_Flexy( $tops );
103 $template->compile("mail/$templateFile.txt");
105 /* use variables from this object to ouput data. */
106 $mailtext = $template->bufferedOutputObject($content);
108 // if a html file with the same name exists, use that as the body
109 // I've no idea where this code went, it was here before..
110 if (false !== $template->resolvePath ( "mail/$templateFile.html" )) {
111 $tops['nonHTML'] = false;
112 $template = new HTML_Template_Flexy( $tops );
113 $template->compile("mail/$templateFile.html");
115 $htmlbody = $template->bufferedOutputObject($content);
122 //echo "<PRE>";print_R($mailtext);
123 //print_R($mailtext);exit;
124 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
125 require_once 'Mail/mimeDecode.php';
126 require_once 'Mail.php';
128 $decoder = new Mail_mimeDecode($mailtext);
129 $parts = $decoder->getSendArray();
130 if (PEAR::isError($parts)) {
132 //echo "PROBLEM: {$parts->message}";
135 list($recipents,$headers,$body) = $parts;
136 $recipents = array($this->email);
137 if (!empty($content->bcc) && is_array($content->bcc)) {
138 $recipents =array_merge($recipents, $content->bcc);
140 $headers['Date'] = date('r');
143 'recipients' => $recipents,
144 'headers' => $headers,
154 * - user must be authenticate or args[no_auth] = true
155 * or template = password_[reset|welcome]
158 function sendTemplate($templateFile, $args)
161 $ar = $this->buildMail($templateFile, $args);
164 //print_r($recipents);exit;
165 $mailOptions = PEAR::getStaticProperty('Mail','options');
166 $mail = Mail::factory("SMTP",$mailOptions);
168 if (PEAR::isError($mail)) {
171 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
172 $ret = $mail->send($ar['recipients'],$ar['headers'],$ar['body']);
173 error_reporting($oe);
182 function getEmailFrom()
184 return '"' . addslashes($this->name) . '" <' . $this->email . '>';
186 function toEventString()
188 return empty($this->name) ? $this->email : $this->name;
190 function verifyAuth()
192 $ff= HTML_FlexyFramework::get();
193 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $this->company()->comptype) {
194 $ff->page->jerr("Login not permited to outside companies");
201 // ---------------- authentication / passwords and keys stuff ----------------
204 $db = $this->getDatabaseConnection();
205 // we combine db + project names,
206 // otherwise if projects use different 'auth' objects
207 // then we get unserialize issues.
208 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
212 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
214 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
216 $u = DB_DataObject::factory('Person');
217 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
223 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
227 $u = DB_DataObject::factory('Person');
228 $ff = HTML_FlexyFramework::get();
229 if (!empty($ff->Pman['local_autoauth']) &&
230 (!empty($_SERVER['SERVER_ADDR'])) &&
231 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
232 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
233 $u->get('email', $ff->Pman['local_autoauth'])
235 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
240 $u = DB_DataObject::factory('Person');
242 if (!empty($_SERVER['PHP_AUTH_USER'])
244 !empty($_SERVER['PHP_AUTH_PW'])
246 $u->get('email', $_SERVER['PHP_AUTH_USER'])
248 $u->checkPassword($_SERVER['PHP_AUTH_PW'])
250 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
257 // not in session or not matched...
258 $u = DB_DataObject::factory('Person');
259 $u->whereAdd(' LENGTH(passwd) > 0');
261 $error = PEAR::getStaticProperty('DB_DataObject','lastError');
263 die($error->toString()); // not really a good thing to do...
265 if (!$n){ // authenticated as there are no users in the system...
272 function getAuthUser()
274 if (!$this->isAuth()) {
277 $db = $this->getDatabaseConnection();
278 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
280 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
281 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
283 $u = DB_DataObject::factory('Person');
284 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
290 $u = DB_DataObject::factory('Person');
291 $u->whereAdd(' LENGTH(passwd) > 0');
293 $u = DB_DataObject::factory('Person');
302 $this->isAuth(); // force session start..
304 $db = $this->getDatabaseConnection();
305 // refresh admin group if we are logged in as one..
306 //DB_DataObject::debugLevel(1);
307 $g = DB_DataObject::factory('Groups');
309 $g->get('name', 'Administrators');
310 $gm = DB_DataObject::Factory('group_members');
311 if (in_array($g->id,$gm->listGroupMembership($this))) {
312 // refresh admin groups.
313 $gr = DB_DataObject::Factory('group_rights');
314 $gr->applyDefs($g, 0);
317 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
318 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
323 $this->isAuth(); // force session start..
324 $db = $this->getDatabaseConnection();
325 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
326 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
329 function genPassKey ($t)
331 return md5($this->email . $t. $this->passwd);
333 function simpleAuthKey($m = 0)
335 $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
337 return md5(implode(',' , array($month, $this->email , $this->passwd, $this->id)));
339 function checkPassword($val)
342 if (substr($this->passwd,0,1) == '$') {
343 return crypt($val,$this->passwd) == $this->passwd ;
345 // old style md5 passwords...- cant be used with courier....
346 return md5($val) == $this->passwd;
348 function setPassword($value)
351 while(strlen($salt)<9) {
352 $salt.=chr(rand(64,126));
353 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
355 $this->passwd = crypt($value, '$1$'. $salt. '$');
362 $x = DB_DataObject::factory('Companies');
363 $x->get($this->company_id);
366 function loadCompany()
368 $this->company = $this->company();
373 return $this->active;
375 function authUserName($n) // set username prior to acheck user exists query.
378 $this->whereAdd('LENGTH(passwd) > 1');
383 if ($val == $this->lang) {
393 function authUserArray()
396 $aur = $this->toArray();
403 //DB_DataObject::debugLevel(1);
404 $c = DB_Dataobject::factory('Companies');
405 $im = DB_Dataobject::factory('Images');
406 $c->joinAdd($im, 'LEFT');
408 $c->selectAs($c, 'company_id_%s');
409 $c->selectAs($im, 'company_id_logo_id_%s');
410 $c->id = $this->company_id;
414 $aur = array_merge( $c->toArray(),$aur);
416 if (empty($c->company_id_logo_id_id)) {
418 $im = DB_Dataobject::factory('Images');
419 $im->ontable = 'Companies';
421 $im->imgtype = 'LOGO';
424 $im->selectAs($im, 'company_id_logo_id_%s');
425 if ($im->find(true)) {
427 foreach($im->toArray() as $k=>$v) {
434 $aur['perms'] = $this->getPerms();
435 $g = DB_DataObject::Factory('group_members');
436 $aur['groups'] = $g->listGroupMembership($this, 'name');
439 $aur['dailykey'] = '';
446 // ----------PERMS------ ----------------
449 //DB_DataObject::debugLevel(1);
450 // find out all the groups they are a member of.. + Default..
452 // ------ INIITIALIZE IF NO GROUPS ARE SET UP.
454 $g = DB_DataObject::Factory('group_rights');
460 return $g->adminRights(); // system is not set up - so they get full rights.
462 //DB_DataObject::debugLevel(1);
463 $g = DB_DataObject::Factory('group_members');
464 $g->whereAdd('group_id is NOT NULL AND user_id IS NOT NULL');
466 // add the current user to the admin group..
467 $g = DB_DataObject::Factory('Groups');
468 if ($g->get('name', 'Administrators')) {
469 $gm = DB_DataObject::Factory('group_members');
470 $gm->group_id = $g->id;
471 $gm->user_id = $this->id;
477 // ------ STANDARD PERMISSION HANDLING.
478 $isOwner = $this->company()->comptype == 'OWNER';
479 $g = DB_DataObject::Factory('group_members');
480 $grps = $g->listGroupMembership($this);
482 $isAdmin = $g->inAdmin;
483 //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
484 // the load all the perms for those groups, and add them all together..
485 // then load all those
486 $g = DB_DataObject::Factory('group_rights');
487 $ret = $g->listPermsFromGroupIds($grps, $isAdmin, $isOwner);
488 //echo '<PRE>';print_r($ret);
494 *Basic group fetching - probably needs to filter by type eventually.
496 *@param String $what - fetchall() argument - eg. 'name' returns names of all groups that they are members of.
499 function groups($what=false)
501 $g = DB_DataObject::Factory('group_members');
502 $grps = $g->listGroupMembership($this);
503 $g = DB_DataObject::Factory('Groups');
504 $g->whereAddIn('id', $grps, 'int');
505 return $g->fetchAll($what);
509 function hasPerm($name, $lvl)
511 static $pcache = array();
513 if (!isset($pcache[$this->id])) {
514 $pcache[$this->id] = $this->getPerms();
516 // echo "<PRE>";print_r($pcache[$au->id]);
517 // var_dump($pcache[$au->id]);
518 if (empty($pcache[$this->id][$name])) {
522 return strpos($pcache[$this->id][$name], $lvl) > -1;
526 // ------------ROO HOOKS------------------------------------
527 function applyFilters($q, $au, $roo)
529 //DB_DataObject::DebugLevel(1);
530 if (!empty($q['query']['person_not_internal'])) {
531 $this->whereAdd(" join_company_id_id.isOwner = 0 ");
535 if (!empty($q['query']['person_internal_only_all'])) {
538 // must be internal and not current user (need for distribution list)
539 // user has a projectdirectory entry and role is not blank.
540 //DB_DataObject::DebugLevel(1);
541 $pd = DB_DataObject::factory('ProjectDirectory');
542 $pd->whereAdd("role != ''");
544 $pd->selectAdd('distinct(person_id) as person_id');
545 $roled = $pd->fetchAll('person_id');
547 {$this->tableName()}.id IN (".implode(',', $roled) . ")
549 $this->whereAdd(" join_company_id_id.comptype = 'OWNER' $rs ");
552 // -- for distribution
553 if (!empty($q['query']['person_internal_only'])) {
554 // must be internal and not current user (need for distribution list)
555 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
557 //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
558 // ".id != ".$au->id);
559 $this->whereAdd("Person.id != {$au->id}");
562 if (!empty($q['query']['comptype_or_company_id'])) {
563 // DB_DataObject::debugLevel(1);
564 $bits = explode(',', $q['query']['comptype_or_company_id']);
565 $id = (int) array_pop($bits);
566 $ct = $this->escape($bits[0]);
568 $this->whereAdd(" join_company_id_id.comptype = '$ct' OR Person.company_id = $id");
574 if (!empty($q['query']['person_inactive'])) {
575 // DB_Dataobject::debugLevel(1);
578 $tn_p = $this->tableName();
579 $tn_gm = DB_DataObject::Factory('group_members')->tableName();
580 $tn_g = DB_DataObject::Factory('Groups')->tableName();
582 ///---------------- Group views --------
583 if (!empty($q['query']['in_group'])) {
584 // DB_DataObject::debugLevel(1);
585 $ing = (int) $q['query']['in_group'];
586 if ($q['query']['in_group'] == -1) {
588 // list all staff who are not in a group.
589 $this->whereAdd("Person.id NOT IN (
590 SELECT distinct(user_id) FROM $tn_gm LEFT JOIN
591 $tn_g ON $tn_g.id = $tn_gm.group_id
592 WHERE $tn_g.type = ".$q['query']['type']."
598 $this->whereAdd("$tn_p.id IN (
599 SELECT distinct(user_id) FROM $tn_gm
600 WHERE group_id = $ing
606 if (!empty($q['query']['not_in_directory'])) {
607 // it's a Person list..
608 // DB_DATaobjecT::debugLevel(1);
610 // specific to project directory which is single comp. login
612 $owncomp = DB_DataObject::Factory('Companies');
613 $owncomp->get('comptype', 'OWNER');
614 if ($q['company_id'] == $owncomp->id) {
620 if ( $q['query']['not_in_directory'] > -1) {
621 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
622 // can list current - so that it does not break!!!
623 $this->whereAdd("$tn_p.id NOT IN
624 ( SELECT distinct person_id FROM $tn_pd WHERE
625 project_id = " . $q['query']['not_in_directory'] . " AND
626 company_id = " . $this->company_id . ')');
630 if (!empty($q['query']['role'])) {
631 // it's a Person list..
632 // DB_DATaobjecT::debugLevel(1);
634 // specific to project directory which is single comp. login
636 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
637 // can list current - so that it does not break!!!
638 $this->whereAdd("$tn_p.id IN
639 ( SELECT distinct person_id FROM $tn_pd WHERE
640 role = '". $this->escape($q['query']['role']) ."'
646 if (!empty($q['query']['project_member_of'])) {
647 // this is also a flag to return if they are a member..
648 //DB_DataObject::debugLevel(1);
649 $do = DB_DataObject::factory('ProjectDirectory');
650 $do->project_id = $q['query']['project_member_of'];
651 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
652 $this->joinAdd($do,array('joinType' => 'LEFT', 'useWhereAsOn' => true));
653 $this->selectAdd("IF($tn_pd.id IS NULL, 0, $tn_pd.id ) as is_member");
656 if (!empty($q['query']['project_member_filter'])) {
657 $this->having('is_member !=0');
664 if (!empty($q['query']['search'])) {
665 $s = $this->escape($q['query']['search']);
667 $tn_p.name LIKE '%$s%' OR
668 $tn_p.email LIKE '%$s%' OR
669 $tn_p.role LIKE '%$s%' OR
670 $tn_p.remarks LIKE '%$s%'
677 function setFromRoo($ar, $roo)
680 if (!empty($ar['passwd1'])) {
681 $this->setPassword($ar['passwd1']);
686 ($this->email == $roo->old->email)&&
687 ($this->company_id == $roo->old->company_id)
691 if (empty($this->email)) {
694 $xx = DB_Dataobject::factory('Person');
696 'email' => $this->email,
697 // 'company_id' => $x->company_id
701 return "Duplicate Email found";
707 * before Delete - delete significant dependancies..
708 * this is called after checkPerm..
711 function beforeDelete()
714 $e = DB_DataObject::Factory('Events');
715 $e->whereAdd('person_id = ' . $this->id);
724 * Check if the a user has access to modify this item.
725 * @param String $lvl Level (eg. Core.Projects)
726 * @param Pman_Core_DataObjects_Person $au The authenticated user.
727 * @param boolean $changes alllow changes???
729 * @return false if no access..
731 function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
734 // do we have an empty system..
735 if ($au && $au->id == -1) {
739 // determine if it's staff!!!
741 if ($au->company()->comptype != 'OWNER') {
743 // - can not change company!!!
745 isset($changes['company_id']) &&
746 $changes['company_id'] != $au->company_id) {
749 // can only set new emails..
751 !empty($this->email) &&
752 isset($changes['email']) &&
753 $changes['email'] != $this->email) {
757 // edit self... - what about other staff members...
759 return $this->company_id == $au->company_id;
763 // yes, only owner company can mess with this...
764 $owncomp = DB_DataObject::Factory('Companies');
765 $owncomp->get('comptype', 'OWNER');
767 $isStaff = ($this->company_id == $owncomp->id);
771 // extra case change passwod?
772 case 'P': //??? password
773 // standard perms -- for editing + if the user is dowing them selves..
774 $ret = $isStaff ? $au->hasPerm("Core.Staff", "E") : $au->hasPerm("Core.Person", "E");
775 return $ret || $au->id == $this->id;
778 return $isStaff ? $au->hasPerm("Core.Staff", $lvl) : $au->hasPerm("Core.Person", $lvl);
783 function onInsert($req, $roo)
786 if ($roo->authUser->id < 0) {
787 $g = DB_DataObject::factory('Groups');
789 $g->get('name', 'Administrators');
791 $p = DB_DataObject::factory('group_members');
792 $p->group_id = $g->id;
793 $p->user_id = $this->id;
796 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
800 if (!empty($req['project_id_addto'])) {
801 $pd = DB_DataObject::factory('ProjectDirectory');
802 $pd->project_id = $req['project_id_addto'];
803 $pd->person_id = $this->id;
805 $pd->office_id = $this->office_id;
806 $pd->company_id = $this->company_id;