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 */
85 require_once 'HTML/Template/Flexy.php';
86 $template = new HTML_Template_Flexy( array(
87 'compiler' => 'Flexy',
89 'filters' => array('SimpleTags','Mail'),
96 $template->compile("mail/$templateFile.txt");
98 /* use variables from this object to ouput data. */
99 $mailtext = $template->bufferedOutputObject($content);
100 //echo "<PRE>";print_R($mailtext);
101 //print_R($mailtext);exit;
102 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
103 require_once 'Mail/mimeDecode.php';
104 require_once 'Mail.php';
106 $decoder = new Mail_mimeDecode($mailtext);
107 $parts = $decoder->getSendArray();
108 if (PEAR::isError($parts)) {
110 //echo "PROBLEM: {$parts->message}";
113 list($recipents,$headers,$body) = $parts;
114 $recipents = array($this->email);
115 if (!empty($content->bcc) && is_array($content->bcc)) {
116 $recipents =array_merge($recipents, $content->bcc);
118 $headers['Date'] = date('r');
121 'recipients' => $recipents,
122 'headers' => $headers,
132 * - user must be authenticate or args[no_auth] = true
133 * or template = password_[reset|welcome]
136 function sendTemplate($templateFile, $args)
139 $ar = $this->buildMail($templateFile, $args);
142 //print_r($recipents);exit;
143 $mailOptions = PEAR::getStaticProperty('Mail','options');
144 $mail = Mail::factory("SMTP",$mailOptions);
146 if (PEAR::isError($mail)) {
149 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
150 $ret = $mail->send($ar['recipients'],$ar['headers'],$ar['body']);
151 error_reporting($oe);
160 function getEmailFrom()
162 return '"' . addslashes($this->name) . '" <' . $this->email . '>';
164 function toEventString()
166 return empty($this->name) ? $this->email : $this->name;
168 function verifyAuth()
170 $ff= HTML_FlexyFramework::get();
171 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $this->company()->comptype) {
172 $ff->page->jerr("Login not permited to outside companies");
179 // ---------------- authentication / passwords and keys stuff ----------------
182 $db = $this->getDatabaseConnection();
183 // we combine db + project names,
184 // otherwise if projects use different 'auth' objects
185 // then we get unserialize issues.
186 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
190 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
192 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
194 $u = DB_DataObject::factory('Person');
195 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
201 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
205 $u = DB_DataObject::factory('Person');
206 $ff = HTML_FlexyFramework::get();
207 if (!empty($ff->Pman['local_autoauth']) &&
208 (!empty($_SERVER['SERVER_ADDR'])) &&
209 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
210 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
211 $u->get('email', $ff->Pman['local_autoauth'])
213 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
218 $u = DB_DataObject::factory('Person');
220 if (!empty($_SERVER['PHP_AUTH_USER'])
222 !empty($_SERVER['PHP_AUTH_PW'])
224 $u->get('email', $_SERVER['PHP_AUTH_USER'])
226 $u->checkPassword($_SERVER['PHP_AUTH_PW'])
228 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
235 // not in session or not matched...
236 $u = DB_DataObject::factory('Person');
237 $u->whereAdd(' LENGTH(passwd) > 0');
239 $error = PEAR::getStaticProperty('DB_DataObject','lastError');
241 die($error->toString()); // not really a good thing to do...
243 if (!$n){ // authenticated as there are no users in the system...
250 function getAuthUser()
252 if (!$this->isAuth()) {
255 $db = $this->getDatabaseConnection();
256 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
258 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
259 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
261 $u = DB_DataObject::factory('Person');
262 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
268 $u = DB_DataObject::factory('Person');
269 $u->whereAdd(' LENGTH(passwd) > 0');
271 $u = DB_DataObject::factory('Person');
280 $this->isAuth(); // force session start..
282 $db = $this->getDatabaseConnection();
283 // refresh admin group if we are logged in as one..
284 //DB_DataObject::debugLevel(1);
285 $g = DB_DataObject::factory('Groups');
287 $g->get('name', 'Administrators');
288 $gm = DB_DataObject::Factory('group_members');
289 if (in_array($g->id,$gm->listGroupMembership($this))) {
290 // refresh admin groups.
291 $gr = DB_DataObject::Factory('Group_Rights');
292 $gr->applyDefs($g, 0);
295 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
296 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
301 $this->isAuth(); // force session start..
302 $db = $this->getDatabaseConnection();
303 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
304 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
307 function genPassKey ($t)
309 return md5($this->email . $t. $this->passwd);
311 function simpleAuthKey($m = 0)
313 $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
315 return md5(implode(',' , array($month, $this->email , $this->passwd, $this->id)));
317 function checkPassword($val)
320 if (substr($this->passwd,0,1) == '$') {
321 return crypt($val,$this->passwd) == $this->passwd ;
323 // old style md5 passwords...- cant be used with courier....
324 return md5($val) == $this->passwd;
326 function setPassword($value)
329 while(strlen($salt)<9) {
330 $salt.=chr(rand(64,126));
331 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
333 $this->passwd = crypt($value, '$1$'. $salt. '$');
340 $x = DB_DataObject::factory('Companies');
341 $x->get($this->company_id);
344 function loadCompany()
346 $this->company = $this->company();
351 return $this->active;
353 function authUserName($n) // set username prior to acheck user exists query.
356 $this->whereAdd('LENGTH(passwd) > 1');
361 if ($val == $this->lang) {
371 function authUserArray()
374 $aur = $this->toArray();
381 //DB_DataObject::debugLevel(1);
382 $c = DB_Dataobject::factory('Companies');
383 $im = DB_Dataobject::factory('Images');
384 $c->joinAdd($im, 'LEFT');
386 $c->selectAs($c, 'company_id_%s');
387 $c->selectAs($im, 'company_id_logo_id_%s');
388 $c->id = $this->company_id;
392 $aur = array_merge( $c->toArray(),$aur);
394 if (empty($c->company_id_logo_id_id)) {
396 $im = DB_Dataobject::factory('Images');
397 $im->ontable = 'Companies';
399 $im->imgtype = 'LOGO';
402 $im->selectAs($im, 'company_id_logo_id_%s');
403 if ($im->find(true)) {
405 foreach($im->toArray() as $k=>$v) {
412 $aur['perms'] = $this->getPerms();
413 $g = DB_DataObject::Factory('group_members');
414 $aur['groups'] = $g->listGroupMembership($this, 'name');
417 $aur['dailykey'] = '';
424 // ----------PERMS------ ----------------
427 //DB_DataObject::debugLevel(1);
428 // find out all the groups they are a member of.. + Default..
430 // ------ INIITIALIZE IF NO GROUPS ARE SET UP.
432 $g = DB_DataObject::Factory('Group_Rights');
438 return $g->adminRights(); // system is not set up - so they get full rights.
440 //DB_DataObject::debugLevel(1);
441 $g = DB_DataObject::Factory('group_members');
442 $g->whereAdd('group_id is NOT NULL AND user_id IS NOT NULL');
444 // add the current user to the admin group..
445 $g = DB_DataObject::Factory('Groups');
446 if ($g->get('name', 'Administrators')) {
447 $gm = DB_DataObject::Factory('group_members');
448 $gm->group_id = $g->id;
449 $gm->user_id = $this->id;
455 // ------ STANDARD PERMISSION HANDLING.
457 $g = DB_DataObject::Factory('group_members');
458 $grps = $g->listGroupMembership($this);
460 $isAdmin = $g->inAdmin;
461 //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
462 // the load all the perms for those groups, and add them all together..
463 // then load all those
464 $g = DB_DataObject::Factory('Group_Rights');
465 $ret = $g->listPermsFromGroupIds($grps, $isAdmin);
466 //echo '<PRE>';print_r($ret);
472 *Basic group fetching - probably needs to filter by type eventually.
474 *@param String $what - fetchall() argument - eg. 'name' returns names of all groups that they are members of.
477 function groups($what=false)
479 $g = DB_DataObject::Factory('group_members');
480 $grps = $g->listGroupMembership($this);
481 $g = DB_DataObject::Factory('Groups');
482 $g->whereAddIn('id', $grps, 'int');
483 return $g->fetchAll($what);
487 function hasPerm($name, $lvl)
489 static $pcache = array();
491 if (!isset($pcache[$this->id])) {
492 $pcache[$this->id] = $this->getPerms();
494 // echo "<PRE>";print_r($pcache[$au->id]);
495 // var_dump($pcache[$au->id]);
496 if (empty($pcache[$this->id][$name])) {
500 return strpos($pcache[$this->id][$name], $lvl) > -1;
504 // ------------ROO HOOKS------------------------------------
505 function applyFilters($q, $au, $roo)
507 //DB_DataObject::DebugLevel(1);
508 if (!empty($q['query']['person_not_internal'])) {
509 $this->whereAdd(" join_company_id_id.isOwner = 0 ");
513 if (!empty($q['query']['person_internal_only_all'])) {
516 // must be internal and not current user (need for distribution list)
517 // user has a projectdirectory entry and role is not blank.
518 //DB_DataObject::DebugLevel(1);
519 $pd = DB_DataObject::factory('ProjectDirectory');
520 $pd->whereAdd("role != ''");
522 $pd->selectAdd('distinct(person_id) as person_id');
523 $roled = $pd->fetchAll('person_id');
525 {$this->tableName()}.id IN (".implode(',', $roled) . ")
527 $this->whereAdd(" join_company_id_id.comptype = 'OWNER' $rs ");
530 // -- for distribution
531 if (!empty($q['query']['person_internal_only'])) {
532 // must be internal and not current user (need for distribution list)
533 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
535 //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
536 // ".id != ".$au->id);
537 $this->whereAdd("Person.id != {$au->id}");
540 if (!empty($q['query']['comptype_or_company_id'])) {
541 // DB_DataObject::debugLevel(1);
542 $bits = explode(',', $q['query']['comptype_or_company_id']);
543 $id = (int) array_pop($bits);
544 $ct = $this->escape($bits[0]);
546 $this->whereAdd(" join_company_id_id.comptype = '$ct' OR Person.company_id = $id");
552 if (!empty($q['query']['person_inactive'])) {
553 // DB_Dataobject::debugLevel(1);
556 $tn_p = $this->tableName();
557 $tn_gm = DB_DataObject::Factory('group_members')->tableName();
558 $tn_g = DB_DataObject::Factory('Groups')->tableName();
560 ///---------------- Group views --------
561 if (!empty($q['query']['in_group'])) {
562 // DB_DataObject::debugLevel(1);
563 $ing = (int) $q['query']['in_group'];
564 if ($q['query']['in_group'] == -1) {
566 // list all staff who are not in a group.
567 $this->whereAdd("Person.id NOT IN (
568 SELECT distinct(user_id) FROM $tn_gm LEFT JOIN
569 $tn_g ON $tn_g.id = $tn_gm.group_id
570 WHERE $tn_g.type = ".$q['query']['type']."
576 $this->whereAdd("$tn_p.id IN (
577 SELECT distinct(user_id) FROM $tn_gm
578 WHERE group_id = $ing
584 if (!empty($q['query']['not_in_directory'])) {
585 // it's a Person list..
586 // DB_DATaobjecT::debugLevel(1);
588 // specific to project directory which is single comp. login
590 $owncomp = DB_DataObject::Factory('Companies');
591 $owncomp->get('comptype', 'OWNER');
592 if ($q['company_id'] == $owncomp->id) {
598 if ( $q['query']['not_in_directory'] > -1) {
599 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
600 // can list current - so that it does not break!!!
601 $this->whereAdd("$tn_p.id NOT IN
602 ( SELECT distinct person_id FROM $tn_pd WHERE
603 project_id = " . $q['query']['not_in_directory'] . " AND
604 company_id = " . $this->company_id . ')');
608 if (!empty($q['query']['role'])) {
609 // it's a Person list..
610 // DB_DATaobjecT::debugLevel(1);
612 // specific to project directory which is single comp. login
614 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
615 // can list current - so that it does not break!!!
616 $this->whereAdd("$tn_p.id IN
617 ( SELECT distinct person_id FROM $tn_pd WHERE
618 role = '". $this->escape($q['query']['role']) ."'
624 if (!empty($q['query']['project_member_of'])) {
625 // this is also a flag to return if they are a member..
626 //DB_DataObject::debugLevel(1);
627 $do = DB_DataObject::factory('ProjectDirectory');
628 $do->project_id = $q['query']['project_member_of'];
629 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
630 $this->joinAdd($do,array('joinType' => 'LEFT', 'useWhereAsOn' => true));
631 $this->selectAdd("IF($tn_pd.id IS NULL, 0, $tn_pd.id ) as is_member");
634 if (!empty($q['query']['project_member_filter'])) {
635 $this->having('is_member !=0');
642 if (!empty($q['query']['search'])) {
643 $s = $this->escape($q['query']['search']);
645 $tn_p.name LIKE '%$s%' OR
646 $tn_p.email LIKE '%$s%' OR
647 $tn_p.role LIKE '%$s%' OR
648 $tn_p.remarks LIKE '%$s%'
655 function setFromRoo($ar, $roo)
658 if (!empty($ar['passwd1'])) {
659 $this->setPassword($ar['passwd1']);
664 ($this->email == $roo->old->email)&&
665 ($this->company_id == $roo->old->company_id)
669 if (empty($this->email)) {
672 $xx = DB_Dataobject::factory('Person');
674 'email' => $this->email,
675 // 'company_id' => $x->company_id
679 return "Duplicate Email found";
685 * before Delete - delete significant dependancies..
686 * this is called after checkPerm..
689 function beforeDelete()
692 $e = DB_DataObject::Factory('Events');
693 $e->whereAdd('person_id = ' . $this->id);
702 * Check if the a user has access to modify this item.
703 * @param String $lvl Level (eg. Core.Projects)
704 * @param Pman_Core_DataObjects_Person $au The authenticated user.
705 * @param boolean $changes alllow changes???
707 * @return false if no access..
709 function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
712 // do we have an empty system..
713 if ($au && $au->id == -1) {
717 // determine if it's staff!!!
719 if ($au->company()->comptype != 'OWNER') {
721 // - can not change company!!!
723 isset($changes['company_id']) &&
724 $changes['company_id'] != $au->company_id) {
727 // can only set new emails..
729 !empty($this->email) &&
730 isset($changes['email']) &&
731 $changes['email'] != $this->email) {
735 // edit self... - what about other staff members...
737 return $this->company_id == $au->company_id;
741 // yes, only owner company can mess with this...
742 $owncomp = DB_DataObject::Factory('Companies');
743 $owncomp->get('comptype', 'OWNER');
745 $isStaff = ($this->company_id == $owncomp->id);
749 // extra case change passwod?
750 case 'P': //??? password
751 // standard perms -- for editing + if the user is dowing them selves..
752 $ret = $isStaff ? $au->hasPerm("Core.Staff", "E") : $au->hasPerm("Core.Person", "E");
753 return $ret || $au->id == $this->id;
756 return $isStaff ? $au->hasPerm("Core.Staff", $lvl) : $au->hasPerm("Core.Person", $lvl);
761 function onInsert($req, $roo)
764 if ($roo->authUser->id < 0) {
765 $g = DB_DataObject::factory('Groups');
767 $g->get('name', 'Administrators');
769 $p = DB_DataObject::factory('group_members');
770 $p->group_id = $g->id;
771 $p->user_id = $this->id;
774 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
778 if (!empty($req['project_id_addto'])) {
779 $pd = DB_DataObject::factory('ProjectDirectory');
780 $pd->project_id = $req['project_id_addto'];
781 $pd->person_id = $this->id;
783 $pd->office_id = $this->office_id;
784 $pd->company_id = $this->company_id;