Nicen the rego form and add back the change password form
[matches/MCTX3420.git] / testing / MCTXWeb / public_html / users / original / 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 require_once("models/header.php");
11
12 $text_area="# Rows starting with '#' are ignored. Rows are of the form:\n# username, Full Name, email";
13
14
15 $show_form = true;
16 if (!empty($_POST))
17 {
18
19   $current_users = fetchAllUsers();
20
21   // Check form action
22   if ($_POST['action'] === "Download") // Download list of users and populate the text area
23   {
24   
25     foreach ($current_users as $u)
26     {
27       if ($u['user_name'] !== "admin")
28         $text_area=$text_area."\n".$u['user_name'].",".$u['display_name'].",".$u['email'];
29     }
30   }
31   else if ($_POST['action'] === "Upload") // Upload users in the text area
32   {
33
34     $text_area = $_POST['userUpload'];
35
36
37     // Iterate through each row
38     $all_rows=preg_split("/((\r?\n)|(\r\n?))/", $_POST['userUpload']);
39    
40     
41     
42     
43     foreach ($all_rows as $row)
44     {
45       if (empty($row) or $row[0] === '#')
46         continue;
47
48       $errors = array();
49
50       $fields = preg_split("/,/",  $row);
51      
52       $user_name = trim($fields[0]);
53       $display_name = trim($fields[1]);
54       $email = trim($fields[2]);
55       // generate the temporary password
56       $password = generatePassword();
57
58
59       if(count($errors) == 0)
60       { 
61         //Construct a user object
62                 $user = new User($username,$displayname,$password,$email);
63                 
64                 //Checking this flag tells us whether there were any errors such as possible data duplication occured
65                 if(!$user->status)
66                 {
67                         if($user->username_taken) $errors[] = lang("ACCOUNT_USERNAME_IN_USE",array($username));
68                         if($user->displayname_taken) $errors[] = lang("ACCOUNT_DISPLAYNAME_IN_USE",array($displayname));
69                         if($user->email_taken)    $errors[] = lang("ACCOUNT_EMAIL_IN_USE",array($email));               
70                 }
71                 else
72                 {
73                         //Attempt to add the user to the database, carry out finishing  tasks like emailing the user (if required)
74                         if(!$user->userCakeAddUser())
75                         {
76                                 if($user->mail_failure) $errors[] = lang("MAIL_ERROR");
77                                 if($user->sql_failure)  $errors[] = lang("SQL_ERROR");
78                         }
79                 }
80       }
81       if(count($errors) == 0)
82       {
83                 $successes[] = $user->success;
84       }
85
86       echo resultBlock($errors,$successes);
87     }
88
89     
90     
91     
92   
93   }
94
95
96 }
97
98 if ($show_form)
99 {
100   /* I can't get fucking file uploads to fucking work with fucking nginx
101   echo "<p> Please provide a CSV file of usernames and email addresses. </p>
102   <p> Click <a href=\"upload_users_example.csv\">here</a> for an example file. </p>
103   <div class=\"title\">Upload</div>
104   <form  action=\"".$_SERVER['PHP_SELF']."\" enctype=\"multipart/form-data\" method=\"post\">
105   <input type=\"file\" name=\"userUpload\"/>
106   <input type=\"submit\" value=\"Upload\"/>
107   </form>";
108   */
109   echo "
110  
111   <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
112   <p> Action to take on adding users: </p>
113   <p> <input type=\"radio\" name=\"upload_mode\" value=\"keep\" checked/>Keep existing users and add these users</p>
114   <p> <input type=\"radio\" name=\"upload_mode\" value=\"purge\"/>Purge existing users and add these users</p>
115   <input type=\"submit\" name=\"action\" value=\"Upload\"/> 
116   <input type=\"submit\" name=\"action\" value=\"Download\"/>
117   <input type=\"submit\" name=\"action\" value=\"Reset\"/>
118   <p> Enter or copy/paste user information below (resize the text area if necessary): </p>
119   <p>
120   <textarea name=\"userUpload\" rows=\"50\" cols=\"100\">".$text_area."</textarea> </p>
121
122   </form>";
123 }
124   
125 ?> 
126

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