Skip to content

Instantly share code, notes, and snippets.

@linc
Created November 15, 2010 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save linc/700805 to your computer and use it in GitHub Desktop.
Save linc/700805 to your computer and use it in GitHub Desktop.
VanillaIdentity (standalone)
<?php
/**
* @copyright Vanilla Forums Inc.
* @license GNU GPL2
*/
/**
* Instantiating this class will store current user's ID from cookie as $this->UserID.
*/
class VanillaIdentity {
# Copy these from Vanilla config
public $CookieName = 'Vanilla';
public $CookieSalt = '';
public $CookieHashMethod = 'md5';
public $UserID = 0;
/**
* Returns the unique id assigned to the user in the database (retrieved
* from the session cookie if the cookie authenticates) or FALSE if not
* found or authentication fails.
*
* @return int
*/
public function __construct() {
if (!$this->_CheckCookie($this->CookieName)) return 0;
list($UserID, $Expiration) = $this->GetCookiePayload($this->CookieName);
if (!is_numeric($UserID) || $UserID < -2) // allow for handshake special id
$this->UserID = 0;
else
$this->UserID = $UserID;
}
public static function GetCookiePayload($CookieName) {
if (!self::CheckCookie($CookieName)) return FALSE;
$Payload = explode('|', $_COOKIE[$CookieName]);
// Get rid of check fields like HashKey, HMAC and Time
array_shift($Payload);
array_shift($Payload);
array_shift($Payload);
return $Payload;
}
protected function _CheckCookie($CookieName) {
return self::CheckCookie($CookieName);
}
public static function CheckCookie($CookieName) {
if (empty($_COOKIE[$CookieName])) {
return FALSE;
}
$CookieHashMethod = $this->CookieHashMethod;
$CookieSalt = $this->CookieSalt;
$CookieData = explode('|', $_COOKIE[$CookieName]);
if (count($CookieData) < 5) {
return FALSE;
}
list($HashKey, $CookieHash, $Time, $UserID, $Expiration) = $CookieData;
if ($Expiration < time() && $Expiration != 0) {
return FALSE;
}
$Key = self::_Hash($HashKey, $CookieHashMethod, $CookieSalt);
$GeneratedHash = self::_HashHMAC($CookieHashMethod, $HashKey, $Key);
if ($CookieHash != $GeneratedHash) {
return FALSE;
}
return TRUE;
}
/**
* Returns $this->_HashHMAC with the provided data, the default hashing method
* (md5), and the server's COOKIE.SALT string as the key.
*
* @param string $Data The data to place in the hash.
*/
protected static function _Hash($Data, $CookieHashMethod, $CookieSalt) {
return Gdn_CookieIdentity::_HashHMAC($CookieHashMethod, $Data, $CookieSalt);
}
/**
* Returns the provided data hashed with the specified method using the
* specified key.
*
* @param string $HashMethod The hashing method to use on $Data. Options are MD5 or SHA1.
* @param string $Data The data to place in the hash.
* @param string $Key The key to use when hashing the data.
*/
protected static function _HashHMAC($HashMethod, $Data, $Key) {
$PackFormats = array('md5' => 'H32', 'sha1' => 'H40');
if (!isset($PackFormats[$HashMethod]))
return false;
$PackFormat = $PackFormats[$HashMethod];
// this is the equivalent of "strlen($Key) > 64":
if (isset($Key[63]))
$Key = pack($PackFormat, $HashMethod($Key));
else
$Key = str_pad($Key, 64, chr(0));
$InnerPad = (substr($Key, 0, 64) ^ str_repeat(chr(0x36), 64));
$OuterPad = (substr($Key, 0, 64) ^ str_repeat(chr(0x5C), 64));
return $HashMethod($OuterPad . pack($PackFormat, $HashMethod($InnerPad . $Data)));
}
}
Copy link

ghost commented Mar 12, 2013

I have been searching for something like this for ages!

Unfortunately there seems to be a flaw as it says on line 59 "$this when not in object context in vanillaidentity.php on line 59". Any chance to get this working? I've tried for hours, but I'm unable to fix it :(

@daigo75
Copy link

daigo75 commented Mar 12, 2013

I forked the class and made some changes to it to suit my needs. It can be found at https://gist.github.com/daigo75/5141929.

Feel free to take any of the changes from it, if you find them useful.

Copy link

ghost commented Mar 12, 2013

Thx alot. I've corrected 2 Typos and changed it a bit to use the Vanilla Config files:

https://gist.github.com/MDMW/5147632

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment