1<?php require_once 'engine/init.php';
2protect_page();
3theme_open();
4
5if (empty($_POST) === false) {
6 // $_POST['']
7 $required_fields = array('name', 'selected_town');
8 foreach($_POST as $key=>$value) {
9 if (empty($value) && in_array($key, $required_fields) === true) {
10 $errors[] = t('reg.fill_all');
11 break 1;
12 }
13 }
14
15 // check errors (= user exist, pass long enough
16 if (empty($errors) === true) {
17 if (!Token::isValid($_POST['token'])) {
18 $errors[] = t('login.token_invalid');
19 }
20 $_POST['name'] = validate_name($_POST['name']);
21 if ($_POST['name'] === false) {
22 $errors[] = t('acc.name_max_words');
23 } else {
24 if (user_character_exist($_POST['name']) !== false) {
25 $errors[] = t('acc.name_taken');
26 }
27 if (!preg_match("/^[a-zA-Z ]+$/", $_POST['name'])) {
28 $errors[] = t('acc.name_letters');
29 }
30 if (strlen($_POST['name']) < $config['minL'] || strlen($_POST['name']) > $config['maxL']) {
31 $errors[] = t('acc.name_length', ['min' => $config['minL'], 'max' => $config['maxL']]);
32 }
33 // name restriction
34 $resname = explode(" ", $_POST['name']);
35 $username = $_POST['name'];
36 foreach($resname as $res) {
37 if(in_array(strtolower($res), $config['invalidNameTags'])) {
38 $errors[] = t('reg.restricted_word');
39 }
40 if(strlen($res) == 1) {
41 $errors[] = t('reg.words_too_short');
42 }
43 }
44 if(in_array(strtolower($username), $config['creatureNameTags'])) {
45 $errors[] = t('createchar.creature_name');
46 }
47 // Validate vocation id
48 if (!in_array((int)$_POST['selected_vocation'], $config['available_vocations'])) {
49 $errors[] = t('createchar.bad_vocation');
50 }
51 // Validate town id
52 if (!in_array((int)$_POST['selected_town'], $config['available_towns'])) {
53 $errors[] = t('createchar.bad_town');
54 }
55 // Validate gender id
56 if (!in_array((int)$_POST['selected_gender'], array(0, 1))) {
57 $errors[] = t('createchar.bad_gender');
58 }
59 if (vocation_id_to_name($_POST['selected_vocation']) === false) {
60 $errors[] = t('createchar.no_vocation');
61 }
62 if (town_id_to_name($_POST['selected_town']) === false) {
63 $errors[] = t('createchar.no_town');
64 }
65 if (gender_exist($_POST['selected_gender']) === false) {
66 $errors[] = t('createchar.no_gender');
67 }
68 // Char count
69 $char_count = user_character_list_count($session_user_id);
70 if ($char_count >= $config['max_characters'] && !is_admin($user_data)) {
71 $errors[] = t('createchar.max_chars', ['max' => $config['max_characters']]);
72 }
73 if (validate_ip(getIP()) === false && $config['validate_IP'] === true) {
74 $errors[] = t('reg.bad_ip');
75 }
76 }
77 }
78}
79
80/**
81 * What the view has to render: 'success', 'errors' or 'form'.
82 * The character creation stays here - a theme must never carry it.
83 */
84$formState = 'form';
85
86if (isset($_GET['success']) && empty($_GET['success'])) {
87 $formState = 'success';
88
89} elseif (empty($_POST) === false && empty($errors) === true) {
90
91 if ($config['log_ip']) {
92 znote_visitor_insert_detailed_data(2);
93 }
94
95 $character_data = array(
96 'name' => format_character_name($_POST['name']),
97 'account_id' => $session_user_id,
98 'vocation' => $_POST['selected_vocation'],
99 'town_id' => $_POST['selected_town'],
100 'sex' => $_POST['selected_gender'],
101 'lastip' => getIPLong(),
102 'created' => time()
103 );
104
105 user_create_character($character_data);
106
107 znote_hook('character.created', array(
108 'name' => $character_data['name'],
109 'account_id' => $character_data['account_id'],
110 'vocation' => $character_data['vocation'],
111 ));
112
113 header('Location: createcharacter.php?success');
114 exit;
115
116} elseif (empty($errors) === false) {
117 $formState = 'errors';
118}
119
120view('createcharacter');
121
122theme_close();
123