While doing some research about the autoloading features in PHP it soon became clear that the build in support aka a non parameterized call to spl_autoload_register(); had little to offer. What this call does is tell PHP that we want to use the build in spl_autoload function for autoloading. While this is nice in theory, in practice this default implementation falls way to short.

What bothered me the most was the lack of build in support for namespaces. The kind of support where a namespace represents the folder where the class file can be found.

Luckily PHP gives us a way to define our own autoload function which will be called upon instantiating an object from an unknown class. So I wrote a simple implementation for a fully functional autoloader with support for namespaces on both Linux and Windows.


function autoload_class ( $namespace_class ){

  // Adapt to OS. Windows uses '\' as directory separator, linux uses '/'
  $path_file = str_replace( '\\', DIRECTORY_SEPARATOR, $namespace_class );

  // Get the autoload extentions in an array
  $autoload_extensions = explode( ',', spl_autoload_extensions() );

  // Loop over the extensions and load accordingly
  foreach( $autoload_extensions as $autoload_extension ){
      include_once( $path_file . $autoload_extension );
  }

}

// Setting the path (I use linux) so our includes work.
set_include_path( get_include_path() . PATH_SEPARATOR . './' );

// Only try to autoload files with this extension(s)
spl_autoload_extensions( '.php' );

// Register our autoload_class function as the spl_autoload implementation to use
spl_autoload_register( 'autoload_class' );

Now if we try to instantiate an object of which the class file is not yet loaded, PHP will call our autoload function and automagically load our class file.

// Instantiating SomeObject from the class NamespaceName\SomeClass located in the file NamespaceName/SomeClass.php
$SomeOject = new NamespaceName\SomeClass();

Cheers.

{ 0 comments }

Whoa, what a title. Don’t be discouraged though, this post is here to make it (look) easy.

The first thing to know is how to construct a SOAP client in PHP.

Since PHP5 SOAP is baked in natively so this should be fairly easy right? Right.

$client = new SoapClient($wsdl, $soapclient_options);

The SoapClient constructor needs 2 arguments for our example to work. A string variable containing the URL of our webservice’s  WSDL and an array with all of our SOAP options.

The WSDL url:

$wsdl = 'https://api.url.com/i_am_a_wsdl.wsdl' ;

The SOAP options:

$soapclient_options = array();
$soapclient_options['login'] = 'your_http_login';
$soapclient_options['password'] = 'your_http_password';
$soapclient_options['local_cert'] = '/path/to/the/certificate/cert.pem';

There, done, finished, ready to go ! Nope, Chuck Testa! I mean Nope, not yet.

The SOAP client will not use the http login and password options for the WSDL retrieval, it only uses these for the transmission of SOAP messages. We need to change the $wsdl variable so that it contains proper HTTP auth credentials.

$wsdl = 'https://' . $soapclient_options['login'] . ':' . $soapclient_options['password'] . '@api.url.com/i_am_a_wsdl.wsdl' ;

There, looking good like this. Now let’s test this thing!

try {
  $our_return_string = $client->DoSomething(); //returns a string
echo $our_return_string;
} catch (SoapFault $exception) {
echo $exception;
}

This is how easy it is to use a PHP SOAP client in WSDL mode over SSL with a private key :)

{ 0 comments }

Why small businesses need pentests too

November 10, 2011

1. They ARE important A common misconception I have heard dozens of times from different kind of small business owners is that they think they’re not important enough. This phrase I have heard many times over and over again: “Why would anyone want to hack me? I don’t have any valuable information”. WRONG. Customer information, [...]

Read the full article →

My .vimrc and vim plugins

November 10, 2011

My .vimrc . Very simple yet super effective ( especially for a web developer) set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cindent “Setting how the statusline must look set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%03.3b]\ [HEX=\%02.2B]\ [POS=%04l,%04v][%p%%]\ [LEN=%L] “Always show the statusline set laststatus=2 “Make working with tabs more usefull(this calls a script defined further in this file) set [...]

Read the full article →

Why I use Vim

November 9, 2011

1. I can do everything with only my keyboard All editing, copy-paste, navigation, etc are done with the keyboard. No time wasted having to grab a mouse and target GUI elements. This not only saves me a lot of time, it also reduces what I call “context switching” . In computer science a context switch [...]

Read the full article →