Category Archives: Code Solutions

Over the years, we have created may different solutions for common (and not-so-common) coding problems. In this section, we will share some of our favorites. Some of these were “borrowed” from other sources. But we try to always credit the original author when possible.

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:
  • 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/
  • 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

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
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
GIT_WORK_TREE=/home/accountname/public_html git checkout -f

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://

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.
		$handle = fopen($source, "rb");
		$contents = fread($handle, filesize($source));
	$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.");

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