source: extensions/NBC_UserAdvManager/trunk/main.inc.php @ 6354

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

[NBC_UserAdvManager]

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