Skip to main content

Posts

Pop Quiz

Here's a little test to separate the serious coders from the cut-and-paste script kiddies. Given the need to generate an arbitrarily long string consisting of random alpha-numeric characters, which solution is best? Solution A: function randomString($len) { $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" . "abcdefghijklmnopqrstuvwxyz" . "0123456789"; $rndMax = strlen($chars) - 1; $str = ""; while ($len-- != 0) { $str .= $chars[rand(0, $rndMax)]; } return $str; } $str = randomString(8); echo "$str\n"; Solution B: class RandomSequenceIterator implements Iterator { protected $seqMembers; protected $key; protected $limit; public function __construct() { $this->setMembers(null) ->setLimit(0) ->rewind(); } protected function setMembers($strValue) { $this->seqMembers = $strValue; return $this; }...

Setting Up SFTP From PHP

PHP's ftp_ssl_connect() function is for SSL-FTP, where as what I need for a client's application is SFTP. Isn't life grand! Well, it's not really too much trouble... PHP can handle that too with functions from the ssh2 PECL extension. I'm just glad I caught it early on instead of at the 11th hour. I figured though I might as well continue my previous post about setting up this project with a brief description on installing ssh2 and testing it to ensure everything is in working order. Installation The ssh2 extension provides bindings to libssh2 which must be installed first on the system. My target platform is CentOS 5.3, so I was able to install libssh2 and libssh2-devel via yum (using the RPMForge repository). yum install libssh2 libssh2-devel The ssh2 extension is available from the pecl.php.net website . There is a minor bug in the current version (0.11.0) which prevents it from compiling against PHP 5.3 so I needed to apply this patch . wget http://remi.fed...

Creating a CentOS-Based LAMP Virtual Image

In doing some preliminary research and planning for a client's new project, I determined his current in-house deployment platform would not be sufficient given his requirements. Specifically, the project calls for a moderate amount of URL re-writing and the ability to programmatically FTP files to a remote host. The client is running IIS on Windows Server 2008; I’m not too keen on ISAPI rewrite and IIS Rewrite seems to have fallen off the face of the Internet, and the ftp_ssl_connect() function is only available in PHP if both the ftp module and OpenSSL support are statically built-in so we would have to maintain a build environment for him, too. A LAMP-stack makes more sense. Apache can rewrite URLs with mod_rewrite and compiling PHP is a more supported practice on Linux than it is on Windows. I discussed the obstacles and some possible solutions with the client and he's okay with LAMP. Instead of bringing in more hardware, though, I suggested taking advantage of virtualizat...

New Feature at Paste Ninja

Some of you may have noticed last week a new feature was rolled out on Paste Ninja , the premier PHP-powered paste bin-- patches! Here's how it works: Update an existing paste Click the Compare link that appears in the updated paste's header In the Compare dialog that opens, select your desired current and target revisions; a colorized unified diff is displayed so you can verify the changes Click the dialog's Download button It’s that simple!

Seamless Error Highlighting

A lot of output can be generated when you compiling large projects. When it breaks, it can be difficult to identify the particular spot in the build-process where things when wrong. Highlighting the error messages can help them stand out from the rest of the output. ANSI escape sequences can be used to modify how your terminal window displays its text. For example, outputting the sequence \033[41;37mHello World\033[0m would result in "Hello World" displayed in white text against a red background. Escape sequences begin with an escape character (ASCII 27, octal 033) and bracket. The control values are then given (multiple values are semicolon-separated) and the entire sequence closes with m . You can highlight certain messages by routing the STDOUT and STDERR streams to sed and performing a replacement. s,(.*error.*|.*fail.*|.*undef.*),\033[41;37m\1\033[0m,gi The values you match are of course entirely up to your discretion. The tricky part is quoting and escaping the ...

OOP in JS

When people would ask me about Object Oriented Programming in JavaScript, I would always send them to two pages written by Gavin Kistner. The other day when I went to visit them myself to quickly check something, they were gone! What happened, Gavin?! I promptly dug the pages up from a search engine's cache to mirror them, but all the original copyrights still belong to Gavin. OOP in JS, Part1: Public/Private Variables and Methods originally at http://phrogz.net/JS/Classes/OOPinJS.html OOP in JS, Part 2: Inheritance originally at http://phrogz.net/JS/Classes/OOPinJS2.html

Currying in PHP

What happens if you don't have all the arguments handy for a function, but you want to give whatever arguments you do have now and then provide the rest of them to the function later? This is called currying , and is a core concept in functional programming. It's messy, but possible to curry functions in PHP now that closures have been added . First, let me show you how currying looks in a functional language. Here's a basic example in OCaml/F#: let do_math op x y = match op with '+' -> x + y | '-' -> x – y | _ -> failwith "Invalid op" let add = do_math '+' let inc = add 1 let dec = add (-1) ;; A function named do_math is defined that accepts an operator and two operands. The function's return value will be either the sum or difference of the operands, depending on whether the given operator is + or - . Notice how do_math is then called with a single argument. OCaml doesn't raise an error; it simply ret...

Kember Identity

Ever wonder if there is an MD5 hash the same as the original input? Nope, me neither. But Mr. Kember does and he's asking the world to help him find out if such a thing exists. There's no fame if you find it for him (he's humbly named it the "Kember Identity" already)—but you might make a little cash. Check out his web page for the details. Go ahead and enter his contest if you're feeling gullible lucky! The MD5 algorithm returns a fixed-length 128-bit hash, so there are 2 128 possible values. The hash is typically expressed as a series of 32 hexadecimal values. Since the input string and its hash must be the same to reflect the Kember Identity, you wouldn't need to test random strings like "ruby on rails rots your brain"; you only need to test strings that are 32-characters long and contain the numbers 0 though 9 and letters a through f like 8d112b3c68248c12f178188c1b921ec1 . Kember suggests testing values at random because the range of ...

What's Wrong with OOP

Proponents of Object Oriented Programming feel the paradigm yields code that is better organized, easier to understand and maintain, and reusable. They view procedural programming code as unwieldy spaghetti and embrace OO-centric design patterns as the "right way" to do things. They argue objects are easier to grasp because they model how we view the world. If the popularity of languages like Java and C# is any indication, they may be right. But after almost 20 years of OOP in the mainstream, there's still a large portion of programmers who resist it. If objects truly model the way people think of things in the real world, then why do people have a hard time understanding and working in OOP? I suspect the problem might be the focus on objects instead of actions. If I may quote from Steve Yegge's Execution in the Kingdom of Nouns : Verbs in Javaland are responsible for all the work, but as they are held in contempt by all, no Verb is ever permitted to wander about ...

Death Knell for MySQL

Someone asked me, "What do you think about the Oracle/Sun buyout as it pertains to MySQL?" Well, since you're asking... I thought it was bad for MySQL when Sun bought them despite what others were saying at the time. It turns out I was right. I think Oracle will be worse, and this time the blogosphere are saying it'll probably be bad. Now the question is, just how bad will it be? Here's my predictions: I'm sure Oracle realizes they need to tread lightly on the subject of MySQL or else risk the wrath of the open source community. They may integrate some of MySQL to improve Oracle, but they won't promote the continued development of MySQL proper (Berkeley DB anyone?). That is, Oracle won't actively kill MySQL, but they'll let continue to languish the slow and painful death that began before Sun came along. I don't see a financial benefit to Oracle for keeping MySQL healthy. If MySQL does survive, it might be branded as "Oracle Lite....