Category: Development

If… Then… Else… In XSLT

If… Then… Else… operation in XSLT is actually Choose… When… Otherwise…

<xsl:choose>
  <xsl:when test="condition"> ... do stuff ... </xsl:when>
  <xsl:when test="condition"> ... do other stuff ... </xsl:when>
  <xsl:otherwise> ... else do other stuff ... </xsl:otherwise>
</xsl:choose>

Best Way to Call a WCF Web Service From PHP

I struggled with this for an hour or so last week in trying to get one of our PHP applications to call a WCF .SVC authentication web service for login. The issue is I am already quite familiar with calling .ASMX and .SVC web services from PHP using soap and PHP’s built-in SoapClient class. And, the information on this topic can be found all over the web with several examples. Here is a great write-up for using soap via PHP SoapClient. The trouble is, I have never had to call a WCF .SVC using a POST method with jSON from PHP before and the answer took a lot more digging.

First of all, I came across this article Three Ways to Make a POST Request from PHP which helped me decide I wanted to use PHP Curl as I already have it installed on my PHP server. Next, I had to find a basic envelope for calling the authentication web service and passing it 4 parameters (username, password, siteid, rememberuser). This thread on Stack Overflow gave me the answer. The rest was a piece of cake to convert the response from the service back to jSON to parse for whether the attribute LoggedIn was true or false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$username = 'blah@blah.com';
$password = 'blahblah';
$url = "https://asp-server/services/AuthenticationService.svc/Login";
 
$transmitObject = array(
        'Username' => $username,
        'Password' => $password,
        'SiteId' => '3',
        'RememberUser' => 'false'
        );
 
$jsonObject = json_encode($transmitObject);
 
$options = array(
        CURLOPT_HTTPHEADER => array(
        "Content-Type:application/json; charset=utf-8",
        "Content-Length:".strlen($jsonObject)));
 
$defaults = array( 
         CURLOPT_POST => 1, 
         CURLOPT_HEADER => 0, 
         CURLOPT_URL => $url, 
         CURLOPT_FRESH_CONNECT => 1, 
         CURLOPT_RETURNTRANSFER => 1, 
         CURLOPT_FORBID_REUSE => 1, 
         CURLOPT_TIMEOUT => 4, 
         CURLOPT_POSTFIELDS => $jsonObject
         );
 
$ch = curl_init(); 
curl_setopt_array($ch, ($options + $defaults)); 
$request = curl_exec($ch);
curl_close($ch); 
 
if($request->{'LoggedIn'} == 1){
        $user = 13;
}
else
        $user = 0;

How to Backup a Single Table in SQL

Have you ever wanted to run a quick backup on a single table prior to doing an insert, update, or delete within SQL Server? I like to use the “Select Into” command to select all data from the table I am modifying into a backup table so that I can quickly recover if need be. This prevents having to backup the entire database when you are only changing one table.

SQL statement to backup a single table.

SELECT * INTO [New_Table]
  FROM [Old_Table]

After you run your insert or update commands, and you can confirm your changes went well, you can easily delete the table created above to free up space.

I should also note this is not a backup strategy. It is a simple cover your ass to have all the data from one table preserved in a new table should the shit hit the fan on your update, insert, or delete statements.

Over-The-Internet Secure SQL Connection

Have you ever wanted to establish a secure encrypted connection between a local server and say a web server running SQL Server? I have and I wanted to do it without reconfiguring anything on my database server. Here’s how:

Prerequisites
Remote Microsoft SQL Database Server

  1. SQL Management Studio
  2. Allows Remotes connections over a port (Default Port: 1433)
  3. Is on the same subnet/network as the SSH Server.

Local Windows Machine

  1. SQL Management Studio
  2. Cygwin Installed
  3. Is your laptop, local server, etc. This is just the machine with which you will connect to the SQL server over the internet using an encrypted SSH Tunnel.

SSH Server

  1. SSH Server Installed
  2. Allows Remote Connections
  3. Rinetd Installed
  4. Is on the same subnet/network as the SQL Server.
Getting Started
First of all, we have to lay down some ground rules. Our shop will run one dedicated Management server where all external access to the servers in that environment are accessed. The management server is the gatekeeper to everything else on that network including SSH access, port forwarding for remote desktop, etc. This allows us to only allow access through the switch or local machine firewalls to vulnerable ports such as 1433 (SQL) or 22 (SSH) or 3389 (Remote Desktop) to only the management server and not the world wide web.  Our Management SSH server is running basically 2 packages. sshd and rinetd.
I’m not going to get into configuring sshd or rinetd other than providing my specific config for port forwarding for SQL Management studio. Once you have this working, it can apply to remote desktop sessions, samba sharing, website access. Basically anything you want to go over an ssh tunnel that you don’t want opened to the world wide web can go over an SSH tunnel using Rinetd to forward.

Configure your SSH Server with Rinetd
1. Install rinetd

sudo apt-get install rinetd

2. Edit the rinetd.conf file

sudo emacs /etc/rinetd.conf

3. Add the following. You will need to edit for your specific environment.

# SQL Server Test
0.0.0.0 3409 10.210.23.12 1433
allow 127.0.0.1
allow 10.210.20.11

4. Notice that the first line has the following.

0.0.0.0 3409 10.210.23.12 1433 # (0.0.0.0) (port) (Local IP of SQL Server) (Port to connect to on SQL Server)

The first parameter is where do you want the remote session to come from. 0.0.0.0 means allow all assuming they can make a success ssh session. The second parameter is the local port on the SSH Server that you want to connect to. The third parameter is the Local IP of the SQL Server that you want to forward to. The fourth parameter is the port on the SQL server you want to connect to. 1433 is the default SQL port so we will use that.

For example if the following were your IP addresses for your SSH and SQL servers.

SQL IP: 192.168.0.10
SSH IP: 192.168.0.5

Your config would look like this.

# SQL Server Test
0.0.0.0 3409 192.168.0.10 1433
allow 127.0.0.1
allow 192.168.0.5

The second parameter is arbitrary. It is just a port that isn’t used by any other service. Generally anything over 3000 is open for use by other items. In the example above I used 3409.

Setup AutoSSH in Cygwin
Now, we can move on to installing AutoSSH within cygwin on your local machine to create a ssh config that will reestablish an SSH connection  if it drops. We will make an assumption that you’ve installed Cygwin before that you are familiar with installing packages, poking around in configs, etc. If you need to install cygwin, you can find it here.

1. Launch the setup.exe of Cygwin and walk through the installation until you get to the packages selection. Find AutoSSH and Open-SSH and select it to be installed and then finish installation.

2. Open a Cygwin prompt and enter the following command.

cygrunsrv -I AutoSSH -p /usr/bin/autossh.exe -a "-M 20000 -N -L 3409:your-ssh-server:3409 username@your-ssh-server" -e AUTOSSH_NTSERVICE=yes

The above command creates a Windows service that uses AutoSSH to create an SSH Tunnel to your SSH Server over the port you specified earlier.

3. Go to Start>Run. Then type services.msc and hit OK. This should bring up a window with all of your Windows Service. Find AutoSSH (The service you created with the command above) right click and select properties.

4. Make sure the settings look like the following 2 screenshots.

 

 

 

5. IMPORTANT! You will need to be sure to select “This Account” and select your username and enter your password rather than using Local System Account. Then select OK.

6. Now, you want to start the AutoSSH service by Right Clicking on it and selecting Start.

7. To test to make sure your tunnel is up and running. Open a Cygwin prompt and type the following command
netstat -an
If your AutoSSH service is working properly, you should see the following line.
TCP 127.0.0.1:3409  0.0.0.0:0 LISTENING
8. Finally, launch SQL Management Studio and try connecting to your remote SQL Server.
Note: You will need to be sure that you have a user with permissions to the database SQL server you are connecting. Be sure that the Servername is your localhost ip address of 127.0.0.1,(port) that you use earlier in the Windows Service creation command.
Then you should be connected to your SQL server over-the-internet and encrypted over an SSH tunnel.
This is a long one and I am happy to provide clarification where needed. Don’t hesitate to hit me on twitter @jdcarg or post a comment below. Thanks for reading!

Make a Donation, Buy Me a Beer!

Want to make a “Donate Now!” button on your wordpress site? Sign up for a Google Checkout account here. Don’t worry, it only takes 5-10 minutes to setup.

1. Once you login, go to the Tools tab and click on “Create a Buy Now button”.
2. Walk through creating your button following the information on the page. I also selected “Digital Good” under Describe Item.
3. Once you’ve entered all of your options, select “Create Button Code.”
4. Then, copy the html and login to your WordPress site.
5. Under the Appearance section select Widgets.

 

 

 

 

 

6. Drag the Text widget from the Available Widgets column to the Sidebar1 column.

 

 

7. Then type a title such as “Make a Donation” and then paste your Google Checkout button code into the text area and select Save and then close.

8. Go to the main page of your blog and you should see something like the following in the Sidebar of your site.

 

 

 

 

 

You can also just copy what I already done to my site and edit away! Make sure to substitue your Merchant ID for [[MerchID]] in the code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<form action="https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/[[MerchID]]" id="BB_BuyButtonForm" method="post" name="BB_BuyButtonForm" target="_top">
    <table cellpadding="5" cellspacing="0" width="1%">
        <tr>
            <td align="right" width="1%">
                <select name="item_selection_1">
                    <option value="1">$1.00 - Throw Me a Bone!</option>
                    <option value="2">$2.00 - Buy Me a Beer!</option>
                    <option value="3">$7.00 - Buy Me a Shot!</option>
                    <option value="4">$10.00 - Buy Me a 6-Pack!</option>
                </select>
                <input name="item_option_name_1" type="hidden" value="Throw Me a Bone!"/>
                <input name="item_option_price_1" type="hidden" value="1.0"/>
                <input name="item_option_description_1" type="hidden" value="Throw Me a Bone! - jdcargile.com"/>
                <input name="item_option_quantity_1" type="hidden" value="1"/>
                <input name="item_option_currency_1" type="hidden" value="USD"/>
                <input name="shopping-cart.item-options.items.item-1.digital-content.url" type="hidden" value="https://jdcargile.com"/>
                <input name="item_option_name_2" type="hidden" value="Buy Me a Beer!"/>
                <input name="item_option_price_2" type="hidden" value="2.0"/>
                <input name="item_option_description_2" type="hidden" value="Buy Me a Beer! - jdcargile.com"/>
                <input name="item_option_quantity_2" type="hidden" value="1"/>
                <input name="item_option_currency_2" type="hidden" value="USD"/>
                <input name="shopping-cart.item-options.items.item-2.digital-content.url" type="hidden" value="https://jdcargile.com"/>
                <input name="item_option_name_3" type="hidden" value="Buy Me a Shot!"/>
                <input name="item_option_price_3" type="hidden" value="7.0"/>
                <input name="item_option_description_3" type="hidden" value="Buy Me a Shot! - jdcargile.com"/>
                <input name="item_option_quantity_3" type="hidden" value="1"/>
                <input name="item_option_currency_3" type="hidden" value="USD"/>
                <input name="shopping-cart.item-options.items.item-3.digital-content.url" type="hidden" value="https://jdcargile.com"/>
                <input name="item_option_name_4" type="hidden" value="Buy Me a 6-Pack!"/>
                <input name="item_option_price_4" type="hidden" value="10.0"/>
                <input name="item_option_description_4" type="hidden" value="Buy Me a 6-Pack! - jdcargile.com"/>
                <input name="item_option_quantity_4" type="hidden" value="1"/>
                <input name="item_option_currency_4" type="hidden" value="USD"/>
                <input name="shopping-cart.item-options.items.item-4.digital-content.url" type="hidden" value="https://jdcargile.com"/>
            </td>
</tr>
<tr>
            <td align="left" width="1%">
                <input alt="" src="https://checkout.google.com/buttons/buy.gif?merchant_id=[[MerchID]]&amp;w=121&amp;h=44&amp;style=white&amp;variant=text&amp;loc=en_US" type="image"/>
            </td>
        </tr>
    </table>
</form>

Obout Suite

Obout.com

I’ve been using the Obout Suite control and grid products from Obout Inc for a little over a year now. We use their AJAX grid, combobox and super form controls for a tool we created for our shipping team to track our smart labels on outgoing shipments. A requirement our shipping team had was an easy to use way to pull up any given delivery, pick those products from the warehouse, scan each product for the smart label which includes information such as mac address, color, etc. and verify through to delivery. Due to this being a side project of mine and the fact I’m not a .Net ninja, the tools that Obout offered allowed me to bust this project out in a couple days time. Not only are the tools extremely easy to use, but they look great as well. (See below)

GRID

EDIT COMBO BOX DIRECTLY IN GRID

Conclusion
Obout has a lot to offer and can easily handle millions of records. Their site is a fantastic resource as well with multiple examples and documentation for each control. They even offer a large portion of their products for FREE to get your .Net application up and running quickly without any hassle. Also, I came across an issue with their ComboBox control within a Grid and submitted a support ticket to which I got a response within 24 hours with a very help answer. Chris seems to be the person who answers most of their development specific questions and was a pleasure to work with as he was very patient and followed my issue to resolution. I’d recommend these tools to anyone looking for easy out-of-the-box controls for any type of project from internal applications to customer facing e-commerce sites. Should you want more examples on how we used the Obout send, leave a comment or hit me on twitter.