-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Add support for running raw SQL files #729
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Tqdev\PhpCrudApi\Controller; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Tqdev\PhpCrudApi\Middleware\Router\Router; | ||
use Tqdev\PhpCrudApi\Record\ErrorCode; | ||
use Tqdev\PhpCrudApi\Procedure\ProcedureService; | ||
use Tqdev\PhpCrudApi\RequestUtils; | ||
|
||
class ProcedureController | ||
{ | ||
private $service; | ||
private $responder; | ||
|
||
public function __construct(Router $router, Responder $responder, ProcedureService $service) | ||
{ | ||
$router->register('GET', '/procedures/*', array($this, 'file')); | ||
$router->register('POST', '/procedures/*', array($this, 'file')); | ||
$router->register('PUT', '/procedures/*', array($this, 'file')); | ||
$router->register('DELETE', '/procedures/*', array($this, 'file')); | ||
$this->service = $service; | ||
$this->responder = $responder; | ||
} | ||
|
||
public function file(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$file = RequestUtils::getPathSegment($request, 2); | ||
$operation = RequestUtils::getOperation($request); | ||
$queryParams = array_map(function($param) { | ||
return $param[0]; | ||
}, RequestUtils::getParams($request)); | ||
$bodyParams = (array) $request->getParsedBody(); | ||
$params = array_merge($queryParams, $bodyParams); | ||
if (!$this->service->hasProcedure($file, $operation)) { | ||
return $this->responder->error(ErrorCode::PROCEDURE_NOT_FOUND, $file); | ||
} | ||
return $this->responder->success($this->service->execute($file, $operation, $params)); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Tqdev\PhpCrudApi\Procedure; | ||
|
||
use Tqdev\PhpCrudApi\Database\GenericDB; | ||
|
||
class ProcedureService { | ||
private $db; | ||
private $procedurePath; | ||
|
||
public function __construct(GenericDB $db, string $procedurePath) | ||
{ | ||
$this->db = $db; | ||
$this->procedurePath = $procedurePath; | ||
} | ||
|
||
public function hasProcedure(string $procedureName, string $operation) { | ||
return file_exists('./' . $this->procedurePath . '/' . $procedureName . '.' .$operation . '.sql'); | ||
} | ||
|
||
public function execute(string $procedureName, string $operation, array $params = []) { | ||
$sql = $this->parseSqlTemplate($this->procedurePath . '/' . $procedureName . '.' . $operation . '.sql', $params); | ||
return $this->db->rawSql($sql, $params); | ||
} | ||
|
||
private function parseSqlTemplate(string $path, array $context) { | ||
ob_start(); | ||
extract($context); | ||
include($path); | ||
return ob_get_clean(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,17 @@ public static function getOperation(ServerRequestInterface $request): string | |
case 'PATCH': | ||
return 'increment'; | ||
} | ||
case 'procedures': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we merge this with the 'records' way of determining the operation? Or we implement that the operation is set to the verb in case of procedures. We could also support: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verb or 'records' are fine with me. If we go for 'records', how do we distinguish between |
||
switch ($method) { | ||
case 'POST': | ||
return 'write'; | ||
case 'GET': | ||
return 'read'; | ||
case 'PUT': | ||
return 'update'; | ||
case 'DELETE': | ||
return 'delete'; | ||
} | ||
} | ||
return 'unknown'; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
SELECT p.id, p.content, c.name | ||
FROM posts p | ||
INNER JOIN categories c ON c.id = p.category_id | ||
WHERE p.id = :id |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
SELECT p.id, p.content, c.name | ||
FROM posts p | ||
INNER JOIN categories c ON c.id = p.category_id | ||
WHERE p.id = <?= $id ?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a serious security issue, string concatenation in SQL with user input. Judging from your earlier comments and the different approach above, it seems that you are already aware that this is not the way to go. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, of course this is a particular bad example. But this feature (
Or
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we need to split the file into a head with some meta information and a body containing the sql query. The include is a risk as the $path variable should not contain (unchecked) user input to avoid path traversal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think path traversing is an issue at this point, because the path is limited to a single URL path segment. The value of
$path
at this location is defined as follows:The
getPathSegment()
blocks path traversing.However, some meta info would allow us to handle user input and server responses better. We could define the files like this:
./procedures/example.GET.php