yesJames.com
ahuh… sure… what ever you say…


Yubikey SSL certificate verification in PHP for Windows

Posted in programming,Software by james on October 21st, 2011

If you’ve ever tried connecting to a remove service or server using SSL from your server side PHP script running on a Windows Server, you may very well have encountered the condition that SSL certificate of the remote server cannot be verified. THe problem occurs when you have the CURLOPT_SSL_VERIFY_PEER curl option set to true.

The error looks like this:

 error:14090086
 SSL routines:SSL3_GET_SERVER_CERTIFICATE
 certificate verify failed

Well, the problem has a very simple solution. CURL cannot talk directly to the Windows certificate repository, so you have to give it a file to validate the Trusted Root Certificate Authority against.

Follow these steps to export your Trusted Root Certificate Authority CA certs:

  1. From the windows start menu, run the command “mmc.exe” to launch the Microsoft Management Console;
  2. From the MMC, choose “File” > “Add/Remove Snap-in…” from the main menu;
  3. Add the “Certificates” snap-in; When asked which certificates you want to manage, select “Computer Account” and “Local computer“;
  4. from the console root, navigate to “Certificates (Local Computer)” > “Trusted Root Certification Authorities” > “Certificates“;
    Trusted Root Certificates
  5. * Select all of the certificates (CTRL+A) and select “Action” > “All tasks” > “Export…” from the MMC main menu;
  6. Follow the wizard to export the certificate(s) to the Cryptographic Message Syntax Standard – PKCS#7 (,pb7 file) to a file called “TrustedRootCAs.pb7“;
  7. Open a command prompt (“cmd.exe“) and navigate to the folder containing the PB7 file you just created;
  8. Run openSSL.exe (you may need to add the path to this program to the Windows PATH environment variable) to convert the file to text using the following command:
    1. c:\php\extras\openssl\openssl.exe pkcs7 -inform DER -in TrustedRootCAs.pb7 -print_certs -text > TrustedRootCAs.certs
  9. Then, in your PHP application, add the following line BEFORE you call curl_exec():
    1. curl_setopt ($ch, CURLOPT_CAINFO, ‘C:\PHP\extras\openssl\TrustedRootCAs.certs’)

That’s about all you should need to get things rolling.

Remember, you don’t necessarily have to export all of the CA’s. In the case of Yubikey, the authentication API server uses a certificate issued by GoDaddy, so you only need export that single on if you prefer not to have to keep this file updated every time ROOT CA certificates are updated by the various providers.

Delphi: Declaring and initialising constant arrays

Posted in Delphi,programming,Software by james on February 16th, 2009

In Delphi, arrays are, put simply, a list of values contained within a common variable, that are accessible by their index within that list.

But what if you don’t want your list of values to be able to be altered at run-time? You need them to be contained in a constant rather than a variable.

Example: to declare an array of abbreviated week names:

const
  Days: Array[0..6] of String = (
      'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
  );

You could then access each name by the index of the day of the week within the Days constant.

i := DayOfWeek(Date); //returns an integer 0-6
ShowMessage('Today is  ' + Days[i]);

Another common use for constant arrays is to describe the values in an ENUM type:

type
  TStatusCodes = [
    scUnknown, scActive, scPending,
    scDisabled, scSuspended
  ];
const
  StatusString = Array[TStatusCodes] of String = (
    'Unknown', 'Active', 'Pending',
    'Disabled', 'Suspended'
  );

Then, the description string for each status code becomes available via the ordinal value of the status code itself:

ShowMessage('Your account is '+ StatusString[scActive]);

Would show a message box with the text “Your account is Active“. Of course in the real world, the status code enumeration value would come from a user object or some such similar method (like “function getAccountStatus(): TStatusCode;” perhaps).

Firefox compatible xPath functions in JavaScript

Posted in Ajax,JavaScript,programming by james on November 19th, 2008

There are a lot of quirks between different browsers, in particular though are the differences in the way each browser handles the DOM in JavaScript.

Internet Explorer, for the most part, implements MSXml 4.0 or higher. However that’s (obviously) a Microsoft technology, and the standard implementation doesn’t support XPath in the DOM.

In particular the selectNodes() and selectSingleNode() functions are of particular use when manipulating XML or the DOM. Using these functions you can parse an XPath expression and get a nodelist of elements in your DOM that match the expression.

(more…)

howto: Format numbers in JavaScript

Posted in JavaScript,programming,Software by james on November 19th, 2008

Formatting Numbers

Numeric formatting in JavaScript can be very tedious. From handling non-numeric characters, to multiple-currency symbols, to alternate negative display methods. The functions below should be valuable in assisting to format numbers and currencies of all types.

(more…)

Delphi: Minimising child forms in an SDI application

Posted in Delphi,programming,Software by james on November 19th, 2008

In an SDI application (Single Document Interface), the main form acs as the “container” for the entire application. This means that child forms, are exactly that – children of the application. The long and short of which, means that when you minimise the main form of the application, all forms are hidden. In addition to which, when you minimise a child form of the application, it doesn’t minimise to the task bar, instead it minimises to the application desktop. This also has the side effect that only the main application form itself has a task bar button.

So, How do I change this behaviour?

(more…)

JavaScript Trim() function

Posted in JavaScript,programming by james on March 20th, 2008

Trim(), is one function that we all use almost every day in most languages, whether it’s a core function of the language you’re using or a member (or prototype in JavaScript) function of the String object itself (as in C#), it’s invaluable for developers.

Unfortunately JavaScript doesn’t have this sort of thing built in, so here’s how to implement your own trim() functions (as well as ltrim() and rtrim()) in various ways depending on the level of backward compatibility you require.

(more…)

howto: GET a URL parameter in JavaScript

Posted in JavaScript,programming by james on February 11th, 2008

A useful little piece of JavaScript for retrieving a URL parameter (Query String) from your page. If a requested parameter doesn’t exist it will return an empty string rather than a NULL object. It will also handle the inclusion of anchor tags in the URL and exclude them from possible results.

e.g. http://www.yesjames.com/test.php?myparam=justatest
calling “gerURLParam(‘myparam’) will return “justatest”

function getURLParam(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var pattern = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(pattern);
  var aResult = regex.exec(window.location.href);
  if ( aResult == null ) {
    return "";
  } else {
    return aResult[1];
  }
}