Skip to content

Commit

Permalink
Third fix
Browse files Browse the repository at this point in the history
  • Loading branch information
foobar1643 committed Jun 5, 2016
1 parent 469153e commit 593a756
Show file tree
Hide file tree
Showing 29 changed files with 653 additions and 403 deletions.
57 changes: 23 additions & 34 deletions app/Controller/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,40 @@

class CommentController
{
private $validator;
private $container;
private $commentHelper;
private $comment;
private $errors;

public function __construct(\Slim\Container $c)
{
$this->container = $c;
$this->validator = $c->get('Validation');
$this->commentHelper = $c->get('CommentHelper');
}

public function __invoke(Request $request, Response $response, $args)
{
$validator = $this->container->get('Validation');
$commentHelper = $this->container->get('CommentHelper');
$getVars = $request->getQueryParams();
$postVars = $request->getParsedBody();
$errors = $validator->validateCommentForm($args['id'], $postVars);
if(!$errors) { // there is no errors
$dateTime = new \DateTime("now");
$comment = new Comment();
$comment->setFileId($args['id'])
->setAuthor("Anonymous")
->setDatePosted($dateTime->format(\DateTime::ATOM))
->setCommentText($postVars['comment'])
->setParentId((isset($postVars['parentComment']) ? $postVars['parentComment'] : NULL));
$comment = $commentHelper->addComment($comment);
if(isset($getVars['ajax']) && $getVars['ajax'] == "true") {
$jsonResponse = ["errors" => false, "comment" => $comment->jsonSerialize()];
print(json_encode($jsonResponse));
return $response->withHeader('Content-Type', "application/json");
} else {
return $response->withHeader('Location', "/file/" . $args['id']);
}
} else {
if(isset($getVars['ajax']) && $getVars['ajax'] == "true") {
$jsonResponse = [
"errors" => $errors,
"parentId" => isset($postVars['parentComment']) ? $postVars['parentComment'] : NULL,
"comment" => isset($postVars['comment']) ? $postVars['comment'] : NULL
];
print(json_encode($jsonResponse));
return $response->withHeader('Content-Type', "application/json");
} else {
$fileController = new FileController($this->container);
$args['commentErrors'] = $errors;
$args['replyTo'] = isset($postVars['parentComment']) ? $postVars['parentComment'] : NULL;
return $fileController->viewFile($request, $response, $args);
}
$this->comment = $this->parsePostRequest($postVars, $args['id']);
$this->errors = $this->validator->validateComment($this->comment);
if(!$this->errors) {
$this->comment = $this->commentHelper->addComment($this->comment);
}
return ["errors" => $this->errors, "comment" => $this->comment];
}

public function parsePostRequest($postVars, $fileId)
{
$comment = new Comment();
$dateTime = new \DateTime("now");
$comment->setFileId($fileId)
->setAuthor("Anonymous")
->setDatePosted($dateTime->format(\DateTime::ATOM))
->setCommentText(isset($postVars['comment']) ? strval($postVars['comment']) : '')
->setParentId((isset($postVars['parentComment']) ? $postVars['parentComment'] : NULL));
return $comment;
}
}
38 changes: 18 additions & 20 deletions app/Controller/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,47 @@

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \Slim\Http\Stream as Stream;
use \Filehosting\Entity\File;
use \Filehosting\Helper\FileHelper;
use \Filehosting\Exception\FileNotFoundException;

class DownloadController
{
private $container;
private $fileMapper;
private $pathingHelper;
private $fileHelper;

public function __construct(\Slim\Container $c)
{
$this->container = $c;
$this->fileMapper = $c->get('FileMapper');
$this->pathingHelper = $c->get('PathingHelper');
$this->fileHelper = $c->get('FileHelper');
}

public function __invoke(Request $request, Response $response, $args)
{
$file = new File();
$fileMapper = $this->container->get('FileMapper');
$pathingHelper = $this->container->get('PathingHelper');
$config = $this->container->get('config');
$params = $request->getQueryParams();
$file = $fileMapper->getFile($args['id']);
$filePath = $pathingHelper->getPathToFile($file);
if($file != null && file_exists($filePath)) {
if($this->fileHelper->fileExists($args['id'])) {
$file = $this->fileMapper->getFile($args['id']);
$filePath = $this->pathingHelper->getPathToFile($file);
if(!isset($params['flag']) || $params['flag'] != 'nocount') {
$file->setDownloads($file->getDownloads() + 1);
$fileMapper->updateFile($file);
$this->fileMapper->updateFile($file);
}
$responseHeader = $response->withHeader('Content-Description', "File Transfer") // ->withHeader('Content-Disposition', "attachment; filename={$file->getName()}")
$response = $response->withHeader('Content-Description', "File Transfer")
->withHeader('Content-Type', "application/octet-stream")
->withHeader('Cache-Control', "must-revalidate")
->withHeader('Pragma', "public")
->withHeader('Content-Length', filesize($filePath));
if($config->getValue('app', 'enableXsendfile') == 1) {
if(strpos($_SERVER["SERVER_SOFTWARE"], "nginx") !== false) {
$responseHeader = $responseHeader->withHeader('X-Accel-Redirect', $pathingHelper->getXaccelPath($file))
->withHeader('X-Accel-Charset', "utf-8");
} else if(strpos($_SERVER["SERVER_SOFTWARE"], "apache") !== false && in_array("mod_xsendfile", apache_get_modules())) {
$responseHeader = $responseHeader->withHeader('X-Sendfile', $pathingHelper->getPathToFile($file));
}
} else {
readfile($filePath);
try {
$response = $this->fileHelper->getXsendfileHeaders($request, $response, $file);
} catch(\Exception $e) {
$fileStream = new Stream(fopen($filePath, "r"));
$response = $response->write($fileStream->getContents());
}
return $responseHeader;
return $response;
} else {
throw new \Slim\Exception\NotFoundException($request, $response);
}
Expand Down
67 changes: 38 additions & 29 deletions app/Controller/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,59 @@
class FileController
{
private $container;
private $view;
private $fileHelper;
private $fileMapper;
private $authHelper;
private $idHelper;
private $commentHelper;

public function __construct(\Slim\Container $c)
{
$this->container = $c;
$this->view = $c->get('view');
$this->fileHelper = $c->get('FileHelper');
$this->fileMapper = $c->get('FileMapper');
$this->commentHelper = $c->get('CommentHelper');
$this->authHelper = $c->get('AuthHelper');
$this->idHelper = $c->get('IdHelper');
}

public function viewFile(Request $request, Response $response, $args)
public function __invoke(Request $request, Response $response, $args)
{
$fileMapper = $this->container->get('FileMapper');
$commentHelper = $this->container->get('CommentHelper');
$authHelper = $this->container->get('AuthHelper');
$idHelper = $this->container->get('IdHelper');
$file = $fileMapper->getFile($args['id']);
if($file == false) {
$commentErrors = null;
$params = $request->getQueryParams();
$replyTo = isset($params['reply']) ? strval($params['reply']) : NULL;
$file = $this->fileMapper->getFile($args['id']);
if(!$this->fileHelper->fileExists($args['id'])) {
throw new \Slim\Exception\NotFoundException($request, $response);
} else {
$idHelper->analyzeFile($file);
if(isset($args['replyTo'])) {
$replyTo = $args['replyTo'];
} else {
$params = $request->getQueryParams();
$replyTo = isset($params['reply']) ? strval($params['reply']) : NULL;
}
if($request->isPost()) {
$commentController = new CommentController($this->container);
$postResult = $commentController->__invoke($request, $response, $args);
$commentErrors = $postResult["errors"];
$replyTo = isset($commentErrors) ? $postResult["comment"]->getParentId() : NULL;
if($request->isXhr()) {
return $response->withJson($postResult);
}
return $this->container->get('view')->render($response, 'file.twig', [
'file' => $file,
'idHelper' => $idHelper,
'replyTo' => $replyTo,
'commentErrors' => isset($args['commentErrors']) ? $args['commentErrors'] : NULL,
'csrf' => ["name" => $request->getAttribute('csrf_name'), "value" => $request->getAttribute('csrf_value')],
'authToken' => $authHelper->getUserToken($request),
'comments' => $commentHelper->getComments($file->getId())]);
}
return $this->view->render($response, 'file.twig', [
'file' => $file,
'idHelper' => $this->idHelper,
'fileInfo' => $this->idHelper->analyzeFile($file),
'replyTo' => $replyTo,
'commentErrors' => $commentErrors,
'canManageFile' => $this->authHelper->canManageFile($request, $file),
'comments' => $this->commentHelper->getComments($file->getId())]);
}

public function deleteFile(Request $request, Response $response, $args)
{
$formData = $request->getParsedBody();
$fileMapper = $this->container->get('FileMapper');
$fileHelper = $this->container->get('FileHelper');
$authHelper = $this->container->get('AuthHelper');
$file = $fileMapper->getFile($args['id']);
if($file != null && $authHelper->canDeleteFile($request, $file)) {
$fileHelper->deleteFile($file);
$file = $this->fileMapper->getFile($args['id']);
if($this->fileHelper->fileExists($args['id']) && $this->authHelper->canManageFile($request, $file)) {
$this->fileHelper->deleteFile($file);
}
return $response->withHeader('Location', "/");
return $response->withRedirect("/");
}
}
27 changes: 11 additions & 16 deletions app/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class SearchController
{
const RESULTS_PER_PAGE = 15;

private $container;
private $view;
private $searchHelper;

public function __construct(\Slim\Container $c)
{
$this->container = $c;
$this->view = $c->get('view');
$this->searchHelper = $c->get('SearchHelper');
}

public function __invoke(Request $request, Response $response, $args)
Expand All @@ -25,33 +27,26 @@ public function __invoke(Request $request, Response $response, $args)
$pager = null;
$error = false;
$searchResults = null;
$files = null;
if(isset($params["query"])) {
if(trim($params["query"]) != null) {
$searchGateway = $this->container->get('SearchGateway');
$fileMapper = $this->container->get('FileMapper');
$searchHelper = $this->container->get('SearchHelper');
if(trim($params["query"]) == null) {
$error = true;
} else {
$query = $params["query"];
$pager = new PaginationHelper(self::RESULTS_PER_PAGE, "/search?query={$query}");
if(isset($params["page"])) {
$page = $pager->checkPage($params["page"]);
}
$offset = $pager->getOffset($page);
$searchIds = $searchGateway->search($searchHelper->escapeString($query), self::RESULTS_PER_PAGE, $offset);
$searchMeta = $searchGateway->showMeta(); // results count
$pager->setTotalRecords($searchMeta[0]['Value']);
$filteredResults = $fileMapper->getFilteredFiles($searchIds);
$searchResults = $searchHelper->showDeleted($searchIds, $filteredResults);
} else {
$error = true;
$searchResults = $this->searchHelper->search($query, $offset, self::RESULTS_PER_PAGE);
$pager->setTotalRecords($searchResults["totalFound"]);
}
}
return $this->container->get('view')->render($response, 'search.twig', [
return $this->view->render($response, 'search.twig', [
'error' => $error,
"query" => $query,
"page" => intval($page),
"pager" => $pager,
"files" => $searchResults]
"files" => $searchResults["results"]]
);
}
}
38 changes: 22 additions & 16 deletions app/Controller/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,42 @@

class UploadController
{
private $container;
private $view;
private $fileHelper;
private $config;
private $validator;
private $authHelper;

public function __construct(\Slim\Container $c)
{
$this->container = $c;
$this->view = $c->get('view');
$this->config = $c->get('config');
$this->fileHelper = $c->get('FileHelper');
$this->authHelper = $c->get('AuthHelper');
$this->validator = $c->get('Validation');
}

public function __invoke(Request $request, Response $response, $args)
{
$errors = null;
if($request->isPost() && isset($_FILES)) {
$fileHelper = $this->container->get('FileHelper');
$validator = $this->container->get('Validation');
$authHelper = $this->container->get('AuthHelper');
if($request->isPost()) {
$uploadedFiles = $request->getUploadedFiles();
$file = new File();
$errors = $validator->validateUploadForm($_FILES);
$errors = $this->validator->validateUploadedFiles($uploadedFiles);
if(!$errors) {
if(!$authHelper->isAuthorized($request)) { // user is not authorized
$response = $authHelper->authorizeUser($response);
if(!$this->authHelper->isAuthorized($request)) {
$response = $this->authHelper->authorizeUser($response);
}
$file->setName($_FILES['filename']['name'])
->setOriginalName($_FILES['filename']['tmp_name'])
$file->setName($uploadedFiles["uploaded-file"]->getClientFilename())
->setUploadObject($uploadedFiles["uploaded-file"])
->setUploader('Anonymous')
->setAuthToken($authHelper->getUserToken($request));
$file = $fileHelper->createFile($file, false);
return $response->withHeader('Location', "/file/{$file->getId()}");
->setAuthToken($this->authHelper->getUserToken($request));
$file = $this->fileHelper->uploadFile($file, $uploadedFiles["uploaded-file"]);
return $response->withRedirect("/file/{$file->getId()}");
}
}
return $this->container->get('view')->render($response, 'upload.twig',
['sizeLimit' => $this->container->get('config')->getValue('app', 'sizeLimit'),
return $this->view->render($response, 'upload.twig',
['sizeLimit' => $this->config->getValue('app', 'sizeLimit'),
'errors' => $errors]);
}
}
10 changes: 9 additions & 1 deletion app/Database/CommentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ public function getComment($commentId)
return $query->fetch();
}

public function deleteComments($fileId)
public function deleteComment($commentId)
{
$query = $this->pdo->prepare("DELETE FROM comments WHERE parent_path LIKE :comment_id");
$query->bindValue(":comment_id", "%" . $commentId . "%", \PDO::PARAM_STR);
$query->execute();
return $query->rowCount();
}

public function purgeComments($fileId)
{
$query = $this->pdo->prepare("DELETE FROM comments WHERE file_id = :file_id_bind");
$query->bindValue(":file_id_bind", $fileId, \PDO::PARAM_INT);
Expand Down
Loading

0 comments on commit 593a756

Please sign in to comment.