Skip to content

Commit bd3bad3

Browse files
committed
2 parents e747c3a + ef9b987 commit bd3bad3

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

mongodbadmin.php

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* @author Jonathan H. Wage <jonwage@gmail.com>
1818
*/
1919

20+
header('Pragma: no-cache');
21+
2022
$server = 'mongodb://localhost:27017';
2123
$options = array(
2224
'connect' => true
@@ -71,12 +73,12 @@ function linkDocumentReferences($mongo, $document)
7173
$ref = findMongoDbDocument($value['$id'], $_REQUEST['db'], $value['$ref'], true);
7274
}
7375

74-
$document[$key]['$ref'] = '<a href="'.$_SERVER['PHP_SELF'].'?db='.$_REQUEST['db'].'&collection='.$value['$ref'].'">'.$document[$key]['$ref'].'</a>';
76+
$document[$key]['$ref'] = '<a href="'.$_SERVER['PHP_SELF'].'?db='.$value['$db'].'&collection='.$value['$ref'].'">'.$document[$key]['$ref'].'</a>';
7577

7678
if ($ref['_id'] instanceof MongoId) {
77-
$document[$key]['$id'] = '<a href="'.$_SERVER['PHP_SELF'].'?db='.$_REQUEST['db'].'&collection='.$value['$ref'].'&id='.$value['$id'].'">'.$document[$key]['$id'].'</a>';
79+
$document[$key]['$id'] = '<a href="'.$_SERVER['PHP_SELF'].'?db='.$value['$db'].'&collection='.$value['$ref'].'&id='.$value['$id'].'">'.$document[$key]['$id'].'</a>';
7880
} else {
79-
$document[$key]['$id'] = '<a href="'.$_SERVER['PHP_SELF'].'?db='.$_REQUEST['db'].'&collection='.$value['$ref'].'&id='.$value['$id'].'&custom_id=1">'.$document[$key]['$id'].'</a>';
81+
$document[$key]['$id'] = '<a href="'.$_SERVER['PHP_SELF'].'?db='.$value['$db'].'&collection='.$value['$ref'].'&id='.$value['$id'].'&custom_id=1">'.$document[$key]['$id'].'</a>';
8082
}
8183
} else {
8284
$document[$key] = linkDocumentReferences($mongo, $value);
@@ -98,20 +100,26 @@ function prepareValueForMongoDB($value)
98100
$customId = isset($_REQUEST['custom_id']);
99101

100102
if (is_string($value)) {
103+
$value = preg_replace('/\'_id\' => \s*MongoId::__set_state\(array\(\s*\)\)/', '\'_id\' => new MongoId("' . (isset($_REQUEST['id']) ? $_REQUEST['id'] : '') . '")', $value);
104+
$value = preg_replace('/MongoId::__set_state\(array\(\s*\)\)/', 'new MongoId()', $value);
105+
$value = preg_replace('/MongoDate::__set_state\(array\(\s*\'sec\' => (\d+),\s*\'usec\' => \d+,\s*\)\)/m', 'new MongoDate($1)', $value);
106+
$value = preg_replace('/MongoBinData::__set_state\(array\(\s*\'bin\' => \'(.*?)\',\s*\'type\' => ([1,2,3,5,128]),\s*\)\)/m', 'new MongoBinData(\'$1\', $2)', $value);
107+
101108
eval('$value = ' . $value . ';');
109+
102110
if (!$value) {
103-
header('location: '.$_SERVER['HTTP_REFERER']);
111+
header('location: ' . $_SERVER['HTTP_REFERER'] . ($customId ? '&custom_id=1' : null));
104112
exit;
105113
}
106114
}
107115

108116
$prepared = array();
109117
foreach ($value as $k => $v) {
110-
if ($k === '_id' && $customId) {
118+
if ($k === '_id' && !$customId) {
111119
$v = new MongoId($v);
112-
}
120+
}
113121

114-
if ($k === '$id' && $customId) {
122+
if ($k === '$id' && !$customId) {
115123
$v = new MongoId($v);
116124
}
117125

@@ -165,7 +173,7 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
165173

166174
$collection = $mongo->selectDB($db)->selectCollection($collection);
167175

168-
if (isset($_REQUEST['custom_id']) && !$forceCustomId) {
176+
if (isset($_REQUEST['custom_id']) || $forceCustomId) {
169177
$document =$collection->findOne(array('_id' => $id));
170178
} else {
171179
$document = $collection->findOne(array('_id' => new MongoId($id)));
@@ -176,6 +184,27 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
176184

177185
// Actions
178186
try {
187+
// SEARCH
188+
if (isset($_REQUEST['search'])) {
189+
$customId = false;
190+
$document = findMongoDbDocument($_REQUEST['search'], $_REQUEST['db'], $_REQUEST['collection']);
191+
192+
if (!$document) {
193+
$document = findMongoDbDocument($_REQUEST['search'], $_REQUEST['db'], $_REQUEST['collection'], true);
194+
$customId = true;
195+
}
196+
197+
if (isset($document['_id'])) {
198+
$url = $_SERVER['PHP_SELF'] . '?db=' . $_REQUEST['db'] . '&collection=' . $_REQUEST['collection'] . '&id=' . (string) $document['_id'];
199+
200+
if ($customId) {
201+
header('location: ' . $url . '&custom_id=true');
202+
} else {
203+
header('location: ' . $url);
204+
}
205+
}
206+
}
207+
179208
// DELETE DB
180209
if (isset($_REQUEST['delete_db'])) {
181210
$mongo
@@ -248,13 +277,14 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
248277

249278
// INSERT OR UPDATE A DB COLLECTION DOCUMENT
250279
if (isset($_POST['save'])) {
280+
$customId = isset($_REQUEST['custom_id']);
251281
$collection = $mongo->selectDB($_REQUEST['db'])->selectCollection($_REQUEST['collection']);
252282

253283
$document = prepareValueForMongoDB($_REQUEST['value']);
254284
$collection->save($document);
255285

256286
$url = $_SERVER['PHP_SELF'] . '?db=' . $_REQUEST['db'] . '&collection=' . $_REQUEST['collection'] . '&id=' . (string) $document['_id'];
257-
header('location: ' . $url);
287+
header('location: ' . $url . ($customId ? '&custom_id=1' : null));
258288
exit;
259289
}
260290

@@ -344,6 +374,18 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
344374
padding: 8px;
345375
margin-bottom: 15px;
346376
width: 350px;
377+
float: left;
378+
}
379+
#search {
380+
-moz-border-radius: 10px;
381+
-webkit-border-radius: 10px;
382+
border-radius: 10px;
383+
background: #f5f5f5;
384+
border: 1px solid #ccc;
385+
padding: 8px;
386+
margin-bottom: 15px;
387+
width: 350px;
388+
float: right;
347389
}
348390
table {
349391
background: #333;
@@ -507,6 +549,14 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
507549
</div>
508550
<?php endif; ?>
509551

552+
<div id="search">
553+
<form action="<?php echo $_SERVER['PHP_SELF'] ?>?db=<?php echo $_REQUEST['db'] ?>&collection=<?php echo $_REQUEST['collection'] ?>" method="POST">
554+
<label for="search_input">Search by ID</label>
555+
<input type="text" id="search_input" name="search" size="20" />
556+
<input type="submit" name="submit_search" value="Search" />
557+
</form>
558+
</div>
559+
510560
<table>
511561
<thead>
512562
<th colspan="3">ID</th>
@@ -583,4 +633,3 @@ function findMongoDbDocument($id, $db, $collection, $forceCustomId = false)
583633
</div>
584634
</body>
585635
</html>
586-

0 commit comments

Comments
 (0)