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")
\r
34 $to_delete = fetchAllUsersWithoutPerm("Administrator");
\r
35 if (count($to_delete) > 0)
\r
37 if ($deletion_count = deleteUsers($to_delete)) {
\r
38 $successes[] = lang("ACCOUNT_DELETIONS_SUCCESSFUL", array($deletion_count));
\r
41 $errors[] = lang("SQL_ERROR");
\r
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);
\r
57 if(count($fields) < 3)
\r
59 $errors[] = "Warning: Ignoring row not in correct format: ".htmlspecialchars($row);
\r
63 $username = trim($fields[0]);
64 $displayname = trim($fields[1]);
65 $email = trim($fields[2]);
\r
66 $title = trim($fields[3]);
\r
68 if(!isValidEmail($email))
\r
70 $errors[] = "Enter a valid email for row: ".htmlspecialchars($row);
\r
74 // generate the temporary password
75 $password = generatePassword();
77 //Construct a user object
78 $user = new User($username,$displayname,$password,$email);
80 //Checking this flag tells us whether there were any errors such as possible data duplication occured
83 if($user->username_taken) $localerrors[] = lang("ACCOUNT_USERNAME_IN_USE",array($username));
84 if($user->displayname_taken) $localerrors[] = lang("ACCOUNT_DISPLAYNAME_IN_USE",array($displayname));
85 if($user->email_taken) $localerrors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));
89 //Attempt to add the user to the database, carry out finishing tasks like emailing the user (if required)
90 $user->userCakeAddUser(); //This doesn't return anything itself
92 if($user->mail_failure) $localerrors[] = lang("MAIL_ERROR");
93 if($user->sql_failure) $localerrors[] = lang("SQL_ERROR");
95 if(strlen($title) >= 50)
\r
97 $localerrors[] = "Warning: User".$username." added but failed to set title: ".lang("ACCOUNT_TITLE_CHAR_LIMIT",array(1,50));
\r
99 else if (count($localerrors) == 0 && strlen($title) > 0)
\r
101 $user_id = fetchUserId($username); //So stupid, when you create a user, it doesn't return the user id
\r
102 if (!updateTitle($user_id, $title))
\r
104 $localerrors[] = "Warning: User ".$username." added but failed to set title: ". lang("SQL_ERROR");
\r
109 if(count($localerrors) == 0)
111 $users[] = [$username, $password]; //Push user onto array
112 //$successes[] = ($user->success);
116 $errors = array_merge($errors, $localerrors);
\r
121 if(count($users) > 0)
\r
123 $successes[] = (count($users)." users created.");
\r
124 $successes[] = ("The list of usernames and passwords follow. You must save this!");
\r
125 foreach($users as $user)
\r
127 $successes[] = $user[0].",".$user[1];
\r
135 require_once("models/header.php");
\r
138 echo notificationBlock($errors,$successes);
\r
140 echo '<div class="widget"><div class="title">Upload users</div>';
144 /* I can't get fucking file uploads to fucking work with fucking nginx
145 echo "<p> Please provide a CSV file of usernames and email addresses. </p>
146 <p> Click <a href=\"upload_users_example.csv\">here</a> for an example file. </p>
147 <div class=\"title\">Upload</div>
148 <form action=\"".$_SERVER['PHP_SELF']."\" enctype=\"multipart/form-data\" method=\"post\">
149 <input type=\"file\" name=\"userUpload\"/>
150 <input type=\"submit\" value=\"Upload\"/>
155 <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
156 <p> Action to take on adding users: </p>
157 <p> <input type=\"radio\" name=\"upload_mode\" value=\"keep\" checked/>Keep existing users and add these users</p>
158 <p> <input type=\"radio\" name=\"upload_mode\" value=\"purge\"/>Purge existing users and add these users</p>
159 <input type=\"submit\" name=\"action\" value=\"Upload\"/>
160 <input type=\"submit\" name=\"action\" value=\"Download\"/>
161 <input type=\"submit\" name=\"action\" value=\"Reset\"/>
162 <p> Enter or copy/paste user information below (resize the text area if necessary): </p>
164 <textarea name=\"userUpload\" rows=\"50\" cols=\"100\" style=\"width: 100%\">".$text_area."</textarea> </p>