Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 General Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 03-27-2006, 11:12 AM
makaiguy's Avatar
makaiguy makaiguy is offline
 
Join Date: May 2004
Location: Aiken, SC, USA
Posts: 150
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Auto-entry in membergroup by field in profile - 3.5.4

Quick background:
In 3.0.7, I use the following code in phpinclude_start template. Every time a user logs on, it checks for our organization's member password entered into custom field5 in user's profile. The user is then added to or removed from a particular secondary usergroup depending on whether field5 matches the organization password stored in a custom $vbphrase. Membership in the secondary usergroup is used to control access to private forums and a couple of other things on the board. We do it this way because the password changes periodically and we wish all users to have to re-enter the current password into their profiles when the password is updated to prove they are current members and have the current password.
PHP Code:
// Add or remove user from secondary usergroup 12
// according to Org Pwd value in user's profile field5

if ($bbuserinfo[field5] == $vbphrase[org_member_password]) // Correct pwd in profile
{
if (
$bbuserinfo[membergroupids] == ''// if no membergroupids already set, set to 12
{
$updatefields $DB_site->query("
UPDATE user
SET membergroupids='12'
WHERE userid=
$bbuserinfo[userid]
"
);
}
elseif ((
strpos($bbuserinfo[membergroupids], '12'))=== false// else if group 12 not found add 12 to list
{
$updatefields $DB_site->query("
UPDATE user
SET membergroupids=concat_ws(',', membergroupids, '12')
WHERE userid=
$bbuserinfo[userid]
"
);
}
}
else 
// Correct pwd not in profile
{
if (
$bbuserinfo[membergroupids] == '12'// 12 only usergroup listed
// so blank it out
$updatefields $DB_site->query("
UPDATE user
SET membergroupids=''
WHERE (userid=
$bbuserinfo[userid])
"
);
}
else 
// 12 could be one of several
// remove it if it's there
$updatefields $DB_site->query("
UPDATE user
SET membergroupids=if(instr(membergroupids, ',12') >0, replace(membergroupids, ',12', ''), replace(membergroupids, '12,', ''))
WHERE (userid=
$bbuserinfo[userid]) AND (find_in_set('12', membergroupids))
"
);
}
}

The problem: This doesn't seem to work in 3.5.4. I'm guessing the problem is that the syntax needed to access the $bbuserinfo fields in the various if() statements has changed (??), and maybe the required query code itself. I can't activate 3.5.4 until I can get this working, as it is essential to the board. Can anybody suggest the needed code changes?

And as a secondary question, is there someplace I can relocate the code to that it would work instantly as the user updates his profile instead of waiting for his next login?
Reply With Quote
  #2  
Old 03-27-2006, 11:16 AM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

1. This is not a Template Modification Release
2. This Code must be ported to 3.5 code
3. phpinclude_start does not exist in 3.5, use global_start instead
Reply With Quote
  #3  
Old 03-27-2006, 11:35 AM
makaiguy's Avatar
makaiguy makaiguy is offline
 
Join Date: May 2004
Location: Aiken, SC, USA
Posts: 150
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Andreas
1. This is not a Template Modification Release
2. This Code must be ported to 3.5 code
3. phpinclude_start does not exist in 3.5, use global_start instead
I appreciate the quick response (and the quick move to the appropriate forum too) but I'm too inexperienced to understand it.

1. I don't understand what you're telling me here.
2. Yes, that's why I came here with the question.
3. I don't find a global_start template in 3.5.4, or a global_start.php in upload. My phpinclude_start template seems to have carried over into 3.5.4, but apparently is never called?
Reply With Quote
  #4  
Old 03-27-2006, 11:55 AM
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Location: Nottingham, UK
Posts: 23,748
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

global_start is a hook, you need to create a plugin, linked to that hook.

In the code, change '$DB_site' to '$vbulletin->db' and '$bbuserinfo' to '$vbulletin->userinfo'
Reply With Quote
  #5  
Old 03-27-2006, 01:17 PM
makaiguy's Avatar
makaiguy makaiguy is offline
 
Join Date: May 2004
Location: Aiken, SC, USA
Posts: 150
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Paul M
global_start is a hook, you need to create a plugin, linked to that hook.

In the code, change '$DB_site' to '$vbulletin->db' and '$bbuserinfo' to '$vbulletin->userinfo'
Very helpful, thank you very much. You gave me just enough information to enable me to find the tutorials I needed, so I'm off to give it a go ....

[Edit: Additional post follows, concatenated by this board into one post. At first I thought this was a neat feature, but since this doesn't bump the thread up to the top of the list, there no reason for anyone that has already looked at it to ever see the new info. Have deleted yesterday's addition and added todays with latest code.]

After several rounds of tweaking, here is the complete code as now entered into the php code window in plugin manager:
PHP Code:
// Add or remove user from Org Members secondary usergroup (12)
// according to Org Pwd value (filed5) in user's profile
  
global $vbulletin;
  if (
$vbulletin->userinfo[field5] == $vbphrase[org_member_password]) // Correct pwd in profile
  
{
     if (
$vbulletin->userinfo[membergroupids] == '')  // if no membergroupids already set, set to 12
     
{  
        
$updatefields $vbulletin->db->query("
            UPDATE user
            SET membergroupids='12'          
            WHERE userid=
$vbulletin->userinfo[userid]
        "
);
     }
     elseif ((
strpos($vbulletin->userinfo[membergroupids], '12'))=== false// else if group 12 not found 
                                                                            // add 12 to list
     
{  
        
$updatefields $vbulletin->db->query("
            UPDATE user
            SET membergroupids=concat_ws(',', 
$vbulletin->userinfo[membergroupids], '12')  
            WHERE userid=
$vbulletin->userinfo[userid]
        "
);
     }
  }
  else                                            
// Correct pwd NOT in profile
  
{
  if ((
strpos($vbulletin->userinfo[membergroupids], '12')) > 0)  // if listed in usergroup 12
    
{
      if (
$vbulletin->userinfo[membergroupids] == '12')          // 12 only usergroup listed
                                                                 // blank out membergroupids
      
{                                                   
        
$updatefields $vbulletin->db->query("
            UPDATE user
            SET membergroupids=''                                       
            WHERE (userid=
$vbulletin->userinfo[userid])
        "
);
      }
      else                                                      
// 12 is one of several
      
{  
        if ((
strpos($vbulletin->userinfo[membergroupids], ',12')) > 0)  // if not first in list
                                                                        // remove ,12
        
{
          
$updatefields $vbulletin->db->query("
            UPDATE user
            SET membergroupids=replace(
$vbulletin->userinfo[membergroupids], ',12', '')           
            WHERE (userid=
$vbulletin->userinfo[userid]) 
        "
);
        }
        else                                                           
// must be first in list
                                                                       // remove 12,
        
{
          
$updatefields $vbulletin->db->query("
            UPDATE user
            SET membergroupids=replace(
$vbulletin->userinfo[membergroupids], '12,', '')           
            WHERE (userid=
$vbulletin->userinfo[userid]) 
        "
);
        }
      }
    }
  } 
Hooked to global_start as suggested above by Andreas. Generates SQL error when logging in or out that stops execution. At least it doesn't seem to farge the database.

Message received in email when password matches and trying to add user to group 12:
Quote:
Invalid SQL:

UPDATE user
SET membergroupids=concat_ws(',', Array[membergroupids], '12')
WHERE userid=Array[userid];

MySQL Error : You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '[membergroupids], '12')
WHERE userid=Array[useri
Error Number : 1064
Message received in email when password doesn't match and trying to remove user from group 12:
Quote:
Invalid SQL:

UPDATE user
SET membergroupids=replace(Array[membergroupids], ',12', '')
WHERE (userid=Array[userid]);

MySQL Error : You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '[membergroupids], ',12', '')
WHERE (use
Error Number : 1064
So it appears the password check, at least, is working. Looks to my novice, untrained eye that it is trying to pass the entire [membergroups] and [userid] arrays instead of the specific value for the user.

What am I missing? PHP and/or SQL syntax error I can't see? Need to declare something global? Need additional code at the beginning of the routine to initialize and populate the user info fields (but the password check DOES seemingly work so the user info fields must be available)?

[Edited to add:] Removing the 'global $vbulletin;' line eliminates the SQL error, but the code does not carry through and modify the usergroup. Could this be because all the $vbulletin->whatever values are nul?
Reply With Quote
  #6  
Old 03-28-2006, 03:03 PM
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Location: Nottingham, UK
Posts: 23,748
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP won't read/process the bold bit properly (as it's written).

Code:
        $updatefields = $vbulletin->db->query("
            UPDATE user
            SET membergroupids='12'          
            WHERE userid=$vbulletin->userinfo[userid]
        ");
Either split the variable out as a seperate string and join it, like this ;

PHP Code:
        $updatefields $vbulletin->db->query("
            UPDATE user
            SET membergroupids='12'          
            WHERE userid="
.$vbulletin->userinfo['userid'] ); 
or use a seperate simple variable, like this ;

PHP Code:
        $userid $vbulletin->userinfo['userid'];
.
.

        
$updatefields $vbulletin->db->query("
            UPDATE user
            SET membergroupids='12'          
            WHERE userid=
$userid
        "
); 
Reply With Quote
  #7  
Old 03-30-2006, 01:57 AM
makaiguy's Avatar
makaiguy makaiguy is offline
 
Join Date: May 2004
Location: Aiken, SC, USA
Posts: 150
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks Paul. I've got the userid working now, apparently, via a variable.

But I think I've got problems with correctly passing the membergroups data. Before messing around with all the logic of when I do what, I thought I'd best get the queries down pat. So here's a basic test case I'm working with right now:
PHP Code:
  global $vbulletin;
  
$userid $vbulletin->userinfo['userid']; 
     
  if (
$vbulletin->userinfo['field5'] == $vbphrase['tug_member_password'])
  {
  
// ### PASSWORD IN PROFILE, SET MGROUPS TO 12 ###
     
$updatefields $vbulletin->db->query("
          UPDATE user
          SET membergroupids='12'  
          WHERE userid=
$userid
     "
);
  }
  else    
  
// ### PASSWORD IN PROFILE, SET MGROUPS TO '' ###
  
{
     
$updatefields $vbulletin->db->query("
          UPDATE user
          SET membergroupids=''  
          WHERE userid=
$userid
     "
);
  } 
This seems to work, most of the time, although if I make some changes then change back to the exact same code sometimes it doesn't work. Haven't got that figured out at all ... very frustrating (??) But anyhow, when it works, the password comparison logic works and group 12 gets turned on and off as it should. It also works with SET membergroupids='9,12' as I'd need if he was already in group 9 and we wanted to add 12 to it.

Now, if I do this instead:
PHP Code:
.
.
     
$new_mg '12'
     
$updatefields $vbulletin->db->query("
          UPDATE user
          SET membergroupids=
$new_mg  
          WHERE userid=
$userid
     "
); 
that seems to work too.

But if I have $new_mg = '' or $new_mg = '9,12' I get a database error:
Quote:
Invalid SQL:

UPDATE user
SET membergroupids=
WHERE userid=0;

MySQL Error : You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE userid=0' at line 3
Note my userid is lost too. I suspect I've got some sort of basic data type problem I don't understand (strings vs numbers vs arrays?)
Reply With Quote
  #8  
Old 03-30-2006, 06:46 AM
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Location: Nottingham, UK
Posts: 23,748
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

membergroupids is a string, so you would need this ;

PHP Code:
.
.
     
$new_mg '12'
     
$updatefields $vbulletin->db->query("
          UPDATE user
          SET membergroupids='
$new_mg'  
          WHERE userid=
$userid
     "
); 
Reply With Quote
  #9  
Old 03-31-2006, 01:53 AM
makaiguy's Avatar
makaiguy makaiguy is offline
 
Join Date: May 2004
Location: Aiken, SC, USA
Posts: 150
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks again, Paul. Your latest hint got me a lot further. Finally I just created a separate display page with my routine in it. This allowed me to insert a bunch of print() functions to check variables and progress and really get this thing debugged. Seems to be working like a champ.

Here's the final code, in case it will be of use to anybody else (please see questions and concerns below):
PHP Code:
// Add or remove user from secondary usergroup
// according to Pwd value in user's profile

  
global $vbulletin;

  
// #### SET THESE THREE STRINGS TO THE PROPER VALUES FOR YOUR SYSTEM ####
  
$org_mg '12';                        // membergroup # as string
  
$org_pwfield 'field5';               // custom field# in profile as string
  
$org_pwphrase 'org_member_password'// vbphrase name containing pwd

  // other string variables 
  
$org_mglist = (''.$vbulletin->userinfo['membergroupids']); // current membergroups string
  
$org_userid = ($vbulletin->userinfo['userid']);         // userid of user

  
$org_mg_pos = (strpos($org_mglist$org_mg));         // find target membergroup position
                                                        // in group list - returns false
                                                        // if not found

  // boolean variables 
  
$org_grp    = ($org_mg_pos !== false);                // User is in target group (true/false)
  
  
$org_haspwd = ($vbulletin->userinfo[$org_pwfield] == $vbphrase[$org_pwphrase]); // User 
                                                        // has correct password in profile (T/F)

  
if ($org_haspwd != $org_grp)      // Need to update membergroups?
  
{
     if (!
$org_grp)                 // If not currently in target group
                                    // add to group
     
{
        
        if (
$org_mglist != '')
        {
           
$org_comma ',';
        }      
        
$org_newmg = ($org_mglist $org_comma $org_mg);
     }
     else                            
// else currently in target group
                                     // and must remove  
     
{
        
// ********* potential problem section *************
        
$org_srch = ($org_mg ',');            // search string to remove = mg#,             
        
$org_newmg str_replace($org_srch,'',$org_mglist);  
        
$org_mglist $org_newmg;           
        
        
$org_srch = (',' $org_mg);            // search string to remove = ,mg#             
        
$org_newmg str_replace($org_srch,'',$org_mglist);  

        if ((
strpos($org_newmg$org_mg)) !== false)
        { 
           
$org_newmg '';                    
        }
        
// ******** end potential problem section **********
     
}
     
$updatefields $vbulletin->db->query("
            UPDATE user
            SET membergroupids='
$org_newmg'                             
            WHERE userid=
$org_userid
     "
);
     
  } 
Couple of concerns/questions:

Had to frog around with the str_replace() function quite a bit. Couldn't get it to work recursively on itself as
PHP Code:
$a str_replace($b,$c,$a)); 
Finally had to bring in a temp string to hold results:
PHP Code:
$d str_replace($b,$c,$a));
$a $d
Second concern is the method used to remove target group from the list. (See portion of code labeled as 'potential problem section'. ) Happens to work okay for me because my target group is 2-digit group 12. So nulling out '12,' and ',12' works.

But if my target were a single digit number, say 3, nulling out '3,' and ',3' via str_replace() could mess up some two-digit group numbers:
'3,12,23,31' would become '12,21'

.. or even I could have problems if, heaven forbid, we ended up with three digit membergroup numbers.

So for MY use for now, this will work, but as a generic routine for others, it's still lacking.

Can anybody suggest a way of dealing with removing the target group that would solve this problem? (Maybe by working with the group numbers as an array?)
Reply With Quote
  #10  
Old 04-01-2006, 02:20 AM
makaiguy's Avatar
makaiguy makaiguy is offline
 
Join Date: May 2004
Location: Aiken, SC, USA
Posts: 150
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Have rewritten to deal with membergroup numbers comprised of any number of digits. The key was, indeed, to deal with them as an array.

Tested with 3.5.4 - performance with other flavors unknown.

To use you'll need:
1) A custom field in your user profile, into which the user can place the required password.
2) A custom $vbphrase in which to store the required password value

Enter the appropriate values for your forum into the first three variables in the script below, then install the script via vB 3.5's Plug-in Manager. Tie to the 'global_start' hook. At each login, the script will check the user's profile for the membergroup password, then add or remove the user from the membergroup as appropriate. Note that this does not act instantaneously upon entry of the password in the profile, but requires a new login for this to take effect.

PHP Code:
// Add or remove user from secondary usergroup
// according to Pwd value in user's profile

  
global $vbulletin;

  
// Enter values for your forum in the 3 strings below
  
$org_mg 'XX';                      // target membergroup # as string
  
$org_pwfield 'fieldX';           // custom field# in profile as string
  
$org_pwphrase 'org_member_password'// vbphrase name containing pwd

  // You don't need to touch anything below here

  
$org_userid = ($vbulletin->userinfo['userid']);            // userid of user
  
$org_mglist = (''.$vbulletin->userinfo['membergroupids']); // current membergroups string
  
  
$org_mgarr explode(',' $org_mglist);  // convert membergroups to array

  
$org_inmg = (in_array($org_mg$org_mgarr));      // user in target membergroup (true/false)
  
  
$org_haspwd = ($vbulletin->userinfo[$org_pwfield] == $vbphrase[$org_pwphrase]); // User 
                                                    // has correct password in profile (T/F)

  
$org_doupdate false;
  
  if (
$org_haspwd && !$org_ingrp)   // Has password, not in membergroup
  
{
        if (
$org_mglist != '')      // if mglist already populated
        
{
           
$org_comma ',';        // we'll need a comma
        
}      
        
$org_newmg = ($org_mglist $org_comma $org_mg);  // add group to string
        
$org_mglist $org_newmg
        
$org_doupdate true;       // set toggle to update database
  
}
  else                            
  {
    if (!
$org_haspwd && $org_ingrp)   // No password, but is in membergroup    
    
{
      foreach (
$org_arr as $value)         // go thru array
      
{
          if (
$value == $org_mg)           // find target membergroup
          
{
             
$org_arr[$iii] = '';          // set to null
          
}
          
$iii++;
      }
      
$org_newmg implode(',' $arr);  // convert array to string  
      
$org_mglist str_replace(',,' ','$org_newmg);  // remove double comma
      
$org_doupdate true;              // set toggle to update database
    
}
  }  
  if (
$org_doupdate)
  {
     
$updatefields $vbulletin->db->query("
            UPDATE user
            SET membergroupids='
$org_mglist'                             
            WHERE userid=
$org_userid
     "
);
  } 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:38 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04375 seconds
  • Memory Usage 2,393KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (11)bbcode_php
  • (5)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete