| 1 | <?php |
|---|
| 2 | /********************************************************************\ |
|---|
| 3 | * This program is free software; you can redistribute it and/or * |
|---|
| 4 | * modify it under the terms of the GNU General Public License as * |
|---|
| 5 | * published by the Free Software Foundation; either version 2 of * |
|---|
| 6 | * the License, or (at your option) any later version. * |
|---|
| 7 | * * |
|---|
| 8 | * This program is distributed in the hope that it will be useful, * |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 11 | * GNU General Public License for more details. * |
|---|
| 12 | * * |
|---|
| 13 | * You should have received a copy of the GNU General Public License* |
|---|
| 14 | * along with this program; if not, contact: * |
|---|
| 15 | * * |
|---|
| 16 | * Free Software Foundation Voice: +1-617-542-5942 * |
|---|
| 17 | * 59 Temple Place - Suite 330 Fax: +1-617-542-2652 * |
|---|
| 18 | * Boston, MA 02111-1307, USA gnu@gnu.org * |
|---|
| 19 | * * |
|---|
| 20 | \********************************************************************/ |
|---|
| 21 | /**@file AbstractDb.php |
|---|
| 22 | * @author Copyright (C) 2004 Technologies Coeus inc. |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | define('BASEPATH','../'); |
|---|
| 26 | require_once BASEPATH.'include/common.php'; |
|---|
| 27 | require_once BASEPATH.'classes/Style.php'; |
|---|
| 28 | require_once BASEPATH.'classes/Security.php'; |
|---|
| 29 | $security=new Security(); |
|---|
| 30 | $security->requireAdmin(); |
|---|
| 31 | |
|---|
| 32 | /** Affiche les informations sur le fichier envoy� par le client |
|---|
| 33 | */ |
|---|
| 34 | function PrintUploadedFileInfo($form_name_file) |
|---|
| 35 | { |
|---|
| 36 | echo "Nom du fichier envoy�:".$_FILES[$form_name_file]['name']."<br>"; |
|---|
| 37 | echo "Taille: ".$_FILES[$form_name_file]['size']." octets"."<br>"; |
|---|
| 38 | echo "Mime type: ".$_FILES[$form_name_file]['type']."<br>"; |
|---|
| 39 | echo "Nom du fichier temporaire sur le serveur: ".$_FILES[$form_name_file]['tmp_name']."<br>"; |
|---|
| 40 | echo "Erreurs au cours du transfert: ".$_FILES[$form_name_file]['error']."<br>"; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | $style = new Style(); |
|---|
| 44 | echo $style->GetHeader(HOTSPOT_NETWORK_NAME.' Import NoCat passwd file'); |
|---|
| 45 | |
|---|
| 46 | echo "<div id='head'><h1>"._('NoCat passwd file (user database) import')."</h1></div>\n"; |
|---|
| 47 | echo "<div id='content'>\n"; |
|---|
| 48 | |
|---|
| 49 | if(empty($_REQUEST['action'])) |
|---|
| 50 | { |
|---|
| 51 | echo "<form name=upload_file enctype='multipart/form-data' action='' method='post'>\n"; |
|---|
| 52 | |
|---|
| 53 | echo "<p>"._('Please select the NoCat passwd file you want to import.')."</p>\n"; |
|---|
| 54 | echo "<input name='userfile' type='file' />\n"; |
|---|
| 55 | echo "<input type='hidden' name='action' value='upload_file' />\n"; |
|---|
| 56 | echo "<input type='hidden' name='MAX_FILE_SIZE' value='300000' />\n"; |
|---|
| 57 | echo "<p>"._("Accept users with no email adresses (Normally, NoCat usernames are expected to be the user's email adress, and the username is generated from the prefix.")."\n"; |
|---|
| 58 | echo "<input type='checkbox' name='accept_empty_email' value='true' /></p>\n"; |
|---|
| 59 | echo "<p><input name='upload' type='submit' value='"._("Upload file")."' />\n"; |
|---|
| 60 | |
|---|
| 61 | echo "<input type='checkbox' name='import_confirm' value='true' />\n"; |
|---|
| 62 | echo _("I am sure I want to import (Otherwise, the import will only be simulated).")."</p>\n"; |
|---|
| 63 | echo "</form>\n"; |
|---|
| 64 | echo "</div>\n"; |
|---|
| 65 | } |
|---|
| 66 | else if ($_REQUEST['action'] == 'upload_file') |
|---|
| 67 | { |
|---|
| 68 | if($_FILES['userfile']['tmp_name']) |
|---|
| 69 | { |
|---|
| 70 | |
|---|
| 71 | $import_user = Array(); |
|---|
| 72 | /* $import_user[$username]['email'] |
|---|
| 73 | $import_user[$username]['passwd_hash'] |
|---|
| 74 | $import_user[$username]['original_username'] |
|---|
| 75 | $import_user[$username]['username_modified_because_of'] |
|---|
| 76 | $import_user[$username]['is_rejected'] |
|---|
| 77 | $import_user[$username]['reject_reason'] |
|---|
| 78 | */ |
|---|
| 79 | |
|---|
| 80 | PrintUploadedFileInfo('userfile'); |
|---|
| 81 | |
|---|
| 82 | $fp = fopen($_FILES['userfile']['tmp_name'], "rb"); |
|---|
| 83 | $output = null; |
|---|
| 84 | |
|---|
| 85 | $row = 1; |
|---|
| 86 | while (!feof($fp)) |
|---|
| 87 | { |
|---|
| 88 | $data = fgets ($fp); |
|---|
| 89 | $num = count($data); |
|---|
| 90 | echo "<hr><p>Line $row: $data<br />\n"; |
|---|
| 91 | |
|---|
| 92 | if(preg_match("/^(.*):(.*)$/", $data, $matches)) |
|---|
| 93 | { |
|---|
| 94 | //echo "<p><pre>". print_r($matches)."</pre></p>\n"; |
|---|
| 95 | $nocat_username = $matches[1]; |
|---|
| 96 | $nocat_password_hash=$matches[2]; |
|---|
| 97 | $matches = null; |
|---|
| 98 | if(preg_match( "/^(.*)@.*$/", $nocat_username, $matches)) |
|---|
| 99 | { |
|---|
| 100 | $email = $nocat_username; |
|---|
| 101 | $original_username = $matches[1]; |
|---|
| 102 | } |
|---|
| 103 | else |
|---|
| 104 | { |
|---|
| 105 | echo "<p class=info>NoCat username isn't an email</p>"; |
|---|
| 106 | $email = ''; |
|---|
| 107 | $original_username = $nocat_username; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | echo "<p class=info>Generating temporary user from: $original_username; Checking internal duplicates (duplicate usernames in the imported file)</p>\n"; |
|---|
| 111 | $username_modified_because_of=null; |
|---|
| 112 | $username=$original_username; |
|---|
| 113 | if(isset($import_user[$username])) |
|---|
| 114 | { |
|---|
| 115 | $index=1; |
|---|
| 116 | while(isset($import_user[$username])) |
|---|
| 117 | { |
|---|
| 118 | $username_modified_because_of=$username; |
|---|
| 119 | echo "<p class=warning>Can't use $username because it was already generated from the imported file</p>\n"; |
|---|
| 120 | $username=$original_username."_$index"; |
|---|
| 121 | $index++; |
|---|
| 122 | } |
|---|
| 123 | echo "<p class=info>Final username is now $username</p>\n"; |
|---|
| 124 | } |
|---|
| 125 | else |
|---|
| 126 | { |
|---|
| 127 | echo "<p class=info>Final username is still $username</p>\n"; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | $import_user[$username]['email']=$email; |
|---|
| 131 | $import_user[$username]['passwd_hash']=convert_nocat_password_hash($nocat_password_hash); |
|---|
| 132 | $import_user[$username]['original_username']=$original_username; |
|---|
| 133 | $import_user[$username]['username_modified_because_of']=$username_modified_because_of; |
|---|
| 134 | $import_user[$username]['is_rejected']=null; |
|---|
| 135 | $import_user[$username]['reject_reason']=null; |
|---|
| 136 | } |
|---|
| 137 | else |
|---|
| 138 | { |
|---|
| 139 | echo "<p class=info>Line skipped</p>\n"; |
|---|
| 140 | } |
|---|
| 141 | $row++; |
|---|
| 142 | } |
|---|
| 143 | echo "<hr><p>Total of ". ($row-1) ." lines read and ".count($import_user)." candidate users generated.<br />\n"; |
|---|
| 144 | foreach($import_user as $username => $user) |
|---|
| 145 | { |
|---|
| 146 | //echo "<p>$username</pre></p>\n"; |
|---|
| 147 | //echo "<p><pre>". print_r($user)."</pre></p>\n"; |
|---|
| 148 | $import_user[$username]['is_rejected']=false; |
|---|
| 149 | |
|---|
| 150 | if(!empty($user['email'])) |
|---|
| 151 | { |
|---|
| 152 | $email_str = $db->EscapeString($user['email']); |
|---|
| 153 | $db->ExecSqlUniqueRes("SELECT email FROM users WHERE email='$email_str'", $user_info_email, false); |
|---|
| 154 | if($user_info_email!=null) |
|---|
| 155 | { |
|---|
| 156 | $import_user[$username]['is_rejected']=true; |
|---|
| 157 | $import_user[$username]['reject_reason'] .= "<p class=error>"._('Sorry, a user account is already associated to the email address: ')."$user[email]</p>\n"; |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | else if(empty($_REQUEST['accept_empty_email'])) |
|---|
| 161 | { |
|---|
| 162 | $import_user[$username]['is_rejected']=true; |
|---|
| 163 | $import_user[$username]['reject_reason'] .= "<p class=error>"._('Sorry, the user must have a email adress.')."</p>\n";null; |
|---|
| 164 | } |
|---|
| 165 | else |
|---|
| 166 | { |
|---|
| 167 | $username_str = $db->EscapeString($username); |
|---|
| 168 | $db->ExecSqlUniqueRes("SELECT user_id FROM users WHERE user_id='$username_str'", $user_info_username, false); |
|---|
| 169 | if($user_info_username!=null) |
|---|
| 170 | { |
|---|
| 171 | $import_user[$username]['is_rejected']=true; |
|---|
| 172 | $import_user[$username]['reject_reason'] .= "<p class=error>"._('Sorry, a user account already exists with the username: ')."$username</p>\n"; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | if(!empty($_REQUEST['import_confirm']) && $_REQUEST['import_confirm']=='true' && $import_user[$username]['is_rejected']==false) |
|---|
| 177 | { |
|---|
| 178 | $status = ACCOUNT_STATUS_ALLOWED; |
|---|
| 179 | $token = gentoken(); |
|---|
| 180 | $reg_date = iso8601_date(time()); |
|---|
| 181 | $password_hash = $db->EscapeString($user['passwd_hash']); |
|---|
| 182 | $username = $db->EscapeString($username); |
|---|
| 183 | $email = $db->EscapeString($user['email']); |
|---|
| 184 | $sql = "INSERT INTO users (user_id,email,pass,account_status,validation_token,reg_date) VALUES ('$username','$email','$password_hash','{$status}','{$token}','{$reg_date}')"; |
|---|
| 185 | $update_successful = $db->ExecSqlUpdate($sql); |
|---|
| 186 | if ($update_successful) |
|---|
| 187 | { |
|---|
| 188 | //send_validation_email($email); |
|---|
| 189 | $showform=false; |
|---|
| 190 | } |
|---|
| 191 | else |
|---|
| 192 | { |
|---|
| 193 | $import_user[$username]['is_rejected']=true; |
|---|
| 194 | $import_user[$username]['reject_reason'] .= "<p class=error>"._('SQL error on: ')."$sql</p>\n"; |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | echo "<h2>"._('Report')."</h2>\n"; |
|---|
| 201 | /* List rejected users */ |
|---|
| 202 | echo "<table class='spreadsheet'>\n"; |
|---|
| 203 | $count_reject=0; |
|---|
| 204 | $count_success=0; |
|---|
| 205 | foreach($import_user as $username => $user) |
|---|
| 206 | { |
|---|
| 207 | if($user['is_rejected']==true) |
|---|
| 208 | { |
|---|
| 209 | $count_reject++; |
|---|
| 210 | echo "<tr class='spreadsheet'>\n"; |
|---|
| 211 | echo "<td class='spreadsheet'>$username</td><td class='spreadsheet'>$user[reject_reason]</td>\n"; |
|---|
| 212 | echo "</tr>\n"; |
|---|
| 213 | } |
|---|
| 214 | else |
|---|
| 215 | { |
|---|
| 216 | $count_success++; |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | echo "<thead><tr class='spreadsheet'><th class='spreadsheet' colspan=2>$count_reject rejected users</th></tr>\n"; |
|---|
| 220 | echo "<tr class='spreadsheet'><th class='spreadsheet'>Username</th><th class='spreadsheet'>Reason for rejection</th></tr></thead>\n"; |
|---|
| 221 | echo "</table>\n"; |
|---|
| 222 | |
|---|
| 223 | /* List users imported with mangled usernames */ |
|---|
| 224 | echo "<table class='spreadsheet'>\n"; |
|---|
| 225 | $count_mangled=0; |
|---|
| 226 | foreach($import_user as $username => $user) |
|---|
| 227 | { |
|---|
| 228 | if($user['is_rejected']==false&&!empty($user['username_modified_because_of'])) |
|---|
| 229 | { |
|---|
| 230 | $count_mangled++; |
|---|
| 231 | echo "<tr class='spreadsheet'>\n"; |
|---|
| 232 | echo "<td class='spreadsheet'>$username</td><td class='spreadsheet'>$user[original_username]</td><td class='spreadsheet'>$user[username_modified_because_of]</td>\n"; |
|---|
| 233 | echo "</tr>\n"; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | echo "<thead><tr class='spreadsheet'><th class='spreadsheet' colspan=3>$count_mangled users were imported with modified usernames</th></tr>\n"; |
|---|
| 237 | echo "<tr class='spreadsheet'><th class='spreadsheet'>Username</th><th class='spreadsheet'>Original username</th><th class='spreadsheet'>Changed because of user</th></tr></thead\n"; |
|---|
| 238 | echo "</table>\n"; |
|---|
| 239 | |
|---|
| 240 | echo "<h2>$count_success user(s) successfully imported ($count_mangled of them had their username modified), $count_reject user(s)rejected</h2>\n"; |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | echo "</div>\n"; |
|---|
| 244 | |
|---|
| 245 | ?> |
|---|
| 246 | |
|---|