Edit tokens moved to model

This commit is contained in:
Marcin Kurczewski 2014-01-25 15:09:20 +01:00
parent be3b39bf42
commit fd9433a2e3
3 changed files with 19 additions and 18 deletions

View file

@ -8,21 +8,6 @@ class PostController
$callback();
}
private static function serializePost($post)
{
$x = [];
foreach ($post->getTags() as $tag)
$x []= TextHelper::reprTag($tag->name);
foreach ($post->getRelations() as $relatedPost)
$x []= TextHelper::reprPost($relatedPost);
$x []= $post->safety;
$x []= $post->source;
$x []= $post->fileHash;
natcasesort($x);
$x = join(' ', $x);
return md5($x);
}
private static function handleUploadErrors($file)
{
switch ($file['error'])
@ -261,7 +246,7 @@ class PostController
if (InputHelper::get('submit'))
{
$editToken = InputHelper::get('edit-token');
if ($editToken != self::serializePost($post))
if ($editToken != $post->getEditToken())
throw new SimpleException('This post was already edited by someone else in the meantime');
LogHelper::bufferChanges();
@ -485,7 +470,6 @@ class PostController
$this->context->transport->post = $post;
$this->context->transport->prevPostId = $prevPost ? $prevPost->id : null;
$this->context->transport->nextPostId = $nextPost ? $nextPost->id : null;
$this->context->transport->editToken = self::serializePost($post);
}

View file

@ -422,4 +422,21 @@ class PostEntity extends AbstractEntity
unlink($srcPath);
}
}
public function getEditToken()
{
$x = [];
foreach ($this->getTags() as $tag)
$x []= TextHelper::reprTag($tag->name);
foreach ($this->getRelations() as $relatedPost)
$x []= TextHelper::reprPost($relatedPost);
$x []= $this->safety;
$x []= $this->source;
$x []= $this->fileHash;
natcasesort($x);
$x = join(' ', $x);
return md5($x);
}
}

View file

@ -17,7 +17,7 @@
<label class="left" for="tags">Tags:</label>
<div class="input-wrapper"><input type="text" name="tags" id="tags" placeholder="enter some tags&hellip;" value="<?php echo join(',', array_map(function($tag) { return $tag->name; }, $this->context->transport->post->getTags())) ?>"/></div>
</div>
<input type="hidden" name="edit-token" id="edit-token" value="<?php echo $this->context->transport->editToken ?>"/>
<input type="hidden" name="edit-token" id="edit-token" value="<?php echo $this->context->transport->post->getEditToken() ?>"/>
<?php endif ?>
<?php if (PrivilegesHelper::confirm(Privilege::EditPostSource, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->post->getUploader()))): ?>