sync
[Pman.Core] / DataObjects / Core_person.php
index 253f0ff..b80ef81 100644 (file)
@@ -222,6 +222,7 @@ class Pman_Core_DataObjects_Core_person extends DB_DataObject
         if (empty($this->name)) {
             return $this->email;
         }
+        
         return '"' . addslashes($this->name) . '" <' . $this->email . '>';
     }
     
@@ -352,7 +353,10 @@ class Pman_Core_DataObjects_Core_person extends DB_DataObject
             ) &&
             ($default_admin ||  $u->get('email', $ff->Pman['local_autoauth']))
         ) {
-            $_SESSION[get_class($this)][$sesPrefix .'-auth'] = serialize($default_admin ? $default_admin : $u);
+            
+            $user = $default_admin ? $default_admin->toArray() : $u->toArray();
+            
+            $_SESSION[get_class($this)][$sesPrefix .'-auth'] = serialize((object) $user);
             return true;
         }
         
@@ -519,19 +523,26 @@ class Pman_Core_DataObjects_Core_person extends DB_DataObject
         
         return md5(implode(',' ,  array($month, $this->email , $this->passwd, $this->id)));
     } 
-    function checkPassword($val)
+    
+    function checkTwoFactorAuthentication($val)
     {
-        if(!empty($this->oath_key)){
-            $ret =  $this->checkTwoFactorAuthentication($val);
-            
-            if(!$ret){
-                print_R('error');
-                exit;
-            }
-            
-            print_r('true');exit;
+        require_once 'System.php';
+        
+        $oathtool = System::which('oathtool');
+        
+        if (!$oathtool) {
+            return false;
         }
         
+        $cmd = "{$oathtool} --totp --base32 {$this->oath_key}";
+        
+        $password = exec($cmd);
+        
+        return ($password == $val) ? true : false;
+    }
+    
+    function checkPassword($val)
+    {
         if (substr($this->passwd,0,1) == '$') {
             if (function_exists('pasword_verify')) {
                 return password_verify($val, $this->passwd);
@@ -1018,21 +1029,28 @@ class Pman_Core_DataObjects_Core_person extends DB_DataObject
         }
         
         /*
-         * Seems we never expose oath_key, so...
+         * Seems we never expose oath_key / passwd, so...
          */
-        $this->selectAdd("
-            CASE WHEN core_person.oath_key != '' THEN
-                TRUE
-            ELSE
-                FALSE
-            END AS has_oath_key
-        ");
+        
+        if($this->tableName() == 'core_person'){
+            $this->_extra_cols = array('length_passwd', 'length_oath_key');
+        
+            $this->selectAdd("
+                LENGTH({$this->tableName()}.passwd) AS length_passwd,
+                LENGTH({$this->tableName()}.oath_key) AS length_oath_key
+            ");
+        }
         
     }
     
     function setFromRoo($ar, $roo)
     {
-         $this->setFrom($ar);
+        $this->setFrom($ar);
+        
+        if(!empty($ar['_enable_oath_key'])){
+            $this->generateOathKey();
+        }
+        
         if (!empty($ar['passwd1'])) {
             $this->setPassword($ar['passwd1']);
         }
@@ -1063,10 +1081,6 @@ class Pman_Core_DataObjects_Core_person extends DB_DataObject
             return "Duplicate Email found";
         }
         
-        if(!empty($ar['_enable_oath_key'])){
-            $this->generateOathKey();
-        }
-        
         return true;
     }
     /**
@@ -1164,8 +1178,8 @@ class Pman_Core_DataObjects_Core_person extends DB_DataObject
         if ($roo->authUser->id > -1 ||  $p->count() > 1) {
             return;
         }
-        $c = DB_DAtaObject::Factory('core_company');
-        $tc =$c->count();
+        $c = DB_DataObject::Factory('core_company');
+        $tc = $c->count();
         if (!$tc || $tc> 1) {
             $roo->jerr("can not create initial user as multiple companies already exist");
         }
@@ -1325,9 +1339,11 @@ class Pman_Core_DataObjects_Core_person extends DB_DataObject
         
         if(!empty($q['_to_qr_code'])){
             $qrcode = $this->generateQRCode();
+            
             if(empty($qrcode)){
                 $roo->jerr('Fail to generate QR Code');
             }
+            
             $roo->jdata($qrcode);
         }
     }
@@ -1356,26 +1372,20 @@ class Pman_Core_DataObjects_Core_person extends DB_DataObject
         
         $uri = "otpauth://totp/{$issuer}:{$this->email}?secret={$this->oath_key}&issuer={$issuer}&algorithm=SHA1&digits=6&period=30";
         
-        $base64 = base64_encode(file_get_contents("https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl={$uri}"));
-        
-        return "data:image/png;base64,{$base64}";
-    }
-    
-    function checkTwoFactorAuthentication($val)
-    {
-        require_once 'System.php';
-        
-        $oathtool = System::which('oathtool');
+        require_once 'Image/QRCode.php';
         
-        if (!$oathtool) {
-            return false;
-        }
+        $qrcode = new Image_QRCode();
         
-        $cmd = "{$oathtool} --totp --base32 {$this->oath_key}";
+        $image = $qrcode->makeCode($uri, array(
+            'output_type' => 'return'
+        ));
         
-        $password = exec($cmd);
+        ob_start();
+        imagepng($image);
+        $base64 = base64_encode(ob_get_contents());
+        ob_end_clean();
         
-        return ($password == $val) ? true : false;
+        return "data:image/png;base64,{$base64}";
     }
     
  }