source: extension/user_mass_register/admin.php @ 18826

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

new plugin to register many users at once

File size: 5.6 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  $emails = explode("\n", $_POST['emails']);
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 
83  foreach ($emails as $email)
84  {
85    $email = trim($email);
86
87    // this test requires PHP 5.2+
88    if (filter_var($email, FILTER_VALIDATE_EMAIL) !== false)
89    {
90      $emails_to_check[] = $email;
91     
92      if (!get_userid_by_email($email))
93      {
94        $emails_to_create[] = $email;
95      }
96      else
97      {
98        $emails_existing[] = $email;
99      }
100    }
101    elseif (!empty($email))
102    {
103      $emails_rejected[] = $email;
104    }
105  }
106
107  foreach ($emails_to_create as $email)
108  {   
109    // find a username
110    list($base_username,) = explode('@', $email);
111   
112    $username = $base_username;
113    $i = 2;
114    while (get_userid($username))
115    {
116      $username = $base_username.($i++);
117    }
118
119    // find a password
120    $password = generate_key(8);
121
122    $errors = register_user_and_notify($username, $password, $email);
123    if (!empty($errors))
124    {
125      $emails_on_error[] = $email;
126    }
127    else
128    {
129      $emails_created[] = $email;
130    }
131  }
132
133  $emails_for_form = array();
134 
135  if (!empty($emails_created))
136  {
137    array_push(
138      $page['infos'],
139      sprintf(
140        l10n('%d users registered'),
141        count($emails_created)
142        )
143      );
144  }
145
146  if (!empty($emails_on_error))
147  {
148    array_push(
149      $page['errors'],
150      sprintf(
151        l10n('%d registrations on error: %s'),
152        count($emails_on_error),
153        implode(', ', $emails_on_error)
154        )
155      );
156
157    $emails_for_form = array_merge($emails_for_form, $emails_on_error);
158  }
159 
160  if (!empty($emails_rejected))
161  {
162    array_push(
163      $page['errors'],
164      sprintf(
165        l10n('%d email addresses rejected: %s'),
166        count($emails_rejected),
167        implode(', ', $emails_rejected)
168        )
169      );
170
171    $emails_for_form = array_merge($emails_for_form, $emails_rejected);
172  }
173
174  if (!empty($emails_existing))
175  {
176    array_push(
177      $page['warnings'],
178      sprintf(
179        l10n('%d email addresses already exist: %s'),
180        count($emails_existing),
181        implode(', ', $emails_existing)
182        )
183      );
184  } 
185}
186
187// +-----------------------------------------------------------------------+
188// | form                                                                  |
189// +-----------------------------------------------------------------------+
190
191// define template file
192$template->set_filename('umr_content', realpath(UMR_PATH . 'admin.tpl'));
193
194// template vars
195$template->assign(array(
196  'UMR_PATH'=> get_root_url() . UMR_PATH, // used for images, scripts, ... access
197  'UMR_ABS_PATH'=> realpath(UMR_PATH),    // used for template inclusion (Smarty needs a real path)
198  'UMR_ADMIN' => UMR_ADMIN,
199  ));
200
201if (isset($emails_for_form) and !empty($emails_for_form))
202{
203  $template->assign('EMAILS', implode("\n", $emails_for_form));
204}
205
206// +-----------------------------------------------------------------------+
207// | sending html code                                                     |
208// +-----------------------------------------------------------------------+
209
210// send page content
211$template->assign_var_from_handle('ADMIN_CONTENT', 'umr_content');
212
213?>
Note: See TracBrowser for help on using the repository browser.