Friday, 16 December 2011

using cite in blockquote

The blockquote tag has had a cite property for quite a while.

Unfortunately this is not used.
An improvement is to append it at the end of the blockquote by using the ::after selector:

blockquote[cite]::after {
 content: "source: " attr(cite);
 display: block;
 border-top: solid 1px grey;
 margin-top: 1em;
 text-align: center;
}

And we get something like so:
If the cite attribute is present, it must be a valid URL
potentially surrounded by spaces.

Unfortunately this generated text cannot be selected in Firefox, it is a long-standing bug.
It can be selected in Opera.
It cannot be selected in Chrome.

... IE9? Doesn't even generate the content.

Thursday, 15 December 2011

HTML5 + JS = validation of input file type and size

With HTML5's File API one can now access the files in an input field. Readonly, of course - security oblige.

So besides using the accept attribute to improve usability in standard-friendly browsers (grumble grumble), one can now go another step further and help the user with some client-side validation.

This feature is present in updated browsers, with the unsurprising exception of IE.


<input type="file" multiple accept="application/pdf" name="documents">
<input type="file"          accept="image/*" name="photo">
<input type="file" multiple accept="text/csv" name="logs">

By using the .files property of the <input type="file"> one can iterate over the uploaded files for the input (see the multiple attribute) and do some validation.
The File has a name and a lastModified date, but it inherits from Blob and thus also has a size and a type.
One could probably use the type instead of the name, to better validate the uploaded file against the input's accept attribute, but we'll leave that for another time.

Let's do some client-side validation:

// HERE BE DRAGONS:
// - Untested, typos and bugs may exist;
// - Does address "foo/*";
// - str.endsWith() is an extension of string that is not normally present otherwise - implement or change the code;
// - Using JQuery is not necessary, this code uses it for simplicity;
// - Fus ro DAH!
$(document).ready(function{
  $("input[accept]").change(function({
    var accept = $(this).attr("accept");
    var MAXFILESIZE = 4000000; // Load this from somewhere. (bytes)
    var extension;
    if( accept === "application/pdf" ) {
      extension = ".pdf";
    } else if( accept === "text/csv") {
      extension = ".csv";
    } else /*if( accept.startsWith("image/")) {
      extension = "." + accept.substring(7);
      if(extension === ".*") {
        extension = "";
      }
    } else*/ {
      return true;
    }
    
    var files = $(this).files;
    if(!files) {
      // WHY U NO HTML5??
      var val = §(this).val();
      if( val && !val.endsWith(extension) ) {
        $(this).val("");
        return false;
      }
    } else {
      for(int i=0; i < files.length; i++) {
        var file = files[i];
        if(!file.name.endsWith(extension)
          && !confirm(
            "File extension is not " + extension + ": " + file.name
            + " \nUpload file anyway?"
            )
          || file.size > MAXFILESIZE
          && confirm(
            "File is bigger than " + (MAXFILESIZE/1024)
            + "KiB and will fail to upload: " + file.name
            + " \nCancel submission?"
            )
          ) {
        }
      }
    }
  });
});

As a last note, never trust user input means one must always do server-side validation.
But also doing it on the client side means better usability and a more pleasant experience - and serving the users is our purpose... most of the times, at least. :)

Thursday, 8 September 2011

Wednesday, 31 August 2011

Machine-independent time: DateTime.UtcNow

DateTime.UtcNow expresses machine-independent time, as opposed to DateTime.Now which expresses local time.

Tuesday, 21 June 2011

c# wishlist: quick check for empty strings

Any object can be null-coallesced using the fancy operator ??.
But alas!, empty strings are not null. Those pesky things keep getting in the way, particularly in order to avoid NullReferenceExceptions.

So, instead of str ?? "some default" or if(str != null) { }, like any other object... the code gets packed full of lengthy if(string.IsNullOrEmpty(str)) { }.

On a happy note though, I'm back to programming - YAY!

Saturday, 26 March 2011

recovering sa password on MS SQL Server

Members of the Windows Administrators group now have access to SQL Server when SQL Server is in started in single-user mode, also known as "maintenance mode".

Simply restart the server in maintenance mode, edit sa's password, and then restart again in normal mode.

To start in maintenance mode, send the -m parameter when initializing the server:
  1. Open the Configuration Manager in SQL Server 2008/Configuration;
  2. Edit the properties of the server instance, adding -m to the Startup Parameters option (user ; to separate options, be careful not to add spaces)
  3. Restart the server

To change back to normal mode, remove the -m parameter and restart the server.

Via Raul Garcia.

Monday, 14 March 2011

testing ruby RegExps

There are several good tools to quickly edit regular expressions and check them against a test string.

I tried Rubular.
It worked perfectly, so I looked no further.

exposing localhost websites, MacOS

  • Open Sharing in System Preferences;
  • Select the Services tab;
  • Select Web Sharing — this will allow internal webpages to be accessed externally, as per the description of the setting;
  • See your address appear in that page, test it.

Simple!
And now I can use VMWare to test my rails app in IE (that wretched browser...) by just visiting http://10.1.1.9:3000.

Via MacInstruct.

Tuesday, 1 March 2011

add PostGIS functionality to a db

psql -d test_db -f /usr/local/Cellar/postgis/1.5.2/share/postgis/postgis.sql
psql -d test_db -f /usr/local/Cellar/postgis/1.5.2/share/postgis/spatial_ref_sys.sql

Monday, 28 February 2011

Locking screen

Setting screen to lock when coming back from idle states:
  • Open Security in System Preferences
  • Select the General tab
  • Activate Require password after sleep or screensaver begins

Setting a shortcut to lock the screen:
  • Open Exposé & Spaces in System Preferences
  • Select the Exposé tab
  • Select one of the shortcuts to be Put display to sleep or Start screen saver

bottom-left corner set for putting display to sleep

Via MacTips

Why MacBook comes from source with lax security settings is beyond my understanding.
A fresh install will have a single user that automatically logs in, and the user's password is only necessary for administration tasks.
But there is a lot more to security than avoiding tampering with system settings. There is identity protection, there is data protection, there is privacy, there is...


The only explanation I find is that the emphasis was placed on the versatility and pleasantry of use that is being ready to start working within a dozen seconds of turning the laptop on.
But for a laptop, on which one expects the average user to take his sensitive data anywhere (be it internal company documents on a work computer, or personal files on a leisure computer)… isn't the vulnerability of this data a much graver concern than the minor inconvenience of having to input a password?
Why would that data be vulnerable without even this minimal protection?


In this day and age of digital identity and privacy management I would expect [much!] better from any company, product or service.

PostgreSQL + PostGIS

  • brew install postgres
  • add the following to .bash_profile:
    export PATH=$PATH:/usr/local/pgsql/bin
    export PGDATA=/usr/local/pgsql/data
  • mkdir /usr/local/pgsql
    mkdir /usr/local/pgsql/data
  • brew install postgis
  • restart the console, to load the new path & etc
  • pg_ctl initdb
  • createdb test
Is it working?
It's working! <3