How to: Contact your representative email form

So after much frustration with plus(+) signs I finally have a working, unfortunately Javascript dependent, setup for the popular “contact your representative” forms.

These forms are used by online campaigns to put public pressure on officials. They do so by making it as easy as possible for people to send prewritten emails (with their own modifications if at all possible) to them.

Now you can’t just have people give out their email and send it in their stead as it would, very quickly, be caught in the target mail servers spam filter. You can’t spoof their emails either (thank you SPF records!) without getting thrown in the spam. Besides, such an action would lack legitimacy as there’s no way of knowing how much fakery you are doing.

So the solution is to get people to either copy/paste the email into their own client … or even better have it open by itself.

Mailto: to the rescue!

HTML supports the mailto: URI for the <a> element.

<a href="mailto:example@example.com?subject=Test%20Subject&body=Test%20body">Send email</a>

As you can see both the subject and the body parameters need to be url encoded!

Make it editable!

The basic problem with the previous solution, while simple and highly functional (no javascript!) is that the message can’t be edited.

If the internet is to believed there exsists another option. Mailto Forms!

<form action="mailto:example@email.com">
  <label for="subject">Subject</label>
  <input id="subject" name="subject" type="text" value="Suggested subject" />

  <label for="body">Email body</label>
  <textarea id="body" name="body" rows="25" >
    Suggested body
  </textarea>

  <button type="submit" value="submit">Submit</button>
</form>

But this solution is only partially functional!

In some email clients the spaces in the subject and title will be replaced by plus(+) signs and no amount of playing around with enctype will help.
Even url encoding the string before submitting it will decode it than replace spaces with plus signs. I know … weird.

Half way, best way!

So my solution is to take the form, use javascript (#sadFace) and compile it into an <a> element just as it is clicked.

<form>
  <label for="subject">Subject</label>
  <input id="subject" name="subject" type="text" value="Suggested subject" />

  <label for="body">Email body</label>
  <textarea id="body" name="body" rows="25" >
    Suggested body
  </textarea>

  <a id="email-submit" class="button" href="javascript:{}" onclick="sendEmail();">Send email</a>
</form>

Now add just a little javascript:

function sendEmail() {
  var email = 'example@example.com';
  var subject = encodeURIComponent(jQuery('input#subject').val());
  var body = encodeURIComponent(jQuery('#body').val());
  var line = 	"mailto:"+email+"?subject="+subject+"&body="+body;
  jQuery('#email-submit').attr('href', line);
}

I used jQuery because I was lazy – it could easily have been done without it.

This will allow users to modify the subject and body and send it with their own clients.

Using MS SQL (dynamic ports) in PHP 7 with ODBC PDO

So I’ve just spent a painful number of hours to figure out how to connect to a Microsoft SQL Database (MS SQL) that’s using dynamic ports and named instances (if you’ve got a feeling I’m definitely not a M$ guy … you would be right).

Because I didn’t find a proper working how to on doing this and had to resort to reading mailinglist archives and far, far too many stackoverflow threads I’m posting everything here before I do my best to forget it.

Note: I use Arch locally and the server had Ubuntu, so this has been tested on both.

Step 1: Install required packages

# Arch
sudo pacman -S odbc php-odbc freetds

# Ubuntu
sudo apt install php7.0-odbc tdsodbc unixodbc unixodbc-dev freetds-dev freetds-bin tdsodbc

Step 2: Enable extensions in php.ini

Uncomment extension=pdo_odbc.so

Make sure to restart PHP-FPM after you’re done.

Step 3: Configure ODBC and FreeTDS

Edit odbcinst.ini

sudo nano /etc/odbcinst.ini
[FreeTDS] # This is your identifier! You'll need it in odbc.ini
Description = FreeTDS Driver v0.91
Driver = /usr/lib/libtdsodbc.so # Arch
# Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so # Ubuntu
fileusage=1
dontdlclose=1
UsageCount=1

If you’re not on arch you’ll have to find the location of libtdsodbc.so for your distro, or you can just use locate to find it.

Edit odbc.ini

sudo nano /etc/odbc.ini
[mssqlconn] # This is important - you'll be refering to your DB with this
Description = MSSQL Server
Driver = FreeTDS # Same as identifier in odbcinst.ini
Database = NAME_OF_DATABASE # Not instance, database within the instance
ServerName = mssqlconn # This  is the same identifier as in freetds.conf
TDS_Version = 8.0

 

Edit freetds.conf

sudo nano /etc/freetds/freetds.conf

Add this to the end:

[mssqlconn]
host = # HOSTNAME or IP of your server
instance = # Name of the INSTANCE (not DB)
tds version = 8.0
client charset = UTF-8 # See below
text size = 64512 # You're gonna need to increase this if you're working with binary, image or large text fields.

Step 4: Test connection

osql -S mssqlconn -U 'username' -P 'password'

If this works, you’re good to go!

Step 5: Connect from PHP

$serverName = 'mssqlconn';
$dbName = 'YourDb';

$pdo = new PDO(sprintf("odbc:DRIVER=FreeTDS;SERVERNAME=%s;DATABASE=%s", $serverName, $dbName), $username, $pass);

 

That’s it! The rest is standard PDO stuff.

Problems I faced

Images in database

So the job I was doing required me to extract images from an “image” field. Don’t ask.

This can’t (as far as I can tell) be done with PDO as you require binary mode for ODBC.

Solution:

$link = odbc_connect(
  sprintf(
    "DRIVER=%s;SERVERNAME=%s;DATABASE=%s",
    $odbc_driver,
    $odbc_name,
    $dbname
  ), 
  $user, 
  $pass
); 

$query = odbc_exec($link, "YOUR QUERY");
odbc_binmode($query, ODBC_BINMODE_RETURN);

while (odbc_fetch_row($query)) {
    $fieldValue = odbc_result($query, 'FIELD_NAME');
}

 

Unicode

This setup isn’t too fond of Unicode chars. The setting in freetds.conf seems to work most of the time, but I’ve found, on multiple systems that the driver will break and stop returning Unicode chars. This requires a PHP-FPM restart.

I should also note that on some machines it does not return a Unicode char (i.e. “č”) but a Unicode code (i.e. “\u010D”). This was less of a problem for me as all data from MSSQL was passed over a JSON API and json_decode will properly decode the codes into chars.

Missing data/truncated fields

The drive has a default maximum length of 64512 chars. If you need more than that (binary, text fields etc.) increase the setting in freetds.conf.

Kako naložiti AdBlock?

Adblock je program za blokiranje oglasov na spletu. Program, ki ga naložite v vaš brskalnik (“browser”) deluje tako da prepoznava in blokira oglase še preden se naložijo na vaš računalnik.

Tako vam prihrani ne le mentalni napor ignoriranja oglasov, ampak tudi pospeši nalaganje strani, zmanjša obremenitev računalnika in (!) pomaga varovati vašo zasebnost.

AdBlock programov je več. Včasih popularnega Adblock Plus* je sedaj nadomestil hitrejši uBlock origin. Kako jih naložiti je pa odvisno od tipa vašega brskalnika.
Continue reading

Facebook chat is locking you in.

Like many I use Facebook Chat not because I like it but because I have no other choice. I know, “you can use X, it’s way better”. But all of the alternatives have one fatal flaw: People don’t use them*.

And this is exactly what Facebook wants.

So, now they have the user base … and they are determined not to lose it.
Continue reading

Effective Transparency: What transparency isn’t, but should be

The veil of transparency has long been used and abused to legitimize otherwise undemocratic decisions. When confronted with criticism that some legislative process was exclusionary, it is often defended with “That’s not correct, all information was posted on the public bulletin board.” Yes it may as well have been, but the fact that that board is located in an unused hallway at the back of the Town Hall, where no one ever visits, does not mitigate the fact that the process was exclusionary. Continue reading

Vaccinations: Don’t blame the parents

I’ve been following the anti-vaxxer movement for a while now but the latest news headlines have prompted me to speak out.
This post is not intended to persuade you to vaccinate your kids, but to convince the “skeptics” community to stop making things worse.

Parents are not idiots for being taken advantage of.

The anti-vaxxer “movement” is fraud, plain and simple. Continue reading