PHP overriding “Soapaction” header

Oh no, SOAP again. I hate SOAP…. but making shit work is everyone’s job. So, here i go again. This time i need (don’t ask why) to change the Soapaction header. It turns out to be quite simple with the bundled Soap extension of PHP. Just need to extend the SoapClient and override the  __doRequest function. The code:

<?

class CustomSoapClient extends SoapClient {
  function __doRequest($request, $location, $action, $version, $one_way = 0) {
    $action = 'my_custom_soap_action'; // override soapaction here
    return parent::__doRequest($request, $location, $action, 
                               $version, $one_way);
  }
}


$options = array('exceptions'=>true,
                 'trace'=>1,
                 'cache_wsdl'=>WSDL_CACHE_NONE
                 );
            
$client = new CustomSoapClient('my.wsdl', $options);

try {
  $input = new stdClass();
  $input->property  = "example";
  $input->property2 = "example2";
   
  $response = $client->helloWorld($input);   
   
} catch (Exception $e) {
  echo "\n";
  echo 'Caught exception: ',  $e->getMessage(), "\n";
  echo "\n";
  echo "REQUEST:\n" . $client->__getLastRequestHeaders() .
                      $client->__getLastRequest() . "\n";
  echo "\n";
  echo "RESPONSE:\n" . $client->__getLastResponseHeaders() . 
                       $client->__getLastResponse() . "\n";   
}			
			
?>

SSH port forwarding

Isn’t SSH great? It’s secure and it can do lots of cool things, as providing access to services to local machines that are only available to the remote machines (that you can connect through SSH). This is called port forwarding.

Windows with Putty

So, you are on your local windows box and got ssh access to a remote machine, let’s call it “Remote” and from there you can access a service in another machine, let’s call it “Far”. The problem is that from your local windows box you can’t directly access “Far” (most times because the good people of network, and their strong sense of security…, vpn’s, etc).

So:
Localbox -> Remote (ok)
Localbox -> Far (not ok)
Localbox -> Remote -> Far (ok)

and it would be nice to test the service (lets say HTTP to exemplify) running on Far with your nice Localbox browser, instead of the console based Lynx browser that you have on Remote.

Enter the black magic of ssh port forwarding. With Putty (the SSH client for Windows) it’s pretty easy. Just open your connection normally, but before pressing the Open button, go to Connection -> SSH -> Tunnels:

The source port will be the port on your Localbox, i usually put there the localhost ip:port combination (127.0.0.1:80).  You should check with “netstat -an” if you have this free, if there is some service (IIS, Apache) already running on this ip:port stop it. The destination is the Far ip:port that you want to get access (far_ip:80). Click “Add”.  And open the connection normally and login to the Remote console. On the Localbox check again with “netstat -an” and you should have an entry like this

TCP    127.0.0.1:80           0.0.0.0:0              LISTENING

And there you go! You have an open tunnel from Localbox to Far. Now just open the browser on localbox and point it to 127.0.0.1, your request is being sent to Far. If you need an hostname to access the service correctly just put it on the hosts file:

127.0.0.1 hostname

Linux

Pretty easy… just with the ssh -L switch.
-L localport:foreig_ip:foreign_port

To make this clear, an example. On my production server i run a MySQL server instance, but it only listens to localhost (127.0.0.1) but i want to use a GUI to manage it. I have the GUI in my linux box, so it would be impossible to connect the GUI to the MySQL server… not with ssh around…

ssh user@mysqlhost -L 3306:127.0.0.1:3306

after the ssh connection is made i can access the MySQL server as if it was running on my Linux localhost. We can even check with netstat.

netstat -an | grep 3306 | grep LISTEN

it should get something like:
tcp    0    0    127.0.0.1:3306    0.0.0.0:*    LISTEN
tcp6    0    0    ::1:3306    :::*    LISTEN

There, a no-brainer sometimes very useful.

(My) Unix useful CLI commands

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Remove the rotating bit in video metadata

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Video

Remove the rotating bit in video metadata with ffmpeg

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Remove the rotating bit in video metadata

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Remove the rotating bit in video metadata

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz

Deep

This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Video

Remove rotation metadata from video with FFMPEG

ffmpeg -i input.mp4 -c copy -metadata:s:v:0 rotate=0 output.mp4

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.

JS setTimeout variable passing, accessing this and scope

I’m really not a JS ninja, but as the defacto language of the Web, it’s rather a unique day if i don’t read or write some javascript code and this a pattern that is well worth to know.

First example, a simple var:

function myFunction() {
    var myVar = 'test';
    setTimeout('alert(myVar)', 1000);
};

myFunction();

fails with myVar is not defined error, because setTimout works on the window object (the top level object for browsers), and there is no myVar registered as a global var (window.myVar is undefined).

You can easily work this out with a closure:

function myFunction() {
    var myVar = 'test';
    setTimeout(function() { alert(myVar) }, 1000);
};

myFunction();

simply a closure is a function declared inside other function. We have an anonymous function as the first argument of setTimeout inside myFunction, so it’s a closure. The magic of closures is that they keep state (the local variables remain accessible) after the container function has exited.

The curious case of this keyword… take the code:

function myClass () {
    this.myVar = 'test';

    this.myFunction = function() {
        setTimeout(function() {alert(this.myVar)}, 1000);
    }
}

myobj = new myClass();
myobj.myFunction();

and boum! undefined pops-up, WTF? after messing around with call, apply and even with i could only make it work assigning this to a local variable:

function myClass () {
    var self   = this;
    this.myVar = 'test';

    this.myFunction = function() {
        setTimeout(function() {alert(self.myVar)}, 1000);
    }
}

myobj = new myClass();
myobj.myFunction();

this of course is a non elegant solution, so please be my guest of posting a better solution to this issue.