<?php
/* Author: Leon Bukhman    ****************************************/
/* Generic Functions **********************************************/

/******************************************************************
This is a library of generic functions used throughout the site.
*******************************************************************/

//get the name of the current script
function me() {
    return 
substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1,strlen($_SERVER['PHP_SELF']));
}

//connect to database and return pointer to PEAR db object
function &db_connect() {
    require_once 
'DB.php';  //pear db class
    
PEAR::setErrorHandling(PEAR_ERROR_DIE);

    
$db_host DB_HOST;
    
$db_user DB_USER;
    
$db_pass DB_PASS;
    
$db_name DB_NAME;

    
$dsn "mysql://$db_user:$db_pass@$db_host/$db_name";
    
$db DB::connect($dsn);
    
$db->setFetchMode(DB_FETCHMODE_ASSOC);
    return 
$db;
}

//encrypt plane text using a key and an mcrypt algorythm
function encrypt($pt$key$cipher='MCRYPT_BLOWFISH') {
    
$td  mcrypt_module_open(constant($cipher), ""MCRYPT_MODE_OFB"");
    
$key substr($key0mcrypt_enc_get_key_size($td));       //trucate key to length
    
$iv  mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); //create iv

    
mcrypt_generic_init($td$key$iv);                        //init encryption
    
$blob mcrypt_generic($tdmd5($pt).$pt);                  //encrypt
    
mcrypt_generic_end($td);                                    //end encryption

    
return base64_encode($iv.$blob);                            //return as text
}

//decrypt ciphered text using a key and an mcrypt algorythm
function decrypt($blob$key$cipher='MCRYPT_BLOWFISH') {
    
$blobbase64_decode($blob);                                //convert to binary
    
$td  mcrypt_module_open(constant($cipher), ""MCRYPT_MODE_OFB"");
    
$key substr($key0mcrypt_enc_get_key_size($td));       //truncate key to size
    
$iv  substr($blob0mcrypt_enc_get_iv_size($td));       //extract the IV
    
$ct  substr($blobmcrypt_enc_get_iv_size($td));          //extract cipher text
    
if (strlen($iv)<mcrypt_enc_get_iv_size($td))                //test for error
        
return FALSE;
    
mcrypt_generic_init($td$key$iv);                        //init decryption
    
$pt  mdecrypt_generic($td$ct);                          //decrypt
    
mcrypt_generic_end($td);                                    //end decryption

    
$check=substr($pt,0,32);                                    //extract md5 hash
    
$pt   =substr($pt,32);                                      //extract text

    
if ($check != md5($pt))                                     //verify against hash
      
return FALSE;
    else
      return 
$pt;
}

//set a cookie on the clients machine (expires in one year)
function sendCookie($name$value$array='LBsharp') {
    if (!
headers_sent()) {
        
$temp unserialize(stripslashes($_COOKIE[$array]));
        
$temp[$name] = $value;
        
$value serialize($temp);
        return 
setcookie($array$valuetime() + 31104000'/');
    } else {
        return 
false;
    }
}

//this will erase a cookie (all will erase all cookies)
function deleteCookie($name$array='LBsharp'$all=false) {
    if (!
headers_sent() ) {
        if (
$all)
            return 
setcookie($array''time() - 3600'/');
        else {
            
$temp unserialize(stripslashes($_COOKIE[$array]));
            unset(
$temp[$name]);
            
$value serialize($temp);
            return 
setcookie($array$valuetime() + 31104000'/');
        }
    } else {
        return 
false;
    }
}

//gets a cookie from the cookie array
function getCookie($name$array='LBsharp') {
    
$temp unserialize(stripslashes($_COOKIE[$array]));
    return 
$temp[$name];
}

?>