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

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

New function : Timelimit for user's validation. When exeeded, users can't validate their registration.

Add of admin settings panel for the new function.

Todo : Unvalidated users management.

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