Advertisement
Guest User

Vanilla Forums Login Integration

a guest
Jun 6th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. <?php
  2. /*
  3.    Vanilla Forum Helper Functions
  4. */
  5.     class appForum {
  6.  
  7.         function __construct(){
  8.             define('APPLICATION', 'Vanilla');
  9.             define('APPLICATION_VERSION', '2.0.18.4');
  10.             define('DS', '/');
  11.             define('PATH_ROOT', 'forum');
  12.         }
  13.  
  14.         /*
  15.         *   Set the vanilla session as logged in with this user object.
  16.         */
  17.         public static function setLoggedIn($bLoggedIn, $cUser, $iVanillaUserID = -1) {
  18.            
  19.             ob_start();
  20.             require_once(PATH_ROOT.DS.'bootstrap.php');
  21.             ob_end_clean();
  22.            
  23.             $cAuthenticator = new Gdn_PasswordAuthenticator();
  24.            
  25.             //Get the vanilla ID:
  26.             if ($iVanillaUserID == -1) {
  27.                 $cUserAuth = appDB::query("SELECT * FROM GDN_UserAuthentication WHERE ForeignUserKey = ? LIMIT 1", array($cUser['id']));
  28.                 $iVanillaUserID = $cUserAuth['userid']; //UserID
  29.             }
  30.            
  31.             //Set cookie: persist login for a month
  32.             $cAuthenticator->SetIdentity($iVanillaUserID, true);
  33.             Gdn::Authenticator()->Trigger(Gdn_Authenticator::AUTH_SUCCESS);
  34.         }
  35.        
  36.        
  37.         /*
  38.         *   Check if user is logged in, if so return user model.
  39.         */
  40.         public static function loggedIn() {
  41.            
  42.             ob_start();
  43.             require_once(PATH_ROOT.DS.'bootstrap.php');
  44.             ob_end_clean();
  45.            
  46.             $Session = Gdn::Session();
  47.             $Authenticator = Gdn::Authenticator();
  48.             if ($Session->IsValid()) {
  49.                 return true;
  50.             } else {
  51.                 return false;
  52.             }
  53.         }
  54.        
  55.        
  56.         /*
  57.         *   Log out of the user's account:
  58.         */
  59.         public static function logout($sCookiePath = '/', $sCookieDomain = '.mydomain.com') {
  60.             $iPastTime = time() - 24 * 60 * 60;
  61.             setcookie('Vanilla', ' ', $iPastTime, $sCookiePath, $sCookieDomain);
  62.             setcookie('VanillaSessionID', ' ', $iPastTime, $sCookiePath, $sCookieDomain);
  63.             setcookie('Vanilla-Volatile', ' ', $iPastTime, $sCookiePath, $sCookieDomain);
  64.             unset($_COOKIE['Vanilla']);
  65.             unset($_COOKIE['Vanilla-Volatile']);
  66.             unset($_COOKIE['VanillaSessionID']);
  67.             session_destroy();
  68.             $_COOKIE = null;
  69.         }
  70.  
  71.  
  72.  
  73.         /*
  74.         *   Manually insert a user into the vanilla tables:
  75.         */
  76.         public static function createUser($iUserID, $sName, $sEmail, $sPassword, $sProviderKey = "my provider key here") {
  77.             define('APPLICATION', 'Vanilla');
  78.             define('APPLICATION_VERSION', '2.0.18.4');
  79.             define('DS', '/');
  80.             define('PATH_ROOT', 'forum');
  81.             ob_start();
  82.             require_once(PATH_ROOT.DS.'bootstrap.php');
  83.             ob_end_clean(); // clear any header output from vanila
  84.  
  85.             $User = new UserModel();
  86.  
  87.             $iVanillaUserID = $User->InsertForBasic(
  88.                 array('Email'=>$sEmail, 'Name'=>$sName, 'ShowEmail'=>'0', 'Gender'=>'m', 'Password'=>$sPassword, 'Preferences'=>'a:1:{s:13:"Authenticator";s:5:"proxy";}'),
  89.                 FALSE,
  90.                 array('ValidateEmail' => FALSE, 'NoConfirmEmail' => TRUE)
  91.             );
  92.  
  93.             if ($iVanillaUserID) {
  94.                 // Save the authentication - different array keys to the database...igits.
  95.                 $User->SaveAuthentication(array(
  96.                     'UniqueID' => $iUserID,
  97.                     'Provider' => $sProviderKey,
  98.                     'UserID' => $iVanillaUserID
  99.                 ));
  100.                
  101.                 //Set user logged in:
  102.                 appForum::setLoggedIn(true, null, $iVanillaUserID);
  103.             }
  104.  
  105.             //Save the vanilla ID to your own user database to call for setLogin later.
  106.             return $iVanillaUserID;
  107.         }
  108.  
  109.  
  110.  
  111.         /*
  112.         *   Vanilla User Is logged iN
  113.         */
  114.         public static function VanillaAuth() {
  115.            
  116.             ob_start();
  117.             require_once(PATH_ROOT.DS.'bootstrap.php');
  118.             ob_end_clean(); // clear any header output from vanila
  119.            
  120.             $Session = Gdn::Session();
  121.             $Authenticator = Gdn::Authenticator();
  122.             if ($Session->IsValid()) {
  123.                 return $Session->User;
  124.             } else {
  125.                 $Authenticator = Gdn::Authenticator()->GetAuthenticator('proxy');
  126.                 $Authenticator->Authenticate();
  127.             }
  128.         }
  129.  
  130.     }
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement