4 * This file is not part of the original UserCake system, but uses it.
7 require_once("models/config.php");
8 if (!securePage($_SERVER['PHP_SELF'])){die();}
10 $text_area="# Rows starting with '#' are ignored. Rows are of the form:\n# Username, Full Name, Email[, Title]";
17 $current_users = fetchAllUsers();
20 if ($_POST['action'] === "Download") // Download list of users and populate the text area
23 foreach ($current_users as $u)
25 if ($u['user_name'] !== "admin")
26 $text_area=$text_area."\n".$u['user_name'].",".$u['display_name'].",".$u['email'];
29 else if ($_POST['action'] === "Upload") // Upload users in the text area
32 if ($_POST['upload_mode'] === "purge")
34 $to_delete = fetchAllUsersWithoutPerm("Administrator");
35 if (count($to_delete) > 0)
37 if ($deletion_count = deleteUsers($to_delete)) {
38 $successes[] = lang("ACCOUNT_DELETIONS_SUCCESSFUL", array($deletion_count));
41 $errors[] = lang("SQL_ERROR");
46 $text_area = $_POST['userUpload'];
48 // Iterate through each row
49 $all_rows=preg_split("/((\r?\n)|(\r\n?))/", $_POST['userUpload']);
51 foreach ($all_rows as $row)
53 if (empty($row) or $row[0] === '#')
56 $fields = preg_split("/,/", $row);
57 if(count($fields) < 3)
59 $errors[] = "Warning: Ignoring row not in correct format: ".htmlspecialchars($row);
63 $username = trim($fields[0]);
64 $displayname = trim($fields[1]);
65 $email = trim($fields[2]);
66 $title = trim($fields[3]);
68 if(!isValidEmail($email))
70 $errors[] = "Enter a valid email for row: ".htmlspecialchars($row);
74 // generate the temporary password
75 //$password = generatePassword();
77 // hey, adrian suggested it
78 $password = "mctx".date("MY");
80 //Construct a user object
81 $user = new User($username,$displayname,$password,$email);
83 //Checking this flag tells us whether there were any errors such as possible data duplication occured
86 if($user->username_taken) $localerrors[] = lang("ACCOUNT_USERNAME_IN_USE",array($username));
87 if($user->displayname_taken) $localerrors[] = lang("ACCOUNT_DISPLAYNAME_IN_USE",array($displayname));
88 if($user->email_taken) $localerrors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));
92 //Attempt to add the user to the database, carry out finishing tasks like emailing the user (if required)
93 $user->userCakeAddUser(); //This doesn't return anything itself
95 if($user->mail_failure) $localerrors[] = lang("MAIL_ERROR");
96 if($user->sql_failure) $localerrors[] = lang("SQL_ERROR");
98 if(strlen($title) >= 50)
100 $localerrors[] = "Warning: User".$username." added but failed to set title: ".lang("ACCOUNT_TITLE_CHAR_LIMIT",array(1,50));
102 else if (count($localerrors) == 0 && strlen($title) > 0)
104 $user_id = fetchUserId($username); //So stupid, when you create a user, it doesn't return the user id
105 if (!updateTitle($user_id, $title))
107 $localerrors[] = "Warning: User ".$username." added but failed to set title: ". lang("SQL_ERROR");
112 if(count($localerrors) == 0)
114 $users[] = [$username, $password]; //Push user onto array
115 //$successes[] = ($user->success);
119 $errors = array_merge($errors, $localerrors);
124 if(count($users) > 0)
126 $successes[] = (count($users)." users created.");
127 $successes[] = "The temporary password is: "."mctx".date("MY");
128 $successes[] = "Please change this as soon as possible.";
129 // $successes[] = ("The list of usernames and passwords follow. You must save this!");
130 // foreach($users as $user)
132 // $successes[] = $user[0].",".$user[1];
140 require_once("models/header.php");
143 echo notificationBlock($errors,$successes);
145 echo '<div class="widget"><div class="title">Upload users</div>';
149 /* I can't get fucking file uploads to fucking work with fucking nginx
150 echo "<p> Please provide a CSV file of usernames and email addresses. </p>
151 <p> Click <a href=\"upload_users_example.csv\">here</a> for an example file. </p>
152 <div class=\"title\">Upload</div>
153 <form action=\"".$_SERVER['PHP_SELF']."\" enctype=\"multipart/form-data\" method=\"post\">
154 <input type=\"file\" name=\"userUpload\"/>
155 <input type=\"submit\" value=\"Upload\"/>
160 <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
161 <p> Action to take on adding users: </p>
162 <p> <input type=\"radio\" name=\"upload_mode\" value=\"keep\" checked/>Keep existing users and add these users</p>
163 <p> <input type=\"radio\" name=\"upload_mode\" value=\"purge\"/>Purge existing users and add these users</p>
164 <input type=\"submit\" name=\"action\" value=\"Upload\"/>
165 <input type=\"submit\" name=\"action\" value=\"Download\"/>
166 <input type=\"submit\" name=\"action\" value=\"Reset\"/>
167 <p> Enter or copy/paste user information below (resize the text area if necessary): </p>
169 <textarea name=\"userUpload\" rows=\"50\" cols=\"100\" style=\"width: 100%\">".$text_area."</textarea> </p>