Skip to content

Commit

Permalink
Merge pull request #59 from C3realGuy/dev_max_subject_length
Browse files Browse the repository at this point in the history
Allow setting the max length a subject should have.
  • Loading branch information
Nao committed Mar 13, 2017
2 parents ded9e47 + 2999165 commit dc72afe
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions core/app/ManagePosts.php
Expand Up @@ -207,6 +207,7 @@ function ModifyPostSettings($return_config = false)
array('check', 'additional_options_collapsable'),
'',
// Posting limits...
array('int', 'max_subjectLength', 'subtext' => $txt['max_subjectLength_zero'], 'postinput' => $txt['manageposts_characters']),
array('int', 'max_messageLength', 'subtext' => $txt['max_messageLength_zero'], 'postinput' => $txt['manageposts_characters']),
array('int', 'topicSummaryPosts', 'postinput' => $txt['manageposts_posts']),
'',
Expand Down
9 changes: 5 additions & 4 deletions core/app/Post.php
Expand Up @@ -353,8 +353,10 @@ function Post($post_errors = array())
$form_message = westr::htmlspecialchars($_REQUEST['message'], ENT_QUOTES);

// Make sure the subject isn't too long - taking into account special characters.
if (westr::strlen($form_subject) > 100)
$form_subject = westr::substr($form_subject, 0, 100);
$len_form_subject = westr::strlen($form_subject);
$max_subject_length = !empty($settings['max_subjectLength']) ? $settings['max_subjectLength'] : 80;
if ($len_form_subject > $max_subject_length)
$post_errors[] = ['subject_too_long', [$max_subject_length, $len_form_subject]];

// Have we inadvertently trimmed off the subject of useful information?
if (!in_array('no_subject', $post_errors) && westr::htmltrim($form_subject) === '')
Expand All @@ -364,15 +366,14 @@ function Post($post_errors = array())
if (!empty($post_errors))
{
loadLanguage('Errors');

foreach ($post_errors as $error)
{
if (is_array($error))
{
$error_id = $error[0];
// Not really used, but we'll still set that.
$context['post_error'][$error_id] = true;
$context['post_error']['messages'][] = sprintf($txt['error_' . $error_id], $error[1]);
$context['post_error']['messages'][] = vsprintf($txt['error_' . $error_id], $error[1]);
}
else
{
Expand Down
10 changes: 6 additions & 4 deletions core/app/Post2.php
Expand Up @@ -410,8 +410,14 @@ function Post2()
$_POST['subject'] = isset($_POST['subject']) ? westr::htmlspecialchars($_POST['subject'], ENT_QUOTES) : '';
$_POST['message'] = isset($_POST['message']) ? westr::htmlspecialchars($_POST['message'], ENT_QUOTES) : '';

// At this point, we want to make sure the subject isn't too long.
$len_form_subject = westr::strlen($form_subject);
$max_subject_length = !empty($settings['max_subjectLength']) ? $settings['max_subjectLength'] : 80;
if (westr::htmltrim($_POST['subject']) === '')
$post_errors[] = 'no_subject';
elseif ($len_form_subject > $max_subject_length)
$post_errors[] = ['subject_too_long', [$max_subject_length, $len_form_subject]];

if (westr::htmltrim($_POST['message']) === '')
$post_errors[] = 'no_message';
elseif (!empty($settings['max_messageLength']) && westr::strlen($_POST['message']) > $settings['max_messageLength'])
Expand Down Expand Up @@ -575,10 +581,6 @@ function Post2()
$_POST['guestname'] = htmlspecialchars($_POST['guestname']);
$_POST['email'] = htmlspecialchars($_POST['email']);

// At this point, we want to make sure the subject isn't too long.
if (westr::strlen($_POST['subject']) > 100)
$_POST['subject'] = westr::substr($_POST['subject'], 0, 100);

// Make the poll...
if (isset($_REQUEST['poll']))
{
Expand Down
4 changes: 2 additions & 2 deletions core/html/Post.template.php
Expand Up @@ -355,7 +355,7 @@ function template_post_header_before()

function template_post_subject()
{
global $context, $txt;
global $context, $txt, $settings;

// Now show the subject box for this post.
echo '
Expand All @@ -381,7 +381,7 @@ function template_post_subject()
echo '
</div>
<div>
<input name="subject" id="subject"', $context['subject'] == '' ? '' : ' value="' . $context['subject'] . '"', ' tabindex="', $context['tabindex']++, '" maxlength="80" class="w100">
<input name="subject" id="subject"', $context['subject'] == '' ? '' : ' value="' . $context['subject'] . '"', ' tabindex="', $context['tabindex']++, '" maxlength="', isset($settings['max_subjectLength']) ? ($settings['max_subjectLength'] == 0 ? -1 : $settings['max_subjectLength']) : 80 ,'" class="w100">
</div>
</div>';
}
Expand Down
1 change: 1 addition & 0 deletions core/languages/Errors.english.php
Expand Up @@ -219,6 +219,7 @@
$txt['error_bad_file'] = 'Sorry but the file specified could not be opened: %1$s';
$txt['error_bad_line'] = 'The line you specified is invalid.';
$txt['error_too_quickly'] = 'You completed the registration form too quickly, faster than would normally be possible.<br><br><div class="smalltext">(Hey, we don\'t take kindly to robots in here!)</div>';
$txt['error_subject_too_long'] = 'Subject is too long. Max length is %d but your subject is %d chars long.';

$txt['smiley_not_found'] = 'Smiley not found.';
$txt['smiley_has_no_code'] = 'No code for this smiley was given.';
Expand Down
2 changes: 2 additions & 0 deletions core/languages/ManagePosts.english.php
Expand Up @@ -7,6 +7,8 @@
$txt['enableEmbeddedFlash_warning'] = 'May be a security risk!';
$txt['additional_options_collapsable'] = 'Enable collapsible additional post options';

$txt['max_subjectLength'] = 'Maximum allowed subject size';
$txt['max_subjectLength_zero'] = '0 for no max. Attention: for values > 255 you have to alter the messages table.';
$txt['max_messageLength'] = 'Maximum allowed post size';
$txt['max_messageLength_zero'] = '0 for no max.';
$txt['topicSummaryPosts'] = 'Posts to show on topic summary';
Expand Down

0 comments on commit dc72afe

Please sign in to comment.