File Encryption/Decryption With PHP


Often we find the need to protect certain files so that if they are ever subject to unauthorized accessed, the contents can be safe. Encrypting info in a database is pretty straightforward, however encrypting files in a directory is not always. Here are two functions to encrypt and decrypt whole files using PHP and Mcrypt.

function encrypt_file($source,$destination,$passphrase,$stream=NULL) {
	// $source can be a local file...
	if($stream) {
		$contents = $source;
	// OR $source can be a stream if the third argument ($stream flag) exists.
	}else{
		$handle = fopen($source, "rb");
		$contents = fread($handle, filesize($source));
		fclose($handle);
	}
 
	$iv = substr(md5("\x1B\x3C\x58".$passphrase, true), 0, 8);
	$key = substr(md5("\x2D\xFC\xD8".$passphrase, true) . md5("\x2D\xFC\xD9".$passphrase, true), 0, 24);
	$opts = array('iv'=>$iv, 'key'=>$key);
	$fp = fopen($destination, 'wb') or die("Could not open file for writing.");
	stream_filter_append($fp, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $opts);
	fwrite($fp, $contents) or die("Could not write to file.");
	fclose($fp);
 
}

Below is the function to perform decryption.

function decrypt_file($file,$passphrase) {
 
	$iv = substr(md5("\x1B\x3C\x58".$passphrase, true), 0, 8);
	$key = substr(md5("\x2D\xFC\xD8".$passphrase, true) .
	md5("\x2D\xFC\xD9".$passphrase, true), 0, 24);
	$opts = array('iv'=>$iv, 'key'=>$key);
	$fp = fopen($file, 'rb');
	stream_filter_append($fp, 'mdecrypt.tripledes', STREAM_FILTER_READ, $opts);
 
	return $fp;
}

Encryption Example:

encrypt_file('/path/to/source/file', '/path/to/destination/file', 'MySuperSecretPassword');

Decryption Example:

// Output to inline PDF
$decrypted = decrypt_file('/path/to/file','MySuperSecretPassword');
header('Content-type: application/pdf');
fpassthru($decrypted);
 
// Output to a string for email attachments, etc.
$decrypted = decrypt_file('/path/to/file','MySuperSecretPassword');
$contents = stream_get_contents($fp);

(Just to make sure, you need to change $passphrase into your very own password and keep it secret.)

Source: Monkey Logic

Advertisements

Dynamically using Iframe Maps with PHP GeoCoding and Google Maps API


Dynamically using Iframe Maps with PHP GeoCoding and Google Maps API

Dynamically using Iframe Maps with PHP GeoCoding and Google Maps API

In a recent project, EnlightenMental Productions needed to dynamically display a Google Map Iframe code based on the user’s current selection.  Each time an address was chosen, the <iframe> map would update to display the current address selected.

In order to do this, we needed to query the Google Maps API to get the longitude and latitude based on an address (1234 Somewhere Street City State Zipcode).  Using an article we found online (www.timshowers.com) and making a few minor adjustments, we created a basic PHP script that will generate a dynamic <iframe>.

<?php // First, setup the variables you will use on your
// Your Iframe will need a Width and Height set
// as well as the address you plan to Iframe
// Don't forget to get a Google Maps API key

$latitude = '';
$longitude = '';
$iframe_width = '400px';
$iframe_height = '400px';
$address = 'ENTER YOUR ADDRESS HERE';
$address = urlencode($address);
$key = "YOUR GOOGLE MAPS API KEY";
$url = "http://maps.google.com/maps/geo?q=".$address."&output=json&key=".$key;
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // Comment out the line below if you receive an error on certain hosts that have security restrictions
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($ch);
curl_close($ch);
$geo_json = json_decode($data, true);

// Uncomment the line below to see the full output from the API request
// var_dump($geo_json);

// If the Json request was successful (status 200) proceed
if ($geo_json['Status']['code'] == '200') {

$latitude = $geo_json['Placemark'][0]['Point']['coordinates'][0];
$longitude = $geo_json['Placemark'][0]['Point']['coordinates'][1]; ?>

<iframe width="<?php echo $iframe_width; ?>" height="<?php echo $iframe_height; ?>" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="

http://maps.google.com/maps

?f=q
&source=s_q
&hl=en
&geocode=
&q=<?php echo $address; ?>
&aq=0
&ie=UTF8
&hq=
&hnear=<?php echo $address; ?>
&t=m
&ll=<?php echo $longitude; ?>,<?php echo $latitude; ?>
&z=12
&iwloc=
&output=embed"> </ iframe >

<?php

} else { echo "<p>No Address Available</p>";}

?>

The above code will output an Iframe into your site and use the dynamic address you’ve selected.