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);
109 // if a html file with the same name exists, use that as the body
110 // I've no idea where this code went, it was here before..
111 if (false !== $template->resolvePath ( "mail/$templateFile.html" )) {
112 $tops['nonHTML'] = false;
113 $template = new HTML_Template_Flexy( $tops );
114 $template->compile("mail/$templateFile.html");
115 $htmlbody = $template->bufferedOutputObject($content);
121 //echo "<PRE>";print_R($mailtext);
122 //print_R($mailtext);exit;
123 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
124 require_once 'Mail/mimeDecode.php';
125 require_once 'Mail.php';
127 $decoder = new Mail_mimeDecode($mailtext);
128 $parts = $decoder->getSendArray();
129 if (PEAR::isError($parts)) {
131 //echo "PROBLEM: {$parts->message}";
134 list($recipents,$headers,$body) = $parts;
135 $recipents = array($this->email);
136 if (!empty($content->bcc) && is_array($content->bcc)) {
137 $recipents =array_merge($recipents, $content->bcc);
139 $headers['Date'] = date('r');
142 'recipients' => $recipents,
143 'headers' => $headers,
153 * - user must be authenticate or args[no_auth] = true
154 * or template = password_[reset|welcome]
157 function sendTemplate($templateFile, $args)
160 $ar = $this->buildMail($templateFile, $args);
163 //print_r($recipents);exit;
164 $mailOptions = PEAR::getStaticProperty('Mail','options');
165 $mail = Mail::factory("SMTP",$mailOptions);
167 if (PEAR::isError($mail)) {
170 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
171 $ret = $mail->send($ar['recipients'],$ar['headers'],$ar['body']);
172 error_reporting($oe);
181 function getEmailFrom()
183 return '"' . addslashes($this->name) . '" <' . $this->email . '>';
185 function toEventString()
187 return empty($this->name) ? $this->email : $this->name;
189 function verifyAuth()
191 $ff= HTML_FlexyFramework::get();
192 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $this->company()->comptype) {
193 $ff->page->jerr("Login not permited to outside companies");
200 // ---------------- authentication / passwords and keys stuff ----------------
203 $db = $this->getDatabaseConnection();
204 // we combine db + project names,
205 // otherwise if projects use different 'auth' objects
206 // then we get unserialize issues.
207 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
211 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
213 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
215 $u = DB_DataObject::factory('Person');
216 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
222 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
226 $u = DB_DataObject::factory('Person');
227 $ff = HTML_FlexyFramework::get();
228 if (!empty($ff->Pman['local_autoauth']) &&
229 (!empty($_SERVER['SERVER_ADDR'])) &&
230 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
231 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
232 $u->get('email', $ff->Pman['local_autoauth'])
234 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
239 $u = DB_DataObject::factory('Person');
241 if (!empty($_SERVER['PHP_AUTH_USER'])
243 !empty($_SERVER['PHP_AUTH_PW'])
245 $u->get('email', $_SERVER['PHP_AUTH_USER'])
247 $u->checkPassword($_SERVER['PHP_AUTH_PW'])
249 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
256 // not in session or not matched...
257 $u = DB_DataObject::factory('Person');
258 $u->whereAdd(' LENGTH(passwd) > 0');
260 $error = PEAR::getStaticProperty('DB_DataObject','lastError');
262 die($error->toString()); // not really a good thing to do...
264 if (!$n){ // authenticated as there are no users in the system...
271 function getAuthUser()
273 if (!$this->isAuth()) {
276 $db = $this->getDatabaseConnection();
277 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
279 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
280 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
282 $u = DB_DataObject::factory('Person');
283 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
289 $u = DB_DataObject::factory('Person');
290 $u->whereAdd(' LENGTH(passwd) > 0');
292 $u = DB_DataObject::factory('Person');
301 $this->isAuth(); // force session start..
303 $db = $this->getDatabaseConnection();
304 // refresh admin group if we are logged in as one..
305 //DB_DataObject::debugLevel(1);
306 $g = DB_DataObject::factory('Groups');
308 $g->get('name', 'Administrators');
309 $gm = DB_DataObject::Factory('group_members');
310 if (in_array($g->id,$gm->listGroupMembership($this))) {
311 // refresh admin groups.
312 $gr = DB_DataObject::Factory('group_rights');
313 $gr->applyDefs($g, 0);
316 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
317 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
322 $this->isAuth(); // force session start..
323 $db = $this->getDatabaseConnection();
324 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
325 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
328 function genPassKey ($t)
330 return md5($this->email . $t. $this->passwd);
332 function simpleAuthKey($m = 0)
334 $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
336 return md5(implode(',' , array($month, $this->email , $this->passwd, $this->id)));
338 function checkPassword($val)
341 if (substr($this->passwd,0,1) == '$') {
342 return crypt($val,$this->passwd) == $this->passwd ;
344 // old style md5 passwords...- cant be used with courier....
345 return md5($val) == $this->passwd;
347 function setPassword($value)
350 while(strlen($salt)<9) {
351 $salt.=chr(rand(64,126));
352 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
354 $this->passwd = crypt($value, '$1$'. $salt. '$');
361 $x = DB_DataObject::factory('Companies');
362 $x->get($this->company_id);
365 function loadCompany()
367 $this->company = $this->company();
372 return $this->active;
374 function authUserName($n) // set username prior to acheck user exists query.
377 $this->whereAdd('LENGTH(passwd) > 1');
382 if ($val == $this->lang) {
392 function authUserArray()
395 $aur = $this->toArray();
402 //DB_DataObject::debugLevel(1);
403 $c = DB_Dataobject::factory('Companies');
404 $im = DB_Dataobject::factory('Images');
405 $c->joinAdd($im, 'LEFT');
407 $c->selectAs($c, 'company_id_%s');
408 $c->selectAs($im, 'company_id_logo_id_%s');
409 $c->id = $this->company_id;
413 $aur = array_merge( $c->toArray(),$aur);
415 if (empty($c->company_id_logo_id_id)) {
417 $im = DB_Dataobject::factory('Images');
418 $im->ontable = 'Companies';
420 $im->imgtype = 'LOGO';
423 $im->selectAs($im, 'company_id_logo_id_%s');
424 if ($im->find(true)) {
426 foreach($im->toArray() as $k=>$v) {
433 $aur['perms'] = $this->getPerms();
434 $g = DB_DataObject::Factory('group_members');
435 $aur['groups'] = $g->listGroupMembership($this, 'name');
438 $aur['dailykey'] = '';
445 // ----------PERMS------ ----------------
448 //DB_DataObject::debugLevel(1);
449 // find out all the groups they are a member of.. + Default..
451 // ------ INIITIALIZE IF NO GROUPS ARE SET UP.
453 $g = DB_DataObject::Factory('group_rights');
459 return $g->adminRights(); // system is not set up - so they get full rights.
461 //DB_DataObject::debugLevel(1);
462 $g = DB_DataObject::Factory('group_members');
463 $g->whereAdd('group_id is NOT NULL AND user_id IS NOT NULL');
465 // add the current user to the admin group..
466 $g = DB_DataObject::Factory('Groups');
467 if ($g->get('name', 'Administrators')) {
468 $gm = DB_DataObject::Factory('group_members');
469 $gm->group_id = $g->id;
470 $gm->user_id = $this->id;
476 // ------ STANDARD PERMISSION HANDLING.
477 $isOwner = $this->company()->comptype == 'OWNER';
478 $g = DB_DataObject::Factory('group_members');
479 $grps = $g->listGroupMembership($this);
481 $isAdmin = $g->inAdmin;
482 //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
483 // the load all the perms for those groups, and add them all together..
484 // then load all those
485 $g = DB_DataObject::Factory('group_rights');
486 $ret = $g->listPermsFromGroupIds($grps, $isAdmin, $isOwner);
487 //echo '<PRE>';print_r($ret);
493 *Basic group fetching - probably needs to filter by type eventually.
495 *@param String $what - fetchall() argument - eg. 'name' returns names of all groups that they are members of.
498 function groups($what=false)
500 $g = DB_DataObject::Factory('group_members');
501 $grps = $g->listGroupMembership($this);
502 $g = DB_DataObject::Factory('Groups');
503 $g->whereAddIn('id', $grps, 'int');
504 return $g->fetchAll($what);
508 function hasPerm($name, $lvl)
510 static $pcache = array();
512 if (!isset($pcache[$this->id])) {
513 $pcache[$this->id] = $this->getPerms();
515 // echo "<PRE>";print_r($pcache[$au->id]);
516 // var_dump($pcache[$au->id]);
517 if (empty($pcache[$this->id][$name])) {
521 return strpos($pcache[$this->id][$name], $lvl) > -1;
525 // ------------ROO HOOKS------------------------------------
526 function applyFilters($q, $au, $roo)
528 //DB_DataObject::DebugLevel(1);
529 if (!empty($q['query']['person_not_internal'])) {
530 $this->whereAdd(" join_company_id_id.isOwner = 0 ");
534 if (!empty($q['query']['person_internal_only_all'])) {
537 // must be internal and not current user (need for distribution list)
538 // user has a projectdirectory entry and role is not blank.
539 //DB_DataObject::DebugLevel(1);
540 $pd = DB_DataObject::factory('ProjectDirectory');
541 $pd->whereAdd("role != ''");
543 $pd->selectAdd('distinct(person_id) as person_id');
544 $roled = $pd->fetchAll('person_id');
546 {$this->tableName()}.id IN (".implode(',', $roled) . ")
548 $this->whereAdd(" join_company_id_id.comptype = 'OWNER' $rs ");
551 // -- for distribution
552 if (!empty($q['query']['person_internal_only'])) {
553 // must be internal and not current user (need for distribution list)
554 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
556 //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
557 // ".id != ".$au->id);
558 $this->whereAdd("Person.id != {$au->id}");
561 if (!empty($q['query']['comptype_or_company_id'])) {
562 // DB_DataObject::debugLevel(1);
563 $bits = explode(',', $q['query']['comptype_or_company_id']);
564 $id = (int) array_pop($bits);
565 $ct = $this->escape($bits[0]);
567 $this->whereAdd(" join_company_id_id.comptype = '$ct' OR Person.company_id = $id");
573 if (!empty($q['query']['person_inactive'])) {
574 // DB_Dataobject::debugLevel(1);
577 $tn_p = $this->tableName();
578 $tn_gm = DB_DataObject::Factory('group_members')->tableName();
579 $tn_g = DB_DataObject::Factory('Groups')->tableName();
581 ///---------------- Group views --------
582 if (!empty($q['query']['in_group'])) {
583 // DB_DataObject::debugLevel(1);
584 $ing = (int) $q['query']['in_group'];
585 if ($q['query']['in_group'] == -1) {
587 // list all staff who are not in a group.
588 $this->whereAdd("Person.id NOT IN (
589 SELECT distinct(user_id) FROM $tn_gm LEFT JOIN
590 $tn_g ON $tn_g.id = $tn_gm.group_id
591 WHERE $tn_g.type = ".$q['query']['type']."
597 $this->whereAdd("$tn_p.id IN (
598 SELECT distinct(user_id) FROM $tn_gm
599 WHERE group_id = $ing
605 if (!empty($q['query']['not_in_directory'])) {
606 // it's a Person list..
607 // DB_DATaobjecT::debugLevel(1);
609 // specific to project directory which is single comp. login
611 $owncomp = DB_DataObject::Factory('Companies');
612 $owncomp->get('comptype', 'OWNER');
613 if ($q['company_id'] == $owncomp->id) {
619 if ( $q['query']['not_in_directory'] > -1) {
620 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
621 // can list current - so that it does not break!!!
622 $this->whereAdd("$tn_p.id NOT IN
623 ( SELECT distinct person_id FROM $tn_pd WHERE
624 project_id = " . $q['query']['not_in_directory'] . " AND
625 company_id = " . $this->company_id . ')');
629 if (!empty($q['query']['role'])) {
630 // it's a Person list..
631 // DB_DATaobjecT::debugLevel(1);
633 // specific to project directory which is single comp. login
635 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
636 // can list current - so that it does not break!!!
637 $this->whereAdd("$tn_p.id IN
638 ( SELECT distinct person_id FROM $tn_pd WHERE
639 role = '". $this->escape($q['query']['role']) ."'
645 if (!empty($q['query']['project_member_of'])) {
646 // this is also a flag to return if they are a member..
647 //DB_DataObject::debugLevel(1);
648 $do = DB_DataObject::factory('ProjectDirectory');
649 $do->project_id = $q['query']['project_member_of'];
650 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
651 $this->joinAdd($do,array('joinType' => 'LEFT', 'useWhereAsOn' => true));
652 $this->selectAdd("IF($tn_pd.id IS NULL, 0, $tn_pd.id ) as is_member");
655 if (!empty($q['query']['project_member_filter'])) {
656 $this->having('is_member !=0');
663 if (!empty($q['query']['search'])) {
664 $s = $this->escape($q['query']['search']);
666 $tn_p.name LIKE '%$s%' OR
667 $tn_p.email LIKE '%$s%' OR
668 $tn_p.role LIKE '%$s%' OR
669 $tn_p.remarks LIKE '%$s%'
676 function setFromRoo($ar, $roo)
679 if (!empty($ar['passwd1'])) {
680 $this->setPassword($ar['passwd1']);
685 ($this->email == $roo->old->email)&&
686 ($this->company_id == $roo->old->company_id)
690 if (empty($this->email)) {
693 $xx = DB_Dataobject::factory('Person');
695 'email' => $this->email,
696 // 'company_id' => $x->company_id
700 return "Duplicate Email found";
706 * before Delete - delete significant dependancies..
707 * this is called after checkPerm..
710 function beforeDelete()
713 $e = DB_DataObject::Factory('Events');
714 $e->whereAdd('person_id = ' . $this->id);
723 * Check if the a user has access to modify this item.
724 * @param String $lvl Level (eg. Core.Projects)
725 * @param Pman_Core_DataObjects_Person $au The authenticated user.
726 * @param boolean $changes alllow changes???
728 * @return false if no access..
730 function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
733 // do we have an empty system..
734 if ($au && $au->id == -1) {
738 // determine if it's staff!!!
740 if ($au->company()->comptype != 'OWNER') {
742 // - can not change company!!!
744 isset($changes['company_id']) &&
745 $changes['company_id'] != $au->company_id) {
748 // can only set new emails..
750 !empty($this->email) &&
751 isset($changes['email']) &&
752 $changes['email'] != $this->email) {
756 // edit self... - what about other staff members...
758 return $this->company_id == $au->company_id;
762 // yes, only owner company can mess with this...
763 $owncomp = DB_DataObject::Factory('Companies');
764 $owncomp->get('comptype', 'OWNER');
766 $isStaff = ($this->company_id == $owncomp->id);
770 // extra case change passwod?
771 case 'P': //??? password
772 // standard perms -- for editing + if the user is dowing them selves..
773 $ret = $isStaff ? $au->hasPerm("Core.Staff", "E") : $au->hasPerm("Core.Person", "E");
774 return $ret || $au->id == $this->id;
777 return $isStaff ? $au->hasPerm("Core.Staff", $lvl) : $au->hasPerm("Core.Person", $lvl);
782 function onInsert($req, $roo)
785 if ($roo->authUser->id < 0) {
786 $g = DB_DataObject::factory('Groups');
788 $g->get('name', 'Administrators');
790 $p = DB_DataObject::factory('group_members');
791 $p->group_id = $g->id;
792 $p->user_id = $this->id;
795 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
799 if (!empty($req['project_id_addto'])) {
800 $pd = DB_DataObject::factory('ProjectDirectory');
801 $pd->project_id = $req['project_id_addto'];
802 $pd->person_id = $this->id;
804 $pd->office_id = $this->office_id;
805 $pd->company_id = $this->company_id;