Twitter Bootstrap JS: Using ButtonGroup (radio type) selected button in a form

This is the function that we use to watch which buttons have been activated in a Twitter Bootstrap `buttonGroup` of a radio type.  You need to have a hidden form field so the function can put the values from the selected buttons in there for your form post.

Using it is as simple as adding this to your document ready of your page.  The first argument of the function call is the id of the `buttonGroup` and the second argument is the id of the hidden input where the function will store the values of the activated buttons:

    $(document).ready(function() {
        watchButtonsRadio('#utilitiesLocatedBtnGroup', '#utilities_located');
    });

Add this function to the general or utility JS file that is loaded on every page:

function watchButtonsRadio(buttonGroup, hiddenInput) {
    $('button', $(buttonGroup)).each(function() {

        var originalValue = $(hiddenInput).val();

        //Convert booleans to numeric
        if (originalValue == 'true') {
            originalValue = 1;
        } else if (originalValue == 'false') {
            originalValue = 0;
        }

        if ($(this).val() == originalValue) {
            $(this).trigger('click');
        }

        $(this).live('click', function() {
            // Hidden by default doesn't trigger the change event so manually fire it
            $(hiddenInput).val($(this).val()).change();
            console.log(hiddenInput + ': ' + $(hiddenInput).val());
        });
    });
}

Tomcat7 Service Start/Stop Script

I’m posting this because I forget the script I used because I setup systems so infrequently.  Place this in a file called tomcat7 in /etc/init.d/ Be sure to sudo chmod 777 tomcat7 so it will run.

#!/bin/bash                                                                                                                                                                                                #                                                                                                                                                                                                          # tomcat7     This shell script takes care of starting and stopping Tomcat                                                                                                                                 
# Description: This shell script takes care of starting and stopping Tomcat
# chkconfig: - 80 20
#
## Source function library.
#. /etc/rc.d/init.d/functions
TOMCAT_HOME=/home/peter/tomcat
SHUTDOWN_WAIT=20

tomcat_pid() {
  echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo "Tomcat is already running (pid: $pid)"
  else
    # Start tomcat
    echo "Starting tomcat"
    ulimit -n 100000
    umask 007
    /bin/su -p -s /bin/sh root $TOMCAT_HOME/bin/startup.sh
  fi


  return 0
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo "Stoping Tomcat"
    /bin/su -p -s /bin/sh root $TOMCAT_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\nwaiting for processes to exit";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\nkilling processes which didn't stop after $SHUTDOWN_WAIT seconds"
      kill -9 $pid
    fi
  else
    echo "Tomcat is not running"
  fi
 
  return 0
}

case $1 in
start)
  start
;;
stop)   
  stop
;;
restart)
  stop
  start
;;
status)
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo "Tomcat is running with pid: $pid"
  else
    echo "Tomcat is not running"
  fi
;;
esac
exit 0

Yet another reason Twitter needs Two Factor Authentication

AP Hack Highlights Two Crucial Features Twitter Needs – Forbes

I swear they haven’t added support for Google Authenticator or Yubikey because any publicity about Twitter is good publicity about Twitter, right?

Get with the program Twitter — you need two factor authentication now more than ever.  It’s not surprising I blogged about it just in February about the hacking of Burger King’s Twitter account.

SSH Tunnel to Remote MySQL (Port Forwarding)

ssh -f -L 13306:localhost:3306 root@example.com -p 10022 -N

We block access to production MySQL servers over port 3306 for security reasons.  Here is an example connection string that proxies localhost port 13306 to port 3306 on the target system.

We use a few switched to make this easier to use:

Use -f to force SSH to go into background just before the SSH session starts.  This way you can get still be prompted for passwords but then run this in background.  We like this because then we don’t have to have an open terminal all the time.

The -L switch indicates port:host:remoteport format. In this case, listen to socket 13306 on localhost and proxy to remote host port 3306.

The -p switch is special.  You might have to connect to SSH on a remote host on a port other than the standard 22.  It is starting to become common practice to change the SSH port on remote hosts to something else for security reasons.  In this example, the remote host only responds to SSH on port 10022.

Using -N tells SSH to NOT execute remote commands.  Useful when all you want this SSH session to do is port forwarding.

AirDroid – Manage your Android from your Computer Browser

I need to transfer some video files from my Ubuntu computer to my Nexus 7. I couldn’t get MTP via USB to work right.

Luckily, I found this nice Android application (free – no ads) that gives you a simple virtual desktop of your Android in your browser. I was able to upload my videos over WiFi in just a few seconds.

You can get AirDroid in the Google Play Store

MySQL FOUND_ROWS() and OpenBD

We recently switched to OpenBD on a production website and it uses MySQL FOUND_ROWS().  There a couple of things to ensure it works properly on OpenBD.
You need to add this setting to the “Connection Settings” in the datasource
useDynamicCharsetInfo=false
The queries need to be in a cftransaction block otherwise they will use a different MySQL connection.   It would look something like:
<cftransaction>
  <cfquery name="foo" datasource="foo">
    SELECT SQL_CALC_FOUND_ROWS * FROM foo 
    WHERE id > 2 LIMIT 2
  </cfquery>
  <cfquery name="bar" datasource="foo">
    SELECT FOUND_ROWS() AS numRows
  </cfquery>
</cftransaction>