您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

498 行
11 KiB

  1. <?php
  2. import("lib.mongo.RMongo");
  3. class MServer {
  4. private $_mongoName = null;
  5. private $_mongoSock = null;
  6. private $_mongoHost = "127.0.0.1";
  7. private $_mongoPort = 27017;
  8. private $_mongoUser = "";
  9. private $_mongoPass = "";
  10. private $_mongoAuth = false;
  11. private $_mongoTimeout = 0;
  12. private $_mongoDb;
  13. private $_mongoOptions = array();
  14. private $_controlAuth = true;
  15. private $_controlUsers = array();
  16. private $_uiDefaultDb;//not be used yet
  17. private $_uiOnlyDbs;
  18. private $_uiHideDbs;
  19. private $_uiHideCollections;
  20. private $_uiHideSystemCollections = false;
  21. private $_docsNatureOrder = false;
  22. private $_docsRender = "default";
  23. /**
  24. * the server you are operating
  25. *
  26. * @var MServer
  27. */
  28. private static $_currentServer;
  29. private static $_servers = array();
  30. /**
  31. * Mongo connection object
  32. *
  33. * @var RMongo
  34. */
  35. private $_mongo;
  36. public function __construct(array $config) {
  37. foreach ($config as $param => $value) {
  38. switch ($param) {
  39. case "mongo_name":
  40. $this->_mongoName = $value;
  41. break;
  42. case "mongo_host":
  43. $this->_mongoHost = $value;
  44. break;
  45. case "mongo_sock":
  46. $this->_mongoSock = $value;
  47. break;
  48. case "mongo_port":
  49. $this->_mongoPort = $value;
  50. break;
  51. case "mongo_user":
  52. $this->_mongoUser = $value;
  53. break;
  54. case "mongo_pass":
  55. $this->_mongoPass = $value;
  56. break;
  57. case "mongo_auth":
  58. $this->_mongoAuth = $value;
  59. break;
  60. case "mongo_timeout":
  61. $this->_mongoTimeout = $value;
  62. break;
  63. case "mongo_db":
  64. $this->_mongoDb = $value;
  65. break;
  66. case "mongo_options":
  67. $this->_mongoOptions = $value;
  68. break;
  69. case "control_auth":
  70. $this->_controlAuth = $value;
  71. break;
  72. case "control_users":
  73. $this->_controlUsers = $value;
  74. break;
  75. case "ui_default_db":
  76. $this->_uiDefaultDb = $value;
  77. break;
  78. case "ui_only_dbs":
  79. $this->_uiOnlyDbs = $value;
  80. break;
  81. case "ui_hide_dbs":
  82. $this->_uiHideDbs = $value;
  83. break;
  84. case "ui_hide_collections":
  85. $this->_uiHideCollections = $value;
  86. break;
  87. case "ui_hide_system_collections":
  88. $this->_uiHideSystemCollections = $value;
  89. break;
  90. case "docs_nature_order":
  91. $this->_docsNatureOrder = $value;
  92. break;
  93. case "docs_render":
  94. $this->_docsRender = $value;
  95. break;
  96. }
  97. }
  98. if (empty($this->_mongoName)) {
  99. $this->_mongoName = $this->_mongoHost . ":" . $this->_mongoPort;
  100. }
  101. }
  102. public function mongoName() {
  103. return $this->_mongoName;
  104. }
  105. public function setMongoName($mongoName) {
  106. $this->_mongoName = $mongoName;
  107. }
  108. public function mongoAuth() {
  109. return $this->_mongoAuth;
  110. }
  111. public function setMongoAuth($mongoAuth) {
  112. $this->_mongoAuth = $mongoAuth;
  113. }
  114. public function mongoHost() {
  115. return $this->_mongoHost;
  116. }
  117. public function setMongoHost($mongoHost) {
  118. $this->_mongoHost = $mongoHost;
  119. }
  120. public function mongoPort() {
  121. return $this->_mongoPort;
  122. }
  123. public function setMongoPort($mongoPort) {
  124. $this->_mongoPort = $mongoPort;
  125. }
  126. public function mongoUser() {
  127. return $this->_mongoUser;
  128. }
  129. public function setMongoUser($mongoUser) {
  130. $this->_mongoUser = $mongoUser;
  131. }
  132. public function mongoPass() {
  133. return $this->_mongoPass;
  134. }
  135. public function setMongoPass($mongoPass) {
  136. $this->_mongoPass = $mongoPass;
  137. }
  138. public function mongoTimeout() {
  139. return $this->_mongoTimeout;
  140. }
  141. public function setMongoTimeout($timeout) {
  142. $this->_mongoTimeout = $timeout;
  143. }
  144. public function mongoDb() {
  145. return $this->_mongoDb;
  146. }
  147. public function setMongoDb($db) {
  148. $this->_mongoDb = $db;
  149. }
  150. public function controlAuth() {
  151. return $this->_controlAuth;
  152. }
  153. public function setControlAuth($controlAuth) {
  154. $this->_controlAuth = $controlAuth;
  155. }
  156. public function addControlUser($user, $pass) {
  157. $this->_controlUsers[$user] = $pass;
  158. }
  159. public function controlUsers() {
  160. return $this->_controlUsers;
  161. }
  162. public function setControlUsers(array $users) {
  163. $this->_controlUsers = $users;
  164. }
  165. public function uiOnlyDbs() {
  166. if (empty($this->_uiOnlyDbs)) {
  167. return array();
  168. }
  169. if (!is_array($this->_uiOnlyDbs)) {
  170. return preg_split("/\\s*,\\s*/", $this->_uiOnlyDbs);
  171. }
  172. return $this->_uiOnlyDbs;
  173. }
  174. public function setUIOnlyDbs($dbs) {
  175. $this->_uiOnlyDbs = $dbs;
  176. }
  177. public function uiHideDbs() {
  178. if (empty($this->_uiHideDbs)) {
  179. return array();
  180. }
  181. if (!is_array($this->_uiHideDbs)) {
  182. return preg_split("/\\s*,\\s*/", $this->_uiHideDbs);
  183. }
  184. return $this->_uiHideDbs;
  185. }
  186. public function setUIHideDbs($dbs) {
  187. $this->_uiHideDbs = $dbs;
  188. }
  189. public function uiHideCollections() {
  190. if (is_array($this->_uiHideCollections)) {
  191. return $this->_uiHideCollections;
  192. }
  193. return preg_split("/\\s*,\\s*/", $this->_uiHideCollections);
  194. }
  195. public function setUIHideCollections($collections) {
  196. $this->_uiHideCollections = $collections;
  197. }
  198. public function uiHideSystemCollections() {
  199. return $this->_uiHideSystemCollections;
  200. }
  201. public function setUIHideSystemCollections($bool) {
  202. $this->_uiHideSystemCollections = $bool;
  203. }
  204. /**
  205. * Set whether documents nature order
  206. *
  207. * @param boolean $bool true or false
  208. * @since 1.1.6
  209. */
  210. public function setDocsNatureOrder($bool) {
  211. $this->_docsNatureOrder = $bool;
  212. }
  213. /**
  214. * Whether documents are in nature order
  215. * @return boolean
  216. * @since 1.1.6
  217. */
  218. public function docsNatureOrder() {
  219. return $this->_docsNatureOrder;
  220. }
  221. /**
  222. * Set documents highlight render
  223. *
  224. * @param string $render can be "default" or "plain"
  225. * @since 1.1.6
  226. */
  227. public function setDocsRender($render) {
  228. $renders = array( "default", "plain" );
  229. if (in_array($render, $renders)) {
  230. $this->_docsRender = $render;
  231. }
  232. else {
  233. exit("docs_render should be either 'default' or 'plain'");
  234. }
  235. }
  236. /**
  237. * Get documents highlight render
  238. *
  239. * @return string
  240. * @since 1.1.6
  241. */
  242. public function docsRender() {
  243. return $this->_docsRender;
  244. }
  245. public function auth($username, $password, $db = "admin") {
  246. if ($db === "") {
  247. if (!$this->_mongoAuth && $this->_mongoDb) {
  248. $db = $this->_mongoDb;
  249. }
  250. else {
  251. $db = "admin";
  252. }
  253. }
  254. $server = null;
  255. if ($this->_mongoSock) {//connect through sock
  256. $server = $this->_mongoSock;
  257. }
  258. else {//connect through host:port
  259. $server = $this->_mongoHost . ":" . $this->_mongoPort;
  260. }
  261. if (!$this->_mongoPort) {
  262. $server = $this->_mongoHost;
  263. }
  264. try {
  265. $options = $this->_mongoOptions;
  266. if ($this->_mongoAuth) {
  267. $options["username"] = $username;
  268. $options["password"] = $password;
  269. $options["db"] = $db;
  270. }
  271. //after 1.2.11 use options to authenticate
  272. if(!$this->_mongoAuth && !empty($this->_mongoUser) && !empty($this->_mongoPass) && RMongo::compareVersion("1.2.11") > 0) {
  273. $options["username"] = $this->_mongoUser;
  274. $options["password"] = $this->_mongoPass;
  275. if (!empty($this->_mongoDb)) {
  276. $options["db"] = $this->_mongoDb;
  277. }
  278. }
  279. $this->_mongo = new RMongo($server, $options);
  280. $this->_mongo->setSlaveOkay(true);
  281. }
  282. catch(Exception $e) {
  283. if (preg_match("/authenticate/i", $e->getMessage())) {
  284. return false;
  285. }
  286. echo "Unable to connect MongoDB, please check your configurations. MongoDB said:" . $e->getMessage() . ".";
  287. exit();
  288. }
  289. // changing timeout to the new value
  290. if (RMongo::compareVersion("1.5.0") < 0) {
  291. MongoCursor::$timeout = $this->_mongoTimeout;
  292. }
  293. //auth by mongo
  294. if ($this->_mongoAuth) {
  295. // "authenticate" can only be used between 1.0.1 - 1.2.11
  296. if (RMongo::compareVersion("1.0.1") >= 0 && RMongo::compareVersion("1.2.11") < 0) {
  297. $dbs = $db;
  298. if (!is_array($dbs)) {
  299. $dbs = preg_split("/\\s*,\\s*/", $dbs);
  300. }
  301. foreach ($dbs as $db) {
  302. $ret = $this->_mongo->selectDb($db)->authenticate($username, $password);
  303. if (!$ret["ok"]) {
  304. return false;
  305. }
  306. }
  307. }
  308. }
  309. //auth by rock
  310. else if ($this->_controlAuth) {
  311. if (!isset($this->_controlUsers[$username]) || $this->_controlUsers[$username] != $password) {
  312. return false;
  313. }
  314. //authenticate
  315. if (!empty($this->_mongoUser)) {
  316. // "authenticate" can only be used between 1.0.1 - 1.2.11
  317. if (RMongo::compareVersion("1.0.1") >= 0 && RMongo::compareVersion("1.2.11") < 0) {
  318. return $this->_mongo
  319. ->selectDB($db)
  320. ->authenticate($this->_mongoUser, $this->_mongoPass);
  321. }
  322. }
  323. }
  324. else {
  325. //authenticate
  326. if (!empty($this->_mongoUser)) {
  327. // "authenticate" can only be used between 1.0.1 - 1.2.11
  328. if (RMongo::compareVersion("1.0.1") >= 0 && RMongo::compareVersion("1.2.11") < 0) {
  329. return $this->_mongo
  330. ->selectDB($db)
  331. ->authenticate($this->_mongoUser, $this->_mongoPass);
  332. }
  333. }
  334. }
  335. return true;
  336. }
  337. /**
  338. * Current Mongo object
  339. *
  340. * @return Mongo
  341. */
  342. public function mongo() {
  343. return $this->_mongo;
  344. }
  345. /**
  346. * List databases on the server
  347. *
  348. * @return array
  349. */
  350. public function listDbs() {
  351. $dbs = array();
  352. try {
  353. $dbs = $this->_mongo->listDBs();
  354. } catch (Exception $e) {
  355. $dbs["ok"] = false;
  356. }
  357. if (!$dbs["ok"]) {
  358. $user = MUser::userInSession();
  359. $dbs = array(
  360. "databases" => array(),
  361. "totalSize" => 0,
  362. "ok" => 1
  363. );
  364. foreach ($user->dbs() as $db) {
  365. $dbs["databases"][] = array( "name" => $db, "empty" => false, "sizeOnDisk" => 0);
  366. }
  367. }
  368. //@todo: should we show user input databases only?
  369. $onlyDbs = $this->uiOnlyDbs();
  370. $hideDbs = $this->uiHideDbs();
  371. foreach ($dbs["databases"] as $index => $database) {
  372. $name = $database["name"];
  373. if (!empty($hideDbs) && in_array($name, $hideDbs)) {
  374. unset($dbs["databases"][$index]);
  375. }
  376. if (!empty($onlyDbs) && !in_array($name, $onlyDbs)) {
  377. unset($dbs["databases"][$index]);
  378. }
  379. }
  380. return $dbs;
  381. }
  382. /**
  383. * Construct mongo server connection URI
  384. *
  385. * @return string
  386. */
  387. public function uri() {
  388. $host = null;
  389. if ($this->_mongoSock) {
  390. $host = $this->_mongoSock;
  391. } else {
  392. $host = $this->_mongoHost . ":" . $this->_mongoPort;
  393. }
  394. if ($this->_mongoAuth) {
  395. $user = MUser::userInSession();
  396. return $user->username() . ":" . $user->password() . "@" . $host;
  397. }
  398. if (empty($this->_mongoUser)) {
  399. return 'mongodb://' . $host;
  400. }
  401. return 'mongodb://' . $this->_mongoUser . ":" . $user->_mongoPass . "@" . $host;
  402. }
  403. /**
  404. * Should we hide the collection
  405. *
  406. * @param unknown_type $collection collection name
  407. * @return boolean
  408. */
  409. public function shouldHideCollection($collection) {
  410. foreach ($this->uiHideCollections() as $v) {
  411. if (preg_match("/^" . $v . "$/", $collection)) {
  412. return true;
  413. }
  414. }
  415. return false;
  416. }
  417. /**
  418. * Enter description here ...
  419. *
  420. * @param unknown_type $hostIndex
  421. * @return MServer
  422. */
  423. public static function serverWithIndex($hostIndex) {
  424. global $MONGO;
  425. if (!isset($MONGO["servers"][$hostIndex])) {
  426. return null;
  427. }
  428. if (!isset(self::$_servers[$hostIndex])) {
  429. self::$_servers[$hostIndex] = new MServer($MONGO["servers"][$hostIndex]);
  430. }
  431. self::$_currentServer = self::$_servers[$hostIndex];
  432. return self::$_servers[$hostIndex];
  433. }
  434. /**
  435. * Enter description here ...
  436. *
  437. * @return MServer
  438. */
  439. public static function currentServer() {
  440. return self::$_currentServer;
  441. }
  442. }
  443. ?>