PHP Classes

File: cl.UniqID.php

Recommend this page to a friend!
  Classes of Till Wehowski   PHP UniqID Generator   cl.UniqID.php   Download  
File: cl.UniqID.php
Role: Class source
Content type: text/plain
Description: class file
Class: PHP UniqID Generator
Generate unique identifiers with random values
Author: By
Last change:
Date: 7 years ago
Size: 4,857 bytes
 

Contents

Class file image Download
<?php
/**
* UniqID - advanced uniqid
* by Till Wehowski (webfan.de)
* (c) Do What The Fuck You Want To Public License (ipConvert: //http://php.net/manual/de/function.ip2long.php)
*
*
* Example:
*
<?php
 $pfx = (isset($_GET['pfx'])) ? strip_tags($_GET['pfx']) : '';
 $suffix = (isset($_GET['suffix'])) ? strip_tags($_GET['suffix']) : '';
 $delimiter = (isset($_GET['delimiter'])) ? strip_tags($_GET['delimiter']) : false;
 echo UniqID::gen($pfx, $suffix, $delimiter);
?>?pfx=pre&suffix=-suff&delimiter=-
   
      results in=>e.g.:
     
    pre-18e860-0av54a-f9oy0kowxl-c2q5ytoc6ij306-suff
    
   
*
*/
class UniqID
{
   
 const
T = 1460655712;
 public
$id = null;
 protected
$pfx;
 protected
$suffix;
 protected
$delimiter;
 protected
$max;
 protected
$pad;

 public function
__construct($pfx='', $suffix = ''/* '.suffix' */, $delimiter = false, $max = 99999999, $pad = '0'){
     
$this->pfx =$pfx;
     
$this->suffix= $suffix;
     
$this->delimiter =$delimiter;
     
$this->max= $max;
     
$this->pad = $pad;
    
$this->id = $this->uniqid();
 }


 public static function
gen($pfx='', $suffix = ''/* '.suffix' */, $delimiter = false, $max = 99999999, $pad = '0'){
    
$s = new self($pfx, $suffix, $delimiter, $max, $pad);
     return
$s->id;
 }


 protected function
ipConvert($ipAddress ='', $basis = 36, $strPad = false) {
    if(
''===$ipAddress)$ipAddress=getEnv("REMOTE_ADDR");
   
   
$hex = '';
    if(
strpos($ipAddress, ',') !== false) {
       
$splitIp = explode(',', $ipAddress);
       
$ipAddress = trim($splitIp[0]);
    }
   
$isIpV6 = false;
   
$isIpV4 = false;
    if(
filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
       
$isIpV6 = true;
    }
    else if(
filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
       
$isIpV4 = true;
    }
    if(!
$isIpV4 && !$isIpV6) {
        return
false;
    }
   
// IPv4 format
   
if($isIpV4) {
       
$parts = explode('.', $ipAddress);
        for(
$i = 0; $i < 4; $i++) {
           
$parts[$i] = str_pad(dechex($parts[$i]), 2, '0', STR_PAD_LEFT);
        }
       
$ipAddress = '::'.$parts[0].$parts[1].':'.$parts[2].$parts[3];
       
$hex = join('', $parts);
    }
   
// IPv6 format
   
else {
       
$parts = explode(':', $ipAddress);
       
// If this is mixed IPv6/IPv4, convert end to IPv6 value
       
if(filter_var($parts[count($parts) - 1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
           
$partsV4 = explode('.', $parts[count($parts) - 1]);
            for(
$i = 0; $i < 4; $i++) {
               
$partsV4[$i] = str_pad(dechex($partsV4[$i]), 2, '0', STR_PAD_LEFT);
            }
           
$parts[count($parts) - 1] = $partsV4[0].$partsV4[1];
           
$parts[] = $partsV4[2].$partsV4[3];
        }
       
$numMissing = 8 - count($parts);
       
$expandedParts = array();
       
$expansionDone = false;
        foreach(
$parts as $part) {
            if(!
$expansionDone && $part == '') {
                for(
$i = 0; $i <= $numMissing; $i++) {
                   
$expandedParts[] = '0000';
                }
               
$expansionDone = true;
            }
            else {
               
$expandedParts[] = $part;
            }
        }
        foreach(
$expandedParts as &$part) {
           
$part = str_pad($part, 4, '0', STR_PAD_LEFT);
        }
       
$ipAddress = join(':', $expandedParts);
       
$hex = join('', $expandedParts);
    }
   
// Validate the final IP
   
if(!filter_var($ipAddress, FILTER_VALIDATE_IP)) {
        return
false;
    }


   
   
$hex = base_convert($hex, 16, $basis);
   
    if(
false===$strPad){
       return
$hex.str_pad(strlen($hex), 2, '0', STR_PAD_LEFT);
    }else{
        return
strtolower(str_pad($hex, 32, '0', STR_PAD_LEFT));
    }
  
 }


 protected function
uniqid() {
    
$pfx = $this->pfx;
    
$suffix = $this->suffix;
    
$delimiter = $this->delimiter;
    
$max = $this->max;
    
$pad = $this->pad;
    if(
true===$delimiter){$delimiter='-';}
    elseif(
false===$delimiter){$delimiter='';}
    elseif(
is_string($delimiter)){$delimiter=$delimiter;}
    else{
$delimiter='';}
   
   
$s = uniqid('', true);
   
$hex = substr($s, 0, 13);
   
$dec = $s[13] . substr($s, 15);
   
mt_srand((double)microtime() * 1000000);
   
$rand = mt_rand(1,$max);
   return
$pfx
         
.((''!==$pfx) ? $delimiter : '')
          .
str_pad(base_convert($rand, 10, 36), strlen((string)base_convert($max, 10, 36)), $pad, STR_PAD_LEFT)
          .
$delimiter
         
.str_pad( base_convert(time()-self::T, 10, 36), 6, $pad, STR_PAD_LEFT)
          .
$delimiter
         
.base_convert($hex, 16, 36)
          .
$delimiter
         
. base_convert($dec, 10, 36)
          .
$this->ipConvert()
          .
$suffix
       
;
       
}
   
   
   
   
}