Skip to content

Instantly share code, notes, and snippets.

@milesrout
Created June 27, 2016 09:54
Show Gist options
  • Save milesrout/f8dc7670d3ac4235df9d6f0743307002 to your computer and use it in GitHub Desktop.
Save milesrout/f8dc7670d3ac4235df9d6f0743307002 to your computer and use it in GitHub Desktop.
DON'T USE THIS I WROTE IT WHEN I WAS A CHILD AND AM STILL EMBARRASSED BUT IT'S MOSTLY PHP'S FAULT FOR BEING AWFUL
<?php
function dbConnect($type, $SQLdata) {
$sqlPassData = array('error' => true);
if($type == 'query') {
$sqlPassData = array( 'user' => $SQLdata['queryUsername'],
'pwd' => $SQLdata['queryPassword']);
} elseif ($type == 'admin') {
$sqlPassData = array( 'user' => $SQLdata['adminUsername'],
'pwd' => $SQLdata['adminPassword']);
}
if(array_key_exists('error', $sqlPassData)) {
return false;
} else {
// $conn = mysql_connect('localhost', $sqlPassData['user'], $sqlPassData['pwd']) or die ('Cannot connect to MySQL server');
// mysql_select_db($SQLdata['database']) or die ('Cannot open database');
$conn = new mysqli('localhost', $sqlPassData['user'], $sqlPassData['pwd'], $SQLdata['database']) or die ('Cannot open database');
return $conn;
}
}
function doSQL($statement, $verbose = false) {
if($verbose) echo "SQL: '$statement'\n";
global $SQLConnection;
$result = $SQLConnection->query($statement) or die(mysqli_error($SQLConnection));
return $result;
}
function fastQuery($statement, $verbose = false) {
if($verbose) echo "SQL: '$statement'";
global $SQLConnection;
$result = $SQLConnection->query($statement) or die(mysqli_error($SQLConnection));
$resultArray = $result->fetch_assoc();
if(count($resultArray) == 1 && $resultArray) {
return implode('', $resultArray);
} else {
return $resultArray;
}
}
function dEcho(&$value) {
if(!isset($value)) {
echo 'Not set';
} elseif (is_array($value)) {
echo "<code>";
print_r($value);
echo "</code>";
} elseif ($value === false) {
echo 'false';
} elseif ($value === true) {
echo 'true';
} else {
echo $value;
}
}
function getAsString($skip = array(), $convertChars = true) {
if(!isset($_GET) || $_GET == array()) {
$getString = '';
} else {
$getString = '?';
foreach($_GET as $key => $value) {
if(!isset($skip[$key])) {
$getString .= $key.'='.$value.'&';
}
}
$getString = substr($getString, 0, strlen($getString) - 1);
}
if($convertChars) {
$getString = str_replace('&', '&amp;', $getString);
}
return $getString;
}
function getIP() {
if (isset($_SERVER) and !empty($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
} else {
if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} else if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
} else {
$ip = getenv('REMOTE_ADDR');
}
}
return $ip;
}
function isIncluded($file) {
// convert backslashes
$fileLocation = str_replace("\\", "/", $file);
$currentURL = $_SERVER['SCRIPT_FILENAME'];
if($fileLocation == $currentURL) {
return false;
} else {
return true;
}
}
function isSafeInteger(&$value, $min = NULL, $max = NULL) {
if(isset($value) && is_numeric($value) && ($min == NULL || $value >= $min) && ($max == NULL || $value <= $max)) {
return true;
} else {
return false;
}
}
function nukeMagicQuotes() {
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value) {
$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}
}
function safeVar(&$value, $preextra = '', $postextra = '', $alternate = NULL) {
if(isset($value)) {
return $preextra.$value.$postextra;
} else {
return $alternate;
}
}
function validateTags($text, $allowedTags = NULL) {
// If comma seperated
if(!is_array($allowedTags) && $allowedTags != NULL) {
$allowedTags = explode(',', $allowedTags);
}
if($allowedTags == NULL) {
// Find all tags
$regEx = '=<(/?[^>]*)>=';
} else {
// Find specified tags
$regEx = '=<(/?(';
$regEx .= implode('|', $allowedTags);
$regEx .= '))>=i';
}
// Run the regEx
preg_match_all($regEx, $text, $matches);
// Use only ${1}
$matches = $matches[1];
// Initialize variables
$foundTags = array();
$i = 0;
$valid = true;
// Loop through each match
foreach($matches as $tag) {
if(substr($tag, 0, 1) == '/') {
// Closing tag found
if($i == 0) {
// First tag is closing
$valid = false;
break;
} else {
if($tag == '/'.$foundTags[$i - 1]) {
// Tag closed, remove it
array_pop($foundTags);
$i--;
} else {
// Wrong closing tag
$valid = false;
break;
}
}
} else {
// Opening tag found
$foundTags[$i] = $tag;
$i++;
}
}
// Check that all tags were closed
if(!$foundTags == array()) {
$valid = false;
}
// Return the result
return $valid;
}
function wordFilter($text, $filters) {
foreach($filters as $badword => $goodword) {
$text = preg_replace($badword, $goodword, $text);
}
return $text;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment