Пример #1
0
 /**
  * 构造查询
  *
  * @param Mongo $mongo MongoDB连接
  * @param string $db 数据库
  * @param string $collection 集合
  */
 function __construct(RMongo $mongo, $db, $collection)
 {
     $this->_dbName = $db;
     $this->_collectionName = $collection;
     $this->_db = $mongo->selectDB($this->_dbName);
     $this->_collection = $mongo->selectCollection($this->_dbName, $this->_collectionName);
 }
Пример #2
0
    /**
     * 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">&lt;?php&nbsp;</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;
    }
Пример #3
0
    /**
     * 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 = $exportor->export($format, $label);
        $string = null;
        if ($format == "array") {
            $string = highlight_string("<?php " . $varString, true);
            $string = preg_replace("/" . preg_quote('<span style="color: #0000BB">&lt;?php&nbsp;</span>', "/") . "/", '', $string, 1);
        } else {
            $string = json_format_html($varString);
        }
        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;
    }
Пример #4
0
 /** transfer db collections from one server to another **/
 public function doDbTransfer()
 {
     $this->db = xn("db");
     $db = $this->_mongo->selectDB($this->db);
     $this->collections = $db->listCollections();
     $this->servers = $this->_admin->servers();
     $this->selectedCollections = array();
     if (!$this->isPost()) {
         $this->selectedCollections[] = xn("collection");
         x("copy_indexes", 1);
         $this->target_host = "";
         $this->target_sock = "";
         $this->target_port = 27017;
         $this->target_auth = 0;
         $this->target_username = "";
         $this->target_password = "";
     } else {
         $this->target_host = trim(xn("target_host"));
         $this->target_sock = trim(xn("target_sock"));
         $this->target_port = xi("target_port");
         $this->target_auth = xi("target_auth");
         $this->target_username = trim(xn("target_username"));
         $this->target_password = trim(xn("target_password"));
         $checkeds = xn("checked");
         if (is_array($checkeds)) {
             $this->selectedCollections = array_keys($checkeds);
         }
         if (empty($checkeds)) {
             $this->error = "Please select collections which you want to transfer.";
             $this->display();
             return;
         }
         if (empty($this->target_host) && empty($this->target_sock)) {
             $this->error = "Target host must not be empty.";
             $this->display();
             return;
         }
         $copyIndexes = xi("copy_indexes");
         /**if ($target === "") {
         				$this->error = "Please enter a valid database name.";
         				$this->display();
         				return;
         			}**/
         //start to transfer
         $targetOptions = array();
         if ($this->target_auth) {
             $targetOptions["username"] = $this->target_username;
             $targetOptions["password"] = $this->target_password;
         }
         $uri = null;
         if ($this->target_sock) {
             $uri = "mongodb://" . $this->target_sock;
         } else {
             $uri = "mongodb://" . $this->target_host . ":" . $this->target_port;
         }
         $targetConnection = new RMongo($uri, $targetOptions);
         $targetDb = $targetConnection->selectDB($this->db);
         if ($this->target_auth) {
             // "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) {
                 $targetDb->authenticate($this->target_username, $this->target_password);
             }
         }
         $errors = array();
         foreach ($this->selectedCollections as $collectionName) {
             $ret = $targetDb->command(array("cloneCollection" => $this->db . "." . $collectionName, "from" => $this->_server->uri(), "copyIndexes" => (bool) $copyIndexes));
             if (!$ret["ok"]) {
                 $errors[] = MMongo::readException($ret);
                 break;
             }
         }
         if (!empty($errors)) {
             $this->error = implode("<br/>", $errors);
             $this->display();
             return;
         }
         $this->message = "All data were transfered to '{$this->target_host}' successfully.";
     }
     $this->display();
 }
Пример #5
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;
 }