/** called before any actions **/ public function onBefore() { global $MONGO; //exception handler set_exception_handler(array($this, "exceptionHandler")); $this->_admin = MUser::userInSession(); if (!$this->_admin) { //if user is loged in? $server = MServer::serverWithIndex(xi("host")); //filter server plugins if (class_exists("RFilter")) { RFilter::apply("SERVER_FILTER", $server); } //if auth is disabled if ($server && !$server->mongoAuth() && !$server->controlAuth()) { MUser::login("rockmongo_memo", "rockmongo_memo", xi("host"), "admin", 10800); $this->_admin = MUser::userInSession(); } else { $this->redirect("login.index", array("host" => xi("host"))); } } if (!$this->_admin->validate()) { $this->redirect("login.index", array("host" => $this->_admin->hostIndex())); } $this->_server = MServer::serverWithIndex($this->_admin->hostIndex()); $this->_mongo = $this->_server->mongo(); //log query if (isset($MONGO["features"]["log_query"]) && $MONGO["features"]["log_query"] == "on") { $this->_logQuery = true; } //render header if (!$this->isAjax()) { render_view("header"); } }
/** * login page and post */ public function doIndex() { global $MONGO; $password = trim(xn("password")); $this->username = trim(xn("username")); $this->db = trim(xn("db")); $this->hostIndex = xi("host"); $this->languages = rock_load_languages(); $this->expires = array(3 => "3 " . rock_lang("hours"), 720 => "1 " . rock_lang("month")); $this->moreOptions = xi("more"); if ($this->isPost()) { //server exists? if (!isset($MONGO["servers"][$this->hostIndex])) { $this->message = "Server does not exist"; return; } //authenticate $server = MServer::serverWithIndex($this->hostIndex); if (!$server->auth($this->username, $password, $this->db)) { $this->message = rock_lang("can_not_auth"); $this->display(); return; } //remember user import("models.MUser"); MUser::login($this->username, $password, $this->hostIndex, $this->db, xi("expire") * 3600); //remember lang setcookie("ROCK_LANG", x("lang"), time() + 365 * 86400); //jump to admin page $this->redirect("admin.index", array("host" => $this->hostIndex)); } else { $this->display(); } }
/** * Validate User * * @return boolean */ public function validate() { import("@.MServer"); $server = MServer::serverWithIndex($this->_hostIndex); if (empty($server)) { return false; } return $server->auth($this->_username, $this->_password, $this->_db); }
/** * Export var as string then highlight it. * * @param mixed $var variable to be exported * @param string $format data format, array|json * @param boolean $label if add label to field * @return string */ protected function _highlight($var, $format = "array", $label = false) { import("classes.VarExportor"); $exportor = new VarExportor($this->_mongo->selectDB("admin"), $var); $varString = null; $highlight = true; $mixed = false; switch ($this->_server->docsRender()) { case "default": $varString = $exportor->export($format, $label); break; case "plain": $varString = $exportor->export($format, false); $label = false; $highlight = false; break; case "mixed": $varString = $exportor->export($format, false); if (strlen($varString) > $this->_server->docsRenderLimit()) { $highlight = false; } else { $highlight = true; } $label = false; $mixed = true; break; default: $varString = $exportor->export($format, $label); break; } $string = null; if ($highlight) { if ($format == "array") { $string = highlight_string("<?php " . $varString, true); $string = preg_replace("/" . preg_quote('<span style="color: #0000BB"><?php </span>', "/") . "/", '', $string, 1); } else { $string = json_format_html($varString); } } else { $string = "<div><xmp style='width:600px;overflow:auto'>" . $varString . "</xmp></div>"; } if ($label) { $id = addslashes(isset($var["_id"]) ? rock_id_string($var["_id"]) : ""); $string = preg_replace_callback("/(['\"])rockfield\\.(.+)\\.rockfield(['\"])/U", create_function('$match', ' $fields = explode(".rockfield.", $match[2]); return "<span class=\\"field\\" field=\\"" . implode(".", $fields) . "\\">" . $match[1] . array_pop($fields) . $match[3] . "</span>";'), $string); $string = preg_replace_callback("/__rockmore\\.(.+)\\.rockmore__/U", create_function('$match', ' $field = str_replace("rockfield.", "", $match[1]); return "<a href=\\"#\\" onclick=\\"fieldOpMore(\'" . $field . "\',\'' . $id . '\');return false;\\" title=\\"More text\\">[...]</a>";'), $string); } return $string; }
/** * List collections in a DB * * @param MongoDB $db DB * @return array<MongoCollection> */ static function listCollections(MongoDB $db) { $server = MServer::currentServer(); $names = array(); $query = $db->execute("function (){ return db.getCollectionNames(); }", array()); if ($query["ok"]) { $names = $query["retval"]; } else { $colls = $db->listCollections(true); foreach ($colls as $coll) { $names[] = $coll->getName(); } } $ret = array(); foreach ($names as $name) { if ($server->shouldHideCollection($name)) { continue; } if (preg_match("/^system\\./", $name)) { continue; } $ret[] = $name; } sort($ret); //system collections if (!$server->uiHideSystemCollections()) { foreach ($names as $name) { if ($server->shouldHideCollection($name)) { continue; } if (preg_match("/^system\\./", $name)) { $ret[] = $name; } } } $collections = array(); foreach ($ret as $v) { if ($v === "") { //older MongoDB version (maybe before 1.7) allow empty collection name continue; } $collections[] = $db->selectCollection($v); } return $collections; }
/** * List collections in a DB * * @param MongoDB $db DB * @return array<MongoCollection> */ static function listCollections(MongoDB $db) { $server = MServer::currentServer(); $names = array(); try { $names = $db->getCollectionNames(true); } catch (Exception $e) { } $ret = array(); foreach ($names as $name) { if ($server->shouldHideCollection($name)) { continue; } if (preg_match("/^system\\./", $name)) { continue; } $ret[] = $name; } sort($ret); //system collections if (!$server->uiHideSystemCollections()) { foreach ($names as $name) { if ($server->shouldHideCollection($name)) { continue; } if (preg_match("/^system\\./", $name)) { $ret[] = $name; } } } $collections = array(); foreach ($ret as $v) { if ($v === "") { //older MongoDB version (maybe before 1.7) allow empty collection name continue; } $collections[] = $db->selectCollection($v); } return $collections; }
/** * Enter description here ... * * @param unknown_type $hostIndex * @return MServer */ public static function serverWithIndex($hostIndex) { global $MONGO; if (!isset($MONGO["servers"][$hostIndex])) { return null; } if (!isset(self::$_servers[$hostIndex])) { self::$_servers[$hostIndex] = new MServer($MONGO["servers"][$hostIndex]); } self::$_currentServer = self::$_servers[$hostIndex]; return self::$_servers[$hostIndex]; }
/** * Enter description here ... * * @return boolean */ public function validate() { import("@.MServer"); return MServer::serverWithIndex($this->_hostIndex)->auth($this->_username, $this->_password, $this->_db); }
/** * Render server hosts * * @param string $name tag name * @param string|null $selected selected host index * @since 1.1.0 */ function render_select_hosts($name = "host", $selected = null) { global $MONGO; $hosts = array(); foreach ($MONGO["servers"] as $config) { $server = new MServer($config); $hosts[] = $server->mongoName(); } render_select($name, $hosts, $selected, array("class" => "select_hosts")); }