Automatic commit of irc logs
[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
32     if ($_POST['upload_mode'] === "purge")
33     {
34       $to_delete = fetchAllUsersWithoutPerm("Administrator");
35       if (count($to_delete) > 0)
36       {
37         if ($deletion_count = deleteUsers($to_delete)) {
38           $successes[] = lang("ACCOUNT_DELETIONS_SUCCESSFUL", array($deletion_count));
39         }
40         else {
41           $errors[] = lang("SQL_ERROR");
42         }
43       }
44     }
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);
57       if(count($fields) < 3)
58       {
59         $errors[] = "Warning: Ignoring row not in correct format: ".htmlspecialchars($row);
60         continue;
61       }
62       
63       $username = trim($fields[0]);
64       $displayname = trim($fields[1]);
65       $email = trim($fields[2]);
66       $title = trim($fields[3]);
67       
68       if(!isValidEmail($email))
69       {
70         $errors[] = "Enter a valid email for row: ".htmlspecialchars($row);
71         continue;
72       }
73       
74       // generate the temporary password
75       //$password = generatePassword();
76
77       // hey, adrian suggested it
78       $password = "mctx".date("MY");
79
80       //Construct a user object
81       $user = new User($username,$displayname,$password,$email);
82   
83       //Checking this flag tells us whether there were any errors such as possible data duplication occured
84       if(!$user->status)
85       {
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));            
89       }
90       else
91       {
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
94         
95         if($user->mail_failure) $localerrors[] = lang("MAIL_ERROR");
96         if($user->sql_failure)  $localerrors[] = lang("SQL_ERROR");
97         
98         if(strlen($title) >= 50)
99         {
100           $localerrors[] = "Warning: User".$username." added but failed to set title: ".lang("ACCOUNT_TITLE_CHAR_LIMIT",array(1,50));
101         }
102         else if (count($localerrors) == 0 && strlen($title) > 0)
103         {
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))
106           {
107             $localerrors[] = "Warning: User ".$username." added but failed to set title: ". lang("SQL_ERROR");
108           }
109         }
110       }
111
112       if(count($localerrors) == 0)
113       {
114         $users[] = [$username, $password]; //Push user onto array
115                 //$successes[] = ($user->success);
116       }
117       else
118       {
119         $errors = array_merge($errors, $localerrors);
120       }
121
122     }
123  
124     if(count($users) > 0)
125     {
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)
131 //      {
132 //        $successes[] = $user[0].",".$user[1];
133 //      }
134     }
135   }
136
137
138 }
139
140 require_once("models/header.php");
141 startPage();
142
143 echo notificationBlock($errors,$successes);
144
145 echo '<div class="widget"><div class="title">Upload users</div>';
146
147 if ($show_form)
148 {
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\"/>
156   </form>";
157   */
158   echo "
159  
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>
168   <p>
169   <textarea name=\"userUpload\" rows=\"50\" cols=\"100\" style=\"width: 100%\">".$text_area."</textarea> </p>
170
171   </form>";
172 }
173
174 echo '</div>';
175 finishPage();
176   
177 ?> 
178

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