From 30226a8cb54abde5f828e9ae4a1302c550408c63 Mon Sep 17 00:00:00 2001 From: Youssef Garas Date: Thu, 28 Mar 2013 00:37:23 -0400 Subject: [PATCH 1/6] hack for mongo connection --- app/classes/BaseController.php | 4 +++- config.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/classes/BaseController.php b/app/classes/BaseController.php index 6f75fa1..766efc1 100644 --- a/app/classes/BaseController.php +++ b/app/classes/BaseController.php @@ -78,7 +78,9 @@ public function onBefore() { $this->redirect("login.index", array( "host" => $this->_admin->hostIndex() )); } $this->_server = MServer::serverWithIndex($this->_admin->hostIndex()); - $this->_mongo = $this->_server->mongo(); + //$this->_mongo = $this->_server->mongo(); + var_dump($_REQUEST); + $this->_mongo = new Mongo('mongodb://admin:groups4J@localhost/admin'); //log query if (isset($MONGO["features"]["log_query"]) && $MONGO["features"]["log_query"] == "on") { diff --git a/config.php b/config.php index 91bc3ac..72b7039 100644 --- a/config.php +++ b/config.php @@ -25,7 +25,7 @@ //$MONGO["servers"][$i]["mongo_db"] = "MONGO_DATABASE";//default mongo db to connect, works only if mongo_auth=false //$MONGO["servers"][$i]["mongo_user"] = "MONGO_USERNAME";//mongo authentication user name, works only if mongo_auth=false //$MONGO["servers"][$i]["mongo_pass"] = "MONGO_PASSWORD";//mongo authentication password, works only if mongo_auth=false -$MONGO["servers"][$i]["mongo_auth"] = false;//enable mongo authentication? +$MONGO["servers"][$i]["mongo_auth"] = true;//enable mongo authentication? $MONGO["servers"][$i]["control_auth"] = true;//enable control users, works only if mongo_auth=false $MONGO["servers"][$i]["control_users"]["admin"] = "admin";//one of control users ["USERNAME"]=PASSWORD, works only if mongo_auth=false From 8cdb899a819e1d0299d020c898bfa475ac93e443 Mon Sep 17 00:00:00 2001 From: Youssef Garas Date: Thu, 28 Mar 2013 01:39:03 -0400 Subject: [PATCH 2/6] Use built in get collection names in mongo --- app/models/MDb.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/MDb.php b/app/models/MDb.php index 1f21b1c..9856045 100644 --- a/app/models/MDb.php +++ b/app/models/MDb.php @@ -10,6 +10,7 @@ class MDb { * @return array */ static function exec(MongoDB $db, $code, array $params = array()) { + return $db->getCollectionNames(); $query = $db->execute($code, $params); if (!$query["ok"]) { exit("Execute failed:" . $query["errmsg"] . "
\n
" . $code . "
"); From f2c4183f8f8693690866bd65929e279d46697dc8 Mon Sep 17 00:00:00 2001 From: Youssef Garas Date: Thu, 28 Mar 2013 01:58:54 -0400 Subject: [PATCH 3/6] Authentication working now --- app/classes/BaseController.php | 8 ++------ app/controllers/server.php | 6 +++--- app/lib/mongo/RMongo.php | 2 ++ app/models/MServer.php | 2 +- config.php | 2 +- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/app/classes/BaseController.php b/app/classes/BaseController.php index 766efc1..d4c56c6 100644 --- a/app/classes/BaseController.php +++ b/app/classes/BaseController.php @@ -53,7 +53,7 @@ public function onBefore() { global $MONGO; //exception handler - set_exception_handler(array($this, "exceptionHandler")); + //set_exception_handler(array($this, "exceptionHandler")); $this->_admin = MUser::userInSession(); if (!$this->_admin) { @@ -78,11 +78,7 @@ public function onBefore() { $this->redirect("login.index", array( "host" => $this->_admin->hostIndex() )); } $this->_server = MServer::serverWithIndex($this->_admin->hostIndex()); - //$this->_mongo = $this->_server->mongo(); - var_dump($_REQUEST); - $this->_mongo = new Mongo('mongodb://admin:groups4J@localhost/admin'); - - //log query + $this->_mongo = $this->_server->mongo(); if (isset($MONGO["features"]["log_query"]) && $MONGO["features"]["log_query"] == "on") { $this->_logQuery = true; } diff --git a/app/controllers/server.php b/app/controllers/server.php index 30b6986..6250d6f 100644 --- a/app/controllers/server.php +++ b/app/controllers/server.php @@ -5,7 +5,7 @@ class ServerController extends BaseController { /** server infomation **/ public function doIndex() { - $db = $this->_mongo->selectDB("admin"); + $db = $this->_mongo->selectDB($_SESSION['login']['db']); //command line $query = $db->command(array("getCmdLineOpts" => 1)); @@ -37,8 +37,8 @@ public function doIndex() { //connection $this->connections = array( - "Host" => $this->_server->mongoHost(), - "Port" => $this->_server->mongoPort(), + "Host" => 'localhost', + "Port" => 27017, "Username" => "******", "Password" => "******" ); diff --git a/app/lib/mongo/RMongo.php b/app/lib/mongo/RMongo.php index 7331152..b5f0254 100644 --- a/app/lib/mongo/RMongo.php +++ b/app/lib/mongo/RMongo.php @@ -123,6 +123,8 @@ public function lastError() { * @return array */ public function listDBs() { + // Not possible with auth in new version of mongodb + return array(); return $this->_mongo->listDBs(); } diff --git a/app/models/MServer.php b/app/models/MServer.php index 26c4626..bf5e172 100644 --- a/app/models/MServer.php +++ b/app/models/MServer.php @@ -320,7 +320,7 @@ public function mongo() { */ public function listDbs() { $dbs = $this->_mongo->listDBs(); - if (!$dbs["ok"]) { + if (empty($dbs["ok"])) { $user = MUser::userInSession(); $dbs = array( diff --git a/config.php b/config.php index 72b7039..a4b358e 100644 --- a/config.php +++ b/config.php @@ -33,7 +33,7 @@ $MONGO["servers"][$i]["ui_only_dbs"] = "";//databases to display $MONGO["servers"][$i]["ui_hide_dbs"] = "";//databases to hide $MONGO["servers"][$i]["ui_hide_collections"] = "";//collections to hide -$MONGO["servers"][$i]["ui_hide_system_collections"] = false;//if hide the system collections +$MONGO["servers"][$i]["ui_hide_system_collections"] = true;//if hide the system collections $i ++; /** From c9eddb5d19bea733f9e43d6de26ccb9b75971589 Mon Sep 17 00:00:00 2001 From: Sergey Malykh Date: Tue, 25 Jun 2013 15:54:14 +0400 Subject: [PATCH 4/6] fixed system pages issues when logged as admin (admin user must have permissions) added workaround for db.eval issue from https://github.com/quibusus/rockmongo added ISODate support --- app/classes/VarEval.php | 70 ++++++++++++++++++++++++++++++++++++++ app/controllers/server.php | 2 +- app/lib/mongo/RMongo.php | 19 +++++++++-- 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/app/classes/VarEval.php b/app/classes/VarEval.php index dab1352..a9bf7ce 100644 --- a/app/classes/VarEval.php +++ b/app/classes/VarEval.php @@ -141,12 +141,82 @@ function ISODate (isoDateStr) { } return ' . $this->_source . ';}' ); + 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; + } } ?> \ No newline at end of file diff --git a/app/controllers/server.php b/app/controllers/server.php index 6250d6f..1a33ad9 100644 --- a/app/controllers/server.php +++ b/app/controllers/server.php @@ -5,7 +5,7 @@ class ServerController extends BaseController { /** server infomation **/ public function doIndex() { - $db = $this->_mongo->selectDB($_SESSION['login']['db']); + $db = $this->_mongo->selectDB( empty($_SESSION['login']['db']) ? 'admin' : $_SESSION['login']['db'] ); //command line $query = $db->command(array("getCmdLineOpts" => 1)); diff --git a/app/lib/mongo/RMongo.php b/app/lib/mongo/RMongo.php index b5f0254..bf99e67 100644 --- a/app/lib/mongo/RMongo.php +++ b/app/lib/mongo/RMongo.php @@ -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("mongodb://{$server}", $options); + } catch ( Exception $e ) { + $options['db'] = 'admin'; + try { + $this->_mongo = new MongoClient("mongodb://{$server}", $options); + } catch ( Exception $tmp ) { + throw $e; + } + } } else { $this->_mongo = new Mongo($server, $options); @@ -124,8 +133,12 @@ public function lastError() { */ public function listDBs() { // Not possible with auth in new version of mongodb - return array(); - return $this->_mongo->listDBs(); + // return array(); + try { + return $this->_mongo->listDBs(); + } catch( Exception $e ) { + return array(); + } } /** From 67f08e841ad245beb2610a5f2a23ce4c3ad41774 Mon Sep 17 00:00:00 2001 From: glebtv Date: Tue, 25 Jun 2013 21:43:04 +0400 Subject: [PATCH 5/6] Fix messed up syncedTo dates --- app/controllers/server.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/server.php b/app/controllers/server.php index 1a33ad9..8383415 100644 --- a/app/controllers/server.php +++ b/app/controllers/server.php @@ -241,7 +241,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; @@ -254,7 +254,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; } } } @@ -268,4 +268,4 @@ public function doReplication() { } } -?> \ No newline at end of file +?> From 95cae00b36149a08d63a804e6718a2428e07dc52 Mon Sep 17 00:00:00 2001 From: Sergey Malykh Date: Tue, 2 Jul 2013 09:52:36 +0400 Subject: [PATCH 6/6] bug fixes --- app/classes/BaseController.php | 6 ++++-- app/classes/VarEval.php | 7 +------ app/controllers/server.php | 4 ++-- app/lib/mongo/RMongo.php | 22 ++++++++++------------ app/lib/page/lang/ru_ru.php | 0 app/models/MDb.php | 3 +-- app/models/MServer.php | 4 ++-- config.php | 2 +- 8 files changed, 21 insertions(+), 27 deletions(-) mode change 100755 => 100644 app/lib/page/lang/ru_ru.php diff --git a/app/classes/BaseController.php b/app/classes/BaseController.php index cf0de4c..b7a1c9a 100644 --- a/app/classes/BaseController.php +++ b/app/classes/BaseController.php @@ -53,7 +53,7 @@ public function onBefore() { global $MONGO; //exception handler - //set_exception_handler(array($this, "exceptionHandler")); + set_exception_handler(array($this, "exceptionHandler")); $this->_admin = MUser::userInSession(); if (!$this->_admin) { @@ -78,7 +78,9 @@ public function onBefore() { $this->redirect("login.index", array( "host" => $this->_admin->hostIndex() )); } $this->_server = MServer::serverWithIndex($this->_admin->hostIndex()); - $this->_mongo = $this->_server->mongo(); + $this->_mongo = $this->_server->mongo(); + + //log query if (isset($MONGO["features"]["log_query"]) && $MONGO["features"]["log_query"] == "on") { $this->_logQuery = true; } diff --git a/app/classes/VarEval.php b/app/classes/VarEval.php index a9bf7ce..4bb8d73 100644 --- a/app/classes/VarEval.php +++ b/app/classes/VarEval.php @@ -141,17 +141,12 @@ function ISODate (isoDateStr) { } return ' . $this->_source . ';}' ); - date_default_timezone_set($timezone); - if ($ret["ok"]) { return $ret["retval"]; - - } elseif( $ret["errmsg"] === "unauthorized" ) { - + } elseif($ret["errmsg"] === "unauthorized") { return $this->parseBson($this->_source); } - return false; } diff --git a/app/controllers/server.php b/app/controllers/server.php index 1af080a..2a8395c 100644 --- a/app/controllers/server.php +++ b/app/controllers/server.php @@ -45,8 +45,8 @@ public function doIndex() { //connection $this->connections = array( - "Host" => 'localhost', - "Port" => 27017, + "Host" => $this->_server->mongoHost(), + "Port" => $this->_server->mongoPort(), "Username" => "******", "Password" => "******" ); diff --git a/app/lib/mongo/RMongo.php b/app/lib/mongo/RMongo.php index bf99e67..4c0a7bc 100644 --- a/app/lib/mongo/RMongo.php +++ b/app/lib/mongo/RMongo.php @@ -17,16 +17,16 @@ class RMongo { */ public function __construct($server, array $options = array()) { if (class_exists("MongoClient")) { - try { - $this->_mongo = new MongoClient("mongodb://{$server}", $options); - } catch ( Exception $e ) { - $options['db'] = 'admin'; - try { - $this->_mongo = new MongoClient("mongodb://{$server}", $options); - } catch ( Exception $tmp ) { - throw $e; - } - } + 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); @@ -132,8 +132,6 @@ public function lastError() { * @return array */ public function listDBs() { - // Not possible with auth in new version of mongodb - // return array(); try { return $this->_mongo->listDBs(); } catch( Exception $e ) { diff --git a/app/lib/page/lang/ru_ru.php b/app/lib/page/lang/ru_ru.php old mode 100755 new mode 100644 diff --git a/app/models/MDb.php b/app/models/MDb.php index 9856045..45b8d84 100644 --- a/app/models/MDb.php +++ b/app/models/MDb.php @@ -10,7 +10,6 @@ class MDb { * @return array */ static function exec(MongoDB $db, $code, array $params = array()) { - return $db->getCollectionNames(); $query = $db->execute($code, $params); if (!$query["ok"]) { exit("Execute failed:" . $query["errmsg"] . "
\n
" . $code . "
"); @@ -29,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) { } diff --git a/app/models/MServer.php b/app/models/MServer.php index ffb4c1b..9e412a8 100644 --- a/app/models/MServer.php +++ b/app/models/MServer.php @@ -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) @@ -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) diff --git a/config.php b/config.php index 1eb822d..8abdd42 100644 --- a/config.php +++ b/config.php @@ -25,7 +25,7 @@ //$MONGO["servers"][$i]["mongo_db"] = "MONGO_DATABASE";//default mongo db to connect, works only if mongo_auth=false //$MONGO["servers"][$i]["mongo_user"] = "MONGO_USERNAME";//mongo authentication user name, works only if mongo_auth=false //$MONGO["servers"][$i]["mongo_pass"] = "MONGO_PASSWORD";//mongo authentication password, works only if mongo_auth=false -$MONGO["servers"][$i]["mongo_auth"] = true;//enable mongo authentication? +$MONGO["servers"][$i]["mongo_auth"] = false;//enable mongo authentication? $MONGO["servers"][$i]["control_auth"] = true;//enable control users, works only if mongo_auth=false $MONGO["servers"][$i]["control_users"]["admin"] = "admin";//one of control users ["USERNAME"]=PASSWORD, works only if mongo_auth=false