<?php /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /** Si occupa di registrare un nuovo utente method: POST params: hash: username e password oscurati (vedi /js/site.js Login.encrypt) returns: success: false => errore, true => OK error: messaggio di errore se success e' false */ require_once "__inc__.php"; $response = new RestfulResponse("json"); $av = new AutoValidator("frm-register", $_POST); if ($av->validate()) { if (isset($_POST['question']) && $_POST['question'] == "10") { $email = db_escape(purify(trim(Charset::Utf8ToDB($_POST['email'])))); $creds = LoginUtils::HashToCredentials(db_escape($_POST['hash'])); $username = db_escape(purify(trim(Charset::Utf8ToDB($creds['username'])))); $password = db_escape(purify(trim(Charset::Utf8ToDB($creds['password'])))); // Username libero? if (!DB::FindOne("SELECT 1 FROM users WHERE user = \"{$username}\"")) { // Legacy: Un timestamp sarebbe stato meglio, ma non dobbiamo fare nulla con questo dato, quindi va bene cosi' $data = date("d/m/Y G:i"); $description = "Normal User"; $md5 = LoginUtils::Md5FromCredentials($username, $password); // Tutto a posto exequery(sprintf('INSERT INTO users (user, mail, ip, os_browser, date, description, permission, verified, md5, last_login_timestamp, last_login_ip, newsletter) VALUES ("%s", "%s", "%s", "%s", "%s", "%s", %s, %s, "%s", %s, "%s", %s)', $username, $email, get_ip(), db_escape(purify($_SERVER["HTTP_USER_AGENT"])), $data, $description, User::PERMISSION_USER, 1, $md5, time(), get_ip(), 1));
public function render() { $vb = new ViewBuilder("form/form.html"); // Calcola i campi di validazione // da passare alla view $all_rules = array(); for ($i = 0; $i < count($this->fields); $i++) { if (!isset($this->fields[$i]['value'])) { $this->fields[$i]['value'] = ""; } if (!isset($this->fields[$i]['label'])) { $this->fields[$i]['label'] = ""; } if (!isset($this->fields[$i]['send'])) { $this->fields[$i]['send'] = true; } $attributes = ""; // Attributi' da aggiungere ad ogni campo $attributes .= ' data-reset-value="' . $this->fields[$i]['value'] . '"'; // valore di default $attributes .= ' data-formfield '; $attributes .= ' id="' . $this->getDOMId($this->fields[$i]["id"]) . '" '; $attributes .= ' name="' . $this->fields[$i]["id"] . '" '; if (!$this->fields[$i]['send']) { $attributes .= ' data-dontsend="true" '; } // Attributi specificati dall'utente if (isset($this->fields[$i]['attrs'])) { $attributes .= ' ' . $this->fields[$i]['attrs'] . ' '; } if (isset($this->fields[$i]['validation'])) { // Una sola regola (stringa)? if (!is_array($this->fields[$i]['validation'])) { // Wrappa $this->fields[$i]['validation'] = array($this->fields[$i]['validation']); } $rules = array(); foreach ($this->fields[$i]['validation'] as $validation_rule) { $parts = explode(",", $validation_rule); // Trim for ($j = 0; $j < count($parts); $j++) { $parts[$j] = trim($parts[$j]); } // Le regole di validazione devono includere l'ID dell'elemento // siccome non vogliamo fare ripetizioni, (l'ID e' gia' conosciuto) // lo aggiungiamo dinamicamente // "[if:FIELDNAME=VALUE,]REQUIREMENT,fieldname[,fieldname2[,fieldname3,date_flag]],error message" if (count($parts) >= 2) { $id_offset = 1; if (strpos($parts[0], "if:") === 0) { $id_offset = 2; } array_splice($parts, $id_offset, 0, array($this->fields[$i]['id'])); } $rules[] = join(",", $parts); } // Le regole verranno aggiunte assieme al campo tramite data-validate $attributes .= ' data-validate="' . join("|", $rules) . '" '; // Tieni traccia di tutte le regole per l'auto validazione lato server // ignorando le regole per i campi che non verranno inviati if ($this->fields[$i]['send']) { $all_rules = array_merge($all_rules, $rules); } } // Ogni elemento ha questi tag aggiuntivi $this->fields[$i]['formbuilder_attrs'] = $attributes; } // Esporta tutti i membri di questa classe alla view $members = array(); foreach ($this as $key => $value) { $members[$key] = $value; } $vb->setValues($members); // Calcola alcuni campi $vb->addValue("control_buttons_count", count($this->control_buttons)); $vb->addValue("widgets_count", count($this->widgets)); // Imposta l'auto validazione se necessario if ($this->serverside_autovalidate) { $av = new AutoValidator($this->form_id); $av->setRules($all_rules); $vb->addValue("av_token_id", AutoValidator::TOKEN_ID); $vb->addValue("av_token", $av->getToken()); } return $vb->render(); }