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'];
99 require_once 'HTML/Template/Flexy.php';
100 $template = new HTML_Template_Flexy( $tops );
105 $template->compile("mail/$templateFile.txt");
107 /* use variables from this object to ouput data. */
108 $mailtext = $template->bufferedOutputObject($content);
109 //echo "<PRE>";print_R($mailtext);
110 //print_R($mailtext);exit;
111 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
112 require_once 'Mail/mimeDecode.php';
113 require_once 'Mail.php';
115 $decoder = new Mail_mimeDecode($mailtext);
116 $parts = $decoder->getSendArray();
117 if (PEAR::isError($parts)) {
119 //echo "PROBLEM: {$parts->message}";
122 list($recipents,$headers,$body) = $parts;
123 $recipents = array($this->email);
124 if (!empty($content->bcc) && is_array($content->bcc)) {
125 $recipents =array_merge($recipents, $content->bcc);
127 $headers['Date'] = date('r');
130 'recipients' => $recipents,
131 'headers' => $headers,
141 * - user must be authenticate or args[no_auth] = true
142 * or template = password_[reset|welcome]
145 function sendTemplate($templateFile, $args)
148 $ar = $this->buildMail($templateFile, $args);
151 //print_r($recipents);exit;
152 $mailOptions = PEAR::getStaticProperty('Mail','options');
153 $mail = Mail::factory("SMTP",$mailOptions);
155 if (PEAR::isError($mail)) {
158 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
159 $ret = $mail->send($ar['recipients'],$ar['headers'],$ar['body']);
160 error_reporting($oe);
169 function getEmailFrom()
171 return '"' . addslashes($this->name) . '" <' . $this->email . '>';
173 function toEventString()
175 return empty($this->name) ? $this->email : $this->name;
177 function verifyAuth()
179 $ff= HTML_FlexyFramework::get();
180 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $this->company()->comptype) {
181 $ff->page->jerr("Login not permited to outside companies");
188 // ---------------- authentication / passwords and keys stuff ----------------
191 $db = $this->getDatabaseConnection();
192 // we combine db + project names,
193 // otherwise if projects use different 'auth' objects
194 // then we get unserialize issues.
195 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
199 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
201 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
203 $u = DB_DataObject::factory('Person');
204 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
210 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
214 $u = DB_DataObject::factory('Person');
215 $ff = HTML_FlexyFramework::get();
216 if (!empty($ff->Pman['local_autoauth']) &&
217 (!empty($_SERVER['SERVER_ADDR'])) &&
218 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
219 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
220 $u->get('email', $ff->Pman['local_autoauth'])
222 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
227 $u = DB_DataObject::factory('Person');
229 if (!empty($_SERVER['PHP_AUTH_USER'])
231 !empty($_SERVER['PHP_AUTH_PW'])
233 $u->get('email', $_SERVER['PHP_AUTH_USER'])
235 $u->checkPassword($_SERVER['PHP_AUTH_PW'])
237 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
244 // not in session or not matched...
245 $u = DB_DataObject::factory('Person');
246 $u->whereAdd(' LENGTH(passwd) > 0');
248 $error = PEAR::getStaticProperty('DB_DataObject','lastError');
250 die($error->toString()); // not really a good thing to do...
252 if (!$n){ // authenticated as there are no users in the system...
259 function getAuthUser()
261 if (!$this->isAuth()) {
264 $db = $this->getDatabaseConnection();
265 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
267 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
268 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
270 $u = DB_DataObject::factory('Person');
271 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
277 $u = DB_DataObject::factory('Person');
278 $u->whereAdd(' LENGTH(passwd) > 0');
280 $u = DB_DataObject::factory('Person');
289 $this->isAuth(); // force session start..
291 $db = $this->getDatabaseConnection();
292 // refresh admin group if we are logged in as one..
293 //DB_DataObject::debugLevel(1);
294 $g = DB_DataObject::factory('Groups');
296 $g->get('name', 'Administrators');
297 $gm = DB_DataObject::Factory('group_members');
298 if (in_array($g->id,$gm->listGroupMembership($this))) {
299 // refresh admin groups.
300 $gr = DB_DataObject::Factory('group_rights');
301 $gr->applyDefs($g, 0);
304 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
305 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
310 $this->isAuth(); // force session start..
311 $db = $this->getDatabaseConnection();
312 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
313 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
316 function genPassKey ($t)
318 return md5($this->email . $t. $this->passwd);
320 function simpleAuthKey($m = 0)
322 $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
324 return md5(implode(',' , array($month, $this->email , $this->passwd, $this->id)));
326 function checkPassword($val)
329 if (substr($this->passwd,0,1) == '$') {
330 return crypt($val,$this->passwd) == $this->passwd ;
332 // old style md5 passwords...- cant be used with courier....
333 return md5($val) == $this->passwd;
335 function setPassword($value)
338 while(strlen($salt)<9) {
339 $salt.=chr(rand(64,126));
340 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
342 $this->passwd = crypt($value, '$1$'. $salt. '$');
349 $x = DB_DataObject::factory('Companies');
350 $x->get($this->company_id);
353 function loadCompany()
355 $this->company = $this->company();
360 return $this->active;
362 function authUserName($n) // set username prior to acheck user exists query.
365 $this->whereAdd('LENGTH(passwd) > 1');
370 if ($val == $this->lang) {
380 function authUserArray()
383 $aur = $this->toArray();
390 //DB_DataObject::debugLevel(1);
391 $c = DB_Dataobject::factory('Companies');
392 $im = DB_Dataobject::factory('Images');
393 $c->joinAdd($im, 'LEFT');
395 $c->selectAs($c, 'company_id_%s');
396 $c->selectAs($im, 'company_id_logo_id_%s');
397 $c->id = $this->company_id;
401 $aur = array_merge( $c->toArray(),$aur);
403 if (empty($c->company_id_logo_id_id)) {
405 $im = DB_Dataobject::factory('Images');
406 $im->ontable = 'Companies';
408 $im->imgtype = 'LOGO';
411 $im->selectAs($im, 'company_id_logo_id_%s');
412 if ($im->find(true)) {
414 foreach($im->toArray() as $k=>$v) {
421 $aur['perms'] = $this->getPerms();
422 $g = DB_DataObject::Factory('group_members');
423 $aur['groups'] = $g->listGroupMembership($this, 'name');
426 $aur['dailykey'] = '';
433 // ----------PERMS------ ----------------
436 //DB_DataObject::debugLevel(1);
437 // find out all the groups they are a member of.. + Default..
439 // ------ INIITIALIZE IF NO GROUPS ARE SET UP.
441 $g = DB_DataObject::Factory('group_rights');
447 return $g->adminRights(); // system is not set up - so they get full rights.
449 //DB_DataObject::debugLevel(1);
450 $g = DB_DataObject::Factory('group_members');
451 $g->whereAdd('group_id is NOT NULL AND user_id IS NOT NULL');
453 // add the current user to the admin group..
454 $g = DB_DataObject::Factory('Groups');
455 if ($g->get('name', 'Administrators')) {
456 $gm = DB_DataObject::Factory('group_members');
457 $gm->group_id = $g->id;
458 $gm->user_id = $this->id;
464 // ------ STANDARD PERMISSION HANDLING.
465 $isOwner = $this->company()->comptype == 'OWNER';
466 $g = DB_DataObject::Factory('group_members');
467 $grps = $g->listGroupMembership($this);
469 $isAdmin = $g->inAdmin;
470 //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
471 // the load all the perms for those groups, and add them all together..
472 // then load all those
473 $g = DB_DataObject::Factory('group_rights');
474 $ret = $g->listPermsFromGroupIds($grps, $isAdmin, $isOwner);
475 //echo '<PRE>';print_r($ret);
481 *Basic group fetching - probably needs to filter by type eventually.
483 *@param String $what - fetchall() argument - eg. 'name' returns names of all groups that they are members of.
486 function groups($what=false)
488 $g = DB_DataObject::Factory('group_members');
489 $grps = $g->listGroupMembership($this);
490 $g = DB_DataObject::Factory('Groups');
491 $g->whereAddIn('id', $grps, 'int');
492 return $g->fetchAll($what);
496 function hasPerm($name, $lvl)
498 static $pcache = array();
500 if (!isset($pcache[$this->id])) {
501 $pcache[$this->id] = $this->getPerms();
503 // echo "<PRE>";print_r($pcache[$au->id]);
504 // var_dump($pcache[$au->id]);
505 if (empty($pcache[$this->id][$name])) {
509 return strpos($pcache[$this->id][$name], $lvl) > -1;
513 // ------------ROO HOOKS------------------------------------
514 function applyFilters($q, $au, $roo)
516 //DB_DataObject::DebugLevel(1);
517 if (!empty($q['query']['person_not_internal'])) {
518 $this->whereAdd(" join_company_id_id.isOwner = 0 ");
522 if (!empty($q['query']['person_internal_only_all'])) {
525 // must be internal and not current user (need for distribution list)
526 // user has a projectdirectory entry and role is not blank.
527 //DB_DataObject::DebugLevel(1);
528 $pd = DB_DataObject::factory('ProjectDirectory');
529 $pd->whereAdd("role != ''");
531 $pd->selectAdd('distinct(person_id) as person_id');
532 $roled = $pd->fetchAll('person_id');
534 {$this->tableName()}.id IN (".implode(',', $roled) . ")
536 $this->whereAdd(" join_company_id_id.comptype = 'OWNER' $rs ");
539 // -- for distribution
540 if (!empty($q['query']['person_internal_only'])) {
541 // must be internal and not current user (need for distribution list)
542 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
544 //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
545 // ".id != ".$au->id);
546 $this->whereAdd("Person.id != {$au->id}");
549 if (!empty($q['query']['comptype_or_company_id'])) {
550 // DB_DataObject::debugLevel(1);
551 $bits = explode(',', $q['query']['comptype_or_company_id']);
552 $id = (int) array_pop($bits);
553 $ct = $this->escape($bits[0]);
555 $this->whereAdd(" join_company_id_id.comptype = '$ct' OR Person.company_id = $id");
561 if (!empty($q['query']['person_inactive'])) {
562 // DB_Dataobject::debugLevel(1);
565 $tn_p = $this->tableName();
566 $tn_gm = DB_DataObject::Factory('group_members')->tableName();
567 $tn_g = DB_DataObject::Factory('Groups')->tableName();
569 ///---------------- Group views --------
570 if (!empty($q['query']['in_group'])) {
571 // DB_DataObject::debugLevel(1);
572 $ing = (int) $q['query']['in_group'];
573 if ($q['query']['in_group'] == -1) {
575 // list all staff who are not in a group.
576 $this->whereAdd("Person.id NOT IN (
577 SELECT distinct(user_id) FROM $tn_gm LEFT JOIN
578 $tn_g ON $tn_g.id = $tn_gm.group_id
579 WHERE $tn_g.type = ".$q['query']['type']."
585 $this->whereAdd("$tn_p.id IN (
586 SELECT distinct(user_id) FROM $tn_gm
587 WHERE group_id = $ing
593 if (!empty($q['query']['not_in_directory'])) {
594 // it's a Person list..
595 // DB_DATaobjecT::debugLevel(1);
597 // specific to project directory which is single comp. login
599 $owncomp = DB_DataObject::Factory('Companies');
600 $owncomp->get('comptype', 'OWNER');
601 if ($q['company_id'] == $owncomp->id) {
607 if ( $q['query']['not_in_directory'] > -1) {
608 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
609 // can list current - so that it does not break!!!
610 $this->whereAdd("$tn_p.id NOT IN
611 ( SELECT distinct person_id FROM $tn_pd WHERE
612 project_id = " . $q['query']['not_in_directory'] . " AND
613 company_id = " . $this->company_id . ')');
617 if (!empty($q['query']['role'])) {
618 // it's a Person list..
619 // DB_DATaobjecT::debugLevel(1);
621 // specific to project directory which is single comp. login
623 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
624 // can list current - so that it does not break!!!
625 $this->whereAdd("$tn_p.id IN
626 ( SELECT distinct person_id FROM $tn_pd WHERE
627 role = '". $this->escape($q['query']['role']) ."'
633 if (!empty($q['query']['project_member_of'])) {
634 // this is also a flag to return if they are a member..
635 //DB_DataObject::debugLevel(1);
636 $do = DB_DataObject::factory('ProjectDirectory');
637 $do->project_id = $q['query']['project_member_of'];
638 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
639 $this->joinAdd($do,array('joinType' => 'LEFT', 'useWhereAsOn' => true));
640 $this->selectAdd("IF($tn_pd.id IS NULL, 0, $tn_pd.id ) as is_member");
643 if (!empty($q['query']['project_member_filter'])) {
644 $this->having('is_member !=0');
651 if (!empty($q['query']['search'])) {
652 $s = $this->escape($q['query']['search']);
654 $tn_p.name LIKE '%$s%' OR
655 $tn_p.email LIKE '%$s%' OR
656 $tn_p.role LIKE '%$s%' OR
657 $tn_p.remarks LIKE '%$s%'
664 function setFromRoo($ar, $roo)
667 if (!empty($ar['passwd1'])) {
668 $this->setPassword($ar['passwd1']);
673 ($this->email == $roo->old->email)&&
674 ($this->company_id == $roo->old->company_id)
678 if (empty($this->email)) {
681 $xx = DB_Dataobject::factory('Person');
683 'email' => $this->email,
684 // 'company_id' => $x->company_id
688 return "Duplicate Email found";
694 * before Delete - delete significant dependancies..
695 * this is called after checkPerm..
698 function beforeDelete()
701 $e = DB_DataObject::Factory('Events');
702 $e->whereAdd('person_id = ' . $this->id);
711 * Check if the a user has access to modify this item.
712 * @param String $lvl Level (eg. Core.Projects)
713 * @param Pman_Core_DataObjects_Person $au The authenticated user.
714 * @param boolean $changes alllow changes???
716 * @return false if no access..
718 function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
721 // do we have an empty system..
722 if ($au && $au->id == -1) {
726 // determine if it's staff!!!
728 if ($au->company()->comptype != 'OWNER') {
730 // - can not change company!!!
732 isset($changes['company_id']) &&
733 $changes['company_id'] != $au->company_id) {
736 // can only set new emails..
738 !empty($this->email) &&
739 isset($changes['email']) &&
740 $changes['email'] != $this->email) {
744 // edit self... - what about other staff members...
746 return $this->company_id == $au->company_id;
750 // yes, only owner company can mess with this...
751 $owncomp = DB_DataObject::Factory('Companies');
752 $owncomp->get('comptype', 'OWNER');
754 $isStaff = ($this->company_id == $owncomp->id);
758 // extra case change passwod?
759 case 'P': //??? password
760 // standard perms -- for editing + if the user is dowing them selves..
761 $ret = $isStaff ? $au->hasPerm("Core.Staff", "E") : $au->hasPerm("Core.Person", "E");
762 return $ret || $au->id == $this->id;
765 return $isStaff ? $au->hasPerm("Core.Staff", $lvl) : $au->hasPerm("Core.Person", $lvl);
770 function onInsert($req, $roo)
773 if ($roo->authUser->id < 0) {
774 $g = DB_DataObject::factory('Groups');
776 $g->get('name', 'Administrators');
778 $p = DB_DataObject::factory('group_members');
779 $p->group_id = $g->id;
780 $p->user_id = $this->id;
783 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
787 if (!empty($req['project_id_addto'])) {
788 $pd = DB_DataObject::factory('ProjectDirectory');
789 $pd->project_id = $req['project_id_addto'];
790 $pd->person_id = $this->id;
792 $pd->office_id = $this->office_id;
793 $pd->company_id = $this->company_id;