Welcome Guest, Not a member yet? Register   Sign In
Sending a Code to an Email Address in CodeIgniter
#8

(04-22-2017, 03:29 AM)InsiteFX Wrote: Universally Unique Identifier Generator

Great for creating unique numbers for codes.


PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * ------------------------------------------------------------------------
 * Editor   : PhpStorm 2017.1
 * Date     : 3/2/2017
 * Time     : 2:36 PM
 * Authors  : Raymond L King Sr.
 * ------------------------------------------------------------------------
 * 
 * Class        UUID
 *
 * @project     starter
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2017 Custom Software Designers, LLC.
 * @license     http://www.procoversfx.com/license
 * ------------------------------------------------------------------------
 */

// version 3, 4 and 5 uuid for namespace - can be generated online
//define('Uuid_Namespace', '1fb10795-5f81-454c-b857-2bc81956b0af');

/**
 * Class UUID
 *
 * Universally Unique Identifier Generator
 *
 * USAGE:
 *
 * $str = ''; // or Uuid_Namespace;
 *
 * $this->load->library(uuid);
 *
 * echo $str.$this->uuid->guid_v4()."<br><br>";
 *
 * echo $str.$this->uuid->v3(Uuid_Namespace, md5(uniqid(mt_rand(), true)))."<br><br>";
 * xxxxxxxx-xxxx-xxxx-xxxx-Out = Xxxxxxxxxxxx
 *
 * echo $str.$this->uuid->v4()."<br><br>";
 * xxxxxxxx-xxxx-xxxx-xxxx-Out = Xxxxxxxxxxxx
 *
 * echo $str.$this->uuid->v5(Uuid_Namespace, md5(uniqid(mt_rand(), true)))."<br><br>";
 * xxxxxxxx-xxxx-xxxx-xxxx-Out = Xxxxxxxxxxxx
 */

class UUID {

 
/**
 * Class variables - public, private, protected and static.
 * --------------------------------------------------------------------
 */

 /**
 * v3 ()
 * --------------------------------------------------------------------
 *
 * Version 3 UUIDs are named based.  They require a namespace (another
 * Valid UUID) and a value (the name).  Given the same namespace and
 * Name, the output is always the same.
 *
 * @param   Uuid   $namespace
 * @param   String $name
 * @return  bool|string
 */
 
public static function v3($namespace$name)
 {
 if (! 
self::isIdValid($namespace))
 {
 return 
false;
 }

 
// Get hexadecimal components of namespace
 
$nHex str_replace(array('-''{''}'), ''$namespace);

 
// Binary Value
 
$nStr '';

 
// Convert Namespace UUID to bits
 
for ($i 0$i strlen($nHex); $i += 2)
 {
 
$nStr .= chr(hexdec($nHex[$i].$nHex[$i 1]));
 }

 
// Calculate hash value
 
$hash md5($nStr.$name);

 return 
sprintf('%08S-04S-%%%04X-%04X-12s',

 
// 32 bits for "time_low"
 
substr($hash08),

 
// 16 bits for "time_mid"
 
substr($hash84),

 
/**
 * 16 bits for "time_hi_and_version",
 * Four most significant bits holds version number 3
 */
 
(hexdec(substr($hash124)) & 0X0Fff) | 0X3000,

 
/**
 * 16 bits, 8 bits for "clk_seq_hi_res", 8 bits for "clk_seq_low",
 * Two most significant bits holds zero and one for variant DCE1.1
 */
 
(hexdec(substr($hash164)) & 0X3Fff) | 0X8000,

 
// 48 bits for "node"
 
substr($hash2012)
 );
 }

 
/**
 * v4 ()
 * --------------------------------------------------------------------
 *
 * Version 4 UUIDs are pseudo-random.
 */
 
public static function v4()
 {
 return 
sprintf ('%04X%04X%04X04X-%04X-%%%04X-04X04X-%',

 
// 32 bits for "time_low"
 
   mt_rand(00Xffff), mt_rand(00Xffff),

 
// 16 bits for "time_mid"
 
   mt_rand(00Xffff),

 
/**
 * 16 bits for "time_hi_and_version",
 * Four most significant bits holds version number 4
 */
 
   mt_rand(00X0Fff) | 0X4000,

 
/**
 * 16 bits, 8 bits for "clk_seq_hi_res", 8 bits for "clk_seq_low",
 * Two most significant bits holds zero and one for variant DCE1.1
 */
 
   mt_rand(00X3Fff) | 0X8000,

 
// 48 bits for "node"
 
   mt_rand(00Xffff), mt_rand(00Xffff), mt_rand(00Xffff)
 );
 }

 
/**
 * guid_v4 ()
 * --------------------------------------------------------------------
 *
 * @return  string
 */
 
public static function guid_v4()
 {
 if (
function_exists('com_create_guid') === true)
 {
 return 
trim(com_create_guid(), '{}');
 }

 
$data openssl_random_pseudo_bytes(16);

 
// set version to 0100
 
$data[6] = chr(ord($data[6]) & 0x0f 0x40);

 
// set bits 6-7 to 10
 
$data[8] = chr(ord($data[8]) & 0x3f 0x80);
 
 return 
vsprintf('%s%s-%s-%s-%s-%s%s%s'str_split(bin2hex($data), 4));
 }

 
/**
 * v5 ()
 * --------------------------------------------------------------------
 *
 * Version 5 UUIDs are named based.  They require a namespace (another
 * Valid UUID) and a value (the name).  Given the same namespace and
 * Name, the output is always the same.
 *
 * @param   Uuid   $namespace
 * @param   String $name
 * @return  bool|string
 */
 
public static function v5($namespace$name)
 {
 if (! 
self::isIdValid($namespace))
 {
 return 
false;
 }

 
// Get hexadecimal components of namespace
 
$nHex str_replace(array('-''{''}'), ''$namespace);

 
// Binary Value
 
   $nStr '';

 
// Convert Namespace UUID to bits
 
for ($i 0$i strlen($nHex); $i += 2)
 {
 
$nStr .= chr(hexdec($nHex[$i].$nHex[$i 1]));
 }

 
// Calculate hash value
 
$hash sha1($nStr.$name);

 return 
sprintf('%08S-04S-%%%04X-%04X-12s',

 
// 32 bits for "time_low"
 
substr($hash08),

 
// 16 bits for "time_mid"
 
substr($hash84),

 
/**
 * 16 bits for "time_hi_and_version",
 * Four most significant bits holds version number 5
 */
 
(hexdec(substr($hash124)) & 0X0Fff) | 0X5000,

 
/**
 * 16 bits, 8 bits for "clk_seq_hi_res", 8 bits for "clk_seq_low",
 * Two most significant bits holds zero and one for variant DCE1.1
 */
 
(hexdec(substr($hash164)) & 0X3Fff) | 0X8000,

 
// 48 bits for "node"
 
substr($hash2012)
 );
 }

 
/**
 * isIdValid ()
 * --------------------------------------------------------------------
 *
 * @param   $uuid
 * @return  bool
 */
 
public static function isIdValid($uuid)
 {
 return 
preg_match ('/^\{?[0-9A-f]{8}\-?[0-9A-f]{4}\-?[0-9A-f]{4}\-?'.
 
                  '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i'$uuid) === 1;
 }

  // End of UUID Class.

/**
 * ------------------------------------------------------------------------
 * Filename: UUID.php
 * Location: ./application/libraries/UUID.php
 * ------------------------------------------------------------------------
 */ 

You could slim this down by creating a helper using methods instead.


I'm grateful for your input, but I'm wanting all of the coding to be simple as possible. I've achieved that with the amendments I've made.
Reply


Messages In This Thread
RE: Sending a Code to an Email Address in CodeIgniter - by christaliise - 04-22-2017, 11:22 PM



Theme © iAndrew 2016 - Forum software by © MyBB