Not logged in. · Lost password · Register
Forum: Customising UNB Modifications and plug-ins RSS
Simple example for session.prelogin and session.newuser?
Pompei2 #1
Member since Mar 2010 · 17 posts
Group memberships: Members
Show profile · Link to this post
Subject: Simple example for session.prelogin and session.newuser?
Hi,

I want that users are not able to register to my unb. Instead, they should login with the username/password they already have in my game. When doing this for the first time, their user should be created for unb with default settings (but e-mail I already know) and they should be able to edit all settings as usual.

Looks like "session.prelogin" and "session.newuser" are made exactly for that! Though the documentation is really nearly non-existent. I have no idea on how it is intended to be used! (Tried around a bit but to no success)

Would anyone mind writing me a simple example plugin that accepts the login for user "joe", password "joe" and uses "session.newuser" to for example set its email address to "joe@example.com"
jense #2
Member since Nov 2006 · 327 posts · Location: Dortmund
Group memberships: Members
Show profile · Link to this post
It won’t work: session.prelogin and session.newuser only help if the user is already logged in somewhere else on your server.

You have to modify the setuser case in the switch statement in forum.php – the hard way: no hooks available...
Alala, Alala, Gimme three wishes - CSS
Pompei2 #3
Member since Mar 2010 · 17 posts
Group memberships: Members
Show profile · Link to this post
Duh, this is some bad news :(

Sounds like you have already done this, do you have any more hints to me or might you show me some sourcecode? How hard is it?
jense #4
Member since Nov 2006 · 327 posts · Location: Dortmund
Group memberships: Members
Show profile · Link to this post
Quote by Pompei2:
Sounds like you have already done this, do you have any more hints to me or might you show me some sourcecode?
I’m sorry, I just know where to look: the relevant place in forum.php is after the comment 'Set new User' or more precisely around if ($userid = $user->FindByName($name)). The details of the user registration can be found in register.inc.php, something along the line $user->Add($name, $password, $email). The IUser class is defined in user.lib.php.
How hard is it?
Hard to say. Easy for PHP and UNB experts, I’d guess... ;)

I’d expect a few lines of code and some testing.
Alala, Alala, Gimme three wishes - CSS
jense #5
Member since Nov 2006 · 327 posts · Location: Dortmund
Group memberships: Members
Show profile · Link to this post
Take this as a start:
  1.                 // Set new User
  2.                 $name = trim($_POST['LoginName']);
  3.                 if (!isset($_POST['LoginName']))
  4.                         // ...
  5.                 elseif ($name != '')
  6.                 {
  7.                         $user = new IUser;
  8. // TODO v
  9.                         $userid = $user->FindByName($name);
  10.                         if (!$userid)
  11.                         {
  12.                                 // check name, look up password, email
  13.                                 $user->Add($name, $password, $email);
  14.                                 $userid = $user->FindByName($name);
  15.                         }
  16. // TODO ^
  17.                         if ($userid/* already done: = $user->FindByName($name)*/)
  18.                         {
  19.                                 // ...
NO guaranty that it works at all, though...
Alala, Alala, Gimme three wishes - CSS
Pompei2 #6
Member since Mar 2010 · 17 posts
Group memberships: Members
Show profile · Link to this post
Yup, I see where it's going. Thanks a bunch for your help, I think I will get the rest alone.

Still, an example for using session.prelogin and session.newuser would be really welcome as I will use that too (still try'n to figure out).
Pompei2 #7
Member since Mar 2010 · 17 posts
Group memberships: Members
Show profile · Link to this post
Hey,

Finally got it working with those hooks. Here is my sourcecode just for further reference if someone else is looking for help on this:

  1. <?php
  2. if (!defined('UNB_RUNNING')) die('Not a UNB environment in ' . basename(__FILE__));
  3.  
  4. // Define plug-in meta-data
  5. UnbPluginMeta('Login using the Arkana-FTS database');
  6. UnbPluginMeta('Lucas Beyer <pompei2@gmail.com>', 'author');
  7. UnbPluginMeta('en', 'lang');
  8. UnbPluginMeta('unb.devel.20090606', 'version');
  9.  
  10. require_once(dirname(__FILE__) . "/../../../system/Account.php.inc");
  11.  
  12. if (!UnbPluginEnabled()) return;
  13.  
  14. function UnbHookPreLogin()
  15. {
  16.     global $UNB;
  17.  
  18.     // Similar to UnbCreateSession from session.inc.php
  19.  
  20.     // Start the PHP session if none is started yet
  21.     if (!session_id())
  22.     {
  23.         $sessionname = (isset($UNB['SessionName']) ? $UNB['SessionName'] : rc('prog_id') . 'sess');
  24.  
  25.         // check session ID for invalid characters
  26.         $sid = $_COOKIE[$sessionname];
  27.         if (!preg_match('/[a-f0-9-,]{0,42}/i', $sid)) die('<b>UNB error:</b> invalid session ID');
  28.  
  29.         @ini_set('session.cookie_path', UnbGetCookiePath());
  30.  
  31.         session_name($sessionname);
  32.         session_start();
  33.  
  34.         // Internet Explorer SSL fix
  35.         @header('Pragma: ');
  36.         @header('Cache-Control: cache');
  37.     }
  38.  
  39.     // This is a call to my own method that checks if the user is already logged
  40.     // in or not.
  41.     if (!Account::singleton()->isLoggedIn())
  42.     {
  43.         $_SESSION['UnbAuthed'] = false;
  44.         return false;
  45.     }
  46.  
  47.     // Again, taken from UnbCreateSession function.
  48.     $_SESSION['UnbAuthed'] = true;
  49.     $_SESSION['UnbLoginTime'] = time();
  50.     $_SESSION['UnbAccessTime'] = time();
  51.     $_SESSION['UnbInvalidLogins'] = 0;
  52.     $_SESSION['UnbProgId'] = rc('prog_id');
  53.     $_SESSION['UnbIpAddr'] = ip2long($_SERVER['REMOTE_ADDR']);
  54.  
  55.     // First, we need to find if there is already such an account in unb.
  56.     $user = new IUser;
  57.     $_SESSION['UnbUserId'] = $user->FindByName(Account::singleton()->getUserName());
  58.  
  59.     // If there is none yet, it will be created later in UnbHookNewUser.
  60.  
  61.     return true;
  62. }
  63.  
  64. // Hook function to add the tags
  65. //
  66. function UnbHookNewUser(&$data)
  67. {
  68.     global $UNB;
  69.     echo "HIHO";
  70.     var_dump($data);
  71.     var_dump($_SESSION);
  72.  
  73.     // Collect informations for the creation of the new user.
  74.     $u_name = Account::singleton()->getUserName();
  75.     $u_pass = Account::singleton()->getPasswordHash();
  76.     $u_email = Account::singleton()->getEMail();
  77.  
  78.     $user = new IUser;
  79.     $user->Add($u_name, $u_pass, $u_email);
  80.     $user->SetValidatedEMail($u_email); // No need for validation.
  81.  
  82.     $user->SetLanguage(Account::singleton()->getLanguage());
  83.  
  84. /// \TODO: Import all the other stuff of the new user..
  85. //     $user->SetLocation("BlaBla");
  86. //     $user->SetGender('m' or 'f' or '');
  87. //     $user->SetBirthDate(day, month, year);
  88. //     $user->SetIM(icq, aim, yahoo, msn, jabber); (null = dont change.)
  89. //     $user->SetTimezone((int) new timezone: -4=UTC-0100, 0=UTC, 1=UTC+0015, 4=UTC+0100,
  90. //                        (int) respect DST);
  91.  
  92.     return true;
  93. }
  94.  
  95. // Register hook functions
  96. UnbRegisterHook('session.prelogin', 'UnbHookPreLogin');
  97. UnbRegisterHook('session.newuser', 'UnbHookNewUser');
  98.  
  99. ?>
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
This board is powered by the Unclassified NewsBoard software, 20110527-dev, © 2003-2011 by Yves Goergen
Page created in 276.7 ms (186.4 ms) · 76 database queries in 139.5 ms
Current time: 2012-02-07, 19:57:41 (UTC +01:00)