Very rough post list sketch; user list placeholder

This commit is contained in:
Marcin Kurczewski 2013-10-07 20:44:14 +02:00
parent c69ff872a9
commit 793d996166
6 changed files with 104 additions and 38 deletions

View file

@ -28,3 +28,5 @@ Kind regards,
[privileges] [privileges]
uploadPost=registered uploadPost=registered
listPosts=anonymous
listUsers=registered

View file

@ -7,7 +7,14 @@ class IndexController
*/ */
public function indexAction() public function indexAction()
{ {
$this->context->activeSection = 'home';
$this->context->subTitle = 'home'; $this->context->subTitle = 'home';
} }
/**
* @route /help
*/
public function helpAction()
{
$this->context->subTitle = 'help';
}
} }

View file

@ -26,7 +26,25 @@ class PostController
$this->context->subTitle = 'browsing posts'; $this->context->subTitle = 'browsing posts';
$this->context->searchQuery = $query; $this->context->searchQuery = $query;
throw new Exception('Not implemented');
PrivilegesHelper::confirmWithException($this->context->user, Privilege::ListPosts);
$page = 1;
$params = [];
$params[':limit'] = 20;
$params[':offset'] = ($page - 1) * $params[':limit'];
//todo safety
//todo construct WHERE based on filters
$whereSql = '';
//todo construct ORDER based on filers
$orderSql = 'ORDER BY upload_date DESC';
$limitSql = 'LIMIT :limit OFFSET :offset';
$posts = R::findAll('post', sprintf('%s %s %s', $whereSql, $orderSql, $limitSql), $params);
$this->context->transport->posts = $posts;
} }
/** /**
@ -51,7 +69,7 @@ class PostController
$suppliedTags = array_filter($suppliedTags); $suppliedTags = array_filter($suppliedTags);
$suppliedTags = array_unique($suppliedTags); $suppliedTags = array_unique($suppliedTags);
foreach ($suppliedTags as $tag) foreach ($suppliedTags as $tag)
if (!preg_match('/^\w+$/i', $tag)) if (!preg_match('/^[a-zA-Z0-9_-]+$/i', $tag))
throw new SimpleException('Invalid tag "' . $tag . '"'); throw new SimpleException('Invalid tag "' . $tag . '"');
$suppliedFile = $_FILES['file']; $suppliedFile = $_FILES['file'];
@ -95,9 +113,11 @@ class PostController
$dbPost = R::dispense('post'); $dbPost = R::dispense('post');
$dbPost->type = $postType; $dbPost->type = $postType;
$dbPost->name = $name; $dbPost->name = $name;
$dbPost->mimeType = $suppliedFile['type']; $dbPost->mime_type = $suppliedFile['type'];
$dbPost->safety = $suppliedSafety; $dbPost->safety = $suppliedSafety;
$dbPost->upload_date = time();
$dbPost->sharedTag = $dbTags; $dbPost->sharedTag = $dbTags;
$dbPost->ownUser = $this->context->user;
move_uploaded_file($suppliedFile['tmp_name'], $path); move_uploaded_file($suppliedFile['tmp_name'], $path);
R::store($dbPost); R::store($dbPost);
@ -109,12 +129,47 @@ class PostController
} }
/** /**
* Action that decorates the page containing the post.
* @route /post/{id} * @route /post/{id}
*/ */
public function showAction($id) public function viewAction($id)
{ {
$this->context->subTitle = 'showing @' . $id; $post = R::findOne('post', 'id = ?', [$id]);
throw new Exception('Not implemented'); if (!$post)
throw new SimpleException('Invalid post ID "' . $id . '"');
//todo: verify access rank...?
//todo: verify sketchy, nsfw, sfw
$this->context->subTitle = 'showing @' . $post->id;
$this->context->transport->post = $post;
}
/**
* Action that renders the requested file itself and sends it to user.
* @route /post/send/{name}
*/
public function sendAction($name)
{
$this->context->layoutName = 'layout-file';
$post = R::findOne('post', 'name = ?', [$name]);
if (!$post)
throw new SimpleException('Invalid post name "' . $name . '"');
//I guess access rank shouldn't be verified here. If someone arrives
//here, they already know the full name of the post (not just the ID)
//either by visiting the HTML container page or by having hotlink.
//Such users should be trusted.
$path = $this->config->main->filesPath . DIRECTORY_SEPARATOR . $post->name;
if (!file_exists($path))
throw new SimpleException('Post file does not exist');
if (!is_readable($path))
throw new SimpleException('Post file is not readable');
$this->context->transport->mimeType = $post->mimeType;
$this->context->transport->filePath = $path;
} }
/** /**
@ -123,5 +178,6 @@ class PostController
public function favoritesAction() public function favoritesAction()
{ {
$this->listAction('favmin:1'); $this->listAction('favmin:1');
$this->context->viewName = 'post-list';
} }
} }

View file

@ -2,4 +2,6 @@
class Privilege class Privilege
{ {
const UploadPost = 1; const UploadPost = 1;
const ListPosts = 2;
const ListUsers = 3;
} }

View file

@ -24,49 +24,44 @@
<div class="main-wrapper"> <div class="main-wrapper">
<ul> <ul>
<?php <?php
$preNav = []; $nav = [];
$postNav = [];
$nav []= ['Home', \Chibi\UrlHelper::route('index', 'index')];
$nav []= ['Browse', \Chibi\UrlHelper::route('post', 'list')];
if (PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts))
{
$nav []= ['Comments', \Chibi\UrlHelper::route('comment', 'list')];
$nav []= ['Favorites', \Chibi\UrlHelper::route('post', 'favorites')];
}
$preNav []= ['Home', \Chibi\UrlHelper::route('index', 'index')];
$preNav []= ['Browse', \Chibi\UrlHelper::route('post', 'list')];
$preNav []= ['Comments', \Chibi\UrlHelper::route('comment', 'list')];
$preNav []= ['Favorites', \Chibi\UrlHelper::route('post', 'favorites')];
if (PrivilegesHelper::confirm($this->context->user, Privilege::UploadPost)) if (PrivilegesHelper::confirm($this->context->user, Privilege::UploadPost))
$preNav []= ['Upload', \Chibi\UrlHelper::route('post', 'upload')]; $nav []= ['Upload', \Chibi\UrlHelper::route('post', 'upload')];
if (PrivilegesHelper::confirm($this->context->user, Privilege::ListUsers))
$nav []= ['Users', \Chibi\UrlHelper::route('user', 'list')];
if (!$this->context->loggedIn) if (!$this->context->loggedIn)
{ {
$postNav []= ['Log in', \Chibi\UrlHelper::route('auth', 'login')]; $nav []= ['Log in', \Chibi\UrlHelper::route('auth', 'login')];
$postNav []= ['Register', \Chibi\UrlHelper::route('auth', 'register')]; $nav []= ['Register', \Chibi\UrlHelper::route('auth', 'register')];
} }
else else
{ {
$postNav []= ['Account', \Chibi\UrlHelper::route('user', 'show', ['name' => $this->context->user->name])]; $nav []= ['My account', \Chibi\UrlHelper::route('user', 'show', ['name' => $this->context->user->name])];
$postNav []= ['Log out', \Chibi\UrlHelper::route('auth', 'logout')]; $nav []= ['Log out', \Chibi\UrlHelper::route('auth', 'logout')];
} }
if (!function_exists('printNav')) $nav []= ['Help', \Chibi\UrlHelper::route('index', 'help')];
foreach ($nav as $navItem)
{ {
function printNav($nav) list ($text, $link) = $navItem;
{ echo '<li>';
foreach ($nav as $navItem) echo '<a href="' . $link . '">' . $text . '</a>';
{ echo '</li>';
list ($text, $link) = $navItem;
echo '<li>';
echo '<a href="' . $link . '">' . $text . '</a>';
echo '</li>';
}
}
} }
?> ?>
<?php printNav($preNav); ?>
<li class="search">
<form action="<?php echo \Chibi\UrlHelper::route('post', 'list') ?>" method="get">
<input type="search" name="query" placeholder="search&hellip;" value="<?php if (isset($this->context->searchQuery)) echo $this->context->searchQuery ?>">
</form>
</li>
<?php printNav($postNav); ?>
</ul> </ul>
<div class="clear"></div> <div class="clear"></div>
</div> </div>

View file

@ -1 +1,5 @@
Todo: view posts <?php foreach ($this->context->transport->posts as $post): ?>
<a href="<?php echo \Chibi\UrlHelper::route('post', 'view', ['id' => $post->id]) ?>">
Post&nbsp;<?php echo $post->id; ?>
</a>
<?php endforeach ?>