Blog

MightyText

I send a lot of text messages, but when Iā€™m at home Iā€™m often working at my computer rather than being attached to my phone. Today I downloadedĀ MightyTextĀ from theĀ Google Play storeĀ (itā€™s free), and I love it!

Thereā€™s a chrome extension available which pops up a little message in the corner of the screen whenever you receive an SMS, and you can click it to reply. You can also initiate a new message by clicking the appā€™s icon at the top of the chrome window.

This wouldnā€™t normally be enough to prompt me to write a blog post, except for one thing – the pop up appears on my computer two or three secondsĀ beforeĀ my phone makes the text alert noise. How is that even possible?

Blog

Publishing Using GIT

Git, for those unaware, is a version control system. It tracks changes that you make to files and lets you review history, revert back to older versions, and do all sorts of useful stuff. It comes into its own when you work with groups of developers – you can each work on different parts of a project, and then very cleanly merge your changes together into an authoritative master version of the codebase.

Even if, like me, you’re the only one working on your code it’s still a great tool. So far I haven’t had to revert back to an old version of my code, but it’s nice to know I could if I wanted to. What I primarily use it for however is (in my opinion) way more useful than its stated purpose.

Publishing Websites Using Git

I have a webserver within my home network. It’s open to the world and I could host my little teeny-tiny web empire on it probably quite nicely. But commercial webhosts offer much better uptime than I might get from using my home internet connection, and certainly much better download speeds for end users like you. So I don’t. I have a VPS that hosts the “production” versions of my web work, and I use my home server as a “development” environment.

What I use git for is publishing from the development to the production server. If you have a similar setup using a local xAMP stack for development then you can probably replicate my setup quite nicely to your advantage. It makes publishing to the web a one-step process, and it uses SSH so the publishing is done over a secure connection.

The prerequisites:

  • Webserver with shell access and git installed
  • Local machine (development server, in my case) with terminal access and git installed
  • Passwordless SSH setup between the local machine and webserver (read this)
  • xAMP or a similar environment on the local machine

Strictly speaking that last point isn’t a requirement, but this setup only really comes into its own if you can test code locally and then only publish it once you’re happy with the results.

The Server

We’ll take care of the server side of things first. Create a directory outside your webserver root that will hold the git meta-data. On my server the web root is at /home/account/domain/. I think this is atypical, so your set-up may be different.

mkdir /home/account/domain.git
cd /home/account/domain.git
git init --bare

Next we need to tell git where to actually put the files:

git config core.worktree /home/account/domain
git config core.bare false
git config receive.denycurrentbranch ignore

Finally, we need to tell git what to do for us when new files are pushed to the server’s git repository. Create a file called hooks/post-receive

vi hooks/post-receive

In the file:

#!/bin/sh
git checkout -f

This file needs to be executable.

chmod +x hooks/post-receive

The Local Machine

Navigate to the directory where the website code lives.

git init
git add .
git commit -m 'Initial Commit'

Next, link the repository on the webserver:

git remote add origin ssh://[email protected]/home/account/domain.git

Finally, “push” all the content from the local machine to the server:

git push origin master

Done! Depending on the size of your codebase and speed of your connection it may take a few moments the first time, but since git is smart enough to only push content that’s changed, it will be quicker next time.

From now on, whenever you’ve made some changes on the development machine that are ready to be published to production, run:

git add .
git commit -a -m ā€˜Change Descriptionā€™
git push

I put these three lines into a little bash script, and I can publish using a little web control panel I made using PHP (relying on PHP’s exec function). Also in my control panel I have the ability to push and pull databases from the server thanks to this page and the comments on it. Simple!

Blog

PHP Unzip

Because my free webhost places a limit on the number of files that can be uploaded using FTP, I needed to come up with a way to upload an archive and then unzip it on the server. Even if weren’t for the limit, this is still a potentially great idea – when uploading FTP opens a connection to the server forĀ every file it uploads, so uploading an archive file of 25mb is significantly faster than uploading a thousand smaller files that total 25mb for example.

After some googling, I found some code that makes use of PHP’s built in zip file compatability to allow me to upload a zipped archive and along with a single PHP file. My tool first unzips the archive into a folder corresponding to the archive name, then deletes the original archive file and finally deletes itself.

I didn’t write the code below, merely adapted it for my own purposes. Unfortunately I don’t remember where I found it so I’m unable to credit the original author.

The code is below and it’s also available for quick and easy download via the link at the top right of this page. There are more detailed usage instructions at the bottom of the page.

<?php
	if (!isset($_GET['src'])) echo "Usage: unzip.php?src=<em><strong>file.zip</strong></em>";
	else {
		unzipnew($_GET['src']);
		unlink("unzip.php");
	}

	function unzipnew($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true, $delzip=true) {
		$filecounter = 0;
		echo "<p>Writing files (a count of files written will be at the bottom the page)...</p>";
		if ($zip = zip_open($src_file)) {
			if ($zip) {
				$splitter = ($create_zip_name_dir === true) ? "." : "/";
				if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
				create_dirs($dest_dir);
				while ($zip_entry = zip_read($zip)) {
					$pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
					if ($pos_last_slash !== false) {
						create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
					}
					if (zip_entry_open($zip,$zip_entry,"r")) {
						$file_name = $dest_dir.zip_entry_name($zip_entry);
						if ($overwrite === true || $overwrite === false && !is_file($file_name)) {
							$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
							@file_put_contents($file_name, $fstream );
							chmod($file_name, 0777);
							echo "File written: ".$file_name."<br />";
							$filecounter++;
						}
						zip_entry_close($zip_entry);
					}
				}
				zip_close($zip);
				if($delzip) unlink($src_file);
				echo "<p>".$filecounter." files written.</p>";
			}
		} else {
			return false;
		}
		return true;
	}

	function create_dirs($path) {
		if (!is_dir($path)) {
			$directory_path = "";
			$directories = explode("/",$path);
			array_pop($directories);
			foreach($directories as $directory) {
				$directory_path .= $directory."/";
				if (!is_dir($directory_path)) {
					mkdir($directory_path);
					@chmod($directory_path, 0777);
				}
			}
		}
	}
?>

Usage instructions:

  1. Save the file as unzip.php (or download it using the link at the top right of this page) and upload it along with the zipped archive you’d like to extract to a folder of your choice.
    For the purposes of an example, let’s say you’re uploading it to the root of your website, foo.com, and the archive you have is named stuff.zip
  2. Once it’s uploaded, open a web-browser and navigate to the unzip.php file, adding the archive filename as a an attribute in the URL named src.
    E.g., foo.com/unzip.php?src=stuff.zip
  3. The files within stuff.zip will be extracted to foo.com/stuff, and any directory structure that existed within the stuff.zip archive is retained.
  4. Both the original archive file, stuff.zip, and the tool itself, unzip.php, are deleted at the end of the process.

Enjoy!