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 'recipents' => $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);
134 //print_r($recipents);exit;
135 $mailOptions = PEAR::getStaticProperty('Mail','options');
136 $mail = Mail::factory("SMTP",$mailOptions);
138 if (PEAR::isError($mail)) {
141 $oe = error_reporting(E_ALL ^ E_NOTICE);
142 $ret = $mail->send($ar['recipents'],$ar['headers'],$ar['body']);
143 error_reporting($oe);
148 function getEmailFrom()
150 return '"' . addslashes($this->name) . '" <' . $this->email . '>';
152 function toEventString()
154 return empty($this->name) ? $this->email : $this->name;
156 function verifyAuth()
158 $ff= HTML_FlexyFramework::get();
159 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $this->company()->comptype) {
160 $ff->page->jerr("Login not permited to outside companies");
167 // ---------------- authentication / passwords and keys stuff ----------------
170 $db = $this->getDatabaseConnection();
171 $sesPrefix = $db->dsn['database'];
173 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
175 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
176 $u = DB_DataObject::factory('Person');
177 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
183 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
187 $u = DB_DataObject::factory('Person');
188 $ff = HTML_FlexyFramework::get();
189 if (!empty($ff->Pman['local_autoauth']) &&
190 (!empty($_SERVER['SERVER_ADDR'])) &&
191 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
192 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
193 $u->get('email', $ff->Pman['local_autoauth'])
195 $db = $this->getDatabaseConnection();
196 $sesPrefix = $db->dsn['database'];
197 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
202 // not in session or not matched...
203 $u = DB_DataObject::factory('Person');
204 $u->whereAdd(' LENGTH(passwd) > 0');
206 $error = PEAR::getStaticProperty('DB_DataObject','lastError');
208 die($error->toString()); // not really a good thing to do...
210 if (!$n){ // authenticated as there are no users in the system...
217 function getAuthUser()
219 if (!$this->isAuth()) {
222 $db = $this->getDatabaseConnection();
223 $sesPrefix = $db->dsn['database'];
224 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
225 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
227 $u = DB_DataObject::factory('Person');
228 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
234 $u = DB_DataObject::factory('Person');
235 $u->whereAdd(' LENGTH(passwd) > 0');
237 $u = DB_DataObject::factory('Person');
246 $this->isAuth(); // force session start..
248 $db = $this->getDatabaseConnection();
249 $sesPrefix = $db->dsn['database'];
250 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
255 $this->isAuth(); // force session start..
256 $db = $this->getDatabaseConnection();
257 $sesPrefix = $db->dsn['database'];
258 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
261 function genPassKey ($t)
263 return md5($this->email . $t. $this->passwd);
265 function simpleAuthKey($m = 0)
267 $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
269 return md5(implode(',' , array($month, $this->email , $this->passwd, $this->id)));
271 function checkPassword($val)
274 if (substr($this->passwd,0,1) == '$') {
275 return crypt($val,$this->passwd) == $this->passwd ;
277 // old style md5 passwords...- cant be used with courier....
278 return md5($val) == $this->passwd;
280 function setPassword($value)
283 while(strlen($salt)<9) {
284 $salt.=chr(rand(64,126));
285 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
287 $this->passwd = crypt($value, '$1$'. $salt. '$');
293 $x = DB_DataObject::factory('Companies');
294 $x->get($this->company_id);
301 return $this->active;
303 function authUserName($n) // set username prior to acheck user exists query.
306 $this->whereAdd('LENGTH(passwd) > 1');
311 if ($val == $this->lang) {
321 function authUserArray()
324 $aur = $this->toArray();
331 //DB_DataObject::debugLevel(1);
332 $c = DB_Dataobject::factory('Companies');
333 $im = DB_Dataobject::factory('Images');
334 $c->joinAdd($im, 'LEFT');
336 $c->selectAs($c, 'company_id_%s');
337 $c->selectAs($im, 'company_id_logo_id_%s');
338 $c->id = $this->company_id;
342 $aur = array_merge( $c->toArray(),$aur);
344 if (empty($c->company_id_logo_id_id)) {
346 $im = DB_Dataobject::factory('Images');
347 $im->ontable = 'Companies';
349 $im->imgtype = 'LOGO';
351 $im->selectAs($im, 'company_id_logo_id_%s');
352 if ($im->find(true)) {
354 foreach($im->toArray() as $k=>$v) {
361 $aur['perms'] = $this->getPerms();
362 $g = DB_DataObject::Factory('Group_Members');
363 $aur['groups'] = $g->listGroupMembership($this, 'name');
366 $aur['dailykey'] = '';
373 // ----------PERMS------ ----------------
376 //DB_DataObject::debugLevel(1);
377 // find out all the groups they are a member of.. + Default..
379 // ------ INIITIALIZE IF NO GROUPS ARE SET UP.
381 $g = DB_DataObject::Factory('Group_Rights');
387 return $g->adminRights(); // system is not set up - so they get full rights.
390 $g = DB_DataObject::Factory('Group_Members');
392 // add the current user to the admin group..
393 $g = DB_DataObject::Factory('Groups');
394 if ($g->get('name', 'Administrators')) {
395 $gm = DB_DataObject::Factory('Group_Members');
396 $gm->group_id = $g->id;
397 $gm->user_id = $this->id;
403 // ------ STANDARD PERMISSION HANDLING.
405 $g = DB_DataObject::Factory('Group_Members');
406 $grps = $g->listGroupMembership($this);
408 $isAdmin = $g->inAdmin;
409 //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
410 // the load all the perms for those groups, and add them all together..
411 // then load all those
412 $g = DB_DataObject::Factory('Group_Rights');
413 $ret = $g->listPermsFromGroupIds($grps, $isAdmin);
414 //echo '<PRE>';print_r($ret);
420 *Basic group fetching - probably needs to filter by type eventually.
426 $g = DB_DataObject::Factory('Group_Members');
427 $grps = $g->listGroupMembership($this);
428 $g = DB_DataObject::Factory('Groups');
429 $g->whereAddIn('id', $grps, 'int');
430 return $g->fetchAll();
434 function hasPerm($name, $lvl)
436 static $pcache = array();
438 if (!isset($pcache[$this->id])) {
439 $pcache[$this->id] = $this->getPerms();
441 // echo "<PRE>";print_r($pcache[$au->id]);
442 // var_dump($pcache[$au->id]);
443 if (empty($pcache[$this->id][$name])) {
447 return strpos($pcache[$this->id][$name], $lvl) > -1;
451 // ------------ROO HOOKS------------------------------------
452 function applyFilters($q, $au)
454 if (!empty($q['query']['person_not_internal'])) {
455 $this->whereAdd(" join_company_id_id.isOwner = 0 ");
457 if (!empty($q['query']['person_internal_only_all'])) {
459 // must be internal and not current user (need for distribution list)
460 // user has a projectdirectory entry and role is not blank.
461 DB_DataObject::DebugLevel(1);
462 $pd = DB_DataObject::factory('ProjectDirectory');
463 $pd->whereAdd("role != ''");
465 $pd->selectAdd('distinct(person_id) as person_id');
466 $roled = $pd->fetchAll('person_id');
468 join_company_id_id.comptype = 'OWNER'
470 id IN (".implode(',', $roled) . ")
476 // -- for distribution
477 if (!empty($q['query']['person_internal_only'])) {
478 // must be internal and not current user (need for distribution list)
479 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
481 //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
482 // ".id != ".$au->id);
483 $this->whereAdd("Person.id != {$au->id}");
486 if (!empty($q['query']['comptype_or_company_id'])) {
487 // DB_DataObject::debugLevel(1);
488 $bits = explode(',', $q['query']['comptype_or_company_id']);
489 $id = (int) array_pop($bits);
490 $ct = $this->escape($bits[0]);
492 $this->whereAdd(" join_company_id_id.comptype = '$ct' OR Person.company_id = $id");
498 if (!empty($q['query']['person_inactive'])) {
499 // DB_Dataobject::debugLevel(1);
503 ///---------------- Group views --------
504 if (!empty($q['query']['in_group'])) {
505 // DB_DataObject::debugLevel(1);
506 $ing = (int) $q['query']['in_group'];
507 if ($q['query']['in_group'] == -1) {
508 // list all staff who are not in a group.
509 $this->whereAdd("Person.id NOT IN (
510 SELECT distinct(user_id) FROM Group_Members LEFT JOIN
511 Groups ON Groups.id = Group_Members.group_id
512 WHERE Groups.type = ".$q['query']['type']."
518 $this->whereAdd("Person.id IN (
519 SELECT distinct(user_id) FROM Group_Members
520 WHERE group_id = $ing
526 if (!empty($q['query']['not_in_directory'])) {
527 // it's a Person list..
528 // DB_DATaobjecT::debugLevel(1);
530 // specific to project directory which is single comp. login
532 $owncomp = DB_DataObject::Factory('Companies');
533 $owncomp->get('comptype', 'OWNER');
534 if ($q['company_id'] == $owncomp->id) {
539 if ( $q['query']['not_in_directory'] > -1) {
540 // can list current - so that it does not break!!!
541 $x->whereAdd('Person.id NOT IN
542 ( SELECT distinct person_id FROM ProjectDirectory WHERE
543 project_id = ' . $q['query']['not_in_directory'] . ' AND
544 company_id = ' . $this->company_id . ')');
549 if (!empty($q['query']['project_member_of'])) {
550 // this is also a flag to return if they are a member..
551 //DB_DataObject::debugLevel(1);
552 $do = DB_DataObject::factory('ProjectDirectory');
553 $do->project_id = $q['query']['project_member_of'];
555 $this->joinAdd($do,array('joinType' => 'LEFT', 'useWhereAsOn' => true));
556 $this->selectAdd('IF(ProjectDirectory.id IS NULL, 0, ProjectDirectory.id ) as is_member');
559 if (!empty($q['query']['project_member_filter'])) {
560 $this->having('is_member !=0');
567 if (!empty($q['query']['search'])) {
568 $s = $this->escape($q['query']['search']);
570 Person.name LIKE '%$s%' OR
571 Person.email LIKE '%$s%' OR
572 Person.role LIKE '%$s%' OR
573 Person.remarks LIKE '%$s%'
580 function setFromRoo($ar, $roo)
583 if (!empty($ar['passwd1'])) {
584 $this->setPassword($ar['passwd1']);
589 ($this->email == $roo->old->email)&&
590 ($this->company_id == $roo->old->company_id)
594 if (empty($this->email)) {
597 $xx = DB_Dataobject::factory('Person');
599 'email' => $this->email,
600 // 'company_id' => $x->company_id
604 return "Duplicate Email found";
610 * before Delete - delete significant dependancies..
611 * this is called after checkPerm..
614 function beforeDelete()
617 $e = DB_DataObject::Factory('Events');
618 $e->whereAdd('person_id = ' . $this->id);
627 * Check if the a user has access to modify this item.
628 * @param String $lvl Level (eg. Core.Projects)
629 * @param Pman_Core_DataObjects_Person $au The authenticated user.
630 * @param boolean $changes alllow changes???
632 * @return false if no access..
634 function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
637 // do we have an empty system..
638 if ($au && $au->id == -1) {
642 // determine if it's staff!!!
644 if ($au->company()->comptype != 'OWNER') {
646 // - can not change company!!!
648 isset($changes['company_id']) &&
649 $changes['company_id'] != $au->company_id) {
652 // can only set new emails..
654 !empty($this->email) &&
655 isset($changes['email']) &&
656 $changes['email'] != $this->email) {
660 // edit self... - what about other staff members...
662 return $this->company_id == $au->company_id;
666 // yes, only owner company can mess with this...
667 $owncomp = DB_DataObject::Factory('Companies');
668 $owncomp->get('comptype', 'OWNER');
670 $isStaff = ($this->company_id == $owncomp->id);
674 // extra case change passwod?
675 case 'P': //??? password
676 // standard perms -- for editing + if the user is dowing them selves..
677 $ret = $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
678 return $ret || $au->id == $this->id;
681 return $au->hasPerm("Core.Person", "S");
684 return $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
687 return $isStaff ? $au->hasPerm("Core.Person", "A") : $au->hasPerm("Core.Staff", "A");
690 return $isStaff ? $au->hasPerm("Core.Person", "D") : $au->hasPerm("Core.Staff", "D");
695 function onInsert($req, $roo)
698 if ($roo->authUser->id < 0) {
699 $g = DB_DataObject::factory('Groups');
701 $g->get('name', 'Administrators');
703 $p = DB_DataObject::factory('Group_Members');
704 $p->group_id = $g->id;
705 $p->user_id = $this->id;
708 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
712 if (!empty($req['project_id_addto'])) {
713 $pd = DB_DataObject::factory('ProjectDirectory');
714 $pd->project_id = $req['project_id_addto'];
715 $pd->person_id = $this->id;
717 $pd->office_id = $this->office_id;
718 $pd->company_id = $this->company_id;