Пример #1
0
 public function auth($username, $password, $db = "admin")
 {
     if ($db === "") {
         if (!$this->_mongoAuth && $this->_mongoDb) {
             $db = $this->_mongoDb;
         } else {
             $db = "admin";
         }
     }
     $server = $this->_mongoHost . ":" . $this->_mongoPort;
     if (!$this->_mongoPort) {
         $server = $this->_mongoHost;
     }
     try {
         $options = $this->_mongoOptions;
         if ($this->_mongoAuth) {
             $options["username"] = $username;
             $options["password"] = $password;
             $options["db"] = $db;
         }
         $this->_mongo = new RMongo($server, $options);
         $this->_mongo->setSlaveOkay(true);
     } catch (Exception $e) {
         if (preg_match("/authenticate/i", $e->getMessage())) {
             return false;
         }
         echo "Unable to connect MongoDB, please check your configurations. MongoDB said:" . $e->getMessage() . ".";
         exit;
     }
     // changing timeout to the new value
     MongoCursor::$timeout = $this->_mongoTimeout;
     //auth by mongo
     if ($this->_mongoAuth) {
         // "authenticate" can only be used between 1.0.1 - 1.2.11
         if (RMongo::compareVersion("1.0.1") >= 0 && RMongo::compareVersion("1.2.11") < 0) {
             $dbs = $db;
             if (!is_array($dbs)) {
                 $dbs = preg_split("/\\s*,\\s*/", $dbs);
             }
             foreach ($dbs as $db) {
                 $ret = $this->_mongo->selectDb($db)->authenticate($username, $password);
                 if (!$ret["ok"]) {
                     return false;
                 }
             }
         }
     } else {
         if ($this->_controlAuth) {
             if (!isset($this->_controlUsers[$username]) || $this->_controlUsers[$username] != $password) {
                 return false;
             }
             //authenticate
             if (!empty($this->_mongoUser)) {
                 // "authenticate" can only be used between 1.0.1 - 1.2.11
                 if (RMongo::compareVersion("1.0.1") >= 0 && RMongo::compareVersion("1.2.11") < 0) {
                     return $this->_mongo->selectDB($db)->authenticate($this->_mongoUser, $this->_mongoPass);
                 }
             }
         } else {
             //authenticate
             if (!empty($this->_mongoUser)) {
                 // "authenticate" can only be used between 1.0.1 - 1.2.11
                 if (RMongo::compareVersion("1.0.1") >= 0 && RMongo::compareVersion("1.2.11") < 0) {
                     return $this->_mongo->selectDB($db)->authenticate($this->_mongoUser, $this->_mongoPass);
                 }
             }
         }
     }
     return true;
 }