DataObjects/Core_person.php
[Pman.Core] / DataObjects / Core_person.php
1 <?php
2 /**
3  * Table Definition for Person
4  */
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
6
7
8 class Pman_Core_DataObjects_Core_person extends DB_DataObject 
9 {
10     ###START_AUTOCODE
11     /* the code below is auto generated do not remove the above tag */
12
13     public $__table = 'core_person';                          // table name
14     public $id;                              // int(11)  not_null primary_key auto_increment
15     public $email;                           // string(128)  not_null
16     public $alt_email;
17     
18     public $company_id;                      // int(11)  
19     public $office_id;                       // int(11)  
20     public $name;                            // string(128)  not_null
21     public $firstname;                            // string(128)  not_null
22     public $lastname;                            // string(128)  not_null
23     public $phone;                           // string(32)  not_null
24     public $fax;                             // string(32)  not_null
25     
26     public $role;                            // string(32)  not_null
27     public $remarks;                         // blob(65535)  not_null blob
28     public $passwd;                          // string(64)  not_null
29     public $owner_id;                        // int(11)  not_null
30     public $lang;                            // string(8)  
31     public $no_reset_sent;                   // int(11)  
32     public $action_type;                     // string(32)  
33     public $project_id;                      // int(11)
34
35     
36     public $active;                          // int(11)  not_null
37     public $deleted_by;                      // int(11)  not_null
38     public $deleted_dt;                      // datetime(19)  binary
39
40
41     public $name_facebook; // VARCHAR(128) NULL;
42     public $url_blog; // VARCHAR(256) NULL ;
43     public $url_twitter; // VARCHAR(256) NULL ;
44     public $url_linkedin; // VARCHAR(256) NULL ;
45     public $linkedin_id; // VARCHAR(256) NULL ;
46     
47     public $phone_mobile; // varchar(32)  NOT NULL  DEFAULT '';
48     public $phone_direct; // varchar(32)  NOT NULL  DEFAULT '';
49     public $countries; // VARCHAR(128) NULL;
50     
51     /* the code above is auto generated do not remove the tag below */
52     ###END_AUTOCODE
53     
54     static $authUser = false;
55     
56  
57     function owner()
58     {
59         $p = DB_DataObject::Factory($this->tableName());
60         $p->get($this->owner_id);
61         return $p;
62     }
63     
64     /**
65      *
66      *
67      *
68      *
69      *  FIXME !!!! -- USE Pman_Core_Mailer !!!!!
70      *
71      *
72      *
73      *  
74      */
75     function buildMail($templateFile, $args)
76     {
77           
78         $args = (array) $args;
79         $content  = clone($this);
80         
81         foreach((array)$args as $k=>$v) {
82             $content->$k = $v;
83         }
84         
85         $ff = HTML_FlexyFramework::get();
86         
87         
88         //?? is this really the place for this???
89         if (
90                 !$ff->cli && 
91                 empty($args['no_auth']) &&
92                 !in_array($templateFile, array(
93                     // templates that can be sent without authentication.
94                      'password_reset' ,
95                      'password_welcome'
96                  ))
97             ) {
98             
99             $content->authUser = $this->getAuthUser();
100             if (!$content->authUser) {
101                 return PEAR::raiseError("Not authenticated");
102             }
103         }
104         
105         // should handle x-forwarded...
106         
107         $content->HTTP_HOST = isset($_SERVER["HTTP_HOST"]) ?
108             $_SERVER["HTTP_HOST"] :
109             (isset($ff->HTTP_HOST) ? $ff->HTTP_HOST : 'localhost');
110             
111         /* use the regex compiler, as it doesnt parse <tags */
112         
113         $tops = array(
114             'compiler'    => 'Flexy',
115             'nonHTML' => true,
116             'filters' => array('SimpleTags','Mail'),
117             //     'debug'=>1,
118         );
119         
120         
121         
122         if (!empty($args['templateDir'])) {
123             $tops['templateDir'] = $args['templateDir'];
124         }
125         
126         
127         
128         require_once 'HTML/Template/Flexy.php';
129         $template = new HTML_Template_Flexy( $tops );
130         $template->compile("mail/$templateFile.txt");
131         
132         /* use variables from this object to ouput data. */
133         $mailtext = $template->bufferedOutputObject($content);
134         
135         $htmlbody = false;
136         // if a html file with the same name exists, use that as the body
137         // I've no idea where this code went, it was here before..
138         if (false !== $template->resolvePath ( "mail/$templateFile.html" )) {
139             $tops['nonHTML'] = false;
140             $template = new HTML_Template_Flexy( $tops );
141             $template->compile("mail/$templateFile.html");
142             $htmlbody = $template->bufferedOutputObject($content);
143             
144         }
145         
146         
147         
148         //echo "<PRE>";print_R($mailtext);
149         //print_R($mailtext);exit;
150         /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
151         require_once 'Mail/mimeDecode.php';
152         require_once 'Mail.php';
153         
154         $decoder = new Mail_mimeDecode($mailtext);
155         $parts = $decoder->getSendArray();
156         
157         if (is_a($parts,'PEAR_Error')) {
158             return $parts;
159             //echo "PROBLEM: {$parts->message}";
160             //exit;
161         } 
162         list($recipents,$headers,$body) = $parts;
163         $recipents = array($this->email);
164         if (!empty($content->bcc) && is_array($content->bcc)) {
165             $recipents =array_merge($recipents, $content->bcc);
166         }
167         $headers['Date'] = date('r');
168         
169         if ($htmlbody !== false) {
170             require_once 'Mail/mime.php';
171             $mime = new Mail_mime(array('eol' => "\n"));
172             $mime->setTXTBody($body);
173             $mime->setHTMLBody($htmlbody);
174             // I think there might be code in mediaoutreach toEmail somewhere
175             // h embeds images here..
176             $body = $mime->get();
177             $headers = $mime->headers($headers);
178         }
179         
180         return array(
181             'recipients' => $recipents,
182             'headers'    => $headers,
183             'body'      => $body
184         );
185     }
186     
187     
188     /**
189      * send a template
190      * - user must be authenticate or args[no_auth] = true
191      *   or template = password_[reset|welcome]
192      * 
193      */
194     function sendTemplate($templateFile, $args)
195     {
196         $ar = $this->buildMail($templateFile, $args);
197       
198         //print_r($recipents);exit;
199         $mailOptions = PEAR::getStaticProperty('Mail','options');
200         $mail = Mail::factory("SMTP",$mailOptions);
201         
202         if (PEAR::isError($mail)) {
203             return $mail;
204         } 
205         $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
206         $ret = $mail->send($ar['recipients'],$ar['headers'],$ar['body']);
207         error_reporting($oe);
208        
209         return $ret;
210     }
211     
212   
213     
214     
215     function getEmailFrom()
216     {
217         if (empty($this->name)) {
218             return $this->email;
219         }
220         
221         return '"' . addslashes($this->name) . '" <' . $this->email . '>';
222     }
223     
224     function toEventString() 
225     {
226         return empty($this->name) ? $this->email : $this->name;
227     } 
228     
229     function verifyAuth()
230     { 
231         $ff= HTML_FlexyFramework::get();
232         if (!empty($ff->Pman['auth_comptype']) &&
233             (!$this->company_id || ($ff->Pman['auth_comptype'] != $this->company()->comptype))
234            ){
235             
236             $sesPrefix = $this->sesPrefix();
237        
238             self::$authUser = false;
239             $_SESSION[get_class($this)][$sesPrefix .'-auth'] = "";
240             
241             return false;
242             
243             //$ff->page->jerr("Login not permited to outside companies");
244         }
245         return true;
246         
247     }    
248    
249    
250     //   ---------------- authentication / passwords and keys stuff  ----------------
251     function isAuth()
252     {
253         @session_start();
254        
255         $ff= HTML_FlexyFramework::get();
256        
257         $sesPrefix = $this->sesPrefix();
258         
259         if (self::$authUser) {
260             return self::$authUser;
261         }
262         
263         
264         if (!empty($_SESSION[get_class($this)][$sesPrefix .'-auth'])) {
265             // in session...
266             $a = unserialize($_SESSION[get_class($this)][$sesPrefix .'-auth']);
267             $u = DB_DataObject::factory($this->tableName());
268             $u->autoJoin();
269             if ($a->id && $u->get($a->id)) { //&& strlen($u->passwd)) {
270                 if ($u->verifyAuth()) {
271                     self::$authUser = $u;
272                     return true;
273                 }
274             }
275             unset($_SESSION[get_class($this)][$sesPrefix .'-auth']);
276             unset($_SESSION[get_class($this)][$sesPrefix .'-timeout']);
277             setcookie('Pman.timeout', -1, time() + (30*60), '/');
278             return false;
279         }
280         
281         // http basic auth..
282         $u = DB_DataObject::factory($this->tableName());
283         
284         if (!empty($_SERVER['PHP_AUTH_USER']) 
285             &&
286             !empty($_SERVER['PHP_AUTH_PW'])
287             &&
288             $u->get('email', $_SERVER['PHP_AUTH_USER'])
289             &&
290             $u->checkPassword($_SERVER['PHP_AUTH_PW'])
291            ) {
292             // logged in via http auth
293             // http auth will not need session... 
294             //$_SESSION[get_class($this)][$sesPrefix .'-auth'] = serialize($u);
295             self::$authUser = $u;
296             return true; 
297         }
298         //die("test init");
299         if (!$this->canInitializeSystem()) {
300           //  die("can not init");
301             return false;
302         }
303         
304         
305         $auto_auth_allow = false;
306         if (!empty($ff->Pman['local_autoauth']) && $ff->Pman['local_autoauth'] === true) {
307             $auto_auth_allow  = true;
308         }
309         if  ( !empty($ff->Pman['local_autoauth'])
310              &&
311                 !empty($_SERVER['SERVER_ADDR']) &&
312                 !empty($_SERVER['REMOTE_ADDR']) &&
313                 (
314                     (
315                        $_SERVER['SERVER_ADDR'] == '127.0.0.1' &&
316                        $_SERVER['REMOTE_ADDR'] == '127.0.0.1'
317                    )
318                    ||
319                    (
320                        $_SERVER['SERVER_ADDR'] == '::1' &&
321                        $_SERVER['REMOTE_ADDR'] == '::1'
322                    )
323                 )
324                 
325             ){
326             $auto_auth_allow  = true;
327         }
328         
329         
330         if (empty($_SERVER['PATH_INFO']) || $_SERVER['PATH_INFO'] == '/Login' || $_SERVER['PATH_INFO'] == '/PasswordReset'
331         ) {
332             $auto_auth_allow  = false;
333         }
334         //var_dump($auto_auth_allow);
335         // local auth - 
336         $default_admin = false;
337         if ($auto_auth_allow) {
338             $group = DB_DataObject::factory('core_group');
339             $group->get('name', 'Administrators');
340             
341             $member = DB_DataObject::factory('core_group_member');
342             $member->autoJoin();
343             $member->group_id = $group->id;
344             $member->whereAdd("
345                 join_user_id_id.id IS NOT NULL
346             ");
347             if($member->find(true)){
348                 $default_admin = DB_DataObject::factory($this->tableName());
349                 $default_admin->autoJoin();
350                 if(!$default_admin->get($member->user_id)){
351                     $default_admin = false;
352                 }
353             }
354         }
355         
356         //var_dump($ff->Pman['local_autoauth']);         var_dump($_SERVER); exit;
357         $u = DB_DataObject::factory($this->tableName());
358         $u->autoJoin();
359         $ff = HTML_FlexyFramework::get();
360         
361         if ($auto_auth_allow && 
362             ($default_admin ||  $u->get('email', $ff->Pman['local_autoauth']))
363         ) {
364             
365             $user = $default_admin ? $default_admin->toArray() : $u->toArray();
366             
367             // if we request other URLS.. then we get auto logged in..
368             self::$authUser = $default_admin ? $default_admin : $u;;
369             //$_SESSION[get_class($this)][$sesPrefix .'-auth'] = serialize((object) $user);
370             return true;
371         }
372         
373         //var_dump(session_id());
374         //var_dump($_SESSION[__CLASS__]);
375         
376         //if (!empty(   $_SESSION[__CLASS__][$sesPrefix .'-empty'] )) {
377         //    return false;
378         //}
379         //die("got this far?");
380         // not in session or not matched...
381         $u = DB_DataObject::factory($this->tableName());
382         $u->whereAdd(' LENGTH(passwd) > 0');
383         $n = $u->count();
384         $_SESSION[get_class($this)][$sesPrefix .'-empty']  = $n;
385         if (class_exists('PEAR')) {
386             $error =  PEAR::getStaticProperty('DB_DataObject','lastError');
387             if ($error) {
388                 die($error->toString()); // not really a good thing to do...
389             }
390         }
391         if (!$n){ // authenticated as there are no users in the system...
392              return true;
393         }
394          return false;
395         
396     }
397     
398     function canInitializeSystem()
399     {
400         
401         return !strcasecmp(get_class($this) , __CLASS__);
402     }
403     
404     function getAuthUser()
405     {
406         if (!$this->isAuth()) {
407             return false;
408         }
409         
410         $ff= HTML_FlexyFramework::get();
411         
412         $sesPrefix = $this->sesPrefix();
413         
414         //var_dump(array(get_class($this),$sesPrefix .'-auth'));
415        
416         if (self::$authUser) {
417              
418             if (isset($_SESSION[get_class($this)][$sesPrefix .'-auth'])) {
419                 $_SESSION[get_class($this)][$sesPrefix .'-auth-timeout'] = time() + (30*60); // eg. 30 minutes
420                 setcookie('Pman.timeout', time() + (30*60), time() + (30*60), '/');
421             }
422             // not really sure why it's cloned..
423             return   clone (self::$authUser);
424              
425             
426         }
427         
428         
429         
430         if (!$this->canInitializeSystem()) {
431             return false;
432         }
433         
434         
435         
436         if (empty(   $_SESSION[get_class($this)][$sesPrefix .'-empty'] )) {
437             $u = DB_DataObject::factory($this->tableName());
438             $u->whereAdd(' LENGTH(passwd) > 0');
439             $_SESSION[get_class($this)][$sesPrefix .'-empty']  = $u->count();
440         }
441                 
442              
443         if (
444             isset(   $_SESSION[get_class($this)][$sesPrefix .'-empty'] ) && 
445             $_SESSION[get_class($this)][$sesPrefix .'-empty']  < 1
446         ) {
447             
448             // fake person - open system..
449             //$ce = DB_DataObject::factory('core_enum');
450             //$ce->initEnums();
451             
452             
453             $u = DB_DataObject::factory($this->tableName());
454             $u->id = -1;
455             
456             // if a company has been created fill that in in company_id_id
457             $c = DB_DAtaObject::factory('core_company')->lookupOwner();
458             if ($c) {
459                 $u->company_id_id = $c->pid();
460                 $u->company_id = $c->pid();
461             }
462             
463             return $u;
464             
465         }
466         return false;
467     }     
468     function login()
469     {
470         $this->isAuth(); // force session start..
471         if (!$this->verifyAuth()) { // check for company valid..
472             return false;
473         }
474         
475         // open up iptables at login..
476         $dbname = $this->databaseNickname();
477         touch( '/tmp/run_pman_admin_iptables-'.$dbname);
478          
479         // refresh admin group if we are logged in as one..
480         //DB_DataObject::debugLevel(1);
481         $g = DB_DataObject::factory('core_group');
482         $g->type = 0;
483         $g->get('name', 'Administrators');
484         $gm = DB_DataObject::Factory('core_group_member');
485         if (in_array($g->id,$gm->listGroupMembership($this))) {
486             // refresh admin groups.
487             $gr = DB_DataObject::Factory('core_group_right');
488             $gr->applyDefs($g, 0);
489         }
490         
491         $sesPrefix = $this->sesPrefix();
492         
493         // we should not store the whole data in the session - otherwise it get's huge.
494         $p = DB_DAtaObject::Factory($this->tableName());
495         $p->get($this->pid());
496         
497         $d = $p->toArray();
498         
499         $_SESSION[get_class($this)][$sesPrefix .'-auth-timeout'] = time() + (30*60); // eg. 30 minutes
500         setcookie('Pman.timeout', time() + (30*60), time() + (30*60), '/');
501         
502         //var_dump(array(get_class($this),$sesPrefix .'-auth'));
503         $_SESSION[get_class($this)][$sesPrefix .'-auth'] = serialize((object)$d);
504         
505         $pp = DB_DAtaObject::Factory($this->tableName());
506         $pp->get($this->pid());
507         $pp->autoJoin();
508         
509         self::$authUser = $pp;
510         // ensure it's written so that ajax calls can fetch it..
511         
512         
513         
514     }
515     function logout()
516     {
517         $this->isAuth(); // force session start..
518         
519         $sesPrefix = $this->sesPrefix();
520         
521         $_SESSION[get_class($this)][$sesPrefix .'-auth-timeout'] = -1;
522         
523         $_SESSION[get_class($this)][$sesPrefix .'-auth'] = "";
524         
525         self::$authUser = false;
526         
527     }    
528     function genPassKey ($t) 
529     {
530         return md5($this->email . $t. $this->passwd);
531     }
532     function simpleAuthKey($m = 0)
533     {
534         $month = $m > -1 ? date('Y-m') : date('Y-m', strtotime('LAST MONTH'));
535         
536         return md5(implode(',' ,  array($month, $this->email , $this->passwd, $this->id)));
537     } 
538     
539     function checkTwoFactorAuthentication($val)
540     {
541         
542         
543         // also used in login
544         require_once 'System.php';
545         
546         if(
547             empty($this->id) ||
548             empty($this->oath_key)
549         ) {
550             return false;
551         }
552         
553         $oathtool = System::which('oathtool');
554         
555         if (!$oathtool) {
556             return false;
557         }
558         
559         $cmd = "{$oathtool} --totp --base32 " . escapeshellarg($this->oath_key);
560         
561         $password = exec($cmd);
562         
563         return ($password == $val) ? true : false;
564     }
565     
566     function checkPassword($val)
567     {
568         if (substr($this->passwd,0,1) == '$') {
569             if (function_exists('pasword_verify')) {
570                 return password_verify($val, $this->passwd);
571             }
572             return crypt($val,$this->passwd) == $this->passwd ;
573         }
574         // old style md5 passwords...- cant be used with courier....
575         return md5($val) == $this->passwd;
576     }
577     
578     function setPassword($value) 
579     {
580         if (function_exists('pasword_hash')) {
581             return password_hash($value);
582         }
583         
584         $salt='';
585         while(strlen($salt)<9) {
586             $salt.=chr(rand(64,126));
587             //php -r var_dump(crypt('testpassword', '$1$'. (rand(64,126)). '$'));
588         }
589         $this->passwd = crypt($value, '$1$'. $salt. '$');
590        
591        
592     }      
593     
594     function generatePassword($length = 5) // genearte a password (add set 'rawPasswd' to it's value)
595     {
596         require_once 'Text/Password.php';
597         $this->rawPasswd = strtr(ucfirst(Text_Password::create($length)).ucfirst(Text_Password::create($length)), array(
598         "a"=>"4", "e"=>"3",  "i"=>"1",  "o"=>"0", "s"=>"5",  "t"=>"7"));
599         $this->setPassword($this->rawPasswd);
600         return $this->rawPasswd;
601     }
602     
603     function company()
604     {
605         $x = DB_DataObject::factory('core_company');
606         $x->autoJoin();
607         $x->get($this->company_id);
608         return $x;
609     }
610     function loadCompany()
611     {
612         $this->company = $this->company();
613     }
614     
615     function active()
616     { 
617         return $this->active;
618     }
619     function authUserName($n) // set username prior to acheck user exists query.
620     {
621         
622         $this->whereAdd('LENGTH(passwd) > 1'); 
623         $this->email = $n;
624     }
625     function lang()
626     {
627         if (!func_num_args()) {
628             return $this->lang;
629         }
630         $ar = func_get_args();
631         $val = array_shift($ar);
632         if ($val == $this->lang) {
633             return;
634         }
635         $uu = clone($this);
636         $this->lang = $val;
637         $this->update($uu);
638         return $this->lang;
639     }
640             
641     
642     function authUserArray()
643     {
644         
645         $aur = $this->toArray();
646         
647         if ($this->id < 1) {
648             return $aur;
649         }
650         
651         
652         //DB_DataObject::debugLevel(1);
653         $c = DB_Dataobject::factory('core_company');
654         $im = DB_Dataobject::factory('Images');
655         $c->joinAdd($im, 'LEFT');
656         $c->selectAdd();
657         $c->selectAs($c, 'company_id_%s');
658         $c->selectAs($im, 'company_id_logo_id_%s');
659         $c->id = $this->company_id;
660         $c->limit(1);
661         $c->find(true);
662         
663         $aur = array_merge( $c->toArray(),$aur);
664         
665         if (empty($c->company_id_logo_id_id))  {
666                  
667             $im = DB_Dataobject::factory('Images');
668             $im->ontable = DB_DataObject::factory('core_company')->tableName();
669             $im->onid = $c->id;
670             $im->imgtype = 'LOGO';
671             $im->limit(1);
672             $im->selectAdd();
673             $im->selectAs($im,  'company_id_logo_id_%s');
674             if ($im->find(true)) {
675                 
676                 foreach($im->toArray() as $k=>$v) {
677                     if (!preg_match('/^company_id_logo_id_/', $k)) {
678                         continue;
679                     }
680                     $aur[$k] = $v;
681                 }
682             }
683         }
684       
685         // perms + groups.
686         $aur['perms']  = $this->getPerms();
687         $g = DB_DataObject::Factory('core_group_member');
688         $aur['groups']  = $g->listGroupMembership($this, 'name');
689         
690         $aur['passwd'] = '';
691         $aur['dailykey'] = '';
692         $aur['oath_key'] = '';
693         
694         $aur['oath_key_enable'] = !empty($this->oath_key);
695         $aur['require_oath'] = 1;
696         
697         $s = DB_DataObject::Factory('core_setting');
698         $oath_require = $s->lookup('core', 'two_factor_auth_required');
699         $aur['require_oath'] = $oath_require ?  $oath_require->val : 0;
700         
701         return $aur;
702     }
703     
704     //   ----------PERMS------  ----------------
705     function getPerms() 
706     {
707          //DB_DataObject::debugLevel(1);
708         // find out all the groups they are a member of.. + Default..
709         
710         // ------ INIITIALIZE IF NO GROUPS ARE SET UP.
711         
712         $g = DB_DataObject::Factory('core_group_right');
713         if (!$g->count()) {
714             $g->genDefault();
715         }
716         
717         if ($this->id < 0) {
718             return $g->adminRights(); // system is not set up - so they get full rights.
719         }
720         //DB_DataObject::debugLevel(1);
721         $g = DB_DataObject::Factory('core_group_member');
722         $g->whereAdd('group_id is NOT NULL AND user_id IS NOT NULL');
723         if (!$g->count()) {
724             // add the current user to the admin group..
725             $g = DB_DataObject::Factory('core_group');
726             if ($g->get('name', 'Administrators')) {
727                 $gm = DB_DataObject::Factory('core_group_member');
728                 $gm->group_id = $g->id;
729                 $gm->user_id = $this->id;
730                 $gm->insert();
731             }
732             
733         }
734         
735         // ------ STANDARD PERMISSION HANDLING.
736         $isOwner = $this->company()->comptype == 'OWNER';
737         $g = DB_DataObject::Factory('core_group_member');
738         $grps = $g->listGroupMembership($this);
739        //var_dump($grps);
740         $isAdmin = $g->inAdmin;   //???  what???
741         //echo '<PRE>'; print_r($grps);var_dump($isAdmin);
742         // the load all the perms for those groups, and add them all together..
743         // then load all those 
744         $g = DB_DataObject::Factory('core_group_right');
745         $ret =  $g->listPermsFromGroupIds($grps, $isAdmin, $isOwner);
746         //echo '<PRE>';print_r($ret);
747         return $ret;
748          
749         
750     }
751     /**
752      *Basic group fetching - probably needs to filter by type eventually.
753      *
754      *@param String $what - fetchall() argument - eg. 'name' returns names of all groups that they are members of.
755      */
756     
757     function groups($what=false)
758     {
759         $g = DB_DataObject::Factory('core_group_member');
760         $grps = $g->listGroupMembership($this);
761         $g = DB_DataObject::Factory('core_group');
762         $g->whereAddIn('id', $grps, 'int');
763         return $g->fetchAll($what);
764         
765     }
766     
767     
768     
769     function hasPerm($name, $lvl) 
770     {
771         static $pcache = array();
772         
773         if (!isset($pcache[$this->id])) {
774             $pcache[$this->id] = $this->getPerms();
775         }
776         
777        // echo "<PRE>";print_r($pcache[$au->id]);
778        // var_dump($pcache[$au->id]);
779         if (empty($pcache[$this->id][$name])) {
780             return false;
781         }
782         
783         return strpos($pcache[$this->id][$name], $lvl) > -1;
784         
785     }    
786     
787     //  ------------ROO HOOKS------------------------------------
788     function applyFilters($q, $au, $roo)
789     {
790         //DB_DataObject::DebugLevel(1);
791         if(!empty($q['_to_qr_code'])){
792             $person = DB_DataObject::factory('Core_person');
793             $person->id = $q['id']; 
794             
795             if(!$person->find(true)) {
796                 $roo->jerr('_invalid_person');
797             }
798             
799             $hash = $this->generateOathKey();
800             
801             $_SESSION[__CLASS__] = 
802                 isset($_SESSION[__CLASS__]) ? 
803                     $_SESSION[__CLASS__] : array();
804             $_SESSION[__CLASS__]['oath'] = 
805                 isset($_SESSION[__CLASS__]['oath']) ? 
806                     $_SESSION[__CLASS__]['oath'] : array();
807                 
808             $_SESSION[__CLASS__]['oath'][$person->id] = $hash;
809
810             $qrcode = $person->generateQRCode($hash);
811             
812             if(empty($qrcode)){
813                 $roo->jerr('Fail to generate QR Code');
814             }
815             
816             $roo->jok($qrcode);
817         }
818         
819         if(!empty($q['two_factor_auth_code'])) {
820             $person = DB_DataObject::factory('core_person');
821             $person->get($q['id']);
822             $o = clone($person);
823             $person->oath_key = $_SESSION[__CLASS__]['oath'][$person->id];
824             
825             if($person->checkTwoFactorAuthentication($q['two_factor_auth_code'])) {
826                 $person->update($o);
827                 unset($_SESSION[__CLASS__]['oath'][$person->id]);
828                 $roo->jok('DONE');
829             }
830             
831             $roo->jerr('_invalid_auth_code');
832         }
833         
834         if(!empty($q['oath_key_disable'])) {
835             $person = DB_DataObject::factory('core_person');
836             $person->get($q['id']);
837             
838             $o = clone($person);
839             
840             $person->oath_key = '';
841             $person->update($o);
842             
843             $roo->jok('DONE');
844         }
845         
846         if (!empty($q['query']['is_owner'])) {
847             $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
848         }
849         
850         if (!empty($q['query']['person_not_internal'])) {
851             $this->whereAdd(" join_company_id_id.isOwner = 0 ");
852         }
853         
854         if (!empty($q['query']['person_internal_only_all'])) {
855             
856             
857             // must be internal and not current user (need for distribution list)
858             // user has a projectdirectory entry and role is not blank.
859             //DB_DataObject::DebugLevel(1);
860             $pd = DB_DataObject::factory('ProjectDirectory');
861             $pd->whereAdd("role != ''");
862             $pd->selectAdd();
863             $pd->selectAdd('distinct(person_id) as person_id');
864             $roled = $pd->fetchAll('person_id');
865             $rs = $roled  ? "  OR
866                     {$this->tableName()}.id IN (".implode(',', $roled) . ") 
867                     " : '';
868             $this->whereAdd(" join_company_id_id.comptype = 'OWNER' $rs ");
869             
870         }
871         // -- for distribution
872         if (!empty($q['query']['person_internal_only'])) {
873             // must be internal and not current user (need for distribution list)
874             $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
875             
876             //$this->whereAdd(($this->tableName() == 'Person' ? 'Person' : "join_person_id_id") .
877             //    ".id  != ".$au->id);
878             $this->whereAdd("{$this->tableName()}.id != {$au->id}");
879         } 
880         
881         if (!empty($q['query']['comptype_or_company_id'])) {
882            // DB_DataObject::debugLevel(1);
883             $bits = explode(',', $q['query']['comptype_or_company_id']);
884             $id = (int) array_pop($bits);
885             $ct = $this->escape($bits[0]);
886             
887             $this->whereAdd(" join_company_id_id.comptype = '$ct' OR {$this->tableName()}.company_id = $id");
888             
889         }
890         
891         
892         // staff list..
893         if (!empty($q['query']['person_inactive'])) {
894            // DB_Dataobject::debugLevel(1);
895             $this->active = 1;
896         }
897         $tn_p = $this->tableName();
898         $tn_gm = DB_DataObject::Factory('core_group_member')->tableName();
899         $tn_g = DB_DataObject::Factory('core_group')->tableName();
900
901         ///---------------- Group views --------
902         if (!empty($q['query']['in_group'])) {
903             // DB_DataObject::debugLevel(1);
904             $ing = (int) $q['query']['in_group'];
905             if ($q['query']['in_group'] == -1) {
906              
907                 // list all staff who are not in a group.
908                 $this->whereAdd("{$this->tableName()}.id NOT IN (
909                     SELECT distinct(user_id) FROM $tn_gm LEFT JOIN
910                         $tn_g ON $tn_g.id = $tn_gm.group_id)");
911                 
912             } else {
913                 
914                 $this->whereAdd("$tn_p.id IN (
915                     SELECT distinct(user_id) FROM $tn_gm
916                         WHERE group_id = $ing
917                     )");
918                }
919             
920         }
921         
922         if(!empty($q['in_group_name'])){
923             
924             $v = $this->escape($q['in_group_name']);
925             
926             $this->whereAdd("
927                 $tn_p.id IN (
928                     SELECT 
929                         DISTINCT(user_id) FROM $tn_gm
930                     LEFT JOIN
931                         $tn_g
932                     ON
933                         $tn_g.id = $tn_gm.group_id
934                     WHERE 
935                         $tn_g.name = '{$v}'
936                 )"
937             );
938         }
939         
940         // #2307 Search Country!!
941         if (!empty($q['query']['in_country'])) {
942             // DB_DataObject::debugLevel(1);
943             $inc = $q['query']['in_country'];
944             $this->whereAdd("$tn_p.countries LIKE '%{$inc}%'");
945         }
946         
947         if (!empty($q['query']['not_in_directory'])) { 
948             // it's a Person list..
949             // DB_DATaobjecT::debugLevel(1);
950             
951             // specific to project directory which is single comp. login
952             //
953             $owncomp = DB_DataObject::Factory('core_company');
954             $owncomp->get('comptype', 'OWNER');
955             if ($q['company_id'] == $owncomp->id) {
956                 $this->active =1;
957             }
958             
959             
960
961             if ( $q['query']['not_in_directory'] > -1) {
962                 $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
963                 // can list current - so that it does not break!!!
964                 $this->whereAdd("$tn_p.id NOT IN 
965                     ( SELECT distinct person_id FROM $tn_pd WHERE
966                         project_id = " . $q['query']['not_in_directory'] . " AND 
967                         company_id = " . $this->company_id . ')');
968             }
969         }
970            
971         if (!empty($q['query']['role'])) { 
972             // it's a Person list..
973             // DB_DATaobjecT::debugLevel(1);
974             
975             // specific to project directory which is single comp. login
976             //
977             $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
978                 // can list current - so that it does not break!!!
979             $this->whereAdd("$tn_p.id IN 
980                     ( SELECT distinct person_id FROM $tn_pd WHERE
981                         role = '". $this->escape($q['query']['role']) ."'
982             )");
983         
984         }
985         
986         
987         if (!empty($q['query']['project_member_of'])) {
988                // this is also a flag to return if they are a member..
989             //DB_DataObject::debugLevel(1);
990             $do = DB_DataObject::factory('ProjectDirectory');
991             $do->project_id = $q['query']['project_member_of'];
992             $tn_pd = DB_DataObject::Factory('ProjectDirectory')->tableName();
993             $this->joinAdd($do,array('joinType' => 'LEFT', 'useWhereAsOn' => true));
994             $this->selectAdd("IF($tn_pd.id IS NULL, 0,  $tn_pd.id )  as is_member");
995                 
996                 
997             if (!empty($q['query']['project_member_filter'])) {
998                 $this->having('is_member !=0');
999             
1000             }
1001             
1002         }
1003         
1004         if(!empty($q['query']['name'])){
1005             $this->whereAdd("
1006                 {$this->tableName()}.name LIKE '%{$this->escape($q['query']['name'])}%'
1007             ");
1008         }
1009          if(!empty($q['query']['name_starts'])){
1010             $this->whereAdd("
1011                 {$this->tableName()}.name LIKE '{$this->escape($q['query']['name_starts'])}%'
1012             ");
1013         }
1014         
1015         if (!empty($q['query']['search'])) {
1016             
1017             // use our magic search builder...
1018             
1019              require_once 'Text/SearchParser.php';
1020             $x = new Text_SearchParser($q['query']['search']);
1021             
1022             $props = array(
1023                     "$tn_p.name",
1024                     "$tn_p.email",
1025                     "$tn_p.role",
1026                     "$tn_p.phone",
1027                     "$tn_p.remarks",
1028                     "join_company_id_id.name"
1029             );
1030             $tbcols = $this->table();
1031             foreach(array('firstname','lastname') as $k) {
1032                 if (isset($tbcols[$k])) {
1033                     $props[] = "{$tn_p}.{$k}";
1034                 }
1035             }
1036             
1037             
1038             
1039             
1040             $str =  $x->toSQL(array(
1041                 'default' => $props,
1042                 'map' => array(
1043                     'company' => 'join_company_id_id.name',
1044                     //'country' => 'Clipping.country',
1045                     //  'media' => 'Clipping.media_name',
1046                 ),
1047                 'escape' => array($this->getDatabaseConnection(), 'escapeSimple'), /// pear db or mdb object..
1048
1049             ));
1050             
1051             
1052             $this->whereAdd($str); /*
1053                         $tn_p.name LIKE '%$s%'  OR
1054                         $tn_p.email LIKE '%$s%'  OR
1055                         $tn_p.role LIKE '%$s%'  OR
1056                         $tn_p.phone LIKE '%$s%' OR
1057                         $tn_p.remarks LIKE '%$s%' 
1058                         
1059                     ");*/
1060         }
1061         
1062         // project directory rules -- this may distrupt things.
1063         $p = DB_DataObject::factory('ProjectDirectory');
1064         // if project directories are set up, then we can apply project query rules..
1065         if ($p->count()) {
1066             $p->autoJoin();
1067             $pids = $p->projects($au);
1068             if (isset($q['query']['project_id'])) {   
1069                 $pid = (int)$q['query']['project_id'];
1070                 if (!in_array($pid, $pids)) {
1071                     $roo->jerr("Project not in users valid projects");
1072                 }
1073                 $pids = array($pid);
1074             }
1075             // project roles..
1076             //if (empty($q['_anyrole'])) {  // should be project_directry_role
1077             //    $p->whereAdd("{$p->tableName()}.role != ''");
1078             // }
1079             if (!empty($q['query']['role'])) {  // should be project_directry_role
1080                 $role = $this->escape($q['query']['role']); 
1081                
1082                 $p->whereAdd("{$p->tableName()}.role LIKE '%{$role}%'");
1083                  
1084             }
1085             
1086             if (!$roo->hasPerm('Core.Projects_All', 'S')) {
1087                 $peps = $p->people($pids);
1088                 $this->whereAddIn("{$tn}.id", $peps, 'int');
1089             }
1090         }    
1091         
1092         // fixme - this needs a more generic fix - it was from the mtrack_person code...
1093         if (isset($q['query']['ticket_id'])) {  
1094             // find out what state the ticket is in.
1095             $t = DB_DataObject::Factory('mtrack_ticket');
1096             $t->autoJoin();
1097             $t->get($q['query']['ticket_id']);
1098             
1099             if (!$this->checkPerm('S', $au)) {
1100                 $roo->jerr("permssion denied to query state of ticket");
1101             }
1102             
1103             $p = DB_DataObject::factory('ProjectDirectory');
1104             $pids = array($t->project_id);
1105            
1106             $peps = $p->people($pids);
1107             
1108             $this->whereAddIn($this->tableName().'.id', $peps, 'int');
1109             
1110             //$this->whereAdd('join_prole != ''");
1111             
1112         }
1113         
1114         /*
1115          * Seems we never expose oath_key / passwd, so...
1116          */
1117         
1118         if($this->tableName() == 'core_person'){
1119             $this->_extra_cols = array('length_passwd', 'length_oath_key');
1120         
1121             $this->selectAdd("
1122                 LENGTH({$this->tableName()}.passwd) AS length_passwd,
1123                 LENGTH({$this->tableName()}.oath_key) AS length_oath_key
1124             ");
1125         }
1126         
1127         
1128     }
1129     
1130     function setFromRoo($ar, $roo)
1131     {
1132         $this->setFrom($ar);
1133         
1134         if(!empty($ar['_enable_oath_key'])){
1135             $oath_key = $this->generateOathKey();
1136         }
1137         
1138         if (!empty($ar['passwd1'])) {
1139             $this->setPassword($ar['passwd1']);
1140         }
1141         
1142         if (    $this->id &&
1143                 ($this->email == $roo->old->email)&&
1144                 ($this->company_id == $roo->old->company_id)
1145             ) {
1146             return true;
1147         }
1148         if (empty($this->email)) {
1149             return true;
1150         }
1151         // this only applies to our owner company..
1152         $c = $this->company();
1153         if (empty($c->comptype_name) || $c->comptype_name != 'OWNER') {
1154             return true;
1155         }
1156         
1157         
1158         $xx = DB_Dataobject::factory($this->tableName());
1159         $xx->setFrom(array(
1160             'email' => $this->email,
1161            // 'company_id' => $x->company_id
1162         ));
1163         
1164         if ($xx->count()) {
1165             return "Duplicate Email found";
1166         }
1167         
1168         return true;
1169     }
1170     /**
1171      *
1172      * before Delete - delete significant dependancies..
1173      * this is called after checkPerm..
1174      */
1175     
1176     function beforeDelete($dependants_array, $roo)
1177     {
1178         //delete group membership except for admin group..
1179         // if they are a member of admin group do not delete anything.
1180         $default_admin = false;
1181         
1182         $e = DB_DataObject::Factory('Events');
1183         $e->whereAdd('person_id = ' . $this->id);
1184         
1185         $g = DB_DataObject::Factory('core_group');
1186         $g->get('name', 'Administrators');  // select * from core_group where name = 'Administrators'
1187         
1188         $p = DB_DataObject::Factory('core_group_member');
1189         $p->setFrom(array(
1190             'user_id' => $this->id,
1191             'group_id' => $g->id
1192         ));
1193
1194         if ($p->count()) {
1195            $roo->jerr("Please remove this user from the Administrator group before deleting");
1196         }
1197  
1198          
1199         $p = DB_DataObject::Factory('core_group_member');
1200         $p->user_id = $this->id;
1201         $mem = $p->fetchAll();  // fetch all the rows and set the $mem variable to the rows data, just like mysqli_fetch_assoc
1202         $e->logDeletedRecord($mem);
1203                 
1204         foreach($mem as $p) { 
1205             $p->delete();
1206         }  
1207         
1208         $e = DB_DataObject::Factory('Events');        
1209         $e->person_id = $this->id;
1210         $eve = $e->fetchAll();  // fetch all the rows and set the $mem variable to the rows data, just like mysqli_fetch_assoc
1211
1212         $e->logDeletedRecord($eve);
1213         foreach($eve as $e) { 
1214             $e->delete();
1215         }  
1216         
1217         
1218         // anything else?  
1219         
1220     }
1221     
1222     
1223     /***
1224      * Check if the a user has access to modify this item.
1225      * @param String $lvl Level (eg. Core.Projects)
1226      * @param Pman_Core_DataObjects_Person $au The authenticated user.
1227      * @param boolean $changes alllow changes???
1228      *
1229      * @return false if no access..
1230      */
1231     function checkPerm($lvl, $au, $changes=false) //heck who is trying to access this. false == access denied..
1232     {
1233          
1234        // do we have an empty system..
1235         if ($au && $au->id == -1) {
1236             return true;
1237         }
1238         // if not authenticated... do not allow in???
1239         if (!$au ) {
1240             return false;
1241         }
1242         
1243         // determine if it's staff!!!
1244         $owncomp = DB_DataObject::Factory('core_company');
1245         $owncomp->get('comptype', 'OWNER');
1246         $isStaff = ($au->company_id ==  $owncomp->id);
1247        
1248        
1249         if (!$isStaff) {
1250             
1251             // - can not change company!!!
1252             if ($changes && 
1253                 isset($changes['company_id']) && 
1254                 $changes['company_id'] != $au->company_id) {
1255                 return false;
1256             }
1257             // can only set new emails..
1258             if ($changes && 
1259                     !empty($this->email) && 
1260                     isset($changes['email']) && 
1261                     $changes['email'] != $this->email) {
1262                 return false;
1263             }
1264             
1265             
1266             // mtrack had the idea that all 'S' should be allowed.. - but filtered later..
1267             // ???? do we want this?
1268             
1269             // edit self... - what about other staff members...
1270             
1271             //return $this->company_id == $au->company_id;
1272         }
1273         
1274          
1275         // yes, only owner company can mess with this...
1276         
1277         
1278         
1279     
1280         switch ($lvl) {
1281             // extra case change passwod?
1282             case 'P': //??? password
1283                 // standard perms -- for editing + if the user is dowing them selves..
1284                 $ret = $isStaff ? $au->hasPerm("Core.Staff", "E") : $au->hasPerm("Core.Person", "E");
1285                 return $ret || $au->id == $this->id;
1286             
1287             default:                
1288                 return $isStaff ? $au->hasPerm("Core.Staff", $lvl) : $au->hasPerm("Core.Person", $lvl);
1289         
1290         }
1291         return false;
1292     }
1293     
1294     function beforeInsert($req, $roo)
1295     {
1296         $p = DB_DataObject::factory('core_person');
1297         if ($roo->authUser->id > -1 ||  $p->count() > 1) {
1298             return;
1299         }
1300         $c = DB_DataObject::Factory('core_company');
1301         $tc = $c->count();
1302         
1303         if (!$tc || $tc> 1) {
1304             $roo->jerr("can not create initial user as multiple companies already exist");
1305         }
1306         $c->find(true);
1307         $this->company_id = $c->id;
1308         $this->email = trim($this->email);
1309         
1310     }
1311     
1312     function onInsert($req, $roo)
1313     {
1314          
1315         $p = DB_DataObject::factory('core_person');
1316         if ($roo->authUser->id < 0 && $p->count() == 1) {
1317             // this seems a bit risky...
1318             
1319             $g = DB_DataObject::factory('core_group');
1320             $g->initGroups();
1321             
1322             $g->type = 0;
1323             $g->get('name', 'Administrators');
1324             
1325             $p = DB_DataObject::factory('core_group_member');
1326             $p->group_id = $g->id;
1327             $p->user_id = $this->id;     
1328             if (!$p->count()) {
1329                 $p->insert();
1330                 $roo->addEvent("ADD", $p, $g->toEventString(). " Added " . $this->toEventString());
1331             }
1332             $this->login();
1333         }
1334         if (!empty($req['project_id_addto'])) {
1335             $pd = DB_DataObject::factory('ProjectDirectory');
1336             $pd->project_id = $req['project_id_addto'];
1337             $pd->person_id = $this->id; 
1338             $pd->ispm =0;
1339             $pd->office_id = $this->office_id;
1340             $pd->company_id = $this->company_id;
1341             $pd->insert();
1342         }
1343         
1344     }
1345     
1346     function importFromArray($roo, $persons, $opts)
1347     {
1348         if (empty($opts['prefix'])) {
1349             $roo->jerr("opts[prefix] is empty - you can not just create passwords based on the user names");
1350         }
1351         
1352         if (!is_array($persons) || empty($persons)) {
1353             $roo->jerr("error in the person data. - empty on not valid");
1354         }
1355         DB_DataObject::factory('core_group')->initGroups();
1356         
1357         foreach($persons as $person){
1358             $p = DB_DataObject::factory('core_person');
1359             if($p->get('name', $person['name'])){
1360                 continue;
1361             }
1362             $p->setFrom($person);
1363             
1364             $companies = DB_DataObject::factory('core_company');
1365             if(!$companies->get('comptype', 'OWNER')){
1366                 $roo->jerr("Missing OWNER companies!");
1367             }
1368             $p->company_id = $companies->pid();
1369             // strip the 'spaces etc.. make lowercase..
1370             $name = strtolower(str_replace(' ', '', $person['name']));
1371             $p->setPassword("{$opts['prefix']}{$name}");
1372             $p->insert();
1373             // set up groups
1374             // if $person->groups is set.. then
1375             // add this person to that group eg. groups : [ 'Administrator' ] 
1376             if(!empty($person['groups'])){
1377                 $groups = DB_DataObject::factory('core_group');
1378                 if(!$groups->get('name', $person['groups'])){
1379                     $roo->jerr("Missing groups : {$person['groups']}");
1380                 }
1381                 $gm = DB_DataObject::factory('core_group_member');
1382                 $gm->change($p, $groups, true);
1383             }
1384             
1385             $p->onInsert(array(), $roo);
1386         }
1387     }
1388     
1389     // this is for the To: "{getEmailName()}" <email@address>
1390     // not good for Dear XXXX, - use {person.firstname} for that.
1391     function getEmailName()
1392     {
1393         $name = array();
1394         
1395         if(!empty($this->honor)){
1396             array_push($name, $this->honor);
1397         }
1398         
1399         if(!empty($this->name)){
1400             array_push($name, $this->name);
1401             
1402             return implode(' ', $name);
1403         }
1404         
1405         if(!empty($this->firstname) || !empty($this->lastname)){
1406             array_push($name, $this->firstname);
1407             array_push($name, $this->lastname);
1408             
1409             $name = array_filter($name);
1410             
1411             return implode(' ', $name);
1412         }
1413         
1414         return $this->email;
1415     }
1416     
1417     function sesPrefix()
1418     {
1419         $ff= HTML_FlexyFramework::get();
1420         
1421         $appname = empty($ff->appNameShort) ? $ff->project : $ff->project . '-' . $ff->appNameShort;
1422         
1423         $dname = method_exists($this, 'getDatabaseConnection') ? $this->getDatabaseConnection()->dsn['database'] : $this->databaseNickname();
1424         
1425         $sesPrefix = $appname.'-' .get_class($this) .'-' . $dname;
1426
1427         return $sesPrefix;
1428     }
1429     
1430     function loginPublic() // used where???
1431     {
1432         $this->isAuth(); // force session start..
1433          
1434         $db = $this->getDatabaseConnection();
1435         
1436         $ff = HTML_FlexyFramework::get();
1437         
1438         if(empty($ff->Pman) || empty($ff->Pman['login_public'])){
1439             return false;
1440         }
1441         
1442         $sesPrefix = $ff->Pman['login_public'] . '-' .get_class($this) .'-'.$db->dsn['database'] ;
1443         
1444         $p = DB_DAtaObject::Factory($this->tableName());
1445         $p->get($this->pid());
1446         
1447         $_SESSION[get_class($this)][$sesPrefix .'-auth'] = serialize((object)$p->toArray());
1448         
1449         return true;
1450     }
1451     
1452     function beforeUpdate($old, $q, $roo)
1453     {
1454         $this->email = trim($this->email);
1455     }
1456     
1457     function generateOathKey()
1458     {
1459         require 'Base32.php';
1460         
1461         $base32 = new Base32();
1462         
1463         return $base32->base32_encode(bin2hex(openssl_random_pseudo_bytes(10)));
1464     }
1465     
1466     function generateQRCode($hash)
1467     {
1468         if(
1469             empty($this->email) &&
1470             empty($hash)
1471         ){
1472             return false;
1473         }
1474         
1475         $issuer = (empty($this->name)) ? 
1476             rawurlencode('ROOJS') : rawurlencode($this->name);
1477         
1478         $uri = "otpauth://totp/{$issuer}:{$this->email}?secret={$hash}&issuer={$issuer}&algorithm=SHA1&digits=6&period=30";
1479         
1480         require_once 'Image/QRCode.php';
1481         
1482         $qrcode = new Image_QRCode();
1483         
1484         $image = $qrcode->makeCode($uri, array(
1485             'output_type' => 'return'
1486         ));
1487         
1488         ob_start();
1489         imagepng($image);
1490         $base64 = base64_encode(ob_get_contents());
1491         ob_end_clean();
1492         
1493         return "data:image/png;base64,{$base64}";
1494     }
1495     
1496     static function test_ADMIN_PASSWORD_RESET($pg, $to)
1497     {
1498         $ff = HTML_FlexyFramework::get();
1499         $person = DB_DataObject::Factory('core_person');
1500         $person->id = -1;
1501         
1502         return array(
1503             'HTTP_HOST' => $_SERVER['SERVER_NAME'],
1504             'person' => $person,
1505             'authFrom' => 'FAKE_LINK',
1506             'authKey' => 'FAKE_KEY',
1507
1508             'rcpts' => $to->email,
1509         );
1510         
1511         return $content;
1512     }
1513     
1514     
1515  }