Commit 389c8211 by pupi1985 Committed by Scott

Added checks and error displays when exceeding post_max_size and upload_max_filesize limits

parent cbf70b46
......@@ -1038,6 +1038,33 @@
}
function qa_is_post_max_size_limit_exceeded()
/*
Checks whether an HTTP request has exceeded the post_max_size PHP variable. This happens whenever an HTTP request
is too big to be properly processed by PHP, usually because there is an attachment in the HTTP request. A warning
is added to the server's log displaying the size of the file that triggered this situation. It is important to note
that whenever this happens the $_POST and $_FILES superglobals are empty.
*/
{
if (in_array($_SERVER['REQUEST_METHOD'], array('POST', 'PUT')) && empty($_POST) && empty($_FILES)) {
$postmaxsize = ini_get('post_max_size'); // Gets the current post_max_size configuration
$unit = substr($postmaxsize, -1);
if (!is_numeric($unit)) {
$postmaxsize = substr($postmaxsize, 0, -1);
}
switch (strtoupper($unit)) { // Gets an integer value that can be compared against the size of the HTTP request
case 'G':
$postmaxsize *= 1024;
case 'M':
$postmaxsize *= 1024;
case 'K':
$postmaxsize *= 1024;
}
return $_SERVER['CONTENT_LENGTH'] > $postmaxsize;
}
}
function qa_is_mobile_probably()
/*
Return true if it appears that the page request is coming from a mobile client rather than a desktop/laptop web browser
......
......@@ -82,6 +82,7 @@
'edited' => 'edited',
'email_error' => 'An error occurred trying to send the email.',
'field_required' => 'Please enter something in this field',
'file_upload_limit_exceeded' => 'The size of the file exceeds the server\'s limits',
'general_error' => 'A server error occurred - please try again.',
'hidden' => 'hidden',
'highest_users' => 'Top scoring users',
......
......@@ -65,34 +65,37 @@
// Process profile if saved
// If the post_max_size is exceeded then the $_POST array is empty so no field processing can be done
if (qa_is_post_max_size_limit_exceeded())
$errors['avatar'] = qa_lang('main/file_upload_limit_exceeded');
else {
if (qa_clicked('dosaveprofile') && !$isblocked) {
require_once QA_INCLUDE_DIR.'qa-app-users-edit.php';
require_once QA_INCLUDE_DIR . 'qa-app-users-edit.php';
$inhandle=$changehandle ? qa_post_text('handle') : $useraccount['handle'];
$inemail=qa_post_text('email');
$inmessages=qa_post_text('messages');
$inwallposts=qa_post_text('wall');
$inmailings=qa_post_text('mailings');
$inavatar=qa_post_text('avatar');
$inhandle = $changehandle ? qa_post_text('handle') : $useraccount['handle'];
$inemail = qa_post_text('email');
$inmessages = qa_post_text('messages');
$inwallposts = qa_post_text('wall');
$inmailings = qa_post_text('mailings');
$inavatar = qa_post_text('avatar');
$inprofile=array();
foreach ($userfields as $userfield)
$inprofile[$userfield['fieldid']]=qa_post_text('field_'.$userfield['fieldid']);
$inprofile[$userfield['fieldid']] = qa_post_text('field_' . $userfield['fieldid']);
if (!qa_check_form_security_code('account', qa_post_text('code')))
$errors['page']=qa_lang_html('misc/form_security_again');
$errors['page'] = qa_lang_html('misc/form_security_again');
else {
$errors=qa_handle_email_filter($inhandle, $inemail, $useraccount);
$errors = qa_handle_email_filter($inhandle, $inemail, $useraccount);
if (!isset($errors['handle']))
qa_db_user_set($userid, 'handle', $inhandle);
if (!isset($errors['email']))
if ($inemail != $useraccount['email']) {
if (!isset($errors['email']) && $inemail !== $useraccount['email']) {
qa_db_user_set($userid, 'email', $inemail);
qa_db_user_set_flag($userid, QA_USER_FLAGS_EMAIL_CONFIRMED, false);
$isconfirmed=false;
$isconfirmed = false;
if ($doconfirms)
qa_send_new_confirm($userid);
......@@ -107,37 +110,43 @@
if (qa_opt('mailing_enabled'))
qa_db_user_set_flag($userid, QA_USER_FLAGS_NO_MAILINGS, !$inmailings);
qa_db_user_set_flag($userid, QA_USER_FLAGS_SHOW_AVATAR, ($inavatar=='uploaded'));
qa_db_user_set_flag($userid, QA_USER_FLAGS_SHOW_GRAVATAR, ($inavatar=='gravatar'));
qa_db_user_set_flag($userid, QA_USER_FLAGS_SHOW_AVATAR, ($inavatar == 'uploaded'));
qa_db_user_set_flag($userid, QA_USER_FLAGS_SHOW_GRAVATAR, ($inavatar == 'gravatar'));
if (is_array(@$_FILES['file'])) {
$avatarfileerror = $_FILES['file']['error'];
if (is_array(@$_FILES['file']) && $_FILES['file']['size']) {
require_once QA_INCLUDE_DIR.'qa-app-limits.php';
// Note if $_FILES['file']['error'] === 1 then upload_max_filesize has been exceeded
if ($avatarfileerror === 1)
$errors['avatar'] = qa_lang('main/file_upload_limit_exceeded');
elseif ($avatarfileerror === 0 && $_FILES['file']['size'] > 0) {
require_once QA_INCLUDE_DIR . 'qa-app-limits.php';
switch (qa_user_permit_error(null, QA_LIMIT_UPLOADS))
{
switch (qa_user_permit_error(null, QA_LIMIT_UPLOADS)) {
case 'limit':
$errors['avatar']=qa_lang('main/upload_limit');
$errors['avatar'] = qa_lang('main/upload_limit');
break;
default:
$errors['avatar']=qa_lang('users/no_permission');
$errors['avatar'] = qa_lang('users/no_permission');
break;
case false:
qa_limits_increment($userid, QA_LIMIT_UPLOADS);
$toobig=qa_image_file_too_big($_FILES['file']['tmp_name'], qa_opt('avatar_store_size'));
$toobig = qa_image_file_too_big($_FILES['file']['tmp_name'], qa_opt('avatar_store_size'));
if ($toobig)
$errors['avatar']=qa_lang_sub('main/image_too_big_x_pc', (int)($toobig*100));
$errors['avatar'] = qa_lang_sub('main/image_too_big_x_pc', (int) ($toobig * 100));
elseif (!qa_set_user_avatar($userid, file_get_contents($_FILES['file']['tmp_name']), $useraccount['avatarblobid']))
$errors['avatar']=qa_lang_sub('main/image_not_read', implode(', ', qa_gd_image_formats()));
$errors['avatar'] = qa_lang_sub('main/image_not_read', implode(', ', qa_gd_image_formats()));
break;
}
} // There shouldn't be any need to catch any other error
}
if (count($inprofile)) {
$filtermodules=qa_load_modules_with('filter', 'filter_profile');
$filtermodules = qa_load_modules_with('filter', 'filter_profile');
foreach ($filtermodules as $filtermodule)
$filtermodule->filter_profile($inprofile, $errors, $useraccount, $userprofile);
}
......@@ -146,9 +155,8 @@
if (!isset($errors[$userfield['fieldid']]))
qa_db_user_profile_set($userid, $userfield['title'], $inprofile[$userfield['fieldid']]);
list($useraccount, $userprofile)=qa_db_select_with_pending(
qa_db_user_account_selectspec($userid, true),
qa_db_user_profile_selectspec($userid, true)
list($useraccount, $userprofile) = qa_db_select_with_pending(
qa_db_user_account_selectspec($userid, true), qa_db_user_profile_selectspec($userid, true)
);
qa_report_event('u_save', $userid, $useraccount['handle'], qa_cookie_get());
......@@ -161,29 +169,29 @@
}
// Process change password if clicked
// Process change password if clicked
if (qa_clicked('dochangepassword')) {
require_once QA_INCLUDE_DIR.'qa-app-users-edit.php';
require_once QA_INCLUDE_DIR . 'qa-app-users-edit.php';
$inoldpassword=qa_post_text('oldpassword');
$innewpassword1=qa_post_text('newpassword1');
$innewpassword2=qa_post_text('newpassword2');
$inoldpassword = qa_post_text('oldpassword');
$innewpassword1 = qa_post_text('newpassword1');
$innewpassword2 = qa_post_text('newpassword2');
if (!qa_check_form_security_code('password', qa_post_text('code')))
$errors['page']=qa_lang_html('misc/form_security_again');
$errors['page'] = qa_lang_html('misc/form_security_again');
else {
$errors=array();
$errors = array();
if ($haspassword && (strtolower(qa_db_calc_passcheck($inoldpassword, $useraccount['passsalt'])) != strtolower($useraccount['passcheck'])))
$errors['oldpassword']=qa_lang('users/password_wrong');
$errors['oldpassword'] = qa_lang('users/password_wrong');
$useraccount['password']=$inoldpassword;
$errors=$errors+qa_password_validate($innewpassword1, $useraccount); // array union
$useraccount['password'] = $inoldpassword;
$errors = $errors + qa_password_validate($innewpassword1, $useraccount); // array union
if ($innewpassword1 != $innewpassword2)
$errors['newpassword2']=qa_lang('users/password_mismatch');
$errors['newpassword2'] = qa_lang('users/password_mismatch');
if (empty($errors)) {
qa_db_user_set_password($userid, $innewpassword1);
......@@ -196,7 +204,7 @@
}
}
}
}
// Prepare content for theme
......
......@@ -661,29 +661,32 @@
$formokhtml = null;
// If the post_max_size is exceeded then the $_POST array is empty so no field processing can be done
if (qa_is_post_max_size_limit_exceeded())
$errors['avatar_default_show'] = qa_lang('main/file_upload_limit_exceeded');
else
if (qa_clicked('doresetoptions')) {
if (!qa_check_form_security_code('admin/'.$adminsection, qa_post_text('code')))
if (!qa_check_form_security_code('admin/' . $adminsection, qa_post_text('code')))
$securityexpired = true;
else {
qa_reset_options($getoptions);
$formokhtml = qa_lang_html('admin/options_reset');
}
}
elseif (qa_clicked('dosaveoptions')) {
if (!qa_check_form_security_code('admin/'.$adminsection, qa_post_text('code')))
} elseif (qa_clicked('dosaveoptions')) {
if (!qa_check_form_security_code('admin/' . $adminsection, qa_post_text('code')))
$securityexpired = true;
else {
foreach ($getoptions as $optionname) {
$optionvalue = qa_post_text('option_'.$optionname);
$optionvalue = qa_post_text('option_' . $optionname);
if (
(@$optiontype[$optionname] == 'number') ||
(@$optiontype[$optionname] == 'checkbox') ||
((@$optiontype[$optionname] == 'number-blank') && strlen($optionvalue))
)
$optionvalue = (int)$optionvalue;
$optionvalue = (int) $optionvalue;
if (isset($optionmaximum[$optionname]))
$optionvalue = min($optionmaximum[$optionname], $optionvalue);
......@@ -707,12 +710,12 @@
break;
case 'block_ips_write':
require_once QA_INCLUDE_DIR.'qa-app-limits.php';
require_once QA_INCLUDE_DIR . 'qa-app-limits.php';
$optionvalue = implode(' , ', qa_block_ips_explode($optionvalue));
break;
case 'block_bad_words':
require_once QA_INCLUDE_DIR.'qa-util-string.php';
require_once QA_INCLUDE_DIR . 'qa-util-string.php';
$optionvalue = implode(' , ', qa_block_words_explode($optionvalue));
break;
}
......@@ -723,22 +726,27 @@
$formokhtml = qa_lang_html('admin/options_saved');
// Uploading default avatar
if (is_array(@$_FILES['avatar_default_file'])) {
$avatarfileerror = $_FILES['avatar_default_file']['error'];
if (is_array(@$_FILES['avatar_default_file']) && $_FILES['avatar_default_file']['size']) {
require_once QA_INCLUDE_DIR.'qa-util-image.php';
// Note if $_FILES['avatar_default_file']['error'] === 1 then upload_max_filesize has been exceeded
if ($avatarfileerror === 1)
$errors['avatar_default_show'] = qa_lang('main/file_upload_limit_exceeded');
elseif ($avatarfileerror === 0 && $_FILES['avatar_default_file']['size'] > 0) {
require_once QA_INCLUDE_DIR . 'qa-util-image.php';
$oldblobid = qa_opt('avatar_default_blobid');
$toobig = qa_image_file_too_big($_FILES['avatar_default_file']['tmp_name'], qa_opt('avatar_store_size'));
if ($toobig)
$errors['avatar_default_show'] = qa_lang_sub('main/image_too_big_x_pc', (int)($toobig*100));
$errors['avatar_default_show'] = qa_lang_sub('main/image_too_big_x_pc', (int) ($toobig * 100));
else {
$imagedata = qa_image_constrain_data(file_get_contents($_FILES['avatar_default_file']['tmp_name']), $width, $height, qa_opt('avatar_store_size'));
if (isset($imagedata)) {
require_once QA_INCLUDE_DIR.'qa-app-blobs.php';
require_once QA_INCLUDE_DIR . 'qa-app-blobs.php';
$newblobid = qa_create_blob($imagedata, 'jpeg');
......@@ -751,14 +759,13 @@
if (strlen($oldblobid))
qa_delete_blob($oldblobid);
}
else
} else
$errors['avatar_default_show'] = qa_lang_sub('main/image_not_read', implode(', ', qa_gd_image_formats()));
}
}
}
}
}
// Mailings management
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment