/** * 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(); } }
function doCreateIndex() { $fields = xn("field"); if (!is_array($fields)) { $this->_outputJson(array("code" => 300, "message" => "Index contains one field at least.")); } $orders = xn("order"); $attrs = array(); foreach ($fields as $index => $field) { $field = trim($field); if (!empty($field)) { $attrs[$field] = $orders[$index] == "asc" ? 1 : -1; } } if (empty($attrs)) { $this->_outputJson(array("code" => 300, "message" => "Index contains one field at least.")); } //if is unique $options = array(); if (x("is_unique")) { $options["unique"] = 1; if (x("drop_duplicate")) { $options["dropDups"] = 1; } } $options["background"] = 1; $options["safe"] = 1; //name $name = trim(xn("name")); if (!empty($name)) { $options["name"] = $name; } //check name $collection = $this->_mongodb->selectCollection($this->collection); $indexes = $collection->getIndexInfo(); foreach ($indexes as $index) { if ($index["name"] == $name) { $this->_outputJson(array("code" => 300, "message" => "The name \"{$name}\" is token by other index.")); break; } if ($attrs === $index["key"]) { $this->_outputJson(array("code" => 300, "message" => "The key on same fields already exists.")); break; } } $ret = null; try { $ret = $collection->ensureIndex($attrs, $options); } catch (Exception $e) { $this->_outputJson(array("code" => 300, "message" => $e->getMessage())); } if ($ret["ok"]) { $this->_outputJson(array("code" => 200)); } else { $this->_outputJson(array("code" => 300, "message" => $ret["err"])); } }
/** drop database **/ public function doDropDatabase() { $this->db = xn("db"); if (!x("confirm")) { $this->display(); return; } $ret = $this->_mongo->dropDB($this->db); $this->ret = $this->_highlight($ret, "json"); $this->display("dropDatabaseResult"); }
/** import a collection **/ public function doCollectionImport() { $this->redirect("db.dbImport", array("db" => xn("db"))); }
/** * Render collection operations * * @param string $dbName database name * @param string $collectionName collection name * @since 1.1.0 */ function render_collection_menu($dbName, $collectionName) { $params = xn(); $exportParams = $params; $exportParams["can_download"] = 1; $menuItems = array(array("action" => "collection.createRow", "params" => $params, "name" => rock_lang("insert")), array("action" => "collection.clearRows", "params" => $params, "name" => rock_lang("clear"), "attr.onclick" => "return window.confirm('Are you sure to delete all records in collection \\'" . $collectionName . "\\'?');"), array("action" => "#", "params" => array(), "name" => rock_lang("new_field"), "attr.onclick" => "fieldOpNew();return false;"), array("action" => "collection.collectionStats", "params" => $params, "name" => rock_lang("statistics")), array("action" => "collection.collectionExport", "params" => $exportParams, "name" => rock_lang("export")), array("action" => "collection.collectionImport", "params" => $params, "name" => rock_lang("import")), array("action" => "collection.collectionProps", "params" => $params, "name" => rock_lang("properties")), array("action" => "collection.collectionIndexes", "params" => $params, "name" => rock_lang("indexes")), array("action" => "collection.collectionRename", "params" => $params, "name" => rock_lang("rename")), array("action" => "collection.collectionDuplicate", "params" => $params, "name" => rock_lang("duplicate")), array("action" => "collection.collectionTransfer", "params" => $params, "name" => rock_lang("transfer")), array("action" => "collection.collectionValidate", "params" => $params, "name" => rock_lang("validate")), array("action" => "collection.removeCollection", "params" => $params, "name" => rock_lang("drop"), "attr.onclick" => "return window.confirm('Are you sure to drop collection \\'" . $collectionName . "\\'?')")); //plugin if (class_exists("RFilter")) { RFilter::apply("COLLECTION_MENU_FILTER", $menuItems, array("dbName" => $dbName, "collectionName" => $collectionName)); } $displayCount = 6; $hasMore = false; $string = ""; $count = count($menuItems); foreach ($menuItems as $index => $op) { if ($index >= $displayCount && !$hasMore) { $hasMore = true; $string .= "<a href=\"#\" onclick=\"showMoreMenus(this);return false;\">" . rock_lang("more") . " »</a>"; $string .= "<div class=\"menu\">"; } if (!empty($op["action"])) { $string .= '<a href="' . url($op["action"], isset($op["params"]) ? $op["params"] : array()) . '"'; if (__CONTROLLER__ . "." . __ACTION__ == $op["action"]) { $string .= ' class="current"'; } foreach ($op as $attrName => $attrValue) { if (preg_match("/^attr\\.(\\w+)/", $attrName, $match)) { $string .= " " . $match[1] . "=\"" . $attrValue . "\""; } } $string .= ">" . $op["name"] . "</a>"; } else { if (!empty($op["url"])) { $string .= "<a href=\"" . $op["url"] . "\" target=\"_blank\">"; } $string .= $op["name"]; if (!empty($op["url"])) { $string .= "</a>"; } } if ($hasMore) { $string .= "<br/>"; } else { if ($index < $count - 1) { $string .= " | "; } } } if ($hasMore) { $string .= "</div>"; } echo $string; }
>modify</option> </select> </td> </tr> <tr> <td colspan="2"><input type="submit" value="<?php hm("submit_query"); ?> "/> <input type="button" value="<?php hm("explain"); ?> " onclick="explainQuery(this.form)" /> <input type="button" value="<?php hm("clear_conditions"); ?> " onclick="window.location='<?php h(url("collection.index", array("db" => $db, "collection" => $collection, "format" => xn("format")))); ?> '"/> <?php if (isset($cost)) { ?> Cost <?php h(round($cost, 6)); ?> s<?php } ?> <?php if (isset($message)) { ?> <p class="error"><?php
} ?> </div> <?php hm("db"); ?> : <select name="db"> <?php foreach ($dbs as $value) { ?> <option value="<?php h($value["name"]); ?> " <?php if (xn("db") == $value["name"]) { ?> selected="selected"<?php } ?> ><?php h($value["name"]); ?> </option> <?php } ?> </select> <?php hm("argument"); ?> :<input type="button" onclick="addArgument()" value="<?php
/** * 开启自动构造查询条件功能 * * @param boolean $bool 是否开启该功能 * @param string|array $except 要去除的参数名 * @param string|array $only 限制的参数名 * @return RPage * @since 1.0.3 */ function setAutoQuery($bool = true, $except = "", $only = "") { if (!is_array($except)) { $except = preg_split("/\\s+,\\s+/", $except); } if (!is_array($only) && strlen($only) > 0) { $only = preg_split("/\\s+,\\s+/", $only); } if ($bool) { $x = xn(); foreach ($x as $name => $value) { if ($except && in_array($name, $except)) { unset($x[$name]); } if ($only && !in_array($name, $only)) { unset($x[$name]); } } $this->setQuery($x); } return $this; }
/** create databse **/ public function doCreateDatabase() { if ($this->isPost()) { $name = trim(xn("name")); if (empty($name)) { $this->error = "Please input a valid database name."; $this->display(); return; } $this->message = "New database created."; $this->_mongo->selectDb($name)->execute("function(){}"); } $this->display(); }
?> selected="selected"<?php } ?> >JSON</option> </select><br/> _id:<br/> <input type="text" readonly value="<?php h_escape($row["_id"]); ?> " size="72"/> <br/> <?php hm("data"); ?> :<br/> <textarea rows="35" cols="70" name="data" id="row_data"><?php h($data); ?> </textarea><br/> <input type="submit" value="<?php hm("save"); ?> "/> <input type="button" value="<?php hm("back"); ?> " onclick="window.location='<?php h(xn("uri")); ?> '"/> </form>
/** * 开启自动构造查询条件功能 * * @param boolean $bool 是否开启该功能 * @param string|array $except 要去除的参数名 * @param string|array $only 限制的参数名 * @return RPage * @since 1.0.3 */ function setAutoQuery($bool = true, $except = "", $only = "") { if ($bool) { $x = xn(); foreach ($x as $name => $value) { if ($except && if_in_array($name, $except)) { unset($x[$name]); } if ($only && !if_in_array($name, $only)) { unset($x[$name]); } } $this->setQuery($x); } return $this; }