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 $office_id; // int(11)
15 public $name; // string(128) not_null
16 public $phone; // string(32) not_null
17 public $fax; // string(32) not_null
18 public $email; // string(128) not_null
19 public $company_id; // int(11)
20 public $role; // string(32) not_null
21 public $active; // int(11) not_null
22 public $remarks; // blob(65535) not_null blob
23 public $passwd; // string(64) not_null
24 public $owner_id; // int(11) not_null
25 public $lang; // string(8)
26 public $no_reset_sent; // int(11)
27 public $action_type; // string(32)
28 public $project_id; // int(11)
29 public $deleted_by; // int(11) not_null
30 public $deleted_dt; // datetime(19) binary
33 /* the code above is auto generated do not remove the tag below */
37 * @param {String} $templateFile (mail/XXXXXXX.txt) exclude the mail and .txt bit.
38 * @param {Array|Object} $args data to send out..
39 * @return {Array|PEAR_Error} array of $recipents, $header, $body
41 function buildMail($templateFile, $args)
44 $args = (array) $args;
45 $content = clone($this);
47 foreach((array)$args as $k=>$v) {
51 $ff = HTML_FlexyFramework::get();
54 //?? is this really the place for this???
57 empty($args['no_auth']) &&
58 !in_array($templateFile, array(
59 // templates that can be sent without authentication.
65 $content->authUser = $this->getAuthUser();
66 if (!$content->authUser) {
67 return PEAR::raiseError("Not authenticated");
71 // should handle x-forwarded...
73 $content->HTTP_HOST = isset($_SERVER["HTTP_HOST"]) ?
74 $_SERVER["HTTP_HOST"] :
75 (isset($ff->HTTP_HOST) ? $ff->HTTP_HOST : 'localhost');
77 /* use the regex compiler, as it doesnt parse <tags */
78 require_once 'HTML/Template/Flexy.php';
79 $template = new HTML_Template_Flexy( array(
80 'compiler' => 'Flexy',
82 'filters' => array('SimpleTags','Mail'),
89 $template->compile("mail/$templateFile.txt");
91 /* use variables from this object to ouput data. */
92 $mailtext = $template->bufferedOutputObject($content);
93 //echo "<PRE>";print_R($mailtext);
94 //print_R($mailtext);exit;
95 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
96 require_once 'Mail/mimeDecode.php';
97 require_once 'Mail.php';
99 $decoder = new Mail_mimeDecode($mailtext);
100 $parts = $decoder->getSendArray();
101 if (PEAR::isError($parts)) {
103 //echo "PROBLEM: {$parts->message}";
106 list($recipents,$headers,$body) = $parts;
107 $recipents = array($this->email);
108 if (!empty($content->bcc) && is_array($content->bcc)) {
109 $recipents =array_merge($recipents, $content->bcc);
111 $headers['Date'] = date('r');
114 'recipients' => $recipents,
115 'headers' => $headers,
125 * - user must be authenticate or args[no_auth] = true
126 * or template = password_[reset|welcome]
129 function sendTemplate($templateFile, $args)
132 $ar = $this->buildMail($templateFile, $args);
135 //print_r($recipents);exit;
136 $mailOptions = PEAR::getStaticProperty('Mail','options');
137 $mail = Mail::factory("SMTP",$mailOptions);
139 if (PEAR::isError($mail)) {
142 $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
143 $ret = $mail->send($ar['recipients'],$ar['headers'],$ar['body']);
144 error_reporting($oe);
153 function getEmailFrom()
155 return '"' . addslashes($this->name) . '" <' . $this->email . '>';
157 function toEventString()
159 return empty($this->name) ? $this->email : $this->name;
161 function verifyAuth()
163 $ff= HTML_FlexyFramework::get();
164 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $this->company()->comptype) {
165 $ff->page->jerr("Login not permited to outside companies");
172 // ---------------- authentication / passwords and keys stuff ----------------
175 $db = $this->getDatabaseConnection();
176 // we combine db + project names,
177 // otherwise if projects use different 'auth' objects
178 // then we get unserialize issues.
179 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
183 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
185 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
187 $u = DB_DataObject::factory('Person');
188 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
194 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
198 $u = DB_DataObject::factory('Person');
199 $ff = HTML_FlexyFramework::get();
200 if (!empty($ff->Pman['local_autoauth']) &&
201 (!empty($_SERVER['SERVER_ADDR'])) &&
202 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
203 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
204 $u->get('email', $ff->Pman['local_autoauth'])
206 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
211 // not in session or not matched...
212 $u = DB_DataObject::factory('Person');
213 $u->whereAdd(' LENGTH(passwd) > 0');
215 $error = PEAR::getStaticProperty('DB_DataObject','lastError');
217 die($error->toString()); // not really a good thing to do...
219 if (!$n){ // authenticated as there are no users in the system...
226 function getAuthUser()
228 if (!$this->isAuth()) {
231 $db = $this->getDatabaseConnection();
232 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
234 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
235 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
237 $u = DB_DataObject::factory('Person');
238 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
244 $u = DB_DataObject::factory('Person');
245 $u->whereAdd(' LENGTH(passwd) > 0');
247 $u = DB_DataObject::factory('Person');
256 $this->isAuth(); // force session start..
258 $db = $this->getDatabaseConnection();
259 // refresh admin group if we are logged in as one..
260 //DB_DataObject::debugLevel(1);
261 $g = DB_DataObject::factory('Groups');
263 $g->get('name', 'Administrators');
264 $gm = DB_DataObject::Factory('Group_Members');
265 if (in_array($g->id,$gm->listGroupMembership($this))) {
266 // refresh admin groups.
267 $gr = DB_DataObject::Factory('Group_Rights');
268 $gr->applyDefs($g, 0);
271 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
272 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
277 $this->isAuth(); // force session start..
278 $db = $this->getDatabaseConnection();
279 $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
280 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
283 function genPassKey ($t)
285 return md5($this->email . $t. $this->passwd);
287 function simpleAuthKey($m = 0)
289 $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
291 return md5(implode(',' , array($month, $this->email , $this->passwd, $this->id)));
293 function checkPassword($val)
296 if (substr($this->passwd,0,1) == '$') {
297 return crypt($val,$this->passwd) == $this->passwd ;
299 // old style md5 passwords...- cant be used with courier....
300 return md5($val) == $this->passwd;
302 function setPassword($value)
305 while(strlen($salt)<9) {
306 $salt.=chr(rand(64,126));
307 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
309 $this->passwd = crypt($value, '$1$'. $salt. '$');
316 $x = DB_DataObject::factory('Companies');
317 $x->get($this->company_id);
324 return $this->active;
326 function authUserName($n) // set username prior to acheck user exists query.
329 $this->whereAdd('LENGTH(passwd) > 1');
334 if ($val == $this->lang) {
344 function authUserArray()
347 $aur = $this->toArray();
354 //DB_DataObject::debugLevel(1);
355 $c = DB_Dataobject::factory('Companies');
356 $im = DB_Dataobject::factory('Images');
357 $c->joinAdd($im, 'LEFT');
359 $c->selectAs($c, 'company_id_%s');
360 $c->selectAs($im, 'company_id_logo_id_%s');
361 $c->id = $this->company_id;
365 $aur = array_merge( $c->toArray(),$aur);
367 if (empty($c->company_id_logo_id_id)) {
369 $im = DB_Dataobject::factory('Images');
370 $im->ontable = 'Companies';
372 $im->imgtype = 'LOGO';
375 $im->selectAs($im, 'company_id_logo_id_%s');
376 if ($im->find(true)) {
378 foreach($im->toArray() as $k=>$v) {
385 $aur['perms'] = $this->getPerms();
386 $g = DB_DataObject::Factory('Group_Members');
387 $aur['groups'] = $g->listGroupMembership($this, 'name');
390 $aur['dailykey'] = '';
397 // ----------PERMS------ ----------------
400 //DB_DataObject::debugLevel(1);
401 // find out all the groups they are a member of.. + Default..
403 // ------ INIITIALIZE IF NO GROUPS ARE SET UP.
405 $g = DB_DataObject::Factory('Group_Rights');
411 return $g->adminRights(); // system is not set up - so they get full rights.
413 //DB_DataObject::debugLevel(1);
414 $g = DB_DataObject::Factory('Group_Members');
415 $g->whereAdd('group_id is NOT NULL AND user_id IS NOT NULL');
417 // add the current user to the admin group..
418 $g = DB_DataObject::Factory('Groups');
419 if ($g->get('name', 'Administrators')) {
420 $gm = DB_DataObject::Factory('Group_Members');
421 $gm->group_id = $g->id;
422 $gm->user_id = $this->id;
428 // ------ STANDARD PERMISSION HANDLING.
430 $g = DB_DataObject::Factory('Group_Members');
431 $grps = $g->listGroupMembership($this);
433 $isAdmin = $g->inAdmin;
434 //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
435 // the load all the perms for those groups, and add them all together..
436 // then load all those
437 $g = DB_DataObject::Factory('Group_Rights');
438 $ret = $g->listPermsFromGroupIds($grps, $isAdmin);
439 //echo '<PRE>';print_r($ret);
445 *Basic group fetching - probably needs to filter by type eventually.
447 *@param String $what - fetchall() argument - eg. 'name' returns names of all groups that they are members of.
450 function groups($what=false)
452 $g = DB_DataObject::Factory('Group_Members');
453 $grps = $g->listGroupMembership($this);
454 $g = DB_DataObject::Factory('Groups');
455 $g->whereAddIn('id', $grps, 'int');
456 return $g->fetchAll($what);
460 function hasPerm($name, $lvl)
462 static $pcache = array();
464 if (!isset($pcache[$this->id])) {
465 $pcache[$this->id] = $this->getPerms();
467 // echo "<PRE>";print_r($pcache[$au->id]);
468 // var_dump($pcache[$au->id]);
469 if (empty($pcache[$this->id][$name])) {
473 return strpos($pcache[$this->id][$name], $lvl) > -1;
477 // ------------ROO HOOKS------------------------------------
478 function applyFilters($q, $au, $roo)
480 //DB_DataObject::DebugLevel(1);
481 if (!empty($q['query']['person_not_internal'])) {
482 $this->whereAdd(" join_company_id_id.isOwner = 0 ");
484 if (!empty($q['query']['person_internal_only_all'])) {
487 // must be internal and not current user (need for distribution list)
488 // user has a projectdirectory entry and role is not blank.
489 //DB_DataObject::DebugLevel(1);
490 $pd = DB_DataObject::factory('ProjectDirectory');
491 $pd->whereAdd("role != ''");
493 $pd->selectAdd('distinct(person_id) as person_id');
494 $roled = $pd->fetchAll('person_id');
496 {$this->tableName()}.id IN (".implode(',', $roled) . ")
498 $this->whereAdd(" join_company_id_id.comptype = 'OWNER' $rs ");
501 // -- for distribution
502 if (!empty($q['query']['person_internal_only'])) {
503 // must be internal and not current user (need for distribution list)
504 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
506 //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
507 // ".id != ".$au->id);
508 $this->whereAdd("Person.id != {$au->id}");
511 if (!empty($q['query']['comptype_or_company_id'])) {
512 // DB_DataObject::debugLevel(1);
513 $bits = explode(',', $q['query']['comptype_or_company_id']);
514 $id = (int) array_pop($bits);
515 $ct = $this->escape($bits[0]);
517 $this->whereAdd(" join_company_id_id.comptype = '$ct' OR Person.company_id = $id");
523 if (!empty($q['query']['person_inactive'])) {
524 // DB_Dataobject::debugLevel(1);
527 $tn_p = $this->tableName();
528 $tn_gm = DB_DataObject::Factory('Group_Members')->tableName();
529 $tn_g = DB_DataObject::Factory('Groups')->tableName();
531 ///---------------- Group views --------
532 if (!empty($q['query']['in_group'])) {
533 // DB_DataObject::debugLevel(1);
534 $ing = (int) $q['query']['in_group'];
535 if ($q['query']['in_group'] == -1) {
537 // list all staff who are not in a group.
538 $this->whereAdd("Person.id NOT IN (
539 SELECT distinct(user_id) FROM $tn_gm LEFT JOIN
540 $tn_g ON $tn_g.id = $tn_gm.group_id
541 WHERE $tn_g.type = ".$q['query']['type']."
547 $this->whereAdd("$tn_p.id IN (
548 SELECT distinct(user_id) FROM $tn_gm
549 WHERE group_id = $ing
555 if (!empty($q['query']['not_in_directory'])) {
556 // it's a Person list..
557 // DB_DATaobjecT::debugLevel(1);
559 // specific to project directory which is single comp. login
561 $owncomp = DB_DataObject::Factory('Companies');
562 $owncomp->get('comptype', 'OWNER');
563 if ($q['company_id'] == $owncomp->id) {
569 if ( $q['query']['not_in_directory'] > -1) {
570 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
571 // can list current - so that it does not break!!!
572 $x->whereAdd("$tn_p.id NOT IN
573 ( SELECT distinct person_id FROM $tn_pd WHERE
574 project_id = " . $q['query']['not_in_directory'] . " AND
575 company_id = " . $this->company_id . ')');
580 if (!empty($q['query']['project_member_of'])) {
581 // this is also a flag to return if they are a member..
582 //DB_DataObject::debugLevel(1);
583 $do = DB_DataObject::factory('ProjectDirectory');
584 $do->project_id = $q['query']['project_member_of'];
585 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
586 $this->joinAdd($do,array('joinType' => 'LEFT', 'useWhereAsOn' => true));
587 $this->selectAdd("IF($tn_pd.id IS NULL, 0, $tn_pd.id ) as is_member");
590 if (!empty($q['query']['project_member_filter'])) {
591 $this->having('is_member !=0');
598 if (!empty($q['query']['search'])) {
599 $s = $this->escape($q['query']['search']);
601 $tn_p.name LIKE '%$s%' OR
602 $tn_p.email LIKE '%$s%' OR
603 $tn_p.role LIKE '%$s%' OR
604 $tn_p.remarks LIKE '%$s%'
611 function setFromRoo($ar, $roo)
614 if (!empty($ar['passwd1'])) {
615 $this->setPassword($ar['passwd1']);
620 ($this->email == $roo->old->email)&&
621 ($this->company_id == $roo->old->company_id)
625 if (empty($this->email)) {
628 $xx = DB_Dataobject::factory('Person');
630 'email' => $this->email,
631 // 'company_id' => $x->company_id
635 return "Duplicate Email found";
641 * before Delete - delete significant dependancies..
642 * this is called after checkPerm..
645 function beforeDelete()
648 $e = DB_DataObject::Factory('Events');
649 $e->whereAdd('person_id = ' . $this->id);
658 * Check if the a user has access to modify this item.
659 * @param String $lvl Level (eg. Core.Projects)
660 * @param Pman_Core_DataObjects_Person $au The authenticated user.
661 * @param boolean $changes alllow changes???
663 * @return false if no access..
665 function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
668 // do we have an empty system..
669 if ($au && $au->id == -1) {
673 // determine if it's staff!!!
675 if ($au->company()->comptype != 'OWNER') {
677 // - can not change company!!!
679 isset($changes['company_id']) &&
680 $changes['company_id'] != $au->company_id) {
683 // can only set new emails..
685 !empty($this->email) &&
686 isset($changes['email']) &&
687 $changes['email'] != $this->email) {
691 // edit self... - what about other staff members...
693 return $this->company_id == $au->company_id;
697 // yes, only owner company can mess with this...
698 $owncomp = DB_DataObject::Factory('Companies');
699 $owncomp->get('comptype', 'OWNER');
701 $isStaff = ($this->company_id == $owncomp->id);
705 // extra case change passwod?
706 case 'P': //??? password
707 // standard perms -- for editing + if the user is dowing them selves..
708 $ret = $isStaff ? $au->hasPerm("Core.Staff", "E") : $au->hasPerm("Core.Person", "E");
709 return $ret || $au->id == $this->id;
712 return $isStaff ? $au->hasPerm("Core.Staff", $lvl) : $au->hasPerm("Core.Person", $lvl);
717 function onInsert($req, $roo)
720 if ($roo->authUser->id < 0) {
721 $g = DB_DataObject::factory('Groups');
723 $g->get('name', 'Administrators');
725 $p = DB_DataObject::factory('Group_Members');
726 $p->group_id = $g->id;
727 $p->user_id = $this->id;
730 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
734 if (!empty($req['project_id_addto'])) {
735 $pd = DB_DataObject::factory('ProjectDirectory');
736 $pd->project_id = $req['project_id_addto'];
737 $pd->person_id = $this->id;
739 $pd->office_id = $this->office_id;
740 $pd->company_id = $this->company_id;