DataObjects/I18n.php
[Pman.Core] / DataObjects / Person.php
index a1f2544..a595cf8 100644 (file)
@@ -42,6 +42,7 @@ 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 '';
@@ -223,10 +224,12 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         }
         return '"' . addslashes($this->name) . '" <' . $this->email . '>';
     }
+    
     function toEventString() 
     {
         return empty($this->name) ? $this->email : $this->name;
     } 
+    
     function verifyAuth()
     { 
         $ff= HTML_FlexyFramework::get();
@@ -237,7 +240,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             // 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'] = "";
+            $_SESSION[get_class($this)][$sesPrefix .'-auth'] = "";
             return false;
             
             $ff->page->jerr("Login not permited to outside companies");
@@ -250,6 +253,10 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     //   ---------------- authentication / passwords and keys stuff  ----------------
     function isAuth()
     {
+        
+        @session_start();
+       
+        
         $db = $this->getDatabaseConnection();
         // we combine db + project names,
         // otherwise if projects use different 'auth' objects
@@ -258,31 +265,35 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
         
         
-        @session_start();
          
-        if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
+        if (!empty($_SESSION[get_class($this)][$sesPrefix .'-auth'])) {
             // in session...
-            $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
+            $a = unserialize($_SESSION[get_class($this)][$sesPrefix .'-auth']);
             
-            $u = DB_DataObject::factory('Person');
-            if ($u->get($a->id)) { //&& strlen($u->passwd)) {
+            
+            $u = DB_DataObject::factory($this->tableName());
+            if ($a->id && $u->get($a->id)) { //&& strlen($u->passwd)) {
               
                 return $u->verifyAuth();
                 
-   
-                return true;
+    
             }
             
-            unset($_SESSION[__CLASS__][$sesPrefix .'-auth']);
+            unset($_SESSION[get_class($this)][$sesPrefix .'-auth']);
             
         }
+        if (!$this->canInitializeSystem()) {
+            return false;
+        }
+        
+        
         // 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')
+            ($_SERVER['REMOTE_ADDR'] == '127.0.0.1')  
         ) {
             $group = DB_DataObject::factory('Groups');
             $group->get('name', 'Administrators');
@@ -301,16 +312,16 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             }
         }
         
-         
+        //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') &&
+            ($_SERVER['REMOTE_ADDR'] == '127.0.0.1')  &&
             ($default_admin ||  $u->get('email', $ff->Pman['local_autoauth']))
         ) {
-            $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($default_admin ? $default_admin : $u);
+            $_SESSION[get_class($this)][$sesPrefix .'-auth'] = serialize($default_admin ? $default_admin : $u);
             return true;
         }
            
@@ -325,7 +336,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
             &&
             $u->checkPassword($_SERVER['PHP_AUTH_PW'])
            ) {
-            $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($u);
+            $_SESSION[get_class($this)][$sesPrefix .'-auth'] = serialize($u);
             return true; 
         }
         //var_dump(session_id());
@@ -339,7 +350,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         $u = DB_DataObject::factory('Person');
         $u->whereAdd(' LENGTH(passwd) > 0');
         $n = $u->count();
-        $_SESSION[__CLASS__][$sesPrefix .'-empty']  = $n;
+        $_SESSION[get_class($this)][$sesPrefix .'-empty']  = $n;
         $error =  PEAR::getStaticProperty('DB_DataObject','lastError');
         if ($error) {
             die($error->toString()); // not really a good thing to do...
@@ -351,6 +362,12 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         return false;
         
     }
+    
+    function canInitializeSystem()
+    {
+        return !strcasecmp(get_class($this) , __CLASS__);
+    }
+    
     function getAuthUser()
     {
         if (!$this->isAuth()) {
@@ -360,27 +377,38 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         
         $ff= HTML_FlexyFramework::get();
         $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
-
         
         
-        if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
-            $a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
+        //var_dump(array(get_class($this),$sesPrefix .'-auth'));
+       
+        if (!empty($_SESSION[get_class($this)][$sesPrefix .'-auth'])) {
+            $a = unserialize($_SESSION[get_class($this)][$sesPrefix .'-auth']);
             
-            $u = DB_DataObject::factory('Person');
-            if ($u->get($a->id)) { /// && strlen($u->passwd)) {
+            
+            $u = DB_DataObject::factory($this->tableName()); // allow extending this ...
+            $u->autoJoin();
+            if ($u->get($a->id)) { /// && strlen($u->passwd)) {  // should work out the pid .. really..
                 return clone($u);
             }
-            unset($_SESSION[__CLASS__][$sesPrefix .'-auth']);
+            unset($_SESSION[get_class($this)][$sesPrefix .'-auth']);
+        }
+        
+        
+        
+        if (!$this->canInitializeSystem()) {
+            return false;
         }
         
-        if (empty(   $_SESSION[__CLASS__][$sesPrefix .'-empty'] )) {
+        
+        
+        if (empty(   $_SESSION[get_class($this)][$sesPrefix .'-empty'] )) {
             $u = DB_DataObject::factory('Person');
             $u->whereAdd(' LENGTH(passwd) > 0');
-            $_SESSION[__CLASS__][$sesPrefix .'-empty']  = $u->count();
+            $_SESSION[get_class($this)][$sesPrefix .'-empty']  = $u->count();
         }
                 
              
-        if (isset(   $_SESSION[__CLASS__][$sesPrefix .'-empty'] ) && $_SESSION[__CLASS__][$sesPrefix .'-empty']  < 1) {
+        if (isset(   $_SESSION[get_class($this)][$sesPrefix .'-empty'] ) && $_SESSION[get_class($this)][$sesPrefix .'-empty']  < 1) {
             
             // fake person - open system..
             //$ce = DB_DataObject::factory('core_enum');
@@ -405,7 +433,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     function login()
     {
         $this->isAuth(); // force session start..
-        if (!$this->verifyAuth()) {
+        if (!$this->verifyAuth()) { // check for company valid..
             return false;
         }
         $db = $this->getDatabaseConnection();
@@ -429,8 +457,16 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         $ff= HTML_FlexyFramework::get();
         $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
 
-
-        $_SESSION[__CLASS__][$sesPrefix .'-auth'] = serialize($this);
+        
+        // we should not store the whole data in the session - otherwise it get's huge.
+        $p = DB_DAtaObject::Factory($this->tableName());
+        $p->get($this->pid());
+        
+        //var_dump(array(get_class($this),$sesPrefix .'-auth'));
+        $_SESSION[get_class($this)][$sesPrefix .'-auth'] = serialize((object)$p->toArray());
+        // ensure it's written so that ajax calls can fetch it..
+        
+        
         
     }
     function logout()
@@ -439,12 +475,11 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         $db = $this->getDatabaseConnection();
         $ff= HTML_FlexyFramework::get();
         $sesPrefix = $ff->appNameShort .'-' .get_class($this) .'-'.$db->dsn['database'] ;
-
-        $_SESSION[__CLASS__][$sesPrefix .'-auth'] = "";
-       
         
+         $_SESSION[get_class($this)][$sesPrefix .'-auth'] = "";
        
         
+        
     }    
     function genPassKey ($t) 
     {
@@ -460,11 +495,13 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
     {
         
         if (substr($this->passwd,0,1) == '$') {
+            
             return crypt($val,$this->passwd) == $this->passwd ;
         }
         // old style md5 passwords...- cant be used with courier....
         return md5($val) == $this->passwd;
     }
+    
     function setPassword($value) 
     {
         $salt='';
@@ -663,6 +700,11 @@ 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 ");
         }
@@ -742,27 +784,7 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         if (!empty($q['query']['in_country'])) {
             // DB_DataObject::debugLevel(1);
             $inc = $q['query']['in_country'];
-            
             $this->whereAdd("$tn_p.countries LIKE '%{$inc}%'");
-            
-//            if ($q['query']['in_group'] == -1) {
-//             
-//                // list all staff who are not in a group.
-//                $this->whereAdd("Person.id NOT IN (
-//                    SELECT distinct(user_id) FROM $tn_gm LEFT JOIN
-//                        $tn_g ON $tn_g.id = $tn_gm.group_id
-//                        WHERE $tn_g.type = ".$q['query']['type']."
-//                    )");
-//                
-//                
-//            } else {
-//                
-//                $this->whereAdd("$tn_p.id IN (
-//                    SELECT distinct(user_id) FROM $tn_gm
-//                        WHERE group_id = $ing
-//                    )");
-//               }
-            
         }
         
         if (!empty($q['query']['not_in_directory'])) { 
@@ -866,8 +888,6 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
                     ");*/
         }
         
-        
-        //
     }
     function setFromRoo($ar, $roo)
     {
@@ -975,7 +995,24 @@ class Pman_Core_DataObjects_Person extends DB_DataObject
         }
         return false;
     }
-    function onInsert($req, $roo)  
+    
+    function beforeInsert($req, $roo)
+    {
+        $p = DB_DataObject::factory('person');
+        if ($roo->authUser->id > -1 ||  $p->count() > 1) {
+            return;
+        }
+        $c = DB_DAtaObject::Factory('Companies');
+        $tc =$c->count();
+        if (!$tc || $tc> 1) {
+            $roo->jerr("can not create initial user as multiple companies already exist");
+        }
+        $c->find(true);
+        $this->company_id = $c->id;
+        
+    }
+    
+    function onInsert($req, $roo)
     {
          
         $p = DB_DataObject::factory('person');
@@ -1051,4 +1088,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;
+    }
+    
  }