segment the admin gui
[matches/MCTX3420.git] / testing / MCTXWeb / public_html / users / admin_upload_users.php
1 <?php
2
3 /**
4  * This file is not part of the original UserCake system, but uses it.
5  */
6
7 require_once("models/config.php");
8 if (!securePage($_SERVER['PHP_SELF'])){die();}
9
10 $text_area="# Rows starting with '#' are ignored. Rows are of the form:\n# Username, Full Name, Email[, Title]";
11
12
13 $show_form = true;
14 if (!empty($_POST))
15 {
16
17   $current_users = fetchAllUsers();
18
19   // Check form action
20   if ($_POST['action'] === "Download") // Download list of users and populate the text area
21   {
22   
23     foreach ($current_users as $u)
24     {
25       if ($u['user_name'] !== "admin")
26         $text_area=$text_area."\n".$u['user_name'].",".$u['display_name'].",".$u['email'];
27     }
28   }
29   else if ($_POST['action'] === "Upload") // Upload users in the text area
30   {
31 \r
32     if ($_POST['upload_mode'] === "purge")\r
33     {\r
34       $to_delete = fetchAllUsersWithoutPerm("Administrator");\r
35       if (count($to_delete) > 0)\r
36       {\r
37         if ($deletion_count = deleteUsers($to_delete)) {\r
38           $successes[] = lang("ACCOUNT_DELETIONS_SUCCESSFUL", array($deletion_count));\r
39         }\r
40         else {\r
41           $errors[] = lang("SQL_ERROR");\r
42         }\r
43       }\r
44     }\r
45   
46     $text_area = $_POST['userUpload'];
47
48     // Iterate through each row
49     $all_rows=preg_split("/((\r?\n)|(\r\n?))/", $_POST['userUpload']);
50     
51     foreach ($all_rows as $row)
52     {
53       if (empty($row) or $row[0] === '#')
54         continue;
55
56       $fields = preg_split("/,/",  $row);\r
57       if(count($fields) < 3)\r
58       {\r
59         $errors[] = "Warning: Ignoring row not in correct format: ".htmlspecialchars($row);\r
60         continue;\r
61       }\r
62       
63       $username = trim($fields[0]);
64       $displayname = trim($fields[1]);
65       $email = trim($fields[2]);\r
66       $title = trim($fields[3]);\r
67       \r
68       if(!isValidEmail($email))\r
69       {\r
70         $errors[] = "Enter a valid email for row: ".htmlspecialchars($row);\r
71         continue;\r
72       }\r
73       
74       // generate the temporary password
75       $password = generatePassword();
76
77       //Construct a user object
78       $user = new User($username,$displayname,$password,$email);
79   
80       //Checking this flag tells us whether there were any errors such as possible data duplication occured
81       if(!$user->status)
82       {
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));            
86       }
87       else
88       {
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
91         
92         if($user->mail_failure) $localerrors[] = lang("MAIL_ERROR");
93         if($user->sql_failure)  $localerrors[] = lang("SQL_ERROR");
94         \r
95         if(strlen($title) >= 50)\r
96         {\r
97           $localerrors[] = "Warning: User".$username." added but failed to set title: ".lang("ACCOUNT_TITLE_CHAR_LIMIT",array(1,50));\r
98         }\r
99         else if (count($localerrors) == 0 && strlen($title) > 0)\r
100         {\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
103           {\r
104             $localerrors[] = "Warning: User ".$username." added but failed to set title: ". lang("SQL_ERROR");\r
105           }\r
106         }
107       }
108
109       if(count($localerrors) == 0)
110       {\r
111         $users[] = [$username, $password]; //Push user onto array
112                 //$successes[] = ($user->success);
113       }\r
114       else\r
115       {\r
116         $errors = array_merge($errors, $localerrors);\r
117       }
118
119     }
120  \r
121     if(count($users) > 0)\r
122     {\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
126       {\r
127         $successes[] = $user[0].",".$user[1];\r
128       }\r
129     }
130   }
131
132
133 }\r
134 \r
135 require_once("models/header.php");\r
136 startPage();\r
137 \r
138 echo notificationBlock($errors,$successes);\r
139 \r
140 echo '<div class="widget"><div class="title">Upload users</div>';
141
142 if ($show_form)
143 {
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\"/>
151   </form>";
152   */
153   echo "
154  
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>
163   <p>
164   <textarea name=\"userUpload\" rows=\"50\" cols=\"100\" style=\"width: 100%\">".$text_area."</textarea> </p>
165
166   </form>";
167 }\r
168 \r
169 echo '</div>';\r
170 finishPage();
171   
172 ?> 
173

UCC git Repository :: git.ucc.asn.au