|
Regular Coder
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
|
Basic PHP Functions
This just includes some basic functions for php that can help you out greatly. THis file can be found HERE to be included with your script if wanted. It is not recommended, but it is possible to eval(file_get_contents($link)); if you would like to have the dynamically updating version, but most people would not advise that as it imposes security issues since I could easily modify it and break into your scripts (which I wouldn't, I respect your security and wish the same to me).
Text version:
PHP Code:
<?php ########################## #### Created By: Dubz #### ##########################
######################## #### This file contains #### #### a few basic php #### #### functions #### ########################
//Define BASICFUNCTIONS so users can add a check for this file and avoid calling it twice define("BASICFUNCTIONS", true);
/* * @credit 3nvisi0n * * Searches the given string and returns the inside of the left and right paremeters (case-sensitive) * * @return A string of text between $deliLeft and $deliRight */ function strbet($inputstr, $deliLeft, $deliRight) { $posLeft = strpos($inputstr, $deliLeft) + strlen($deliLeft); $posRight = strpos($inputstr, $deliRight, $posLeft); return substr($inputstr, $posLeft, $posRight - $posLeft); }
/* * @credit 3nvisi0n * * Searches the given string and returns the inside of the left and right paremeters (case-insensitive) * * @return A string of text between $deliLeft and $deliRight */ function stribet($inputstr, $deliLeft, $deliRight) { $posLeft = stripos($inputstr, $deliLeft) + strlen($deliLeft); $posRight = stripos($inputstr, $deliRight, $posLeft); return substr($inputstr, $posLeft, $posRight - $posLeft); }
/* * @credit 3nvisi0n * * Opens a webpage and grabs the sites source code (html) * * @param $includeHeaders determines if you want headers included in the results * @return A string of the source code */ function get($url, $includeHeaders = false) { $urlp = parse_url($url); $fp = fsockopen($urlp['host'], 80); $path = explode('/', $url, 4); $cp = count($path); $path = ($cp >= 4) ? $path[3] : ""; $req = "GET /$path HTTP/1.0\r\n"; $req .= "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"; $req .= "Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"; $req .= "Host: $urlp[host]\r\n"; $req .= "Accept-Language: en-us,en;q=0.5\r\n"; $req .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0\r\n"; $req .= "Connection: close\r\n\r\n"; fputs($fp, $req); stream_set_timeout($fp,4); $res = stream_get_contents($fp); fclose($fp); if($includeHeaders) return $res; $res = explode("\r\n\r\n", $res, 2); return $res[1]; }
/* * @credit 3nvisi0n * * Posts data to a website using the curl method * * @param $url The URL to post to * @param $postData The data to be posted * @return An array of strings containing the status and content (html) */ function curl_post($url, $postData) { if(!isset($timeout)) $timeout = 30; $curl = curl_init(); if($curl) { curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)'); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded")); $html = curl_exec($curl); } else { return array( "status" => "error", ); } curl_close($curl); return array( "status" => "ok", "content" => $html ); }
/* * @credit Dubz * Determines if the file is ran by command prompt * * @return A boolean regarding execution by command line */ function commandLine() { return (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])); }
/* * @credit Dubz * * Gets the IP of the user requesting the file * * @return A string containing the user's IP */ function getIP() { if(key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; else $ip = $_SERVER['REMOTE_ADDR']; $ip = trim($ip); if($ip == '::1') $ip = 'localhost'; return $ip; }
/* * @credit Dubz * * Generates a random string * * @param $length The length of the string to return * @param $lower Include lower-case characters * @param $upper Include upper-case characters * @param $numeric Include numeric characters * @return A randomly generated string or false if empty */ function generateRandom($length = 8, $lower = true, $upper = true, $numeric = true) { $length = (String)floor($length); if(!ctype_digit($length) || $length < 1) return false; $array = array(); $string = ''; if($lower) $array = array_merge($array, range('a', 'z')); if($upper) $array = array_merge($array, range('A', 'Z')); if($numeric) $array = array_merge($array, range('0', '9')); if(empty($array)) return null; while(strlen($string) < $length) { $string .= $array[array_rand($array)]; } return $string; }
/* * @credit Dubz * * Converts a numerical time() to a 'datetime' format * * @return A string with mysql 'datetime' stamp */ function mysql_datetime($time = null) { if(!$time) $time = time(); $stamp = date("Y-m-d H:i:s", $time); return $stamp; }
/* * @credit Dubz * * Opens and saves an image from a website or local location * * @param $inPath Location to get image * @param $directory Location to save image * @return Returns a booolean of if the image was saved */ function save_image($inPath, $directory = '') { //Download images from remote server $file = explode('/', $inPath); $file = array_pop($file); if(substr($directory, -1) != '/' || empty($directory)) $directory .= '/'; if(!is_dir($directory)) mkdir($directory); $outPath = $directory.$file; $in = @fopen($inPath, "rb"); if($in) { $out = fopen($outPath, "wb"); while ($chunk = fread($in, 8192)) { fwrite($out, $chunk, 8192); } fclose($in); fclose($out); return true; } else return false; }
/* * @credit Dubz * * Checks if the given string is base64 valid * * @param $base64String String to be tesed for base64 validity * @return Returns a booolean of if it is base64 valid */ function is_base64($base64String) { return (base64_encode(base64_decode($base64String)) == $base64String); }
/* * @credit Dubz * * Checks if the port is open for the ip address * * @param $ip IP adress of server checking * @param $port Port number to check if open * @return Returns a boolean of the port's status */ function checkPort($ip, $port = 80, $timeout = 2) { if(!$ip || !$port) return false; $conn = @fsockopen($ip, $port, $errno, $errstr, $timeout); if($conn) { fclose($conn); return true; } }
/* * @credit Dubz * * Turns an array into a string to be saved to a php file * Used to save an array to a php file instead of a text file, which will not show up if ran * * @param $array Array to be converted to string * @param $arrayName Name to be used for array in return * @param $spaceTabs Uses 4 spaces instead of a tab * @param $tabPos How far the array should be tabbed over * @return Returns a string of a php array in standard php format */ function array2Text($array, $arrayName = 'array', $spaceTabs = false, $startingTab = 0, $tabsOver = 0) { #Handle errors before possibly overwriting data if(!is_array($array)) { $backtrace = debug_backtrace(); die("<b>Warning</b>: ".$backtrace[0]['function']."() expects parameter 1 to be array, ".gettype($backtrace[0]['argv'][0])." given in <b>".$backtrace[0]['file']."</b> on line <b>".$backtrace[0]['line']."</b>"); } $tab = ($spaceTabs) ? ' ' : "\t"; #Start the array $text = ''; if($tabsOver == 0) { for($i = 0; $i < $startingTab; $i++) { $text .= $tab; } $text .= '$'.$arrayName.' = '; } #Declare an array as the value $text .= 'array('; #End array on the same line, no need to expand it if it's empty if(empty($array)) { #End the array if($tabsOver == 0) $text .= ');'; #Another key is after, just place a comma else $text .= '),'; } #Expand the array and include data inside else { $text .= "\n"; #Insert the array as key -> value foreach($array as $key => $value) { #Escape any apostrophes in the key $key = str_replace('\'', '\\\'', $key); for($i = 0; $i <= ($startingTab + $tabsOver); $i++) { $text .= $tab; } if(is_int($key)) $text .= "$key => "; else $text .= "'$key' => "; if(is_array($value)) $text .= array2Text($value, $arrayName, $spaceTabs, $startingTab, $tabsOver+1); elseif(is_int($value)) $text .= "$value,"; else { #Escape any apostrophes in the value $value = str_replace('\'', '\\\'', $value); $text .= "'$value',"; } $text .= "\n"; } #Trim the last comma from the array $text = substr($text, 0, -2)."\n"; #Add spacing for the closing parenthesis for($i = 0; $i < ($startingTab + $tabsOver); $i++) { $text .= $tab; } #End the array if($tabsOver == 0) $text .= ');'; #Another key is after, just place a comma else $text .= '),'; } return $text; }
/* * @credit Dubz * * Checks if the user is on a proxy network * NOTE: This is not 100% guaranteed and may return false positives if the client is running a webserver * * @return A boolean regarding if they are on a proxy */ function usingProxy() { return checkPort(getIP(), 80); } ?>
|
|