Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

300 linhas
7.7 KiB

  1. <?php
  2. ob_start();
  3. session_write_close();
  4. session_start();
  5. set_time_limit(0);
  6. import("lib.ext.RExtController");
  7. import("funcs.functions", false);
  8. import("funcs.render", false);
  9. import("funcs.rock", false);
  10. import("models.MMongo");
  11. import("models.MServer");
  12. import("models.MUser");
  13. import("models.MDb");
  14. import("models.MCollection");
  15. import("classes.VarExportor");
  16. import("classes.VarEval");
  17. //filter $MONGO
  18. if (class_exists("RFilter")) {
  19. global $MONGO;
  20. RFilter::apply("CONFIG_FILTER", $MONGO);
  21. }
  22. class BaseController extends RExtController {
  23. /**
  24. * Enter description here...
  25. *
  26. * @var MServer
  27. */
  28. protected $_server;
  29. /**
  30. * Enter description here ...
  31. *
  32. * @var RMongo
  33. */
  34. protected $_mongo;
  35. /**
  36. * current session user
  37. *
  38. * @var MUser
  39. */
  40. protected $_admin;
  41. protected $_logQuery = false;
  42. /** called before any actions **/
  43. public function onBefore() {
  44. global $MONGO;
  45. //exception handler
  46. set_exception_handler(array($this, "exceptionHandler"));
  47. $this->_admin = MUser::userInSession();
  48. if (!$this->_admin) {
  49. //if user is loged in?
  50. $server = MServer::serverWithIndex(xi("host"));
  51. //filter server plugins
  52. if (class_exists("RFilter")) {
  53. RFilter::apply("SERVER_FILTER", $server);
  54. }
  55. //if auth is disabled
  56. if ($server && !$server->mongoAuth() && !$server->controlAuth()) {
  57. MUser::login("rockmongo_memo", "rockmongo_memo", xi("host"), "admin", 10800);
  58. $this->_admin = MUser::userInSession();
  59. }
  60. else {
  61. $this->redirect("login.index", array( "host" => xi("host")));
  62. }
  63. }
  64. if (!$this->_admin->validate()) {
  65. $this->redirect("login.index", array( "host" => $this->_admin->hostIndex() ));
  66. }
  67. $this->_server = MServer::serverWithIndex($this->_admin->hostIndex());
  68. $this->_mongo = $this->_server->mongo();
  69. //log query
  70. if (isset($MONGO["features"]["log_query"]) && $MONGO["features"]["log_query"] == "on") {
  71. $this->_logQuery = true;
  72. }
  73. //render header
  74. if (!$this->isAjax()) {
  75. render_view("header");
  76. }
  77. }
  78. /** called after action call **/
  79. public function onAfter() {
  80. if (!$this->isAjax()) {
  81. render_view("footer");
  82. }
  83. }
  84. /**
  85. * handle exception in runtime
  86. *
  87. * @param Exception $exception exception to handle
  88. */
  89. public function exceptionHandler($exception) {
  90. $message = $exception->getMessage();
  91. render_view("exception", array( "message" => $message ));
  92. render_view("footer");
  93. exit();
  94. }
  95. /**
  96. * convert variable from string values
  97. *
  98. * @param MongoDB $mongodb MongoDB instance
  99. * @param string $dataType data type
  100. * @param string $format string format
  101. * @param string $value string value
  102. * @param integer $integerValue integer value
  103. * @param long $longValue long value
  104. * @param string $doubleValue float value
  105. * @param string $boolValue boolea value
  106. * @param string $mixedValue mixed value (array or object)
  107. * @return mixed
  108. * @throws Exception
  109. */
  110. protected function _convertValue($mongodb, $dataType, $format, $value, $integerValue, $longValue, $doubleValue, $boolValue, $mixedValue) {
  111. $realValue = null;
  112. switch ($dataType) {
  113. case "integer":
  114. if (class_exists("MongoInt32")) {
  115. $realValue = new MongoInt32($integerValue);
  116. }
  117. else {
  118. $realValue = intval($realValue);
  119. }
  120. break;
  121. case "long":
  122. if (class_exists("MongoInt64")) {
  123. $realValue = new MongoInt64($longValue);
  124. }
  125. else {
  126. $realValue = $longValue;
  127. }
  128. break;
  129. case "float":
  130. case "double":
  131. $realValue = doubleval($doubleValue);
  132. break;
  133. case "string":
  134. $realValue = $value;
  135. break;
  136. case "boolean":
  137. $realValue = ($boolValue == "true");
  138. break;
  139. case "null":
  140. $realValue = NULL;
  141. break;
  142. case "mixed":
  143. $eval = new VarEval($mixedValue, $format, $mongodb);
  144. $realValue = $eval->execute();
  145. if ($realValue === false) {
  146. throw new Exception("Unable to parse mixed value, just check syntax!");
  147. }
  148. break;
  149. }
  150. return $realValue;
  151. }
  152. protected function _encodeJson($var) {
  153. if (function_exists("json_encode")) {
  154. return json_encode($var);
  155. }
  156. import("classes.Services_JSON");
  157. $service = new Services_JSON();
  158. return $service->encode($var);
  159. }
  160. /**
  161. * Output variable as JSON
  162. *
  163. * @param mixed $var variable
  164. * @param boolean $exit if exit after output
  165. */
  166. protected function _outputJson($var, $exit = true) {
  167. echo $this->_encodeJson($var);
  168. if ($exit) {
  169. exit();
  170. }
  171. }
  172. protected function _decodeJson($var) {
  173. import("classes.Services_JSON");
  174. $service = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  175. $ret = array();
  176. $decode = $service->decode($var);
  177. return $decode;
  178. }
  179. /**
  180. * Export var as string then highlight it.
  181. *
  182. * @param mixed $var variable to be exported
  183. * @param string $format data format, array|json
  184. * @param boolean $label if add label to field
  185. * @return string
  186. */
  187. protected function _highlight($var, $format = "array", $label = false) {
  188. import("classes.VarExportor");
  189. $exportor = new VarExportor($this->_mongo->selectDB("admin"), $var);
  190. $varString = null;
  191. $highlight = true;
  192. switch ($this->_server->docsRender()) {
  193. case "default":
  194. $varString = $exportor->export($format, $label);
  195. break;
  196. case "plain":
  197. $varString = $exportor->export($format, false);
  198. $label = false;
  199. $highlight = false;
  200. break;
  201. default:
  202. $varString = $exportor->export($format, $label);
  203. break;
  204. }
  205. $string = null;
  206. if ($highlight) {
  207. if ($format == "array") {
  208. $string = highlight_string("<?php " . $varString, true);
  209. $string = preg_replace("/" . preg_quote('<span style="color: #0000BB">&lt;?php&nbsp;</span>', "/") . "/", '', $string, 1);
  210. }
  211. else {
  212. $string = json_format_html($varString);
  213. }
  214. }
  215. else {
  216. $string = "<div><xmp style='width:600px;overflow:auto'>" . $varString . "</xmp></div>";
  217. }
  218. if ($label) {
  219. $id = addslashes(isset($var["_id"]) ? rock_id_string($var["_id"]) : "");
  220. $string = preg_replace_callback("/(['\"])rockfield\\.(.+)\\.rockfield(['\"])/U", create_function('$match', ' $fields = explode(".rockfield.", $match[2]);
  221. return "<span class=\"field\" field=\"" . implode(".", $fields) . "\">" . $match[1] . array_pop($fields) . $match[3] . "</span>";'), $string);
  222. $string = preg_replace_callback("/__rockmore\\.(.+)\\.rockmore__/U", create_function('$match', '
  223. $field = str_replace("rockfield.", "", $match[1]);
  224. return "<a href=\"#\" onclick=\"fieldOpMore(\'" . $field . "\',\'' . $id . '\');return false;\" title=\"More text\">[...]</a>";'), $string);
  225. }
  226. return $string;
  227. }
  228. /**
  229. * Enter description here...
  230. *
  231. * @param MongoDB $db
  232. * @param unknown_type $from
  233. * @param unknown_type $to
  234. * @param unknown_type $index
  235. */
  236. protected function _copyCollection($db, $from, $to, $index = true) {
  237. if ($index) {
  238. $indexes = $db->selectCollection($from)->getIndexInfo();
  239. foreach ($indexes as $index) {
  240. $options = array();
  241. if (isset($index["unique"])) {
  242. $options["unique"] = $index["unique"];
  243. }
  244. if (isset($index["name"])) {
  245. $options["name"] = $index["name"];
  246. }
  247. if (isset($index["background"])) {
  248. $options["background"] = $index["background"];
  249. }
  250. if (isset($index["dropDups"])) {
  251. $options["dropDups"] = $index["dropDups"];
  252. }
  253. $db->selectCollection($to)->ensureIndex($index["key"], $options);
  254. }
  255. }
  256. $ret = $db->execute('function (coll, coll2) { return db.getCollection(coll).copyTo(coll2);}', array( $from, $to ));
  257. return $ret["ok"];
  258. }
  259. protected function _logFile($db, $collection) {
  260. $logDir = dirname(__ROOT__) . DS . "logs";
  261. return $logDir . DS . urlencode($this->_admin->username()) . "-query-" . urlencode($db) . "-" . urlencode($collection) . ".php";
  262. }
  263. /**
  264. * remember data format choice
  265. *
  266. * @param string $format data format
  267. */
  268. protected function _rememberFormat($format) {
  269. setcookie("rock_format", $format, time() + 365 * 86400, "/");
  270. }
  271. }
  272. ?>