<?php /** * Copyright (C) 2013 MoaDB * @license GPL v3 */ /** * moaDB Start */ session_start(); if (get_magic_quotes_gpc()) { $_GET = moaDB::stripslashes($_GET); $_POST = moaDB::stripslashes($_POST); } $hasDB = isset($_GET['db']) ? true : (isset($_POST['db']) ? true : false); //$_GET['db'] will default to admin $hasCollection = isset($_GET['collection']) ? true : false; if (!isset($_GET['db']) && !isset($_POST['newdb'])) { $_GET['db'] = moaModel::$dbName; } else { if (!isset($_GET['db']) && isset($_POST['newdb'])) { $_GET['db'] = $_POST['newdb']; } } try { moaController::$model = new moaModel($_GET['db']); } catch (Exception $e) { echo $e; exit(0); } $html = get::helper('html');
/** * Get the records in a collection * * @param string $collection * @return array */ public function listRows($collection) { foreach ($this->sort as $key => $val) { //cast vals to int $sort[$key] = (int) $val; } $col = $this->mongo->selectCollection($collection); $find = array(); if (isset($_GET['find']) && $_GET['find']) { $_GET['find'] = trim($_GET['find']); if (strpos($_GET['find'], 'array') === 0) { eval('$find = ' . $_GET['find'] . ';'); } else { if (is_string($_GET['find'])) { $findArr = json_decode($_GET['find'], true); if ($findArr) { $find = $findArr; } else { if (!$findArr && isset($_GET['find'])) { //Not valid JSON :) } } } } } $removequery = array(); if (isset($_GET['remove']) && $_POST['remove']) { $_POST['remove'] = trim($_POST['remove']); if (strpos($_POST['remove'], 'array') === 0) { eval('$remove = ' . $_POST['remove'] . ';'); } else { if (is_string($_POST['remove'])) { $removeArr = json_decode($_POST['remove'], true); if ($removeArr) { $removequery = $removeArr; } } } if (!empty($removequery)) { $col->remove($removequery); } } if (isset($_GET['search']) && $_GET['search']) { switch (substr(trim($_GET['search']), 0, 1)) { //first character case '/': //regex $find[$_GET['searchField']] = new mongoRegex($_GET['search']); break; case '{': //JSON if ($search = json_decode($_GET['search'], true)) { $find[$_GET['searchField']] = $search; } break; case '(': $types = array('bool', 'boolean', 'int', 'integer', 'float', 'double', 'string', 'array', 'object', 'null', 'mongoid'); $closeParentheses = strpos($_GET['search'], ')'); if ($closeParentheses) { $cast = strtolower(substr($_GET['search'], 1, $closeParentheses - 1)); if (in_array($cast, $types)) { $search = trim(substr($_GET['search'], $closeParentheses + 1)); if ($cast == 'mongoid') { $search = new MongoID($search); } else { settype($search, $cast); } $find[$_GET['searchField']] = $search; break; } } //else no-break //else no-break default: //text-search if (strpos($_GET['search'], '*') === false) { if (!is_numeric($_GET['search'])) { $find[$_GET['searchField']] = $_GET['search']; } else { //$_GET is always a string-type $in = array((string) $_GET['search'], (int) $_GET['search'], (double) $_GET['search']); $find[$_GET['searchField']] = array('$in' => $in); } } else { //text with wildcards $regex = '/' . str_replace('\\*', '.*', preg_quote($_GET['search'])) . '/i'; $find[$_GET['searchField']] = new mongoRegex($regex); } break; } } // Test document find // print_r($find); // $cursor = $col->find($find); // foreach ($cursor as $doc) { // var_dump($doc); // } exit; $cols = !isset($_GET['cols']) ? array() : array_fill_keys($_GET['cols'], true); $cur = $col->find($find, $cols)->sort($sort); $this->count = $cur->count(); //get keys of first object if ($_SESSION['limit'] && $this->count > $_SESSION['limit'] && (!isset($_GET['export']) || $_GET['export'] != 'nolimit')) { if ($this->count > 1) { $this->colKeys = moaDB::getArrayKeys($col->findOne()); } $cur->limit($_SESSION['limit']); if (isset($_GET['skip'])) { if ($this->count <= $_GET['skip']) { $_GET['skip'] = $this->count - $_SESSION['limit']; } $cur->skip($_GET['skip']); } } else { if ($this->count) { // results exist but are fewer than per-page limit $this->colKeys = moaDB::getArrayKeys($cur->getNext()); } else { if ($find && $col->count()) { //query is not returning anything, get cols from first obj in collection $this->colKeys = moaDB::getArrayKeys($col->findOne()); } } } //get keys of last or much-later object if ($this->count > 1) { $curLast = $col->find()->sort($sort); if ($this->count > 2) { $curLast->skip(min($this->count, 100) - 1); } $this->colKeys = array_merge($this->colKeys, moaDB::getArrayKeys($curLast->getNext())); ksort($this->colKeys); } return $cur; }