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)
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 $project_id; // int(11)
28 public $action_type; // string(32)
31 /* the code above is auto generated do not remove the tag below */
35 * - user must be authenticate or args[no_auth] = true
36 * or template = password_[reset|welcome]
39 function sendTemplate($templateFile, $args)
44 $content = clone($this);
46 foreach((array)$args as $k=>$v) {
50 if (empty($args['no_auth']) && !in_array($templateFile, array(
51 // templates that can be sent without authentication.
55 $content->authUser = $this->getAuthUser();
56 if (!$content->authUser) {
57 return PEAR::raiseError("Not authenticated");
61 $content->HTTP_HOST = $_SERVER["HTTP_HOST"];
62 /* use the regex compiler, as it doesnt parse <tags */
63 require_once 'HTML/Template/Flexy.php';
64 $template = new HTML_Template_Flexy( array(
65 'compiler' => 'Regex',
66 'filters' => array('SimpleTags','Mail'),
73 $template->compile("mail/$templateFile.txt");
75 /* use variables from this object to ouput data. */
76 $mailtext = $template->bufferedOutputObject($content);
77 //echo "<PRE>";print_R($mailtext);
79 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
80 require_once 'Mail/mimeDecode.php';
81 require_once 'Mail.php';
83 $decoder = new Mail_mimeDecode($mailtext);
84 $parts = $decoder->getSendArray();
85 if (PEAR::isError($parts)) {
87 //echo "PROBLEM: {$parts->message}";
90 list($recipents,$headers,$body) = $parts;
91 $recipents = array($this->email);
92 if (!empty($content->bcc) && is_array($content->bcc)) {
93 $recipents =array_merge($recipents, $content->bcc);
95 //print_r($recipents);exit;
96 $mailOptions = PEAR::getStaticProperty('Mail','options');
97 $mail = Mail::factory("SMTP",$mailOptions);
98 $headers['Date'] = date('r');
99 if (PEAR::isError($mail)) {
102 $oe = error_reporting(E_ALL ^ E_NOTICE);
103 $ret = $mail->send($recipents,$headers,$body);
104 error_reporting($oe);
109 function getEmailFrom()
111 return '"' . addslashes($this->name) . '" <' . $this->email . '>';
113 function toEventString()
115 return empty($this->name) ? $this->email : $this->name;
117 function whereAddIn($key, $list, $type) {
119 foreach($list as $k) {
120 $ar[] = $type =='int' ? (int)$k : $this->escape($k);
125 $this->whereAdd("$key IN (". implode(',', $ar). ')');
127 function fetchAll($k= false, $v = false)
129 // should it even do this!!!?!?
131 ( // only do this is we have not been explicit..
132 empty($this->_query['data_select']) ||
133 ($this->_query['data_select'] == '*')
137 $this->selectAdd($k);
139 $this->selectAdd($v);
145 while ($this->fetch()) {
147 $ret[$this->$k] = $this->$v;
150 $ret[] = $k === false ? clone($this) : $this->$k;
155 // ---------------- authentication / passwords and keys stuff ----------------
158 $db = $this->getDatabaseConnection();
159 $sesPrefix = $db->dsn['database'];
161 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
163 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
164 $u = DB_DataObject::factory('Person');
165 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
168 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
171 // not in session or not matched...
172 $u = DB_DataObject::factory('Person');
173 $u->whereAdd(' LENGTH(passwd) > 0');
175 $error = PEAR::getStaticProperty('DB_DataObject','lastError');
177 die($error->toString()); // not really a good thing to do...
179 if (!$n){ // authenticated as there are no users in the system...
186 function getAuthUser()
188 if (!$this->isAuth()) {
191 $db = $this->getDatabaseConnection();
192 $sesPrefix = $db->dsn['database'];
193 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
194 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
196 $u = DB_DataObject::factory('Person');
197 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
203 $u = DB_DataObject::factory('Person');
204 $u->whereAdd(' LENGTH(passwd) > 0');
206 $u = DB_DataObject::factory('Person');
215 $this->isAuth(); // force session start..
216 $db = $this->getDatabaseConnection();
217 $sesPrefix = $db->dsn['database'];
218 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
223 $this->isAuth(); // force session start..
224 $db = $this->getDatabaseConnection();
225 $sesPrefix = $db->dsn['database'];
226 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
229 function genPassKey ($t)
231 return md5($this->email . $t. $this->passwd);
233 function simpleAuthKey($m = 0)
235 $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
237 return md5(implode(',' , array($month, $this->email , $this->passwd, $this->id)));
239 function checkPassword($val)
241 if (substr($this->passwd,0,1) == '$') {
242 return crypt($val,$this->passwd) == $this->passwd ;
244 // old style md5 passwords...- cant be used with courier....
245 return md5($val) == $this->passwd;
247 function setPassword($value)
250 while(strlen($salt)<9) {
251 $salt.=chr(rand(64,126));
252 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
254 $this->passwd = crypt($value, '$1$'. $salt. '$');
260 $x = DB_DataObject::factory('Companies');
261 $x->get($this->company_id);
268 return $this->active;
270 function authUserName($n) // set username prior to acheck user exists query.
273 $this->whereAdd('LENGTH(passwd) > 1');
278 if ($val == $this->lang) {
288 function authUserArray()
291 $aur = $this->toArray();
295 //DB_DataObject::debugLevel(1);
296 $c = DB_Dataobject::factory('Companies');
297 $im = DB_Dataobject::factory('Images');
298 $c->joinAdd($im, 'LEFT');
300 $c->selectAs($c, 'company_id_%s');
301 $c->selectAs($im, 'company_id_logo_id_%s');
302 $c->id = $this->company_id;
306 $aur = array_merge( $c->toArray(),$aur);
308 if (empty($c->company_id_logo_id_id)) {
310 $im = DB_Dataobject::factory('Images');
311 $im->ontable = 'Companies';
313 $im->imgtype = 'LOGO';
315 $im->selectAs($im, 'company_id_logo_id_%s');
316 if ($im->find(true)) {
318 foreach($im->toArray() as $k=>$v) {
324 now handled by interface...
326 $lang = empty($this->lang) ? 'en' : $this->lang;
327 if (empty($_SESSION['Pman_I18N'][$lang])) {
328 require_once 'Pman/I18N.php';
329 $x = new Pman_I18N();
330 $x->setSession($this);
334 $aur['i18n'] =$_SESSION['Pman_I18N'][$lang];
337 $aur['perms'] = $this->getPerms();
338 $g = DB_DataObject::Factory('Group_Members');
339 $aur['groups'] = $g->listGroupMembership($this, 'name');
342 $aur['dailykey'] = '';
349 // ----------PERMS------ ----------------
352 //DB_DataObject::debugLevel(1);
353 // find out all the groups they are a member of.. + Default..
354 $g = DB_DataObject::Factory('Group_Rights');
359 return $g->adminRights();
362 $g = DB_DataObject::Factory('Group_Members');
364 // add the current user to the admin group..
365 $g = DB_DataObject::Factory('Groups');
366 $g->get('name', 'Administrators');
367 $gm = DB_DataObject::Factory('Group_Members');
368 $gm->group_id = $g->id;
369 $gm->user_id = $this->id;
373 $g = DB_DataObject::Factory('Group_Members');
374 $grps = $g->listGroupMembership($this);
375 $isAdmin = $g->inAdmin;
377 // the load all the perms for those groups, and add them all together..
378 // then load all those
379 $g = DB_DataObject::Factory('Group_Rights');
380 $ret = $g->listPermsFromGroupIds($grps, $isAdmin);
381 // echo '<PRE>';print_r($ret);
386 function hasPerm($name, $lvl)
388 static $pcache = array();
390 if (!isset($pcache[$this->id])) {
391 $pcache[$this->id] = $this->getPerms();
393 // echo "<PRE>";print_r($pcache[$au->id]);
394 // var_dump($pcache[$au->id]);
395 if (empty($pcache[$this->id][$name])) {
399 return strpos($pcache[$this->id][$name], $lvl) > -1;
403 // ------------ROO HOOKS------------------------------------
404 function applyFilters($q, $au)
406 if (!empty($q['query']['person_not_internal'])) {
407 $this->whereAdd(" join_company_id_id.isOwner = 0 ");
409 if (!empty($q['query']['person_internal_only_all'])) {
410 // must be internal and not current user (need for distribution list)
411 $this->whereAdd(" join_company_id_id.isOwner = 1");
414 // -- for distribution
415 if (!empty($q['query']['person_internal_only'])) {
416 // must be internal and not current user (need for distribution list)
417 $this->whereAdd(" join_company_id_id.isOwner = 1");
419 //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
420 // ".id != ".$au->id);
421 $this->whereAdd("Person.id != {$au->id}");
424 if (!empty($q['query']['person_inactive'])) {
425 // DB_Dataobject::debugLevel(1);
429 ///---------------- Group views --------
430 if (!empty($q['query']['in_group'])) {
431 // DB_DataObject::debugLevel(1);
432 $ing = (int) $q['query']['in_group'];
433 if ($q['query']['in_group'] == -1) {
434 // list all staff who are not in a group.
435 $this->whereAdd("Person.id NOT IN (
436 SELECT distinct(user_id) FROM Group_Members LEFT JOIN
437 Groups ON Groups.id = Group_Members.group_id
438 WHERE Groups.type = ".$q['query']['type']."
444 $this->whereAdd("Person.id IN (
445 SELECT distinct(user_id) FROM Group_Members
446 WHERE group_id = $ing
452 if (!empty($q['query']['not_in_directory'])) {
453 // it's a Person list..
454 // DB_DATaobjecT::debugLevel(1);
456 // specific to project directory which is single comp. login
458 $owncomp = DB_DataObject::Factory('Companies');
459 $owncomp->get('comptype', 'OWNER');
460 if ($q['company_id'] == $owncomp->id) {
465 if ( $q['query']['not_in_directory'] > -1) {
466 // can list current - so that it does not break!!!
467 $x->whereAdd('Person.id NOT IN
468 ( SELECT distinct person_id FROM ProjectDirectory WHERE
469 project_id = ' . $q['query']['not_in_directory'] . ' AND
470 company_id = ' . $this->company_id . ')');
474 if (!empty($q['query']['search'])) {
475 $s = $this->escape($q['query']['search']);
477 Person.name LIKE '%$s%' OR
478 Person.email LIKE '%$s%' OR
479 Person.role LIKE '%$s%' OR
480 Person.remarks LIKE '%$s%'
487 function setFromRoo($ar, $roo)
490 if (!empty($ar['passwd1'])) {
491 $this->setPassword($ar['passwd1']);
496 ($this->email == $roo->old->email)&&
497 ($this->company_id == $roo->old->company_id)
501 if (empty($this->email)) {
504 $xx = DB_Dataobject::factory('Person');
506 'email' => $this->email,
507 // 'company_id' => $x->company_id
511 return "Duplicate Email found";
515 function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
518 // do we have an empty system..
519 if ($au && $au->id == -1) {
523 // determine if it's staff!!!
525 if ($au->company()->comptype != 'OWNER') {
527 // - can not change company!!!
529 isset($changes['company_id']) &&
530 $changes['company_id'] != $au->company_id) {
533 // can only set new emails..
535 !empty($this->email) &&
536 isset($changes['email']) &&
537 $changes['email'] != $this->email) {
541 // edit self... - what about other staff members...
543 return $this->company_id == $au->company_id;
547 // yes, only owner company can mess with this...
548 $owncomp = DB_DataObject::Factory('Companies');
549 $owncomp->get('comptype', 'OWNER');
551 $isStaff = ($this->company_id == $owncomp->id);
555 // extra case change passwod?
556 case 'P': //??? password
557 // standard perms -- for editing + if the user is dowing them selves..
558 $ret = $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
559 return $ret || $au->id == $this->id;
562 return $au->hasPerm("Core.Person", "S");
565 return $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
568 return $isStaff ? $au->hasPerm("Core.Person", "A") : $au->hasPerm("Core.Staff", "A");
571 return $isStaff ? $au->hasPerm("Core.Person", "D") : $au->hasPerm("Core.Staff", "D");
576 function onInsert($req, $roo)
579 if ($roo->authUser->id < 0) {
580 $g = DB_DataObject::factory('Groups');
582 $g->get('name', 'Administrators');
584 $p = DB_DataObject::factory('Group_Members');
585 $p->group_id = $g->id;
586 $p->user_id = $this->id;
589 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
593 if (!empty($req['project_id_addto'])) {
594 $pd = DB_DataObject::factory('ProjectDirectory');
595 $pd->project_id = $req['project_id_addto'];
596 $pd->person_id = $this->id;
598 $pd->office_id = $this->office_id;
599 $pd->company_id = $this->company_id;