-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
388 lines (320 loc) · 11.7 KB
/
lib.php
File metadata and controls
388 lines (320 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
defined('MOODLE_INTERNAL') || die();
/*
// Old Hook to inject JS in the login page if enabled in settings
function local_cpfformat_before_http_headers() {
global $PAGE;
// Only add script if enabled in settings
if (get_config('local_cpfformat', 'enabledcpfformatedlogin')) {
$PAGE->requires->js(new moodle_url('/local/cpfformat/cpfmask.js.php'));
}
}
*/
function local_cpfformat_pre_user_signup($user) {
if (!empty($_POST['profile_field_cpf'])) {
$cpf = preg_replace('/\D/', '', $_POST['profile_field_cpf']);
// Define como username
$user->username = $cpf;
}
}
// Extend the signup form to include CPF formatting and Brazilian cities
function local_cpfformat_extend_signup_form($mform)
{
global $CFG, $PAGE;
if (empty($CFG->registerauth) || $CFG->registerauth !== 'email') {
return;
}
//Verify if formatting on registration is enabled
$enabled = get_config('local_cpfformat', 'enabled');
// Fallback for manual config in config.php
if (empty($enabled) && isset($CFG->local_cpfformat_enabled)) {
$enabled = $CFG->local_cpfformat_enabled;
}
// Desable if manually disabled in config.php
if (isset($CFG->local_cpfformat_enabled) && $CFG->local_cpfformat_enabled === false) {
return;
}
// If not enabled, exit
if (empty($enabled)) {
return;
}
// Modify form field city to use Brazilian list with autocomplete
if ($mform->elementExists('city')) {
$mform->removeElement('city');
// add city field with Brazilian cities autocomplete
$cities = get_brazilian_cities();
$mform->addElement(
'autocomplete',
'city',
get_string('city'),
$cities,
array(
'noselectionstring' => get_string('selectacity', 'local_cpfformat'),
'allowclear' => true
)
);
$mform->addRule('city', get_string('required'), 'required', null, 'client');
}
// Add CPF field with formatting
$formatusername = get_config('local_cpfformat', 'formatusername');
if (!isset($formatusername) && isset($CFG->local_cpfformat_formatusername)) {
$formatusername = $CFG->local_cpfformat_formatusername;
}
$formatusername = !empty($formatusername);
// Username como CPF
if ($formatusername && $mform->elementExists('username')) {
$username = $mform->getElement('username');
$username->setLabel(get_string('cpfform', 'local_cpfformat'));
$username->updateAttributes([
'placeholder' => '000.000.000-00',
'maxlength' => '14'
]);
}
// Profile field CPF
if ($mform->elementExists('profile_field_cpf')) {
$cpf = $mform->getElement('profile_field_cpf');
$cpf->updateAttributes([
'placeholder' => '000.000.000-00',
'maxlength' => '14'
]);
}
// 🔥 JS UMA ÚNICA VEZ
$PAGE->requires->js_init_code(
local_cpfformat_get_js($formatusername)
);
$enabled = get_config('local_cpfformat', 'modifynamesurname');
if (empty($enabled)) {
return;
}
$PAGE->requires->js_init_code("
document.addEventListener('input', function(e) {
if (e.target.name === 'firstname' || e.target.name === 'lastname') {
e.target.value = e.target.value.toUpperCase();
}
});
document.addEventListener('submit', function(e) {
const f = e.target.querySelector('input[name=\"firstname\"]');
const l = e.target.querySelector('input[name=\"lastname\"]');
if (f) f.value = f.value.toUpperCase();
if (l) l.value = l.value.toUpperCase();
});
");
}
// Validate CPF format and uniqueness on signup
function local_cpfformat_validate_extend_signup_form($data) {
global $CFG, $DB;
// Só valida cadastro por e-mail
if (empty($CFG->registerauth) || $CFG->registerauth !== 'email') {
return [];
}
// Plugin habilitado?
$enabled = get_config('local_cpfformat', 'enabled');
if (empty($enabled) && isset($CFG->local_cpfformat_enabled)) {
$enabled = $CFG->local_cpfformat_enabled;
}
if (isset($CFG->local_cpfformat_enabled) && $CFG->local_cpfformat_enabled === false) {
return [];
}
if (empty($enabled)) {
return [];
}
// Flag: CPF no username?
$formatusername = get_config('local_cpfformat', 'formatusername');
if (!isset($formatusername) && isset($CFG->local_cpfformat_formatusername)) {
$formatusername = $CFG->local_cpfformat_formatusername;
}
$formatusername = !empty($formatusername);
$errors = [];
/*
* =========================================================
* MODO 1 — CPF NO USERNAME (comportamento antigo)
* =========================================================
*/
if ($formatusername && !empty($data['username'])) {
$cpfraw = $data['username'];
if (!preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/', $cpfraw)) {
$errors['username'] = get_string('invalidcpf', 'local_cpfformat');
return $errors;
}
$cpf = preg_replace('/\D/', '', $cpfraw);
if (!local_cpfformat_validate_cpf($cpf)) {
$errors['username'] = get_string('invalidcpfverify', 'local_cpfformat');
return $errors;
}
// CPF já usado como username
if (
$DB->record_exists('user', ['username' => $cpf]) ||
$DB->record_exists('user', ['username' => $cpfraw])
) {
$errors['username'] = get_string('thiscpfinuse', 'local_cpfformat');
return $errors;
}
// CPF já usado em campo de perfil
$sql = "SELECT 1
FROM {user} u
JOIN {user_info_data} uid ON uid.userid = u.id
JOIN {user_info_field} uif ON uif.id = uid.fieldid
WHERE uif.shortname = 'cpf'
AND (uid.data = ? OR uid.data = ?)
AND u.deleted = 0";
if ($DB->record_exists_sql($sql, [$cpf, $cpfraw])) {
$errors['username'] = get_string('thiscpfisregistred', 'local_cpfformat');
}
}
/*
* =========================================================
* MODO 2 — CPF SOMENTE NO profile_field_cpf
* =========================================================
*/
if (!$formatusername && !empty($data['profile_field_cpf'])) {
$cpfraw = $data['profile_field_cpf'];
if (!preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/', $cpfraw)) {
$errors['profile_field_cpf'] =
get_string('thiscpfisneedinformatted', 'local_cpfformat');
return $errors;
}
$cpf = preg_replace('/\D/', '', $cpfraw);
if (!local_cpfformat_validate_cpf($cpf)) {
$errors['profile_field_cpf'] =
get_string('invalidcpfverify', 'local_cpfformat');
return $errors;
}
// CPF já usado no campo de perfil
$sql = "SELECT 1
FROM {user_info_data} uid
JOIN {user_info_field} uif ON uif.id = uid.fieldid
WHERE uif.shortname = 'cpf'
AND (uid.data = ? OR uid.data = ?)";
if ($DB->record_exists_sql($sql, [$cpf, $cpfraw])) {
$errors['profile_field_cpf'] =
get_string('thiscpfisregistred', 'local_cpfformat');
}
}
return $errors;
}
// Validate CPF format
// This function checks if the CPF is valid according to the Brazilian CPF rules.
// It returns true if the CPF is valid, false otherwise.
function local_cpfformat_validate_cpf($cpf)
{
$cpf = preg_replace('/\D/', '', $cpf);
if (strlen($cpf) !== 11) {
return false;
}
if (preg_match('/^(\d)\1{10}$/', $cpf)) {
return false;
}
$soma = 0;
for ($i = 0; $i < 9; $i++) {
$soma += intval($cpf[$i]) * (10 - $i);
}
$resto = $soma % 11;
$dv1 = ($resto < 2) ? 0 : 11 - $resto;
if (intval($cpf[9]) !== $dv1) {
return false;
}
$soma = 0;
for ($i = 0; $i < 10; $i++) {
$soma += intval($cpf[$i]) * (11 - $i);
}
$resto = $soma % 11;
$dv2 = ($resto < 2) ? 0 : 11 - $resto;
if (intval($cpf[10]) !== $dv2) {
return false;
}
return true;
}
// Get Brazilian cities from local file or API IBGE
function get_brazilian_cities()
{
global $CFG;
$possible_paths = [
__DIR__ . '/municipios.json',
];
$json_file_path = null;
foreach ($possible_paths as $path) {
if (file_exists($path)) {
$json_file_path = $path;
break;
}
}
if ($json_file_path) {
$cities = file_get_contents($json_file_path);
$cities_array = json_decode($cities, true);
$city_names = array_column($cities_array, 'nome');
sort($city_names);
$sorted_cities = array('' => '');
foreach ($city_names as $name) {
$sorted_cities[$name] = $name;
}
return $sorted_cities;
} else {
return get_brazilian_cities_from_api();
}
}
// Fetch Brazilian cities from IBGE API if local file is not available
function get_brazilian_cities_from_api()
{
$url = 'https://servicodados.ibge.gov.br/api/v1/localidades/municipios';
$cities = file_get_contents($url);
if ($cities) {
file_put_contents(__DIR__ . '/municipios.json', $cities);
$cities_array = json_decode($cities, true);
$city_names = array_column($cities_array, 'nome');
sort($city_names);
$sorted_cities = array('' => '');
foreach ($city_names as $name) {
$sorted_cities[$name] = $name;
}
return $sorted_cities;
} else {
return array(
'' => '',
'São Paulo' => 'São Paulo',
'Rio de Janeiro' => 'Rio de Janeiro',
'Brasília' => 'Brasília',
'Salvador' => 'Salvador',
'Fortaleza' => 'Fortaleza'
);
}
}
// Get JavaScript code for CPF formatting
// This function returns a string containing the JavaScript code that formats CPF input fields.
function local_cpfformat_get_js($formatusername = false) {
$formatusername = $formatusername ? 'true' : 'false';
return "
require(['jquery'], function($) {
function formatCPF(value) {
value = value.replace(/\\D/g, '').substring(0, 11);
if (value.length <= 3) {
return value;
} else if (value.length <= 6) {
return value.replace(/(\\d{3})(\\d+)/, '\$1.\$2');
} else if (value.length <= 9) {
return value.replace(/(\\d{3})(\\d{3})(\\d+)/, '\$1.\$2.\$3');
} else {
return value.replace(/(\\d{3})(\\d{3})(\\d{3})(\\d+)/, '\$1.\$2.\$3-\$4');
}
}
function bindCPF(selector) {
$(document).on('input', selector, function() {
var newValue = formatCPF(this.value);
if (this.value !== newValue) {
this.value = newValue;
}
});
$(document).on('focus', selector, function() {
$(this)
.attr('placeholder', '000.000.000-00')
.attr('maxlength', '14');
});
}
// Sempre formata o profile_field_cpf
bindCPF('input[name=\"profile_field_cpf\"]');
// Só formata username se habilitado
if ($formatusername) {
bindCPF('input[name=\"username\"]');
}
});
";
}