1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-03-25 18:13:28 +01:00

[ticket/14492] Send statistics via ajax request

Flooding ajax requests will try to be prevented and sending
stats without JS will also properly work.

PHPBB3-14492
This commit is contained in:
Marc Alexander 2016-02-21 22:14:58 +01:00
parent 80a63a9a94
commit 89fef2ce13
No known key found for this signature in database
GPG Key ID: 50E0D2423696F995
5 changed files with 136 additions and 23 deletions

View File

@ -4,7 +4,7 @@
<h1>{L_ACP_HELP_PHPBB}</h1>
<form id="acp_board" method="post" action="{U_ACTION}">
<form id="acp_help_phpbb" method="post" action="{U_ACTION}" data-ajax-action="{U_COLLECT_STATS}">
<div class="send-stats-row">
<!-- EVENT acp_help_phpbb_stats_before -->
<div class="send-stats-tile">
@ -12,9 +12,9 @@
<p>{L_EXPLAIN_SEND_STATISTICS}</p>
<div class="send-stats-row">
<div class="send-stats-data-row send-stats-data-only-row">
<a id="trigger-configlist" data-ajax="toggle_link" data-toggle-text="{L_HIDE_STATISTICS}"><span>{L_SHOW_STATISTICS}</span><i class="icon fa-angle-down"></i></a>
<a id="trigger-configlist" data-ajax="toggle_link" data-overlay="false" data-toggle-text="{L_HIDE_STATISTICS}"><span>{L_SHOW_STATISTICS}</span><i class="icon fa-angle-down"></i></a>
</div>
<div class="send-stats-data-row send-stats-data-hidden">
<div class="send-stats-data-row">
<div class="configlist" id="configlist">
<!-- BEGIN providers -->
<fieldset>
@ -41,11 +41,21 @@
<!-- EVENT acp_help_phpbb_stats_after -->
<fieldset>
<p class="submit-buttons">
<input type="hidden" name="systemdata" value="{RAW_DATA}" />
<input type="hidden" name="help_send_statistics_time" value="{COLLECT_STATS_TIME}" />
<input class="button1" type="submit" id="submit" name="submit" value="{L_SUBMIT}" />
</p>
{S_FORM_TOKEN}
</fieldset>
</div>
</form>
<form action="{U_COLLECT_STATS}" method="post" target="questionaire_result" id="questionnaire-form">
<fieldset>
<p class="submit-buttons">
<input type="hidden" name="systemdata" value="{RAW_DATA}" />
<input class="button1" type="submit" id="submit_stats" name="submit" value="{L_SEND_STATISTICS}" />
</p>
</fieldset>
</form>
<!-- INCLUDE overall_footer.html -->

View File

@ -243,11 +243,16 @@ function parse_document(container)
parse_document($('body'));
$('#trigger-configlist').on('click', function () {
$('#questionnaire-form').css('display', 'none');
var $triggerConfiglist = $('#trigger-configlist');
$triggerConfiglist.on('click', function () {
var $configlist = $('#configlist');
$configlist.closest('.send-stats-data-row').toggleClass('send-stats-data-hidden');
$configlist.closest('.send-stats-row').find('.send-stats-data-row:first-child').toggleClass('send-stats-data-only-row');
$(this).find('i').toggleClass('fa-angle-down fa-angle-up');
});
$('#configlist').closest('.send-stats-data-row').addClass('send-stats-data-hidden');
});
})(jQuery);

View File

@ -4,6 +4,101 @@
'use strict';
phpbb.prepareSendStats = function () {
var $form = $('#acp_help_phpbb');
var $dark = $('#darkenwrapper');
var $loadingIndicator;
$form.on('submit', function () {
var $this = $(this),
currentTime = Math.floor(new Date().getTime() / 1000),
statsTime = parseInt($this.find('input[name=help_send_statistics_time]').val(), 10);
event.preventDefault();
$this.unbind('submit');
// Skip ajax request if form is submitted too early or send stats
// checkbox is not checked
if (!$this.find('input[name=help_send_statistics]').is(':checked') ||
statsTime > currentTime) {
$form.find('input[type=submit]').click();
setTimeout(function () {
$form.find('input[type=submit]').click();
}, 300);
return;
}
/**
* Handler for AJAX errors
*/
function errorHandler(jqXHR, textStatus, errorThrown) {
if (typeof console !== 'undefined' && console.log) {
console.log('AJAX error. status: ' + textStatus + ', message: ' + errorThrown);
}
phpbb.clearLoadingTimeout();
var errorText = '';
if (typeof errorThrown === 'string' && errorThrown.length > 0) {
errorText = errorThrown;
} else {
errorText = $dark.attr('data-ajax-error-text-' + textStatus);
if (typeof errorText !== 'string' || !errorText.length) {
errorText = $dark.attr('data-ajax-error-text');
}
}
phpbb.alert($dark.attr('data-ajax-error-title'), errorText);
}
/**
* This is a private function used to handle the callbacks, refreshes
* and alert. It calls the callback, refreshes the page if necessary, and
* displays an alert to the user and removes it after an amount of time.
*
* It cannot be called from outside this function, and is purely here to
* avoid repetition of code.
*
* @param {object} res The object sent back by the server.
*/
function returnHandler(res) {
phpbb.clearLoadingTimeout();
// If a confirmation is not required, display an alert and call the
// callbacks.
$dark.fadeOut(phpbb.alertTime);
if ($loadingIndicator) {
$loadingIndicator.fadeOut(phpbb.alertTime);
}
var $sendStatisticsSuccess = $('<input />', {
type: 'hidden',
name: 'send_statistics_response',
value: res
});
$sendStatisticsSuccess.appendTo('p.submit-buttons');
// Finish actual form submission
$form.find('input[type=submit]').click();
}
$loadingIndicator = phpbb.loadingIndicator();
$.ajax({
url: $this.attr('data-ajax-action').replace('&amp;', '&'),
type: 'POST',
data: 'systemdata=' + $this.find('input[name=systemdata]').val(),
success: returnHandler,
error: errorHandler,
cache: false
}).always(function() {
if ($loadingIndicator && $loadingIndicator.is(':visible')) {
$loadingIndicator.fadeOut(phpbb.alertTime);
}
});
});
};
/**
* The following callbacks are for reording items. row_down
* is triggered when an item is moved down, and row_up is triggered when
@ -225,6 +320,10 @@ $(function() {
$(this).attr('data-clicked', true);
});
}
if ($('#acp_help_phpbb')) {
phpbb.prepareSendStats();
}
});

View File

@ -84,23 +84,17 @@ class acp_help_phpbb
if ($submit)
{
$config->set('help_send_statistics', $request->variable('help_send_statistics', false));
$response = $request->variable('send_statistics_response', '');
if ($config['help_send_statistics'])
if (!empty($response))
{
$client = new \GuzzleHttp\Client([
'timeout' => 6,
'connect_timeout' => 6,
]);
$response = $client->post($collect_url, [
'body' => [
'systemdata' => $collector->get_data_for_form(),
]
]);
$response_status = $response->getStatusCode();
if ($response_status >= 200 && $response_status < 300)
if ((strpos($response, 'Thank you') !== false || strpos($response, 'Flood protection') !== false))
{
// Update time when statistics were actually sent
if (strpos($response, 'Thank you') !== false)
{
$config->set('help_send_statistics_time', time());
}
trigger_error($user->lang('THANKS_SEND_STATISTICS') . adm_back_link($this->u_action));
}
else
@ -108,14 +102,18 @@ class acp_help_phpbb
trigger_error($user->lang('FAIL_SEND_STATISTICS') . adm_back_link($this->u_action));
}
}
trigger_error($user->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
}
$template->assign_vars(array(
'U_COLLECT_STATS' => $collect_url,
'S_COLLECT_STATS' => (!empty($config['help_send_statistics'])) ? true : false,
'RAW_DATA' => $collector->get_data_for_form(),
'U_ACP_MAIN' => append_sid("{$phpbb_admin_path}index.$phpEx"),
'U_ACTION' => $this->u_action,
'U_COLLECT_STATS' => $collect_url,
'S_COLLECT_STATS' => (!empty($config['help_send_statistics'])) ? true : false,
'RAW_DATA' => $collector->get_data_for_form(),
'U_ACP_MAIN' => append_sid("{$phpbb_admin_path}index.$phpEx"),
'U_ACTION' => $this->u_action,
// Pass earliest time we should try to send stats again
'COLLECT_STATS_TIME' => intval($config['help_send_statistics_time']) + 86400,
));
$raw = $collector->get_data_raw();

View File

@ -31,6 +31,7 @@ class add_help_phpbb extends \phpbb\db\migration\migration
{
return array(
array('config.add', array('help_send_statistics', true)),
array('config.add', array('help_send_statistics_time', 0)),
array('module.remove', array(
'acp',
false,