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

Last change on this file since 3881 was 3881, checked in by Eric, 15 years ago

Bug fixed : Bad query on unvalidated users display
Bug fixed : Sql syntax error on plugin install / update
Language files corrections

  • Property svn:eol-style set to LF
File size: 14.3 KB
Line 
1<?php
2/*
3Plugin Name: NBC UserAdvManager
4Version: 2.11.2
5Description: Permet de renforcer les possibilités de 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/*
12 ***** Plugin history (branch 2.10)*****
13
14-- 2.10.0-beta : Initial beta release for Piwigo compatibility
15-- 2.10.1-beta : Small correction on generated path
16-- 2.10.2-beta : Bug resolved on register validation page
17
18-- 2.10.3 : Final and fully functional release
19                        Bug resolved on plugin activation
20
21-- 2.10.4 : Bug fixed on profiles update
22
23-- 2.10.5 : Improved code on profiles update
24
25-- 2.10.6 : Old language packs (iso) deleted (forget from PWG 1.7.x version)
26
27-- 2.10.7 : Bug fixed on user's validation email sending
28
29-- 2.10.8 : ConfirmMail page looks better (Sylvia theme only)
30                        Improved code for checking author on guest comments
31
32-- 2.10.9 : Bug fixed - Missing english translation
33                        Bug fixed - Notice on forbidden characters function use
34                        Bug fixed - Audit on forbidden characters in username didn't work
35                        Adding of email provider exclusion (like *@hotmail.com) - Warning ! -> Known bug : This feature doesn't work on user profile page. So, already registered users can change their email address to a forbiden one.
36
37-- 2.10.9a : Email provider exclusion is no longer case sensitive
38
39-- 2.10.9b : Bug fixed - Home icon wasn't linked to gallery url in ConfirmMail page. If GALLERY_URL is not set, Home icon gets the pwg root path.
40
41-- 2.10.9c : Bug fixed - If Email provider exclusion is set off, new registered user will have a PHP notice on "Undefined variable: ncsemail"
42
43-- 2.10.9d : Code simplification - need no more ""template"" sub-directory in plugin directory for enhance "back link" icon in ConfirMail.tpl
44
45-- 2.10.9e : Compatibility improvement with PHP 5.3 - Some old functions will be deprecated like :
46                                ereg replaced by preg_match
47                                eregi replace by preg_match with "i" moderator
48                                split replace by preg_split
49                               
50-- 2.10.9f : Compatibility bug fixed when used with DynamicRecentPeriod plugin
51
52-- 2.11.0 : New tabsheet menu to manage ConfirMail functions (setting a timeout without validation, Cleanup expired user's accounts, Force confirmation, Renew validation key, list unvalidated users,...)
53                                                Beautify plugin's main admin panel
54                                               
55-- 2.11.1 : Bug fixed with install and upgrade functions
56                                                Language files correction
57
58-- 2.11.2 : Bug fixed on bad query for unvalidated users display in unvalidated users list
59                                                Bug fixed : Sql syntax error on plugin activation
60
61*/
62
63/*
64
65 ***** TODO List *****
66
67++ No validation needed for admins users comments (new trigger needed in comments.php)
68
69++ No single email check for admins (new trigger needed in (functions_user.inc.php ?))
70
71++ Password control and enforcement
72  -- Empty password (done in Piwigo 2.x)
73  ++ Can not be the same as username
74  ++ complexity of the password (Numbers+Lettrers+Low and high case+Special+minimal length)
75 
76++ Security : Blocking brut-force attacks !
77
78++ Opportunity to copy a registered user for new user creation
79  ++ new copied user will (or not) belong to the same groups
80  ++ new copied user will (or not) get the same status (visitor, admin, webmaster, guest (??))
81  ++ new copied user will (or not) get the same properties
82  ++ new copied user will (or not) get the same language
83  ... and so on
84 
85*/
86
87
88if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
89
90define('NBC_UserAdvManager_DIR' , basename(dirname(__FILE__)));
91define('NBC_UserAdvManager_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
92
93include_once (NBC_UserAdvManager_PATH.'include/constants.php');
94include_once (NBC_UserAdvManager_PATH.'include/functions_UserAdvManager.inc.php');
95
96load_language('plugin.lang', NBC_UserAdvManager_PATH);
97
98
99/* Plugin admin */
100add_event_handler('get_admin_plugin_menu_links', 'nbc_UserAdvManager_admin_menu');
101
102function nbc_UserAdvManager_admin_menu($menu)
103{
104  array_push($menu,
105    array(
106      'NAME' => 'UserAdvManager',
107      'URL'  => get_admin_plugin_menu_link(NBC_UserAdvManager_PATH.'/admin/UserAdvManager_admin.php')
108    )
109  );
110
111  return $menu;
112}
113
114
115
116/* User creation */
117add_event_handler('register_user', 'UserAdvManager_Adduser');
118
119function UserAdvManager_Adduser($register_user)
120{
121  global $conf;
122 
123  $conf_nbc_UserAdvManager = isset($conf['nbc_UserAdvManager']) ? explode(";" , $conf['nbc_UserAdvManager']) : array();
124
125  if (( isset($conf_nbc_UserAdvManager[0]) and $conf_nbc_UserAdvManager[0] == 'true') or ( isset($conf_nbc_UserAdvManager[2]) and $conf_nbc_UserAdvManager[2] == 'true'))
126       
127        SendMail2User(1, $register_user['id'], $register_user['username'], $_POST['password'], $register_user['email'], true);
128}
129
130
131
132/* User deletion */
133add_event_handler('delete_user', 'UserAdvManager_Deluser');
134
135function UserAdvManager_Deluser($user_id)
136{
137
138  DeleteConfirmMail($user_id);
139
140}
141
142
143
144add_event_handler('init', 'UserAdvManager_InitPage');
145 
146function UserAdvManager_InitPage()
147{
148  load_language('plugin.lang', NBC_UserAdvManager_PATH);
149  global $conf, $template, $page, $lang;
150
151  $conf_nbc_UserAdvManager = isset($conf['nbc_UserAdvManager']) ? explode(";" , $conf['nbc_UserAdvManager']) : array();
152 
153
154  if ( isset($conf_nbc_UserAdvManager[1]) and $conf_nbc_UserAdvManager[1] == 'true' )
155    $lang['reg_err_login5'] = l10n('new_reg_err_login5');
156 
157
158 
159/* User identification */
160  if (script_basename() == 'identification')
161  {
162    if (isset($_POST['login']))
163    {
164      /* User non case sensitive */
165      if (isset($conf_nbc_UserAdvManager[1]) and $conf_nbc_UserAdvManager[1] == 'true' )
166      {
167        $new_username =  NotSensibleSearchUsername($_POST['username']);
168        $_POST['username'] = $new_username == '' ? $_POST['username'] : $new_username;
169      }
170    }
171  }
172
173
174
175/* Admin user management */
176  if (script_basename() == 'admin' and isset($_GET['page']) and $_GET['page'] == 'user_list')
177  {
178    if (isset($_POST['submit_add']))
179    {
180      /* User non case sensitive */
181      if (isset($conf_nbc_UserAdvManager[1]) and $conf_nbc_UserAdvManager[1] == 'true' )
182      {
183        $new_username =  NotSensibleSearchUsername($_POST['login']);
184        $_POST['login'] = $new_username == '' ? $_POST['login'] : $new_username;
185      }
186
187
188      /* Username without forbidden keys */
189      if (isset($conf_nbc_UserAdvManager[7]) and $conf_nbc_UserAdvManager[7] == 'true' and !empty($_POST['login']) and !ValidateUsername($_POST['login']))
190      {
191        $lang['reg_err_login1'] = l10n('reg_err_login6')."'".$conf_nbc_UserAdvManager[8]."'";
192        $_POST['login'] = '';
193      }
194
195      /* Email without forbidden domains */
196      /* This don't work on call of ValidateEmailProvider() function - Why ?? -> Due to the "return = false|true" in function ?*/
197      //if (isset($conf_nbc_UserAdvManager[12]) and $conf_nbc_UserAdvManager[12] == 'true' and !empty($_POST['email']) and !ValidateEmailProvider($_POST['email']))
198      //{
199      //  $lang['reg_err_login1'] = l10n('reg_err_login7')."'".$conf_nbc_UserAdvManager[13]."'";
200          //  $_POST['login'] = '';
201          //}
202      /* This work with a code copy of ValidateEmailProvider() function */
203                        if (isset($conf_nbc_UserAdvManager[12]) and $conf_nbc_UserAdvManager[12] == 'true' and !empty($_POST['email']))
204                        {
205                        $ncsemail = strtolower($_POST['email']);
206                        $conf_nbc_MailExclusion = preg_split('/,/',$conf_nbc_UserAdvManager[13]);
207                        for ($i = 0 ; $i < count($conf_nbc_MailExclusion) ; $i++)
208                    {
209                  $pattern = '/'.$conf_nbc_MailExclusion[$i].'/';
210                                if (preg_match($pattern, $ncsemail))
211                        {
212                        $lang['reg_err_login1'] = l10n('reg_err_login7')."'".$conf_nbc_UserAdvManager[13]."'";
213                        $_POST['login'] = '';
214                                        }
215                                }
216                        }
217    }
218  }
219
220/* User creation */
221  if (script_basename() == 'register')
222  {
223    if (isset($_POST['submit']))
224    {
225      /* Username non case sensitive */
226      if (isset($conf_nbc_UserAdvManager[1]) and $conf_nbc_UserAdvManager[1] == 'true')
227      {
228        $new_username =  NotSensibleSearchUsername($_POST['login']);
229        $_POST['login'] = $new_username == '' ? $_POST['login'] : $new_username;
230      }
231
232
233      /* Username without forbidden keys */
234      if (isset($conf_nbc_UserAdvManager[7]) and $conf_nbc_UserAdvManager[7] == 'true' and !empty($_POST['login']) and !ValidateUsername($_POST['login']))
235      {
236        $lang['reg_err_login1'] = l10n('reg_err_login6')."'".$conf_nbc_UserAdvManager[8]."'";
237        $_POST['login'] = '';
238      }
239
240
241      /* Email without forbidden domains */
242      /* This don't work on call of ValidateEmailProvider() function - Why ?? -> Due to the "return = false|true" in function ?*/
243      //if (isset($conf_nbc_UserAdvManager[12]) and $conf_nbc_UserAdvManager[12] == 'true' and !empty($_POST['mail_address']) and !ValidateEmailProvider($_POST['mail_address']))
244      //{
245      //  $lang['reg_err_mail_address'] = l10n('reg_err_login7')."'".$conf_nbc_UserAdvManager[13]."'";
246      //  $_POST['mail_address'] = '';
247      //}
248      /* This work with a code copy of ValidateEmailProvider() function */
249                        if (isset($conf_nbc_UserAdvManager[12]) and $conf_nbc_UserAdvManager[12] == 'true' and !empty($_POST['mail_address']))
250                  {
251                        $ncsemail = strtolower($_POST['mail_address']);
252                    $conf_nbc_MailExclusion = preg_split('/,/',$conf_nbc_UserAdvManager[13]);
253                                for ($i = 0 ; $i < count($conf_nbc_MailExclusion) ; $i++)
254                          {
255                                $pattern = '/'.$conf_nbc_MailExclusion[$i].'/';
256                                if (preg_match($pattern, $ncsemail))
257                                  {
258                                        $lang['reg_err_login1'] = l10n('reg_err_login7')."'".$conf_nbc_UserAdvManager[13]."'";
259                                                $_POST['login'] = '';
260                                  }
261                          }
262                  }
263    }
264  }
265
266/* User profile update */
267  if (script_basename() == 'profile')
268  {
269    if (isset($_POST['validate']))
270    {
271      /* Sending email to user */
272      if (( isset($conf_nbc_UserAdvManager[0]) and $conf_nbc_UserAdvManager[0] == 'true') or ( isset($conf_nbc_UserAdvManager[2]) and $conf_nbc_UserAdvManager[2] == 'true'))
273      {
274        global $conf, $user ;
275        $errors = array();
276 
277        $int_pattern = '/^\d+$/';
278        if (empty($_POST['nb_image_line'])
279            or (!preg_match($int_pattern, $_POST['nb_image_line'])))
280        {
281          $errors[] = l10n('nb_image_line_error');
282        }
283     
284        if (empty($_POST['nb_line_page'])
285            or (!preg_match($int_pattern, $_POST['nb_line_page'])))
286        {
287          $errors[] = l10n('nb_line_page_error');
288        }
289     
290        if ($_POST['maxwidth'] != ''
291            and (!preg_match($int_pattern, $_POST['maxwidth'])
292                 or $_POST['maxwidth'] < 50))
293        {
294          $errors[] = l10n('maxwidth_error');
295        }
296        if ($_POST['maxheight']
297             and (!preg_match($int_pattern, $_POST['maxheight'])
298                   or $_POST['maxheight'] < 50))
299        {
300          $errors[] = l10n('maxheight_error');
301        }
302
303        if (isset($_POST['mail_address']))
304        {
305          $mail_error = validate_mail_address($user['id'], $_POST['mail_address']);
306          if (!empty($mail_error))
307          {
308            $errors[] = $mail_error;
309          }
310         
311                                        if (isset($conf_nbc_UserAdvManager[12]) and $conf_nbc_UserAdvManager[12] == 'true' and !empty($_POST['mail_address']))
312                                {
313                                        $ncsemail = strtolower($_POST['mail_address']);
314                                $conf_nbc_MailExclusion = preg_split('/,/',$conf_nbc_UserAdvManager[13]);
315                                                for ($i = 0 ; $i < count($conf_nbc_MailExclusion) ; $i++)
316                                        {
317                                                $pattern = '/'.$conf_nbc_MailExclusion[$i].'/';
318                                                if (preg_match($pattern, $ncsemail))
319                                                {
320                                                $mail_error = l10n('reg_err_login7')."'".$conf_nbc_UserAdvManager[13]."'";
321                                                }
322                                        }
323                                }
324                if (!empty($mail_error))
325          {
326            $errors[] = $mail_error;
327          }
328        }
329
330        $typemail = 3;
331       
332        if (!empty($_POST['use_new_pwd']))
333        {
334          $typemail = 2;
335
336          // password must be the same as its confirmation
337          if ($_POST['use_new_pwd'] != $_POST['passwordConf'])
338          {
339            $errors[] = l10n('New password confirmation does not correspond');
340          }
341     
342          if ( !defined('IN_ADMIN') )
343          {// changing password requires old password
344            $query = '
345              SELECT '.$conf['user_fields']['password'].' AS password
346              FROM '.USERS_TABLE.'
347              WHERE '.$conf['user_fields']['id'].' = \''.$user['id'].'\'
348            ;';
349            list($current_password) = mysql_fetch_row(pwg_query($query));
350       
351            if ($conf['pass_convert']($_POST['password']) != $current_password)
352            {
353              $errors[] = l10n('Current password is wrong');
354            }
355          }
356        }
357       
358        $confirm_mail_need = false;
359             
360        if (!empty($_POST['mail_address']))
361        {
362          $query = '
363            SELECT '.$conf['user_fields']['email'].' AS email
364            FROM '.USERS_TABLE.'
365            WHERE '.$conf['user_fields']['id'].' = \''.$user['id'].'\'
366          ;';
367          list($current_email) = mysql_fetch_row(pwg_query($query));
368     
369          if ( $_POST['mail_address'] != $current_email and ( isset($conf_nbc_UserAdvManager[2]) and $conf_nbc_UserAdvManager[2] == 'true') )
370            $confirm_mail_need = true;
371        }
372
373        if (count($errors) == 0 and (!empty($_POST['use_new_pwd']) and ( isset($conf_nbc_UserAdvManager[0]) and $conf_nbc_UserAdvManager[0] == 'true') or $confirm_mail_need) )
374        {
375          $query = '
376            SELECT '.$conf['user_fields']['username'].'
377            FROM '.USERS_TABLE.'
378            WHERE '.$conf['user_fields']['id'].' = \''.$user['id'].'\'
379          ;';
380          list($username) = mysql_fetch_row(pwg_query($query));
381
382
383          SendMail2User($typemail, $user['id'], $username, $_POST['use_new_pwd'], $_POST['mail_address'], $confirm_mail_need);
384        }
385      }
386    }
387  }
388}
389
390add_event_handler('user_comment_check', 'UserAdvManager_CheckEmptyCommentAuthor', 50, 2);
391
392function UserAdvManager_CheckEmptyCommentAuthor($comment_action, $comm)
393{
394  load_language('plugin.lang', NBC_UserAdvManager_PATH);
395  global $infos, $conf, $template;
396
397  $conf_nbc_UserAdvManager = isset($conf['nbc_UserAdvManager']) ? explode(";" , $conf['nbc_UserAdvManager']) : array();
398
399/* User creation OR update */
400  if (isset($conf_nbc_UserAdvManager[6]) and $conf_nbc_UserAdvManager[6] == 'true' and $conf['comments_forall'] == 'true' and $comm['author'] == 'guest')
401  {
402    $comment_action = 'reject';
403
404    array_push($infos, l10n('UserAdvManager_Empty Author'));
405  }
406
407  return $comment_action;
408}
409
410?>
Note: See TracBrowser for help on using the repository browser.