Web-izing command line apps with PHP

My task was to take this perl app and port it to the web.  Here was the app being executed in a terminal:

Running the GECOS phonebook from the command line

Running the GECOS phonebook from the command line

Now I could have done the smart thing and rewrote it to output HTML instead of text.  Or I could have done what’s far more common in the real [corporate] world and use PHP to parse the text output into HTML.  Which is what I did.

Here’s the PHP code:

<?php
	$phonebook_exec = "/var/www/cgi-bin/phonebook.cgi";
	$phonebook_args = "-g lakelodge";

	$output_buffer	= array();
	$return_buffer  = 0;
	$result		= "";

	$result = exec($phonebook_exec . " " . $phonebook_args, $output_buffer, $return_buffer);
	if ($return_buffer != 0)
		die ("Exec of $phonebook_exec with arguments $phonebook_args failed because $result.\n");

	# output results
	$count = 0;
	print "<table>\n\t";
	foreach ($output_buffer as $row) {
		if (strlen($row) < 1) {
			continue;
		}

		$dh = $count == 0 ? "h" : "d";
		# tokenize row
		$row         = preg_replace("/\s\s+/", "</t$dh><t$dh>", $row);
		$matches     = array();
		$num_matches = preg_match("/(\w*)<\/td><td>(\w*)<\/td><td>(\w*).*$/", $row, $matches);
		print "<!-- DEBUG: \$num_matches = $num_matches -->\n";
		$anchor = $matches[3];
		$row    = "<a name=\"$anchor\"></a>" . $row;
		$row    = "<t$dh>" . $row . "</t$dh>";

		# print row
		if ($count == 0) {
			# header
			print "<thead>\n\t<tr>\n\t";
		}

		else if ($count == 1) {
			# skip line 1
			$count++;
			continue;
		}

		else {
			# row
			$class = $count % 2 == 0 ? "even" : "odd";
			print "<tr class=\"$class\">\n\t";
		}

		print $row . "\n</tr>\n";

		if ($count == 0) {
			# header
			print "</thead><tbody>\n";
		}

		$count++;
	}

	print "</tbody>\n</table>\n";
?>

This took about 45 minutes this morning. Here’s the result:

The End Result

The End Result

I also coded a quick ‘n dirty app for pretty-printing the contents of /etc/aliases:

The mailing list display app

The mailing list display app

It turns empty lines into <table> tags and comments into <h2> tags.  Here’s the source:

<?php
	$aliases = file_get_contents("/etc/aliases");
	#print "<!-- DEBUG: \$aliases = $aliases -->\n";
	$begin   = strpos($aliases, "# Lake Lodge lists");
	$end     = strpos($aliases, "##");
	$length  = $end - $begin + 1;
	$result  = substr($aliases, $begin, $length);
	#print "<!-- DEBUG: \$result = $result -->\n";
	
	$lines      = explode("\n", $result);
	#print "<!-- DEBUG: \$lines = $lines -->\n";
	$count      = 0;
	$open_table = false;
	foreach ($lines as $line) {
		#print "<!-- DEBUG: \$line = $line -->\n";
		# section headings
		if (preg_match("/^#/", $line)) {
			if ($open_table) {
				$open_table = false;
				print "</table>\n";
			}

			$line = substr($line, 1);
			print "<h2>$line</h2>\n";
			$count++;
			continue;
		}

		# whitespace
		if (strlen($line) < 2) {
			if ($open_table) {
				$open_table = false;
				print "</table>\n";
			}

			continue;
		}

		# row
		if (!$open_table) {
			$open_table = true;
			print "<table>\n";
		}

		$class = $count % 2 == 0 ? "even" : "odd";
		print "<tr class=\"$class\">\n";
		$line = preg_replace("/:/", "</td><td>", $line);
		$names = array();
		print "<!-- DEBUG: \$line = $line -->\n";
		preg_match("/<\/td><td>\s*((?:\w|,)+)/", $line, $names);
		print "<!-- DEBUG: \$names[1] = " . $names[1] . " -->\n";
		$snames = explode(",", $names[1]);
		$output_string = "";
		$ncount = 0;
		foreach ($snames as $name) {
			print "<!-- DEBUG: \$name = $name -->\n";
			$comma = $ncount > 0 ? "," : "";
			$output_string .= "$comma<a href=\"/lakelodge/phonebook#$name\">$name</a>";
			$ncount++;
		}

		$line = preg_replace("/<\/td><td>.*/", "</td><td>$output_string",$line);
		print "<td>" . $line . "</td>\n";
		print "</tr>\n";
		$count++;
	}

	if ($open_table) {
		print "</table>\n";
	}
?>
About these ads
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 48 other followers

%d bloggers like this: