From b885411b2eff1ce5d633f505f0c43c91a4ef77cc Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 4 May 2014 20:09:03 +0200 Subject: [PATCH] Encapsulated a few entity getters and setters --- src/Api/Jobs/AddUserJob.php | 2 +- src/Api/Jobs/DeleteUserJob.php | 2 +- src/Api/Jobs/EditPostTagsJob.php | 4 +-- src/Api/Jobs/EditUserNameJob.php | 4 +-- src/Api/Jobs/FeaturePostJob.php | 2 +- src/Api/Jobs/GetPostContentJob.php | 4 +-- src/Api/Jobs/ListRelatedTagsJob.php | 2 +- src/Api/Jobs/TogglePostTagJob.php | 4 +-- src/Api/Jobs/ToggleUserBanJob.php | 5 ++- src/Auth.php | 18 +++++------ src/Controllers/PostController.php | 2 +- src/Controllers/TagController.php | 4 +-- src/Controllers/UserController.php | 4 +-- src/Helpers/TextHelper.php | 4 +-- src/Models/Entities/PostEntity.php | 24 +++++++++----- src/Models/Entities/TagEntity.php | 12 ++++++- src/Models/Entities/UserEntity.php | 31 +++++++++++++++++-- src/Models/PostModel.php | 4 +-- src/Models/SearchParsers/PostSearchParser.php | 2 +- src/Models/TagModel.php | 6 ++-- src/Models/UserModel.php | 6 ++-- src/Views/comment-small.phtml | 8 ++--- src/Views/post-edit.phtml | 2 +- src/Views/post-file-render.phtml | 12 +++---- src/Views/post-small.phtml | 2 +- src/Views/post-view.phtml | 22 ++++++------- src/Views/static-main.phtml | 10 +++--- src/Views/tag-list.phtml | 2 +- src/Views/top-navigation.phtml | 6 ++-- src/Views/user-delete.phtml | 2 +- src/Views/user-edit.phtml | 2 +- src/Views/user-list.phtml | 8 ++--- src/Views/user-settings.phtml | 2 +- src/Views/user-view.phtml | 30 +++++++++--------- 34 files changed, 151 insertions(+), 103 deletions(-) diff --git a/src/Api/Jobs/AddUserJob.php b/src/Api/Jobs/AddUserJob.php index 36188e13..4ba703c2 100644 --- a/src/Api/Jobs/AddUserJob.php +++ b/src/Api/Jobs/AddUserJob.php @@ -8,7 +8,7 @@ class AddUserJob extends AbstractJob $user = UserModel::spawn(); $user->joinDate = time(); $user->staffConfirmed = $firstUser; - $user->name = $this->getArgument(EditUserNameJob::NEW_USER_NAME); + $user->setName($this->getArgument(EditUserNameJob::NEW_USER_NAME)); UserModel::forgeId($user); $arguments = $this->getArguments(); diff --git a/src/Api/Jobs/DeleteUserJob.php b/src/Api/Jobs/DeleteUserJob.php index e4219b50..b2d5a223 100644 --- a/src/Api/Jobs/DeleteUserJob.php +++ b/src/Api/Jobs/DeleteUserJob.php @@ -5,7 +5,7 @@ class DeleteUserJob extends AbstractUserJob { $user = $this->user; - $name = $user->name; + $name = $user->getName(); UserModel::remove($user); Logger::log('{user} removed {subject}\'s account', [ diff --git a/src/Api/Jobs/EditPostTagsJob.php b/src/Api/Jobs/EditPostTagsJob.php index ef064f66..cdf0dd7e 100644 --- a/src/Api/Jobs/EditPostTagsJob.php +++ b/src/Api/Jobs/EditPostTagsJob.php @@ -6,9 +6,9 @@ class EditPostTagsJob extends AbstractPostEditJob $post = $this->post; $tags = $this->getArgument(self::TAG_NAMES); - $oldTags = array_map(function($tag) { return $tag->name; }, $post->getTags()); + $oldTags = array_map(function($tag) { return $tag->getName(); }, $post->getTags()); $post->setTagsFromText($tags); - $newTags = array_map(function($tag) { return $tag->name; }, $post->getTags()); + $newTags = array_map(function($tag) { return $tag->getName(); }, $post->getTags()); if (!$this->skipSaving) { diff --git a/src/Api/Jobs/EditUserNameJob.php b/src/Api/Jobs/EditUserNameJob.php index 55680ab1..d65caff8 100644 --- a/src/Api/Jobs/EditUserNameJob.php +++ b/src/Api/Jobs/EditUserNameJob.php @@ -8,11 +8,11 @@ class EditUserNameJob extends AbstractUserEditJob $user = $this->user; $newName = $this->getArgument(self::NEW_USER_NAME); - $oldName = $user->name; + $oldName = $user->getName(); if ($oldName == $newName) return $user; - $user->name = $newName; + $user->setName($newName); UserModel::validateUserName($user); if (!$this->skipSaving) diff --git a/src/Api/Jobs/FeaturePostJob.php b/src/Api/Jobs/FeaturePostJob.php index b1dfd43a..f18297d7 100644 --- a/src/Api/Jobs/FeaturePostJob.php +++ b/src/Api/Jobs/FeaturePostJob.php @@ -7,7 +7,7 @@ class FeaturePostJob extends AbstractPostJob PropertyModel::set(PropertyModel::FeaturedPostId, $post->id); PropertyModel::set(PropertyModel::FeaturedPostDate, time()); - PropertyModel::set(PropertyModel::FeaturedPostUserName, Auth::getCurrentUser()->name); + PropertyModel::set(PropertyModel::FeaturedPostUserName, Auth::getCurrentUser()->getName()); Logger::log('{user} featured {post} on main page', [ 'user' => TextHelper::reprPost(Auth::getCurrentUser()), diff --git a/src/Api/Jobs/GetPostContentJob.php b/src/Api/Jobs/GetPostContentJob.php index ae3d8c9a..823e65a0 100644 --- a/src/Api/Jobs/GetPostContentJob.php +++ b/src/Api/Jobs/GetPostContentJob.php @@ -13,7 +13,7 @@ class GetPostContentJob extends AbstractJob $post = $this->post; $config = getConfig(); - $path = $config->main->filesPath . DS . $post->name; + $path = $config->main->filesPath . DS . $post->getName(); $path = TextHelper::absolutePath($path); if (!file_exists($path)) throw new SimpleNotFoundException('Post file does not exist'); @@ -23,7 +23,7 @@ class GetPostContentJob extends AbstractJob $fileName = sprintf('%s_%s_%s.%s', $config->main->title, $post->id, - join(',', array_map(function($tag) { return $tag->name; }, $post->getTags())), + join(',', array_map(function($tag) { return $tag->getName(); }, $post->getTags())), TextHelper::resolveMimeType($post->mimeType) ?: 'dat'); $fileName = preg_replace('/[[:^print:]]/', '', $fileName); diff --git a/src/Api/Jobs/ListRelatedTagsJob.php b/src/Api/Jobs/ListRelatedTagsJob.php index 66f77ba2..8ee5b77c 100644 --- a/src/Api/Jobs/ListRelatedTagsJob.php +++ b/src/Api/Jobs/ListRelatedTagsJob.php @@ -10,7 +10,7 @@ class ListRelatedTagsJob extends ListTagsJob $tags = TagSearchService::getRelatedTags($tag); $tagCount = count($tags); - $tags = array_filter($tags, function($tag) use ($otherTags) { return !in_array($tag->name, $otherTags); }); + $tags = array_filter($tags, function($tag) use ($otherTags) { return !in_array($tag->getName(), $otherTags); }); $tags = array_slice($tags, 0, $pageSize); return $this->getPager($tags, $tagCount, $page, $pageSize); diff --git a/src/Api/Jobs/TogglePostTagJob.php b/src/Api/Jobs/TogglePostTagJob.php index 4baacae2..577f2e61 100644 --- a/src/Api/Jobs/TogglePostTagJob.php +++ b/src/Api/Jobs/TogglePostTagJob.php @@ -15,7 +15,7 @@ class TogglePostTagJob extends AbstractPostJob if ($tag === null) { $tag = TagModel::spawn(); - $tag->name = $tagName; + $tag->setName($tagName); TagModel::save($tag); } @@ -24,7 +24,7 @@ class TogglePostTagJob extends AbstractPostJob else { foreach ($tags as $i => $tag) - if ($tag->name == $tagName) + if ($tag->getName() == $tagName) unset($tags[$i]); } diff --git a/src/Api/Jobs/ToggleUserBanJob.php b/src/Api/Jobs/ToggleUserBanJob.php index c4ff5241..34e585d9 100644 --- a/src/Api/Jobs/ToggleUserBanJob.php +++ b/src/Api/Jobs/ToggleUserBanJob.php @@ -6,7 +6,10 @@ class ToggleUserBanJob extends AbstractUserJob $user = $this->user; $banned = boolval($this->getArgument(self::STATE)); - $user->banned = $banned; + if ($banned) + $user->ban(); + else + $user->unban(); UserModel::save($user); Logger::log( diff --git a/src/Auth.php b/src/Auth.php index f4ba0840..951ee42a 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -13,22 +13,22 @@ class Auth $config = getConfig(); $context = getContext(); - $dbUser = UserModel::findByNameOrEmail($name, false); - if ($dbUser === null) + $user = UserModel::findByNameOrEmail($name, false); + if ($user === null) throw new SimpleException('Invalid username'); - $passwordHash = UserModel::hashPassword($password, $dbUser->passSalt); - if ($passwordHash != $dbUser->passHash) + $passwordHash = UserModel::hashPassword($password, $user->passSalt); + if ($passwordHash != $user->passHash) throw new SimpleException('Invalid password'); - if (!$dbUser->staffConfirmed and $config->registration->staffActivation) + if (!$user->staffConfirmed and $config->registration->staffActivation) throw new SimpleException('Staff hasn\'t confirmed your registration yet'); - if ($dbUser->banned) + if ($user->isBanned()) throw new SimpleException('You are banned'); if ($config->registration->needEmailForRegistering) - Access::requireEmail($dbUser); + Access::requireEmail($user); if ($remember) { @@ -36,7 +36,7 @@ class Auth setcookie('auth', TextHelper::encrypt($token), time() + 365 * 24 * 3600, '/'); } - self::setCurrentUser($dbUser); + self::setCurrentUser($user); } public static function tryAutoLogin() @@ -86,7 +86,7 @@ class Auth { $dummy = UserModel::spawn(); $dummy->id = null; - $dummy->name = UserModel::getAnonymousName(); + $dummy->setName(UserModel::getAnonymousName()); $dummy->setAccessRank(new AccessRank(AccessRank::Anonymous)); return $dummy; } diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 9d940c64..513fcb2f 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -36,7 +36,7 @@ class PostController $context->massTagQuery = $query; if (!Access::check(new Privilege(Privilege::MassTag, 'all'))) - $query = trim($query . ' submit:' . Auth::getCurrentUser()->name); + $query = trim($query . ' submit:' . Auth::getCurrentUser()->getName()); } $ret = Api::run( diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php index f05c9c4d..fead2b0f 100644 --- a/src/Controllers/TagController.php +++ b/src/Controllers/TagController.php @@ -36,7 +36,7 @@ class TagController function($tag) { return [ - 'name' => $tag->name, + 'name' => $tag->getName(), 'count' => $tag->getPostCount(), ]; }, $ret->entities)); @@ -61,7 +61,7 @@ class TagController function($tag) { return [ - 'name' => $tag->name, + 'name' => $tag->getName(), 'count' => $tag->getPostCount(), ]; }, $ret->entities)); diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index d6a9a3da..b2a1f483 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -28,9 +28,9 @@ class UserController $flagged = in_array(TextHelper::reprUser($user), SessionHelper::get('flagged', [])); if ($tab == 'uploads') - $query = 'submit:' . $user->name; + $query = 'submit:' . $user->getName(); elseif ($tab == 'favs') - $query = 'fav:' . $user->name; + $query = 'fav:' . $user->getName(); elseif ($tab == 'delete') Access::assert(new Privilege(Privilege::DeleteUser)); diff --git a/src/Helpers/TextHelper.php b/src/Helpers/TextHelper.php index c0b758f1..ff108db9 100644 --- a/src/Helpers/TextHelper.php +++ b/src/Helpers/TextHelper.php @@ -157,14 +157,14 @@ class TextHelper { if (!is_object($user)) return '+' . $user; - return '+' . $user->name; + return '+' . $user->getName(); } public static function reprTag($tag) { if (!is_object($tag)) return '#' . $tag; - return '#' . $tag->name; + return '#' . $tag->getName(); } public static function reprTags($tags) diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index d9c50b99..b93eecf9 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -5,7 +5,7 @@ use \Chibi\Database as Database; class PostEntity extends AbstractEntity { protected $type; - public $name; + protected $name; public $origName; public $fileHash; public $fileSize; @@ -151,7 +151,7 @@ class PostEntity extends AbstractEntity if (!$tag) { $tag = TagModel::spawn(); - $tag->name = $tagName; + $tag->setName($tagName); TagModel::save($tag); } $tags []= $tag; @@ -163,7 +163,7 @@ class PostEntity extends AbstractEntity { $tagName = trim(strtolower($tagName)); foreach ($this->getTags() as $tag) - if (trim(strtolower($tag->name)) == $tagName) + if (trim(strtolower($tag->getName())) == $tagName) return true; return false; } @@ -173,6 +173,16 @@ class PostEntity extends AbstractEntity $this->hidden = boolval($hidden); } + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + } + public function getType() { return $this->type; @@ -202,17 +212,17 @@ class PostEntity extends AbstractEntity public function getThumbCustomPath($width = null, $height = null) { - return PostModel::getThumbCustomPath($this->name, $width, $height); + return PostModel::getThumbCustomPath($this->getName(), $width, $height); } public function getThumbDefaultPath($width = null, $height = null) { - return PostModel::getThumbDefaultPath($this->name, $width, $height); + return PostModel::getThumbDefaultPath($this->getName(), $width, $height); } public function getFullPath() { - return PostModel::getFullPath($this->name); + return PostModel::getFullPath($this->getName()); } public function hasCustomThumb($width = null, $height = null) @@ -374,7 +384,7 @@ class PostEntity extends AbstractEntity { $x = []; foreach ($this->getTags() as $tag) - $x []= TextHelper::reprTag($tag->name); + $x []= TextHelper::reprTag($tag->getName()); foreach ($this->getRelations() as $relatedPost) $x []= TextHelper::reprPost($relatedPost); $x []= $this->getSafety()->toInteger(); diff --git a/src/Models/Entities/TagEntity.php b/src/Models/Entities/TagEntity.php index 6cdd72c5..14296944 100644 --- a/src/Models/Entities/TagEntity.php +++ b/src/Models/Entities/TagEntity.php @@ -4,7 +4,17 @@ use \Chibi\Database as Database; class TagEntity extends AbstractEntity { - public $name; + protected $name; + + public function setName($name) + { + $this->name = $name; + } + + public function getName() + { + return $this->name; + } public function getPostCount() { diff --git a/src/Models/Entities/UserEntity.php b/src/Models/Entities/UserEntity.php index 871a08a2..abb296ca 100644 --- a/src/Models/Entities/UserEntity.php +++ b/src/Models/Entities/UserEntity.php @@ -4,7 +4,7 @@ use \Chibi\Database as Database; class UserEntity extends AbstractEntity { - public $name; + protected $name; public $passSalt; public $passHash; public $staffConfirmed; @@ -14,7 +14,32 @@ class UserEntity extends AbstractEntity public $lastLoginDate; protected $accessRank; public $settings; - public $banned; + protected $banned = false; + + public function isBanned() + { + return $this->banned; + } + + public function ban() + { + $this->banned = true; + } + + public function unban() + { + $this->banned = false; + } + + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + } public function getAccessRank() { @@ -31,7 +56,7 @@ class UserEntity extends AbstractEntity { $subject = !empty($this->emailConfirmed) ? $this->emailConfirmed - : $this->passSalt . $this->name; + : $this->passSalt . $this->getName(); $hash = md5(strtolower(trim($subject))); $url = 'http://www.gravatar.com/avatar/' . $hash . '?s=' . $size . '&d=retro'; return $url; diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index a2ae0760..b0e37e52 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -37,7 +37,7 @@ class PostModel extends AbstractCrudModel $post->uploadDate = time(); do { - $post->name = md5(mt_rand() . uniqid()); + $post->setName(md5(mt_rand() . uniqid())); } while (file_exists($post->getFullPath())); return $post; @@ -51,7 +51,7 @@ class PostModel extends AbstractCrudModel $bindings = [ 'type' => $post->getType()->toInteger(), - 'name' => $post->name, + 'name' => $post->getName(), 'orig_name' => $post->origName, 'file_hash' => $post->fileHash, 'file_size' => $post->fileSize, diff --git a/src/Models/SearchParsers/PostSearchParser.php b/src/Models/SearchParsers/PostSearchParser.php index 7958f308..6ef1ac9a 100644 --- a/src/Models/SearchParsers/PostSearchParser.php +++ b/src/Models/SearchParsers/PostSearchParser.php @@ -156,7 +156,7 @@ class PostSearchParser extends AbstractSearchParser $value = strtolower($value); if (in_array($value, ['fav', 'favs', 'favd'])) { - return $this->prepareCriterionForComplexToken('fav', $activeUser->name); + return $this->prepareCriterionForComplexToken('fav', $activeUser->getName()); } elseif (in_array($value, ['like', 'liked', 'likes'])) diff --git a/src/Models/TagModel.php b/src/Models/TagModel.php index 7fc46d76..6bf91420 100644 --- a/src/Models/TagModel.php +++ b/src/Models/TagModel.php @@ -25,7 +25,7 @@ class TagModel extends AbstractCrudModel $stmt = new Sql\UpdateStatement(); $stmt->setTable('tag'); - $stmt->setColumn('name', new Sql\Binding($tag->name)); + $stmt->setColumn('name', new Sql\Binding($tag->getName())); $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($tag->id))); Database::exec($stmt); @@ -58,8 +58,8 @@ class TagModel extends AbstractCrudModel if ($targetTag and $targetTag->id != $sourceTag->id) throw new SimpleException('Target tag already exists'); - $sourceTag->name = $targetName; - TagModel::validateTag($sourceTag->name); + $sourceTag->setName($targetName); + TagModel::validateTag($sourceTag->getName()); self::save($sourceTag); }); } diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 3c5dd3e9..1811500b 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -42,7 +42,7 @@ class UserModel extends AbstractCrudModel self::forgeId($user); $bindings = [ - 'name' => $user->name, + 'name' => $user->getName(), 'pass_salt' => $user->passSalt, 'pass_hash' => $user->passHash, 'staff_confirmed' => $user->staffConfirmed, @@ -52,7 +52,7 @@ class UserModel extends AbstractCrudModel 'last_login_date' => $user->lastLoginDate, 'access_rank' => $user->getAccessRank()->toInteger(), 'settings' => $user->settings, - 'banned' => $user->banned + 'banned' => $user->isBanned(), ]; $stmt = (new Sql\UpdateStatement) @@ -190,7 +190,7 @@ class UserModel extends AbstractCrudModel public static function validateUserName(UserEntity $user) { - $userName = trim($user->name); + $userName = trim($user->getName()); $config = getConfig(); $otherUser = self::findByName($userName, false); diff --git a/src/Views/comment-small.phtml b/src/Views/comment-small.phtml index 5b91bb43..f664bd39 100644 --- a/src/Views/comment-small.phtml +++ b/src/Views/comment-small.phtml @@ -8,8 +8,8 @@ Assets::addScript('comment-edit.js');
context->comment->getCommenter() ?> - - <?= $commenter->name ?> + + <?= $commenter->getName() ?> - name ?> + ['name' => $commenter->getName()]) ?>"> + getName() ?> diff --git a/src/Views/post-edit.phtml b/src/Views/post-edit.phtml index aa8e09bf..aaf2a844 100644 --- a/src/Views/post-edit.phtml +++ b/src/Views/post-edit.phtml @@ -46,7 +46,7 @@ id="tags" placeholder="enter some tags…" value="name); + return htmlspecialchars($tag->getName()); }, $this->context->transport->post->getTags())) ?>"/>
diff --git a/src/Views/post-file-render.phtml b/src/Views/post-file-render.phtml index 0bb41679..21542ed3 100644 --- a/src/Views/post-file-render.phtml +++ b/src/Views/post-file-render.phtml @@ -1,7 +1,7 @@ $this->context->transport->post->name])); + ['name' => $this->context->transport->post->getName()])); $post = $this->context->transport->post; ?> @@ -11,10 +11,10 @@ $post = $this->context->transport->post; - <?= $post->name ?> + ['name' => $post->getName()]) ?>"/> context->imageLink)): ?> @@ -28,12 +28,12 @@ $post = $this->context->transport->post; height="imageHeight ?>" data=" $post->name]) ?>"> + ['name' => $post->getName()]) ?>"> + ['name' => $post->getName()]) ?>"/> @@ -53,7 +53,7 @@ $post = $this->context->transport->post; type="mimeType ?>" src=" $post->name]) ?>"> + ['name' => $post->getName()]) ?>"> Your browser doesn't support HTML5 <video> tag. diff --git a/src/Views/post-small.phtml b/src/Views/post-small.phtml index cfe92ae7..5a2f4e82 100644 --- a/src/Views/post-small.phtml +++ b/src/Views/post-small.phtml @@ -48,7 +48,7 @@ if ($masstag) @<?= $this->context->post->id ?> 0; context->transport->post->getTags() ?>

tags ()