Jan 212014
 

I just recently remodified an install of awstats to detect ios and iphone, and while I was at it, I reapplied my changes to detect our xymon server as well.

Following the instructions here: http://cornempire.net/2009/08/31/adding-a-robot-to-awstats/ and here: http://wiki.cornempire.net/awstats/awstatsrobots still work. I just made a small tweak to match our new version of xymon, and also to make the detection more broad, which should future proof it a bit: http://wiki.cornempire.net/awstats/awstatsrobots#updatejan_21_2014

As a side note, if you are trying to detect ios for iPhones and iPads, this tutorial is useful: http://serverfault.com/questions/248692/analyze-http-logs-looking-for-ios except for one thing.

I received an error about a mismatch in the number of entries in the @OSSearchIDOrder and %OSHashID. This was because more recent versions of awstats includes basic ios detection, and because the footprint of the new code has the same ios label as the stock code, it would throw this error. Commenting out the built in ios detection did the trick for me. I may try to expand upon the ios section a bit and try to detect version numbers in a future version. I may also create a patch that breaks out iOS and Android devices to a new family called tablets, or mobile devices….maybe.

Dec 082013
 

I have updated my Hesk hack based on a request from a reader. You can view the new instructions or download the new files here: http://wiki.cornempire.net/hesk/hesk252

For those unaware, my previous post on this matter was here: http://cornempire.net/2010/07/31/hesk-enhancements-for-version-2-2/

2.5 introduced a lot of changes. One of the big ones that affected my hack was the ability to directly link to a category type. This is now built into the application, so I modified my hack to take advantage of this.

If you have any questions, let me know.

Jun 212013
 

The Goal

Have all syslogs from all servers shipped to a central server so that we can query them in one spot, and review old logs in the event of a compromise using only free software.

After looking at a number of options, I settled on rsyslog for the server, standard syslog on the clients (for now), LogAnalyzer as the UI for the web, both mysql backend for rsyslog and file based backend. The configuration for rsyslog will be a little different then most of the tutorials out there. I wanted to be able to query across all servers at one time, and LogAnalyzer only allows you to configure specific endpoints. With most configuration examples you will find on the web, they show you how to either dump it all into a database (which will no doubt get huge if you don’t clean it up) or dump each server to a single file which rotates daily, which is not ideal for a LogAnalyzer end point because your config has to change daily. This solution will dump all events into a database for all servers which will be configured as one realtime endpoint. rsyslog will also be configured to dump to a file, and this file will be rotated monthly using the usual logrotate scripts. Several archival (non-compressed) files will also be configured in LogAnalyzer for historical purposes.

Install rsyslog

I’m not going to go into this in detail. I’m going to assume you can already do this. But here are some useful notes:

  • My solution was built on RHEL5 using the packages available from yum. You will need the rsyslog package, as well as apache, php, and mysql for the database. I used link 2 below for this, although I didn’t remove any of the previous syslog packages. I just turned them off. Additionally, you only need to install rsyslog on your central server for this solution, not on each client.
  • I used this template line at the end of my rsyslog config: $template DailyPerHostLogs,”/var/log/LOGHOSTS/%HOSTNAME%/%HOSTNAME%.log”

Configure DB

I’m going to assume you have already installed the DB, but if not, here is a cheat sheet:

  • yum install mysql-server

Run this command (or create the database in another way):

  • mysql -uroot -p < /usr/share/doc/rsyslog-mysql-3.22.1/createDB.sql

Then create a user and password that can access the database from the host the server is on.

Configure rsyslog to log to both

Now this is where the magic happens. Provided you took some part of a sample config from somewhere, you should have something like this is your rsyslog:

$ModLoad imuxsock.so
$ModLoad imklog.so
$ModLoad imudp.so
#load the network stuff
$UDPServerAddress 0.0.0.0
$UDPServerRun 514
#reduce any duplicates
$RepeatedMsgReduction on
# The template that wil format the message as it is writen to the file
# you can edit this line if you want to customize te message format
$template TraditionalFormat,"%timegenerated% %HOSTNAME% %syslogtag%%msg:::drop-last-lf%\n"
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
#*.info;mail.none;authpriv.none;cron.none                /var/log/messages
if \
        $source == 'YOUR.CENTRAL.SERVER' \
        and \
               $syslogseverity <= '6' \
        and ( \
                        $syslogfacility-text != 'mail' \
                and \
                        $syslogfacility-text != 'authpriv' \
                and \
                        $syslogfacility-text != 'cron' \
        ) \
then   /var/log/messages;TraditionalFormat
#authpriv.* /var/log/secure;TraditionalFormat
# The authpriv file has restricted access.
#authpriv.*                                              /var/log/secure
if \
        $source == 'YOUR.CENTRAL.SERVER' \
                and \
        $syslogfacility-text == 'authpriv' \
then    /var/log/secure;TraditionalFormat
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
# mail.* /var/log/maillog;TraditionalFormat
if \
        $source == 'YOUR.CENTRAL.SERVER' \
                and \
        $syslogfacility-text == 'mail' \
then    /var/log/maillog;TraditionalFormat
 
# Log cron stuff
#cron.* /var/log/cron;TraditionalFormat
if \
        $source == 'YOUR.CENTRAL.SERVER' \
                and \
        $syslogfacility-text == 'cron' \
then    /var/log/cron;TraditionalFormat
# Everybody gets emergency messages
#*.emerg *
if \
        $source == 'YOUR.CENTRAL.SERVER' \
                and \
        $syslogseverity-text == 'emerg' \
then    *
# this line creates a template that will store the messages for each host in a seperate file.
# The same file will always be used, and should be rotated using a Log Rotate script.
$template DailyPerHostLogs,"/var/log/LOGHOSTS/%HOSTNAME%/%HOSTNAME%.log"
*.* -?DailyPerHostLogs;TraditionalFormat

What we need to add for the mysql logging is:

$ModLoad ommysql.so
*.*  :ommysql:localhost OR IP OF MYSQL SERVER,Syslog,USERNAME,PASSWORD

I added the above code to the very end of the file. Then I restarted rsyslog.

Then I configured a new entry in my config file for LogAnaylzer. If you haven’t already installed it yet, you can use the install wizard to set this up. But if you have installed it, you will need to use the config.php file to make changes. The sample included in the comments seemed a little off, so this is what I used:

$CFG['Sources']['Source2']['ID'] = "Source2";
$CFG['Sources']['Source2']['Name'] = "All Servers Realtime";
$CFG['Sources']['Source2']['Description'] = "";
$CFG['Sources']['Source2']['SourceType'] = SOURCE_DB;
$CFG['Sources']['Source2']['MsgParserList'] = "";
$CFG['Sources']['Source2']['DBTableType'] = "winsyslog";
$CFG['Sources']['Source2']['DBType'] = DB_MYSQL;
$CFG['Sources']['Source2']['DBServer'] = "localhost OR YOUR DB HOST";
$CFG['Sources']['Source2']['DBName'] = "Syslog";
$CFG['Sources']['Source2']['DBUser'] = "YOUR DB USERNAME";
$CFG['Sources']['Source2']['DBPassword'] = "YOUR DB PASSWORD";
$CFG['Sources']['Source2']['DBTableName'] = "SystemEvents";
$CFG['Sources']['Source2']['ViewID'] = "SYSLOG";

You can optionally configure a mysql database to store the configuration settings and manage user accounts. I eventually did this for our install, and it did make configuration much simpler.

Log Rotate and mysql Purge Script

It is a good idea to rotate the logs regularly and update the database to purge older entries.

Place this file in /etc/logrotate.d/ and call it rsyslog:

# Rotate each log monthly
/var/log/LOGHOSTS/*/*.log {
    monthly
    sharedscripts
    nocompress
#    extension .1
    rotate 4
    create 644 root root
    postrotate
         restart rsyslog >/dev/null 2>&1 || true
    endscript
}
#/var/log/LOGHOSTS/*/*.log.1 {
#    monthly
#    extension .2
#}
#/var/log/LOGHOSTS/*/*.log.2 {
#    monthly
#    extension .3
#}
/var/log/LOGHOSTS/*/*.log.3 {
    rotate 8
    monthly
    delaycompress
    maxage 365
}

EDIT: My log rotate script didn’t work as expected. Try this instead. Another option could be to reverse the order of the files above, handle .3 first, then .2…etc It seemed to work ok for the first month, then started added extra .# extensions and messed things up a bit.

To purge the database and keep your logs clean, just set up this script to run as a cronjob:

#!/bin/sh
# Special account to run this query only.
#MYSQL queries
RECORDSTODELETE=`mysql -u{MYSQL USERNAME} -p{MYSQL PASSWORD} --silent -e "SELECT COUNT(*) FROM Syslog.SystemEvents WHERE Syslog.SystemEvents.ReceivedAt &lt; DATE_SUB(NOW(), INTERVAL 30 DAY);&quot;`
RESULTS=`mysql -u{MYSQL USERNAME} -p{MYSQL PASSWORD} --silent -e &quot;use Syslog; DELETE from Syslog.SystemEvents WHERE Syslog.SystemEvents.ReceivedAt &lt; DATE_SUB(NOW(), INTERVAL 30 day);&quot;`
`logger -i Purged ${RECORDSTODELETE} records from logging database`

Odds and Ends

If you got this all to work, you may notice that your database based sources show more information than your file based sources. If you want facility and severity to display for text based entries, you will need to setup rsyslog to include this information in the file.

First, I’m using version 3.22.1 of rsyslog from the RHEL5 repos. Which is very old. I used this template to change what is logged to the file:

$template TraditionalFormatWithPRI,"%syslogfacility%.%syslogpriority%: %timegenerated% %HOSTNAME% %syslogtag%%msg:::drop-last-lf%\n"

I also added a new database template that separates the process ID from the syslog tag:

$template SQLWithProcessID,"insert into SystemEvents (Message, Facility, FromHost, Priority, DeviceReportedTime, ReceivedAt, InfoUnitID, SysLogTag, ProcessID) values ('%msg%', %syslogfacility%, '%HOSTNAME%', %syslogpriority%, '%timereported:::date-mysql%', '%timegenerated:::date-mysql%', %iut%, '%syslogtag:R,ERE,1,FIELD:([a-zA-Z\/]+)(\[[0-9]{1,5}\])*:--end%', '%syslogtag:R,ERE,1,BLANK:\[([0-9]{1,5})\]--end%')",sql

and then make a slight change to our database logging line:

*.*  :ommysql:localhost OR IP OF MYSQL SERVER,Syslog,USERNAME,PASSWORD;SQLWithProcessID

But you then have to change LogAnalyzer in order to read the extra data.

In the $APPROOT$/classes/logstreamlineparsersyslog.class.php your first four entries in the if block should look like this:

                // Standard syslog with process and facility/severity. TGH Apr. 4, 2013
                if ( preg_match("/(.*)\.(.*): (...)(?:.|..)([0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([a-zA-Z0-9_\-\.]{1,256}) ([A-Za-z0-9_\-\/\.]{1,32})\[(.*?)\]:(.*?)$/", $szLine, $out ) )
                {
                        // Copy parsed properties!
                        $arrArguments[SYSLOG_FACILITY] = strtoupper($out[1]);
                        $arrArguments[SYSLOG_SEVERITY] = strtoupper($out[2]);
                        $arrArguments[SYSLOG_DATE] = GetEventTime($out[3] . " " . $out[4]);
                        $arrArguments[SYSLOG_HOST] = $out[5];
                        $arrArguments[SYSLOG_SYSLOGTAG] = $out[6];
                        $arrArguments[SYSLOG_PROCESSID] = $out[7];
                        $arrArguments[SYSLOG_MESSAGE] = $out[8];
                }
                // Standard syslog with process.
                elseif ( preg_match("/(...)(?:.|..)([0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([a-zA-Z0-9_\-\.]{1,256}) ([A-Za-z0-9_\-\/\.]{1,32})\[(.*?)\]:(.*?)$/", $szLine, $out ) )
                {
                        // Copy parsed properties!
                        $arrArguments[SYSLOG_DATE] = GetEventTime($out[1] . " " . $out[2]);
                        $arrArguments[SYSLOG_HOST] = $out[3];
                        $arrArguments[SYSLOG_SYSLOGTAG] = $out[4];
                        $arrArguments[SYSLOG_PROCESSID] = $out[5];
                        $arrArguments[SYSLOG_MESSAGE] = $out[6];
                }
                // Sample (Syslog): syslog with facility.priority and no process - TGH Apr. 4, 2013
                else if ( preg_match("/(.*)\.(.*): (...)(?:.|..)([0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([a-zA-Z0-9_\-\.]{1,256}) ([A-Za-z0-9_\-\/\.]{1,32}):(.*?)$/", $szLine, $out ) )
                {
                        // Copy parsed properties!
                        $arrArguments[SYSLOG_FACILITY] = strtoupper($out[1]);
                        $arrArguments[SYSLOG_SEVERITY] = strtoupper($out[2]);
                        $arrArguments[SYSLOG_DATE] = GetEventTime($out[3] . " " . $out[4]);
                        $arrArguments[SYSLOG_HOST] = $out[5];
                        $arrArguments[SYSLOG_SYSLOGTAG] = $out[6];
                        $arrArguments[SYSLOG_MESSAGE] = $out[7];
                }

                // Sample (Syslog): Mar 10 14:45:39 debandre syslogd 1.4.1#18: restart
                else if ( preg_match("/(...)(?:.|..)([0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([a-zA-Z0-9_\-\.]{1,256}) ([A-Za-z0-9_\-\/\.]{1,32}):(.*?)$/", $szLine, $out ) )
                {
                        // Copy parsed properties!
                        $arrArguments[SYSLOG_DATE] = GetEventTime($out[1] . " " . $out[2]);
                        $arrArguments[SYSLOG_HOST] = $out[3];
                        $arrArguments[SYSLOG_SYSLOGTAG] = $out[4];
                        $arrArguments[SYSLOG_MESSAGE] = $out[5];
                }

Once you put it all together, you should have solid log viewer for all of your servers at once, as well as access to historical logs.

Tutorials

Here are some helpful tutorials that I found useful:

  1. Install docs for LogAnalyzer: http://loganalyzer.adiscon.com/doc/install.html
  2. RHEL Install and config of rsyslog: http://www.howtoforge.com/building-a-central-loghost-on-centos-and-rhel-5-with-rsyslog
  3. rsyslog and mysql: http://www.dawnstar.id.au/linux-documentation/configuring-rsyslog-log-mysql-rhel-52/
Feb 262012
 

One of my long time goals has been to be able to easily display tournament brackets on the web. It seems to be a difficult problem to solve with lots of questions out there, and few answers. There are a few solutions, but they have various issues and downfalls.

I decided to give it another try myself. And this is what I have come up with: A bracket builder, a saved file with raw data, and a bracket viewer that can read the files. If all that sounds like too much, the bracket builder can just spit out the HTML code to display the tree.

I’ve outlined how it works, uploaded an example and have provided a download so you can integrate it with your own applications.

Jan 152012
 

This is the third part of a three part series on how to embed a Google Calendar into a web page and use it to accept online bookings/appointments from other online users.

The Series:

  1. Part 1: Setting up Google Calendar
  2. Part 2: OAuth2 and Configuring Your ‘Application’ With Google
  3. Part 3: A Sample Web Page For Bookings <- You Are Here

Recap

So, I’ve gone through adding a calendar to your webpage to display to your users, and then creating your application with Google so that you can do queries against your calendar. Now we get to the fun part, writing the application! I should mention that this is written in PHP, because I don’t know if I mentioned that specifically yet.

The Plan

The plan is to create a PHP script that will display a list of calendars people can book items on, and then allow them to book off time on one of these calendars. We will also include some restrictions, like not booking over other events, not booking in the past, only allowing bookings so far into the future, and not allowing bookings during ‘closed’ times. You can add more restrictions as you see fit.

In my example, the script is hosted behind some password protection and user accounts. Depending on the nature of your application, that may or may not be required. For this example, I’m going to assume it is an open system.

The API

Google provides a series of classes that you can use to interface with their API. However, I could not figure out how to use them with refresh tokens. If you intend to use a lot of the functions of the API, it might be worth while for you to get it working. However, for me, it was easier to make raw calls to the API since I’m only using 2 of the API functions. If you want to download all of the Google code for PHP, you can find the directions here.

Getting Started

The Google API is based on REST principles, and uses JSON to encode data. To know exactly what to send to the API, I played around with the Google API explorer. To make a long story short, I’ll include the code I used for my calls.

The Functions Needed

As we are not using the Google code, we will need to create some functions to do our bidding. We need a function to manage our authentication tokens, to submit post requests (to add events), to submit get requests (to check for previous bookings), and to craft our JSON request for the POST.

Auth Function

function getAccessToken(){
	$tokenURL = 'https://accounts.google.com/o/oauth2/token';
	$postData = array(
		'client_secret'=>'############',
		'grant_type'=>'refresh_token',
		'refresh_token'=>'###############',
		'client_id'=>'#############.apps.googleusercontent.com'
	);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $tokenURL);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	$tokenReturn = curl_exec($ch);
	$token = json_decode($tokenReturn);
	//var_dump($tokenReturn);
	$accessToken = $token->access_token;
	return $accessToken;
}

You will need to add your values for client_id, refresh_token and client_secret that you established in Part 2 of this tutorial. This function is pretty stand alone. The only thing you might want to add to your finished product is better token management. I generate a new token every time. It would be best to check the token before regenerating, as tokens tend to be good for 3600 seconds.

Format JSON For Post

function createPostArgsJSON($date,$starttime,$endtime,$title){
	$arg_list = func_get_args();
	foreach($arg_list as $key => $arg){
		$arg_list[$key] = urlencode($arg);
	}
	$postargs = <<<JSON
{
 "start": {
  "dateTime": "{$date}T{$starttime}:00.000-03:30"
 },
 "end": {
  "dateTime": "{$date}T{$endtime}:00.000-03:30"
 },
 "summary": "$title",
 "description": "$title"
}
JSON;
	return $postargs;
}

This function creates the body of the our post request. What you include depends on what you are storing in the event. I only need the start and end time, and a title for the event. Check out the API explorer to see what you can do.

The date is YYYY-MM-DD format, and times are passed to the function in HH:mm format. I then pad the string with the seconds and timezone.

Odds and Ends

The rest of my functions depend on these odds and ends I set up before I start doing stuff, so I’ll cover them here.

$thecal = 'court1';
if(isset($_GET['cal'])){
	$thecal = addslashes($_GET['cal']);
}

/*
 * Advance is the amount of time in the future someone can book something.
 * days
 * weeks
 * months
 * If it is 0, it will allow unlimited future booking
 */
$courts = array(
	'court1' => array('cid' => 'court1', 'name' => 'Court 1', 'id' => '###########@group.calendar.google.com', 'starttime' => '08:00:00', 'endtime' => '23:00:00', 'advance' => '1 week'),
	'court2' => array('cid' => 'court2', 'name' => 'Court 2', 'id' => '###########@group.calendar.google.com', 'starttime' => '08:00:00', 'endtime' => '23:00:00', 'advance' => '1 week'),
);

$APIKEY = '###################';

This script expects a URL parameter to tell it which calendar to load. I first set it if nothing is specified. Then I set up the calendars. I’m using this for tennis court bookings, hence my naming conventions. The keys here are:

  • The array index/cid – This is the url parameter you will pass to the page to load the correct calendar.
  • name – The nice name you will display to users.
  • id – This is your calendar id from Google that you set up in Part 1.
  • starttime/endtime – The limits in booking times specified in HH:mm:ss
  • advance – This is the amount of time in the future that you allow bookings. PHPs strtotime syntax is used for defining the future time.

The API key is the key from Part 2.

Function For Get Requests

function sendGetRequest($token,$request){
	global $APIKEY;
	
	$session = curl_init($request);
	
	// Tell curl to use HTTP POST
	curl_setopt ($session, CURLOPT_HTTPGET, true); 
	// Tell curl not to return headers, but do return the response
	curl_setopt($session, CURLOPT_HEADER, false); 
	curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($session, CURLINFO_HEADER_OUT, false);
	curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization:  Bearer ' . $token,'X-JavaScript-User-Agent:  Mount Pearl Tennis Club Bookings'));
    
	$response = curl_exec($session);
	
	curl_close($session); 
	return $response;
}

This function preforms a get request. You need to pass into it the request you want to make, as well as a valid auth token to complete the request. The function returns the response from the server.

Function For Post Requests

function sendPostRequest($postargs,$token, $cal){
	global $APIKEY;
	$request = 'https://www.googleapis.com/calendar/v3/calendars/' . $cal . '/events?pp=1&key=' . $APIKEY;
	
	$session = curl_init($request);
	
	// Tell curl to use HTTP POST
	curl_setopt ($session, CURLOPT_POST, true); 
	// Tell curl that this is the body of the POST
	curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs); 
	// Tell curl not to return headers, but do return the response
	curl_setopt($session, CURLOPT_HEADER, false); 
	curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
	//curl_setopt($session, CURLOPT_VERBOSE, true);
	curl_setopt($session, CURLINFO_HEADER_OUT, true);
	curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:  application/json','Authorization:  Bearer ' . $token,'X-JavaScript-User-Agent:  Mount Pearl Tennis Club Bookings'));
    
	$response = curl_exec($session);
	
	curl_close($session); 
	return $response;
}

This function accepts your post arguments that were created in another function, your valid auth token, as well as the calendar you want to post too.

It returns the result from the API.

Function To Test If Booked Already

function isTimeBooked($date,$starttime,$endtime,$cal){
	global $APIKEY;
	$start = $date . 'T' . $starttime . ':00-03%3A30';
	$end = $date . 'T' . $endtime . ':00-03%3A30';
	$token = getAccessToken();
	$result = sendGetRequest($token, 'https://www.googleapis.com/calendar/v3/calendars/' . $cal . '/events?timeMax=' . $end . '&timeMin=' . $start . '&fields=items(end%2Cstart%2Csummary)&pp=1&key=' . $APIKEY);
	if(strlen($result) > 5){
		return true;
	}
	else{
		return false;
	}
}

This function accepts the date, starttime, endtime and calendar id. It does a check to see if any event exists for this time on the calendar already. It uses the get request function to get it’s result. If the result is more than 5 characters return true, telling us that the time is used, but if it is less then 5, then there are no events, so return false.

HTML Code For Page

<html>
<head>

</head>
<body>

<div class="courtlist">
<?php 
	foreach($courts as $court){
		echo '<a href="calbook.php?cal=' . $court['cid'] . '">' . $court['name'] . '</a> | ';
	}
?>
</div>

<?php
	if(strlen($message) > 1){
		echo '<div class="message">';
		echo $message;
		echo '</div>';
	}
?>

<iframe src="https://www.google.com/calendar/embed?mode=WEEK&amp;showTitle=1&amp;showCalendars=0&amp;height=1000&amp;wkst=2&amp;bgcolor=%23FFFFFF&amp;src=<?php echo $courts[$thecal]['id']; ?>&amp;color=%232952A3&amp;ctz=America%2FSt_Johns" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no" id="califrame" onload="document.getElementById('califrame').contentWindow.scrollTo(0,document.getElementById('califrame').contentWindow.document.body.scrollHeight)"></iframe>
<h2>Book a Court</h2>
<form action="calbook.php?cal=<?php echo $thecal; ?>" method="post" name="booking">
	<input type="hidden" readonly="true" value="<?php echo $thecal; ?>" name="calendar"></input>
	Court: <input type="text" readonly="true" value="<?php echo $courts[$thecal]['name']; ?>" name="calendarname"></input><br />
	Title of Booking: <input type="text" value="Booking for ...." name="title"></input><br />
	Date: <input type="text" value="<?php echo date('Y-m-d'); ?>" id="startdate" name="startdate"></input><br />
	Start Time: <input type="text" value="" id="starttime" name="starttime"></input><br />
	End Time: <input type="text" value="" id="endtime" name="endtime"></input><br />
	<input type="submit" name="submit" value="Book Court"></input>
</form>
</body>
</html>

This code prints out a list of calendars from our array, and links to them.

Then it prints out a message if one is set.

Then we use the Google iframe code from Part 1 to display a calendar.

Then we create a form to accept input from our users. Using a nice date/time picker would make things easier on the user. The forms posts back to itself, but you could separate much of this code into different files.

PHP Code To Handle Booking

So now that we have all of the pieces in place, we can create the code that will accept the input from the users.

$message = "";

if(isset($_POST['submit']) && $_POST['submit'] == 'Book Court'){
	/*
	 * Check to see if everything was filled out properly.
	 */
	if(date('Ymd') > date('Ymd',strtotime($_POST['startdate']))){
		$message = 'You cannot make a booking in the past.  Please check your date.';
	}
	elseif($_POST['starttime'] == ''){
		$message = 'You must enter a start time.';
	}
	elseif($_POST['endtime'] == ''){
		$message = 'You must enter an end time.';
	}
	/*
	 * Check to see if booking is available for this time.
	 */
	elseif(date('Hms', strtotime($_POST['starttime'] . ':00')) < date('Hms', strtotime($courts[$_POST['calendar']]['starttime'])) || date('Hms', strtotime($_POST['endtime'] . ':00')) > date('Hms', strtotime($courts[$_POST['calendar']]['endtime']))){
		$message = 'Booking not available during this time.  Please select another time.';
	}
	/*
	 * Check to see if we are alowed to book this far in advance.
	 */
	elseif(date('Ymd',strtotime($_POST['startdate'])) > date('Ymd',strtotime('+' . $courts[$_POST['calendar']]['advance'],strtotime($_POST['startdate'])))){
		$message = 'You cannot book that far into the future.  You can only book ' . $courts[$_POST['calendar']]['advance'] . ' in the future.  Please try again.';
		//$message .= date('Ymd',strtotime($_POST['startdate'])) . ' > ' . date('Ymd',strtotime('+' . $courts[$_POST['calendar']]['advance'],strtotime($_POST['startdate'])));
	}
	/*
	 * Check and see if a booking already exists.
	 */
	elseif(isTimeBooked($_POST['startdate'],$_POST['starttime'],$_POST['endtime'],$courts[$_POST['calendar']]['id'])){
		$message = 'Time already booked.  Please select another time.';
	}
	/*
	 * Everything is good, submit the event.
	 */
	else{
		$postargs = createPostArgsJSON($_POST['startdate'],$_POST['starttime'],$_POST['endtime'],$_POST['title']);
		$token = getAccessToken();
		$result = sendPostRequest($postargs,$token,$courts[$_POST['calendar']]['id']);
		//echo '<pre>' . $result . '</pre>';
	}
}

This code checks to see if a form was submitted, and then completes a series of checks to make sure that it is a valid request. If they all pass, then the event is added to the calendar. You may want to print a message to let the user know that the event was added.

At this point, you have a fully working calendar booking system. You can experiment with the API to add additional features.

Hopefully this tutorial series was of some use to you. If you have any questions, feel free to post them below.

UPDATE (Feb. 1, 2013):

By popular demand, I’ve included a complete example of putting all of these pieces together. I’ve stripped out all of the authentication and calendar codes and keys. The first two steps of this tutorial will teach you how to get all of those.

<?php
/*
 * Example of a working config for booking to a calendar.
 */
session_start();

require_once('../backend/libs/config.php');
require_once('../backend/libs/cal/corecal.php');

/* 
 * Core Calendar Functions (contents of the corecal.php file above)
 */
function sendPostRequest($postargs,$token, $cal){
        global $APIKEY;
        $request = 'https://www.googleapis.com/calendar/v3/calendars/' . $cal . '/events?pp=1&key=' . $APIKEY;

        //$auth = json_decode($_SESSION['oauth_access_token'],true);

        //var_dump($auth);

        $session = curl_init($request);

        // Tell curl to use HTTP POST
        curl_setopt ($session, CURLOPT_POST, true);
        // Tell curl that this is the body of the POST
        curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
        // Tell curl not to return headers, but do return the response
        curl_setopt($session, CURLOPT_HEADER, true);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($session, CURLOPT_VERBOSE, true);
        curl_setopt($session, CURLINFO_HEADER_OUT, true);
        curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:  application/json','Authorization:  Bearer ' . $token,'X-JavaScript-User-Agent:  Mount Pearl Tennis Club Bookings'));
    
        $response = curl_exec($session);
   
        //echo '<pre>';
        //var_dump(curl_getinfo($session, CURLINFO_HEADER_OUT)); 
        //echo '</pre>';
        
        curl_close($session);
        return $response;
}

function sendGetRequest($token,$request){
        global $APIKEY;
        //$request = 'https://www.googleapis.com/calendar/v3/calendars/' . $CAL . '/events?pp=1&key=' . $APIKEY;

        //$auth = json_decode($_SESSION['oauth_access_token'],true);

        //var_dump($auth);

        $session = curl_init($request);

        // Tell curl to use HTTP POST
        curl_setopt ($session, CURLOPT_HTTPGET, true);
        // Tell curl not to return headers, but do return the response
        curl_setopt($session, CURLOPT_HEADER, false);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($session, CURLINFO_HEADER_OUT, false);
        curl_setopt($session, CURLOPT_HTTPHEADER, array('Authorization:  Bearer ' . $token,'X-JavaScript-User-Agent:  Mount Pearl Tennis Club Bookings'));

        $response = curl_exec($session);

        //echo '<pre>';
        //var_dump(curl_getinfo($session, CURLINFO_HEADER_OUT)); 
        //echo '</pre>';

        curl_close($session);
        return $response;
}

function createPostArgsJSON($date,$starttime,$endtime,$title){
        $arg_list = func_get_args();
        foreach($arg_list as $key => $arg){
                $arg_list[$key] = urlencode($arg);
        }
        $postargs = <<<JSON
{
 "start": {
  "dateTime": "{$date}T{$starttime}:00.000-03:30"
 },
 "end": {
  "dateTime": "{$date}T{$endtime}:00.000-03:30"
 },
 "summary": "$title",
 "description": "$title"
}
JSON;
        return $postargs;
}

function getAccessToken(){
        $tokenURL = 'https://accounts.google.com/o/oauth2/token';
        $postData = array(
                'client_secret'=>'......', #You need to fill these in to match your site.
                'grant_type'=>'refresh_token',
                'refresh_token'=>'...........', #You need to fill these in to match your site.
                'client_id'=>'.................' #You need to fill these in to match your site.
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $tokenURL);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $tokenReturn = curl_exec($ch);
        $token = json_decode($tokenReturn);
        //var_dump($tokenReturn);
        $accessToken = $token->access_token;
        return $accessToken;
}

function isTimeBooked($date,$starttime,$endtime,$cal){
        global $APIKEY;
        $start = $date . 'T' . $starttime . ':00-03%3A30';
        $end = $date . 'T' . $endtime . ':00-03%3A30';
        $token = getAccessToken();
        $result = sendGetRequest($token, 'https://www.googleapis.com/calendar/v3/calendars/' . $cal . '/events?timeMax=' . $end . '&timeMin=' . $start . '&fields=items(end%2Cstart%2Csummary)&pp=1&key=' . $APIKEY);
        if(strlen($result) > 5){
                return true; 
        }
        else{
                return false;
        }
}
  
function checkCourtRegistrations($startdate,$starttime,$enddate,$endtime,$cal){
        global $APIKEY;
        $start = $startdate . 'T' . $starttime . ':00-03%3A30';
        $end = $enddate . 'T' . $endtime . ':00-03%3A30';
        $token = getAccessToken();
        $result = sendGetRequest($token, 'https://www.googleapis.com/calendar/v3/calendars/' . $cal . '/events?timeMax=' . $end . '&timeMin=' . $start . '&fields=items(end%2Cstart%2Csummary)%2Csummary&pp=1&key=' . $APIKEY);
        if(strlen($result) > 5){
            $result = json_decode($result,true);
            //return array($result['summary'] => $result['items']);
            return array('items' => $result['items'], 'court' => $result['summary']);
        }
        else{
            return '';
        }
}
/* 
 * End Core Calendar Functions (contents of the corecal.php file above)
 */

$thecal = 'court1';
if(isset($_GET['cal'])){
        $thecal = addslashes($_GET['cal']);
}

/*
 * Advance is the amount of time in the future someone can book something.
 * days
 * weeks
 * months
 * If it is 0, it will allow unlimited future booking
 */

$courts = array(
        'court1' => array('cid' => 'court1', 'name' => 'Court 1', 'id' => '{YOUR CAL}@group.calendar.google.com', 'starttime' => '08:00:00', 'endtime' => '23:00:00', 'advance' => '1 week'),
        'court2' => array('cid' => 'court2', 'name' => 'Court 2', 'id' => '{YOUR CAL}@group.calendar.google.com', 'starttime' => '08:00:00', 'endtime' => '23:00:00', 'advance' => '1 week'),
        'court3' => array('cid' => 'court3', 'name' => 'Court 3', 'id' => '{YOUR CAL}@group.calendar.google.com', 'starttime' => '08:00:00', 'endtime' => '23:00:00', 'advance' => '1 week'),
        'court4' => array('cid' => 'court4', 'name' => 'Court 4', 'id' => '{YOUR CAL}@group.calendar.google.com', 'starttime' => '08:00:00', 'endtime' => '23:00:00', 'advance' => '1 week'),
        'court5' => array('cid' => 'court5', 'name' => 'Court 5', 'id' => '{YOUR CAL}@group.calendar.google.com', 'starttime' => '08:00:00', 'endtime' => '23:00:00', 'advance' => '1 week'),
        'court6' => array('cid' => 'court6', 'name' => 'Court 6', 'id' => '{YOUR CAL}@group.calendar.google.com', 'starttime' => '08:00:00', 'endtime' => '23:00:00', 'advance' => '1 week'),
);

$APIKEY = '{YOUR API KEY}';

$message = "";

if(isset($_POST['submit']) && $_POST['submit'] == 'Book Court'){
        /*
         * Check to see if everything was filled out properly.
         */
        //echo 'start submit' . date('Hms', strtotime($_POST['starttime'] . ':00'));
        //echo 'start default' . date('Hms', strtotime($courts[$_POST['calendar']]['starttime']));
        //echo 'end submit' . date('Hms', strtotime($_POST['endtime'] . ':00'));
        //echo 'end default' . date('Hms', strtotime($courts[$_POST['calendar']]['endtime']));
        if(date('Ymd') > date('Ymd',strtotime($_POST['startdate']))){
                $message = 'You cannot make a booking in the past.  Please check your date.';
        }
        elseif($_POST['starttime'] == ''){
                $message = 'You must enter a start time.';
        }
        elseif($_POST['endtime'] == ''){
                $message = 'You must enter an end time.';
        }
        /*
         * Check to see if booking is available for this time.
         */
        elseif(date('Hms', strtotime($_POST['starttime'] . ':00')) < date('Hms', strtotime($courts[$_POST['calendar']]['starttime'])) || date('Hms', strtotime($_POST['endtime'] . ':00')) > date('Hms', strtotime($courts[$_POST['calendar']]['endtime']))){
                $message = 'Booking not available during this time.  Please select another time.';
        }
        /*
         * Check to see if we are alowed to book this far in advance.
         */

        elseif(date('Ymd',strtotime($_POST['startdate'])) > date('Ymd',strtotime('+' . $courts[$_POST['calendar']]['advance'],strtotime($_POST['startdate'])))){
                $message = 'You cannot book that far into the future.  You can only book ' . $courts[$_POST['calendar']]['advance'] . ' in the future.  Please try again.';
                //$message .= date('Ymd',strtotime($_POST['startdate'])) . ' > ' . date('Ymd',strtotime('+' . $courts[$_POST['calendar']]['advance'],strtotime($_POST['startdate'])));
        }
        /*
         * Check and see if a booking already exists.
         */
        elseif(isTimeBooked($_POST['startdate'],$_POST['starttime'],$_POST['endtime'],$courts[$_POST['calendar']]['id'])){
                $message = 'Time already booked.  Please select another time.';
        }
        /*
         * Everything is good, submit the event.
         */
        else{
                $postargs = createPostArgsJSON($_POST['startdate'],$_POST['starttime'],$_POST['endtime'],$_POST['title']);
                $token = getAccessToken();
                $result = sendPostRequest($postargs,$token,$courts[$_POST['calendar']]['id']);
                //echo '<pre>' . $result . '</pre>';
        }
}
?>
<html>
<head>
	<!-- The below JS library is from home.jongsma.org/software/js/datepicker -->
        <script language="javascript" src="../backend/libs/picker/js/prototype-1.6.0.2.js"></script>
        <script language="javascript" src="../backend/libs/picker/js/prototype-base-extensions.js"></script>
        <script language="javascript" src="../backend/libs/picker/js/prototype-date-extensions.js"></script>
        <script language="javascript" src="../backend/libs/picker/js/datepicker.js"></script>
        <link rel="stylesheet" href="../backend/libs/picker/css/datepicker.css" />
        <link rel="stylesheet" type="text/css" href="../backend/styles/default.css" />
        <script type="text/javascript">
                function showHideCourt(){
                        if(document.getElementById('courtref').style.display == 'block'){
                                document.getElementById('courtref').style.display = 'none';
                                document.getElementById('showHideToggle').innerHTML = "Show Court Reference";
                        }
                        else{
                                document.getElementById('courtref').style.display = 'block';
                                document.getElementById('showHideToggle').innerHTML = "Hide Court Reference";
                        }
                }
        </script>
</head>
<body>
<div class="notes">
        <h2>Instructions</h2>
        <ul>
                <li>Use the links below to view existing bookings for each of the courts.</li>
                <li>Once you have selected a time you would like to book, scroll to the bottom of the page and fill out the booking form.</li>

        </ul>
</div>
<div class="courtlist">
<?php
        foreach($courts as $court){
                echo '<a href="bookings.php?cal=' . $court['cid'] . '">' . $court['name'] . '</a> | ';
        }
?>
        <a href="#" id="showHideToggle" onclick="showHideCourt()">Show Court Reference</a>
</div>
<div id="courtref" class="courtref">
        <img src="<?php echo SITE_ROOT . 'images/courts.png'; ?>" />
</div>

<?php
        if(strlen($message) > 1){
                echo '<div class="message">';
                echo $message;
                echo '</div>';
        }
?>

<iframe src="https://www.google.com/calendar/embed?mode=WEEK&amp;showTitle=1&amp;showCalendars=0&amp;height=1000&amp;wkst=2&amp;bgcolor=%23FFFFFF&amp;src=<?php echo $courts[$thecal]['id']; ?>&amp;color=%232952A3&amp;ctz=America%2FSt_Johns" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no" id="califrame" onload="document.getElementById('califrame').contentWindow.scrollTo(0,document.getElementById('califrame').contentWindow.document.body.scrollHeight)"></iframe>
<h2>Book a Court</h2>
<form action="bookings.php?cal=<?php echo $thecal; ?>" method="post" name="booking">
        <input type="hidden" readonly="true" value="<?php echo $thecal; ?>" name="calendar"></input>
        Court: <input type="text" readonly="true" value="<?php echo $courts[$thecal]['name']; ?>" name="calendarname"></input><br />
        Title of Booking: <input type="text" value="Booking for: <?php echo substr($_SESSION['fname'],0,1) . '. ' . $_SESSION['lname'];  ?>" name="title"></input><br />
        Date: <input type="text" readonly="true" value="<?php echo date('Y-m-d'); ?>" id="startdate" name="startdate"></input><br />
        Start Time: <input type="text" readonly="true" value="" id="starttime" name="starttime"></input><br />
        End Time: <input type="text" readonly="true" value="" id="endtime" name="endtime"></input><br />
        <input type="submit" name="submit" value="Book Court"></input>
</form>
<div class="notes">
        <h2>Notes</h2>
        <ul>
                <li>If you need to change your booking, please call the club house.</li>
                <li>You can only book one session per day, and up to one week in advance.</li>
        </ul>
</div>
<script language="javascript">
        new Control.DatePicker('startdate', { icon: '../backend/libs/picker/images/calendar.png', datePicker: true, timePicker: false, dateFormat: 'yyyy-MM-dd' });
        new Control.DatePicker('starttime', { icon: '../backend/libs/picker/images/clock.png', datePicker: false, timePicker: true });
        new Control.DatePicker('endtime', { icon: '../backend/libs/picker/images/clock.png', datePicker: false, timePicker: true });
</script>
</body>
</html>
Jan 082012
 

This is the second part of a three part series on how to embed a Google Calendar into a web page and use it to accept online bookings/appointments from other online users.

The Series:

  1. Part 1: Setting up Google Calendar
  2. Part 2: OAuth2 and Configuring Your ‘Application’ With Google <- You Are Here
  3. Part 3: A Sample Web Page For Bookings

Background

This was by far the hardest part of the whole exercise. I had worked with version 1 of the Google API for PHP a few years back. This allowed you to code your username and password into your script, and it would handle authentication for your application. Now that we are on to version 3 of the API, that method is no longer available. Instead OAuth2 is used for authentication and token management.

I downloaded code samples, and went about building my application, however, I quickly realized that the OAuth2 code samples are designed to allow you to interact with a visitors calendar. In the case of taking online bookings, I need to work with a single calendar, namely my calendar, not theirs.

After a lot of trial and error, and then reading, I realized that it could be done, and relied on what is called a ‘Refresh Token’ in OAuth2. This token allows you to get a new valid authentication token when the initial grant from your end user expires. Since the refresh token doesn’t expire, you can always use it to get a new authentication token, and therefore people can continue to use your application after you have initially configured it. I spent a while trying to implement it myself with no success, but then I came across this page: http://www.ericnagel.com/how-to-tips/google-affiliate-network-api.html This explains in some detail how to configure the application and token. It is written for the Google Affiliate Network API, but I made a few tweaks to make it work for Calendar. I will now take you through the steps of setting up your application with Google, and generating your Refresh Token.

Create Your Application

Log into your Google Account, and then visit https://code.google.com/apis/console/. This will take you to a page that invites you to create a project with the Google API. Click on Create project….

You are now asked to activate the services you wish to use. Click the button next to Calendar API to enable the calendar. You will be redirected to a page with a Terms of Service. Read and accept this.

Now click on API Access. Here we will configure the IDs needed for your application to authenticate with Google. Click on Create an OAuth2 client ID…. You will be offered to create Branding Information. You should add your project/product name. The rest won’t be necessary as you will not be asking users directly for access to their resources, but you can complete it if you like.

Then click Next. Here you will want to select Installed application. Click Create client ID.

You will be taken back to the API Access screen, with your new Client ID and Client secret. You will need this information to generate your Refresh Token, and to configure your application.

This page will also have your API key for ‘Simple API Access’. You will also need this API Key for your final calendar application.

Get Your Application Information

Now that you have your application information, it is time to generate your refresh token. I’ve modified the script available from http://www.ericnagel.com/how-to-tips/google-affiliate-network-api.html to just get us our refresh token for our calendar application. Here is the code for the script. Download this and save it as oauth-setup.php:

<?php
$cScope         =   'https://www.googleapis.com/auth/calendar';
$cClientID      =   '';
$cClientSecret  =   '';
$cRedirectURI   =   'urn:ietf:wg:oauth:2.0:oob';
 
$cAuthCode      =   '';

if (empty($cAuthCode)) {
    $rsParams = array(
                        'response_type' =>   'code',
                        'client_id'     =>   $cClientID,
                        'redirect_uri'  =>   $cRedirectURI,
                        'scope'         =>   $cScope
                        );
    $cOauthURL = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($rsParams);
    echo("Go to\n$cOauthURL\nand enter the given value into this script under \$cAuthCode\n");
    exit();
} // ends if (empty($cAuthCode))
elseif (empty($cRefreshToken)) {
    $cTokenURL = 'https://accounts.google.com/o/oauth2/token';
    $rsPostData = array(
                        'code'          =>   $cAuthCode,
                        'client_id'     =>   $cClientID,
                        'client_secret' =>   $cClientSecret,
                        'redirect_uri'  =>   $cRedirectURI,
                        'grant_type'    =>   'authorization_code',
                        );
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, $cTokenURL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $rsPostData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
    $cTokenReturn = curl_exec($ch);
    $oToken = json_decode($cTokenReturn);
    echo("Here is your Refresh Token for your application.  Do not loose this!\n\n");
    echo("Refresh Token = '" . $oToken->refresh_token . "';\n");
} // ends
?>

Before running this script, you will need to enter your Client ID ($cClientID) and Client Secret ($cClientSecret) as we found on the API page with Google. Once these values are added, run this script from the command line like this: php oauth-setup.php. You should see output like this:

thomas@thomas-desktop:~/code$ php oauth-setup.php 
Go to

https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=##########################&redirect_uri=###############&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar

and enter the given value into this script under $cAuthCode

Visit the website, grant permission to access your resources, and then copy the code on this page. This is your auth code, and is normally good for 3600 seconds or so.

Enter this code into the oauth-setup.php script in the $cAuthCode variable. Then run the script again: php oauth-setup.php. You should see output like this:

thomas@thomas-desktop:~/code$ php oauth-setup.php 
Here is your Refresh Token for your application.  Do not loose this!

Refresh Token = '#####################################';

Now, copy down the Refresh Token and save it for later. You will need it to make subsequent requests to Google to get a valid Auth Code for a transaction.

Stay tuned for Part 3 of the tutorial, which will use the above information to make calendar requests to Google. And allow us to create a web application that uses Google Calendar as a backend for a scheduling application.

Dec 312011
 

This is the first part of a three part series on how to embed a Google Calendar into a web page and use it to accept online bookings/appointments from other online users.

The Series:

  1. Part 1: Setting up Google Calendar <- You Are Here
  2. Part 2: OAuth2 and Configuring Your ‘Application’ With Google
  3. Part 3: A Sample Web Page For Bookings

Part 1: Setting up Google Calendar to be embedded into a web page

You can already use Google Calendar on your web page for displaying your schedule. This tutorial will take you through that process quickly highlighting some of the key pieces that you will need later.

Step 1: Signup for a Google Account and Create Your Calendar

I’m going to assume you have, or know how to get a Google Account. You can get a Google Account (with your email address) here: https://accounts.google.com/NewAccount or you can just sign up for a Gmail account, which is awesome, here: https://accounts.google.com/NewAccount?service=mail&continue=http://mail.google.com/mail/e-11-cf5c9f72b92b7a0f5c1f47b879e4b-e035ec50fab44f274a9436e7e96b8bbcef278d4e&type=2.

Now, go into Google Calendar, and create a calendar that you will use for your appointments.

Give the calendar a name (1), description (2), and location (3).

Here you have the option to configure sharing (4). You will need to make the calendar public for others to see it. You can optionally choose to display only Free/Busy information to protect the privacy of the events (5).

Now, you need to grab the embed code to add the calendar to your site. You grab this by going to Calendar Settings.

Step 2: Get The Calendar Embed Code and Calendar ID

Here you will see the embed code (1), and a link to customize your output. On this page you will also see the Calendar ID (2).

This code will put an IFRAME on your page which will display your calendar. You can use the customized to tweak the look and feel of the calendar.

You will need the calendar ID for the next parts of the tutorial.

That is the end of this tutorial. You now have a calendar added to your website which will display your schedule to your web users.

Stay tuned for Part 2 of the tutorial, which will get our application set up with Google so that we can use the API to add events to the calendar.

Aug 092011
 

Filezilla is a great ftp/sftp/… tool. But it is missing one feature that would make it more useful: a file diff ability. This allows you to see the remote file, and local file side by side to see what is different between them. This is an often requested feature in the Filezilla forums, and until it is implemented officially, you can use this workaround to get diffs in Filezilla.

This was done running Filezilla 3.3.1 on Ubuntu 10.04.

  1. First, install Meld (haven’t tried it with any other diff viewer yet)
  2. Then go to Edit -> Settings -> File editing
  3. Change to ‘Use custom editor:’ and enter: /usr/bin/meld /home/{yourusername}/filezillafake.txt
  4. Click OK.
  5. Create a file in your home directory called filezillafake.txt
  6. You may need to also select “Always use default editor”. Optionally, you can go one menu below to “Filetype associations” and add the command for any file types that you want so it isn’t available for all.

Now, when you View/Edit a file, it will open in meld with your fake file on the left, and the remote file on the right. Then drag the file from your filezilla window into the ‘Browse’ area for the fake file in the Meld Window. It will load up and show you the diff.

You should be able to edit the file here and save it and Filezilla should prompt for an upload.

This works because Meld supports drag and drop, and also inserting two file names at the command prompt. Any application for any platform that does that should be supported by this method. The only downside is that Meld is now your default editor for files, and you may not like that if you do a lot of remote editing.

Aug 072011
 

I recently began working on a new website that will catalog all of the tennis courts in Newfoundland.  You can visit at http://www.tennisnewfoundland.com

Today I added coordinates for each court and city. This allows me to create a custom google map for each page and put links directly on the map. I also added a verification feature so that courts which are confirmed open float to the top and those which aren’t float to the bottom. I made some tweaks to the style sheets as well which should make the site more userfriendly on mobile devices.

Other features include a commenting feature using Disqus, pages for cities and a list of clubs in the province.

Take a look and let me know what you think.

Jan 272011
 

Recently, I needed the ability to get a readout of the average bandwidth used on the server at one time. As many of you probably know, bandwidth is tough to measure for an instant. I could not find a tool or command that would allow me to get the data I wanted. So I decided to write one.

I write a perl script that uses the output from one invocation of ifconfig with a later invocation. The results are compared, and the total transferred, along with the average bps is extracted and printed.

Here is an example of printout:


thomas@thomas-desktop:~$ perl bandwidth-checker.pl -p -i=eth0 -t=5
Monitored traffic on interface eth0 for 5 seconds:
Incoming: 17.7109375 kbit (2.2138671875 KB) Average: 3.5421875 kbps (0.4427734375 KBps)
Outgoing: 2.109375 kbit (0.263671875 KB) Average: 0.421875 kbps (0.052734375 KBps)

I wrote a little page on the script, and have the source there as well. Hopefully it will help someone else out there: http://wiki.cornempire.net/scripts/bandwidth

If you have any use for it, on find any problems with the script, please comment below.