<?php
 
 
class secure_ids {
 
    use addModules;
 
 
    private $realIDs, $abstractIDs;
 
    private $encryptSession;
 
 
 
    public function __construct($encrypted = false, $salt = '5cfqfm8ep5a3jz01e0wr9', $method = "AES-256-CBC"){
 
        if(session_status() != 2) session_start();
 
          
 
        $this->importStaticModules();
 
        if(empty($this->modules['encrypt'])){
 
            $this->addModule('encrypt',function($string) use ($salt,$method){
 
                $iv = substr(hash('sha256', $salt), 50, 66);
 
                $output = openssl_encrypt($string, $method, $salt, 0, $iv);
 
                return base64_encode($output);
 
            });
 
        }
 
 
        if(empty($this->modules['decrypt'])){
 
            $this->addModule('dncrypt',function($string) use ($salt,$method){
 
                $iv = substr(hash('sha256', $salt), 50, 66);
 
                $output = openssl_decrypt(base64_decode($string), $method, $salt, 0, $iv);
 
            });
 
        }
 
          
 
        if($encrypted == true){
 
            $this->encryptSession = true;
 
            $secureIDs = $this->decrypt($_SESSION['_secure_ids']);
 
            $secureIDs = json_decode($secureIDs, true);
 
            $this->abstractIDs = $secureIDs['AbstractIDs'];
 
 
            $this->realIDs = $secureIDs['RealIDs'];
 
        } else {
 
            $this->encryptSession = false;
 
            $this->abstractIDs = $_SESSION['_secure_ids']['AbstractIDs'];
 
 
            $this->realIDs = $_SESSION['_secure_ids']['RealIDs'];
 
        }
 
    }
 
 
 
    public function __destruct(){
 
        if($this->encryptSession == true){
 
            $secureIDs['AbstractIDs'] = $this->abstractIDs;
 
            $secureIDs['RealIDs'] = $this->realIDs;
 
            $secureIDs = json_encode($secureIDs);
 
            $_SESSION['_secure_ids'] = $this->encrypt($secureIDs);
 
        } else {
 
            $_SESSION['_secure_ids']['AbstractIDs'] = $this->abstractIDs;
 
            $_SESSION['_secure_ids']['RealIDs'] = $this->realIDs;
 
        }
 
    }
 
    
 
 
    public static function addEncryptionModule($module){
 
        self::defineStaticModuleType('encrypt','single');
 
        self::addStaticModule('encrypt', $module);
 
    }
 
    
 
    public static function addDecryptionModule($module){
 
        self::defineStaticModuleType('decrypt','single');
 
        self::addStaticModule('decrypt', $module);
 
    }
 
    
 
 
    private function encrypt($string){
 
        $data = $this->runStaticModules('encrypt',$string);
 
        return $data[0];
 
    }
 
    
 
 
    private function decrypt($string){
 
        $data = $this->runStaticModules('decrypt',$string);
 
        return $data[0];
 
    }
 
 
 
    private function checkIDStatus($id){
 
        return (strpos($id, '-') != FALSE) ? TRUE : FALSE;
 
    }
 
 
 
    private function generateUniqueID(){
 
        $nextID = base_convert(rand(60466176,2176782335),10,36);
 
        $nextID = implode("-", str_split(str_pad($nextID, 6, '0', STR_PAD_LEFT), 2));
 
 
        if(isset($this->abstractIDs[$nextID])) $nextID = $this->generateUniqueID();
 
        return $nextID;
 
    }
 
 
 
    private function store($id){
 
        $nextID = $this->generateUniqueID();
 
 
        $this->realIDs[$nextID] = $id;
 
        $this->abstractIDs[$id] = $nextID;
 
 
        return $this;
 
    }
 
 
 
    public function displayID($id){
 
        $isDisplay = $this->checkIDStatus($id);
 
 
        if($isDisplay) return $id;
 
        if(isset($this->abstractIDs[$id])) return $this->abstractIDs[$id];
 
 
        return $this->store($id)->displayID($id);
 
    }
 
 
 
    public function realID($id){
 
        $isDisplay = $this->checkIDStatus($id);
 
 
        if(!$isDisplay && isset($this->abstractIDs[$id])) return $id;
 
        if($isDisplay && isset($this->realIDs[$id])) return $this->realIDs[$id];
 
 
        return $this->store($id)->realID($id);
 
    }
 
 
} 
 
 |