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 * - user must be authenticate or args[no_auth] = true
38 * or template = password_[reset|welcome]
41 function sendTemplate($templateFile, $args)
46 $content = clone($this);
48 foreach((array)$args as $k=>$v) {
52 if (empty($args['no_auth']) && !in_array($templateFile, array(
53 // templates that can be sent without authentication.
57 $content->authUser = $this->getAuthUser();
58 if (!$content->authUser) {
59 return PEAR::raiseError("Not authenticated");
63 $content->HTTP_HOST = $_SERVER["HTTP_HOST"];
64 /* use the regex compiler, as it doesnt parse <tags */
65 require_once 'HTML/Template/Flexy.php';
66 $template = new HTML_Template_Flexy( array(
67 'compiler' => 'Regex',
68 'filters' => array('SimpleTags','Mail'),
75 $template->compile("mail/$templateFile.txt");
77 /* use variables from this object to ouput data. */
78 $mailtext = $template->bufferedOutputObject($content);
79 //echo "<PRE>";print_R($mailtext);
81 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
82 require_once 'Mail/mimeDecode.php';
83 require_once 'Mail.php';
85 $decoder = new Mail_mimeDecode($mailtext);
86 $parts = $decoder->getSendArray();
87 if (PEAR::isError($parts)) {
89 //echo "PROBLEM: {$parts->message}";
92 list($recipents,$headers,$body) = $parts;
93 $recipents = array($this->email);
94 if (!empty($content->bcc) && is_array($content->bcc)) {
95 $recipents =array_merge($recipents, $content->bcc);
97 //print_r($recipents);exit;
98 $mailOptions = PEAR::getStaticProperty('Mail','options');
99 $mail = Mail::factory("SMTP",$mailOptions);
100 $headers['Date'] = date('r');
101 if (PEAR::isError($mail)) {
104 $oe = error_reporting(E_ALL ^ E_NOTICE);
105 $ret = $mail->send($recipents,$headers,$body);
106 error_reporting($oe);
111 function getEmailFrom()
113 return '"' . addslashes($this->name) . '" <' . $this->email . '>';
115 function toEventString()
117 return empty($this->name) ? $this->email : $this->name;
119 function verifyAuth()
121 $ff= HTML_FlexyFramework::get();
122 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $this->company()->comptype) {
123 $ff->page->jerr("Login not permited to outside companies");
130 // ---------------- authentication / passwords and keys stuff ----------------
133 $db = $this->getDatabaseConnection();
134 $sesPrefix = $db->dsn['database'];
136 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
138 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
139 $u = DB_DataObject::factory('Person');
140 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
146 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
150 $u = DB_DataObject::factory('Person');
151 $ff = HTML_FlexyFramework::get();
152 if (!empty($ff->Pman['local_autoauth']) &&
153 (!empty($_SERVER['SERVER_ADDR'])) &&
154 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
155 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
156 $u->get('email', $ff->Pman['local_autoauth'])
158 $db = $this->getDatabaseConnection();
159 $sesPrefix = $db->dsn['database'];
160 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
165 // not in session or not matched...
166 $u = DB_DataObject::factory('Person');
167 $u->whereAdd(' LENGTH(passwd) > 0');
169 $error = PEAR::getStaticProperty('DB_DataObject','lastError');
171 die($error->toString()); // not really a good thing to do...
173 if (!$n){ // authenticated as there are no users in the system...
180 function getAuthUser()
182 if (!$this->isAuth()) {
185 $db = $this->getDatabaseConnection();
186 $sesPrefix = $db->dsn['database'];
187 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
188 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
190 $u = DB_DataObject::factory('Person');
191 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
197 $u = DB_DataObject::factory('Person');
198 $u->whereAdd(' LENGTH(passwd) > 0');
200 $u = DB_DataObject::factory('Person');
209 $this->isAuth(); // force session start..
211 $db = $this->getDatabaseConnection();
212 $sesPrefix = $db->dsn['database'];
213 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
218 $this->isAuth(); // force session start..
219 $db = $this->getDatabaseConnection();
220 $sesPrefix = $db->dsn['database'];
221 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
224 function genPassKey ($t)
226 return md5($this->email . $t. $this->passwd);
228 function simpleAuthKey($m = 0)
230 $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
232 return md5(implode(',' , array($month, $this->email , $this->passwd, $this->id)));
234 function checkPassword($val)
237 if (substr($this->passwd,0,1) == '$') {
238 return crypt($val,$this->passwd) == $this->passwd ;
240 // old style md5 passwords...- cant be used with courier....
241 return md5($val) == $this->passwd;
243 function setPassword($value)
246 while(strlen($salt)<9) {
247 $salt.=chr(rand(64,126));
248 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
250 $this->passwd = crypt($value, '$1$'. $salt. '$');
256 $x = DB_DataObject::factory('Companies');
257 $x->get($this->company_id);
264 return $this->active;
266 function authUserName($n) // set username prior to acheck user exists query.
269 $this->whereAdd('LENGTH(passwd) > 1');
274 if ($val == $this->lang) {
284 function authUserArray()
287 $aur = $this->toArray();
294 //DB_DataObject::debugLevel(1);
295 $c = DB_Dataobject::factory('Companies');
296 $im = DB_Dataobject::factory('Images');
297 $c->joinAdd($im, 'LEFT');
299 $c->selectAs($c, 'company_id_%s');
300 $c->selectAs($im, 'company_id_logo_id_%s');
301 $c->id = $this->company_id;
305 $aur = array_merge( $c->toArray(),$aur);
307 if (empty($c->company_id_logo_id_id)) {
309 $im = DB_Dataobject::factory('Images');
310 $im->ontable = 'Companies';
312 $im->imgtype = 'LOGO';
314 $im->selectAs($im, 'company_id_logo_id_%s');
315 if ($im->find(true)) {
317 foreach($im->toArray() as $k=>$v) {
324 $aur['perms'] = $this->getPerms();
325 $g = DB_DataObject::Factory('Group_Members');
326 $aur['groups'] = $g->listGroupMembership($this, 'name');
329 $aur['dailykey'] = '';
336 // ----------PERMS------ ----------------
339 //DB_DataObject::debugLevel(1);
340 // find out all the groups they are a member of.. + Default..
342 // ------ INIITIALIZE IF NO GROUPS ARE SET UP.
344 $g = DB_DataObject::Factory('Group_Rights');
350 return $g->adminRights(); // system is not set up - so they get full rights.
353 $g = DB_DataObject::Factory('Group_Members');
355 // add the current user to the admin group..
356 $g = DB_DataObject::Factory('Groups');
357 if ($g->get('name', 'Administrators')) {
358 $gm = DB_DataObject::Factory('Group_Members');
359 $gm->group_id = $g->id;
360 $gm->user_id = $this->id;
366 // ------ STANDARD PERMISSION HANDLING.
368 $g = DB_DataObject::Factory('Group_Members');
369 $grps = $g->listGroupMembership($this);
371 $isAdmin = $g->inAdmin;
372 //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
373 // the load all the perms for those groups, and add them all together..
374 // then load all those
375 $g = DB_DataObject::Factory('Group_Rights');
376 $ret = $g->listPermsFromGroupIds($grps, $isAdmin);
377 //echo '<PRE>';print_r($ret);
383 *Basic group fetching - probably needs to filter by type eventually.
389 $g = DB_DataObject::Factory('Group_Members');
390 $grps = $g->listGroupMembership($this);
391 $g = DB_DataObject::Factory('Groups');
392 $g->whereAddIn('id', $grps, 'int');
393 return $g->fetchAll();
397 function hasPerm($name, $lvl)
399 static $pcache = array();
401 if (!isset($pcache[$this->id])) {
402 $pcache[$this->id] = $this->getPerms();
404 // echo "<PRE>";print_r($pcache[$au->id]);
405 // var_dump($pcache[$au->id]);
406 if (empty($pcache[$this->id][$name])) {
410 return strpos($pcache[$this->id][$name], $lvl) > -1;
414 // ------------ROO HOOKS------------------------------------
415 function applyFilters($q, $au)
417 if (!empty($q['query']['person_not_internal'])) {
418 $this->whereAdd(" join_company_id_id.isOwner = 0 ");
420 if (!empty($q['query']['person_internal_only_all'])) {
421 // must be internal and not current user (need for distribution list)
422 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
425 // -- for distribution
426 if (!empty($q['query']['person_internal_only'])) {
427 // must be internal and not current user (need for distribution list)
428 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
430 //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
431 // ".id != ".$au->id);
432 $this->whereAdd("Person.id != {$au->id}");
435 if (!empty($q['query']['comptype_or_company_id'])) {
436 // DB_DataObject::debugLevel(1);
437 $bits = explode(',', $q['query']['comptype_or_company_id']);
438 $id = (int) array_pop($bits);
439 $ct = $this->escape($bits[0]);
441 $this->whereAdd(" join_company_id_id.comptype = '$ct' OR Person.company_id = $id");
447 if (!empty($q['query']['person_inactive'])) {
448 // DB_Dataobject::debugLevel(1);
452 ///---------------- Group views --------
453 if (!empty($q['query']['in_group'])) {
454 // DB_DataObject::debugLevel(1);
455 $ing = (int) $q['query']['in_group'];
456 if ($q['query']['in_group'] == -1) {
457 // list all staff who are not in a group.
458 $this->whereAdd("Person.id NOT IN (
459 SELECT distinct(user_id) FROM Group_Members LEFT JOIN
460 Groups ON Groups.id = Group_Members.group_id
461 WHERE Groups.type = ".$q['query']['type']."
467 $this->whereAdd("Person.id IN (
468 SELECT distinct(user_id) FROM Group_Members
469 WHERE group_id = $ing
475 if (!empty($q['query']['not_in_directory'])) {
476 // it's a Person list..
477 // DB_DATaobjecT::debugLevel(1);
479 // specific to project directory which is single comp. login
481 $owncomp = DB_DataObject::Factory('Companies');
482 $owncomp->get('comptype', 'OWNER');
483 if ($q['company_id'] == $owncomp->id) {
488 if ( $q['query']['not_in_directory'] > -1) {
489 // can list current - so that it does not break!!!
490 $x->whereAdd('Person.id NOT IN
491 ( SELECT distinct person_id FROM ProjectDirectory WHERE
492 project_id = ' . $q['query']['not_in_directory'] . ' AND
493 company_id = ' . $this->company_id . ')');
498 if (!empty($q['query']['project_member_of'])) {
499 // this is also a flag to return if they are a member..
500 //DB_DataObject::debugLevel(1);
501 $do = DB_DataObject::factory('ProjectDirectory');
502 $do->project_id = $q['query']['project_member_of'];
504 $this->joinAdd($do,array('joinType' => 'LEFT', 'useWhereAsOn' => true));
505 $this->selectAdd('IF(ProjectDirectory.id IS NULL, 0, ProjectDirectory.id ) as is_member');
508 if (!empty($q['query']['project_member_filter'])) {
509 $this->having('is_member !=0');
516 if (!empty($q['query']['search'])) {
517 $s = $this->escape($q['query']['search']);
519 Person.name LIKE '%$s%' OR
520 Person.email LIKE '%$s%' OR
521 Person.role LIKE '%$s%' OR
522 Person.remarks LIKE '%$s%'
529 function setFromRoo($ar, $roo)
532 if (!empty($ar['passwd1'])) {
533 $this->setPassword($ar['passwd1']);
538 ($this->email == $roo->old->email)&&
539 ($this->company_id == $roo->old->company_id)
543 if (empty($this->email)) {
546 $xx = DB_Dataobject::factory('Person');
548 'email' => $this->email,
549 // 'company_id' => $x->company_id
553 return "Duplicate Email found";
559 * before Delete - delete significant dependancies..
560 * this is called after checkPerm..
563 function beforeDelete()
566 $e = DB_DataObject::Factory('Events');
567 $e->whereAdd('person_id = ' . $this->id);
576 * Check if the a user has access to modify this item.
577 * @param String $lvl Level (eg. Core.Projects)
578 * @param Pman_Core_DataObjects_Person $au The authenticated user.
579 * @param boolean $changes alllow changes???
581 * @return false if no access..
583 function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
586 // do we have an empty system..
587 if ($au && $au->id == -1) {
591 // determine if it's staff!!!
593 if ($au->company()->comptype != 'OWNER') {
595 // - can not change company!!!
597 isset($changes['company_id']) &&
598 $changes['company_id'] != $au->company_id) {
601 // can only set new emails..
603 !empty($this->email) &&
604 isset($changes['email']) &&
605 $changes['email'] != $this->email) {
609 // edit self... - what about other staff members...
611 return $this->company_id == $au->company_id;
615 // yes, only owner company can mess with this...
616 $owncomp = DB_DataObject::Factory('Companies');
617 $owncomp->get('comptype', 'OWNER');
619 $isStaff = ($this->company_id == $owncomp->id);
623 // extra case change passwod?
624 case 'P': //??? password
625 // standard perms -- for editing + if the user is dowing them selves..
626 $ret = $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
627 return $ret || $au->id == $this->id;
630 return $au->hasPerm("Core.Person", "S");
633 return $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
636 return $isStaff ? $au->hasPerm("Core.Person", "A") : $au->hasPerm("Core.Staff", "A");
639 return $isStaff ? $au->hasPerm("Core.Person", "D") : $au->hasPerm("Core.Staff", "D");
644 function onInsert($req, $roo)
647 if ($roo->authUser->id < 0) {
648 $g = DB_DataObject::factory('Groups');
650 $g->get('name', 'Administrators');
652 $p = DB_DataObject::factory('Group_Members');
653 $p->group_id = $g->id;
654 $p->user_id = $this->id;
657 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
661 if (!empty($req['project_id_addto'])) {
662 $pd = DB_DataObject::factory('ProjectDirectory');
663 $pd->project_id = $req['project_id_addto'];
664 $pd->person_id = $this->id;
666 $pd->office_id = $this->office_id;
667 $pd->company_id = $this->company_id;