Category Archives: Technology

Pinterest – Scaling the App (link)

A very good read on scalability and high-performance data management.

Over the last 10 years I’ve used all but Mongo and Redis to solve these very same issues, and had the same Findings.

A couple of the surprising lessons to me was how bad Cassandra was, and how good Solr is.

I hope you find this as interesting as I did:

Scaling Pinterest – From 0 to 10s of Billions of Page Views a Month in Two Years

UPI and Washington Post Twitter Accounts Hacked!

When I heard about a UPI report that the US Carrier George Washington has been attacked by a Chinese war ship, and was damaged, it smelled fishy so I started poking around the interwebs.

USA Today:

New York Post, UPI Twitter accounts hacked

UPI Responds

UPI website, Twitter account hacked

Press International’s website and Twitter account were hacked Friday afternoon, with someone attempting to publish false stories.

It started on Twitter, where six fake headlines were posted in about 10 minutes, starting about 1:20 p.m. Some of them were about the Federal Reserve; others contained a false report that the USS George Washington had been attacked.

Engaget

Pope declares WWWIII?
Twitter accounts for NY Post, UPI hacked to push fake WWIII alerts

Screen Shot 2015-01-16 at 1.01.34 PM

NAVY Times

I was actually alerted to this by the NAVY Times post that the carrier was not attacked and actually safely in port!
Screen Shot 2015-01-16 at 1.04.32 PM

Navy: China has not attacked U.S. aircraft carrier

The aircraft carrier George Washington has not been attacked, and World War III has not begun, despite what tweets from United Press International say, the Navy has confirmed.

The carrier is in port, not in the South China Sea, the Navy told Military Times on Friday.

PHP single vs. double quotes what’s the diff?

Using single vs. double quotes when handling strings in PHP (and code in general). This article is a re-hash of experimentation done about 6 years ago with PERL. It was very clear that unless you have a VERY compelling reason to use double-quotes with strings.. you shouldn’t do it.

Some people will ask.. “Why, what’s the diff”? Well, simply put.. double quoted string are more work for interpreted code languages such as PERL and PHP (and possibly others too, but I’ve never tested them). Compiles languages should not be subject to such unfortunate circumstance.

The Short of it

Using double quotes vs. single quotes in string copies or setting will cost you and extra processing time (proofs follow).

However, when it comes to variable substitutions, that’s where you’ll see more of the speed benefit, when not forcing PHP to interpret the string looking for variables to substitute.

Although, one interested finding after multiple test runs was that bounding the variable with brackets does not offer a consistent benefit, and often it’s a slight loss of speed.

Here is the raw comparison of the following string copies (heavily iterated):

The Raw Data

$x = “THIS IS A STRING” 1.336
$x = ‘THIS IS A STRING’ 1.187
$x = “THIS IS A STRING $i” 3.004
$x = “THIS IS A STRING ${i}” 3.015
$x = “THIS IS A ${i} STRING” 3.448
$x = ‘THIS IS A STRING’.$i 2.647
$x = ‘THIS IS A ‘.$i.’ STRING’ 3.488

Is Google looking at a rough 2015?

Screen Shot 2014-12-18 at 10.09.15 AMInteresting read about possibly looming troubles for Google. I will say that in the past I used Google to look for products, but most of the items I found that way were from shaky looking distributors, or links to Amazon, where I found they had a very competitive price.

Perception is reality, my personal perception is that Amazon is a trustworthy enough for me to buy from them. Over the last few months I’ve simply quit Googling for products and checked Amazon first, and only using Google if I felt that Amazon didn’t offer the product or the price was more than I wanted to pay.


Google’s stocks have taken a dive recently. It was a rocky 2014 but the last month has seen a nose dive in stock trading value:
Screen Shot 2014-12-18 at 10.12.44 AM

That’s not all. As the Mercury News (headquartered in Silicon Valley) reported last month, FireFox has dropped Google as it’s default search engine:
http://www.mercurynews.com/business/ci_26971412/firefox-drops-google-yahoo-default-search-engine

Here is a link to an opinion piece on LinkedIn that discusses this further:

https://www.linkedin.com/pulse/googles-very-rough-transition-nicholas

Playing with Code — hacking a CraigsList Parser

Intro:

While watching the sky fall here on the California Coast, I decided to hack together a fun little toy for scouring some of the local Craigs List sites for things; such as Track Bikes. 🙂

The Concept:

  • Collect regions of interest list for Craigs List.
  • Execute search in each region using AJAX’ed page grabs.
  • Display parsed results in a list on the final page.

The Execution:

Using a multi-dimetional array of States, with sub-regions, hostnames were collected recorded. It looks something like this:

/*  Craigs List Stores */
$CLStores = array(
	'California' => array(
		'San Francisco' => 'http://sfbay.craigslist.org',
		'Chico' => 'http://chico.craigslist.org',
		'Sacramento' => 'http://sacramento.craigslist.org',
...
		),
	'Nevada' => array(
		'Reno' => 'http://reno.craigslist.org',
		'Elko' => 'http://elko.craigslist.org',
...
		),
...

This list is iterated upon, with each entry being passed to and AJAX worker bot. When the bot completed the page grab and parsing, the data is returned to the main document, and dynamically inserted.

foreach($CLStores as $state => $center){
        printf('
  • %s
    • ',$state); ... printf('
    • %s
      Loading...
    • ',$url,$state,$name,$id); ...

    This is all pretty basic stuff, but automation of searches is a specialty of mine, and it’s kept me gainfully employed with many contracts over the last 15 years.

    THE LINK:

    Here is THE TRACK BIKE SEARCH LINK

    Final results look like this:
    Screen Shot 2014-12-12 at 7.57.16 AM

    Upgrading OSX and impacts to Gearman PHP components

    Originally posted June 2013
    Being on the Apple Developers list, I’ve installed the latest edition of the OS and am doing some Beta testing of my apps.

    A few days after upgrading, my Gearman test code stopped working with this error:

    Fatal error: Class ‘GearmanClient’ not found in connect.class.php on line 35

    That triggered a slight bit of panic, however I knew my libraries were mostly in tact as I was able to start my gearmand service without a problem at all. Hoping against odds I decided to simply run a new make and install of the Gearman PHP components.

    UPDATE: If you do not have the latest Gearman libraries for PHP, they are located here: http://pecl.php.net/package/gearman I recommend you download the latest version and build from that.. My page on building PHP Gearman on OSX is located [HERE]

    I cd’d to the directory where I’d built my Gearman PHP libraries a few days prior:

    david$ cd /usr/local/gearman-1.1.2

    NOTE: If you have not recently built PHP Gearman modules, this page [ HERE ] detailed getting to the next step.

    Then I ran a make and a make install in the directory.

    gearman-1.1.1 david$ make
    /bin/sh /usr/local/gearman-1.1.2/libtool –mode=install cp ./gearman.la /usr/local/gearman-1.1.2/modules
    cp ./.libs/gearman.so /usr/local/gearman-1.1.2/modules/gearman.so
    cp ./.libs/gearman.lai /usr/local/gearman-1.1.2/modules/gearman.la
    […]
    Build complete.
    Don’t forget to run ‘make test’.

    gearman-1.1.1 david$ sudo make install
    /bin/sh /usr/local/gearman-1.1.2/libtool –mode=install cp ./gearman.la /usr/local/gearman-1.1.2/modules
    cp ./.libs/gearman.so /usr/local/gearman-1.1.2/modules/gearman.so
    cp ./.libs/gearman.lai /usr/local/gearman-1.1.2/modules/gearman.la
    ———————————————————————-
    Libraries have been installed in:
    /usr/local/gearman-1.1.2/modules

    […]
    ———————————————————————-
    Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20090626/

    This worked perfectly, and following a RE-CREATION of my /etc/php.ini file (which I also lost), I was good to go!

    include_path=.:/mnt/crawler
    extension=”gearman.so”

    Viola.. Gearman development back underway!!

    Installing Gearman PHP components for OSX

    Locating the latest PHP Components

    The Gearman.org page has links to the PHP code on the Downloads page, however the link is very old. The latest code is located at: http://pecl.php.net/package/gearman.

    As of 23-OCT-2014, the current stable version is gearman-1.1.2.

    I like to drop these files in my /opt directory, and work on them there and unball the package.

    mv ~/Downloads/gearman-1.1.2.tgz /opt/.
    tar xvzf gearman-1.0.2.tgz
    cd gearman-1.0.2

    Configuring for Build

    The following commands prepared the PHP package to build on OSX Yosemite (10.10).

    phpize
    Configuring for:
    PHP Api Version: 20121113
    Zend Module Api No: 20121212
    Zend Extension Api No: 220121212

    ./configure
    checking for grep that handles long lines and -e… /usr/bin/grep
    checking for egrep… /usr/bin/grep -E
    checking for a sed that does not truncate output… /usr/bin/sed
    […]
    appending configuration tag “CXX” to libtool
    configure: creating ./config.status
    config.status: creating config.h

    Building the Library

    Next step is to run the compile and install the built objects:

    make
    /bin/sh /opt/gearman-1.1.2/libtool –mode=compile cc -I. -I/opt/gearman-1.1.2 -DPHP_ATOM_INC -I/opt/gearman-1.1.2/include -I/opt/gearman-1.1.2/main -I/opt/gearman-1.1.2 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/local/include -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -Wall -c /opt/gearman-1.1.2/php_gearman.c -o php_gearman.lo
    mkdir .libs
    […]
    Build complete.
    Don’t forget to run ‘make test’.

    make install
    Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20121212/

    Telling PHP about gearman

    You will need to identify your relevant php.ini file, and edit it, letting PHP know where the library file are located.

    Typically under OSX, this file does not exist, and it must be created.

    Edit the file:

    vi /etc/php.ini

    Either way, make sure these two lines are in the file:

    Add these lines:

    include_path=.:/mnt/crawler
    extension=gearman.so

    DONE

    At this point you should be able to reference Gearman library in your PHP code.

    These lines of code, should not throw an error:

    $client = new GearmanClient(); // instance
    $worker = new GearmanWorker(); // instance

    node.js — using cheerio.js to find all script elements in a page

    Finding <script> nodes in a page

    Why.. why? Just because it’s useful when pages had dynamic content in javascript. Is there a way to subsequently evaluate the javascript parsed.. that’s for another article, but for now, I’m going to assume you have node.js installed, and you have at least come idea of how to use it.

    The idea

    Finding all the <script> nodes in an HTML page, rendered using

    ‘request.get()’

    .

    In the example, url (in this case www.amazon.com) is resolved and the HTML loaded. The loaded HTML is then passed to cheerio using this expression:

    var $ = cheerio.load(html,{ normalizeWhitespace: false, xmlMode: false, decodeEntities: true });

    .. then iterated upon using the .each( ..) object method.

    $(‘script’).each( function () {…

    In the very simple example the follows the script is logged to the console (STDOUT) for display. In an more advanced and useful implementation, the returned javascript would be interacted with, parsed or some other action taken.

    The Script

    // MAKE REQUIREMENTS
    var request = require(‘request’);
    var cheerio = require(‘cheerio’);

    // Local Vars
    var url = ‘https://www.amazon.com’;

    // Define the requests default params
    var request = request.defaults({
    jar: true,
    headers: { ‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0’ },
    })

    // execute request and parse all the javascript blocks
    request(formUrl, function (error, response, html) {
    if (!error && response.statusCode == 200) {

    // load the html into cheerio
    var $ = cheerio.load(html,{ normalizeWhitespace: false, xmlMode: false, decodeEntities: true });

    // iterate on all of the JS blocks in the page
    $(‘script’).each( function () {
    console.log(‘JS: %s’,$(this).text());
    });
    }
    else {
    console.log(‘ERR: %j\t%j’,error,response.statusCode);
    }
    });

    End

    node.js — parse page title (simple example)

    node.js — Toolkit of the Code Gods!!

    Or, so some would have you believe. Is it pretty awesome, YES. I’m I sold on it yet, NO. But it’s growing on me.

    Since parsing webpages has been my business for nearly 15 years now, I’ve used a lot of tools and strategies, but it was only recently I decided to try out node.js for a few of my projects.

    Starting with node.js

    If you are new to node.js, go check out these URLS here. They more than successfully cover getting started with node.

    The goal here is to answer a question that for some reason eluded my best searches for code examples. I thought I had the syntax dialed but still saw some strange responses. This page will show you definitively how to get a page title. Every time (every time the page loads at least).

    How I parsed the title off a page

    Here is how I did it, using cheerio and request:

    /*
    * MAKE REQUIREMENTS
    */
    var request = require(‘request’);
    var cheerio = require(‘cheerio’);

    /*
    * Handle Commandline Params
    */
    var url = process.argv[2];

    /*
    * Local Vars
    */
    // Define the requests default params
    var request = request.defaults({
    headers: { ‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0’ },
    })

    // DO THE WORK!!
    request(url, function (error, response, html) {
    if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html,{ normalizeWhitespace: true, decodeEntities: true });
    var title = $(‘title’).text();
    console.log(“TITLE: %j”,title);
    }
    else {
    console.log(‘ERR: %j\t%j’,error,response.statusCode);
    }
    });

    Running the example from the command line looks like this (I’m using the 1st available parameter to pass my URL, hard-coding is for fools):

    node get.title.js http://www.yahoo.com
    TITLE: “Yahoo”

    Conclusion

    The reason I’ve posted this blog, is that this specific node.js cheerio syntax was not clearly specd:

    var title = $(‘title’).text();

    Enjoy toying around with node.js to parse your super-awesome pages.

    Patching OSX against the ‘ShellShock’ exploit

    While everyone waits for Apple to release a patch for the ShellShock bug, one of the maintainers of BASH assisted with detailing out how to patch BASH (and SH) on OSX to prevent the Vuln. This comes from the helpful Apple section of Stack Exchange.

    NOTE: To perform this patch you MUST be granted sudo privs on your machine — if not you won’t be able to move the new files into the required location.

    Testing to see if you are vulnerable

    First things first.. see if you are vulnerable by checking your version of BASH. The desired version is this; GNU bash, version 3.2.54:
    Screen Shot 2014-09-29 at 8.05.00 AM

    If you are not seeing that, then you should check to see if you have the vuln. When I checked my updated version of OX Mavericks, I was on Bash 3.2.52 and it was vulnerable to the exploit.

    If you see the word ‘vulnerable’ when you run this, your at risk!
    env x='() { :;}; echo vulnerable' bash -c 'echo hello'

    This is a PASS (OK):
    env x='() { :;}; echo vulnerable' bash -c 'echo hello'
    hello

    This is a FAIL:
    env x='() { :;}; echo vulnerable' bash -c 'echo hello'
    vulnerable
    hello

    Time to get down to patching

    This process is going to require you to do some command line work, namely compiling bash and replacing the bad versions with the good ones. If you are NOT comfortable do that.. best to wait for Apple to create the installable patch. If your geek level is above basic, continue forward:

    First, agree to using xcodebuild
    If you have no run xcodebuild, you are going to need to run it, then agree to the terms, before you’ll be able to finish this build. I recommend you run it NOW and get that out of the way:
    xcodebuild

    Set environment to NOT auto-include
    This capability is part of the reason the exploit exists. It’s highly recommend you turn this on before starting the build. Ignore at your own peril. This parameter is used in the build stage for two patches:

    export ADD_IMPORT_FUNCTIONS_PATCH=YES

    Make a place to build the new objects
    I dropped everything into the directory ‘new-bash’… and did it thus. NOTE: I am not using sudo, (yet)

    mkdir new-bash

    Download base-92 source
    Move to that directory and download the the bash-92 source using good old curl and extract the compressed tarball:

    cd new-bash
    curl https://opensource.apple.com/tarballs/bash/bash-92.tar.gz | tar zxf -

    Get the patch packages next
    CD to the source directory for bash, and then download 2 patch packages:

    cd bash-92/bash-3.2
    curl https://ftp.gnu.org/pub/gnu/bash/bash-3.2-patches/bash32-052 | patch -p0
    curl https://ftp.gnu.org/pub/gnu/bash/bash-3.2-patches/bash32-053 | patch -p0

    Start creating the patches
    Execute these two commands, in order two build and apply the two patches:

    [ "$ADD_IMPORT_FUNCTIONS_PATCH" == "YES" ] && curl http://alblue.bandlem.com/import_functions.patch | patch -p0
    [ "$ADD_IMPORT_FUNCTIONS_PATCH" == "YES" ] || curl https://ftp.gnu.org/pub/gnu/bash/bash-3.2-patches/bash32-054 | patch -p0

    Start building!
    Traverse back up the tree and start running the builds. It is recommended that you NOT run xcodebuild at this point. Doing so could enable root powers in the shell and that is something that you certainly do not want!

    xcodebuild

    OK.. PATCH MADE!
    At this point you have a new bash and sh object build to replace the exploitable ones. Backup your old versions, move these into place and you are now safe.

    # Test your versions:
    build/Release/bash --version # you should see "version 3.2.54(1)-release"
    build/Release/sh --version # you should see "version 3.2.54(1)-release"

    # move the files into location
    sudo mv /bin/bash /bin/bash.BAD
    sudo mv /bin/sh /bin/sh.BAD
    sudo mv build/Release/bash /bin
    sudo mv build/Release/sh /bin

    Now clean up the local mess
    Now the local directory where you build bash is no longer needed. I don’t like to leave cruft around on my system that creates a confusing environment. Removing the source tree is my last task. You can leave it if you like, but if I need to do this again I’m going to perform a full fresh rebuild, so this will not be re-used.

    cd
    rm -rf new-bash

    YOU ARE DONE!

    BIG HUGE THANKS TO ALL THAT DID THE REAL WORK HERE.. the people maintaining bash, the people that post awesome solutions to StackExchange and all the other fantastic resources on the net!