This commit is contained in:
Paminger
2016-06-29 08:00:29 +02:00
parent 9108b9ecd4
commit b4fdd11686
3 changed files with 46 additions and 18 deletions
+34 -2
View File
@@ -290,8 +290,40 @@ class MessageLib
return $result;
}
// ------------------------------------------------------------------------
/**
* generateToken() - generates a new token for reading Messages from external
*
* @return string
*/
function generateToken($length = 64)
{
// For PHP 7 you can use random_bytes()
if(function_exists('random_bytes'))
{
$token = base64_encode(random_bytes($length));
//base64 is about 33% longer, so we need to truncate the result
return strtr(substr($token, 0, $length), '+/=', '-_,');
}
// for PHP >=5.3 and <7
if(function_exists('openssl_random_pseudo_bytes'))
{
$token = base64_encode(openssl_random_pseudo_bytes($length, $strong));
// is the token strong enough?
if($strong == true)
return strtr(substr($token, 0, $length), '+/=', '-_,');
}
//fallback to mt_rand if php < 5.3 or no openssl available
$characters = '0123456789';
$characters .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/+';
$charactersLength = strlen($characters)-1;
$token = '';
//select some random characters
for ($i = 0; $i < $length; $i++)
$token .= $characters[mt_rand(0, $charactersLength)];
return $token;
}
// ------------------------------------------------------------------------
@@ -342,4 +374,4 @@ class MessageLib
'msg' => lang('message_'.$error)
);
}
}
}