Passed
Push — release-2.1 ( ed0580...b96745 )
by Mathias
09:05
created

AcceptAgreement()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 20
c 1
b 0
f 0
nc 20
nop 0
dl 0
loc 36
rs 8.4444
1
<?php
2
3
/**
4
 * This file handles the user and privacy policy agreements.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines https://www.simplemachines.org
10
 * @copyright 2020 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC3
14
 */
15
16
if (!defined('SMF'))
17
	die('Hacking attempt...');
18
19
/*	The purpose of this file is to show the user an updated registration
20
	agreement, and get them to agree to it.
21
22
	bool prepareAgreementContext()
23
		// !!!
24
25
	bool canRequireAgreement()
26
		// !!!
27
28
	bool canRequirePrivacyPolicy()
29
		// !!!
30
31
	void Agreement()
32
		- Show the new registration agreement
33
34
	void AcceptAgreement()
35
		- Called when they actually accept the agreement
36
		- Save the date of the current agreement to the members database table
37
		- Redirect back to wherever they came from
38
*/
39
40
function prepareAgreementContext()
41
{
42
	global $boarddir, $context, $language, $modSettings, $user_info;
43
44
	// What, if anything, do they need to accept?
45
	$context['can_accept_agreement'] = !empty($modSettings['requireAgreement']) && canRequireAgreement();
46
	$context['can_accept_privacy_policy'] = !empty($modSettings['requirePolicyAgreement']) && canRequirePrivacyPolicy();
47
	$context['accept_doc'] = $context['can_accept_agreement'] || $context['can_accept_privacy_policy'];
48
49
	if (!$context['accept_doc'] || $context['can_accept_agreement'])
50
	{
51
		// Grab the agreement.
52
		// Have we got a localized one?
53
		if (file_exists($boarddir . '/agreement.' . $user_info['language'] . '.txt'))
54
			$context['agreement_file'] = $boarddir . '/agreement.' . $user_info['language'] . '.txt';
55
		elseif (file_exists($boarddir . '/agreement.txt'))
56
			$context['agreement_file'] = $boarddir . '/agreement.txt';
57
58
		if (!empty($context['agreement_file']))
59
		{
60
			$cache_id = strtr($context['agreement_file'], array($boarddir => '', '.txt' => '', '.' => '_'));
61
			$context['agreement'] = parse_bbc(file_get_contents($context['agreement_file']), true, $cache_id);
62
		}
63
		elseif ($context['can_accept_agreement'])
64
			fatal_lang_error('error_no_agreement', false);
65
	}
66
67
	if (!$context['accept_doc'] || $context['can_accept_privacy_policy'])
68
	{
69
		// Have we got a localized policy?
70
		if (!empty($modSettings['policy_' . $user_info['language']]))
71
			$context['privacy_policy'] = parse_bbc($modSettings['policy_' . $user_info['language']]);
72
		elseif (!empty($modSettings['policy_' . $language]))
73
			$context['privacy_policy'] = parse_bbc($modSettings['policy_' . $language]);
74
		// Then I guess we've got nothing
75
		elseif ($context['can_accept_privacy_policy'])
76
			fatal_lang_error('error_no_privacy_policy', false);
77
	}
78
}
79
80
function canRequireAgreement()
81
{
82
	global $boarddir, $context, $modSettings, $options, $user_info;
83
84
	// Guests can't agree
85
	if (!empty($user_info['is_guest']) || empty($modSettings['requireAgreement']))
86
		return false;
87
88
	$agreement_lang = file_exists($boarddir . '/agreement.' . $user_info['language'] . '.txt') ? $user_info['language'] : 'default';
89
90
	if (empty($modSettings['agreement_updated_' . $agreement_lang]))
91
		return false;
92
93
	$context['agreement_accepted_date'] = empty($options['agreement_accepted']) ? 0 : $options['agreement_accepted'];
94
95
	// A new timestamp means that there are new changes to the registration agreement and must therefore be shown.
96
	return empty($options['agreement_accepted']) || $modSettings['agreement_updated_' . $agreement_lang] > $options['agreement_accepted'];
97
}
98
99
function canRequirePrivacyPolicy()
100
{
101
	global $modSettings, $options, $user_info, $language, $context;
102
103
	if (!empty($user_info['is_guest']) || empty($modSettings['requirePolicyAgreement']))
104
		return false;
105
106
	$policy_lang = !empty($modSettings['policy_' . $user_info['language']]) ? $user_info['language'] : $language;
107
108
	if (empty($modSettings['policy_updated_' . $policy_lang]))
109
		return false;
110
111
	$context['privacy_policy_accepted_date'] = empty($options['policy_accepted']) ? 0 : $options['policy_accepted'];
112
113
	return empty($options['policy_accepted']) || $modSettings['policy_updated_' . $policy_lang] > $options['policy_accepted'];
114
}
115
116
// Let's tell them there's a new agreement
117
function Agreement()
118
{
119
	global $context, $modSettings, $scripturl, $smcFunc, $txt;
120
121
	prepareAgreementContext();
122
123
	loadLanguage('Agreement');
124
	loadTemplate('Agreement');
125
126
	$page_title = '';
127
	if (!empty($context['agreement']) && !empty($context['privacy_policy']))
128
		$page_title = $txt['agreement_and_privacy_policy'];
129
	elseif (!empty($context['agreement']))
130
		$page_title = $txt['agreement'];
131
	elseif (!empty($context['privacy_policy']))
132
		$page_title = $txt['privacy_policy'];
133
134
	$context['page_title'] = $page_title;
135
	$context['linktree'][] = array(
136
		'url' => $scripturl . '?action=agreement',
137
		'name' => $context['page_title'],
138
	);
139
140
	if (isset($_SESSION['old_url']))
141
		$_SESSION['redirect_url'] = $_SESSION['old_url'];
142
143
}
144
145
// I solemly swear to no longer chase squirrels.
146
function AcceptAgreement()
147
{
148
	global $context, $modSettings, $smcFunc, $user_info;
149
150
	$can_accept_agreement = !empty($modSettings['requireAgreement']) && canRequireAgreement();
151
	$can_accept_privacy_policy = !empty($modSettings['requirePolicyAgreement']) && canRequirePrivacyPolicy();
152
153
	if ($can_accept_agreement || $can_accept_privacy_policy)
154
	{
155
		checkSession();
156
157
		if ($can_accept_agreement)
158
		{
159
			$smcFunc['db_insert']('replace',
160
				'{db_prefix}themes',
161
				array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string', 'value' => 'string'),
162
				array($user_info['id'], 1, 'agreement_accepted', time()),
163
				array('id_member', 'id_theme', 'variable')
164
			);
165
			logAction('agreement_accepted', array('applicator' => $user_info['id']), 'user');
166
		}
167
168
		if ($can_accept_privacy_policy)
169
		{
170
			$smcFunc['db_insert']('replace',
171
				'{db_prefix}themes',
172
				array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string', 'value' => 'string'),
173
				array($user_info['id'], 1, 'policy_accepted', time()),
174
				array('id_member', 'id_theme', 'variable')
175
			);
176
			logAction('policy_accepted', array('applicator' => $user_info['id']), 'user');
177
		}
178
	}
179
180
	// Redirect back to chasing those squirrels, er, viewing those memes.
181
	redirectexit(!empty($_SESSION['redirect_url']) ? $_SESSION['redirect_url'] : '');
182
}
183
184
?>