New exception style; split long lines in php

This commit is contained in:
Marcin Kurczewski 2014-04-27 14:42:39 +02:00
parent cc51d943e2
commit f495774be4
17 changed files with 192 additions and 80 deletions

View file

@ -84,7 +84,9 @@ class CommentController
$comment = CommentModel::findById($id);
$this->context->transport->comment = $comment;
PrivilegesHelper::confirmWithException(Privilege::EditComment, PrivilegesHelper::getIdentitySubPrivilege($comment->getCommenter()));
PrivilegesHelper::confirmWithException(
Privilege::EditComment,
PrivilegesHelper::getIdentitySubPrivilege($comment->getCommenter()));
if (InputHelper::get('submit'))
{
@ -113,7 +115,10 @@ class CommentController
{
$comment = CommentModel::findById($id);
PrivilegesHelper::confirmWithException(Privilege::DeleteComment, PrivilegesHelper::getIdentitySubPrivilege($comment->getCommenter()));
PrivilegesHelper::confirmWithException(
Privilege::DeleteComment,
PrivilegesHelper::getIdentitySubPrivilege($comment->getCommenter()));
CommentModel::remove($comment);
LogHelper::log('{user} removed comment from {post}', ['post' => TextHelper::reprPost($comment->getPost())]);

View file

@ -14,7 +14,9 @@ class IndexController
{
$this->context->featuredPost = $featuredPost;
$this->context->featuredPostDate = PropertyModel::get(PropertyModel::FeaturedPostDate);
$this->context->featuredPostUser = UserModel::findByNameOrEmail(PropertyModel::get(PropertyModel::FeaturedPostUserName), false);
$this->context->featuredPostUser = UserModel::findByNameOrEmail(
PropertyModel::get(PropertyModel::FeaturedPostUserName),
false);
}
}

View file

@ -63,7 +63,12 @@ class LogController
$lines = array_reverse($lines);
if (!empty($filter))
$lines = array_filter($lines, function($line) use ($filter) { return stripos($line, $filter) !== false; });
{
$lines = array_filter($lines, function($line) use ($filter)
{
return stripos($line, $filter) !== false;
});
}
$lineCount = count($lines);
$logsPerPage = intval($this->config->browsing->logsPerPage);

View file

@ -7,20 +7,28 @@ class PostController
{
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_INI_SIZE:
throw new SimpleException('File is too big (maximum size allowed: ' . ini_get('upload_max_filesize') . ')');
throw new SimpleException('File is too big (maximum size: %s)', ini_get('upload_max_filesize'));
case UPLOAD_ERR_FORM_SIZE:
throw new SimpleException('File is too big than it was allowed in HTML form');
case UPLOAD_ERR_PARTIAL:
throw new SimpleException('File transfer was interrupted');
case UPLOAD_ERR_NO_FILE:
throw new SimpleException('No file was uploaded');
case UPLOAD_ERR_NO_TMP_DIR:
throw new SimpleException('Server misconfiguration error: missing temporary folder');
case UPLOAD_ERR_CANT_WRITE:
throw new SimpleException('Server misconfiguration error: cannot write to disk');
case UPLOAD_ERR_EXTENSION:
throw new SimpleException('Server misconfiguration error: upload was canceled by an extension');
default:
throw new SimpleException('Generic file upload error (id: ' . $file['error'] . ')');
}
@ -57,7 +65,11 @@ class PostController
$this->context->transport->lastSearchQuery = $formQuery;
if (strpos($formQuery, '/') !== false)
throw new SimpleException('Search query contains invalid characters');
$url = \Chibi\UrlHelper::route('post', 'list', ['source' => $source, 'additionalInfo' => $additionalInfo, 'query' => $formQuery]);
$url = \Chibi\UrlHelper::route('post', 'list', [
'source' => $source,
'additionalInfo' => $additionalInfo,
'query' => $formQuery]);
\Chibi\UrlHelper::forward($url);
return;
}
@ -107,7 +119,9 @@ class PostController
if (InputHelper::get('submit'))
{
PrivilegesHelper::confirmWithException(Privilege::MassTag, PrivilegesHelper::getIdentitySubPrivilege($post->getUploader()));
PrivilegesHelper::confirmWithException(
Privilege::MassTag,
PrivilegesHelper::getIdentitySubPrivilege($post->getUploader()));
$tags = $post->getTags();
@ -116,7 +130,10 @@ class PostController
foreach ($tags as $i => $tag)
if ($tag->name == $tagName)
unset($tags[$i]);
LogHelper::log('{user} untagged {post} with {tag}', ['post' => TextHelper::reprPost($post), 'tag' => TextHelper::reprTag($tag)]);
LogHelper::log('{user} untagged {post} with {tag}', [
'post' => TextHelper::reprPost($post),
'tag' => TextHelper::reprTag($tag)]);
}
elseif ($enable)
{
@ -129,7 +146,9 @@ class PostController
}
$tags []= $tag;
LogHelper::log('{user} tagged {post} with {tag}', ['post' => TextHelper::reprPost($post), 'tag' => TextHelper::reprTag($tag)]);
LogHelper::log('{user} tagged {post} with {tag}', [
'post' => TextHelper::reprPost($post),
'tag' => TextHelper::reprTag($tag)]);
}
$post->setTags($tags);

View file

@ -111,7 +111,10 @@ class TagController
TagModel::merge($suppliedSourceTag, $suppliedTargetTag);
LogHelper::log('{user} merged {source} with {target}', ['source' => TextHelper::reprTag($suppliedSourceTag), 'target' => TextHelper::reprTag($suppliedTargetTag)]);
LogHelper::log('{user} merged {source} with {target}', [
'source' => TextHelper::reprTag($suppliedSourceTag),
'target' => TextHelper::reprTag($suppliedTargetTag)]);
StatusHelper::success('Tags merged successfully.');
}
}
@ -137,7 +140,10 @@ class TagController
TagModel::rename($suppliedSourceTag, $suppliedTargetTag);
LogHelper::log('{user} renamed {source} to {target}', ['source' => TextHelper::reprTag($suppliedSourceTag), 'target' => TextHelper::reprTag($suppliedTargetTag)]);
LogHelper::log('{user} renamed {source} to {target}', [
'source' => TextHelper::reprTag($suppliedSourceTag),
'target' => TextHelper::reprTag($suppliedTargetTag)]);
StatusHelper::success('Tag renamed successfully.');
}
}

View file

@ -43,11 +43,13 @@ class UserController
if (empty($recipientEmail))
throw new SimpleException('Destination e-mail address was not found');
$messageId = $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['HTTP_HOST'];
$headers = [];
$headers []= sprintf('MIME-Version: 1.0');
$headers []= sprintf('Content-Transfer-Encoding: 7bit');
$headers []= sprintf('Date: %s', date('r', $_SERVER['REQUEST_TIME']));
$headers []= sprintf('Message-ID: <%s>', $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $_SERVER['HTTP_HOST']);
$headers []= sprintf('Message-ID: <%s>', $messageId);
$headers []= sprintf('From: %s <%s>', $senderName, $senderEmail);
$headers []= sprintf('Reply-To: %s', $senderEmail);
$headers []= sprintf('Return-Path: %s', $senderEmail);
@ -58,7 +60,9 @@ class UserController
$encodedSubject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
mail($recipientEmail, $encodedSubject, $body, implode("\r\n", $headers), '-f' . $senderEmail);
LogHelper::log('Sending e-mail with subject "{subject}" to {mail}', ['subject' => $subject, 'mail' => $recipientEmail]);
LogHelper::log('Sending e-mail with subject "{subject}" to {mail}', [
'subject' => $subject,
'mail' => $recipientEmail]);
}
private static function sendEmailChangeConfirmation($user)
@ -107,7 +111,8 @@ class UserController
*/
public function listAction($filter, $page)
{
PrivilegesHelper::confirmWithException(Privilege::ListUsers);
PrivilegesHelper::confirmWithException(
Privilege::ListUsers);
$suppliedFilter = $filter ?: InputHelper::get('filter') ?: 'order:alpha,asc';
$page = max(1, intval($page));
@ -137,7 +142,9 @@ class UserController
public function flagAction($name)
{
$user = UserModel::findByNameOrEmail($name);
PrivilegesHelper::confirmWithException(Privilege::FlagUser, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::FlagUser,
PrivilegesHelper::getIdentitySubPrivilege($user));
if (InputHelper::get('submit'))
{
@ -149,7 +156,9 @@ class UserController
$flagged []= $key;
SessionHelper::set('flagged', $flagged);
LogHelper::log('{user} flagged {subject} for moderator attention', ['subject' => TextHelper::reprUser($user)]);
LogHelper::log('{user} flagged {subject} for moderator attention', [
'subject' => TextHelper::reprUser($user)]);
StatusHelper::success();
}
}
@ -163,7 +172,9 @@ class UserController
public function banAction($name)
{
$user = UserModel::findByNameOrEmail($name);
PrivilegesHelper::confirmWithException(Privilege::BanUser, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::BanUser,
PrivilegesHelper::getIdentitySubPrivilege($user));
if (InputHelper::get('submit'))
{
@ -184,7 +195,9 @@ class UserController
public function unbanAction($name)
{
$user = UserModel::findByNameOrEmail($name);
PrivilegesHelper::confirmWithException(Privilege::BanUser, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::BanUser,
PrivilegesHelper::getIdentitySubPrivilege($user));
if (InputHelper::get('submit'))
{
@ -205,7 +218,9 @@ class UserController
public function acceptRegistrationAction($name)
{
$user = UserModel::findByNameOrEmail($name);
PrivilegesHelper::confirmWithException(Privilege::AcceptUserRegistration);
PrivilegesHelper::confirmWithException(
Privilege::AcceptUserRegistration);
if (InputHelper::get('submit'))
{
$user->staffConfirmed = true;
@ -224,8 +239,12 @@ class UserController
public function deleteAction($name)
{
$user = UserModel::findByNameOrEmail($name);
PrivilegesHelper::confirmWithException(Privilege::ViewUser, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(Privilege::DeleteUser, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::ViewUser,
PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::DeleteUser,
PrivilegesHelper::getIdentitySubPrivilege($user));
$this->loadUserView($user);
$this->context->transport->tab = 'delete';
@ -262,8 +281,12 @@ class UserController
public function settingsAction($name)
{
$user = UserModel::findByNameOrEmail($name);
PrivilegesHelper::confirmWithException(Privilege::ViewUser, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(Privilege::ChangeUserSettings, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::ViewUser,
PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::ChangeUserSettings,
PrivilegesHelper::getIdentitySubPrivilege($user));
$this->loadUserView($user);
$this->context->transport->tab = 'settings';
@ -300,7 +323,9 @@ class UserController
try
{
$user = UserModel::findByNameOrEmail($name);
PrivilegesHelper::confirmWithException(Privilege::ViewUser, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::ViewUser,
PrivilegesHelper::getIdentitySubPrivilege($user));
$this->loadUserView($user);
$this->context->transport->tab = 'edit';
@ -320,16 +345,24 @@ class UserController
if ($suppliedName != '' and $suppliedName != $user->name)
{
PrivilegesHelper::confirmWithException(Privilege::ChangeUserName, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::ChangeUserName,
PrivilegesHelper::getIdentitySubPrivilege($user));
$suppliedName = UserModel::validateUserName($suppliedName);
$oldName = $user->name;
$user->name = $suppliedName;
LogHelper::log('{user} renamed {old} to {new}', ['old' => TextHelper::reprUser($oldName), 'new' => TextHelper::reprUser($suppliedName)]);
LogHelper::log('{user} renamed {old} to {new}', [
'old' => TextHelper::reprUser($oldName),
'new' => TextHelper::reprUser($suppliedName)]);
}
if ($suppliedPassword1 != '')
{
PrivilegesHelper::confirmWithException(Privilege::ChangeUserPassword, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::ChangeUserPassword,
PrivilegesHelper::getIdentitySubPrivilege($user));
if ($suppliedPassword1 != $suppliedPassword2)
throw new SimpleException('Specified passwords must be the same');
$suppliedPassword = UserModel::validatePassword($suppliedPassword1);
@ -339,7 +372,10 @@ class UserController
if ($suppliedEmail != '' and $suppliedEmail != $user->emailConfirmed)
{
PrivilegesHelper::confirmWithException(Privilege::ChangeUserEmail, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::ChangeUserEmail,
PrivilegesHelper::getIdentitySubPrivilege($user));
$suppliedEmail = UserModel::validateEmail($suppliedEmail);
if ($this->context->user->id == $user->id)
{
@ -352,16 +388,23 @@ class UserController
{
$user->emailUnconfirmed = null;
$user->emailConfirmed = $suppliedEmail;
LogHelper::log('{user} changed {subject}\'s e-mail to {mail}', ['subject' => TextHelper::reprUser($user), 'mail' => $suppliedEmail]);
LogHelper::log('{user} changed {subject}\'s e-mail to {mail}', [
'subject' => TextHelper::reprUser($user),
'mail' => $suppliedEmail]);
}
}
if ($suppliedAccessRank != '' and $suppliedAccessRank != $user->accessRank)
{
PrivilegesHelper::confirmWithException(Privilege::ChangeUserAccessRank, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::ChangeUserAccessRank,
PrivilegesHelper::getIdentitySubPrivilege($user));
$suppliedAccessRank = UserModel::validateAccessRank($suppliedAccessRank);
$user->accessRank = $suppliedAccessRank;
LogHelper::log('{user} changed {subject}\'s access rank to {rank}', ['subject' => TextHelper::reprUser($user), 'rank' => AccessRank::toString($suppliedAccessRank)]);
LogHelper::log('{user} changed {subject}\'s access rank to {rank}', [
'subject' => TextHelper::reprUser($user),
'rank' => AccessRank::toString($suppliedAccessRank)]);
}
if ($this->context->user->id == $user->id)
@ -409,7 +452,10 @@ class UserController
if ($page === null)
$page = 1;
PrivilegesHelper::confirmWithException(Privilege::ViewUser, PrivilegesHelper::getIdentitySubPrivilege($user));
PrivilegesHelper::confirmWithException(
Privilege::ViewUser,
PrivilegesHelper::getIdentitySubPrivilege($user));
$this->loadUserView($user);
$query = '';
@ -443,7 +489,9 @@ class UserController
*/
public function toggleSafetyAction($safety)
{
PrivilegesHelper::confirmWithException(Privilege::ChangeUserSettings, PrivilegesHelper::getIdentitySubPrivilege($this->context->user));
PrivilegesHelper::confirmWithException(
Privilege::ChangeUserSettings,
PrivilegesHelper::getIdentitySubPrivilege($this->context->user));
if (!in_array($safety, PostSafety::getAll()))
throw new SimpleExcetpion('Invalid safety');

View file

@ -34,7 +34,7 @@ abstract class AbstractCrudModel implements IModel
return self::convertRow($row);
if ($throw)
throw new SimpleNotFoundException('Invalid ' . static::getTableName() . ' ID "' . $key . '"');
throw new SimpleNotFoundException('Invalid %s ID "%s"', static::getTableName(), $key);
return null;
}

View file

@ -93,10 +93,10 @@ class CommentModel extends AbstractCrudModel
$config = \Chibi\Registry::getConfig();
if (strlen($text) < $config->comments->minLength)
throw new SimpleException(sprintf('Comment must have at least %d characters', $config->comments->minLength));
throw new SimpleException('Comment must have at least %d characters', $config->comments->minLength);
if (strlen($text) > $config->comments->maxLength)
throw new SimpleException(sprintf('Comment must have at most %d characters', $config->comments->maxLength));
throw new SimpleException('Comment must have at most %d characters', $config->comments->maxLength);
return $text;
}

View file

@ -115,7 +115,7 @@ class PostEntity extends AbstractEntity
continue;
if (count($relatedPosts) > $config->browsing->maxRelatedPosts)
throw new SimpleException('Too many related posts (maximum: ' . $config->browsing->maxRelatedPosts . ')');
throw new SimpleException('Too many related posts (maximum: %d)', $config->browsing->maxRelatedPosts);
$relatedPosts []= PostModel::findById($relatedId);
}
@ -219,13 +219,13 @@ class PostEntity extends AbstractEntity
$mimeType = mime_content_type($srcPath);
if (!in_array($mimeType, ['image/gif', 'image/png', 'image/jpeg']))
throw new SimpleException('Invalid thumbnail type "' . $mimeType . '"');
throw new SimpleException('Invalid thumbnail type "%s"', $mimeType);
list ($imageWidth, $imageHeight) = getimagesize($srcPath);
if ($imageWidth != $config->browsing->thumbWidth)
throw new SimpleException('Invalid thumbnail width (should be ' . $config->browsing->thumbWidth . ')');
throw new SimpleException('Invalid thumbnail width (should be %d)', $config->browsing->thumbWidth);
if ($imageHeight != $config->browsing->thumbHeight)
throw new SimpleException('Invalid thumbnail height (should be ' . $config->browsing->thumbHeight . ')');
throw new SimpleException('Invalid thumbnail height (should be %d)', $config->browsing->thumbHeight);
$dstPath = $this->getThumbCustomPath();
@ -264,7 +264,13 @@ class PostEntity extends AbstractEntity
case 'application/x-shockwave-flash':
$srcImage = null;
$tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png';
exec('dump-gnash --screenshot last --screenshot-file "' . $tmpPath . '" -1 -r1 --max-advances 15 "' . $srcPath . '"');
$cmd = sprintf(
'dump-gnash --screenshot last --screenshot-file "%s" -1 -r1 --max-advances 15 "%s"',
$tmpPath,
$srcPath);
exec($cmd);
if (file_exists($tmpPath))
$srcImage = imagecreatefrompng($tmpPath);
@ -285,13 +291,24 @@ class PostEntity extends AbstractEntity
case 'video/3gpp':
$srcImage = null;
$tmpPath = tempnam(sys_get_temp_dir(), 'thumb') . '.png';
exec('ffmpegthumbnailer -i"' . $srcPath . '" -o"' . $tmpPath . '" -s0 -t"12%"');
$cmd = sprintf(
'ffmpegthumbnailer -i"%s" -o"%s" -s0 -t"12%"',
$srcPath,
$tmpPath);
exec($cmd);
if (file_exists($tmpPath))
$srcImage = imagecreatefrompng($tmpPath);
if (!$srcImage)
{
exec('ffmpeg -i "' . $srcPath . '" -vframes 1 "' . $tmpPath . '"');
exec($cmd);
$cmd = sprintf(
'ffmpeg -i "%s" -vframes 1 "%s"',
$srcPath,
$tmpPath);
if (file_exists($tmpPath))
$srcImage = imagecreatefrompng($tmpPath);
}
@ -365,12 +382,12 @@ class PostEntity extends AbstractEntity
$this->imageHeight = $imageHeight;
break;
default:
throw new SimpleException('Invalid file type "' . $this->mimeType . '"');
throw new SimpleException('Invalid file type "%s"', $this->mimeType);
}
$duplicatedPost = PostModel::findByHash($this->fileHash, false);
if ($duplicatedPost !== null and (!$this->id or $this->id != $duplicatedPost->id))
throw new SimpleException('Duplicate upload: @' . $duplicatedPost->id);
throw new SimpleException('Duplicate upload: %s', TextHelper::reprPost($duplicatedPost));
$dstPath = $this->getFullPath();
@ -387,7 +404,7 @@ class PostEntity extends AbstractEntity
public function setContentFromUrl($srcUrl)
{
if (!preg_match('/^https?:\/\//', $srcUrl))
throw new SimpleException('Invalid URL "' . $srcUrl . '"');
throw new SimpleException('Invalid URL "%s"', $srcUrl);
if (preg_match('/youtube.com\/watch.*?=([a-zA-Z0-9_-]+)/', $srcUrl, $matches))
{
@ -405,7 +422,7 @@ class PostEntity extends AbstractEntity
$duplicatedPost = PostModel::findByHash($youtubeId, false);
if ($duplicatedPost !== null and (!$this->id or $this->id != $duplicatedPost->id))
throw new SimpleException('Duplicate upload: @' . $duplicatedPost->id);
throw new SimpleException('Duplicate upload: %s' . TextHelper::reprPost($duplicatedPost));
return;
}
@ -434,7 +451,7 @@ class PostEntity extends AbstractEntity
throw new SimpleException('Cannot write into file');
fflush($srcFP);
if (ftell($srcFP) > $maxBytes)
throw new SimpleException('File is too big (maximum allowed size: ' . TextHelper::useBytesUnits($maxBytes) . ')');
throw new SimpleException('File is too big (maximum size: %s)', TextHelper::useBytesUnits($maxBytes));
}
}
finally

View file

@ -146,7 +146,7 @@ class PostModel extends AbstractCrudModel
return self::convertRow($row);
if ($throw)
throw new SimpleNotFoundException('Invalid post name "' . $key . '"');
throw new SimpleNotFoundException('Invalid post name "%s"', $key);
return null;
}
@ -171,7 +171,7 @@ class PostModel extends AbstractCrudModel
return self::convertRow($row);
if ($throw)
throw new SimpleNotFoundException('Invalid post hash "' . $hash . '"');
throw new SimpleNotFoundException('Invalid post hash "%s"', $hash);
return null;
}
@ -267,7 +267,7 @@ class PostModel extends AbstractCrudModel
$safety = intval($safety);
if (!in_array($safety, PostSafety::getAll()))
throw new SimpleException('Invalid safety type "' . $safety . '"');
throw new SimpleException('Invalid safety type "%s"', $safety);
return $safety;
}
@ -278,7 +278,7 @@ class PostModel extends AbstractCrudModel
$maxLength = 200;
if (strlen($source) > $maxLength)
throw new SimpleException('Source must have at most ' . $maxLength . ' characters');
throw new SimpleException('Source must have at most %d characters', $maxLength);
return $source;
}

View file

@ -35,13 +35,13 @@ abstract class AbstractSearchParser
else
{
if (!$this->processComplexToken($key, $value, $neg))
throw new SimpleException('Invalid search token: ' . $key);
throw new SimpleException('Invalid search token "%s"', $key);
}
}
else
{
if (!$this->processSimpleToken($token, $neg))
throw new SimpleException('Invalid search token: ' . $token);
throw new SimpleException('Invalid search token "%s"', $token);
}
}
$this->processTeardown();
@ -62,7 +62,7 @@ abstract class AbstractSearchParser
$arr []= 'desc';
if (count($arr) != 2)
throw new SimpleException('Invalid search order token: ' . $orderToken);
throw new SimpleException('Invalid search order token "%s"', $orderToken);
$orderByString = strtolower(array_shift($arr));
$orderDirString = strtolower(array_shift($arr));
@ -71,7 +71,7 @@ abstract class AbstractSearchParser
elseif ($orderDirString == 'desc')
$orderDir = Sql\SelectStatement::ORDER_DESC;
else
throw new SimpleException('Invalid search order direction: ' . $searchOrderDir);
throw new SimpleException('Invalid search order direction "%s"', $searchOrderDir);
if ($neg)
{
@ -81,7 +81,7 @@ abstract class AbstractSearchParser
}
if (!$this->processOrderToken($orderByString, $orderDir))
throw new SimpleException('Invalid search order type: ' . $orderByString);
throw new SimpleException('Invalid search order type "%s"', $orderByString);
}
protected function processComplexToken($key, $value, $neg)

View file

@ -6,13 +6,15 @@ class CommentSearchParser extends AbstractSearchParser
protected function processSetup(&$tokens)
{
$this->statement->addInnerJoin('post', new Sql\EqualsFunctor('post_id', 'post.id'));
$crit = new Sql\ConjunctionFunctor();
$allowedSafety = PrivilegesHelper::getAllowedSafety();
$this->statement->setCriterion(new Sql\ConjunctionFunctor());
$this->statement->getCriterion()->add(Sql\InFunctor::fromArray('post.safety', Sql\Binding::fromArray($allowedSafety)));
if (!PrivilegesHelper::confirm(Privilege::ListPosts, 'hidden'))
$this->statement->getCriterion()->add(new Sql\NegationFunctor(new Sql\StringExpression('hidden')));
$crit->add(Sql\InFunctor::fromArray('post.safety', Sql\Binding::fromArray($allowedSafety)));
if (!PrivilegesHelper::confirm(Privilege::ListPosts, 'hidden'))
$crit->add(new Sql\NegationFunctor(new Sql\StringExpression('hidden')));
$this->statement->setCriterion($crit);
$this->statement->addOrderBy('comment.id', Sql\SelectStatement::ORDER_DESC);
}
}

View file

@ -12,13 +12,14 @@ class PostSearchParser extends AbstractSearchParser
$config = \Chibi\Registry::getConfig();
$this->tags = [];
$this->statement->setCriterion(new Sql\ConjunctionFunctor());
$crit = new Sql\ConjunctionFunctor();
$allowedSafety = PrivilegesHelper::getAllowedSafety();
$this->statement->getCriterion()->add(Sql\InFunctor::fromArray('safety', Sql\Binding::fromArray($allowedSafety)));
$crit->add(Sql\InFunctor::fromArray('safety', Sql\Binding::fromArray($allowedSafety)));
$this->statement->setCriterion($crit);
if (count($tokens) > $config->browsing->maxSearchTokens)
throw new SimpleException('Too many search tokens (maximum: ' . $config->browsing->maxSearchTokens . ')');
throw new SimpleException('Too many search tokens (maximum: %d)', $config->browsing->maxSearchTokens);
}
protected function processTeardown()
@ -182,7 +183,7 @@ class PostSearchParser extends AbstractSearchParser
}
else
throw new SimpleException('Invalid special token: ' . $value);
throw new SimpleException('Invalid special token "%s"', $value);
}
elseif ($key == 'type')
@ -197,7 +198,7 @@ class PostSearchParser extends AbstractSearchParser
elseif ($value == 'yt' or $value == 'youtube')
$type = PostType::Youtube;
else
throw new SimpleException('Invalid post type: ' . $value);
throw new SimpleException('Invalid post type "%s"', $value);
return new Sql\EqualsFunctor('type', new Sql\Binding($type));
}

View file

@ -9,7 +9,8 @@ class TagSearchParser extends AbstractSearchParser
$this->statement
->addInnerJoin('post_tag', new Sql\EqualsFunctor('tag.id', 'post_tag.tag_id'))
->addInnerJoin('post', new Sql\EqualsFunctor('post.id', 'post_tag.post_id'))
->setCriterion((new Sql\ConjunctionFunctor)->add(Sql\InFunctor::fromArray('safety', Sql\Binding::fromArray($allowedSafety))))
->setCriterion((new Sql\ConjunctionFunctor)
->add(Sql\InFunctor::fromArray('safety', Sql\Binding::fromArray($allowedSafety))))
->setGroupBy('tag.id');
}
@ -22,7 +23,9 @@ class TagSearchParser extends AbstractSearchParser
$value = '%' . $value;
$value .= '%';
$this->statement->getCriterion()->add(new Sql\NoCaseFunctor(new Sql\LikeFunctor('tag.name', new Sql\Binding($value))));
$this->statement->getCriterion()
->add(new Sql\NoCaseFunctor(new Sql\LikeFunctor('tag.name', new Sql\Binding($value))));
return true;
}

View file

@ -130,7 +130,7 @@ class TagModel extends AbstractCrudModel
return self::convertRow($row);
if ($throw)
throw new SimpleNotFoundException('Invalid tag name "' . $key . '"');
throw new SimpleNotFoundException('Invalid tag name "%s"', $key);
return null;
}
@ -158,15 +158,15 @@ class TagModel extends AbstractCrudModel
$minLength = 1;
$maxLength = 64;
if (strlen($tag) < $minLength)
throw new SimpleException('Tag must have at least ' . $minLength . ' characters');
throw new SimpleException('Tag must have at least %d characters', $minLength);
if (strlen($tag) > $maxLength)
throw new SimpleException('Tag must have at most ' . $maxLength . ' characters');
throw new SimpleException('Tag must have at most %d characters', $maxLength);
if (!preg_match('/^[()\[\]a-zA-Z0-9_.-]+$/i', $tag))
throw new SimpleException('Invalid tag "' . $tag . '"');
throw new SimpleException('Invalid tag "%s"', $tag);
if (preg_match('/^\.\.?$/', $tag))
throw new SimpleException('Invalid tag "' . $tag . '"');
throw new SimpleException('Invalid tag "%s"', $tag);
return $tag;
}

View file

@ -100,7 +100,7 @@ class UserModel extends AbstractCrudModel
return self::convertRow($row);
if ($throw)
throw new SimpleNotFoundException('Invalid user name "' . $key . '"');
throw new SimpleNotFoundException('Invalid user name "%s"', $key);
return null;
}
@ -118,7 +118,7 @@ class UserModel extends AbstractCrudModel
return self::convertRow($row);
if ($throw)
throw new SimpleNotFoundException('Invalid user name "' . $key . '"');
throw new SimpleNotFoundException('Invalid user name "%s"', $key);
return null;
}
@ -197,10 +197,10 @@ class UserModel extends AbstractCrudModel
$userNameRegex = \Chibi\Registry::getConfig()->registration->userNameRegex;
if (strlen($userName) < $userNameMinLength)
throw new SimpleException(sprintf('User name must have at least %d characters', $userNameMinLength));
throw new SimpleException('User name must have at least %d characters', $userNameMinLength);
if (strlen($userName) > $userNameMaxLength)
throw new SimpleException(sprintf('User name must have at most %d characters', $userNameMaxLength));
throw new SimpleException('User name must have at most %d characters', $userNameMaxLength);
if (!preg_match($userNameRegex, $userName))
throw new SimpleException('User name contains invalid characters');
@ -214,7 +214,7 @@ class UserModel extends AbstractCrudModel
$passRegex = \Chibi\Registry::getConfig()->registration->passRegex;
if (strlen($password) < $passMinLength)
throw new SimpleException(sprintf('Password must have at least %d characters', $passMinLength));
throw new SimpleException('Password must have at least %d characters', $passMinLength);
if (!preg_match($passRegex, $password))
throw new SimpleException('Password contains invalid characters');
@ -237,10 +237,10 @@ class UserModel extends AbstractCrudModel
$accessRank = intval($accessRank);
if (!in_array($accessRank, AccessRank::getAll()))
throw new SimpleException('Invalid access rank type "' . $accessRank . '"');
throw new SimpleException('Invalid access rank type "%s"', $accessRank);
if ($accessRank == AccessRank::Nobody)
throw new SimpleException('Cannot set special accesss rank "' . $accessRank . '"');
throw new SimpleException('Cannot set special accesss rank "%s"', $accessRank);
return $accessRank;
}

View file

@ -1,4 +1,8 @@
<?php
class SimpleException extends Exception
{
public function __construct()
{
parent::__construct(call_user_func_array('sprintf', func_get_args()));
}
}