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;
118 // ---------------- authentication / passwords and keys stuff ----------------
121 $db = $this->getDatabaseConnection();
122 $sesPrefix = $db->dsn['database'];
124 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
126 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
127 $u = DB_DataObject::factory('Person');
128 if ($u->get($a->id)) { //&& strlen($u->passwd)) {
131 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
135 $u = DB_DataObject::factory('Person');
136 $ff = HTML_FlexyFramework::get();
137 if (!empty($ff->Pman['local_autoauth']) &&
138 (!empty($_SERVER['SERVER_ADDR'])) &&
139 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
140 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
141 $u->get('email', $ff->Pman['local_autoauth'])
143 $db = $this->getDatabaseConnection();
144 $sesPrefix = $db->dsn['database'];
145 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
150 // not in session or not matched...
151 $u = DB_DataObject::factory('Person');
152 $u->whereAdd(' LENGTH(passwd) > 0');
154 $error = PEAR::getStaticProperty('DB_DataObject','lastError');
156 die($error->toString()); // not really a good thing to do...
158 if (!$n){ // authenticated as there are no users in the system...
165 function getAuthUser()
167 if (!$this->isAuth()) {
170 $db = $this->getDatabaseConnection();
171 $sesPrefix = $db->dsn['database'];
172 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
173 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
175 $u = DB_DataObject::factory('Person');
176 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
182 $u = DB_DataObject::factory('Person');
183 $u->whereAdd(' LENGTH(passwd) > 0');
185 $u = DB_DataObject::factory('Person');
194 $this->isAuth(); // force session start..
195 $db = $this->getDatabaseConnection();
196 $sesPrefix = $db->dsn['database'];
197 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
202 $this->isAuth(); // force session start..
203 $db = $this->getDatabaseConnection();
204 $sesPrefix = $db->dsn['database'];
205 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
208 function genPassKey ($t)
210 return md5($this->email . $t. $this->passwd);
212 function simpleAuthKey($m = 0)
214 $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
216 return md5(implode(',' , array($month, $this->email , $this->passwd, $this->id)));
218 function checkPassword($val)
221 if (substr($this->passwd,0,1) == '$') {
222 return crypt($val,$this->passwd) == $this->passwd ;
224 // old style md5 passwords...- cant be used with courier....
225 return md5($val) == $this->passwd;
227 function setPassword($value)
230 while(strlen($salt)<9) {
231 $salt.=chr(rand(64,126));
232 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
234 $this->passwd = crypt($value, '$1$'. $salt. '$');
240 $x = DB_DataObject::factory('Companies');
241 $x->get($this->company_id);
248 return $this->active;
250 function authUserName($n) // set username prior to acheck user exists query.
253 $this->whereAdd('LENGTH(passwd) > 1');
258 if ($val == $this->lang) {
268 function authUserArray()
271 $aur = $this->toArray();
275 //DB_DataObject::debugLevel(1);
276 $c = DB_Dataobject::factory('Companies');
277 $im = DB_Dataobject::factory('Images');
278 $c->joinAdd($im, 'LEFT');
280 $c->selectAs($c, 'company_id_%s');
281 $c->selectAs($im, 'company_id_logo_id_%s');
282 $c->id = $this->company_id;
286 $aur = array_merge( $c->toArray(),$aur);
288 if (empty($c->company_id_logo_id_id)) {
290 $im = DB_Dataobject::factory('Images');
291 $im->ontable = 'Companies';
293 $im->imgtype = 'LOGO';
295 $im->selectAs($im, 'company_id_logo_id_%s');
296 if ($im->find(true)) {
298 foreach($im->toArray() as $k=>$v) {
305 $aur['perms'] = $this->getPerms();
306 $g = DB_DataObject::Factory('Group_Members');
307 $aur['groups'] = $g->listGroupMembership($this, 'name');
310 $aur['dailykey'] = '';
317 // ----------PERMS------ ----------------
320 //DB_DataObject::debugLevel(1);
321 // find out all the groups they are a member of.. + Default..
322 $g = DB_DataObject::Factory('Group_Rights');
327 return $g->adminRights(); // system is not set up - so they get full rights.
330 $g = DB_DataObject::Factory('Group_Members');
332 // add the current user to the admin group..
333 $g = DB_DataObject::Factory('Groups');
334 if ($g->get('name', 'Administrators')) {
335 $gm = DB_DataObject::Factory('Group_Members');
336 $gm->group_id = $g->id;
337 $gm->user_id = $this->id;
343 $g = DB_DataObject::Factory('Group_Members');
344 $grps = $g->listGroupMembership($this);
345 $isAdmin = $g->inAdmin;
346 //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
347 // the load all the perms for those groups, and add them all together..
348 // then load all those
349 $g = DB_DataObject::Factory('Group_Rights');
350 $ret = $g->listPermsFromGroupIds($grps, $isAdmin);
351 //echo '<PRE>';print_r($ret);
356 function hasPerm($name, $lvl)
358 static $pcache = array();
360 if (!isset($pcache[$this->id])) {
361 $pcache[$this->id] = $this->getPerms();
363 // echo "<PRE>";print_r($pcache[$au->id]);
364 // var_dump($pcache[$au->id]);
365 if (empty($pcache[$this->id][$name])) {
369 return strpos($pcache[$this->id][$name], $lvl) > -1;
373 // ------------ROO HOOKS------------------------------------
374 function applyFilters($q, $au)
376 if (!empty($q['query']['person_not_internal'])) {
377 $this->whereAdd(" join_company_id_id.isOwner = 0 ");
379 if (!empty($q['query']['person_internal_only_all'])) {
380 // must be internal and not current user (need for distribution list)
381 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
384 // -- for distribution
385 if (!empty($q['query']['person_internal_only'])) {
386 // must be internal and not current user (need for distribution list)
387 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
389 //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
390 // ".id != ".$au->id);
391 $this->whereAdd("Person.id != {$au->id}");
394 if (!empty($q['query']['comptype_or_company_id'])) {
395 // DB_DataObject::debugLevel(1);
396 $bits = explode(',', $q['query']['comptype_or_company_id']);
397 $id = (int) array_pop($bits);
398 $ct = $this->escape($bits[0]);
400 $this->whereAdd(" join_company_id_id.comptype = '$ct' OR Person.company_id = $id");
406 if (!empty($q['query']['person_inactive'])) {
407 // DB_Dataobject::debugLevel(1);
411 ///---------------- Group views --------
412 if (!empty($q['query']['in_group'])) {
413 // DB_DataObject::debugLevel(1);
414 $ing = (int) $q['query']['in_group'];
415 if ($q['query']['in_group'] == -1) {
416 // list all staff who are not in a group.
417 $this->whereAdd("Person.id NOT IN (
418 SELECT distinct(user_id) FROM Group_Members LEFT JOIN
419 Groups ON Groups.id = Group_Members.group_id
420 WHERE Groups.type = ".$q['query']['type']."
426 $this->whereAdd("Person.id IN (
427 SELECT distinct(user_id) FROM Group_Members
428 WHERE group_id = $ing
434 if (!empty($q['query']['not_in_directory'])) {
435 // it's a Person list..
436 // DB_DATaobjecT::debugLevel(1);
438 // specific to project directory which is single comp. login
440 $owncomp = DB_DataObject::Factory('Companies');
441 $owncomp->get('comptype', 'OWNER');
442 if ($q['company_id'] == $owncomp->id) {
447 if ( $q['query']['not_in_directory'] > -1) {
448 // can list current - so that it does not break!!!
449 $x->whereAdd('Person.id NOT IN
450 ( SELECT distinct person_id FROM ProjectDirectory WHERE
451 project_id = ' . $q['query']['not_in_directory'] . ' AND
452 company_id = ' . $this->company_id . ')');
457 if (!empty($q['query']['project_member_of'])) {
458 // this is also a flag to return if they are a member..
459 //DB_DataObject::debugLevel(1);
460 $do = DB_DataObject::factory('ProjectDirectory');
461 $do->project_id = $q['query']['project_member_of'];
463 $this->joinAdd($do,array('joinType' => 'LEFT', 'useWhereAsOn' => true));
464 $this->selectAdd('IF(ProjectDirectory.id IS NULL, 0, ProjectDirectory.id ) as is_member');
467 if (!empty($q['query']['project_member_filter'])) {
468 $this->having('is_member !=0');
475 if (!empty($q['query']['search'])) {
476 $s = $this->escape($q['query']['search']);
478 Person.name LIKE '%$s%' OR
479 Person.email LIKE '%$s%' OR
480 Person.role LIKE '%$s%' OR
481 Person.remarks LIKE '%$s%'
488 function setFromRoo($ar, $roo)
491 if (!empty($ar['passwd1'])) {
492 $this->setPassword($ar['passwd1']);
497 ($this->email == $roo->old->email)&&
498 ($this->company_id == $roo->old->company_id)
502 if (empty($this->email)) {
505 $xx = DB_Dataobject::factory('Person');
507 'email' => $this->email,
508 // 'company_id' => $x->company_id
512 return "Duplicate Email found";
516 function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
519 // do we have an empty system..
520 if ($au && $au->id == -1) {
524 // determine if it's staff!!!
526 if ($au->company()->comptype != 'OWNER') {
528 // - can not change company!!!
530 isset($changes['company_id']) &&
531 $changes['company_id'] != $au->company_id) {
534 // can only set new emails..
536 !empty($this->email) &&
537 isset($changes['email']) &&
538 $changes['email'] != $this->email) {
542 // edit self... - what about other staff members...
544 return $this->company_id == $au->company_id;
548 // yes, only owner company can mess with this...
549 $owncomp = DB_DataObject::Factory('Companies');
550 $owncomp->get('comptype', 'OWNER');
552 $isStaff = ($this->company_id == $owncomp->id);
556 // extra case change passwod?
557 case 'P': //??? password
558 // standard perms -- for editing + if the user is dowing them selves..
559 $ret = $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
560 return $ret || $au->id == $this->id;
563 return $au->hasPerm("Core.Person", "S");
566 return $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
569 return $isStaff ? $au->hasPerm("Core.Person", "A") : $au->hasPerm("Core.Staff", "A");
572 return $isStaff ? $au->hasPerm("Core.Person", "D") : $au->hasPerm("Core.Staff", "D");
577 function onInsert($req, $roo)
580 if ($roo->authUser->id < 0) {
581 $g = DB_DataObject::factory('Groups');
583 $g->get('name', 'Administrators');
585 $p = DB_DataObject::factory('Group_Members');
586 $p->group_id = $g->id;
587 $p->user_id = $this->id;
590 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
594 if (!empty($req['project_id_addto'])) {
595 $pd = DB_DataObject::factory('ProjectDirectory');
596 $pd->project_id = $req['project_id_addto'];
597 $pd->person_id = $this->id;
599 $pd->office_id = $this->office_id;
600 $pd->company_id = $this->company_id;