Skip to content

Instantly share code, notes, and snippets.

@abbotto
abbotto / Snipplr-67836.txt
Created August 16, 2013 12:55
A function to get a files contents with curl.
function file_content($_path) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $_path);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
@abbotto
abbotto / Snipplr-67832.txt
Created August 16, 2013 12:55
A function to get file mime-types with curl.
function file_mime($_path) {
$ch = curl_init($_path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
$content_info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
$content_parts = explode(";", $content_info);
$mime = $content_parts[0];
if(isset($mime) && !empty($mime)){return $mime;}
else if (function_exists('finfo_open')) {
@abbotto
abbotto / Snipplr-67834.txt
Created August 16, 2013 12:55
A function to get file sizes in multiple formats with curl.
function file_size($_path, $_unit, $_float = null, $_username = null, $_password = null) {
$_unit = trim(strtoupper($_unit)); //Making the unit compatible
function unit_size($data, $_unit, $_float) {
if (!isset($_float)) {$_float = 3;} // float 3 decimal places by default
$sizes = array("B"=>0, "KB"=>1, "MB"=>2, "GB"=>3, "TB"=>4, "PB"=>5, "EB"=>6, "ZB"=>7, "YB"=>8); //Associated with each unit is the exponent that will be used to do the conversion from bytes
if (array_key_exists($_unit, $sizes) && $sizes[$_unit] != 0) { // If the unit is not bytes we get down to business
$number = $sizes[$_unit];
$total = $data / (pow(1024, $number));
return round($total, $_float)." ".$_unit;
}
@abbotto
abbotto / Snipplr-67837.txt
Created August 16, 2013 12:55
A function to get the real IP address.
function get_ip(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } // Check if the IP is from a shared internet connection
else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } // Check if the IP is passed from a proxy
else{ $ip = $_SERVER['REMOTE_ADDR']; } // The real IP address
return $ip;
}
@abbotto
abbotto / Snipplr-67835.txt
Created August 16, 2013 12:55
A function to see if a file exists with curl.
function file_alive($_path) {
$handle = curl_init($_path);
if (false === $handle) {return false;}
curl_setopt($handle, CURLOPT_HEADER, false);
curl_setopt($handle, CURLOPT_FAILONERROR, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox because some sites request a valid header from you
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
$alive = curl_exec($handle);
curl_close($handle);
@abbotto
abbotto / Snipplr-69038.js
Created August 16, 2013 12:55
JavaScript: Bare-Bones Selector Engine
var $ = function(selector, node) { // Bare-Bones Selector Engine
var selector, node;
selector = selector.trim();
node = node || document.body;
if (selector != null) {
return Array.prototype.slice.call(node.querySelectorAll(selector), 0);
}
}
Array.prototype.put = function(html) {
@abbotto
abbotto / Snipplr-67852.txt
Created August 16, 2013 12:55
Compress any text-based content.
function crush($_file) {
$_file = preg_replace(array("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/","!/\*[^*]*\*+([^/][^*]*\*+)*/!"), "", $_file); /* remove comments */
$_file = str_replace(array('\r\n','\r','\t','\n',' ',' ',' '), '', $_file); /* remove tabs, spaces, newlines, etc. */
/* remove spaces before/after certain characters */
$_file = preg_replace(array('(( )+\))','(\)( )+)'), ')', $_file); $_file = preg_replace(array('(( )+\()','(\(( )+)'), '(', $_file);
$_file = preg_replace(array('(( )+\})','(\}( )+)'), '}', $_file); $_file = preg_replace(array('(( )+\{)','(\{( )+)'), '{', $_file);
$_file = preg_replace(array('(( )+\:)','(\:( )+)'), ':', $_file); $_file = preg_replace(array('(( )+\;)','(\;( )+)'), ';', $_file);
$_file = preg_replace(array('(( )+\,)','(\,( )+)'), ',', $_file); return $_file;
}
@abbotto
abbotto / Snipplr-68021.txt
Created August 16, 2013 12:55
Cross-Browser CSS Hacks.
CSS HACKS
-----------------------------------------------------------------
All except IE5
selector { property/**/: value; }
All except IE5/Mac
/*\*/ selector { property:value; } /**/
IE5/Mac
@abbotto
abbotto / Snipplr-72137.py
Created August 16, 2013 12:55
Python: Display the download percentage of a file
def report(count, blockSize, totalSize):
percent = int(count*blockSize*100/totalSize)
sys.stdout.write("\r%d%%" % percent + ' complete')
sys.stdout.flush()
sys.stdout.write('\rFetching ' + name + '...\n')
urllib.urlretrieve(getFile, saveFile, reporthook=report)
sys.stdout.write("\rDownload complete, saved as %s" % (fileName) + '\n\n')
sys.stdout.flush()
@abbotto
abbotto / Snipplr-67949.txt
Created August 16, 2013 12:55
Display the source code of a webpage.
// display source code
$lines = file('http://google.com/');
foreach ($lines as $line_num => $line) {
// loop thru each line and prepend line numbers
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br>\n";
}