$order_by[] = $order[0] . ' ' . $order[1]; } $order_by = implode(',', $order_by); $query->order_by($order_by); } $limits = json_decode($app->request()->params('limit')); if (is_array($limits)) { $query->limit(implode(', ', $limits)); } else { if (is_array($config['default_sql_limit']) && $tableName !== 'managepackages') { $query->limit(implode(', ', $config['default_sql_limit'])); } } $data = $query->get(); } catch (Exception $e) { $data = R::exportAll(R::findAll($tableName)); } $data = $r->unserialize($data); $data = $r->fireHookIfExists($package, $name, 'afterGet', $data); if ($data === false) { return $r->respond(403, 'FORBIDDEN:HOOK', true); } return $r->respond(200, $data); }); $app->get('/:package/:name/:id', 'API', 'CHECKTOKEN', 'RATELIMITER', function ($package, $name, $id) use($r) { $tableName = $r->genTableName($package, $name); if (!$r->packageOK($package, 'list') && $tableName !== 'managepackages') { return $r->respond(400, 'BAD REQUEST', true); } $data = R::findOne($tableName, 'id = ?', array($id)); if ($data) {
/** * Handles a JSON RPC 2 request to export a bean. * * @param string $id request ID, identification for request * @param string $beanType type of the bean you want to export * @param array $data data array * * @return string */ private function export($id, $beanType, $data) { if (!isset($data[0])) { return $this->resp(NULL, $id, self::C_JSONRPC2_INVALID_PARAMETERS, 'First param needs to be Bean ID'); } $bean = RedBean_Facade::load($beanType, $data[0]); $array = RedBean_Facade::exportAll(array($bean), TRUE); return $this->resp($array, $id); }
/** * Processes a JSON object request. * * @param array $jsonObject JSON request object * * @return mixed $result result */ public function handleJSONRequest($jsonString) { //Decode JSON string $jsonArray = json_decode($jsonString, true); if (!$jsonArray) { return $this->resp(null, null, -32700, 'Cannot Parse JSON'); } if (!isset($jsonArray['jsonrpc'])) { return $this->resp(null, null, -32600, 'No RPC version'); } if ($jsonArray['jsonrpc'] != '2.0') { return $this->resp(null, null, -32600, 'Incompatible RPC Version'); } //DO we have an ID to identify this request? if (!isset($jsonArray['id'])) { return $this->resp(null, null, -32600, 'No ID'); } //Fetch the request Identification String. $id = $jsonArray['id']; //Do we have a method? if (!isset($jsonArray['method'])) { return $this->resp(null, $id, -32600, 'No method'); } //Do we have params? if (!isset($jsonArray['params'])) { $data = array(); } else { $data = $jsonArray['params']; } //Check method signature $method = explode(':', trim($jsonArray['method'])); if (count($method) != 2) { return $this->resp(null, $id, -32600, 'Invalid method signature. Use: BEAN:ACTION'); } //Collect Bean and Action $beanType = $method[0]; $action = $method[1]; //May not contain anything other than ALPHA NUMERIC chars and _ if (preg_match('/\\W/', $beanType)) { return $this->resp(null, $id, -32600, 'Invalid Bean Type String'); } if (preg_match('/\\W/', $action)) { return $this->resp(null, $id, -32600, 'Invalid Action String'); } try { switch ($action) { case 'store': if (!isset($data[0])) { return $this->resp(null, $id, -32602, 'First param needs to be Bean Object'); } $data = $data[0]; if (!isset($data['id'])) { $bean = RedBean_Facade::dispense($beanType); } else { $bean = RedBean_Facade::load($beanType, $data['id']); } $bean->import($data); $rid = RedBean_Facade::store($bean); return $this->resp($rid, $id); case 'load': if (!isset($data[0])) { return $this->resp(null, $id, -32602, 'First param needs to be Bean ID'); } $bean = RedBean_Facade::load($beanType, $data[0]); return $this->resp($bean->export(), $id); case 'trash': if (!isset($data[0])) { return $this->resp(null, $id, -32602, 'First param needs to be Bean ID'); } $bean = RedBean_Facade::load($beanType, $data[0]); RedBean_Facade::trash($bean); return $this->resp('OK', $id); case 'export': if (!isset($data[0])) { return $this->resp(null, $id, -32602, 'First param needs to be Bean ID'); } $bean = RedBean_Facade::load($beanType, $data[0]); $array = RedBean_Facade::exportAll(array($bean), true); return $this->resp($array, $id); default: $modelName = $this->modelHelper->getModelName($beanType); if (!class_exists($modelName)) { return $this->resp(null, $id, -32601, 'No such bean in the can!'); } $beanModel = new $modelName(); if (!method_exists($beanModel, $action)) { return $this->resp(null, $id, -32601, "Method not found in Bean: {$beanType} "); } return $this->resp(call_user_func_array(array($beanModel, $action), $data), $id); } } catch (Exception $exception) { return $this->resp(null, $id, -32099, $exception->getCode() . '-' . $exception->getMessage()); } }