Universal check for form submission

This commit is contained in:
Marcin Kurczewski 2013-10-21 23:50:30 +02:00
parent 90a75e4d30
commit 823888b0c1
10 changed files with 198 additions and 168 deletions

View file

@ -70,7 +70,7 @@ $(function()
aDom.addClass('inactive'); aDom.addClass('inactive');
var url = $(this).attr('href') + '?json'; var url = $(this).attr('href') + '?json';
$.get(url, function(data) $.get(url, {submit: 1}, function(data)
{ {
if (data['success']) if (data['success'])
{ {

View file

@ -82,6 +82,7 @@ $(function()
fd.append('tags', tags); fd.append('tags', tags);
fd.append('safety', safety); fd.append('safety', safety);
fd.append('source', source); fd.append('source', source);
fd.append('submit', 1);
var ajaxData = var ajaxData =
{ {

View file

@ -203,7 +203,7 @@ class PostController
if ($this->config->registration->needEmailForUploading) if ($this->config->registration->needEmailForUploading)
PrivilegesHelper::confirmEmail($this->context->user); PrivilegesHelper::confirmEmail($this->context->user);
if (!empty($_FILES['file']['name'])) if (InputHelper::get('submit'))
{ {
/* file contents */ /* file contents */
$suppliedFile = $_FILES['file']; $suppliedFile = $_FILES['file'];
@ -290,73 +290,71 @@ class PostController
{ {
$post = Model_Post::locate($id); $post = Model_Post::locate($id);
R::preload($post, ['uploader' => 'user']); R::preload($post, ['uploader' => 'user']);
$edited = false;
$this->context->transport->post = $post; $this->context->transport->post = $post;
/* safety */ if (InputHelper::get('submit'))
$suppliedSafety = InputHelper::get('safety');
if ($suppliedSafety !== null)
{ {
PrivilegesHelper::confirmWithException(Privilege::EditPostSafety, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); /* safety */
$suppliedSafety = Model_Post::validateSafety($suppliedSafety); $suppliedSafety = InputHelper::get('safety');
$post->safety = $suppliedSafety; if ($suppliedSafety !== null)
$edited = true; {
} PrivilegesHelper::confirmWithException(Privilege::EditPostSafety, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
$suppliedSafety = Model_Post::validateSafety($suppliedSafety);
$post->safety = $suppliedSafety;
$edited = true;
}
/* tags */ /* tags */
$suppliedTags = InputHelper::get('tags'); $suppliedTags = InputHelper::get('tags');
if ($suppliedTags !== null) if ($suppliedTags !== null)
{ {
PrivilegesHelper::confirmWithException(Privilege::EditPostTags, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); PrivilegesHelper::confirmWithException(Privilege::EditPostTags, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
$currentToken = self::serializeTags($post); $currentToken = self::serializeTags($post);
if (InputHelper::get('tags-token') != $currentToken) if (InputHelper::get('tags-token') != $currentToken)
throw new SimpleException('Someone else has changed the tags in the meantime'); throw new SimpleException('Someone else has changed the tags in the meantime');
$suppliedTags = Model_Tag::validateTags($suppliedTags); $suppliedTags = Model_Tag::validateTags($suppliedTags);
$dbTags = Model_Tag::insertOrUpdate($suppliedTags); $dbTags = Model_Tag::insertOrUpdate($suppliedTags);
$post->sharedTag = $dbTags; $post->sharedTag = $dbTags;
$edited = true; $edited = true;
} }
/* thumbnail */ /* thumbnail */
if (!empty($_FILES['thumb']['name'])) if (!empty($_FILES['thumb']['name']))
{ {
PrivilegesHelper::confirmWithException(Privilege::EditPostThumb, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); PrivilegesHelper::confirmWithException(Privilege::EditPostThumb, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
$suppliedFile = $_FILES['thumb']; $suppliedFile = $_FILES['thumb'];
self::handleUploadErrors($suppliedFile); self::handleUploadErrors($suppliedFile);
$mimeType = mime_content_type($suppliedFile['tmp_name']); $mimeType = mime_content_type($suppliedFile['tmp_name']);
if (!in_array($mimeType, ['image/gif', 'image/png', 'image/jpeg'])) if (!in_array($mimeType, ['image/gif', 'image/png', 'image/jpeg']))
throw new SimpleException('Invalid thumbnail type "' . $mimeType . '"'); throw new SimpleException('Invalid thumbnail type "' . $mimeType . '"');
list ($imageWidth, $imageHeight) = getimagesize($suppliedFile['tmp_name']); list ($imageWidth, $imageHeight) = getimagesize($suppliedFile['tmp_name']);
if ($imageWidth != $this->config->browsing->thumbWidth) if ($imageWidth != $this->config->browsing->thumbWidth)
throw new SimpleException('Invalid thumbnail width (should be ' . $this->config->browsing->thumbWidth . ')'); throw new SimpleException('Invalid thumbnail width (should be ' . $this->config->browsing->thumbWidth . ')');
if ($imageWidth != $this->config->browsing->thumbHeight) if ($imageWidth != $this->config->browsing->thumbHeight)
throw new SimpleException('Invalid thumbnail width (should be ' . $this->config->browsing->thumbHeight . ')'); throw new SimpleException('Invalid thumbnail width (should be ' . $this->config->browsing->thumbHeight . ')');
$path = $this->config->main->thumbsPath . DS . $post->name; $path = $this->config->main->thumbsPath . DS . $post->name;
move_uploaded_file($suppliedFile['tmp_name'], $path); move_uploaded_file($suppliedFile['tmp_name'], $path);
} }
/* source */ /* source */
$suppliedSource = InputHelper::get('source'); $suppliedSource = InputHelper::get('source');
if ($suppliedSource !== null) if ($suppliedSource !== null)
{ {
PrivilegesHelper::confirmWithException(Privilege::EditPostSource, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); PrivilegesHelper::confirmWithException(Privilege::EditPostSource, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
$suppliedSource = Model_Post::validateSource($suppliedSource); $suppliedSource = Model_Post::validateSource($suppliedSource);
$post->source = $suppliedSource; $post->source = $suppliedSource;
$edited = true; $edited = true;
} }
/* db storage */
if ($edited)
R::store($post); R::store($post);
$this->context->transport->success = true; $this->context->transport->success = true;
}
} }
@ -368,9 +366,12 @@ class PostController
{ {
$post = Model_Post::locate($id); $post = Model_Post::locate($id);
PrivilegesHelper::confirmWithException(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); PrivilegesHelper::confirmWithException(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
$post->hidden = true; if (InputHelper::get('submit'))
R::store($post); {
$this->context->transport->success = true; $post->hidden = true;
R::store($post);
$this->context->transport->success = true;
}
} }
/** /**
@ -380,9 +381,12 @@ class PostController
{ {
$post = Model_Post::locate($id); $post = Model_Post::locate($id);
PrivilegesHelper::confirmWithException(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); PrivilegesHelper::confirmWithException(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
$post->hidden = false; if (InputHelper::get('submit'))
R::store($post); {
$this->context->transport->success = true; $post->hidden = false;
R::store($post);
$this->context->transport->success = true;
}
} }
/** /**
@ -392,12 +396,15 @@ class PostController
{ {
$post = Model_Post::locate($id); $post = Model_Post::locate($id);
PrivilegesHelper::confirmWithException(Privilege::DeletePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); PrivilegesHelper::confirmWithException(Privilege::DeletePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader));
//remove stuff from auxiliary tables if (InputHelper::get('submit'))
$post->ownFavoritee = []; {
$post->sharedTag = []; //remove stuff from auxiliary tables
R::store($post); $post->ownFavoritee = [];
R::trash($post); $post->sharedTag = [];
$this->context->transport->success = true; R::store($post);
R::trash($post);
$this->context->transport->success = true;
}
} }
@ -410,18 +417,21 @@ class PostController
{ {
$post = Model_Post::locate($id); $post = Model_Post::locate($id);
R::preload($post, ['favoritee' => 'user']); R::preload($post, ['favoritee' => 'user']);
if (!$this->context->loggedIn)
throw new SimpleException('Not logged in');
foreach ($post->via('favoritee')->sharedUser as $fav)
if ($fav->id == $this->context->user->id)
throw new SimpleException('Already in favorites');
PrivilegesHelper::confirmWithException(Privilege::FavoritePost); PrivilegesHelper::confirmWithException(Privilege::FavoritePost);
$post->link('favoritee')->user = $this->context->user;
R::store($post); if (InputHelper::get('submit'))
$this->context->transport->success = true; {
if (!$this->context->loggedIn)
throw new SimpleException('Not logged in');
foreach ($post->via('favoritee')->sharedUser as $fav)
if ($fav->id == $this->context->user->id)
throw new SimpleException('Already in favorites');
$post->link('favoritee')->user = $this->context->user;
R::store($post);
$this->context->transport->success = true;
}
} }
/** /**
@ -432,22 +442,25 @@ class PostController
{ {
$post = Model_Post::locate($id); $post = Model_Post::locate($id);
R::preload($post, ['favoritee' => 'user']); R::preload($post, ['favoritee' => 'user']);
PrivilegesHelper::confirmWithException(Privilege::FavoritePost); PrivilegesHelper::confirmWithException(Privilege::FavoritePost);
if (!$this->context->loggedIn)
throw new SimpleException('Not logged in');
$finalKey = null; if (InputHelper::get('submit'))
foreach ($post->ownFavoritee as $key => $fav) {
if ($fav->user->id == $this->context->user->id) if (!$this->context->loggedIn)
$finalKey = $key; throw new SimpleException('Not logged in');
if ($finalKey === null) $finalKey = null;
throw new SimpleException('Not in favorites'); foreach ($post->ownFavoritee as $key => $fav)
if ($fav->user->id == $this->context->user->id)
$finalKey = $key;
unset ($post->ownFavoritee[$finalKey]); if ($finalKey === null)
R::store($post); throw new SimpleException('Not in favorites');
$this->context->transport->success = true;
unset ($post->ownFavoritee[$finalKey]);
R::store($post);
$this->context->transport->success = true;
}
} }

View file

@ -49,22 +49,25 @@ class TagController
public function mergeAction() public function mergeAction()
{ {
PrivilegesHelper::confirmWithException(Privilege::MergeTags); PrivilegesHelper::confirmWithException(Privilege::MergeTags);
$sourceTag = Model_Tag::locate(InputHelper::get('source-tag')); if (InputHelper::get('submit'))
$targetTag = Model_Tag::locate(InputHelper::get('target-tag'));
R::preload($sourceTag, 'post');
foreach ($sourceTag->sharedPost as $post)
{ {
foreach ($post->sharedTag as $key => $postTag) $sourceTag = Model_Tag::locate(InputHelper::get('source-tag'));
if ($postTag->id == $sourceTag->id) $targetTag = Model_Tag::locate(InputHelper::get('target-tag'));
unset($post->sharedTag[$key]);
$post->sharedTag []= $targetTag; R::preload($sourceTag, 'post');
R::store($post);
foreach ($sourceTag->sharedPost as $post)
{
foreach ($post->sharedTag as $key => $postTag)
if ($postTag->id == $sourceTag->id)
unset($post->sharedTag[$key]);
$post->sharedTag []= $targetTag;
R::store($post);
}
R::trash($sourceTag);
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('tag', 'list'));
$this->view->context->success = true;
} }
R::trash($sourceTag);
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('tag', 'list'));
$this->view->context->success = true;
} }
/** /**
@ -73,18 +76,20 @@ class TagController
public function renameAction() public function renameAction()
{ {
PrivilegesHelper::confirmWithException(Privilege::MergeTags); PrivilegesHelper::confirmWithException(Privilege::MergeTags);
if (InputHelper::get('submit'))
{
$suppliedSourceTag = InputHelper::get('source-tag');
$suppliedSourceTag = Model_Tag::validateTag($suppliedSourceTag);
$suppliedSourceTag = InputHelper::get('source-tag'); $suppliedTargetTag = InputHelper::get('target-tag');
$suppliedSourceTag = Model_Tag::validateTag($suppliedSourceTag); $suppliedTargetTag = Model_Tag::validateTag($suppliedTargetTag);
$suppliedTargetTag = InputHelper::get('target-tag'); $sourceTag = Model_Tag::locate($suppliedSourceTag);
$suppliedTargetTag = Model_Tag::validateTag($suppliedTargetTag); $sourceTag->name = $suppliedTargetTag;
R::store($sourceTag);
$sourceTag = Model_Tag::locate($suppliedSourceTag); \Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('tag', 'list'));
$sourceTag->name = $suppliedTargetTag; $this->context->transport->success = true;
R::store($sourceTag); }
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('tag', 'list'));
$this->context->transport->success = true;
} }
} }

View file

@ -128,9 +128,12 @@ class UserController
{ {
$user = Model_User::locate($name); $user = Model_User::locate($name);
PrivilegesHelper::confirmWithException(Privilege::BanUser, PrivilegesHelper::getIdentitySubPrivilege($user)); PrivilegesHelper::confirmWithException(Privilege::BanUser, PrivilegesHelper::getIdentitySubPrivilege($user));
$user->banned = true; if (InputHelper::get('submit'))
R::store($user); {
$this->context->transport->success = true; $user->banned = true;
R::store($user);
$this->context->transport->success = true;
}
} }
/** /**
@ -141,9 +144,12 @@ class UserController
{ {
$user = Model_User::locate($name); $user = Model_User::locate($name);
PrivilegesHelper::confirmWithException(Privilege::BanUser, PrivilegesHelper::getIdentitySubPrivilege($user)); PrivilegesHelper::confirmWithException(Privilege::BanUser, PrivilegesHelper::getIdentitySubPrivilege($user));
$user->banned = false; if (InputHelper::get('submit'))
R::store($user); {
$this->context->transport->success = true; $user->banned = false;
R::store($user);
$this->context->transport->success = true;
}
} }
/** /**
@ -154,14 +160,16 @@ class UserController
{ {
$user = Model_User::locate($name); $user = Model_User::locate($name);
PrivilegesHelper::confirmWithException(Privilege::AcceptUserRegistration); PrivilegesHelper::confirmWithException(Privilege::AcceptUserRegistration);
$user->staff_confirmed = true; if (InputHelper::get('submit'))
R::store($user); {
$this->context->transport->success = true; $user->staff_confirmed = true;
R::store($user);
$this->context->transport->success = true;
}
} }
/** /**
* @route /user/{name}/delete * @route /user/{name}/delete
* @validate name [^\/]+ * @validate name [^\/]+
@ -181,7 +189,7 @@ class UserController
$this->context->suppliedCurrentPassword = $suppliedCurrentPassword = InputHelper::get('current-password'); $this->context->suppliedCurrentPassword = $suppliedCurrentPassword = InputHelper::get('current-password');
if (InputHelper::get('remove')) if (InputHelper::get('submit'))
{ {
if ($this->context->user->id == $user->id) if ($this->context->user->id == $user->id)
{ {
@ -217,9 +225,7 @@ class UserController
{ {
try try
{ {
$user = Model_User::locate($name); $user = Model_User::locate($name);
$edited = false;
PrivilegesHelper::confirmWithException(Privilege::ViewUser, PrivilegesHelper::getIdentitySubPrivilege($user)); PrivilegesHelper::confirmWithException(Privilege::ViewUser, PrivilegesHelper::getIdentitySubPrivilege($user));
$this->context->handleExceptions = true; $this->context->handleExceptions = true;
@ -237,51 +243,47 @@ class UserController
$this->context->suppliedAccessRank = $suppliedAccessRank = InputHelper::get('access-rank'); $this->context->suppliedAccessRank = $suppliedAccessRank = InputHelper::get('access-rank');
$currentPasswordHash = $user->pass_hash; $currentPasswordHash = $user->pass_hash;
if ($suppliedName != '' and $suppliedName != $user->name) if (InputHelper::get('submit'))
{ {
PrivilegesHelper::confirmWithException(Privilege::ChangeUserName, PrivilegesHelper::getIdentitySubPrivilege($user)); if ($suppliedName != '' and $suppliedName != $user->name)
$suppliedName = Model_User::validateUserName($suppliedName);
$user->name = $suppliedName;
$edited = true;
}
if ($suppliedPassword1 != '')
{
PrivilegesHelper::confirmWithException(Privilege::ChangeUserPassword, PrivilegesHelper::getIdentitySubPrivilege($user));
if ($suppliedPassword1 != $suppliedPassword2)
throw new SimpleException('Specified passwords must be the same');
$suppliedPassword = Model_User::validatePassword($suppliedPassword1);
$user->pass_hash = Model_User::hashPassword($suppliedPassword, $user->pass_salt);
$edited = true;
}
if ($suppliedEmail != '' and $suppliedEmail != $user->email_confirmed)
{
PrivilegesHelper::confirmWithException(Privilege::ChangeUserEmail, PrivilegesHelper::getIdentitySubPrivilege($user));
$suppliedEmail = Model_User::validateEmail($suppliedEmail);
if ($this->context->user->id == $user->id)
{ {
$user->email_unconfirmed = $suppliedEmail; PrivilegesHelper::confirmWithException(Privilege::ChangeUserName, PrivilegesHelper::getIdentitySubPrivilege($user));
if (!empty($user->email_unconfirmed)) $suppliedName = Model_User::validateUserName($suppliedName);
self::sendEmailConfirmation($user); $user->name = $suppliedName;
} }
else
if ($suppliedPassword1 != '')
{ {
$user->email_confirmed = $suppliedEmail; PrivilegesHelper::confirmWithException(Privilege::ChangeUserPassword, PrivilegesHelper::getIdentitySubPrivilege($user));
if ($suppliedPassword1 != $suppliedPassword2)
throw new SimpleException('Specified passwords must be the same');
$suppliedPassword = Model_User::validatePassword($suppliedPassword1);
$user->pass_hash = Model_User::hashPassword($suppliedPassword, $user->pass_salt);
} }
$edited = true;
}
if ($suppliedAccessRank != '' and $suppliedAccessRank != $user->access_rank) if ($suppliedEmail != '' and $suppliedEmail != $user->email_confirmed)
{ {
PrivilegesHelper::confirmWithException(Privilege::ChangeUserAccessRank, PrivilegesHelper::getIdentitySubPrivilege($user)); PrivilegesHelper::confirmWithException(Privilege::ChangeUserEmail, PrivilegesHelper::getIdentitySubPrivilege($user));
$suppliedAccessRank = Model_User::validateAccessRank($suppliedAccessRank); $suppliedEmail = Model_User::validateEmail($suppliedEmail);
$user->access_rank = $suppliedAccessRank; if ($this->context->user->id == $user->id)
$edited = true; {
} $user->email_unconfirmed = $suppliedEmail;
if (!empty($user->email_unconfirmed))
self::sendEmailConfirmation($user);
}
else
{
$user->email_confirmed = $suppliedEmail;
}
}
if ($suppliedAccessRank != '' and $suppliedAccessRank != $user->access_rank)
{
PrivilegesHelper::confirmWithException(Privilege::ChangeUserAccessRank, PrivilegesHelper::getIdentitySubPrivilege($user));
$suppliedAccessRank = Model_User::validateAccessRank($suppliedAccessRank);
$user->access_rank = $suppliedAccessRank;
}
if ($edited)
{
if ($this->context->user->id == $user->id) if ($this->context->user->id == $user->id)
{ {
$suppliedPasswordHash = Model_User::hashPassword($suppliedCurrentPassword, $user->pass_salt); $suppliedPasswordHash = Model_User::hashPassword($suppliedCurrentPassword, $user->pass_salt);
@ -291,7 +293,6 @@ class UserController
R::store($user); R::store($user);
$this->context->transport->success = true; $this->context->transport->success = true;
} }
} }
catch (Exception $e) catch (Exception $e)
{ {

View file

@ -81,6 +81,8 @@
<label class="left">Source:</label> <label class="left">Source:</label>
<div class="input-wrapper"><input type="text" name="source" placeholder="where did you get this from? (optional)"/></div> <div class="input-wrapper"><input type="text" name="source" placeholder="where did you get this from? (optional)"/></div>
</div> </div>
<input type="hidden" name="submit" value="1"/>
</form> </form>
</div> </div>
</div> </div>

View file

@ -252,6 +252,8 @@
</div> </div>
<?php endif ?> <?php endif ?>
<input type="hidden" name="submit" value="1"/>
<div> <div>
<label class="left">&nbsp;</label> <label class="left">&nbsp;</label>
<button type="submit">Submit</button> <button type="submit">Submit</button>

View file

@ -25,6 +25,8 @@
<div class="input-wrapper"><input type="text" name="target-tag" id="merge-target-tag"></div> <div class="input-wrapper"><input type="text" name="target-tag" id="merge-target-tag"></div>
</div> </div>
<input type="hidden" name="submit" value="1"/>
<div> <div>
<label class="left">&nbsp;</label> <label class="left">&nbsp;</label>
<button type="submit">Merge!</button> <button type="submit">Merge!</button>
@ -47,6 +49,8 @@
<div class="input-wrapper"><input type="text" name="target-tag" id="rename-target-tag"></div> <div class="input-wrapper"><input type="text" name="target-tag" id="rename-target-tag"></div>
</div> </div>
<input type="hidden" name="submit" value="1"/>
<div> <div>
<label class="left">&nbsp;</label> <label class="left">&nbsp;</label>
<button type="submit">Rename!</button> <button type="submit">Rename!</button>

View file

@ -1,4 +1,4 @@
<form action="<?php echo \Chibi\UrlHelper::route('user', 'delete', ['name' => $this->context->transport->user->name]) ?>" method="post" class="edit aligned" autocomplete="off" data-confirm-text="Are you sure you want to delete your account?"> <form action="<?php echo \Chibi\UrlHelper::route('user', 'delete', ['name' => $this->context->transport->user->name]) ?>" method="post" class="delete aligned" autocomplete="off" data-confirm-text="Are you sure you want to delete your account?">
<?php if ($this->context->user->id == $this->context->transport->user->id): ?> <?php if ($this->context->user->id == $this->context->transport->user->id): ?>
<div class="current-password"> <div class="current-password">
<label class="left" for="current-password">Current password:</label> <label class="left" for="current-password">Current password:</label>
@ -6,7 +6,7 @@
</div> </div>
<?php endif ?> <?php endif ?>
<input type="hidden" name="remove" value="1"/> <input type="hidden" name="submit" value="1"/>
<?php if ($this->context->transport->success === true): ?> <?php if ($this->context->transport->success === true): ?>
<p class="alert alert-success">Account settings updated!</p> <p class="alert alert-success">Account settings updated!</p>

View file

@ -50,6 +50,8 @@
</div> </div>
<?php endif ?> <?php endif ?>
<input type="hidden" name="submit" value="1"/>
<?php if ($this->context->transport->success === true): ?> <?php if ($this->context->transport->success === true): ?>
<p class="alert alert-success">Account settings updated! <?php if (!empty($this->context->mailSent)) echo 'You will be sent new e-mail address confirmation message soon.' ?></p> <p class="alert alert-success">Account settings updated! <?php if (!empty($this->context->mailSent)) echo 'You will be sent new e-mail address confirmation message soon.' ?></p>
<?php elseif (isset($this->context->transport->errorMessage)): ?> <?php elseif (isset($this->context->transport->errorMessage)): ?>