source: extensions/UserAdvManager/tags/2.15.4/main.inc.php @ 14511

Last change on this file since 14511 was 6776, checked in by Eric, 14 years ago

Merge from Trunk to Branch 2.5

  • Property svn:eol-style set to LF
File size: 15.8 KB
Line 
1<?php
2/*
3Plugin Name: UserAdvManager
4Version: 2.15.4
5Description: Renforcer la gestion des utilisateurs - Enforce users management
6Plugin URI: http://fr.piwigo.org/ext/extension_view.php?eid=216
7Author: Nicco, Eric
8Author URI: http://gallery-nicco.no-ip.org, http://www.infernoweb.net
9*/
10
11/* History:  UAM_PATH.'Changelog.txt.php' */
12
13/*
14 ***** TODO List *****
15++ No validation needed for admins users comments (new trigger needed in comments.php ?)
16
17++ No single email check for admins (new trigger needed in functions_user.inc.php ?)
18
19++ Password control and enforcement
20  ?? Can not be the same as username -> Could password score control be sufficient ?
21 
22++ Security : Blocking brut-force attacks !
23              -> Way to do that : Count the number of failed attempts to connect and lock the targetted account after x attempts. Where x will be settable by admin.
24              To unlock the locked account :
25               -> A new table in admin's plugin panel which would display the locked accounts.
26               -> Sending an email to account owner to inform him his account is blocked due to multiple failed connexions attempts. This email could have a link with a security key to unlock the account.
27               -> Both of above solutions ?
28
29++ Opportunity to copy a registered user for new user creation
30  ++ new copied user will (or not) belong to the same groups
31  ++ new copied user will (or not) get the same status (visitor, admin, webmaster, guest (??))
32  ++ new copied user will (or not) get the same properties
33  ++ new copied user will (or not) get the same language
34  ... and so on
35*/
36
37if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
38if (!defined('UAM_DIR')) define('UAM_DIR' , basename(dirname(__FILE__)));
39if (!defined('UAM_PATH')) define('UAM_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
40
41include_once (UAM_PATH.'include/constants.php');
42include_once (UAM_PATH.'include/functions.inc.php');
43
44load_language('plugin.lang', UAM_PATH);
45
46
47/* Plugin admin */
48add_event_handler('get_admin_plugin_menu_links', 'UAM_admin_menu');
49
50function UAM_admin_menu($menu)
51{
52// +-----------------------------------------------------------------------+
53// |                      Getting plugin name                              |
54// +-----------------------------------------------------------------------+
55  $plugin =  PluginInfos(UAM_PATH);
56  $name = $plugin['name'];
57 
58  array_push($menu,
59    array(
60      'NAME' => $name,
61      'URL'  => get_admin_plugin_menu_link(UAM_PATH.'/admin/UAM_admin.php')
62    )
63  );
64
65  return $menu;
66}
67
68/* Lastvisit table feed for Ghost Tracker */
69add_event_handler('loc_begin_index', 'UAM_GhostTracker');
70
71function UAM_GhostTracker()
72{
73  global $conf, $user;
74
75  $conf_UAM = unserialize($conf['UserAdvManager']);
76
77  /* Admins and Guests are not tracked for Ghost Tracker or Users Tracker */
78  if (!is_admin() and !is_a_guest())
79  {
80    if ((isset($conf_UAM[16]) and $conf_UAM[16] == 'true') or (isset($conf_UAM[19]) and $conf_UAM[19] == 'true'))
81    {
82
83      $userid = get_userid($user['username']);
84         
85      /* Looking for existing entry in last visit table */
86      $query = '
87SELECT *
88  FROM '.USER_LASTVISIT_TABLE.'
89WHERE user_id = '.$userid.'
90;';
91       
92      $count = pwg_db_num_rows(pwg_query($query));
93         
94      if ($count == 0)
95      {
96        /* If not, data are inserted in table */
97        $query = '
98INSERT INTO '.USER_LASTVISIT_TABLE.' (user_id, lastvisit, reminder)
99VALUES ('.$userid.', now(), "false")
100;';
101        pwg_query($query);
102      }
103      else if ($count > 0)
104      {
105        /* If yes, data are updated in table */
106        $query = '
107UPDATE '.USER_LASTVISIT_TABLE.'
108SET lastvisit = now(), reminder = "false"
109WHERE user_id = '.$userid.'
110LIMIT 1
111;';
112        pwg_query($query);
113      }
114    }
115  }
116}
117
118
119/* User creation */
120add_event_handler('register_user', 'UAM_Adduser');
121
122function UAM_Adduser($register_user)
123{
124  global $conf;
125
126  $conf_UAM = unserialize($conf['UserAdvManager']);
127 
128  if ((isset($conf_UAM[0]) and $conf_UAM[0] == 'true') and (isset($conf_UAM[1]) and $conf_UAM[1] == 'local'))
129  {
130    /* This is to send an information email and set user to "waiting" group or status until admin validation */
131    $passwd = (isset($_POST['password'])) ? $_POST['password'] : '';
132    SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], false);
133    setgroup($register_user['id']);// Set to "waiting" group or status until admin validation
134  }
135  elseif ((isset($conf_UAM[0]) and $conf_UAM[0] == 'false') and (isset($conf_UAM[1]) and $conf_UAM[1] == 'local'))
136  {
137    /* This is to set user to "wainting" group or status until admin validation */
138    setgroup($register_user['id']);// Set to "waiting" group or status until admin validation
139  }
140  elseif ((isset($conf_UAM[0]) and $conf_UAM[0] == 'true') and (isset($conf_UAM[1]) and $conf_UAM[1] == 'false'))
141  {
142    /* This is to send an information email without validation key */
143    $passwd = (isset($_POST['password'])) ? $_POST['password'] : '';
144    SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], false);
145  }
146  /* Sending registration confirmation by email */
147  elseif ((isset($conf_UAM[0]) and $conf_UAM[0] == 'true') and (isset($conf_UAM[1]) and $conf_UAM[1] == 'true'))
148  {
149    if (is_admin() and isset($conf_UAM[20]) and $conf_UAM[20] == 'true')
150    {
151      $passwd = (isset($_POST['password'])) ? $_POST['password'] : '';
152      SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], true); 
153    }
154    elseif (is_admin() and isset($conf_UAM[20]) and $conf_UAM[20] == 'false')
155    {
156      $passwd = (isset($_POST['password'])) ? $_POST['password'] : '';
157      SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], false);
158    }
159    elseif (!is_admin())
160    {
161      $passwd = (isset($_POST['password'])) ? $_POST['password'] : '';
162      SendMail2User(1, $register_user['id'], $register_user['username'], $passwd, $register_user['email'], true);
163    }
164  }
165}
166
167
168/* User deletion */
169add_event_handler('delete_user', 'UAM_Deluser');
170
171function UAM_Deluser($user_id)
172{
173  /* Cleanup for ConfirmMail table */
174  DeleteConfirmMail($user_id);
175  /* Cleanup for LastVisit table */
176  DeleteLastVisit($user_id);
177  /* Cleanup Redirection settings */
178  DeleteRedir($user_id);
179}
180
181
182/* Check users registration */
183add_event_handler('register_user_check', 'UAM_RegistrationCheck', EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
184
185function UAM_RegistrationCheck($err, $user)
186{
187  global $errors, $conf;
188
189/* *********************************************************** */
190/* We need to reset the standard Piwigo's register controls    */
191/* because the call of register_user_check trigger resets them */
192/* *********************************************************** */
193  /* ********************************** */
194  /* Standard Piwigo's username control */
195  /* ********************************** */
196  if ($_POST['login'] == '')
197  {
198    return l10n('reg_err_login1');
199  }
200  if (preg_match('/^.* $/', $_POST['login']))
201  {
202    return l10n('reg_err_login2');
203  }
204  if (preg_match('/^ .*$/', $_POST['login']))
205  {
206    return l10n('reg_err_login3');
207  }
208  if (get_userid($_POST['login']))
209  {
210    return l10n('reg_err_login5');
211  }
212 
213  if (script_basename() == 'admin' and isset($_GET['page']) and $_GET['page'] == 'user_list') /* not the same email variable if we are on users registration page or on admin's user registration page*/
214  {
215  /* Email doblons check */
216    $atom   = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]';   // before  arobase
217    $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // domain name
218    $regex = '/^' . $atom . '+' . '(\.' . $atom . '+)*' . '@' . '(' . $domain . '{1,63}\.)+' . $domain . '{2,63}$/i';
219 
220    if (!preg_match($regex, $_POST['email']))
221    {
222      return l10n('reg_err_mail_address');
223    }
224   
225    $query = '
226SELECT count(*)
227FROM '.USERS_TABLE.'
228WHERE upper('.$conf['user_fields']['email'].') = upper(\''.$_POST['email'].'\')
229;';
230    list($count) = pwg_db_fetch_row(pwg_query($query));
231    if ($count != 0)
232    {
233      return l10n('reg_err_mail_address_dbl');
234    }
235  }
236
237  if (script_basename() == 'register') /* not the same email variable if we are on users registration page or on admin's user registration page*/
238  {
239  /* Email doblons check */
240    $atom   = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]';   // before  arobase
241    $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // domain name
242    $regex = '/^' . $atom . '+' . '(\.' . $atom . '+)*' . '@' . '(' . $domain . '{1,63}\.)+' . $domain . '{2,63}$/i';
243 
244    if (!preg_match($regex, $_POST['mail_address']))
245    {
246      return l10n('reg_err_mail_address');
247    }
248   
249    $query = '
250SELECT count(*)
251FROM '.USERS_TABLE.'
252WHERE upper('.$conf['user_fields']['email'].') = upper(\''.$_POST['mail_address'].'\')
253;';
254    list($count) = pwg_db_fetch_row(pwg_query($query));
255    if ($count != 0)
256    {
257      return l10n('reg_err_mail_address_dbl');
258    }
259  }
260/* ****************************************** */
261/* End of Piwigo's standard register controls */
262/* ****************************************** */
263
264
265/* ****************************************** */
266/* Here begins the advanced register controls */
267/* ****************************************** */
268  $PasswordCheck = 0;
269
270  $conf_UAM = unserialize($conf['UserAdvManager']);
271
272  /* Password enforcement control */
273  if (isset($conf_UAM[13]) and $conf_UAM[13] == 'true' and !empty($conf_UAM[14]))
274  {
275    if (!empty($user['password']) and !is_admin())
276    {
277      $PasswordCheck = testpassword($user['password']);
278 
279      if ($PasswordCheck < $conf_UAM[14])
280      {
281        $message = get_l10n_args('reg_err_login4_%s', $PasswordCheck);
282        return($lang['reg_err_pass'] = l10n_args($message).$conf_UAM[14]);
283      }
284    }
285    else if (!empty($user['password']) and is_admin() and isset($conf_UAM[15]) and $conf_UAM[15] == 'true')
286    { 
287      $PasswordCheck = testpassword($user['password']);
288 
289      if ($PasswordCheck < $conf_UAM[14])
290      {
291        $message = get_l10n_args('reg_err_login4_%s', $PasswordCheck);
292        return($lang['reg_err_pass'] = l10n_args($message).$conf_UAM[14]);
293      }
294    }
295  }
296
297  /* Username without forbidden keys */
298  if (isset($conf_UAM[6]) and $conf_UAM[6] == 'true' and !empty($_POST['login']) and ValidateUsername($_POST['login']) and !is_admin())
299  {
300    $_POST['login'] = '';
301    return($lang['reg_err_login1'] = l10n('reg_err_login6')."'".$conf_UAM[7]."'");
302  }
303
304  /* Email without forbidden domains */
305  if (isset($conf_UAM[11]) and $conf_UAM[11] == 'true' and !empty($_POST['mail_address']) and ValidateEmailProvider($_POST['mail_address']) and !is_admin())
306  {
307    $_POST['mail_address'] = '';
308    return($lang['reg_err_login1'] = l10n('reg_err_login7')."'".$conf_UAM[12]."'");
309  }
310}
311
312
313if (script_basename() == 'profile')
314{
315  add_event_handler('loc_begin_profile', 'UAM_Profile_Init');
316
317  function UAM_Profile_Init()
318  {
319    global $conf, $user, $template;
320
321    $conf_UAM = unserialize($conf['UserAdvManager']);
322
323    if ((isset($conf_UAM[21]) and $conf_UAM[21] == 'true'))
324    {
325      $user_idsOK = array();
326      if (!check_consult($user['id'], $user_idsOK))
327      {
328        $user_idsOK[] = $user['id'];
329       
330        $query = "
331          UPDATE ".CONFIG_TABLE."
332          SET value = \"".implode(',', $user_idsOK)."\"
333          WHERE param = 'UserAdvManager_Redir';";
334         
335        pwg_query($query);
336      }
337    }
338
339    if (isset($_POST['validate']) and !is_admin())
340    {
341      /* Email without forbidden domains */
342      if (isset($conf_UAM[11]) and $conf_UAM[11] == 'true' and !empty($_POST['mail_address']))
343      {
344        if (ValidateEmailProvider($_POST['mail_address']))
345        {
346          $template->append('errors', l10n('reg_err_login7')."'".$conf_UAM[12]."'");
347          unset($_POST['validate']);
348        }
349      }
350
351      $typemail = 3;
352     
353      if (!empty($_POST['use_new_pwd']))
354      {
355        $typemail = 2;
356       
357        /* Password enforcement control */
358        if (isset($conf_UAM[13]) and $conf_UAM[13] == 'true' and !empty($conf_UAM[14]))
359        {
360          $PasswordCheck = testpassword($_POST['use_new_pwd']);
361         
362          if ($PasswordCheck < $conf_UAM[14])
363          {
364            $message = get_l10n_args('reg_err_login4_%s', $PasswordCheck);
365            $template->append('errors', l10n_args($message).$conf_UAM[14]);
366            unset($_POST['use_new_pwd']);
367            unset($_POST['validate']);
368          }
369        }
370      }
371     
372      /* Sending registration confirmation by email */
373      if ((isset($conf_UAM[0]) and $conf_UAM[0] == 'true') or (isset($conf_UAM[1]) and $conf_UAM[1] == 'true') or (isset($conf_UAM[1]) and $conf_UAM[1] == 'local'))
374      {
375        $confirm_mail_need = false;
376             
377        if (!empty($_POST['mail_address']))
378        {
379          $query = '
380SELECT '.$conf['user_fields']['email'].' AS email
381FROM '.USERS_TABLE.'
382WHERE '.$conf['user_fields']['id'].' = \''.$user['id'].'\'
383;';
384         
385          list($current_email) = pwg_db_fetch_row(pwg_query($query));
386
387          /* This is to send a new validation key */
388          if ($_POST['mail_address'] != $current_email and (isset($conf_UAM[1]) and $conf_UAM[1] == 'true'))
389       
390            $confirm_mail_need = true;
391
392          /* This is to set the user to "waiting" group or status until admin validation */
393          if ($_POST['mail_address'] != $current_email and (isset($conf_UAM[1]) and $conf_UAM[1] == 'local'))
394       
395            setgroup($register_user['id']);// Set to "waiting" group or status until admin validation
396            $confirm_mail_need = false;
397        }
398       
399        if ((!empty($_POST['use_new_pwd']) and (isset($conf_UAM[0]) and $conf_UAM[0] == 'true') or $confirm_mail_need))
400        {
401          $query = '
402SELECT '.$conf['user_fields']['username'].'
403FROM '.USERS_TABLE.'
404WHERE '.$conf['user_fields']['id'].' = \''.$user['id'].'\'
405;';
406       
407          list($username) = pwg_db_fetch_row(pwg_query($query));
408
409          SendMail2User($typemail, $user['id'], $username, $_POST['use_new_pwd'], $_POST['mail_address'], $confirm_mail_need);
410        }
411      }
412    }
413  }
414}
415
416
417// RedirectToProfile - Thx to LucMorizur
418// redirects a visitor (not generic (forbidden) neither admin) to his
419// profile.php page
420//
421// no variable, no return
422add_event_handler('login_success', 'RedirectToProfile');
423
424function RedirectToProfile()
425{
426  global $conf, $user;
427 
428  $conf_UAM = unserialize($conf['UserAdvManager']);
429
430  if ((isset($conf_UAM[21]) and $conf_UAM[21] == 'true'))
431  {
432    $user_idsOK = array();
433    if (!check_consult($user['id'], $user_idsOK))
434      redirect(PHPWG_ROOT_PATH.'profile.php');
435  }
436}
437
438
439add_event_handler('init', 'UAM_InitPage');
440/* *** Important ! This is necessary to make email exclusion work in admin's users management panel *** */
441function UAM_InitPage()
442{
443  load_language('plugin.lang', UAM_PATH);
444  global $conf, $template, $page, $lang, $errors;
445
446  $conf_UAM = unserialize($conf['UserAdvManager']);
447
448/* Admin user management */
449  if (script_basename() == 'admin' and isset($_GET['page']) and $_GET['page'] == 'user_list')
450  {
451    if (isset($_POST['submit_add']))
452    {
453      /* Email without forbidden domains */
454      if (isset($conf_UAM[11]) and $conf_UAM[11] == 'true' and !empty($_POST['email']) and ValidateEmailProvider($_POST['email']))
455      {
456        $template->append('errors', l10n('reg_err_login7')."'".$conf_UAM[12]."'");
457        unset($_POST['submit_add']);
458      }
459    }
460  }
461}
462
463
464add_event_handler('user_comment_check', 'UAM_CheckEmptyCommentAuthor', 50, 2);
465
466function UAM_CheckEmptyCommentAuthor($comment_action, $comm)
467{
468  load_language('plugin.lang', UAM_PATH);
469  global $infos, $conf, $template;
470
471  $conf_UAM = unserialize($conf['UserAdvManager']);
472
473/* User creation OR update */
474  if (isset($conf_UAM[5]) and $conf_UAM[5] == 'true' and $conf['comments_forall'] == 'true' and $comm['author'] == 'guest')
475  {
476    $comment_action = 'reject';
477
478    array_push($infos, l10n('UAM_Empty Author'));
479  }
480
481  return $comment_action;
482}
483?>
Note: See TracBrowser for help on using the repository browser.