From fd9433a2e3d67cf103723a1960ef28506ff2c6b1 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 25 Jan 2014 15:09:20 +0100 Subject: [PATCH] Edit tokens moved to model --- src/Controllers/PostController.php | 18 +----------------- src/Models/Entities/PostEntity.php | 17 +++++++++++++++++ src/Views/post-edit.phtml | 2 +- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index a2148335..561098b6 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -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); } diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index ce71a854..55eb51a3 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -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); + } } diff --git a/src/Views/post-edit.phtml b/src/Views/post-edit.phtml index 0f8d8e1c..25b88b4b 100644 --- a/src/Views/post-edit.phtml +++ b/src/Views/post-edit.phtml @@ -17,7 +17,7 @@
- + context->transport->post->getUploader()))): ?>