Skip to content

Instantly share code, notes, and snippets.

@DrARoberts
Last active May 10, 2018 02:39
Show Gist options
  • Save DrARoberts/7c285bc7d0c9d3eaae88506fadf49d16 to your computer and use it in GitHub Desktop.
Save DrARoberts/7c285bc7d0c9d3eaae88506fadf49d16 to your computer and use it in GitHub Desktop.
Simple Decryption Protector for Number Systems for MySQL Queries
/**
* getINForProtectedNumber()
*
* Protects an Number System with an alpha numeric stripper by replace the number of characters to be $protect and replaced
* with an alpha character from the english characters in capitals for IP Address, Phone Numbers, Addresses!
*
* Result for function is used in the following for matching store numbers in a mysql database
*
* $sql = "SELECT * FROM `cards` WHERE `number` IN ('" . implode("', '", $results['in']) . "')";
*
* @param string $number the number string being protected
* @param integer $protect the number of character to replace 1 - 9
* @return array
*/
function getINForProtectedNumber($number, $protect = 2) {
{
$pool = getCollapsingNumberArray(0, 1, $protect, array());
$results = array('number'=>$number);
foreach($pool as $number => $lot)
{
$key = implode("-", $lot);
$results['in'][$key] = str_replace($lot, "_", $number);
}
return $results;
}
/**
* getCollapsingNumberArray()
*
* Gets a collapsed array with all the numeric changed for an replace for a LIKE/IN with SQL for matching a protected number
*
* @param integer $depth the depth explored so far
* @param integer $level the number level explored so far
* @param integer $protect the number of character to replace 1 - 9
* @param array $results the array object that is passed in final return
* @param array $lot the number replacement lotto
* @param string $key the array object that is passed into function key indicies
* @return array
*/
function getCollapsingNumberArray($depth = 0, $level = 1, $protect = 2, $results = array(), $lot = array(), $key ='') {
{
if (!empty($key) && count($lot)>0)
$results[$key] = $lot;
if (($depth < $protect && count($lot)<$protect) && $level <= 9)
{
$depth++;
for($ilevel = $level; $ilevel <= 9; $ilevel++) {
$index = "$key" . (strlen($key)>0?"-":"") . "$ilevel";
$results = getCollapsingNumberArray($depth, ++$level, $protect, array_merge(array($ilevel), $results, $results[(strlen($key)>0?(isset($results[$key])?$results[$key]:(isset($results[$ilevel]))):array())]), $index);
$level--;
}
$depth--;
}
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment