source: extensions/user_mass_register/admin.php @ 19431

Last change on this file since 19431 was 19431, checked in by plg, 11 years ago

ability to force a username

File size: 5.9 KB
Line 
1<?php
2if (!defined("PHPWG_ROOT_PATH"))
3{
4  die ("Hacking attempt!");
5}
6
7// +-----------------------------------------------------------------------+
8// | Functions                                                             |
9// +-----------------------------------------------------------------------+
10
11function register_user_and_notify($login, $password, $mail_address)
12{
13  global $conf;
14 
15  $errors = register_user($login, $password, $mail_address, false);
16
17  if (empty($errors))
18  {
19    include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
20       
21    $keyargs_content = array(
22      get_l10n_args('Hello %s,', $login),
23      get_l10n_args('Thank you for registering at %s!', $conf['gallery_title']),
24      get_l10n_args('', ''),
25      get_l10n_args('Here are your connection settings', ''),
26      get_l10n_args('Username: %s', $login),
27      get_l10n_args('Password: %s', $password),
28      get_l10n_args('Email: %s', $mail_address),
29      get_l10n_args('', ''),
30      get_l10n_args('If you think you\'ve received this email in error, please contact us at %s', get_webmaster_mail_address()),
31      );
32   
33    pwg_mail(
34      $mail_address,
35      array(
36        'subject' => '['.$conf['gallery_title'].'] '.l10n('Registration'),
37        'content' => l10n_args($keyargs_content),
38        'content_format' => 'text/plain',
39        )
40      );
41  }
42
43  return $errors;
44}
45
46// +-----------------------------------------------------------------------+
47// | Check Access and exit when user status is not ok                      |
48// +-----------------------------------------------------------------------+
49
50check_status(ACCESS_ADMINISTRATOR);
51
52load_language('plugin.lang', UMR_PATH);
53
54// +-----------------------------------------------------------------------+
55// | Tabs                                                                  |
56// +-----------------------------------------------------------------------+
57
58$page['tab'] = 'home';
59
60// tabsheet
61include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
62$tabsheet = new tabsheet();
63$tabsheet->set_id('umr');
64
65$tabsheet->add('home', l10n('User Mass Register'), UMR_ADMIN . '-home');
66$tabsheet->select($page['tab']);
67$tabsheet->assign();
68
69// +-----------------------------------------------------------------------+
70// | Actions                                                               |
71// +-----------------------------------------------------------------------+
72
73if (isset($_POST['submit']))
74{
75  $lines = explode("\n", $_POST['users']);
76
77  $emails_to_create = array();
78  $emails_rejected = array();
79  $emails_already_exist = array();
80  $emails_created = array();
81  $emails_on_error = array();
82  $username_for = array();
83 
84  foreach ($lines as $line)
85  {
86    $fields = explode(';', $line);
87    $email = trim($fields[0]);
88
89    if (isset($fields[1]))
90    {
91      $username_for[$email] = trim($fields[1]);
92    }
93
94    $email = trim($email);
95
96    // this test requires PHP 5.2+
97    if (filter_var($email, FILTER_VALIDATE_EMAIL) !== false)
98    {
99      $emails_to_check[] = $email;
100     
101      if (!get_userid_by_email($email))
102      {
103        $emails_to_create[] = $email;
104      }
105      else
106      {
107        $emails_existing[] = $email;
108      }
109    }
110    elseif (!empty($email))
111    {
112      $emails_rejected[] = $email;
113    }
114  }
115
116  foreach ($emails_to_create as $email)
117  {   
118    // find a username
119    if (isset($username_for[$email]))
120    {
121      $username = $username_for[$email];
122    }
123    else
124    {
125      list($username,) = explode('@', $email);
126    }
127   
128    $base_username = $username;
129    $i = 2;
130    while (get_userid($username))
131    {
132      $username = $base_username.($i++);
133    }
134
135    // find a password
136    $password = generate_key(8);
137
138    $errors = register_user_and_notify($username, $password, $email);
139    if (!empty($errors))
140    {
141      $emails_on_error[] = $email;
142    }
143    else
144    {
145      $emails_created[] = $email;
146    }
147  }
148
149  $emails_for_form = array();
150 
151  if (!empty($emails_created))
152  {
153    array_push(
154      $page['infos'],
155      sprintf(
156        l10n('%d users registered'),
157        count($emails_created)
158        )
159      );
160  }
161
162  if (!empty($emails_on_error))
163  {
164    array_push(
165      $page['errors'],
166      sprintf(
167        l10n('%d registrations on error: %s'),
168        count($emails_on_error),
169        implode(', ', $emails_on_error)
170        )
171      );
172
173    $emails_for_form = array_merge($emails_for_form, $emails_on_error);
174  }
175 
176  if (!empty($emails_rejected))
177  {
178    array_push(
179      $page['errors'],
180      sprintf(
181        l10n('%d email addresses rejected: %s'),
182        count($emails_rejected),
183        implode(', ', $emails_rejected)
184        )
185      );
186
187    $emails_for_form = array_merge($emails_for_form, $emails_rejected);
188  }
189
190  if (!empty($emails_existing))
191  {
192    array_push(
193      $page['warnings'],
194      sprintf(
195        l10n('%d email addresses already exist: %s'),
196        count($emails_existing),
197        implode(', ', $emails_existing)
198        )
199      );
200  } 
201}
202
203// +-----------------------------------------------------------------------+
204// | form                                                                  |
205// +-----------------------------------------------------------------------+
206
207// define template file
208$template->set_filename('umr_content', realpath(UMR_PATH . 'admin.tpl'));
209
210// template vars
211$template->assign(array(
212  'UMR_PATH'=> get_root_url() . UMR_PATH, // used for images, scripts, ... access
213  'UMR_ABS_PATH'=> realpath(UMR_PATH),    // used for template inclusion (Smarty needs a real path)
214  'UMR_ADMIN' => UMR_ADMIN,
215  ));
216
217if (isset($emails_for_form) and !empty($emails_for_form))
218{
219  $template->assign('EMAILS', implode("\n", $emails_for_form));
220}
221
222// +-----------------------------------------------------------------------+
223// | sending html code                                                     |
224// +-----------------------------------------------------------------------+
225
226// send page content
227$template->assign_var_from_handle('ADMIN_CONTENT', 'umr_content');
228
229?>
Note: See TracBrowser for help on using the repository browser.