CVS update: /cowiki/includes/cowiki/class/auth/

[email protected] 6 May 2005 02:01:21 -0000
Newsgroups gmane.comp.php.cowiki.cvs
Message-ID <[email protected]>
User: dgorski 
Date: 2005/05/05 19:01:21

Modified:
   cowiki/includes/cowiki/class/auth/class.AuthMySQL.php

Log:
 Simplified

File Changes:

Directory: /cowiki/includes/cowiki/class/auth/
==============================================

File [changed]: class.AuthMySQL.php
Url: http://cowiki.tigris.org/source/browse/cowiki/includes/cowiki/class/auth/class.AuthMySQL.php?r1=1.13&r2=1.14
Delta lines:  +27 -49
---------------------
--- class.AuthMySQL.php	12 Apr 2005 19:09:29 -0000	1.13
+++ class.AuthMySQL.php	6 May 2005 02:01:19 -0000	1.14
@@ -2,7 +2,7 @@
 
 /**
  *
- * $Id: class.AuthMySQL.php,v 1.13 2005/04/12 19:09:29 dgorski Exp $
+ * $Id: class.AuthMySQL.php,v 1.14 2005/05/06 02:01:19 dgorski Exp $
  *
  * This file is part of coWiki. coWiki is free software under the terms of
  * the GNU General Public License (GPL). Read the LICENSE file. If you did
@@ -17,7 +17,7 @@
  * @author      Daniel T. Gorski, <[email protected]>
  * @copyright   (C) Daniel T. Gorski, {@link http://www.develnet.org}
  * @license     http://www.gnu.org/licenses/gpl.html
- * @version     $Revision: 1.13 $
+ * @version     $Revision: 1.14 $
  *
  */
 
@@ -33,13 +33,10 @@
  *
  * @todo        [D11N]  Complete documentation
  */
-class AuthMySQL extends Object
-                implements AuthCallback {
+class AuthMySQL extends Object implements AuthCallback {
 
     private
-        $Context = null,
-        $rLink = null,
-        $bConnected = false;
+        $Storage = null;
 
     // --------------------------------------------------------------------
 
@@ -56,34 +53,14 @@
      */
     public function init() {
 
-        $this->Context = RuntimeContext::getInstance();
-        $Registry = $this->Context->getRegistry();
-        $UriInfo = new UriInfo($Registry->get('.AUTH_RESOURCE'));
+        $Registry = Registry::getInstance();
 
-        if ($UriInfo->get('scheme') == 'mysql') {
+        try {
+            $this->Storage = StorageFactory::getInstance()
+                                ->create($Registry->get('.AUTH_RESOURCE'));
         
-            if (!function_exists('mysql_connect')) {
-                $this->Context->addError(540, 'MySQL extension not available');
-                $this->Context->terminate();
-            }
-  
-            $sPort = '';
-            if ($UriInfo->get('port')) {
-                $sPort = ':' . $sPort;
-            }
-
-            $this->rLink = @mysql_connect(
-                $UriInfo->get('host') . $sPort,
-                $UriInfo->get('user'),
-                $UriInfo->get('pass')
-            );
-
-            $this->bConnected = $this->rLink
-                && @mysql_select_db($UriInfo->get('basepath'), $this->rLink);
-        }
-
-        if (!$this->bConnected) {
-            $this->Context->addError(553);
+        } catch (StorageException $se) {
+            RuntimeContext::getInstance()->addError(553);
         }
     }
 
@@ -105,33 +82,36 @@
     public function validate($sLogin, $sPasswd) {
 
         // No connection, no result
-        if (!$this->bConnected) {
+        if (!is_object($this->Storage)) {
             return false;
         }
 
         // ----------------------------------------------------------------
 
-        $Registry = $this->Context->getRegistry();
+        $Context = RuntimeContext::getInstance();
+        $Registry = $Context->getRegistry();
 
-        // AuthMySQL handler requires .AUTH_QUERY
+        // This handler requires .AUTH_QUERY
         if (!$Registry->has('.AUTH_QUERY')) {
-            $this->Context->addError(516, '.AUTH_QUERY');
+            $Context->addError(516, '.AUTH_QUERY');
             return false;
         }
 
-        // AuthMySQL handler requires .AUTH_ENCRYPTION
+        // This handler requires .AUTH_ENCRYPTION
         if (!$Registry->has('.AUTH_ENCRYPTION')) {
-            $this->Context->addError(516, '.AUTH_ENCRYPTION');
+            $Context->addError(516, '.AUTH_ENCRYPTION');
             return false;
         }
 
         // Check if .AUTH_QUERY starts with 'SELECT '
         $sStr = strtolower(substr(trim($Registry->get('.AUTH_QUERY')),0,7));
         if ($sStr != 'select ') {
-            $this->Context->addError(560, $Registry->get('.AUTH_QUERY'));
+            $Context->addError(560, $Registry->get('.AUTH_QUERY'));
             return false;
         }
 
+        // ---
+
         // Remove possible colons
         $sQuery = str_replace(';', '', $Registry->get('.AUTH_QUERY'));
 
@@ -139,16 +119,17 @@
         $sQuery = str_replace('{%LOGIN%}', addslashes($sLogin), $sQuery);
 
         // Get user password
-        $rResult = @mysql_query($sQuery, $this->rLink);
-        if (!is_resource($rResult)) {
-            $this->Context->addError(560, $Registry->get('.AUTH_QUERY'));
+        try {
+            $rResult = $this->Storage->query($sQuery);
+
+        } catch (StorageException $se) {
+            $Context->addError(560, $Registry->get('.AUTH_QUERY'));
             return false; 
         }
 
         // ----------------------------------------------------------------
 
-        $aData = @mysql_fetch_assoc($rResult);
-        @mysql_free_result($rResult);
+        $aData = $this->Storage->fetchArrayAndFreeResult($rResult);
 
         if ($aData) {
             $sDbPass = array_pop($aData);
@@ -167,9 +148,6 @@
                 case '':
                     return $sDbPass == $sPasswd;
                     break;
-
-                default:
-                    return false;
             }
         }