Go Back   vb.org Archive > vBulletin 5 Connect Discussion > vB5 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 08-22-2019, 04:10 PM
doc55 doc55 is offline
 
Join Date: Aug 2019
Posts: 32
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Custom Logout Button

I want to create a custom logout button for my vB 5.3.3.
I'm using the following code from the core/vb/user.php file but it is not working. Can someone help me troubleshoot this code, please?
Thank you

PHP Code:
//init the vBulletin system
require_once( JPATH_SITE '/forum/core/vb/vb.php' );
vB::init();

function 
processLogout() {
  global 
$vbulletin;

  
$assertor vB::getDbAssertor();
  
$session vB::getCurrentSession();
  
$userinfo $session->fetch_userinfo();
  
$timeNow vB::getRequest()->getTimeNow();
  
$options vB::getDatastore()->getValue'options' );

  if ( 
$userinfo'userid' ]AND $userinfo'userid' ] != -) {
    
// init user data manager
    
$userdata = new vB_Datamanager_UservB_DataManager_Constants::ERRTYPE_SILENT );
    
$userdata->set_existing$userinfo );
    
$userdata->set'lastactivity'$timeNow $options'cookietimeout' ] );
    
$userdata->set'lastvisit'$timeNow );
    
$userdata->save();

    if ( !
defined'VB_API' ) ) {
      
$assertor->delete'session', array( 'userid' => $userinfo'userid' ], 'apiaccesstoken' => null ) );
      
$assertor->delete'cpsession', array( 'userid' => $userinfo'userid' ] ) );
    }
  }

  
$assertor->delete'session', array( 'sessionhash' => $session->get'dbsessionhash' ) ) );

  
// Remove accesstoken from apiclient table so that a new one will be generated
  
if ( defined'VB_API' )AND VB_API === true AND $vbulletin->apiclient'apiclientid' ] ) {
    
$assertor->update(
      
'apiclient',
      array( 
'apiaccesstoken' => '''userid' => ),
      array( 
'apiclientid' => intval$vbulletin->apiclient'apiclientid' ] ) )
    );
    
$vbulletin->apiclient'apiaccesstoken' ] = '';
  }

  if ( 
$vbulletin->session->created == true AND( !defined'VB_API' )OR!VB_API ) ) {
    
// if we just created a session on this page, there's no reason not to use it
    
$newsession $vbulletin->session;
  } else {
    
// API should always create a new session here to generate a new accesstoken
    
$newsession vB_Session::getNewSessionvB::getDbAssertor(), vB::getDatastore(), vB::getConfig(), ''0''vB::getCurrentSession()->get'styleid' ) );
  }

  
$newsession->set'userid');
  
$newsession->set'loggedin');
  
$vbulletin->session = & $newsession;

  
$result = array();
  
$result'sessionhash' ] = $newsession->get'dbsessionhash' );
  
$result'apiaccesstoken' ] = $newsession->get'apiaccesstoken' );

  if ( 
defined'VB_API' )AND VB_API === true ) {
    if ( 
$_REQUEST'api_c' ] ) {
      
$assertor->update'apiclient',
        array(
          
'apiaccesstoken' => $result'apiaccesstoken' ],
          
'userid' => 0,
        ),
        array(
          
'apiclientid' => intval$_REQUEST'api_c' ] )
        )
      );
    }
  }

  
vB::getHooks()->invoke'hookProcessLogout', array(
    
'result' => & $result,
    
'userinfo' => $userinfo,
  ) );

  return 
$result;

Reply With Quote
  #2  
Old 09-02-2019, 02:39 PM
doc55 doc55 is offline
 
Join Date: Aug 2019
Posts: 32
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

What do you think about the following method to logout users?
Is it OK or does it cause any issues or security concerns?

PHP Code:
if (isset($_SERVER['HTTP_COOKIE'])) {
    
$cookies explode(';'$_SERVER['HTTP_COOKIE']);
    foreach(
$cookies as $cookie) {
        
$parts explode('='$cookie);
        
$name trim($parts[0]);
        
setcookie($name''time()-1000);
        
setcookie($name''time()-1000'/');
    }

Reply With Quote
  #3  
Old 09-02-2019, 03:01 PM
Dave Dave is offline
 
Join Date: May 2010
Posts: 2,583
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Use the official vBulletin API. /core/vb/api/user.php has a logout method.

I believe something like:
PHP Code:
$api Api_InterfaceAbstract::instance();
$api->callApi('user''logout'); 
Reply With Quote
  #4  
Old 09-03-2019, 12:14 AM
doc55 doc55 is offline
 
Join Date: Aug 2019
Posts: 32
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you Dave for your helpful reply.
Here is the final and functioning code for others, if they need one:
PHP Code:
//Init the vBulletin system
require_once('/forum/includes/vb5/autoloader.php' );
vB5_Autoloader::register('/forum' );
vB5_Frontend_Application::init'config.php' );

//Get user info
$vb_userInfo vB_Api::instance'user' )->fetchUserinfo();

//Log out URL
$logout_url '/forum/auth/logout?logouthash=' $vb_userInfo'logouthash' ];
header"Location: $logout_url); 
One question, with the above code, the user is logged out then redirected to the forum home. My forum is installed in mydomain.com/forum. How can I redirect users after logout to mydomain.com?

--------------- Added [DATE]1567529927[/DATE] at [TIME]1567529927[/TIME] ---------------

Maybe I should ask the question differently.
Is it possible to execute the as a function or in the background, instead of redirecting to it as a URL?
PHP Code:
$logout_url '/forum/auth/logout?logouthash=' $vb_userInfo'logouthash' ]; 
Reply With Quote
Благодарность от:
In Omnibus
  #5  
Old 09-04-2019, 04:09 AM
delicjous's Avatar
delicjous delicjous is offline
 
Join Date: Nov 2014
Posts: 352
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Did you try to call the API user logout?
PHP Code:
$logoutInfo $api->callApi('user''logout'$logouthash); 
Reply With Quote
  #6  
Old 09-05-2019, 12:45 AM
doc55 doc55 is offline
 
Join Date: Aug 2019
Posts: 32
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by delicjous View Post
Did you try to call the API user logout?
PHP Code:
$logoutInfo $api->callApi('user''logout'$logouthash); 
Thank you for your reply. I did try the API, but I can't make it work.
Here is my code when I use the API:
PHP Code:
//Init the vBulletin system
require_once( '/forum/includes/vb5/autoloader.php' );
vB5_Autoloader::register'/forum' );
vB5_Frontend_Application::init'config.php' );
$api Api_InterfaceAbstract::instance();

//Get user info
$vb_userInfo vB_Api::instance'user' )->fetchUserinfo();
$logouthash $vb_userInfo'logouthash'];

//Logout
$logoutInfo $api->callApi('user''logout'$logouthash); 

//Redirect
header"Location: /index.php" ); 
With this, I get the following general error from vB:
That action could not be completed. Please try again, and if this occurs again please contact the system administrator and tell them how you got this message.

Maybe you could help me find what I'm doing wrong.
Thanks again.
Reply With Quote
  #7  
Old 09-25-2019, 11:07 PM
doc55 doc55 is offline
 
Join Date: Aug 2019
Posts: 32
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Anyone could help with this script? I really appreciate anu help I can get.
Reply With Quote
  #8  
Old 09-25-2019, 11:36 PM
Dave Dave is offline
 
Join Date: May 2010
Posts: 2,583
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm pretty sure you must put define('CSRF_PROTECTION', false); on top of the file.
Reply With Quote
  #9  
Old 09-26-2019, 10:25 AM
doc55 doc55 is offline
 
Join Date: Aug 2019
Posts: 32
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Dave View Post
I'm pretty sure you must put define('CSRF_PROTECTION', false); on top of the file.
Thank you for your reply.
I tried adding this line on top:
PHP Code:
define"CSRF_PROTECTION"false ); 
I also tried this line on top:
PHP Code:
require_once( '/forum/core/vb/vb.php' );
vB::init();
define"CSRF_PROTECTION"false ); 
Both are giving me the same error message and the user is not logged out.

Here is the my final code so far:

PHP Code:
define"CSRF_PROTECTION"false );
require_once( 
'/forum/includes/vb5/autoloader.php' );
vB5_Autoloader::register(  '/forum' );
vB5_Frontend_Application::init'config.php' );
$api Api_InterfaceAbstract::instance();


//Get user info
$vb_userInfo vB_Api::instance'user' )->fetchUserinfo();
$logouthash $vb_userInfo'logouthash'];

//Logout
$logoutInfo $api->callApi('user''logout'$logouthash); 

//Redirect
header"Location: /index.php" ); 
Reply With Quote
  #10  
Old 09-26-2019, 02:37 PM
Dave Dave is offline
 
Join Date: May 2010
Posts: 2,583
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Works fine if I do
PHP Code:
$logoutInfo vB_Api::instance('user')->logout($logouthash); 
instead.
Reply With Quote
Reply


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 02:02 PM.


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.07824 seconds
  • Memory Usage 2,326KB
  • Queries Executed 11 (?)
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
  • (12)bbcode_php
  • (2)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
  • (1)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (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_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
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete