DataObjects/Core_person_signup.php
[Pman.Core] / DataObjects / Person.php
index 6ed7815..8d3a809 100644 (file)
@@ -4,6 +4,7 @@
  */
 require_once 'DB/DataObject.php';
 
+
 class Pman_Core_DataObjects_Person extends DB_DataObject 
 {
     ###START_AUTOCODE
@@ -41,9 +42,11 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     public $url_blog; // VARCHAR(256) NULL ;
     public $url_twitter; // VARCHAR(256) NULL ;
     public $url_linkedin; // VARCHAR(256) NULL ;
+    public $linkedin_id; // VARCHAR(256) NULL ;
     
     public $phone_mobile; // varchar(32)  NOT NULL  DEFAULT '';
     public $phone_direct; // varchar(32)  NOT NULL  DEFAULT '';
+    public $countries; // VARCHAR(128) NULL;
     
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
@@ -216,18 +219,30 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     
     function getEmailFrom()
     {
+        if (empty($this->name)) {
+            return $this->email;
+        }
         return '"' . addslashes($this->name) . '" <' . $this->email . '>';
     }
+    
     function toEventString() 
     {
         return empty($this->name) ? $this->email : $this->name;
     } 
+    
     function verifyAuth()
     { 
         $ff= HTML_FlexyFramework::get();
         if (!empty($ff->Pman['auth_comptype']) &&
             (!$this->company_id || ($ff->Pman['auth_comptype'] != $this->company()->comptype))
            ){
+            
+            // force a logout - without a check on the isAuth - as this is called from there..
+            $db = $this->getDatabaseConnection();
+            $sesPrefix = $ff->appNameShort .'-'.get_class($this) .'-'.$db->dsn['database'] ;
+            $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
+            return false;
+            
             $ff->page->jerr("Login not permited to outside companies");
         }
         return true;
@@ -242,7 +257,8 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         // we combine db + project names,
         // otherwise if projects use different 'auth' objects
         // then we get unserialize issues.
-        $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
+        $ff= HTML_FlexyFramework::get();
+        $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
         
         
         @session_start();
@@ -254,25 +270,50 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             $u = DB_DataObject::factory('Person');
             if ($u->get($a->id)) { //&& strlen($u->passwd)) {
               
-                $u->verifyAuth();
+                return $u->verifyAuth();
                 
-  die("verify");
+   
                 return true;
             }
             
-            $_SESSION[__CLASS__][$sesPrefix .'-auth'] = '';
+            unset($_SESSION[__CLASS__][$sesPrefix .'-auth']);
             
         }
         // local auth - 
+        $default_admin = false;
+        if (!empty($ff->Pman['local_autoauth']) && 
+            ($ff->Pman['local_autoauth'] === true) &&
+            (!empty($_SERVER['SERVER_ADDR'])) &&
+            ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
+            ($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
+        ) {
+            $group = DB_DataObject::factory('Groups');
+            $group->get('name', 'Administrators');
+            
+            $member = DB_DataObject::factory('group_members');
+            $member->autoJoin();
+            $member->group_id = $group->id;
+            $member->whereAdd("
+                join_user_id_id.id IS NOT NULL
+            ");
+            if($member->find(true)){
+                $default_admin = DB_DataObject::factory('Person');
+                if(!$default_admin->get($member->user_id)){
+                    $default_admin = false;
+                }
+            }
+        }
+        
+        //var_dump($ff->Pman['local_autoauth']);         var_dump($_SERVER); exit;
         $u = DB_DataObject::factory('Person');
         $ff = HTML_FlexyFramework::get();
         if (!empty($ff->Pman['local_autoauth']) && 
             (!empty($_SERVER['SERVER_ADDR'])) &&
             ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
             ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') &&
-            $u->get('email', $ff->Pman['local_autoauth'])
+            ($default_admin ||  $u->get('email', $ff->Pman['local_autoauth']))
         ) {
-            $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
+            $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($default_admin ? $default_admin : $u);
             return true;
         }
            
@@ -290,11 +331,13 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
             return true; 
         }
+        //var_dump(session_id());
+        //var_dump($_SESSION[__CLASS__]);
         
-        if (!empty(   $_SESSION[__CLASS__][$sesPrefix .'-empty'] )) {
-            return false;
-        }
-        
+        //if (!empty(   $_SESSION[__CLASS__][$sesPrefix .'-empty'] )) {
+        //    return false;
+        //}
+        //die("got this far?");
         // not in session or not matched...
         $u = DB_DataObject::factory('Person');
         $u->whereAdd(' LENGTH(passwd) > 0');
@@ -317,8 +360,10 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             return false;
         }
         $db = $this->getDatabaseConnection();
-        $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
         
+        $ff= HTML_FlexyFramework::get();
+        $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
+
         
         
         if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
@@ -328,17 +373,17 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             if ($u->get($a->id)) { /// && strlen($u->passwd)) {
                 return clone($u);
             }
-             
+            unset($_SESSION[__CLASS__][$sesPrefix .'-auth']);
         }
         
         if (empty(   $_SESSION[__CLASS__][$sesPrefix .'-empty'] )) {
             $u = DB_DataObject::factory('Person');
             $u->whereAdd(' LENGTH(passwd) > 0');
-             $_SESSION[__CLASS__][$sesPrefix .'-empty']  = $u->count();
+            $_SESSION[__CLASS__][$sesPrefix .'-empty']  = $u->count();
         }
                 
              
-        if (empty(   $_SESSION[__CLASS__][$sesPrefix .'-empty'] )) {
+        if (isset(   $_SESSION[__CLASS__][$sesPrefix .'-empty'] ) && $_SESSION[__CLASS__][$sesPrefix .'-empty']  < 1) {
             
             // fake person - open system..
             //$ce = DB_DataObject::factory('core_enum');
@@ -347,6 +392,14 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             
             $u = DB_DataObject::factory('Person');
             $u->id = -1;
+            
+            // if a company has been created fill that in in company_id_id
+            $c = DB_DAtaObject::factory('Companies')->lookupOwner();
+            if ($c) {
+                $u->company_id_id = $c->pid();
+                $u->company_id = $c->pid();
+            }
+            
             return $u;
             
         }
@@ -355,7 +408,9 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     function login()
     {
         $this->isAuth(); // force session start..
-        $this->verifyAuth();
+        if (!$this->verifyAuth()) {
+            return false;
+        }
         $db = $this->getDatabaseConnection();
         
         
@@ -374,17 +429,24 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             $gr = DB_DataObject::Factory('group_rights');
             $gr->applyDefs($g, 0);
         }
-             
-        $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
+        $ff= HTML_FlexyFramework::get();
+        $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
+
+
         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
         
     }
     function logout()
     {
         $this->isAuth(); // force session start..
-         $db = $this->getDatabaseConnection();
-        $sesPrefix = get_class($this) .'-'.$db->dsn['database'] ;
+        $db = $this->getDatabaseConnection();
+        $ff= HTML_FlexyFramework::get();
+        $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
+
         $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
+       
+        
+       
         
     }    
     function genPassKey ($t) 
@@ -449,15 +511,19 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         $this->whereAdd('LENGTH(passwd) > 1'); 
         $this->email = $n;
     }
-    function lang($val)
+    function lang()
     {
+        if (!func_num_args()) {
+            return $this->lang;
+        }
+        $val = array_shift(func_get_args());
         if ($val == $this->lang) {
             return;
         }
         $uu = clone($this);
         $this->lang = $val;
         $this->update($uu);
-
+        return $this->lang;
     }
             
     
@@ -600,11 +666,15 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     function applyFilters($q, $au, $roo)
     {
         //DB_DataObject::DebugLevel(1);
+        
+        if (!empty($q['query']['is_owner'])) {
+            $this->whereAdd(" join_company_id_id.comptype = 'OWNER'");
+        }
+        
         if (!empty($q['query']['person_not_internal'])) {
             $this->whereAdd(" join_company_id_id.isOwner = 0 ");
         }
         
-        
         if (!empty($q['query']['person_internal_only_all'])) {
             
             
@@ -676,6 +746,13 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             
         }
         
+        // #2307 Search Country!!
+        if (!empty($q['query']['in_country'])) {
+            // DB_DataObject::debugLevel(1);
+            $inc = $q['query']['in_country'];
+            $this->whereAdd("$tn_p.countries LIKE '%{$inc}%'");
+        }
+        
         if (!empty($q['query']['not_in_directory'])) { 
             // it's a Person list..
             // DB_DATaobjecT::debugLevel(1);
@@ -733,6 +810,12 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             
         }
         
+        if(!empty($q['query']['name'])){
+            $this->whereAdd("
+                Person.name LIKE '%{$this->escape($q['query']['name'])}%'
+            ");
+        }
+        
         if (!empty($q['query']['search'])) {
             
             // use our magic search builder...
@@ -771,7 +854,6 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
                     ");*/
         }
         
-        //
     }
     function setFromRoo($ar, $roo)
     {
@@ -879,7 +961,8 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         }
         return false;
     }
-    function onInsert($req, $roo)  
+    
+    function onInsert($req, $roo)
     {
          
         $p = DB_DataObject::factory('person');
@@ -913,8 +996,12 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         
     }
     
-    function importFromArray($roo, $persons, $prefix)
+    function importFromArray($roo, $persons, $opts)
     {
+        if (empty($opts['prefix'])) {
+            $roo->jerr("opts[prefix] is empty - you can not just create passwords based on the user names");
+        }
+        
         if (!is_array($persons) || empty($persons)) {
             $roo->jerr("error in the person data. - empty on not valid");
         }
@@ -934,7 +1021,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             $p->company_id = $companies->pid();
             // strip the 'spaces etc.. make lowercase..
             $name = strtolower(str_replace(' ', '', $person['name']));
-            $p->setPassword("$prefix$name");
+            $p->setPassword("{$opts['prefix']}{$name}");
             $p->insert();
             // set up groups
             // if $person->groups is set.. then
@@ -951,4 +1038,31 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             $p->onInsert(array(), $roo);
         }
     }
+    
+    function getEmailName()
+    {
+        $name = array();
+        
+        if(!empty($this->honor)){
+            array_push($name, $this->honor);
+        }
+        
+        if(!empty($this->name)){
+            array_push($name, $this->name);
+            
+            return implode(' ', $name);
+        }
+        
+        if(!empty($this->firstname) || !empty($this->lastname)){
+            array_push($name, $this->firstname);
+            array_push($name, $this->lastname);
+            
+            $name = array_filter($name);
+            
+            return $name;
+        }
+        
+        return $this->email;
+    }
+    
  }