source: extensions/Password_Policy/main.inc.php @ 25090

Last change on this file since 25090 was 25090, checked in by Eric, 10 years ago

Missing plugin URI

File size: 12.5 KB
Line 
1<?php
2/*
3Plugin Name: Password Policy
4Version: auto
5Description: Renforcer la sécurité des mots de passe - Enforce password security
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=718
7Author: Eric
8Author URI: http://www.infernoweb.net
9*/
10
11/* History:  PP_PATH.'Changelog.txt.php' */
12
13if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
14if (!defined('PP_PATH')) define('PP_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
15
16global $conf;
17
18include_once (PP_PATH.'include/functions.inc.php');
19
20load_language('plugin.lang', PP_PATH);
21
22$conf_PP = unserialize($conf['PasswordPolicy']);
23
24
25// Plugin administration panel
26// ---------------------------
27add_event_handler('get_admin_plugin_menu_links', 'PP_admin_menu');
28
29// Features and controls on user connexion
30// ---------------------------------------
31add_event_handler('loc_begin_index', 'PP_Init');
32
33// Display messages on index page
34// ------------------------------
35add_event_handler('init', 'PP_InitPage');
36
37// Check users registration
38// ------------------------
39add_event_handler('register_user_check', 'PP_RegistrationCheck', EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
40
41if (script_basename() == 'profile')
42{
43  add_event_handler('loc_begin_profile', 'PP_Profile_Init');
44}
45
46// Redirection to profile page
47// ---------------------------
48add_event_handler('login_success', 'PP_LoginTasks',EVENT_HANDLER_PRIORITY_NEUTRAL+10, 1);
49
50// Security option : Count of login failure and lock account after x attempt
51// -------------------------------------------------------------------------
52add_event_handler('login_failure', 'PP_log_fail');
53
54// Add new feature in user_list - Password Reset
55// ---------------------------------------------
56if (isset($conf_PP['PWDRESET']) and $conf_PP['PWDRESET'] == 'true')
57{
58  // Add new column on user_list
59  // ---------------------------
60  add_event_handler('loc_visible_user_list', 'PP_user_list_pwdreset');
61
62  // Add prefilter on user_list
63  // --------------------------
64  add_event_handler('loc_begin_admin', 'PP_PwdReset_Action',60);
65
66  /**
67   * PP_PwdReset_Action - Triggered on PP_PwdReset_Action
68   * Handle password reset action in user_list.php
69   */
70  function PP_PwdReset_Action()
71  {
72    global $conf, $user, $template, $lang, $errors;
73
74    $page['errors'] = array();
75    $page['infos'] = array();
76    $page['filtered_users'] = array();
77
78    load_language('plugin.lang', PP_PATH);
79
80    if (isset($_POST['pwdreset']))
81    {
82      $collection = array();
83
84      switch ($_POST['target'])
85      {
86        case 'all' :
87        {
88          foreach($page['filtered_users'] as $local_user)
89          {
90            array_push($collection, $local_user['id']);
91          }
92          break;
93        }
94        case 'selection' :
95        {
96          if (isset($_POST['selection']))
97          {
98            $collection = $_POST['selection'];
99          }
100          break;
101        }
102      }
103
104      if (count($collection) == 0)
105      {
106        array_push($page['errors'], l10n('Select at least one user'));
107      }
108    }
109
110    if (isset($_POST['pwdreset']) and count($collection) > 0)
111    {
112      if (in_array($conf['guest_id'], $collection))
113      {
114        array_push($page['errors'], l10n('PP_Guest cannot be pwdreset'));
115        $template->append('errors', l10n('PP_Guest cannot be pwdreset'));
116      }
117      if (($conf['guest_id'] != $conf['default_user_id']) and
118        in_array($conf['default_user_id'], $collection))
119      {
120        array_push($page['errors'], l10n('PP_Default user cannot be pwdreset'));
121        $template->append('errors', l10n('PP_Default user cannot be pwdreset'));
122      }
123      if (in_array($conf['webmaster_id'], $collection))
124      {
125        array_push($page['errors'], l10n('PP_Webmaster cannot be pwdreset'));
126        $template->append('errors', l10n('PP_Webmaster cannot be pwdreset'));
127      }
128      if (in_array($user['id'], $collection))
129      {
130        array_push($page['errors'], l10n('PP_You cannot pwdreset your account'));
131        $template->append('errors', l10n('PP_You cannot pwdreset your account'));
132      }
133
134      // Generic accounts exclusion (including Adult_Content generic users)
135      // ------------------------------------------------------------------
136      $query ='
137SELECT u.id
138FROM '.USERS_TABLE.' AS u
139INNER JOIN '.USER_INFOS_TABLE.' AS ui
140  ON u.id = ui.user_id
141WHERE ui.status = "generic"
142;';
143
144            $result = pwg_query($query);
145
146      while ($row = pwg_db_fetch_assoc($result))
147      {
148        if (in_array($row['id'], $collection))
149        {
150          array_push($page['errors'], l10n('PP_Generic cannot be pwdreset'));
151          $errors = l10n('PP_Generic cannot be pwdreset');
152        }
153      }
154
155      // Admins accounts exclusion
156      // --------------------------
157      $query ='
158SELECT u.id
159FROM '.USERS_TABLE.' AS u
160INNER JOIN '.USER_INFOS_TABLE.' AS ui
161  ON u.id = ui.user_id
162WHERE ui.status = "admin"
163;';
164
165            $result = pwg_query($query);
166
167      while ($row = pwg_db_fetch_assoc($result))
168      {
169        if (in_array($row['id'], $collection))
170        {
171          array_push($page['errors'], l10n('PP_Admins cannot be pwdreset'));
172          $errors = l10n('PP_Admins cannot be pwdreset');
173        }
174      }
175
176      $template->append('errors', $errors);
177
178      if (count($page['errors']) == 0)
179      {
180        if (isset($_POST['confirm_pwdreset']) and 1 == $_POST['confirm_pwdreset'])
181        {
182          foreach ($collection as $user_id)
183          {
184            PP_Set_PwdReset($user_id);
185          }
186          array_push(
187            $page['infos'],
188            l10n_dec(
189              'PP %d user pwdreseted', 'PP %d users pwdreseted',
190              count($collection)
191              )
192            );
193          $template->append('infos', l10n_dec(
194              'PP %d user pwdreseted', 'PP %d users pwdreseted',
195              count($collection)));
196          foreach ($page['filtered_users'] as $filter_key => $filter_user)
197          {
198            if (in_array($filter_user['id'], $collection))
199            {
200              unset($page['filtered_users'][$filter_key]);
201            }
202          }
203        }
204        else
205        {
206          array_push($page['errors'], l10n('PP_You need to confirm pwdreset'));
207          $template->append('errors', l10n('PP_You need to confirm pwdreset'));
208        }
209      }
210    }
211    $template->set_prefilter('user_list', 'PP_PwdReset_Prefilter');
212  }
213
214  /**
215   * PP_PwdReset_Prefilter
216   * Adds action field for password reset in user_list.tpl
217   */
218  function PP_PwdReset_Prefilter($content, &$smarty)
219  {
220    $search = '
221<fieldset>
222  <legend>{\'Deletions\'|@translate}</legend>
223  <label><input type="checkbox" name="confirm_deletion" value="1"> {\'confirm\'|@translate}</label>
224  <input class="submit" type="submit" value="{\'Delete selected users\'|@translate}" name="delete">
225</fieldset>
226';
227 
228    $addon = '
229<fieldset>
230  <legend>{\'PP_PwdReset\'|@translate}</legend>
231  <label><input type="checkbox" name="confirm_pwdreset" value="1"> {\'confirm\'|@translate}</label>
232  <input class="submit" type="submit" value="{\'PP_Password reset selected users\'|@translate}" name="pwdreset">
233</fieldset>
234';
235
236    $replacement = $addon.$search;
237
238    return str_replace($search, $replacement, $content);
239  }
240}
241
242
243// Add new feature in user_list - Show locked accounts and give unlock function
244// ----------------------------------------------------------------------------
245if (isset($conf_PP['LOGFAILBLOCK']) and $conf_PP['LOGFAILBLOCK']=='true')
246{
247  // Add new column on user_list
248  // ---------------------------
249  add_event_handler('loc_visible_user_list', 'PP_user_list_locked');
250
251  // Add prefilter on user_list
252  // --------------------------
253  add_event_handler('loc_begin_admin', 'PP_Unlock_Action',60);
254
255  /**
256   * PP_Unlock_Action - Triggered on PP_Unlock_Action
257   * Handle unlocking action in user_list.php
258   */
259  function PP_Unlock_Action()
260  {
261    global $conf, $user, $template, $lang, $errors;
262
263    $page['errors'] = array();
264    $page['infos'] = array();
265    $page['filtered_users'] = array();
266
267    load_language('plugin.lang', PP_PATH);
268
269    if (isset($_POST['unlock']))
270    {
271      $collection = array();
272
273      switch ($_POST['target'])
274      {
275        case 'all' :
276        {
277          foreach($page['filtered_users'] as $local_user)
278          {
279            array_push($collection, $local_user['id']);
280          }
281          break;
282        }
283        case 'selection' :
284        {
285          if (isset($_POST['selection']))
286          {
287            $collection = $_POST['selection'];
288          }
289          break;
290        }
291      }
292
293      if (count($collection) == 0)
294      {
295        array_push($page['errors'], l10n('Select at least one user'));
296      }
297    }
298
299    if (isset($_POST['unlock']) and count($collection) > 0)
300    {
301      if (in_array($conf['guest_id'], $collection))
302      {
303        array_push($page['errors'], l10n('PP_Guest is not unlockable'));
304        $template->append('errors', l10n('PP_Guest is not unlockable'));
305      }
306      if (($conf['guest_id'] != $conf['default_user_id']) and
307        in_array($conf['default_user_id'], $collection))
308      {
309        array_push($page['errors'], l10n('PP_Default user is not unlockable'));
310        $template->append('errors', l10n('PP_Default user is not unlockable'));
311      }
312      if (in_array($conf['webmaster_id'], $collection))
313      {
314        array_push($page['errors'], l10n('PP_Webmaster is not unlockable'));
315        $template->append('errors', l10n('PP_Webmaster is not unlockable'));
316      }
317      if (in_array($user['id'], $collection))
318      {
319        array_push($page['errors'], l10n('PP_You cannot unlock your account'));
320        $template->append('errors', l10n('PP_You cannot unlock your account'));
321      }
322
323      // Generic accounts exclusion (including Adult_Content generic users)
324      // ------------------------------------------------------------------
325      $query ='
326SELECT u.id
327FROM '.USERS_TABLE.' AS u
328INNER JOIN '.USER_INFOS_TABLE.' AS ui
329  ON u.id = ui.user_id
330WHERE ui.status = "generic"
331;';
332
333            $result = pwg_query($query);
334
335      while ($row = pwg_db_fetch_assoc($result))
336      {
337        if (in_array($row['id'], $collection))
338        {
339          array_push($page['errors'], l10n('PP_Generic is not unlockable'));
340          $errors = l10n('PP_Generic is not unlockable');
341        }
342      }
343
344      // Admins accounts exclusion
345      // --------------------------
346      $query ='
347SELECT u.id
348FROM '.USERS_TABLE.' AS u
349INNER JOIN '.USER_INFOS_TABLE.' AS ui
350  ON u.id = ui.user_id
351WHERE ui.status = "admin"
352;';
353
354            $result = pwg_query($query);
355
356      while ($row = pwg_db_fetch_assoc($result))
357      {
358        if (in_array($row['id'], $collection))
359        {
360          array_push($page['errors'], l10n('PP_Admins is not unlockable'));
361          $errors = l10n('PP_Admins is not unlockable');
362        }
363      }
364
365      $template->append('errors', $errors);
366
367      if (count($page['errors']) == 0)
368      {
369        if (isset($_POST['confirm_unlock']) and 1 == $_POST['confirm_unlock'])
370        {
371          foreach ($collection as $user_id)
372          {
373            PP_unlock_user($user_id);
374          }
375          array_push(
376            $page['infos'],
377            l10n_dec(
378              'PP %d user unlocked', 'PP %d users unlocked',
379              count($collection)
380              )
381            );
382          $template->append('infos', l10n_dec(
383              'PP %d user unlocked', 'PP %d users unlocked',
384              count($collection)));
385          foreach ($page['filtered_users'] as $filter_key => $filter_user)
386          {
387            if (in_array($filter_user['id'], $collection))
388            {
389              unset($page['filtered_users'][$filter_key]);
390            }
391          }
392        }
393        else
394        {
395          array_push($page['errors'], l10n('PP_You need to confirm unlock'));
396          $template->append('errors', l10n('PP_You need to confirm unlock'));
397        }
398      }
399    }
400    $template->set_prefilter('user_list', 'PP_Unlocking_Prefilter');
401  }
402
403  /**
404   * PP_Unlocking_Prefilter
405   * Adds action field for user unlocking in user_list.tpl
406   */
407  function PP_Unlocking_Prefilter($content, &$smarty)
408  {
409    $search = '
410<fieldset>
411  <legend>{\'Deletions\'|@translate}</legend>
412  <label><input type="checkbox" name="confirm_deletion" value="1"> {\'confirm\'|@translate}</label>
413  <input class="submit" type="submit" value="{\'Delete selected users\'|@translate}" name="delete">
414</fieldset>
415';
416 
417    $addon = '
418<fieldset>
419  <legend>{\'PP_Unlock\'|@translate}</legend>
420  <label><input type="checkbox" name="confirm_unlock" value="1"> {\'confirm\'|@translate}</label>
421  <input class="submit" type="submit" value="{\'PP_Unlock selected users\'|@translate}" name="unlock">
422</fieldset>
423';
424
425    $replacement = $addon.$search;
426
427    return str_replace($search, $replacement, $content);
428  }
429}
430?>
Note: See TracBrowser for help on using the repository browser.