svn: /web/doc-editor/trunk/php/ AccountManager.php ExtJsController.php SvnClient.php conf.inc.php

[email protected] (Yannick Torres)
Newsgroups php.doc.web
Message-ID <[email protected]>
yannick                                  Tue, 29 Dec 2009 00:28:03 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=292728

Log:
Authenticate via master.php.net if the online editor is installed into php.net server ; otherwise, the authentication is from Svn server. We check now also the karma of this user and trow an error if he haven't the right karma according to the language chosen

Changed paths:
    U   web/doc-editor/trunk/php/AccountManager.php
    U   web/doc-editor/trunk/php/ExtJsController.php
    U   web/doc-editor/trunk/php/SvnClient.php
    U   web/doc-editor/trunk/php/conf.inc.php

Modified: web/doc-editor/trunk/php/AccountManager.php
===================================================================
--- web/doc-editor/trunk/php/AccountManager.php	2009-12-28 20:36:36 UTC (rev 292727)
+++ web/doc-editor/trunk/php/AccountManager.php	2009-12-29 00:28:03 UTC (rev 292728)
@@ -121,12 +121,31 @@

         } // End anonymous's login
         else {
-           // We try to authenticate this user to VCS server.
-           $AuthReturn = VCSFactory::getInstance()->authenticate($vcsLogin, $vcsPasswd);
+
+           // If this app is installed into Php's server, we use the standad way to verify login/password
+           if( $_SERVER["SERVER_NAME"] == "doc.php.net" ) {
+               // We try to authenticate this user to master php server.
+               $AuthReturn = VCSFactory::getInstance()->masterPhpAuthenticate($vcsLogin, $vcsPasswd);
+               $return['authMethod'] = 'masterPhp';
+           } else {
+               // We try to authenticate this user to VCS server.
+               $AuthReturn = VCSFactory::getInstance()->svnAuthenticate($vcsLogin, $vcsPasswd);
+               $return['authMethod'] = 'svnServer';
+           }
         }

         if( $AuthReturn === true ) {

+           // Check the karma
+           $karma = VCSFactory::getInstance()->checkKarma($vcsLogin, $lang);
+
+           if( $karma !== true ) {
+
+               $return['state'] = false;
+               $return['msg']   = $karma;
+
+           }
+
            $this->vcsLogin  = $vcsLogin;
            $this->vcsPasswd = $vcsPasswd;
            $this->vcsLang   = $lang;

Modified: web/doc-editor/trunk/php/ExtJsController.php
===================================================================
--- web/doc-editor/trunk/php/ExtJsController.php	2009-12-28 20:36:36 UTC (rev 292727)
+++ web/doc-editor/trunk/php/ExtJsController.php	2009-12-29 00:28:03 UTC (rev 292728)
@@ -82,7 +82,10 @@
             return JsonResponseBuilder::success();
         } elseif ($response['state'] === false) {
             // This user is unknow from this server
-            return JsonResponseBuilder::failure(array('msg' => $response['msg']));
+            return JsonResponseBuilder::failure(array(
+                                                 'msg'        => $response['msg'],
+                                                 'authMethod' => $response['authMethod']
+                                               ));
         } else {
             return JsonResponseBuilder::failure();
         }

Modified: web/doc-editor/trunk/php/SvnClient.php
===================================================================
--- web/doc-editor/trunk/php/SvnClient.php	2009-12-28 20:36:36 UTC (rev 292727)
+++ web/doc-editor/trunk/php/SvnClient.php	2009-12-29 00:28:03 UTC (rev 292728)
@@ -19,6 +19,116 @@
     {
     }

+    private function getKarmaList() {
+
+        // If this data is older than 1 day, we update it
+        $data = RepositoryFetcher::getStaticValue('karma_list', '');
+
+        if( $data === false || ($data["update_time"] + (24*60*60)) > time() ) {
+            $this->updateKarmaList();
+            $data = RepositoryFetcher::getStaticValue('karma_list', '');
+        }
+        return $data["data"];
+    }
+
+    private function updateKarmaList() {
+
+        $file = @file(DOC_EDITOR_VCS_KARMA_FILE);
+
+        $line_avail = array();
+        $user = array();
+
+        // We cleanUp the content of this file
+        for( $i=0; $i < count($file); $i++) {
+            if( substr($file[$i], 0, 6) == 'avail|') {
+                $line_avail[] = $file[$i];
+
+                $t = explode("|", $file[$i]);
+                $users   = trim($t[1]);
+                $karmas = ( isset($t[2]) ) ? trim($t[2]) : 'ALL';
+
+                $users = explode(",", $users);
+                $karmas = explode(",", $karmas);
+
+                for( $j=0; $j < count($users); $j++ ) {
+                    if( isset($user[$users[$j]]) ) {
+
+                        $user[$users[$j]]['karma'] = array_merge( $karmas, $user[$users[$j]]['karma'] );
+
+                    } else {
+
+                        $user[$users[$j]]['karma'] = $karmas;
+                    }
+                }
+
+            }
+        }
+
+        // We store this value into DB as json string to retrieve it later
+        $to_store = array(
+            "update_time" => time(),
+            "data"        => $user
+        );
+        RepositoryManager::setStaticValue('karma_list', '', json_encode($to_store));
+
+    }
+
+    // Return true if the $user have the right karma for $module
+    public function checkKarma($user, $lang)
+    {
+
+        $userList = $this->getKarmaList();
+
+        if( isset($userList[$user]) ) {
+
+            $karma = $userList[$user]['karma'];
+
+            // Must have ALL, phpdoc or phpdoc/$lang
+            if( in_array("ALL", $karma) || in_array("phpdoc", $karma) || in_array("phpdoc/$lang", $karma) ) {
+                return true;
+            } else {
+                return 'You haven\'t good Karma for the chosen language. Your Current karma is : '.implode(", ", $karma);
+            }
+
+        }
+        return 'You haven\'t any karma !';
+
+    }
+
+    public function masterPhpAuthenticate($username, $password)
+    {
+
+        $post = http_build_query(
+                array(
+                        "token"    => getenv("TOKEN"),
+                        "username" => $username,
+                        "password" => $password,
+                )
+        );
+
+        $opts = array(
+                "method"  => "POST",
+                "header"  => "Content-type: application/x-www-form-urlencoded",
+                "content" => $post,
+        );
+
+        $ctx = stream_context_create(array("http" => $opts));
+
+        $s = file_get_contents("https://master.php.net/fetch/cvsauth.php", false, $ctx);
+
+        $a = @unserialize($s);
+        if (!is_array($a)) {
+                return 'svn.php.net seems to be down !';
+        }
+        if (isset($a["errno"])) {
+                if( $a["errno"] == 0 ) { return 'svn login failed'; }
+                if( $a["errno"] == 1 ) { return 'Bad login'; }
+                if( $a["errno"] == 2 ) { return 'Bad password'; }
+        }
+
+        return true;
+    }
+
     /**
      * Test the SVN credentials against the server
      * ref - http://www.php.net/manual/en/features.http-auth.php
@@ -27,7 +137,7 @@
      * @param $password svn login password.
      * @return TRUE if the loggin success, error message otherwise.
      */
-    public function authenticate($username, $password)
+    public function svnAuthenticate($username, $password)
     {
         $uuid = md5(uniqid(rand(), true));
         $uuid =   substr($uuid, 0, 8)  . '-'

Modified: web/doc-editor/trunk/php/conf.inc.php
===================================================================
--- web/doc-editor/trunk/php/conf.inc.php	2009-12-28 20:36:36 UTC (rev 292727)
+++ web/doc-editor/trunk/php/conf.inc.php	2009-12-29 00:28:03 UTC (rev 292728)
@@ -63,6 +63,10 @@
  * VCS module
  */
 define('DOC_EDITOR_VCS_MODULE', 'phpdoc-all');
+/**
+ * Karma File
+ */
+define('DOC_EDITOR_VCS_KARMA_FILE', 'http://svn.php.net/viewvc/SVNROOT/global_avail?view=co');


 /**
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.