Example #1
0
 /**
  * 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);
             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());
     }
 }
Example #2
0
 /**
  * Handles a JSON RPC 2 request to trash a bean.
  *
  * @param string $id       request ID, identification for request
  * @param string $beanType type of the bean you want to delete
  * @param array  $data     data array
  *
  * @return string
  */
 private function trash($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]);
     RedBean_Facade::trash($bean);
     return $this->resp('OK', $id);
 }
Example #3
0
 public function handleJSONRequest($jsonString)
 {
     $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");
     }
     if (!isset($jsonArray["id"])) {
         return $this->resp(null, null, -32600, "No ID");
     }
     $id = $jsonArray["id"];
     if (!isset($jsonArray["method"])) {
         return $this->resp(null, $id, -32600, "No method");
     }
     if (!isset($jsonArray["params"])) {
         $data = array();
     } else {
         $data = $jsonArray["params"];
     }
     $method = explode(":", trim($jsonArray["method"]));
     if (count($method) != 2) {
         return $this->resp(null, $id, -32600, "Invalid method signature. Use: BEAN:ACTION");
     }
     $beanType = $method[0];
     $action = $method[1];
     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);
                 break;
             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);
                 break;
             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);
                 break;
             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());
     }
 }
Example #4
0
});
$app->delete('/:package/:name/:id', 'API', 'CHECKTOKEN', 'RATELIMITER', function ($package, $name, $id) use($r) {
    $tableName = $r->genTableName($package, $name);
    if (!$r->packageOK($package, 'remove') && $tableName !== 'managepackages') {
        return $r->respond(400, 'BAD REQUEST', true);
    }
    $data = R::findOne($tableName, 'id = ?', array($id));
    if ($data) {
        $existingSyncMeta = R::findOne('syncmeta', 'where row_id = ? and tableName = ?', array($id, $tableName));
        if ($existingSyncMeta) {
            $existingSyncMeta->type = 'remove';
            $existingSyncMeta->timestamp = date('Y-m-d H:i:s');
            R::store($existingSyncMeta);
        }
        if ($r->fireHookIfExists($package, $name, 'beforeRemove', $r->unserialize(array($data->export()))[0])) {
            R::trash($data);
            $r->fireHookIfExists($package, $name, 'afterRemove', $r->unserialize(array($data->export()))[0]);
            return $r->respond(200, 'DELETED');
        }
        return $r->respond(403, 'FORBIDDEN:HOOK', true);
    }
    return $r->respond(404, 'NOT FOUND', true);
});
/* Handle Options Route */
$app->options('/:any+', 'API', function () use($app, $r) {
    return $r->respond(200);
});
/* default 404 and Error Handler */
$app->error('API', function (\Exception $e) use($app) {
    return $r->respond(500, $e, true);
});
Example #5
0
 public function delete($_entity = false)
 {
     $entity = !$_entity ? $this->entity : $_entity;
     R::trash($entity);
     return $this;
 }