Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Fix auth #24

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
65 changes: 65 additions & 0 deletions app/classes/VarEval.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,74 @@ function ISODate (isoDateStr) {
date_default_timezone_set($timezone);
if ($ret["ok"]) {
return $ret["retval"];
} elseif($ret["errmsg"] === "unauthorized") {
return $this->parseBson($this->_source);
}
return false;
}

private function parseBson($source)
{
$pattern = "/([a-z]{1,})\(([^)]+)\)/i";
$matches = null;
preg_match_all($pattern, $source, $matches);

$sourceEscaped = $source;

if(isset($matches[0]))
{
foreach($matches[0] as $matchKey => $objectString)
{
$tmpArray = array(
'ClassName' => $matches[1][$matchKey],
'Params' => $matches[2][$matchKey],
);

$sourceEscaped = str_replace($objectString, json_encode($tmpArray), $sourceEscaped);
}
}
$bson = $this->initBson(json_decode($sourceEscaped, true));
return $bson;
}

private function initBson($source)
{
foreach($source as $key => $val)
{
if(isset($val['ClassName']))
{

$paramWithoutQuotes = substr($val['Params'] , 1, -1 ); // remove quotes

switch($val['ClassName'])
{
case 'ObjectId':
$source[$key] = new MongoId($paramWithoutQuotes);
break;
case 'NumberInt':
case 'NumberLong':
$source[$key] = (int) $val['Params'];
break;
case 'NumberDouble':
$source[$key] = (double) $val['Params'];
break;
case 'NumberFloat':
$source[$key] = (float) $val['Params'];
break;
case 'ISODate':
$dateTime = new DateTime($paramWithoutQuotes);
$source[$key] = new MongoDate($dateTime->getTimestamp(), $dateTime->format("u"));
default:
unset($source['key']);
}
}
elseif( is_array( $val ) || $val instanceof Traversable )
{
$source[$key] = $this->initBson($val);
}
}
return $source;
}
}

?>
6 changes: 3 additions & 3 deletions app/controllers/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class ServerController extends BaseController {
/** server infomation **/
public function doIndex() {
$db = $this->_mongo->selectDB("admin");
$db = $this->_mongo->selectDB( empty($_SESSION['login']['db']) ? 'admin' : $_SESSION['login']['db'] );

//command line
try {
Expand Down Expand Up @@ -267,7 +267,7 @@ public function doReplication() {
foreach ($query as $one) {
foreach ($one as $param=>$value) {
if ($param == "syncedTo") {
$one[$param] = date("Y-m-d H:i:s", $value->inc) . "." . $value->sec;
$one[$param] = date("Y-m-d H:i:s", $value->sec) . "." . $value->inc;
}
}
$this->slaves[] = $one;
Expand All @@ -284,7 +284,7 @@ public function doReplication() {
foreach ($one as $param=>$value) {
if ($param == "syncedTo" || $param == "localLogTs") {
if ($value->inc > 0) {
$one[$param] = date("Y-m-d H:i:s", $value->inc) . "." . $value->sec;
$one[$param] = date("Y-m-d H:i:s", $value->sec) . "." . $value->inc;
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions app/lib/mongo/RMongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ class RMongo {
*/
public function __construct($server, array $options = array()) {
if (class_exists("MongoClient")) {
$this->_mongo = new MongoClient($server, $options);
try {
$this->_mongo = new MongoClient($server, $options);
} catch ( Exception $e ) {
$options['db'] = 'admin';
try {
$this->_mongo = new MongoClient($server, $options);
} catch ( Exception $tmp ) {
throw $e;
}
}
}
else {
$this->_mongo = new Mongo($server, $options);
Expand Down Expand Up @@ -123,7 +132,11 @@ public function lastError() {
* @return array
*/
public function listDBs() {
return $this->_mongo->listDBs();
try {
return $this->_mongo->listDBs();
} catch( Exception $e ) {
return array();
}
}

/**
Expand Down
Empty file modified app/lib/page/lang/ru_ru.php
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion app/models/MDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static function listCollections(MongoDB $db) {

$names = array();
try {
$names = self::exec($db, 'function (){ return db.getCollectionNames(); }');
$names = $db->getCollectionNames();
} catch(Exception $e) {

}
Expand Down
12 changes: 6 additions & 6 deletions app/models/MServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ public function docsNatureOrder() {
return $this->_docsNatureOrder;
}

/**
/**
* Set documents highlight render
*
* @param string $render can be "default" or "plain"
* @since 1.1.6
* @param string $render can be "default" or "plain"
* @since 1.1.6
*/
public function setDocsRender($render) {
$renders = array( "default", "plain" );
Expand Down Expand Up @@ -338,7 +338,7 @@ public function auth($username, $password, $db = "admin") {

//authenticate
if (!empty($this->_mongoUser)) {
// "authenticate" can only be used between 1.0.1 - 1.2.11
// "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)
Expand All @@ -349,7 +349,7 @@ public function auth($username, $password, $db = "admin") {
else {
//authenticate
if (!empty($this->_mongoUser)) {
// "authenticate" can only be used between 1.0.1 - 1.2.11
// "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)
Expand Down Expand Up @@ -381,7 +381,7 @@ public function listDbs() {
} catch (Exception $e) {
$dbs["ok"] = false;
}
if (!$dbs["ok"]) {
if (empty($dbs["ok"])) {
$user = MUser::userInSession();

$dbs = array(
Expand Down