/** * 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"><?php </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; }
/** * load field data * */ function doLoad() { $collection = $this->_mongodb->selectCollection($this->collection); $id = xn("id"); $field = xn("field"); $type = "integer"; $data = null; if ($id) { $one = $collection->findOne(array("_id" => rock_real_id($id))); //not select field, because there is a bug in list, such as "list.0" $data = rock_array_get($one, $field); switch (gettype($data)) { case "boolean": $type = "boolean"; break; case "integer": $type = "integer"; break; case "long": $type = "long"; break; case "float": case "double": $type = "double"; break; case "string": $type = "string"; break; case "array": $type = "mixed"; break; case "object": // int64 is returned as object (Kyryl Bilokurov <*****@*****.**>) if (get_class($data) == "MongoInt64") { $type = "long"; } else { $type = "mixed"; } break; case "resource": $type = "mixed"; break; case "NULL": $type = "null"; break; } } $exporter = new VarExportor($this->_mongodb, $data); $format = rock_cookie("rock_format", "json"); $represent = $exporter->export($format); if ($format == "json") { $represent = json_unicode_to_utf8($represent); } $this->_outputJson(array("code" => 200, "type" => $type, "value" => $type == "long" ? $data->__toString() : $data, "represent" => $represent, "format" => $format)); }
/** export db **/ public function doDbExport() { $this->db = xn("db"); $db = $this->_mongo->selectDB($this->db); $this->collections = MDb::listCollections($db); $this->selectedCollections = array(); if (!$this->isPost()) { $this->selectedCollections[] = xn("collection"); } else { $checkeds = xn("checked"); $canDownload = xn("can_download"); if (is_array($checkeds)) { $this->selectedCollections = array_keys($checkeds); } sort($this->selectedCollections); import("classes.VarExportor"); $this->contents = ""; $this->countRows = 0; //indexes foreach ($this->selectedCollections as $collection) { $collObj = $db->selectCollection($collection); $infos = $collObj->getIndexInfo(); foreach ($infos as $info) { $options = array(); if (isset($info["unique"])) { $options["unique"] = $info["unique"]; } $exportor = new VarExportor($db, $info["key"]); $exportor2 = new VarExportor($db, $options); $this->contents .= "\n/** {$collection} indexes **/\ndb.getCollection(\"" . addslashes($collection) . "\").ensureIndex(" . $exportor->export(MONGO_EXPORT_JSON) . "," . $exportor2->export(MONGO_EXPORT_JSON) . ");\n"; } } //data foreach ($this->selectedCollections as $collection) { $cursor = $db->selectCollection($collection)->find(); $this->contents .= "\n/** " . $collection . " records **/\n"; foreach ($cursor as $one) { $this->countRows++; $exportor = new VarExportor($db, $one); $this->contents .= "db.getCollection(\"" . addslashes($collection) . "\").insert(" . $exportor->export(MONGO_EXPORT_JSON) . ");\n"; unset($exportor); } unset($cursor); } if (x("can_download")) { $prefix = "mongo-" . urlencode($this->db) . "-" . date("Ymd-His"); //gzip if (x("gzip")) { ob_end_clean(); header("Content-type: application/x-gzip"); header("Content-Disposition: attachment; filename=\"{$prefix}.gz\""); echo gzcompress($this->contents, 9); exit; } else { ob_end_clean(); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"{$prefix}.js\""); echo $this->contents; exit; } } } $this->display(); }
/** modify one row **/ public function doModifyRow() { $this->db = xn("db"); $this->collection = xn("collection"); $id = rock_real_id(xn("id")); //selected format last time $this->last_format = rock_cookie("rock_format", "json"); import("lib.mongo.RQuery"); $query = new RQuery($this->_mongo, $this->db, $this->collection); $this->row = $query->id($id)->findOne(); if (empty($this->row)) { $this->error = "Record is not found."; $this->display(); return; } $this->data = $this->row; unset($this->data["_id"]); import("classes.VarExportor"); $export = new VarExportor($query->db(), $this->data); $this->data = $export->export($this->last_format); if ($this->last_format == "json") { $this->data = json_unicode_to_utf8($this->data); } if ($this->isPost()) { $this->data = xn("data"); $format = x("format"); $this->last_format = $format; $row = null; $eval = new VarEval($this->data, $format, $this->_mongo->selectDb($this->db)); $row = $eval->execute(); if ($row === false || !is_array($row)) { $this->error = "Only valid {$format} is accepted."; $this->display(); return; } $query = new RQuery($this->_mongo, $this->db, $this->collection); $obj = $query->id($this->row["_id"])->find(); $oldAttrs = $obj->attrs(); $obj->setAttrs($row); foreach ($oldAttrs as $oldAttr => $oldValue) { if ($oldAttr == "_id") { continue; } if (!array_key_exists($oldAttr, $row)) { $obj->remove($oldAttr); } } try { $obj->save(); } catch (Exception $e) { $this->error = $e->getMessage(); $this->display(); return; } //remember format choice $this->_rememberFormat($format); $this->message = "Updated successfully."; } $this->display(); }
/** * 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; }
/** export db **/ public function doDbExport() { if (x("format") == "delete") { $file = BACKUP_DIR . '/' . $_POST["name"]; //你要删除的文件目录 if (file_exists($file)) { $result = @unlink($file); //unlink函数删除文件用的 if ($result == true) { //echo '文件已经成功删除'; } else { //echo '文件无法删除'; } } else { //echo("文件不存在"); } $this->display(); return; } global $MONGO; $MONGO['data']['MongoBinData'] = 'info'; $this->db = xn("db"); $db = $this->_mongo->selectDB($this->db); $this->collections = MDb::listCollections($db); $this->selectedCollections = array(); if (!$this->isPost()) { $this->selectedCollections[] = xn("collection"); } else { $checkeds = xn("checked"); $canDownload = xn("can_download"); if (is_array($checkeds)) { $this->selectedCollections = array_keys($checkeds); } sort($this->selectedCollections); import("classes.VarExportor"); $prefix = "mongo-" . urlencode($this->db) . "-" . date("Ymd-His"); //将文件放到服务器上 $file_path = BACKUP_DIR . '/' . $prefix . '.js'; //indexes foreach ($this->selectedCollections as $collection) { $collObj = $db->selectCollection($collection); $infos = $collObj->getIndexInfo(); foreach ($infos as $info) { $options = array(); if (isset($info["unique"])) { $options["unique"] = $info["unique"]; } $exportor = new VarExportor($db, $info["key"]); $exportor2 = new VarExportor($db, $options); $contents = "\n/** {$collection} indexes **/\ndb.getCollection(\"" . addslashes($collection) . "\").ensureIndex(" . $exportor->export(MONGO_EXPORT_JSON) . "," . $exportor2->export(MONGO_EXPORT_JSON) . ");\n"; file_put_contents($file_path, $contents, FILE_APPEND); unset($exportor); unset($exportor2); } } $countRows = 0; //data foreach ($this->selectedCollections as $collection) { $contents = "\n/** " . $collection . " records **/\n"; file_put_contents($file_path, $contents, FILE_APPEND); $cursor = $db->selectCollection($collection)->find(); foreach ($cursor as $one) { $countRows++; $exportor = new VarExportor($db, $one); $contents = "db.getCollection(\"" . addslashes($collection) . "\").insert(" . $exportor->export(MONGO_EXPORT_JSON) . ");\n\n"; file_put_contents($file_path, $contents, FILE_APPEND); unset($exportor); } unset($cursor); } if (x("can_download")) { //gzip if (x("gzip")) { ob_end_clean(); header("Content-type: application/x-gzip"); header("Content-Disposition: attachment; filename=\"{$prefix}.gz\""); $str = file_get_contents($file_path); if ($str) { echo gzcompress($str, 9); } @unlink($file_path); //echo gzcompress($this->contents, 9); exit; } else { ob_end_clean(); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"{$prefix}.js\""); readfile($file_path); @unlink($file_path); //echo $this->contents; exit; } } elseif (x("butts")) { //error_log(json_encode(array('GET'=>$_GET,'POST'=>$_POST))); $myfile = fopen(BACKUP_DIR . '/' . $prefix . '.txt', "w") or die("Unable to open file!"); fwrite($myfile, json_encode($_POST['checked'])); fclose($myfile); } } $this->display(); }