VPN and hosts file

Struggled through this tonight.

Launched my OS X Juniper Network Connect VPN client, grabbed source code for a new project and started setting configuring my system to run it. Edited the /etc/hosts file to add the virtual host name to it. Project was working at that point. A little later I disconnected from VPN and when to work on the project again and nothing worked.

Apparently the Network Connect VPN client made a copy of the /etc/hosts file and added a few lines at the top of the file. I noticed it, but didn’t think anything of it. When I disconnected it reverted to the original version. That blew away the changes that I made, and the reason nothing worked.

Very frustrating trying to debug that when I knew I had made the proper changes. Moral of the story, don’t edit a Network Connect VPN created /etc/hosts file, it will revert to the original version when it disconnects.

Houston Chronicle Accessibility

Reading through Twitter today, I ran across a tweet from @wilto stating that The Houston Chronicle nailed their redesign.  While it’s a great design in my opinion (clean, concise, and responsive) there were a few problems with implementation.  There are several accessibility issues that are either missing, overlooked, or not thought about. Accessibility is something that most web developers I know are aware of, but rarely does it get treated as a first class citizen. Most of the problems that I note are fairly simple to overcome, but they make a world of difference to people who depend upon them.

From just running the WAVE Toolbar and JuicyStudio Web Accessibility Toolbar in FireFox I was able to find these. While a lot of web developers stay in Chrome for most of their development they should also be aware that FireFox is typically the browser of choice for Assistive Technology (AT) users.

  • “Skip to” Navigation – In general it is used to skip over navigation.  Typically AT users will use these links to get to the main content of a site without having to listen to the menus, navigation, and tangential information of a site.  While you might say just scroll down the page to what you want, not all AT user have the mobility or dexterity to do that.
  • Alt attributes for Images – This is something that trivially easy to fix as well.  A descriptive blurb about what the image is portraying.  For an vision impaired user, they have no idea what an image is trying to portray.  Screen readers pickup the ALT text and read it to the AT user.
  • Empty Headers – Screen Readers use content structure to portray content to AT users.  Without actual content in the header an AT wouldn’t know if that was the content that they were looking for.
  • ARIA Landmarks – There are zero landmarks on the page.  Landmarks define areas of a page so that screen readers can convey that information to the AT User.  Landmarks indicate items like Navigation, Main, Search, Complementary, etc.   It’s a bit more granularly defined than the “Skip to” navigation links.
  • Color Contrast – This is used to help make text more readable to sight impaired users. The W3C lays out some guidelines to help accomplish this in the WCAG 2.0 standard.  Basically for small text you need a minimum of 4.5:1 ratio for small text, 3.5:1 for large text. What that accomplishes is a dark color on a light background or a light text on a dark background.  For instance, having a light gray text on a white background, for sight impaired users the text blends together and is hard to discern the text.

The corrections to these problems are fairly unobtrusive to unimpaired users and are fairly easy to diagnose and correct.  There are automated tools and validators that are available to help detect them.  Sites like WAVE by WebAIM and FireFox plugins like Juicy Studio: Colour Contrast Analyser and Web Developer Toolbar are available.

Zend Framework Project in a sub-folder

We had a problem at work where we needed to deploy a Zend framework project to a subfolder on the server.  This is a new server for us, and the first time this situation has came up.  It took us a few hours to figure out the solution as we could get one thing working and then another would break.  So to try and prevent that for someone else I’m sharing what I learned.

The server we are using is on a subdomain and soon to have multiple Zend projects.  We couldn’t use multiple sub-domains, which would have made this much easier.

So the problem at hand is that we need to deploy a Zend Framework project to a subfolder using a Virtualhost.  In this particular case there is a static index.html page that needs to be served up at the root of the subdomain (not everyone will have this situation, but it’s what we needed).

The svn check out in a nutshell:

/admin.myserver.com
	L webadmin
	|	L index.html
	L service
		L application
			L bootstrap.php
		L docs
		L library
		L public
			L index.php
			L .htaccess
		L tests

So let me set the stage.

  • http://admin.myserver.com/service is the url that we want the ZF project to be served out of.
  • The application has multiple virtual folders/controllers and parameters that need to be called. e.g.  http://admin.myserver.com/service/reps?name=Bob%20Smith
  • /admin.myserver.com/service/public is the SVN location of the ZF project to be served up to apache
  • /admin.myserver.com/webadmin is the location of index.html that is going to consume the service.

 

So what we are doing is creating a virtual host for admin.myserver.com.  The key thing is creating the Alias for the /service, so when we go to http://admin.myserver.com/services  the public folder of our app is used.  Next configure the settings for the front end HTML directory that will be served at the root of the domain.  We then configure the settings for the Zend application.  Pretty straight forward, but if you aren’t sure where to start it’s hard to find.

Apache vHost for project:

<VirtualHost *:80>
    DocumentRoot "/Source/admin.myserver.com/webadmin"
    ServerName admin.myserver.com
    Alias /service /Source/admin.myserver.com/service/public

    <Directory "/Source/admin.myserver.com/webadmin">
        Options Indexes FollowSymLinks MultiViews ExecCGI
        AllowOverride All
        order allow,deny
        Allow from All
    </Directory>

    <Directory "/Source/admin.myserver.com/service/public">
        Options Indexes FollowSymLinks MultiViews ExecCGI
        AllowOverride All
        order allow,deny
        Allow from All
    </Directory>
</VirtualHost>

Next we edit the Bootstrap.php in the Zend project.  Some examples I came across had this set in the application.ini, but I couldn’t get that to work, so this is what we ended up with.  The key thing is that we are setting the setBaseUrl to the subfolder where the application lives.

protected function _initSetupBaseUrl() {
	$this-&gt;bootstrap('frontcontroller');
	$controller = Zend_Controller_Front::getInstance();
	$controller-&gt;setBaseUrl('/service'); //the subfolder your app is in
}

This is where the tricky part is.  Because I’m on a Mac I didn’t see the .htaccess file in the Finder.  The .htaccess resides in the /service/public folder.  The contents of this file could be migrated to the Directory settings in the services/public location and delete the .htaccess file if you so desire.  The addition of RewriteBase is the key to this file.  This is the piece that took us (read: me) forever to find.

RewriteEngine On
RewriteBase /service
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

So I hope this helps out someone else so they don’t waste a ton of time like I did searching for the answers.

Solving the Fizz Buzz Problem

I was asked to solve a classic programming question today, the “FizzBuzz” problem.

The problem statement:

  • Write an algorithm that prints the numbers from 1 to 100.
  • for multiples of three print “Fizz” instead of the number
  • for multiples of five print “Buzz” instead of the number
  • For numbers which are multiples of both three and five print “FizzBuzz”

To spice it up with some more UI work here’s some additional criteria

  • Create a basic web page
  • have inputs for two 2 multiples used in the algorithm (e.g. 3 and 5 in the base case)
  • an input used to determine how many numbers to go through
  • a button that triggers the custom fizz buzz calculation to be ran and updates the page with the results

Feel free to use any javascript library you’re familiar with to accomplish this task.

So this is how I tackled it:

First, I’m familiar with jQuery more than anything else at the moment, so this example will leverage it.  Because this was essentially just a JavaScript/jQuery coding test I just grabbed HTML5 Boilerplate as a baseline.  It included the libraries, base html, and css that I needed.

This is the stripped down HTML that we are going to be working with.  First I am going to markup the form that we will be using, I’m using the fancy new HTML5 number input and defining some minimum and base values.  Next I chose to use an unordered list for this example.

<form id="fizzbuzzform">
	<fieldset>
		<label for="num1">Enter Fizz Number:</label>
		<input type="number" id="num1" min="0" value="3"></input>
		<label for="num2">Enter Buzz Number:</label>
		<input type="number" id="num2" min="0" value="5"></input>
	</fieldset>
	<fieldset>
		<label for="totalitems">Enter Number of items to iterate through:</label>
		<input type="number" id="totalitems" min="0" value="100"></input>
	</fieldset>
	<input type="submit" id="submit" value="Submit"></input>
</form>

<ul id="fizzbuzzlist">
</ul>

Next we are going to need a function to call

function fizzBuzz(){};

We are going to need to pass some parameters to it for the 3 inputs

function fizzBuzz(fizz, buzz, iterations){};

Next we need to loop over the amount of iterations

function fizzBuzz(fizz, buzz, iterations){
	for (var i=1; i<=iterations; i++){
		//this will go through how ever many iterations are set.
	}
};

We now need to start looking at the logic of determining if the number is the fizz or buzz number.  The core logic of this is based on the Modulus operator or in JavaScript the % operator.  What the % operator does is provide the remainder of a division operation.  For 15 % 5, there is no remainder left because it is divisible by 5.  For 7 % 3  there is a remainder of 1.  To determine if the iteration is a number that is divisible, we need to see if the result is 0.  That means that it will be a Fizz or Buzz number.

function fizzBuzz(fizz, buzz, iterations){
	for (var i=1; i<=iterations; i++){
		if (i % fizz == 0){
		  //this is fizz number
		}
	}
};

Throw in a || to test for the buzz number.

function fizzBuzz(fizz, buzz, iterations){
	for (var i=1; i<=iterations; i++){
		if (i % fizz == 0 || i % buzz == 0){
			//this is a fizz or buzz number
		}
	}
};

One of the other things that we need to do is to make sure that we aren’t dividing by 0.  I believe in JavaScript that it would return an undefined result.  But in Math I believe it’s called a NaN, or Not a Number.

function fizzBuzz(fizz, buzz, iterations){
	for (var i=1; i<=iterations; i++){
		if (i != 0 && (i % num1 == 0 || i % num2 == 0)){
			//this is a number above 0 that is a fizz or buzz number
		}
	}
};

So at this point, we know if the number in question is a fizz or buzz number, but we don’t know which one of the two.

function fizzBuzz(fizz, buzz, iterations){
	for (var i=1; i<=iterations; i++){
		if (i != 0 && (i % num1 == 0 || i % num2 == 0)){
			if (i % fizz == 0) {
				//output fizz
			};
			if (i % buzz == 0) {
				//output buzz
			}
		}
	}
};

Because of how the problem is phrased we need to output the iteration number if it isn’t a fizz or buzz number.  So we need to add an else to the first if statement.

function fizzBuzz(fizz, buzz, iterations){
	for (var i=1; i<=iterations; i++){
		if (i != 0 && (i % num1 == 0 || i % num2 == 0)){
			if (i % fizz == 0) {
				//output fizz
			};
			if (i % buzz == 0) {
				//output buzz
			}
		} else {
			//output the iteration number
		}
	}
};

Now comes the time we add some string building.  In this particular example I’m going to use an unordered list for the HTML.  We could have used just about anything here, this is just what I picked.

We need to initialize an empty string before the for loop.  The reason we do that is for scope reasons and we wouldn’t want to keep reinitializing the same variable.

function fizzBuzz(fizz, buzz, iterations){
	var fizzBuzzListItems = "";

	for (var i=1; i<=iterations; i++){

		if (i != 0 && (i % fizz == 0 || i % buzz == 0)){
			//building out a string and appending it to the list
			fizzBuzzListItems += "<li>";
			if (i % fizz== 0) {
				fizzBuzzListItems += "Fizz";
			};
			if (i % buzz == 0) {
				fizzBuzzListItems += "Buzz";
			}
			fizzBuzzListItems += "</li>"
		} else {
			//if the modulus is 0 write out the iteration number
			fizzBuzzListItems += "<li>" + i + "</li>";
		}
	}
};

The last thing that we need to do for the function is to return the HTML that we just built up.

function fizzBuzz (fizz, buzz, iterations){
	var muListItems = "";
	
	for (var i=1; i<=iterations; i++){
		
		if (i != 0 && (i % fizz == 0 || i % buzz == 0)){
			muListItems += "<li>";
			if (i % fizz == 0) {
				muListItems += "Fizz";
			};
			if (i % buzz == 0) {
				muListItems += "Buzz";
			}
			muListItems += "</li>"
		} else {
			muListItems += "<li>" + i + "</li>";
		}
	}
	return muListItems;
}

Now we need to setup a listener event for when the user submits the form. There is a gotcha in this though. When the user submits a form, the default action is to post the form and render a new page. If you don’t prevent that behavior then you will probably see your results flash for a second and then get reset because the page reloaded. So we are going to add a .preventDefault() so that the post isn’t performed.  We also want this event listener to be added on $(document).ready().  Also notice that we did not put a listener on the Submit button, but on submit action of the form.

$(document).ready(function(){
	//setting a listener for the Submit action.
	$('form').submit(function(event){
		//preventing the default submit action as it would clear the results.
		event.preventDefault();
		//pass the function as a parameter to the $.html() with the appropriate values as inputs
		//write out the HTML to the selected markup element.
		//this is done once so that multiple writes to the DOM are not made in the iteration
		$('#fizzbuzzlist').html(
			fizzBuzz ($('#num1').val(), $('#num2').val(), $('#totalitems').val())
		);
	});
});

So at this point we are done.  When you load the page there are 3 inputs for the Fizz Number, Buzz Number, and the amount of numbers to check. When you hit submit, it returns the results as list items and inserts them into the unordered list.

Switched comments over to Disqus

I decided to switch how my comments were controlled on the site.  I decided to go with Disqus because you could login/oauth with multiple different ID’s, the installation was simple, and it could import the previous comments into it.

I ran into a small issue with how the theme was style for some list-item-style elements, but I just overrode it with some custom styles from within Disqus.

So let me know if you like the change, I think it should provide some more interactivity on my posts.