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 ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
139 ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
140 $u->get('email', $ff->Pman['local_autoauth'])
142 $db = $this->getDatabaseConnection();
143 $sesPrefix = $db->dsn['database'];
144 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
149 // not in session or not matched...
150 $u = DB_DataObject::factory('Person');
151 $u->whereAdd(' LENGTH(passwd) > 0');
153 $error = PEAR::getStaticProperty('DB_DataObject','lastError');
155 die($error->toString()); // not really a good thing to do...
157 if (!$n){ // authenticated as there are no users in the system...
164 function getAuthUser()
166 if (!$this->isAuth()) {
169 $db = $this->getDatabaseConnection();
170 $sesPrefix = $db->dsn['database'];
171 if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
172 $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
174 $u = DB_DataObject::factory('Person');
175 if ($u->get($a->id)) { /// && strlen($u->passwd)) {
181 $u = DB_DataObject::factory('Person');
182 $u->whereAdd(' LENGTH(passwd) > 0');
184 $u = DB_DataObject::factory('Person');
193 $this->isAuth(); // force session start..
194 $db = $this->getDatabaseConnection();
195 $sesPrefix = $db->dsn['database'];
196 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
201 $this->isAuth(); // force session start..
202 $db = $this->getDatabaseConnection();
203 $sesPrefix = $db->dsn['database'];
204 $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
207 function genPassKey ($t)
209 return md5($this->email . $t. $this->passwd);
211 function simpleAuthKey($m = 0)
213 $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
215 return md5(implode(',' , array($month, $this->email , $this->passwd, $this->id)));
217 function checkPassword($val)
219 if (substr($this->passwd,0,1) == '$') {
220 return crypt($val,$this->passwd) == $this->passwd ;
222 // old style md5 passwords...- cant be used with courier....
223 return md5($val) == $this->passwd;
225 function setPassword($value)
228 while(strlen($salt)<9) {
229 $salt.=chr(rand(64,126));
230 //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
232 $this->passwd = crypt($value, '$1$'. $salt. '$');
238 $x = DB_DataObject::factory('Companies');
239 $x->get($this->company_id);
246 return $this->active;
248 function authUserName($n) // set username prior to acheck user exists query.
251 $this->whereAdd('LENGTH(passwd) > 1');
256 if ($val == $this->lang) {
266 function authUserArray()
269 $aur = $this->toArray();
273 //DB_DataObject::debugLevel(1);
274 $c = DB_Dataobject::factory('Companies');
275 $im = DB_Dataobject::factory('Images');
276 $c->joinAdd($im, 'LEFT');
278 $c->selectAs($c, 'company_id_%s');
279 $c->selectAs($im, 'company_id_logo_id_%s');
280 $c->id = $this->company_id;
284 $aur = array_merge( $c->toArray(),$aur);
286 if (empty($c->company_id_logo_id_id)) {
288 $im = DB_Dataobject::factory('Images');
289 $im->ontable = 'Companies';
291 $im->imgtype = 'LOGO';
293 $im->selectAs($im, 'company_id_logo_id_%s');
294 if ($im->find(true)) {
296 foreach($im->toArray() as $k=>$v) {
302 now handled by interface...
304 $lang = empty($this->lang) ? 'en' : $this->lang;
305 if (empty($_SESSION['Pman_I18N'][$lang])) {
306 require_once 'Pman/I18N.php';
307 $x = new Pman_I18N();
308 $x->setSession($this);
312 $aur['i18n'] =$_SESSION['Pman_I18N'][$lang];
315 $aur['perms'] = $this->getPerms();
316 $g = DB_DataObject::Factory('Group_Members');
317 $aur['groups'] = $g->listGroupMembership($this, 'name');
320 $aur['dailykey'] = '';
327 // ----------PERMS------ ----------------
330 //DB_DataObject::debugLevel(1);
331 // find out all the groups they are a member of.. + Default..
332 $g = DB_DataObject::Factory('Group_Rights');
337 return $g->adminRights();
340 $g = DB_DataObject::Factory('Group_Members');
342 // add the current user to the admin group..
343 $g = DB_DataObject::Factory('Groups');
344 if ($g->get('name', 'Administrators')) {
345 $gm = DB_DataObject::Factory('Group_Members');
346 $gm->group_id = $g->id;
347 $gm->user_id = $this->id;
353 $g = DB_DataObject::Factory('Group_Members');
354 $grps = $g->listGroupMembership($this);
355 $isAdmin = $g->inAdmin;
357 // the load all the perms for those groups, and add them all together..
358 // then load all those
359 $g = DB_DataObject::Factory('Group_Rights');
360 $ret = $g->listPermsFromGroupIds($grps, $isAdmin);
361 // echo '<PRE>';print_r($ret);
366 function hasPerm($name, $lvl)
368 static $pcache = array();
370 if (!isset($pcache[$this->id])) {
371 $pcache[$this->id] = $this->getPerms();
373 // echo "<PRE>";print_r($pcache[$au->id]);
374 // var_dump($pcache[$au->id]);
375 if (empty($pcache[$this->id][$name])) {
379 return strpos($pcache[$this->id][$name], $lvl) > -1;
383 // ------------ROO HOOKS------------------------------------
384 function applyFilters($q, $au)
386 if (!empty($q['query']['person_not_internal'])) {
387 $this->whereAdd(" join_company_id_id.isOwner = 0 ");
389 if (!empty($q['query']['person_internal_only_all'])) {
390 // must be internal and not current user (need for distribution list)
391 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
394 // -- for distribution
395 if (!empty($q['query']['person_internal_only'])) {
396 // must be internal and not current user (need for distribution list)
397 $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
399 //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
400 // ".id != ".$au->id);
401 $this->whereAdd("Person.id != {$au->id}");
404 if (!empty($q['query']['comptype_or_company_id'])) {
405 DB_DataObject::debugLevel(1);
406 $bits = explode(',', $q['query']['comptype_or_company_id']);
407 $id = (int) array_pop($bits);
408 $ct = $this->escape($bits[0]);
410 $this->whereAdd(" join_company_id_id.comptype = '$ct' OR Person.company_id = $id");
416 if (!empty($q['query']['person_inactive'])) {
417 // DB_Dataobject::debugLevel(1);
421 ///---------------- Group views --------
422 if (!empty($q['query']['in_group'])) {
423 // DB_DataObject::debugLevel(1);
424 $ing = (int) $q['query']['in_group'];
425 if ($q['query']['in_group'] == -1) {
426 // list all staff who are not in a group.
427 $this->whereAdd("Person.id NOT IN (
428 SELECT distinct(user_id) FROM Group_Members LEFT JOIN
429 Groups ON Groups.id = Group_Members.group_id
430 WHERE Groups.type = ".$q['query']['type']."
436 $this->whereAdd("Person.id IN (
437 SELECT distinct(user_id) FROM Group_Members
438 WHERE group_id = $ing
444 if (!empty($q['query']['not_in_directory'])) {
445 // it's a Person list..
446 // DB_DATaobjecT::debugLevel(1);
448 // specific to project directory which is single comp. login
450 $owncomp = DB_DataObject::Factory('Companies');
451 $owncomp->get('comptype', 'OWNER');
452 if ($q['company_id'] == $owncomp->id) {
457 if ( $q['query']['not_in_directory'] > -1) {
458 // can list current - so that it does not break!!!
459 $x->whereAdd('Person.id NOT IN
460 ( SELECT distinct person_id FROM ProjectDirectory WHERE
461 project_id = ' . $q['query']['not_in_directory'] . ' AND
462 company_id = ' . $this->company_id . ')');
466 if (!empty($q['query']['search'])) {
467 $s = $this->escape($q['query']['search']);
469 Person.name LIKE '%$s%' OR
470 Person.email LIKE '%$s%' OR
471 Person.role LIKE '%$s%' OR
472 Person.remarks LIKE '%$s%'
479 function setFromRoo($ar, $roo)
482 if (!empty($ar['passwd1'])) {
483 $this->setPassword($ar['passwd1']);
488 ($this->email == $roo->old->email)&&
489 ($this->company_id == $roo->old->company_id)
493 if (empty($this->email)) {
496 $xx = DB_Dataobject::factory('Person');
498 'email' => $this->email,
499 // 'company_id' => $x->company_id
503 return "Duplicate Email found";
507 function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
510 // do we have an empty system..
511 if ($au && $au->id == -1) {
515 // determine if it's staff!!!
517 if ($au->company()->comptype != 'OWNER') {
519 // - can not change company!!!
521 isset($changes['company_id']) &&
522 $changes['company_id'] != $au->company_id) {
525 // can only set new emails..
527 !empty($this->email) &&
528 isset($changes['email']) &&
529 $changes['email'] != $this->email) {
533 // edit self... - what about other staff members...
535 return $this->company_id == $au->company_id;
539 // yes, only owner company can mess with this...
540 $owncomp = DB_DataObject::Factory('Companies');
541 $owncomp->get('comptype', 'OWNER');
543 $isStaff = ($this->company_id == $owncomp->id);
547 // extra case change passwod?
548 case 'P': //??? password
549 // standard perms -- for editing + if the user is dowing them selves..
550 $ret = $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
551 return $ret || $au->id == $this->id;
554 return $au->hasPerm("Core.Person", "S");
557 return $isStaff ? $au->hasPerm("Core.Person", "E") : $au->hasPerm("Core.Staff", "E");
560 return $isStaff ? $au->hasPerm("Core.Person", "A") : $au->hasPerm("Core.Staff", "A");
563 return $isStaff ? $au->hasPerm("Core.Person", "D") : $au->hasPerm("Core.Staff", "D");
568 function onInsert($req, $roo)
571 if ($roo->authUser->id < 0) {
572 $g = DB_DataObject::factory('Groups');
574 $g->get('name', 'Administrators');
576 $p = DB_DataObject::factory('Group_Members');
577 $p->group_id = $g->id;
578 $p->user_id = $this->id;
581 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
585 if (!empty($req['project_id_addto'])) {
586 $pd = DB_DataObject::factory('ProjectDirectory');
587 $pd->project_id = $req['project_id_addto'];
588 $pd->person_id = $this->id;
590 $pd->office_id = $this->office_id;
591 $pd->company_id = $this->company_id;