Author Archives: Ash Daulton

Pushing PHP Output to the Browser

Because PHP is our favorite programming language, we do a lot of behind-the-scenes processing with it. Some of these processes are called from the command line, but most are triggered from the browser. When a long script is processing, it is useful to get updated messages about where it is at. Normally, the browser will just hang and spin until the entire script is done. But here is a way to send output to the browser before the process is finished. The loop and sleep command are contrived for demonstration purposes.

Please note that the header is very important here.


header( 'Content-type: text/html; charset=utf-8' );
for($i=1;$i<11;$i++) { echo "Step $i complete...
";
ob_flush();
flush();
sleep(3);
}

This is dependent on your server environment. Your results may vary.

Linked fields with datetimepicker

While using the Bootstrap DateTime Picker, it is often handy to link two fields for a date range. For example, a stop date (to) should not be before a start date (from). Here’s how…


var now = new Date();

$("#from_").datetimepicker({
format: "mm/dd/yyyy",
linkField: "from",
linkFormat: "yyyy-mm-dd",
minView: 2,
endDate: now
}).on('changeDate', function(ev){
$('#to_').datetimepicker('setStartDate', ev.date);
});

$("#to_").datetimepicker({
format: "mm/dd/yyyy",
linkField: "to",
linkFormat: "yyyy-mm-dd",
minView: 2,
endDate: now
}).on('changeDate', function(ev){
$('#from_').datetimepicker('setEndDate', ev.date);
});

This also has linked fields for submitting a date in a preferred format and today’s date for an upper limit (useful for reports).

Using cPanel to Host Your Git Repository and Deploy Automatically

Below are the variables for the examples.  Change these to suit your needs/preference.

  • The hostname is:  your.host.name
  • The SSH port is:  222
  • The cPanel account name is:  accountname
  • The local Git directory is called: localname
  • The remote Git repo will be stored in a directory called: www.git
  • shortcut  is what I will call the optional ssh shortcut.
  • ~/Sites is your local development directory (so ~/Sites/localname will be the full path to your local Git.)

 

Step 0: Create an SSH key on your local machine.

Step 1: Add your SSH public key to the cPanel account.

  • In the “Security” section click on SSH Shell Access
  • Click “Manage SSH Keys”
  • Click “Import Key”
  • Copy and paste the key into the public key text box.  (Hint: on a Mac, use this command from the Terminal to copy your public key, in this example, it is called “id_rsa”.)  pbcopy < ~/.ssh/id_rsa.pub
  • Click “Import”
  • Go back to the Manage SSH Keys page and click “Manage Authorization”.
  • Click “Authorize”.

Step 2: Set up the remote Git repository.

Log into the remote server via SSH…

ssh -p222 accountname@your.host.name

Hint:  I like to set up shortcuts in my SSH config file (~/.ssh/config).  For example, to make a shortcut named for the localname…

Host shortcut
HostName your.host.name
Port 222
User accountname

So then you can simply log in with: ssh shortcut

From the account root (above public_html), set up a directory (called www in this example) for your Git repo..

mkdir -p ~/www.git && cd $_
git init --bare

Add a command in the post-receive hook…

cat > hooks/post-receive << EOF
#!/bin/sh
GIT_WORK_TREE=/home/accountname/public_html git checkout -f
EOF

Change permissions on that same file…

chmod +x hooks/post-receive;

Step 3: Add the remote Git repo.

Now, back on the local machine move to your local repository (or create one if need be)…

cd ~/Sites/localname

Add a remote Git repo that points to your server. (i will call mine www as above, but you can call it “production” or whatever you want it need not match the repo name)…

git remote add www ssh://accountname@your.host.name:222/home/accountname/www.git/

Or if you are using the SSH config hint from above, simply…

git remote add www ssh://shortcut/home/accountname/www.git/

Push to the server…

git push www +master:refs/heads/master

To update the site in the future, just use…

git push www

…Or any Git GUI will do for the pushing.

Whole 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;
}

Decryption Example Usage:

// 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);

Adapted from Licson Lee’s Article, Encrypt files in PHP

Don’t let your domain name get taken hostage!

Who owns your domain name? You may be surprised by the answer. If you let a web designer or web developer register your domain name for you, he or she may be the legal owner. And if you end up parting ways with them, you may find yourself in a bad situation.

How can I make sure I own my domain? When registering a domain, make sure you register it yourself and you are the one paying for it. Domains are only about $10 per year, well worth the piece-of-mind you get. When you register a domain, contact and ownership information are kept on file with the registrar. The most important piece of information is the “Registrant” or “Registered Owner”. Registering a domain yourself insures that this will be you. A design or development firm could register a name on your behalf and any reputable one would make sure your name is listed as the registrar. But why risk it? Register the name yourself to be certain.

How can I see who owns my domain? You can look up the public records of a domain name here: http://who.is You should see your name and or company under Raw Registrar Data. If not, you need to contact the Registrant and get him to update this information. You may also see that there is a “Private Registration” put on the domain. This hides the registrant’s information from the public. With this privacy on, it is impossible to tell who is the owner without going to the registrar. The privacy feature usually costs more, so if you did not register the domain yourself and there is privacy in place, this can be a bad sign.

I don’t own my domain, what can I do now? The first thing you should do is contact the owner of your domain and nicely ask if they are willing to transfer the domain into your name. If you are in good standing with this person or company there is no reason they should refuse. If you are not in good standing you will need to begin a dispute with ICANN. This dispute falls under the Uniform Dispute Resolution Policy and is normally resolved by court litigation between the parties that are claiming rights to the registered domain. More information on the Uniform Dispute Resolution Policy can be found here.