source: extensions/Register_FluxBB/include/functions.inc.php @ 22092

Last change on this file since 22092 was 22092, checked in by Eric, 7 years ago

Version 2.5.8:
Add accounts synchronization from FluxBB to Piwigo
Bug fixed : Missing function in audit

  • Property svn:eol-style set to LF
File size: 26.0 KB
Line 
1<?php
2
3include_once (PHPWG_ROOT_PATH.'/include/constants.php');
4include_once (REGFLUXBB_PATH.'include/constants.php');
5
6
7function Register_FluxBB_admin_menu($menu)
8{
9  array_push($menu, array(
10    'NAME' => 'Register FluxBB',
11    'URL' => get_root_url().'admin.php?page=plugin-'.basename(REGFLUXBB_PATH)
12    )
13  );
14  return $menu;
15}
16
17
18/**
19 * Change user's password in FluxBB user table if a new password is set in Piwigo
20 * or an account synchronization was set
21 */
22function Register_FluxBB_InitProfile()
23{
24  global $template, $conf, $user;
25
26  if (Reg_FluxBB_PwdSynch($user['id']))
27  {
28    $template->append('errors', l10n('RegFluxBB_Password_Reset_Msg'));
29  }
30
31  if (isset($_POST['validate']))
32  {
33    if (!empty($_POST['use_new_pwd']))
34    {
35      $query = '
36SELECT '.$conf['user_fields']['username'].' AS username
37FROM '.USERS_TABLE.'
38WHERE '.$conf['user_fields']['id'].' = \''.$user['id'].'\'
39AND '.$conf['user_fields']['username'].' NOT IN ("18","16")
40;';
41
42      list($username) = pwg_db_fetch_row(pwg_query($query));
43      FluxBB_Updateuser($user['id'], stripslashes($username), sha1($_POST['use_new_pwd']), $_POST['mail_address']);
44    }
45  }
46}
47
48
49/**
50 * Triggered on login_success
51 *
52 * Redirect user to profile page and displays a message to make him change his password for synch with FluxBB
53 *
54 */
55function Register_FluxBB_Login()
56{
57  global $conf, $user;
58
59    if (Reg_FluxBB_PwdSynch($user['id']))
60    {
61      redirect(PHPWG_ROOT_PATH.'profile.php');
62    }
63}
64
65
66/**
67 * Checks special users exclusion befaore add new registered user in FluxBB user table
68 */
69function Register_FluxBB_Adduser($register_user)
70{
71  global $errors, $conf;
72       
73  // Exclusion of Adult_Content users
74  if ($register_user['username'] != "16" and $register_user['username'] != "18")
75  {
76    FluxBB_Adduser($register_user['id'], $register_user['username'], sha1($_POST['password']), $register_user['email']);
77  }
78}
79
80/**
81 * Delete registered user in FluxBB user table
82 */
83function Register_FluxBB_Deluser($user_id)
84{
85  FluxBB_Deluser(FluxBB_Searchuser($user_id), true);
86}
87
88
89/**
90 * Update FluxBB password if user uses "lost password"
91 */
92function Register_FluxBB_PasswReset()
93{
94  global $page, $user, $conf;
95
96  if (isset($_POST['submit']))
97  {
98    if ('reset' == $_GET['action'])
99    {
100      $user_id = check_password_reset_key($_GET['key']);
101
102      $query = '
103SELECT '.$conf['user_fields']['username'].' AS username, mail_address
104FROM '.USERS_TABLE.'
105WHERE '.$conf['user_fields']['id'].' = \''.$user_id.'\'
106AND '.$conf['user_fields']['username'].' NOT IN ("18","16")
107;';
108
109      list($username,$mail_address) = pwg_db_fetch_row(pwg_query($query));
110      FluxBB_Updateuser($user_id, stripslashes($username), sha1($_POST['use_new_pwd']), $mail_address);
111    }
112  }
113}
114
115/**
116 * Bridge with UAM confirmation option
117 */
118function UAM_Bridge()
119{
120  global $conf, $user;
121 
122  $conf_Register_FluxBB = unserialize($conf['Register_FluxBB']);
123 
124  // Check if UAM is installed and if bridge is set - Exception for admins and webmasters
125  $query ='
126SELECT user_id, status
127FROM '.USER_INFOS_TABLE.'
128WHERE user_id = '.$user['id'].'
129;';
130  $data = pwg_db_fetch_assoc(pwg_query($query));
131 
132  if ($data['status'] <> "admin" and $data['status'] <> "webmaster")
133  {
134    if (function_exists('FindAvailableConfirmMailID') and isset($conf_Register_FluxBB['FLUXBB_UAM_LINK']) and $conf_Register_FluxBB['FLUXBB_UAM_LINK'] == 'true')
135    {
136      $conf_UAM = unserialize($conf['UserAdvManager']);
137   
138      // Getting unvalidated users group else Piwigo's default group
139      if (isset($conf_UAM['NO_CONFIRM_GROUP']) and $conf_UAM['NO_CONFIRM_GROUP'] != '-1')
140      {
141        $Waitingroup = $conf_UAM['NO_CONFIRM_GROUP'];
142      }
143      else
144      {
145        $query = '
146SELECT id
147FROM '.GROUPS_TABLE.'
148WHERE is_default = "true"
149LIMIT 1
150;';
151        $data = pwg_db_fetch_assoc(pwg_query($query));
152        $Waitingroup = $data['id'];
153      }
154   
155      // check if logged in user is in a Piwigo's validated or unvalidated users group
156      $query = '
157SELECT *
158FROM '.USER_GROUP_TABLE.'
159WHERE user_id = '.$user['id'].'
160AND group_id = '.$Waitingroup.'
161;';
162      $count = pwg_db_num_rows(pwg_query($query));
163
164      // Check if logged in user is in a FluxBB's unvalidated group
165      $query = '
166SELECT group_id
167FROM '.FluxBB_USERS_TABLE.'
168WHERE id = '.FluxBB_Searchuser($user['id']).'
169;';
170
171      $data = pwg_db_fetch_assoc(pwg_query($query));
172
173      // Logged in user switch to the default FluxBB's group if he is validated
174      if ($count == 0 and $data['group_id'] = $conf_Register_FluxBB['FLUXBB_GROUP'])
175      {
176        $query = '
177SELECT conf_value
178FROM '.FluxBB_CONFIG_TABLE.'
179WHERE conf_name = "o_default_user_group"
180;';
181
182        $o_user_group = pwg_db_fetch_assoc(pwg_query($query));
183     
184        $query = '
185UPDATE '.FluxBB_USERS_TABLE.'
186SET group_id = '.$o_user_group['conf_value'].'
187WHERE id = '.FluxBB_Searchuser($user['id']).'
188;';
189        pwg_query($query);
190      }
191    }
192  }
193}
194
195/**
196 * Check the username accuracy in FluxBB users table
197 */
198function Register_FluxBB_RegistrationCheck($errors, $user)
199{
200  global $conf;
201 
202  // Because FluxBB is case insensitive on login name, we have to check if a similar login already exists in FluxBB's user table
203  // If "test" user already exists, "TEST" or "Test" (and so on...) can't register
204  $query = '
205SELECT username
206  FROM '.FluxBB_USERS_TABLE.'
207WHERE LOWER('.stripslashes('username').') = "'.strtolower($user['username']).'"
208;';
209
210  $count = pwg_db_num_rows(pwg_query($query));
211
212  if ($count > 0)
213  {
214    array_push($errors, l10n('this login is already used'));
215  }
216  return $errors; 
217}
218
219
220/**
221 * Users linking in a dedicated links table
222 */
223function FluxBB_Linkuser($pwg_id, $bb_id, $PwdSync)
224{
225  global $conf;
226
227  $conf_Register_FluxBB = unserialize($conf['Register_FluxBB']);
228
229  $query = '
230SELECT pwg.id as pwg_id, bb.id as bb_id
231FROM '.USERS_TABLE.' pwg, '.FluxBB_USERS_TABLE.' bb
232WHERE pwg.id = '.$pwg_id.'
233  AND bb.id = '.$bb_id.'
234  AND pwg.username = bb.username
235  AND pwg.username NOT IN ("18","16")
236;';
237
238  $data = pwg_db_fetch_row(pwg_query($query));
239
240  if (!empty($data) and !is_null($PwdSync))
241  {
242    $subquery = '
243DELETE FROM '.Register_FluxBB_ID_TABLE.'
244WHERE id_user_pwg = "'.$pwg_id.'"
245OR id_user_FluxBB = "'.$bb_id.'"
246;';
247
248    pwg_query($subquery);
249
250    $subquery = '
251INSERT INTO '.Register_FluxBB_ID_TABLE.'
252  (id_user_pwg, id_user_FluxBB, PwdSynch)
253VALUES ('.$pwg_id.', '.$bb_id.', "'.$PwdSync.'")
254;';
255
256    pwg_query($subquery);
257  }
258  else
259  {
260    $PwdSync = NULL;
261    $subquery = '
262DELETE FROM '.Register_FluxBB_ID_TABLE.'
263WHERE id_user_pwg = "'.$pwg_id.'"
264OR id_user_FluxBB = "'.$bb_id.'"
265;';
266
267    pwg_query($subquery);
268
269    $subquery = '
270INSERT INTO '.Register_FluxBB_ID_TABLE.'
271  (id_user_pwg, id_user_FluxBB, PwdSynch)
272VALUES ('.$pwg_id.', '.$bb_id.', "'.$PwdSync.'")
273;';
274
275    pwg_query($subquery);
276  }
277}
278
279
280/**
281 * Users unlinking in a dedicated links table (on user deletion)
282 */
283function FluxBB_Unlinkuser($bb_id)
284{
285  $query = '
286DELETE FROM '.Register_FluxBB_ID_TABLE.'
287WHERE id_user_FluxBB = '.$bb_id.'
288;';
289
290  pwg_query($query);
291}
292
293
294/**
295 * Add new registered user in fluxBB users table
296 * Called from Register_FluxBB_Adduser()
297 */
298function FluxBB_Adduser($pwg_id, $login, $password, $adresse_mail)
299{
300  global $errors, $conf;
301
302  $conf_Register_FluxBB = unserialize($conf['Register_FluxBB']);
303
304  $registred = time();
305  $registred_ip = $_SERVER['REMOTE_ADDR'];
306
307  // Set default FluxBB group - Check if UAM is installed and if bridge is set
308  if (function_exists('FindAvailableConfirmMailID') and isset($conf_Register_FluxBB['FLUXBB_UAM_LINK']) and $conf_Register_FluxBB['FLUXBB_UAM_LINK'] == 'true')
309  {
310    $o_default_user_group1 = $conf_Register_FluxBB['FLUXBB_GROUP'];
311  }
312  else
313  {
314    $query = '
315SELECT conf_value
316FROM '.FluxBB_CONFIG_TABLE.'
317WHERE conf_name = "o_default_user_group"
318;';
319
320    $o_default_user_group = pwg_db_fetch_assoc(pwg_query($query));
321  }
322
323// Check for timezone settings in FluxBB version 1.4.x or higher
324  $query1 = '
325SELECT conf_value
326FROM '.FluxBB_CONFIG_TABLE.'
327WHERE conf_name = "o_default_timezone"
328;';
329
330  $count1 = pwg_db_num_rows(pwg_query($query1));
331
332// Check for timezone settings in FluxBB version 1.2.x
333  $query2 = '
334SELECT conf_value
335FROM '.FluxBB_CONFIG_TABLE.'
336WHERE conf_name = "o_server_timezone"
337;';
338
339  $count2 = pwg_db_num_rows(pwg_query($query2));
340
341// Set timezone var according of FluxBB version
342  if ($count1 == 1 and $count2 == 0)
343  {
344    $o_default_timezone = pwg_db_fetch_assoc(pwg_query($query1));
345  }
346  else if ($count1 == 0 and $count2 == 1)
347  {
348    $o_default_timezone = pwg_db_fetch_assoc(pwg_query($query2));
349  }
350
351// Get FluxBB default language
352  $query = '
353SELECT conf_value
354FROM '.FluxBB_CONFIG_TABLE.'
355WHERE conf_name = "o_default_lang"
356;';
357
358  $o_default_lang = pwg_db_fetch_assoc(pwg_query($query));
359
360// Get FluxBB default style
361  $query = '
362SELECT conf_value
363FROM '.FluxBB_CONFIG_TABLE.'
364WHERE conf_name = "o_default_style"
365;';
366
367  $o_default_style = pwg_db_fetch_assoc(pwg_query($query));
368
369  // Add user - Check if UAM is installed and if bridge is set
370  if (function_exists('FindAvailableConfirmMailID') and isset($conf_Register_FluxBB['FLUXBB_UAM_LINK']) and $conf_Register_FluxBB['FLUXBB_UAM_LINK'] == 'true')
371  {
372    $query = "
373INSERT INTO ".FluxBB_USERS_TABLE." (
374  username,
375  ". ( isset($o_default_user_group1) ? 'group_id' : '' ) .",
376  password,
377  email,
378  ". ( isset($o_default_timezone['conf_value']) ? 'timezone' : '' ) .",
379  ". ( isset($o_default_lang['conf_value']) ? 'language' : '' ) .",
380  ". ( isset($o_default_style['conf_value']) ? 'style' : '' ) .",
381  registered,
382  registration_ip,
383  last_visit
384  )
385VALUES(
386  '".pwg_db_real_escape_string($login)."',
387  ". ( isset($o_default_user_group1) ? "'".$o_default_user_group1."'" : '' ) .",
388  '".$password."',
389        '".$adresse_mail."',
390  ". ( isset($o_default_timezone['conf_value']) ? "'".$o_default_timezone['conf_value']."'" : '' ) .",
391  ". ( isset($o_default_lang['conf_value']) ? "'".$o_default_lang['conf_value']."'" : '' ) .",
392  ". ( isset($o_default_style['conf_value']) ? "'".$o_default_style['conf_value']."'" : '' ) .",
393  '".$registred."',
394  '".$registred_ip."',
395  '".$registred."'
396  );";
397 
398    pwg_query($query);
399  }
400  else
401  {
402    $query = "
403INSERT INTO ".FluxBB_USERS_TABLE." (
404  username,
405  ". ( isset($o_default_user_group['conf_value']) ? 'group_id' : '' ) .",
406  password,
407  email,
408  ". ( isset($o_default_timezone['conf_value']) ? 'timezone' : '' ) .",
409  ". ( isset($o_default_lang['conf_value']) ? 'language' : '' ) .",
410  ". ( isset($o_default_style['conf_value']) ? 'style' : '' ) .",
411  registered,
412  registration_ip,
413  last_visit
414  )
415VALUES(
416  '".pwg_db_real_escape_string($login)."',
417  ". ( isset($o_default_user_group['conf_value']) ? "'".$o_default_user_group['conf_value']."'" : '' ) .",
418  '".$password."',
419        '".$adresse_mail."',
420  ". ( isset($o_default_timezone['conf_value']) ? "'".$o_default_timezone['conf_value']."'" : '' ) .",
421  ". ( isset($o_default_lang['conf_value']) ? "'".$o_default_lang['conf_value']."'" : '' ) .",
422  ". ( isset($o_default_style['conf_value']) ? "'".$o_default_style['conf_value']."'" : '' ) .",
423  '".$registred."',
424  '".$registred_ip."',
425  '".$registred."'
426  )
427;";
428    pwg_query($query);
429  }
430
431  $bb_id = pwg_db_insert_id();
432
433  FluxBB_Linkuser($pwg_id, $bb_id, "OK");
434}
435
436
437/**
438 * Update user information in FluxBB users table
439 */
440function FluxBB_Updateuser($pwg_id, $username, $password, $adresse_mail)
441{
442  global $conf;
443 
444  include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
445  $conf_Register_FluxBB = unserialize($conf['Register_FluxBB']);
446
447// Select users to update in ID link table
448  $query = '
449SELECT id_user_FluxBB as FluxBB_id
450FROM '.Register_FluxBB_ID_TABLE.'
451WHERE id_user_pwg = '.$pwg_id.'
452;';
453
454  $row = pwg_db_fetch_assoc(pwg_query($query));
455
456  if (!empty($row))
457  {
458    $query = '
459UPDATE '.FluxBB_USERS_TABLE.'
460SET username = "'.pwg_db_real_escape_string($username).'", email = "'.$adresse_mail.'", password = "'.$password.'"
461WHERE id = '.$row['FluxBB_id'].'
462AND "'.pwg_db_real_escape_string($username).'" NOT IN ("18","16")
463;';
464
465    pwg_query($query);
466
467    FluxBB_Linkuser($pwg_id, $row['FluxBB_id'], "OK");
468  }
469  else
470  {
471    $query = '
472SELECT id as FluxBB_id
473FROM '.FluxBB_USERS_TABLE.'
474WHERE BINARY username = BINARY "'.pwg_db_real_escape_string($username).'"
475;';
476
477    $row = pwg_db_fetch_assoc(pwg_query($query));
478
479    if (!empty($row))
480    {
481      $query = '
482UPDATE '.FluxBB_USERS_TABLE.'
483SET username = "'.pwg_db_real_escape_string($username).'", email = "'.$adresse_mail.'", password = "'.$password.'"
484WHERE id = '.$row['FluxBB_id'].'
485AND "'.pwg_db_real_escape_string($username).'" NOT IN ("18","16")
486;';
487
488      pwg_query($query);
489
490      FluxBB_Linkuser($pwg_id, $row['FluxBB_id'], "OK");
491    }
492  }
493}
494
495
496/**
497 * Add new registered user in fluxBB users table from audit/synch action
498 * Standard FluxBB_Adduser() function is not used because of existing password mismatch
499 * To solve password synch problem, passwords are reset to NULL to force users to get a new password on their profile page
500 */
501function Synch_FluxBB_Adduser($pwg_id, $login, $adresse_mail)
502{
503  global $errors, $conf;
504
505  $conf_Register_FluxBB = unserialize($conf['Register_FluxBB']);
506
507  $registred = time();
508  $registred_ip = $_SERVER['REMOTE_ADDR'];
509  $password = NULL;
510
511  // Set default FluxBB group - Check if UAM is installed and if bridge is set
512  if (function_exists('FindAvailableConfirmMailID') and isset($conf_Register_FluxBB['FLUXBB_UAM_LINK']) and $conf_Register_FluxBB['FLUXBB_UAM_LINK'] == 'true')
513  {
514    $o_default_user_group1 = $conf_Register_FluxBB['FLUXBB_GROUP'];
515  }
516  else
517  {
518    $query = '
519SELECT conf_value
520FROM '.FluxBB_CONFIG_TABLE.'
521WHERE conf_name = "o_default_user_group"
522;';
523
524    $o_default_user_group = pwg_db_fetch_assoc(pwg_query($query));
525  }
526
527// Check for timezone settings in FluxBB version 1.4.x or higher
528  $query1 = '
529SELECT conf_value
530FROM '.FluxBB_CONFIG_TABLE.'
531WHERE conf_name = "o_default_timezone"
532;';
533
534  $count1 = pwg_db_num_rows(pwg_query($query1));
535
536// Check for timezone settings in FluxBB version 1.2.x
537  $query2 = '
538SELECT conf_value
539FROM '.FluxBB_CONFIG_TABLE.'
540WHERE conf_name = "o_server_timezone"
541;';
542
543  $count2 = pwg_db_num_rows(pwg_query($query2));
544
545// Set timezone var according of FluxBB version
546  if ($count1 == 1 and $count2 == 0)
547  {
548    $o_default_timezone = pwg_db_fetch_assoc(pwg_query($query1));
549  }
550  else if ($count1 == 0 and $count2 == 1)
551  {
552    $o_default_timezone = pwg_db_fetch_assoc(pwg_query($query2));
553  }
554
555// Get FluxBB default language
556  $query = '
557SELECT conf_value
558FROM '.FluxBB_CONFIG_TABLE.'
559WHERE conf_name = "o_default_lang"
560;';
561
562  $o_default_lang = pwg_db_fetch_assoc(pwg_query($query));
563
564// Get FluxBB default style
565  $query = '
566SELECT conf_value
567FROM '.FluxBB_CONFIG_TABLE.'
568WHERE conf_name = "o_default_style"
569;';
570
571  $o_default_style = pwg_db_fetch_assoc(pwg_query($query));
572
573  // Add user - Check if UAM is installed and if bridge is set
574  if (function_exists('FindAvailableConfirmMailID') and isset($conf_Register_FluxBB['FLUXBB_UAM_LINK']) and $conf_Register_FluxBB['FLUXBB_UAM_LINK'] == 'true')
575  {
576    $query = "
577INSERT INTO ".FluxBB_USERS_TABLE." (
578  username,
579  ". ( isset($o_default_user_group1) ? 'group_id' : '' ) .",
580  password,
581  email,
582  ". ( isset($o_default_timezone['conf_value']) ? 'timezone' : '' ) .",
583  ". ( isset($o_default_lang['conf_value']) ? 'language' : '' ) .",
584  ". ( isset($o_default_style['conf_value']) ? 'style' : '' ) .",
585  registered,
586  registration_ip,
587  last_visit
588  )
589VALUES(
590  '".pwg_db_real_escape_string($login)."',
591  ". ( isset($o_default_user_group1) ? "'".$o_default_user_group1."'" : '' ) .",
592  '".$password."',
593        '".$adresse_mail."',
594  ". ( isset($o_default_timezone['conf_value']) ? "'".$o_default_timezone['conf_value']."'" : '' ) .",
595  ". ( isset($o_default_lang['conf_value']) ? "'".$o_default_lang['conf_value']."'" : '' ) .",
596  ". ( isset($o_default_style['conf_value']) ? "'".$o_default_style['conf_value']."'" : '' ) .",
597  '".$registred."',
598  '".$registred_ip."',
599  '".$registred."'
600  );";
601 
602    pwg_query($query);
603  }
604  else
605  {
606    $query = "
607INSERT INTO ".FluxBB_USERS_TABLE." (
608  username,
609  ". ( isset($o_default_user_group['conf_value']) ? 'group_id' : '' ) .",
610  password,
611  email,
612  ". ( isset($o_default_timezone['conf_value']) ? 'timezone' : '' ) .",
613  ". ( isset($o_default_lang['conf_value']) ? 'language' : '' ) .",
614  ". ( isset($o_default_style['conf_value']) ? 'style' : '' ) .",
615  registered,
616  registration_ip,
617  last_visit
618  )
619VALUES(
620  '".pwg_db_real_escape_string($login)."',
621  ". ( isset($o_default_user_group['conf_value']) ? "'".$o_default_user_group['conf_value']."'" : '' ) .",
622  '".$password."',
623        '".$adresse_mail."',
624  ". ( isset($o_default_timezone['conf_value']) ? "'".$o_default_timezone['conf_value']."'" : '' ) .",
625  ". ( isset($o_default_lang['conf_value']) ? "'".$o_default_lang['conf_value']."'" : '' ) .",
626  ". ( isset($o_default_style['conf_value']) ? "'".$o_default_style['conf_value']."'" : '' ) .",
627  '".$registred."',
628  '".$registred_ip."',
629  '".$registred."'
630  )
631;";
632    pwg_query($query);
633  }
634
635  $bb_id = pwg_db_insert_id();
636
637  FluxBB_Linkuser($pwg_id, $bb_id, "NOK");
638}
639
640
641/**
642 * Add new registered user in Piwigo users table from audit/synch action
643 * To solve password synch problem, passwords are reset to NULL to force users to get a new password on their profile page
644 *
645 * @return : $error
646 */
647function Synch_Piwigo_Adduser($fluxbb_id, $username, $email)
648{
649  global $conf;
650  load_language('plugin.lang', REGFLUXBB_PATH);
651
652  if (!get_userid_by_email($email) and !get_userid($username))
653  {
654    // find a password
655    $password = generate_key(8);
656
657    $error = register_user($username, $password, $email, false);
658
659    if (empty($error))
660    {
661      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
662
663      $keyargs_content = array(
664        get_l10n_args('Hello %s,', $login),
665        get_l10n_args('To synchronize your forum access with the gallery you have been registered at %s!', $conf['gallery_title']),
666        get_l10n_args('', ''),
667        get_l10n_args('Here are your connection settings', ''),
668        get_l10n_args('Username: %s', $login),
669        get_l10n_args('Password: %s', $password),
670        get_l10n_args('Email: %s', $mail_address),
671        get_l10n_args('', ''),
672        get_l10n_args('Please change your password at your first connexion on the gallery', ''),
673        get_l10n_args('', ''),
674        get_l10n_args('If you think you\'ve received this email in error, please contact us at %s', get_webmaster_mail_address()),
675      );
676
677      pwg_mail(
678        $mail_address,
679        array(
680          'subject' => '['.$conf['gallery_title'].'] '.l10n('Registration'),
681          'content' => l10n_args($keyargs_content),
682          'content_format' => 'text/plain',
683          )
684      );
685
686      $pwg_id = get_userid($username);
687
688      FluxBB_Linkuser($pwg_id, $fluxbb_id, "NOK");
689     
690      $error = false;
691    }
692  }
693  else $error = true;
694
695  return $error;
696}
697
698
699/**
700 * Update user information in FluxBB users table from audit/synch action
701 * Standard FluxBB_Updateuser() function is not used because of existing password mismatch
702 * To solve password synch problem, passwords are reset to NULL to force users to get a new password by using "forgotten password" function
703 */
704function Synch_FluxBB_Updateuser($pwg_id, $username, $adresse_mail)
705{
706  global $conf;
707 
708  include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
709  $conf_Register_FluxBB = unserialize($conf['Register_FluxBB']);
710  $password = NULL;
711
712// Select users to update in ID link table
713  $query = '
714SELECT id_user_FluxBB as FluxBB_id
715FROM '.Register_FluxBB_ID_TABLE.'
716WHERE id_user_pwg = '.$pwg_id.'
717;';
718
719  $row = pwg_db_fetch_assoc(pwg_query($query));
720
721  if (!empty($row))
722  {
723    $query = '
724UPDATE '.FluxBB_USERS_TABLE.'
725SET username = "'.pwg_db_real_escape_string($username).'", email = "'.$adresse_mail.'", password = "'.$password.'"
726WHERE id = '.$row['FluxBB_id'].'
727AND "'.pwg_db_real_escape_string($username).'" NOT IN ("18","16")
728AND "'.pwg_db_real_escape_string($username).'" <> "'.stripslashes($conf_Register_FluxBB['FLUXBB_ADMIN']).'"
729;';
730
731    pwg_query($query);
732
733    FluxBB_Linkuser($pwg_id, $row['FluxBB_id'], "NOK");
734  }
735  else
736  {
737    $query = '
738SELECT id as FluxBB_id
739FROM '.FluxBB_USERS_TABLE.'
740WHERE BINARY username = BINARY "'.pwg_db_real_escape_string($username).'"
741AND "'.pwg_db_real_escape_string($username).'" <> "'.stripslashes($conf_Register_FluxBB['FLUXBB_ADMIN']).'"
742;';
743
744    $row = pwg_db_fetch_assoc(pwg_query($query));
745
746    if (!empty($row))
747    {
748      $query = '
749UPDATE '.FluxBB_USERS_TABLE.'
750SET username = "'.pwg_db_real_escape_string($username).'", email = "'.$adresse_mail.'", password = "'.$password.'"
751WHERE id = '.$row['FluxBB_id'].'
752AND "'.pwg_db_real_escape_string($username).'" NOT IN ("18","16")
753AND "'.pwg_db_real_escape_string($username).'" <> "'.stripslashes($conf_Register_FluxBB['FLUXBB_ADMIN']).'"
754;';
755
756      pwg_query($query);
757
758      FluxBB_Linkuser($pwg_id, $row['FluxBB_id'], "NOK");
759    }
760  }
761}
762
763
764/**
765 * Reg_FluxBB_PwdSynch
766 * Checks if a password needs to be resynch between Piwigo and FluxBB
767 *
768 * @uid        : the user id
769 *
770 * @returns    : true if the password is NOT synchronized (NOK) else false
771 *
772 */
773function Reg_FluxBB_PwdSynch($uid)
774{
775  $query = '
776SELECT PwdSynch
777FROM '.Register_FluxBB_ID_TABLE.'
778WHERE id_user_pwg='.$uid.'
779;';
780
781  $result = pwg_db_fetch_assoc(pwg_query($query));
782
783  if($result['PwdSynch'] == "NOK" or is_null($result['PwdSynch']))
784  {
785    return true;
786  }
787  else return false; 
788}
789
790
791/**
792 * Search linked users
793 */
794function FluxBB_Searchuser($id_user_pwg)
795{
796  $query = '
797SELECT id_user_FluxBB, id_user_pwg
798FROM '.Register_FluxBB_ID_TABLE.'
799WHERE id_user_pwg = '.$id_user_pwg.'
800LIMIT 1
801;';
802
803  $data = pwg_db_fetch_assoc(pwg_query($query));
804 
805  if (!empty($data))
806    return $data['id_user_FluxBB'];
807  else
808    return '0'; 
809}
810
811
812/**
813 * Delete user from FluxBB users table
814 * Called from Register_FluxBB_Deluser()
815 */
816function FluxBB_Deluser($id_user_FluxBB, $SuppTopicsPosts)
817{
818  global $conf;
819
820  $conf_Register_FluxBB = unserialize($conf['Register_FluxBB']);
821
822  $query0 = '
823SELECT username, id
824FROM '.FluxBB_USERS_TABLE.'
825WHERE id = '.$id_user_FluxBB.'
826LIMIT 1
827;';
828
829  $data0 = pwg_db_fetch_assoc(pwg_query($query0));
830
831  // If True, delete related topics and posts
832  if ($SuppTopicsPosts and $conf_Register_FluxBB['FLUXBB_DEL_PT'])
833  {
834    // Delete posts and topics of this user
835    $subquery = '
836DELETE FROM '.FluxBB_POSTS_TABLE.'
837WHERE poster_id = '.$id_user_FluxBB.'
838;';
839
840    $subresult = pwg_query($subquery);
841
842    // Delete topics of this user
843    $subquery = '
844DELETE FROM '.FluxBB_TOPICS_TABLE.'
845WHERE BINARY poster = BINARY "'.pwg_db_real_escape_string($data0['username']).'"
846;';
847
848    $subresult = pwg_query($subquery);
849  }
850
851  // Delete user's subscriptions
852  $subquery = '
853DELETE FROM '.FluxBB_SUBSCRIPTIONS_TABLE.'
854WHERE user_id = '.$id_user_FluxBB.'
855;';
856
857  $subresult = pwg_query($subquery);
858 
859  // Delete user's account
860  $subquery = '
861DELETE FROM '.FluxBB_USERS_TABLE.'
862WHERE id = '.$id_user_FluxBB.'
863;';
864
865  $subresult = pwg_query($subquery);
866
867  FluxBB_Unlinkuser($id_user_FluxBB);
868}
869
870
871/**
872 * Get plugin information
873 */
874function RegFluxBB_Infos($dir)
875{
876  $path = $dir;
877
878  $plg_data = implode( '', file($path.'main.inc.php') );
879  if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
880  {
881    $plugin['name'] = trim( $val[1] );
882  }
883  if (preg_match("|Version: (.*)|", $plg_data, $val))
884  {
885    $plugin['version'] = trim($val[1]);
886  }
887  if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) )
888  {
889    $plugin['uri'] = trim($val[1]);
890  }
891  if ($desc = load_language('description.txt', $path.'/', array('return' => true)))
892  {
893    $plugin['description'] = trim($desc);
894  }
895  elseif ( preg_match("|Description: (.*)|", $plg_data, $val) )
896  {
897    $plugin['description'] = trim($val[1]);
898  }
899  if ( preg_match("|Author: (.*)|", $plg_data, $val) )
900  {
901    $plugin['author'] = trim($val[1]);
902  }
903  if ( preg_match("|Author URI: (.*)|", $plg_data, $val) )
904  {
905    $plugin['author uri'] = trim($val[1]);
906  }
907  if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid='))
908  {
909    list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']);
910    if (is_numeric($extension)) $plugin['extension'] = $extension;
911  }
912// IMPORTANT SECURITY !
913  $plugin = array_map('htmlspecialchars', $plugin);
914
915  return $plugin ;
916}
917
918
919/**
920 * Delete obsolete files at plugin update
921 */
922function regfluxbb_obsolete_files()
923{
924  if (file_exists(REGFLUXBB_PATH.'obsolete.list')
925    and $old_files = file(REGFLUXBB_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES)
926    and !empty($old_files))
927  {
928    array_push($old_files, 'obsolete.list');
929    foreach($old_files as $old_file)
930    {
931      $path = REGFLUXBB_PATH.$old_file;
932      if (is_file($path))
933      {
934        @unlink($path);
935      }
936    }
937  }
938}
939
940
941/**
942 * Function to update plugin version number in config table
943 * Used everytime a new version is updated even if no database
944 * upgrade is needed
945 */
946function RegFluxBB_version_update()
947{
948  global $conf;
949
950  // Get current plugin version
951  // --------------------------
952  $plugin =  RegFluxBB_Infos(REGFLUXBB_PATH);
953  $version = $plugin['version'];
954
955  // Upgrading options
956  // -----------------
957  $query = '
958SELECT value
959FROM '.CONFIG_TABLE.'
960WHERE param = "Register_FluxBB"
961;';
962
963  $result = pwg_query($query);
964  $Conf_RegFluxBB = pwg_db_fetch_assoc($result);
965
966  $Newconf_RegFluxBB = unserialize($Conf_RegFluxBB['value']);
967
968  $Newconf_RegFluxBB['REGFLUXBB_VERSION'] = $version;
969
970  $update_conf = serialize($Newconf_RegFluxBB);
971
972  conf_update_param('Register_FluxBB', pwg_db_real_escape_string($update_conf));
973
974
975// Check #_plugin table consistency
976// Only useful if a previous version upgrade has not worked correctly (rare case)
977// ------------------------------------------------------------------------------
978  $query = '
979SELECT version
980  FROM '.PLUGINS_TABLE.'
981WHERE id = "Register_FluxBB"
982;';
983 
984  $data = pwg_db_fetch_assoc(pwg_query($query));
985 
986  if (empty($data['version']) or $data['version'] <> $version)
987  {
988    $query = '
989UPDATE '.PLUGINS_TABLE.'
990SET version="'.$version.'"
991WHERE id = "Register_FluxBB"
992LIMIT 1
993;';
994
995    pwg_query($query);
996  }
997}
998
999
1000/**
1001 * Useful for debugging - 4 vars can be set
1002 * Output result to log.txt file
1003 *
1004 */
1005function RegFluxBBLog($var1, $var2, $var3, $var4)
1006{
1007   $fo=fopen (REGFLUXBB_PATH.'log.txt','a') ;
1008   fwrite($fo,"======================\n") ;
1009   fwrite($fo,'le ' . date('D, d M Y H:i:s') . "\r\n");
1010   fwrite($fo,$var1 ."\r\n") ;
1011   fwrite($fo,$var2 ."\r\n") ;
1012   fwrite($fo,$var3 ."\r\n") ;
1013   fwrite($fo,$var4 ."\r\n") ;
1014   fclose($fo) ;
1015}
1016?>
Note: See TracBrowser for help on using the repository browser.