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 */
34 function sendTemplate($templateFile, $args)
39 $content = clone($this);
41 foreach((array)$args as $k=>$v) {
45 if (!in_array($templateFile, array(
46 // templates that can be sent without authentication.
50 $content->authUser = $this->getAuthUser();
51 if (!$content->authUser) {
52 return PEAR::raiseError("Not authenticated");
56 $content->HTTP_HOST = $_SERVER["HTTP_HOST"];
57 /* use the regex compiler, as it doesnt parse <tags */
58 require_once 'HTML/Template/Flexy.php';
59 $template = new HTML_Template_Flexy( array(
60 'compiler' => 'Regex',
61 'filters' => array('SimpleTags','Mail'),
68 $template->compile("mail/$templateFile.txt");
70 /* use variables from this object to ouput data. */
71 $mailtext = $template->bufferedOutputObject($content);
72 //echo "<PRE>";print_R($mailtext);
74 /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
75 require_once 'Mail/mimeDecode.php';
76 require_once 'Mail.php';
78 $decoder = new Mail_mimeDecode($mailtext);
79 $parts = $decoder->getSendArray();
80 if (PEAR::isError($parts)) {
82 //echo "PROBLEM: {$parts->message}";
85 list($recipents,$headers,$body) = $parts;
86 $recipents = array($this->email);
87 if (!empty($content->bcc) && is_array($content->bcc)) {
88 $recipents =array_merge($recipents, $content->bcc);
90 //print_r($recipents);exit;
91 $mailOptions = PEAR::getStaticProperty('Mail','options');
92 $mail = Mail::factory("SMTP",$mailOptions);
93 $headers['Date'] = date('r');
94 if (PEAR::isError($mail)) {
97 $oe = error_reporting(E_ALL ^ E_NOTICE);
98 $ret = $mail->send($recipents,$headers,$body);
104 function getEmailFrom()
106 return '"' . addslashes($this->name) . '" <' . $this->email . '>';
108 function toEventString()
110 return empty($this->name) ? $this->email : $this->name;
112 function whereAddIn($key, $list, $type) {
114 foreach($list as $k) {
115 $ar[] = $type =='int' ? (int)$k : $this->escape($k);
120 $this->whereAdd("$key IN (". implode(',', $ar). ')');
122 function fetchAll($k= false, $v = false)
124 // should it even do this!!!?!?
126 ( // only do this is we have not been explicit..
127 empty($this->_query['data_select']) ||
128 ($this->_query['data_select'] == '*')
132 $this->selectAdd($k);
134 $this->selectAdd($v);
140 while ($this->fetch()) {
142 $ret[$this->$k] = $this->$v;
145 $ret[] = $k === false ? clone($this) : $this->$k;
150 // ---------------- authentication / passwords and keys stuff ----------------
153 $db = $this->getDatabaseConnection();
154 $sesPrefix = $db->dsn['database'];
156 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
158 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
159 $u = DB_DataObject::factory('Person');
160 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
163 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
166 // not in session or not matched...
167 $u = DB_DataObject::factory('Person');
168 $u->whereAdd(' LENGTH(passwd) > 0');
170 $error = PEAR::getStaticProperty('DB_DataObject','lastError');
172 die($error->toString()); // not really a good thing to do...
174 if (!$n){ // authenticated as there are no users in the system...
181 function getAuthUser()
183 if (!$this->isAuth()) {
186 $db = $this->getDatabaseConnection();
187 $sesPrefix = $db->dsn['database'];
188 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
189 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
191 $u = DB_DataObject::factory('Person');
192 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
198 $u = DB_DataObject::factory('Person');
199 $u->whereAdd(' LENGTH(passwd) > 0');
201 $u = DB_DataObject::factory('Person');
210 $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)
236 if (substr($this->passwd,0,1) == '$') {
237 return crypt($val,$this->passwd) == $this->passwd ;
239 // old style md5 passwords...- cant be used with courier....
240 return md5($val) == $this->passwd;
242 function setPassword($value)
245 while(strlen($salt)<9) {
246 $salt.=chr(rand(64,126));
247 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
249 $this->passwd = crypt($value, '$1$'. $salt. '$');
255 $x = DB_DataObject::factory('Companies');
256 $x->get($this->company_id);
263 return $this->active;
265 function authUserName($n) // set username prior to acheck user exists query.
268 $this->whereAdd('LENGTH(passwd) > 1');
273 if ($val == $this->lang) {
283 function authUserArray()
286 $aur = $this->toArray();
290 //DB_DataObject::debugLevel(1);
291 $c = DB_Dataobject::factory('Companies');
292 $im = DB_Dataobject::factory('Images');
293 $c->joinAdd($im, 'LEFT');
295 $c->selectAs($c, 'company_id_%s');
296 $c->selectAs($im, 'company_id_logo_id_%s');
297 $c->id = $this->company_id;
301 $aur = array_merge( $c->toArray(),$aur);
303 if (empty($c->company_id_logo_id_id)) {
305 $im = DB_Dataobject::factory('Images');
306 $im->ontable = 'Companies';
308 $im->imgtype = 'LOGO';
310 $im->selectAs($im, 'company_id_logo_id_%s');
311 if ($im->find(true)) {
313 foreach($im->toArray() as $k=>$v) {
320 $lang = empty($this->lang) ? 'en' : $this->lang;
321 if (empty($_SESSION['Pman_I18N'][$lang])) {
322 require_once 'Pman/I18N.php';
323 $x = new Pman_I18N();
324 $x->setSession($this);
328 $aur['i18n'] =$_SESSION['Pman_I18N'][$lang];
331 $aur['perms'] = $this->getPerms();
332 $g = DB_DataObject::Factory('Group_Members');
333 $aur['groups'] = $g->listGroupMembership($this, 'name');
336 $aur['dailykey'] = '';
343 // ----------PERMS------ ----------------
346 //DB_DataObject::debugLevel(1);
347 // find out all the groups they are a member of.. + Default..
348 $g = DB_DataObject::Factory('Group_Rights');
353 return $g->adminRights();
356 $g = DB_DataObject::Factory('Group_Members');
357 $grps = $g->listGroupMembership($this);
358 $isAdmin = $g->inAdmin;
360 // the load all the perms for those groups, and add them all together..
361 // then load all those
362 $g = DB_DataObject::Factory('Group_Rights');
363 $ret = $g->listPermsFromGroupIds($grps, $isAdmin);
364 // echo '<PRE>';print_r($ret);
369 function hasPerm($name, $lvl)
371 static $pcache = array();
373 if (!isset($pcache[$this->id])) {
374 $pcache[$this->id] = $this->getPerms();
376 // echo "<PRE>";print_r($pcache[$au->id]);
377 // var_dump($pcache[$au->id]);
378 if (empty($pcache[$this->id][$name])) {
382 return strpos($pcache[$this->id][$name], $lvl) > -1;
386 // ------------ROO HOOKS------------------------------------
387 function applyFilters($q, $au)
389 if (!empty($q['query']['person_not_internal'])) {
390 $this->whereAdd(" join_company_id_id.isOwner = 0 ");
392 if (!empty($q['query']['person_internal_only_all'])) {
393 // must be internal and not current user (need for distribution list)
394 $this->whereAdd(" join_company_id_id.isOwner = 1");
397 // -- for distribution
398 if (!empty($q['query']['person_internal_only'])) {
399 // must be internal and not current user (need for distribution list)
400 $this->whereAdd(" join_company_id_id.isOwner = 1");
402 //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
403 // ".id != ".$au->id);
404 $this->whereAdd("Person.id != {$au->id}");
407 if (!empty($q['query']['person_inactive'])) {
408 // DB_Dataobject::debugLevel(1);
412 ///---------------- Group views --------
413 if (!empty($q['query']['in_group'])) {
414 // DB_DataObject::debugLevel(1);
415 $ing = (int) $q['query']['in_group'];
416 if ($q['query']['in_group'] == -1) {
417 // list all staff who are not in a group.
418 $this->whereAdd("Person.id NOT IN (
419 SELECT distinct(user_id) FROM Group_Members LEFT JOIN
420 Groups ON Groups.id = Group_Members.group_id
421 WHERE Groups.type = ".$q['query']['type']."
427 $this->whereAdd("Person.id IN (
428 SELECT distinct(user_id) FROM Group_Members
429 WHERE group_id = $ing
435 if (!empty($q['query']['not_in_directory'])) {
436 // it's a Person list..
437 // DB_DATaobjecT::debugLevel(1);
439 // specific to project directory which is single comp. login
441 $owncomp = DB_DataObject::Factory('Companies');
442 $owncomp->get('comptype', 'OWNER');
443 if ($q['company_id'] == $owncomp->id) {
448 if ( $q['query']['not_in_directory'] > -1) {
449 // can list current - so that it does not break!!!
450 $x->whereAdd('Person.id NOT IN
451 ( SELECT distinct person_id FROM ProjectDirectory WHERE
452 project_id = ' . $q['query']['not_in_directory'] . ' AND
453 company_id = ' . $this->company_id . ')');
457 if (!empty($q['query']['search'])) {
458 $s = $this->escape($q['query']['search']);
460 Person.name LIKE '%$s%' OR
461 Person.email LIKE '%$s%' OR
462 Person.role LIKE '%$s%' OR
463 Person.remarks LIKE '%$s%'
470 function setFromRoo($ar, $roo)
473 if (!empty($ar['passwd1'])) {
474 $this->setPassword($ar['passwd1']);
479 ($this->email == $roo->old->email)&&
480 ($this->company_id == $roo->old->company_id)
484 $xx = DB_Dataobject::factory('Person');
486 'email' => $this->email,
487 // 'company_id' => $x->company_id
491 return "Duplicate Email found";
495 function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
497 // determine if it's staff!!!
500 if ($au->company()->comptype != 'OWNER') {
502 // - can not change company!!!
504 isset($changes['company_id']) &&
505 $changes['company_id'] != $au->company_id) {
508 // can only set new emails..
510 !empty($this->email) &&
511 isset($changes['email']) &&
512 $changes['email'] != $this->email) {
516 // edit self... - what about other staff members...
518 return $this->company_id == $au->company_id;
522 // yes, only owner company can mess with this...
523 $owncomp = DB_DataObject::Factory('Companies');
524 $owncomp->get('comptype', 'OWNER');
526 $isStaff = ($this->company_id == $owncomp->id);
530 // extra case change passwod?
531 case 'P': //??? password
532 // standard perms -- for editing + if the user is dowing them selves..
533 $ret = $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
534 return $ret || $au->id == $this->id;
537 return $au->hasPerm("Core.Person", "S");
540 return $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
543 return $isStaff ? $au->hasPerm("Core.Person", "A") : $au->hasPerm("Core.Staff", "A");
546 return $isStaff ? $au->hasPerm("Core.Person", "D") : $au->hasPerm("Core.Staff", "D");
551 function onInsert($req, $roo)
554 if ($roo->authUser->id < 0) {
555 $g = DB_DataObject::factory('Groups');
557 $g->get('name', 'Administrators');
559 $p = DB_DataObject::factory('Group_Members');
560 $p->group_id = $g->id;
561 $p->user_id = $this->id;
564 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
568 if (!empty($req['project_id_addto'])) {
569 $pd = DB_DataObject::factory('ProjectDirectory');
570 $pd->project_id = $req['project_id_addto'];
571 $pd->person_id = $this->id;
573 $pd->office_id = $this->office_id;
574 $pd->company_id = $this->company_id;