1<?php
2require '../config.php';
3require '../engine/database/connect.php';
4require '../engine/function/general.php';
5require '../engine/function/users.php';
6?>
7
8<h1>Old database to Znote AAC compatibility converter:</h1>
9<p>Converting accounts and characters to work with Znote AAC:</p>
10<?php
11 // some variables
12 $updated_acc = 0;
13 // $updated_acc += 1;
14 $updated_char = 0;
15 // $updated_char += 1;
16 $updated_pass = 0;
17
18 // install functions
19 function fetch_all_accounts() {
20 $results = db()->fetchAll("SELECT `id` FROM `accounts`");
21 $accounts = array();
22 foreach ($results as $row) {
23 $accounts[] = $row['id'];
24 }
25 return (count($accounts) > 0) ? $accounts : false;
26 }
27
28 function user_count_znote_accounts() {
29 $data = db()->fetchOne("SELECT COUNT(`account_id`) AS `count` from `znote_accounts`;");
30 return ($data !== false) ? $data['count'] : 0;
31 }
32
33 function user_character_is_compatible($pid) {
34 $data = db()->fetchOne("SELECT COUNT(`player_id`) AS `count` from `znote_players` WHERE `player_id` = ?;", [$pid]);
35 return ($data !== false) ? $data['count'] : 0;
36 }
37
38 function fetch_znote_accounts() {
39 $results = db()->fetchAll("SELECT `account_id` FROM `znote_accounts`");
40 $accounts = array();
41 foreach ($results as $row) {
42 $accounts[] = $row['account_id'];
43 }
44 return (count($accounts) > 0) ? $accounts : false;
45 }
46 // end install functions
47
48 // count all accounts, znote accounts, find out which accounts needs to be converted.
49 $all_account = fetch_all_accounts();
50 $znote_account = fetch_znote_accounts();
51 if ($all_account !== false) {
52 if ($znote_account !== false) { // If existing znote compatible account exists:
53 foreach ($all_account as $all) { // Loop through every element in znote_account array
54 if (!in_array($all, $znote_account)) {
55 $old_accounts[] = $all;
56 }
57 }
58 } else {
59 foreach ($all_account as $all) {
60 $old_accounts[] = $all;
61 }
62 }
63 }
64 // end ^
65
66 // Send count status
67 if (isset($all_account) && $all_account !== false) {
68 echo '<br>';
69 echo 'Total accounts detected: '. count($all_account) .'.';
70
71 if (isset($znote_account) && $znote_account !== false) {
72 echo '<br>';
73 echo 'Znote compatible accounts detected: '. count($znote_account) .'.';
74
75 if (isset($old_accounts)) {
76 echo '<br>';
77 echo 'Old accounts detected: '. count($old_accounts) .'.';
78 }
79 } else {
80 echo '<br>';
81 echo 'Znote compatible accounts detected: 0.';
82 }
83 echo '<br>';
84 echo '<br>';
85 } else {
86 echo '<br>';
87 echo 'Total accounts detected: 0.';
88 }
89 // end count status
90
91 // validate accounts
92 if (isset($old_accounts) && $old_accounts !== false) {
93 $time = time();
94 foreach ($old_accounts as $old) {
95
96 // Make acc data compatible:
97 db()->execute("INSERT INTO `znote_accounts` (`account_id`, `ip`, `created`, `flag`) VALUES (?, 0, ?, '')", [$old, $time]);
98 $updated_acc += 1;
99
100 // Fetch unsalted password
101 if ($config['ServerEngine'] == 'TFS_03' && $config['salt'] === true) {
102 $password = user_data($old, 'password', 'salt');
103 $p_pass = str_replace($password['salt'],"",$password['password']);
104 }
105 if ($config['ServerEngine'] == 'TFS_02' || $config['salt'] === false) {
106 $password = user_data($old, 'password');
107 $p_pass = $password['password'];
108 }
109
110 // Verify lenght of password is less than 28 characters (most likely a plain password)
111 if (strlen($p_pass) < 28 && $old > 1) {
112 // encrypt it with sha1
113 if ($config['ServerEngine'] == 'TFS_02' || $config['salt'] === false) $p_pass = sha1($p_pass);
114 if ($config['ServerEngine'] == 'TFS_03' && $config['salt'] === true) $p_pass = sha1($password['salt'].$p_pass);
115
116 // Update their password so they are sha1 encrypted
117 db()->execute("UPDATE `accounts` SET `password` = ? WHERE `id` = ?;", [$p_pass, $old]);
118 $updated_pass += 1;
119 }
120
121 }
122 }
123
124 // validate players
125 if ($all_account !== false) {
126 $time = time();
127 foreach ($all_account as $all) {
128
129 $chars = user_character_list_player_id($all);
130 if ($chars !== false) {
131 // since char list is not false, we found a character list
132
133 // Lets loop through the character list
134 foreach ($chars as $c) {
135 // Is character not compatible yet?
136 if (user_character_is_compatible($c['id']) == 0) {
137 // Then lets make it compatible:
138 $cid = $c['id'];
139 db()->execute("INSERT INTO `znote_players` (`player_id`, `created`, `hide_char`, `comment`) VALUES (?, ?, 0, '')", [$cid, $time]);
140 $updated_char += 1;
141
142 }
143 }
144 }
145 }
146 }
147
148 echo "<br><b><font color=\"green\">SUCCESS</font></b><br><br>";
149 echo 'Updated accounts: '. $updated_acc .'<br>';
150 echo 'Updated characters: : '. $updated_char .'<br>';
151 echo 'Detected:'. $updated_pass .' accounts with plain passwords. These passwords has been given sha1 encryption.<br>';
152 echo '<br>All accounts and characters are compatible with Znote AAC<br>';
153?>
154