Spam sucks

I went through a massive cleansing of this blog, deleting over 560 user accounts.

Rest assured, no actual accounts were deleted. Not that anyone registered is actually human…yep that’s right I’m essentially talking to myself.

Posted in Uncategorized | Leave a comment

reCaptcha Fail

Yea so I was busy testing a form when I ran across this..  lol. Needless to say, my attempt to enter the right ‘phrase’ failed as well. NICE reCaptcha, NICE.

reCaptcha Fail

reCaptcha Fail

I accidentally Googled ‘recaptcha fail’ on image search and found more disturbing occurrences.  Find out for yourself.

Posted in Uncategorized | 2 Comments

App Idea: Google Maps + User Traffic Data

It’s not that I want to know what the traffic was like this past Saturday, but more like what it COULD be like this next Tuesday morning, 8:00 A.M. when I have an important meeting to get to at 8:30 am. Leaving earlier is of course the more prudent thing to do but is not always possible for the workaholic like me.

So come Tuesday your Android phone gives you an unfortunate straight shot through the slow parts of traffic when you think there might be a good chance of getting there quicker with another route via surface streets. Gamble or not to gamble? How about option C: Get a smarter traffic app.

Forget IBM’s shotty attempt at this. So what if I’m talking about a huge amount of data. Google has plenty of space to spare. Why not make it into a global idea? Not just user-centric but constant data submitted by users to inform them which are the best routes for their destination.

Thus the solution: Generate better route information based on statistical data submitted by users using the same app. Don’t be selfish.. sharing is caring.

In short, an artificially intelligent path-finder (A-Star Cost search anyone?). I did not do my homework a whole lot but I think its suffice to say that other devices or apps might do something similar (hello. IBM), but not with the accuracy of constant updates to traffic information from users on the same road. They might gather their information from traffic analysts, government and other publicly available monitoring devices (tool booths, stop lights, etc) but the best source I’d say is the person sitting in traffic.

As time goes on, the reports for particular routes become more and more accurate. Anonymous usage statistics of course without any way to trace it back to the user for privacy/security reasons.

It’s like Wikipedia but for the open highways and surface streets.

Forget user-centric. Think global-centric.

Make it free to the public.

Posted in Android, Programming Theory | Tagged , , , , , | Leave a comment

Facebook Challenges – LiarLiar

Recently I’ve been wanting a challenge.  After watching the Social Network, the dramatized version of Mark Zuckerberg’s story, I was inspired by some of the short coding sessions that he likes to do as proven with the all-night Facebook Hackathons (which mostly do not have much to do with actual hacking.  I won’t get into that).

I’ve definitely been met with a challenge. This is a cut and dry version of the Knights and Knaves puzzle by our friend Smullyan

The idea: Your algorithm must process a huge amount of data quickly and accurately decide who is lying and who is not. A nice little Turing test if you will.

For those who are theory-savvy think of one-pass in a file and that’s it. Your processing should be done in a ‘reasonable amount of time’ (reasonable to humans because that’s who would really care, right?).

I guess a hint in the right direction might be:

  • Draw a graph with arrows to each entity that is being ‘accused’
  • Use a list of accuser ->accusee symbols
  • Use truth tables
  • For the best results, think of data space vs. cpu space carefully
  • Remember to test ‘border’ or ‘edge’ cases!
  • Definitely hit up the unofficial guide to the Facebook Puzzles

Finally.. try your algorithm with the 5 test cases they have. Just in case your algorithm is too slow to handle a large amount of data be sure to start out with the first test they provide in their example or else you might have to kill the process. :)

There’s a ton more puzzles to tackle and this is definitely a good break from the day to day tasks for any programmer.

Post your ideas and what languages you might be using, but please no algorithms/solutions.  Email me at bran [dot] cham [at] gmail if you would like to discuss algorithms further!

Good luck and have fun!

Posted in Programming Theory, Puzzles | 1 Comment

AJAX (jQuery) + eZ Publish: Demystified!

eZ Publish is by nature, easy to understand in the administration panel, adding and editing content. However, once you look under the hood it’s like a complex maze of code, intricately designed, but still a maze. While the template code, based on Smarty I believe, is well documented there’s no single source to figure out how things work in PHP land.  You just find out for yourself by looking through other extensions as well as the kernel code, ask other people or by just plain “elbow grease.” Well here’s some findings I came across fumbling around on a project as well as spare time.

Tutorial Skill requirements:

  • JS
  • eZ Publish
  • PHP
  • Extension/Module/View installation & configuration

If you need to head over to eZ Systems’ site for more information on installing and configuring an extension

Discovery

I came across two main ways to implement AJAX (jQuery) with eZPublish.

  • AJAX + a Custom Module
  • AJAX + eZJSCore + a Custom Class (implementing a special type of ezjscore class)

Which offers the most flexibility?

  • Both allow access to the eZ Publish class interface
  • Module approach allows flexibility of having a view associated with the AJAX functionality thus allowing up to three avenues of functionality for the script to run as. (AJAX Post + Regular Post + View)
  • Module approach allows access to the Module parameters array. (This allows one to use AJAX calls in an unconventional way using a post to /mymodule/param1/param2/param3 which isn’t recommended)
  • eZJSCore approach has cleaner, more concise code.
  • eZJSCore approach allows for multiple functions in the class (don’t forget to register your other functions in the INI files!) as opposed to a one-time run-through with the module approach. Although you can add other functions to a module, eZ Publish requires you to  add a separate PHP file.
  • Anyone find something else I’m missing?

Show me the Code!

AJAX + Custom Module:

So obviously you need a module and a view (and thus an extension). By looking at the JavaScript you’ll see the module and view names I used. You can use whatever you want.

The JavaScript:

var data_sent = { message: "testing!"};
 
$.ajax({
    url: "/mymodule/post",
    type: 'POST',
    data: ( data_sent),
    dataType: 'json',
    success: function( data ){
        if( data.response == "ok" )
        {
            alert( data.message );
        }
    }
});

The Module code:

In post.php I usually have something along the lines of this initial setup:

if ( $http->hasPostVariable( 'message'' ) && $http->postVariable( 'message' ) )
{
    $response = array( 'response' => 'ok');
    print( json_encode( $response ) );
    eZExecution::cleanExit();
}

That’s IT!

AJAX + eZJSCore + Custom Class:

This approach requires you have eZJSCore installed and working.

One of the first things you’ll notice with this approach by looking at the code is there’s a little more of it, but it looks cleaner in the JavaScript code.

To get eZJSCore setup add your custom class to ezjscore.ini (preferably in your extension’s settings directory):

[ezjscServer]
FunctionList[]=ajaxFunc
 
[ezjscServer_myajax]
Class=MyAJAX
File=extension/myextension/classes/myajax.php
Functions[]=ajaxFunc

The JavaScript:

//You can keep going with the args
var  dataSent = {arg1: "123", arg2: "456"};
$.ez(  'myajax::ajaxfunc', dataSent, function( ezp_data )
{
    if ( ezp_data.error_text )
    {
        alert( ezp_data.error_text );
    }
    else
    {
        alert( ezp_data.content );
    }
}

Custom PHP Class setup:

class MyAJAX extends ezjscServerFunctions
{
    public static function ajaxFunc( $args )
    {
        if ( isset( $args[0] ) && isset( $args[1] ) && isset( $args[2] ) && isset( $args[3] ) )
        {
            $var1 = htmlspecialchars( $args[0] );
            $var2 = htmlspecialchars( $args[1] );
            $var3 = htmlspecialchars( $args[2] );
            $var4 = htmlspecialchars( $args[3] );
        }
        else if ( $http->hasPostVariable( 'arg1' ) && $http->hasPostVariable( 'arg2' ) && $http->hasPostVariable( 'arg3' ) && $http->hasPostVariable( 'arg4' ) )
        {
            $var1 = htmlspecialchars( $http->postVariable( 'arg1') );
            $var2 = htmlspecialchars( $http->postVariable( 'arg2') );
            $var3 = htmlspecialchars( $http->postVariable( 'arg3') );
            $var4 = htmlspecialchars( $http->postVariable( 'arg4') );
        }
        else
        {
            return $error['args'];
        }
        return "ok";
    }
}

Use for debugging via HTTP:

http://dev.server.com/ezjscore/call/myajax::ajaxfunc::val1::val2::val3::val4

eZ Find + AJAX = Lightning Fast?

eZ Find definitely has its advantages and it would be a good idea to use eZ Find for large data sets. In my next post I will try to demystify the PHP implementation of an eZFind fetch.

Until then, get used to using the template code outlined in Ivo Lukac’s guide to using eZ Find. There you can also find the advantages and disadvantages of eZ Find.

Posted in eZ Publish, JavaScript, PHP | Leave a comment

Dynamic Forms with jQTransform

The Background

In a recent project I needed to get a basic form working. It took some effort to get a custom registration form plugged in to eZ Publish but I’m still very new to the system so I can’t complain much. Once the custom registration extension was coded and the form was tested in its unstyled state, it was time to add the styling. I was handed static HTML from the designer (not me in this case) and needed to get the live version working quickly as the project was coming to an end. So I stayed on course religiously with the design specs given to me and used the jQTransform plugin for jQuery. It takes your forms and styles them. Very swanky and neat looking. However, some things to note about jQTransform:

  • Bad keyboard support for form fields including drop down menus (tabbing and arrow interaction)
  • User Interface needs work (i.e. Users expect checkboxes to be selected with spacebar as opposed to enter.. what about both?)
  • Does not handle the case of showing/hiding form elements depending on selection of other form elements out-of-the-box

This last item I found to be quite discouraging.

In hindsight it wasn’t very complicated at all. It was a 3 state form:

  • show no optional fields (initial state)
  • show the Employee’s fields (hide Contractor fields)
  • show the Contractor’s fields (hide Employee fields)

State 1:

form_state1

State 2:

form_state3

State 3:

form_state2

Easy right? Just add jQuery show/hide!

WRONG

The plugin doesn’t do too bad with the regular text line inputs but the problem I ran into is the plugin changes the HTML on page load. Actually, it appends an entirely new set of elements to the HTML and hides the original <select> block. Not only that, but other classes were added/removed on the fly when a user clicks the drop-down menu. So essentially it’s doing some fancy dancy manipulation to get the fields to be styled like you want it. So instead of trying to actually style the <select> tags it’s replacing those with <ul>/<li> tags and keeping track of what’s selected to get around that issue of no styling support for the <select> tag.

The Fix

The reason I could not simply use my own show/hide code to control the simple behavior was that the plugin had hijacked control of the elements in question, namely the click handler for the <a> tags within <ul>/<li> tags. Instead of fighting for control of the <a> click handler I had to make the plugin give that control back to me. Thus by adding an ID to the HTML and a couple function calls I was back in control of my world.

Hacking jqTransform

Here’s a snippit of the jqTransform code starting around line 250, but more specifically within the Select section. I gave my custom code top priority in the handler as it looked for the custom ID’s I set up. Not that pretty, but it’ll do.

/***************************
  Select
 ***************************/
$.fn.jqTransSelect = function(){
    ...
    ...
	/* Now add the html for the select */
	$wrapper.prepend('<div><span></span><a href="#"></a></div><ul></ul>');
	var $ul = $('ul', $wrapper).css('width',$select.width()).hide();
	/* Now we add the options */
	$('option', this).each(function(i){...});

	/* Add click handler to the a */
	$ul.find('a').click(function(){
		//Custom behavior
		var thisID = $(this).attr('id');
		if (thisID == "emp-fields"){showEmployeeFields();}
		else if(thisID == "con-fields"){showContractorFields();}
                ...
                ...
        });

My jQuery Code

I placed this code outside of the jqTransform code and in a separate JS file. The reason for the $(document).ready part WITHIN these functions is for insurance. In fact I saw some funky stuff happening when I first decided to leave it out.

function showEmployeeFields(){
	$(document).ready(function(){
		$("#emp").show();
		$("#con1").hide();
		$("#con2").hide();
	});
}
function showContractorFields(){
	$(document).ready(function(){
		$("#emp").hide();
		$("#con1").show();
		$("#con2").show();
	});
}

The HTML

<div>
     <label for="user_type">I Am A :</label>
     <select tabindex="4" name="user_type">
          <option id="selected_user_type">* Please Select</option>
          <option id="emp-fields" value="Internal Staff">Employee</option>
          <option id="con-fields" value="External Contractor">Vendor</option>
     </select>
</div>

<!-- Additional fields for Contractors -->
<div id="con1"><input type="text" name="sponsor_name" size="20"value="* Sponsor's Name"/></div>
<div id="con2"><input type="text" name="sponsor_email_address" size="20" value="* Sponsor's E-mail"/></div>

<!-- Additional fields for Emplopyees -->
<div id="emp">
 <select name="position">
 <option value="196">Administrative Assistant</option>
 <option value="194">Manager</option>
 <option value="195">Senior Manager</option>
 </select>
</div>

This could have been done differently but this is just a simple modification to let a user take back some control that’s lost when using a heavy plugin such as jqTransform.

Posted in JavaScript | 2 Comments

A new beginning

My current work as a web developer has landed me in the middle of an Open Source CMS I never heard of (there are literally hundreds out there) called eZ Publish. I was placed on a project after 3 days or so of training on the system and was quickly on my way to creating a website backed by a solid CMS

Coding with eZ  Publish

The most coding I have done so far in the system has been with CSS/HTML/JavaScript and their Template Code (based on Smarty). I have barely touched PHP so far in the two months I have used eZ Publish, but it is a great system for plugging in reusable PHP (extensions and modules).  For example a fellow developer and I created a business locator using Google Maps (JavaScript) and the eZ CMS. We stored the businesses and their addresses as well as the latitude/longitude  as one “object” and then the user could find the closest business to them. The other portion of PHP I had to use more recently was to create a custom user registration form.

I haven’t investigated the eZ Publish API Documentation for PHP yet but it is definitely on my list of things to do before I try to take the eZ Publish developer exam. It seems complete at a first glance and uses doxygen, a great sign.

I’ll admit, this system is not for the faint of heart but if you like learning new systems and like to get your hands dirty, this is the place. It is a highly customizable system for the developer but easy enough for a non-techie to make content contributions. As a developer there are lots of little things to remember when building a custom website backed by a CMS such as this. It will take months to learn and I recommend learning PHP while you’re at it.

Posted in eZ Publish | Leave a comment