public function req_post() { $action = isset($_POST['action']) ? $_POST['action'] : false; // Grant user access to an application if ($action === 'grant-app-access') { $bucket_id = $this->app->request->get('bucket'); if (!$bucket_id) { return $this->alert('You need to select a Bucket', 'danger'); } $bucket = BucketModel::findById($bucket_id); if (!$bucket) { return $this->alert('Invalid Bucket ID', 'danger'); } $role = $this->app->request->get('role'); if (!in_array($role, array('read', 'write', 'admin', 'owner'))) { return $this->alert('Invalid Role. Please select read, write, admin or owner', 'danger'); } $bucket->addRole($this->id, $role); $this->alert('<strong>' . $this->user->email . '</strong> was granted <strong>' . $role . '</strong> permission to <strong>' . $bucket->id . '</strong>'); } // Change Password if ($action === 'change-password') { $password = $this->app->request->get('password'); if (!$password || strlen($password) < 4) { return $this->alert('Password must be >= 4 chars', 'danger'); } $saved = $this->user->setPassword($password); if ($saved === 0) { $this->user->save(); $this->alert('Password updated', 'success'); } else { $this->alert($saved, 'danger'); } } }
public function before() { // Require Authentication if (!$this->isLoggedIn()) { header('Location: /login/'); exit; } // Get app key $this->id = isset($this->uri[1]) ? $this->uri[1] : ''; if (!$this->id) { header('Location: /'); exit; } $this->bucket = BucketModel::findById($this->id); if ($this->bucket === null) { // LEGACY: Check mongoid from legacy systems $this->bucket = BucketModel::findById(new \MongoId($this->id)); if ($this->bucket === null) { header('Location: /'); exit; } } // Check if legacy if ($this->bucket->legacy) { $this->alert('This bucket is running in legacy mode. Please upgrade!'); } // Check action $app_action = isset($this->uri[2]) ? $this->uri[2] : ''; $collection = $this->app->mongo->selectCollection(BucketModel::$collection); switch ($app_action) { case 'save': $alias_string = $this->app->request->get('alias'); $explode = explode(',', $alias_string); $aliases = array(); foreach ($explode as $alias) { $alias = trim($alias); if (preg_match(BucketModel::$regex_id, $alias)) { $aliases[] = $alias; } } $this->bucket->alias = $aliases; $this->bucket->description = $this->app->request->get('description'); $this->bucket->save(); break; case 'delete': $collection->remove(array('_id' => $this->bucket->id)); $this->app->mongo->selectCollection($this->bucket->event_collection)->drop(); header('Location: /'); exit; break; case 'empty': $this->app->mongo->selectCollection($this->bucket->event_collection)->drop(); header('Location: /bucket/' . $this->id); exit; break; } }
public function req_get() { header('Content-Type: text/plain'); $req = $this->app->request; $params = array_merge($req->query->all(), $req->request->all()); $format = 'json'; $dataload = $req->get('data') ? json_decode(urldecode($req->get('data')), true) : array(); $payload = $req->get('payload') ? json_decode(urldecode($req->get('payload')), true) : array(); $data = array_merge($dataload, $payload); // Event is required $event = $req->get('event') ?: (isset($data['event']) ? $data['event'] : (!empty($this->uri[1]) ? $this->uri[1] : false)); if (!$event) { echo '500 No Event Specified'; exit; } // Get Bucket Instance if ($req->get('bucket')) { $bucket_id = $req->get('bucket'); } else { $bucket_id = array_key_exists('appkey', $params) ? $params['appkey'] : (array_key_exists('appkey', $data) ? $data['appkey'] : false); } $bucket_id = trim($bucket_id); if (!$bucket_id) { echo '500 No Bucket ID Specified'; exit; } $bucket = Bucket::findById($bucket_id); if (!$bucket) { echo '500 Invalid Bucket ID'; exit; } // Normalize Data unset($data['event']); unset($data['appkey']); unset($data['bucket']); unset($data['sig'], $data['hash']); // Append to data $insert = array(); $insert['t'] = new \MongoDate(); $insert['e'] = $event; $insert['d'] = $data; // Save Data to log try { $collection = $this->app->mongo->selectCollection($bucket->event_collection); $collection->insert($insert); echo $insert['_id']; exit; } catch (MongoConnectionException $e) { echo '503 Database Exception'; exit; } exit; }
public function exec() { $debug = $this->app->request->get('debug') ? true : false; // Get JSON Body if ($this->app->request->getMethod() === 'POST') { $postBody = file_get_contents('php://input'); if (!$postBody) { return $this->error('No data', 400); } $payload_data = json_decode($postBody, true); } else { $payload_data = json_decode($this->app->request->get('payload') ?: '', true); } // Convert into standardized payload $payload = new Payload($payload_data); if (!$payload->isVersionSupported()) { return $this->error('Payload version ' . $payload->version . ' not supported'); } // Verify Event Credentials if (!$payload->event) { return $this->error('No Event specified', 400); } // Verify Bucket Credentials if (!$payload->bucket) { return $this->error('No Bucket specified', 400); } $bucket_id = $payload->bucket; $bucket = Bucket::findById($bucket_id); if ($bucket === null) { return $this->error('Invalid Bucket', 404); } // Save Event $insert = array(); $insert['t'] = $payload->time; $insert['e'] = $payload->event; $insert['d'] = $payload->data; // Save Data to log $id = null; try { $collection = $this->app->mongo->selectCollection($bucket->event_collection); $collection->insert($insert); $id = (string) $insert['_id']; } catch (\Exception $e) { // TODO: Queue event for later processing return $this->error('Database Exception', 503); } // Output Results $output = array('data' => array('id' => $id)); if ($debug) { $output['debug'] = array('payload' => $payload->asArray()); } return $output; }
public function req_get() { // Get bucket $this->bucket_id = $this->app->request->get('bucket'); if (!$this->bucket_id) { return $this->jsonError('No bucket specified'); } $this->bucket = Bucket::findById($this->bucket_id); if (!$this->bucket) { return $this->jsonError('Invalid Bucket ID', 404); } $this->collection = $this->app->mongo->selectCollection($this->bucket->event_collection); // Event filtering $this->event = $this->app->request->get('event'); // Custom Queries $query = $this->app->request->get('query'); if (is_string($query)) { $this->query = json_decode($query, true) ?: null; } // Verify input (TODO: Stronger validation and conversion) $period = $this->app->request->get('period') ?: false; $now = time(); $default_time_gap = 1800; $default_time_step = 60; // Per day if ($period === 'month' || $period === 18144000) { $default_time_gap = 18144000; $default_time_step = 86400; } elseif ($period === 'week' || $period === 604800) { $default_time_gap = 604800; $default_time_step = 86400; } elseif ($period === 'day' || $period === '86400') { $default_time_gap = 86400; $default_time_step = 3600; } elseif ($period === 'hour' || $period == '3600') { $default_time_gap = 3600; $default_time_step = 60; } elseif ($period === 'minute' || $period == '60') { $default_time_gap = 60; $default_time_step = 1; } elseif ((int) $period) { $default_time_gap = $period; $default_time_step = $default_time_gap / 30; } // Build time groups $this->time_step = (int) $this->app->request->get('step') ?: $default_time_step; $this->time_gap = $default_time_gap; // Make sure there aren't too many steps (possiblity of crashing stats engine) if ($this->time_gap / $this->time_step > 300) { $this->time_step = $this->time_gap / 300; } // Create time ranges $this->time_start = $now - $this->time_gap - ($now - $this->time_gap) % $this->time_step; $this->time_end = $now - $now % $this->time_step + $this->time_step; // Build Query $results = array(); $query = array(); if ($this->event) { $query['e'] = $this->event; } if ($this->query) { foreach ($this->query as $key => $val) { $query['d.' . $key] = $val; } } // Function $func = '$sum'; $func_inc = 1; $func_override = $this->app->request->get('func'); if ($func_override) { list($func_name, $func_var) = explode(':', $func_override); if ($func_name === 'avg') { $func = '$avg'; $func_inc = '$d.' . $func_var; } } // Loop over times for ($time = $this->time_start; $time < $this->time_end; $time += $this->time_step) { $query['t'] = array('$gte' => new \MongoDate($time), '$lte' => new \MongoDate($time + $this->time_step)); $op = array(array('$match' => $query), array('$group' => array('_id' => '$e', 'v' => array($func => $func_inc)))); $aggregate = $this->app->mongo->command(array('aggregate' => $this->collection->getName(), 'pipeline' => $op)); $count = 0; $result_arr = array(); foreach ($aggregate['result'] as $result) { $count += $result['v']; $result_arr[$result['_id']] = $result['v']; } // Calculate Average if ($func === '$avg') { if ($result_arr) { $count = round($count / count($result_arr), 2); } } $results[] = array('range' => array($time, $time + $this->time_step), 'count' => $count, 'events' => $result_arr); } // Output Results return $this->json($results, 200, array('range' => array($this->time_start, $this->time_end), 'step' => $this->time_step)); }
public function req_get() { header('Content-Type: text/plain'); $params = $_GET + $_POST; // Retrict Access to logged in users // if ( ! Auth::$id) // { // echo '{"error":"Authentication Required"}'; // exit; // } // App Key Required, Secret too in future $bucket_id = $this->app->request->get('bucket') ?: $this->uri[1]; if (empty($bucket_id)) { return $this->jsonError('Bucket ID is Required', 400); } $bucket = BucketModel::findById($bucket_id); if (!$bucket) { return $this->jsonError('Invalid Bucket ID', 404); } // Vars //$event = isset($this->uri[1]) ? $this->uri[1] : false; $event = $this->app->request->get('event') ?: ''; $limit = isset($params['limit']) ? (int) $params['limit'] : 10; if ($limit < 1) { $limit = 10; } // Where $where = array(); if ($event) { $where['e'] = $event; } /* if ($bucket) { $where['appkey'] = $bucket; } else { $app_keys = array(); foreach (Auth::$buckets as $k => $app) { $app_keys[] = $app['appkey']; } $where['appkey'] = array('$in' => $app_keys); } */ if (!empty($params['query'])) { $json = $this->json2array($params['query'], true); if (is_scalar($json) || !is_array($json)) { $json = array('$e' => $params['query']); } foreach ($json as $k => $v) { // Specials if ($k === '$e') { $where['e'] = $v; continue; } if (is_array($v)) { foreach ($v as $_k1 => $_v1) { if ($_k1 === '$regex') { $v[$_k1] = new MongoRegex($_v1); } } } $where['d.' . $k] = $v; } } // print_r($where); exit; if (isset($where['_id'])) { $where['_id'] = new MongoId($where['_id']); } // Fields $fields = array(); if (!empty($params['fields'])) { $explode = explode(',', $params['fields']); foreach ($explode as $field) { $field = trim($field); $fields['d.' . $field] = 1; } } if ($fields) { $fields['t'] = 1; $fields['e'] = 1; } // Sort $sort = array(); if (!empty($params['sort'])) { $json = $this->json2array($params['sort'], true); if (!$json) { preg_match('/^([^:]+)(:([\\-]*1))?$/', $params['sort'], $matches); if (isset($matches[1])) { $order = isset($matches[3]) && $matches[3] === '-1' ? -1 : 1; $json = array($matches[1] => $order); } } foreach ($json as $k => $v) { if ($k === '$time') { $sort['t'] = $v; } else { $sort["d." . $k] = $v; } } } if (!$sort) { $sort['t'] = -1; } // print_r($sort); // Find Data // Save Data to log try { $collection = $this->app->mongo->selectCollection($bucket->event_collection); try { $cursor = $collection->find($where, $fields)->limit($limit); if ($sort) { $cursor->sort($sort); } $data = array(); foreach ($cursor as $row) { $row['_id'] = (string) $row['_id']; // $row['date'] = (array) $row['t']; $data[] = $row; } echo json_encode($data); } catch (MongoCursorException $e) { echo '{"error":"Cursor Exception"}'; exit; } exit; } catch (MongoConnectionException $e) { echo '{"error":"Connection Exception"}'; exit; } // Output exit; }
public function exec() { $request = $this->app->request; // Bucket ID is required $bucket_id = $request->get('bucket'); if (empty($bucket_id)) { return $this->error('Bucket ID is Required', 400); } $bucket = Bucket::findById($bucket_id); if (!$bucket) { return $this->error('Invalid Bucket ID', 404); } // Grab request params $event = $request->get('event') ?: ''; $limit = (int) $request->get('limit') ?: 10; // Where $where_param = $request->get('where'); $where = array(); if ($event) { $where['e'] = $event; } if (!empty($where_param)) { $json = json_decode($where_param, true); if (is_scalar($json) || !is_array($json)) { $json = array('$e' => $where_param); } foreach ($json as $k => $v) { // Specials if ($k === '$e') { $where['e'] = $v; continue; } if (is_array($v)) { foreach ($v as $_k1 => $_v1) { if ($_k1 === '$regex') { $v[$_k1] = new MongoRegex($_v1); } } } $where['d.' . $k] = $v; } } // Fields $fields_param = $request->get('fields'); $fields = array(); if (!empty($fields_param)) { $explode = explode(',', $fields_param); foreach ($explode as $field) { $field = trim($field); $fields['d.' . $field] = 1; } } if ($fields) { $fields['t'] = 1; $fields['e'] = 1; } // Sorting $sort_param = $request->get('sort'); $sort = array(); if (!empty($sort_param)) { $json = json_decode($sort_param, true); if (!$json) { preg_match('/^([^:]+)(:([\\-]*1))?$/', $params['sort'], $matches); if (isset($matches[1])) { $order = isset($matches[3]) && $matches[3] === '-1' ? -1 : 1; $json = array($matches[1] => $order); } } foreach ($json as $k => $v) { if ($k === '$time') { $sort['t'] = $v; } else { $sort['d.' . $k] = $v; } } } if (!$sort) { $sort['t'] = -1; } // Grab from database try { $collection = $this->app->mongo->selectCollection($bucket->event_collection); try { $cursor = $collection->find($where, $fields)->limit($limit); if ($sort) { $cursor->sort($sort); } $data = array(); foreach ($cursor as $row) { $item = array('id' => (string) $row['_id'], 'event' => $row['e'], 'data' => $row['d'], 'time' => [$row['t']->sec, $row['t']->usec]); $data[] = $item; } } catch (MongoCursorException $e) { return $this->error('Database Write Error', 503); } } catch (MongoConnectionException $e) { return $this->error('Database Connection Error', 503); } // Output Results return array('data' => $data, 'meta' => array('where' => $where, 'fields' => $fields, 'limit' => $limit, 'sort' => $sort)); }