source: extensions/NBC_UserAdvManager/trunk/include/functions_UserAdvManager.inc.php @ 3845

Last change on this file since 3845 was 3845, checked in by Eric, 15 years ago
  • Finalization and improvement of resending validation mail with and without new key regeneration. That works fine now.
  • Adding of a new plugin tab to display users management table

-> Left to do :

  • Adding "force validation" function
  • Beta testing
  • Property svn:eol-style set to LF
File size: 16.4 KB
Line 
1<?php
2include_once (NBC_UserAdvManager_PATH.'include/constants.php');
3load_language('plugin.lang', NBC_UserAdvManager_PATH);
4
5function SendMail2User($typemail, $id, $username, $password, $email, $confirm)
6{
7  global $conf;
8  $conf_nbc_UserAdvManager = isset($conf['nbc_UserAdvManager']) ? explode(";" , $conf['nbc_UserAdvManager']) : array();
9 
10        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
11 
12        $infos1_perso = "";
13  $infos2_perso = "";
14
15  switch($typemail)
16  {
17    case 1:
18      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Add of %s', $username));
19      $password = $password <> '' ? $password : l10n('UserAdvManager_empty_pwd');
20     
21      if ( function_exists('get_user_language_desc') and isset($conf_nbc_UserAdvManager[10]) and $conf_nbc_UserAdvManager[10] <> '' )
22        $infos1_perso = get_user_language_desc($conf_nbc_UserAdvManager[10])."\n\n";
23     
24      break;
25     
26    case 2:
27      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Update of %s', $username));
28      $password = $password <> '' ? $password : l10n('UserAdvManager_empty_pwd');
29
30      break;
31       
32    case 3:
33      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Update of %s', $username));
34      $password = $password <> '' ? $password : l10n('UserAdvManager_no_update_pwd');
35
36      break;
37  }
38
39  if ( isset($conf_nbc_UserAdvManager[0]) and $conf_nbc_UserAdvManager[0] == 'true' )
40  {
41    $infos1 = array(
42      get_l10n_args('infos_mail %s', $username),
43      get_l10n_args('User: %s', $username),
44      get_l10n_args('Password: %s', $password),
45      get_l10n_args('Email: %s', $email),
46      get_l10n_args('', ''),
47    );
48  }
49
50
51  if ( isset($conf_nbc_UserAdvManager[2]) and $conf_nbc_UserAdvManager[2] == 'true' and $confirm)
52  {
53    $infos2 = array
54    (
55      get_l10n_args('Link: %s', AddConfirmMail($id, $email)),
56      get_l10n_args('', ''),
57    );
58
59    if ( function_exists('get_user_language_desc') and isset($conf_nbc_UserAdvManager[11]) and $conf_nbc_UserAdvManager[11] <> '' )
60      $infos2_perso = get_user_language_desc($conf_nbc_UserAdvManager[11])."\n\n";
61  }
62
63  pwg_mail($email, array(
64    'subject' => $subject,
65    'content' => (isset($infos1) ? $infos1_perso.l10n_args($infos1)."\n\n" : "").(isset($infos2) ? $infos2_perso.l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
66  ));
67
68/* ********************** */
69/* Email sending debugger */
70/* This is only to trace  */
71/* the send of emails for */
72/* debugging              */
73/* ********************** */ 
74  MailLog($email,$subject);
75/* ********************** */
76
77}
78
79/* Email sending debugger function */
80function MailLog  ($to, $subject)
81{
82   $fo=fopen (NBC_UserAdvManager_PATH.'admin/maillog.txt','a') ;
83   fwrite($fo,"======================\n") ;
84   fwrite($fo,'le ' . date('D, d M Y H:i:s') . "\r\n");
85   fwrite($fo,$to . "\n" . $subject . "\r\n") ;
86   fclose($fo) ;
87   //return mail ($to,$subject) ;
88}
89
90
91function ResendMail2User($typemail, $user_id, $username, $email, $confirm)
92{
93  global $conf;
94  $conf_nbc_UserAdvManager = isset($conf['nbc_UserAdvManager']) ? explode(";" , $conf['nbc_UserAdvManager']) : array();
95        $conf_nbc_UserAdvManager_ConfirmMail = isset($conf['nbc_UserAdvManager_ConfirmMail']) ? explode(";" , $conf['nbc_UserAdvManager_ConfirmMail']) : array();
96 
97        include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
98 
99        $infos1_perso = "";
100  $infos2_perso = "";
101
102  switch($typemail)
103  {
104    case 1:
105      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Reminder_with_key_of_%s', $username));
106     
107      if (function_exists('get_user_language_desc') and isset($conf_nbc_UserAdvManager_ConfirmMail[2]) and $conf_nbc_UserAdvManager_ConfirmMail[2] <> '' and isset($conf_nbc_UserAdvManager_ConfirmMail[3]) and $conf_nbc_UserAdvManager_ConfirmMail[3] == 'true' and $confirm)
108      {
109                                $infos1 = get_user_language_desc($conf_nbc_UserAdvManager_ConfirmMail[2])."\n\n";
110                       
111                                $infos2 = array
112                                (
113                                        get_l10n_args('Link: %s', ResetConfirmMail($user_id)),
114                                        get_l10n_args('', ''),
115                                );
116                        }
117                break;
118     
119    case 2:
120      $subject = '['.$conf['gallery_title'].'] '.l10n_args(get_l10n_args('Reminder_without_key_of_%s', $username));
121     
122      if (function_exists('get_user_language_desc') and isset($conf_nbc_UserAdvManager_ConfirmMail[2]) and $conf_nbc_UserAdvManager_ConfirmMail[2] <> '' and isset($conf_nbc_UserAdvManager_ConfirmMail[3]) and $conf_nbc_UserAdvManager_ConfirmMail[3] == 'true' and !$confirm)
123      {
124                                $infos1 = get_user_language_desc($conf_nbc_UserAdvManager_ConfirmMail[2])."\n\n";
125      }
126    break;
127        }
128  pwg_mail($email, array(
129    'subject' => $subject,
130    'content' => ($infos1."\n\n").(isset($infos2) ? l10n_args($infos2)."\n\n" : "").get_absolute_root_url(),
131  ));
132
133/* ********************** */
134/* Email sending debugger */
135/* This is only to trace  */
136/* the send of emails for */
137/* debugging              */
138/* ********************** */ 
139  MailLog($email,$subject);
140/* ********************** */
141
142}
143
144
145function FindAvailableConfirmMailID()
146{
147  while (true)
148  {
149    $id = generate_key(16);
150    $query = "
151      SELECT COUNT(*)
152      FROM ".USER_CONFIRM_MAIL_TABLE."
153      WHERE id = '".$id."'
154    ;";
155    list($count) = mysql_fetch_row(pwg_query($query));
156
157    if ($count == 0)
158      return $id;
159  }
160}
161
162
163
164function AddConfirmMail($user_id, $email)
165{
166  global $conf;
167  $conf_nbc_UserAdvManager = isset($conf['nbc_UserAdvManager']) ? explode(";" , $conf['nbc_UserAdvManager']) : array();
168  $Confirm_Mail_ID = FindAvailableConfirmMailID();
169
170  list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
171 
172  if ( isset($Confirm_Mail_ID) )
173  {
174    $query = "
175      SELECT status
176      FROM ".USER_INFOS_TABLE."
177      WHERE user_id = '".$user_id."'
178    ;";
179    list($status) = mysql_fetch_row(pwg_query($query));
180   
181    $query = "
182      INSERT INTO ".USER_CONFIRM_MAIL_TABLE."
183      (id, user_id, mail_address, status, date_check)
184      VALUES
185      ('".$Confirm_Mail_ID."', '".$user_id."', '".$email."', '".$status."', null)
186    ;";
187    pwg_query($query);
188
189    $query = "
190      DELETE FROM ".USER_GROUP_TABLE."
191      WHERE user_id = '".$user_id."'
192      AND (
193        group_id = '".$conf_nbc_UserAdvManager[3]."'
194        OR
195        group_id = '".$conf_nbc_UserAdvManager[4]."'
196      )
197    ;";
198    pwg_query($query);
199
200    if ( !is_admin() and $conf_nbc_UserAdvManager[9] <> -1 )
201    {
202      $query = "
203        UPDATE ".USER_INFOS_TABLE."
204        SET status = '".$conf_nbc_UserAdvManager[9]."'
205        WHERE user_id = '".$user_id."'
206      ;";
207      pwg_query($query);
208    }
209
210    if ( $conf_nbc_UserAdvManager[3] <> -1 )
211    {
212      $query = "
213        INSERT INTO ".USER_GROUP_TABLE."
214        (user_id, group_id)
215        VALUES
216        ('".$user_id."', '".$conf_nbc_UserAdvManager[3]."')
217      ;";
218      pwg_query($query);
219    }
220   
221    return get_absolute_root_url().NBC_UserAdvManager_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID;
222  }
223}
224
225
226function ResetConfirmMail($user_id)
227{
228  global $conf;
229 
230  $Confirm_Mail_ID = FindAvailableConfirmMailID();
231
232  list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
233 
234  if ( isset($Confirm_Mail_ID) )
235  { 
236    $query = "
237                        UPDATE ".USER_CONFIRM_MAIL_TABLE."
238                        SET id = '".$Confirm_Mail_ID."'
239                        WHERE user_id = '".$user_id."'
240    ;";
241    pwg_query($query);
242
243                $query = "
244        UPDATE ".USER_INFOS_TABLE."
245      SET registration_date = '".$dbnow."'
246      WHERE user_id = '".$user_id."'
247      ;";
248                pwg_query($query);
249   
250    return get_absolute_root_url().NBC_UserAdvManager_PATH.'ConfirmMail.php?key='.$Confirm_Mail_ID;
251  }
252}
253
254
255function DeleteConfirmMail($user_id)
256{
257  $query = "
258    DELETE FROM ".USER_CONFIRM_MAIL_TABLE."
259    WHERE user_id = '".$user_id."'
260  ;";
261  pwg_query($query);
262}
263
264
265
266function VerifyConfirmMail($id)
267{
268  global $conf;
269
270  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
271 
272  $conf_nbc_UserAdvManager = isset($conf['nbc_UserAdvManager']) ? explode(";" , $conf['nbc_UserAdvManager']) : array();
273  $conf_nbc_UserAdvManager_ConfirmMail = isset($conf['nbc_UserAdvManager_ConfirmMail']) ? explode(";" , $conf['nbc_UserAdvManager_ConfirmMail']) : array();
274
275  $query = "
276    SELECT COUNT(*)
277    FROM ".USER_CONFIRM_MAIL_TABLE."
278    WHERE id = '".$id."'
279  ;";
280  list($count) = mysql_fetch_row(pwg_query($query));
281
282  if ($count == 1)
283  {
284    $query = "
285      SELECT user_id, status, date_check
286      FROM ".USER_CONFIRM_MAIL_TABLE."
287      WHERE id = '".$id."'
288    ;";
289    $data = mysql_fetch_array(pwg_query($query));
290       
291    if (!empty($data) and isset($data['user_id']) and !isset($data['date_check']))
292    {
293      $query = "
294        SELECT registration_date
295        FROM ".USER_INFOS_TABLE."
296        WHERE user_id = '".$data['user_id']."'
297      ;";
298      list($registration_date) = mysql_fetch_row(pwg_query($query));
299
300/*              Time limit process              */
301/* ****************** begin ******************* */ 
302      if (!empty($registration_date))
303      {
304                // Verify Confirmmail with time limit ON
305                if (isset ($conf_nbc_UserAdvManager_ConfirmMail[1]))
306                {
307                        // dates formating and compare
308                        $today = date("d-m-Y"); // Get today's date
309                        list($day, $month, $year) = explode('-', $today); // explode date of today                                               
310                        $daytimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp
311                       
312                        list($regdate, $regtime) = explode(' ', $registration_date); // Explode date and time from registration date
313                        list($regyear, $regmonth, $regday) = explode('-', $regdate); // Explode date from registration date
314                        $regtimestamp = mktime(0, 0, 0, $regmonth, $regday, $regyear);// Generate UNIX timestamp
315                       
316                        $deltasecs = $daytimestamp - $regtimestamp;// Compare the 2 UNIX timestamps     
317                        $deltadays = floor($deltasecs / 86400);// Convert result from seconds to days
318
319                        // Condition with the value set for time limit
320                        if ($deltadays <= $conf_nbc_UserAdvManager_ConfirmMail[1]) // If Nb of days is less than the set limit
321                        {
322                                list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
323
324                                $query = '
325                                        UPDATE '.USER_CONFIRM_MAIL_TABLE.'
326                                        SET date_check="'.$dbnow.'"
327                                        WHERE id = "'.$id.'"
328                                        ;';
329                                pwg_query($query);
330     
331                                if ( $conf_nbc_UserAdvManager[3] <> -1 )
332                                {
333                                        $query = "
334                                                DELETE FROM ".USER_GROUP_TABLE."
335                                                WHERE user_id = '".$data['user_id']."'
336                                                AND group_id = '".$conf_nbc_UserAdvManager[3]."'
337                                                ;";
338                                        pwg_query($query);
339                                }
340           
341                                if ( $conf_nbc_UserAdvManager[4] <> -1 ) // Change user's group
342                                {
343                                        $query = "
344                                                DELETE FROM ".USER_GROUP_TABLE."
345                                                WHERE user_id = '".$data['user_id']."'
346                                                AND group_id = '".$conf_nbc_UserAdvManager[4]."'
347                                                ;";
348                                        pwg_query($query);
349       
350                                        $query = "
351                                                INSERT INTO ".USER_GROUP_TABLE."
352                                                        (user_id, group_id)
353                                                VALUES
354                                                        ('".$data['user_id']."', '".$conf_nbc_UserAdvManager[4]."')
355                                                ;";
356                                        pwg_query($query);
357                                }
358
359                                if (($conf_nbc_UserAdvManager[5] <> -1 or isset($data['status']))) // Change user's status
360                                {
361                                        $query = "
362                                                UPDATE ".USER_INFOS_TABLE."
363                                                SET status = '".(isset($data['status']) ? $data['status'] : $conf_nbc_UserAdvManager[5])."'
364                                                WHERE user_id = '".$data['user_id']."'
365                                                ;";
366                                        pwg_query($query);
367                                }
368                        // Refresh user's category cache
369                                invalidate_user_cache();
370 
371                                return true;
372                        }
373                        elseif ($deltadays > $conf_nbc_UserAdvManager_ConfirmMail[1]) // If timelimit exeeds
374                        {
375                                return false;
376                        }
377                }
378                // Verify Confirmmail with time limit OFF
379                else
380                {
381                        list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
382
383                        $query = '
384                                UPDATE '.USER_CONFIRM_MAIL_TABLE.'
385                                SET date_check="'.$dbnow.'"
386                                WHERE id = "'.$id.'"
387                                ;';
388                        pwg_query($query);
389     
390                        if ( $conf_nbc_UserAdvManager[3] <> -1 )
391                        {
392                                $query = "
393                                        DELETE FROM ".USER_GROUP_TABLE."
394                                        WHERE user_id = '".$data['user_id']."'
395                                        AND group_id = '".$conf_nbc_UserAdvManager[3]."'
396                                        ;";
397                                pwg_query($query);
398                        }
399   
400                        if ( $conf_nbc_UserAdvManager[4] <> -1 )
401                        {
402                                $query = "
403                                        DELETE FROM ".USER_GROUP_TABLE."
404                                        WHERE user_id = '".$data['user_id']."'
405                                        AND group_id = '".$conf_nbc_UserAdvManager[4]."'
406                                        ;";
407                                pwg_query($query);
408
409                                $query = "
410                                        INSERT INTO ".USER_GROUP_TABLE."
411                                                (user_id, group_id)
412                                        VALUES
413                                                ('".$data['user_id']."', '".$conf_nbc_UserAdvManager[4]."')
414                                        ;";
415                                pwg_query($query);
416                        }
417
418                        if ( ( $conf_nbc_UserAdvManager[5] <> -1 or isset($data['status']) ) )
419                        {
420                                $query = "
421                                        UPDATE ".USER_INFOS_TABLE."
422                                        SET status = '".(isset($data['status']) ? $data['status'] : $conf_nbc_UserAdvManager[5])."'
423                                        WHERE user_id = '".$data['user_id']."'
424                                        ;";
425                                pwg_query($query);
426                        }
427// Refresh user's category cache
428                        invalidate_user_cache();
429 
430                        return true;
431                }
432      }
433    }
434  }
435  else
436    return false;
437}
438
439
440function NotSensibleSearchUsername($username)
441{
442  global $conf;
443 
444  if ( isset($username) )
445  {
446    $query = "
447      SELECT ".$conf['user_fields']['username']."
448      FROM ".USERS_TABLE."
449      WHERE LOWER(".$conf['user_fields']['username'].") = '".strtolower($username)."'
450    ;";
451    list($username) = mysql_fetch_row(pwg_query($query));
452
453    return isset($username) ? $username : '';
454  }
455}
456
457
458function SearchMail($email)
459{
460  global $conf, $lang;
461 
462  if ( isset($email) )
463  {
464    $query = "
465      SELECT COUNT(*)
466      FROM ".USERS_TABLE."
467      WHERE ".$conf['user_fields']['email']." = '".$email."'
468    ;";
469    list($nbr_mail) = mysql_fetch_row(pwg_query($query));
470 
471    return isset($nbr_mail) ? $nbr_mail : 0;
472  }
473}
474
475function ValidateUsername($login)
476{
477  global $conf;
478
479  $conf_nbc_UserAdvManager = isset($conf['nbc_UserAdvManager']) ? explode(";" , $conf['nbc_UserAdvManager']) : array();
480
481  if ( isset($login) and isset($conf_nbc_UserAdvManager[8]) and $conf_nbc_UserAdvManager[8] <> '' )
482  {
483        $pattern = '/'.$conf_nbc_UserAdvManager[8].'/';
484    if ( preg_match($pattern, $login) )
485      return false;
486    else
487      return true;
488  }
489  else
490  {
491    return true;
492  }
493}
494
495
496function ValidateEmailProvider($email)
497{
498  global $conf;
499
500        $conf_nbc_UserAdvManager = isset($conf['nbc_UserAdvManager']) ? explode(";" , $conf['nbc_UserAdvManager']) : array();
501        if (isset($conf_nbc_UserAdvManager[12]))
502        {
503                $ncsemail = strtolower($email);
504                $conf_nbc_MailExclusion = preg_split('/,/',$conf_nbc_UserAdvManager[13]);
505                for ($i = 0 ; $i < count($conf_nbc_MailExclusion) ; $i++)
506                {
507                        $pattern = '/'.$conf_nbc_MailExclusion[$i].'/';
508                        if (preg_match($pattern, $ncsemail))
509                        return false;
510                else
511                        return true;
512                }
513        }
514}
515
516
517function get_unvalid_user_list()
518{
519        global $conf, $page;
520         
521        /* Get ConfirmMail configuration */
522    $conf_nbc_UserAdvManager_ConfirmMail = isset($conf['nbc_UserAdvManager_ConfirmMail']) ? explode(";" , $conf['nbc_UserAdvManager_ConfirmMail']) : array();
523         
524        /* Get UserAdvManager configuration */
525        $conf_nbc_UserAdvManager = isset($conf['nbc_UserAdvManager']) ? explode(";" , $conf['nbc_UserAdvManager']) : array(); 
526 
527    $users = array();
528   
529        /* search users depending expiration date */
530    $query = '
531        SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
532                                                                                        u.'.$conf['user_fields']['username'].' AS username,
533                      u.'.$conf['user_fields']['email'].' AS email,
534                      ui.status,
535                      ui.adviser,
536                      ui.enabled_high,
537                      ui.level,
538                      ui.registration_date,
539                      ug.group_id
540                        FROM '.USERS_TABLE.' AS u
541                        INNER JOIN '.USER_INFOS_TABLE.' AS ui
542                                ON u.'.$conf['user_fields']['id'].' = ui.user_id
543                INNER JOIN '.USER_GROUP_TABLE.' AS ug
544        ON u.'.$conf['user_fields']['id'].' = ug.user_id
545        WHERE u.'.$conf['user_fields']['id'].' >= 0
546        AND ug.group_id = "'.$conf_nbc_UserAdvManager[3].'"
547        AND TO_DAYS(NOW()) - TO_DAYS(ui.registration_date) >= "'.$conf_nbc_UserAdvManager_ConfirmMail[1].'"
548        ORDER BY id ASC
549                ;';
550
551        $result = pwg_query($query);
552     
553    while ($row = mysql_fetch_array($result))
554        {
555        $user = $row;
556        $user['groups'] = array();
557
558        array_push($users, $user);
559        }
560
561        /* add group lists */
562    $user_ids = array();
563    foreach ($users as $i => $user)
564    {
565        $user_ids[$i] = $user['id'];
566        }
567       
568        $user_nums = array_flip($user_ids);
569
570    if (count($user_ids) > 0)
571    {
572        $query = '
573                SELECT user_id, group_id
574                        FROM '.USER_GROUP_TABLE.'
575            WHERE user_id IN ('.implode(',', $user_ids).')
576            ;';
577       
578                $result = pwg_query($query);
579       
580        while ($row = mysql_fetch_array($result))
581        {
582                array_push(
583                $users[$user_nums[$row['user_id']]]['groups'],
584                $row['group_id']
585                );
586         }
587        }
588
589return $users;
590}
591?>
Note: See TracBrowser for help on using the repository browser.