Wednesday, February 28, 2007

PCC giving an AJAX class and one on Eclipse this term

The faculty asked me to pass this on…

Building Websites with AJAX

Bring richness and responsiveness to your website using the
methods that make Google maps so much fun. Learn the techniques and tools of AJAX to bring speed and interactivity to your web presence. With Asynchronous JavaScript and XML you
will find a new way to impress your web visitors. An AJAX toolkit will also be used to develop
projects. Suggested prerequisites: HTML and JavaScript knowledge.

CRN Location Time Days Dates
Cost
26670 SE Center TABOR 118 06:00PM-09:00PM MW 21-May-2007
to 18-Jun-2007 $400

Using the Eclipse Platform IDE

Provide the skills for productive use of the Eclipse Integrated
Development Environment tool for software development and debugging.

CRN Location Time Days Dates
Cost
26739 Cent. Ptlnd CPWTC 207 06:00 PM-09:00PM TTh 29-May-2007
to 21-Jun-2007 $480

To register, go to https://my.pcc.edu/cp/home/displaylogin

Friday, February 16, 2007

Find and Replace in Mysql

Simple but very useful MySQL trick...

UPDATE `table` SET `field` = REPLACE (
`field` ,
'{string to find}',
'{replacement string}'
)

Getting Around Clients that only allow connection from a single IP

As a freelance Web Developer, I often find myself this circumstance.
Client X states…

We can allow you access to our network but only through your single dedicated IP

I won’t go into how ‘un’-realword this is in this day and age (what freelance coder always works behind a single IP?). I tend to work from coffee shops and at home I prefer (cheaper) dynamic ip broadband.

Overview: We will use a ‘trusted’ server we have ssh access to as a ‘proxy’ to connect to the ClientX server. From ClientX’s point of view, it will simply look as though you are connecting directly from the proxy server but you will be able to connect to clientX from any server (through the proxy server).

Step 1:

Give clientX the IP to any “trusted” static IP server you have ssh access to (this will be the proxy server).

Step 2:

The setup

On local, issue this command…

$ ssh -L2001:clientX:22 proxy

You are instructing SSH to encrypt traffic from port 2001 (chosen arbitrarily, you can choose any unprivleged port that is available) on your local computer and send it to port 22 on clientX, using the SSH server on proxy.

Leave this terminal open (where you issue the above command. It will be logged into proxy through ssh).

Now in a new terminal window you can ssh, scp, sftp to clientX by using local port 2001. You can even use your favorite graphical ssh, scp,or sftp client, just point it to localhost and port 2001.

ex:

$ ssh -p2001 user@localhost
user@localhost's password:
Last login: Fri Jan 19 17:29:23 2007 from 99.99.180.136
[user@clientX user]$ hostname
clientX.com
[user@clientX user]$

As you can see, you ssh to localhost port 2001 and end up on clientX

Shown below, you can take your GUI sftp client and point it to localhost and port 2011, after logging in (with your clientX credentials), you will see your files on clientX

To close the connection, disconnect any ssh, scp, or sftp clients then simply type exit in the original terminal where you typed $ ssh -L2001:clientX:22 proxy

Running PHP5 fast cgi with lighttpd on OS X

Lighttpd is a lightweight (lighter than Apache) http server. It has been gaining more an more press lately (The Ruby crowd uses it often for Rails).
You can run PHP on lighttpd as a CGI (Fast CGI prefferably). You can even keep your existing Apache/PHP install and run Lighttpd/PHP side by side (on another port)

Step 1: Install lighttpd.

By far the easiest way to do this on a OSX is with darwin ports (easy to install, easy to upgrade). In a terminal just issue:

$ sudo port install lighttpd

It will most likley install a few prerecusits and then lighttpd. If you are not a dp fan, compiling and installing lighttpd is not dificult either, see their docs for that.
Step 2: Install a Fast CGI enabled PHP5.

Download the latest PHP5 tarball (at the time of this writing that was 5.2.0)
cd to the unpacked php-5.2.0 directory and run your configure command. In my case I used…

./configure --prefix=/usr/local/php-fcgi
--enable-fastcgi
--with-mysqli=/usr/local/mysql/bin/mysql_config
--with-mysql=/usr/local/mysql
--with-curl=/usr/include
--with-xsl
--with-gd
--with-png-dir=/opt/local
--with-jpeg-dir=/opt/local
--enable-sockets
--with-gettext=/opt/local
--with-zlib-dir=/opt/local
--with-config-file-path=/etc
--with-xml-rpc
--enable-mbstring
--with-iconv
--enable-memory-limit

Two things are important here.

1. –prefix=/usr/local/php-fcgi : This needs to be different than your existing php install. If not you will overwrite/corrupt that existing install.
2. –enable-fastcgi : Self explanitory

As for the other config flag, just use what you need, this example is just what I use.
When configure completes run make then sudo make install
You will end up with a php install at what was used for ‘–prefix=…’ (/usr/local/php-fcgi in my case)
Take PHP’s example php.ini file ({php-source-dir}/php.ini-recommended) and move it to /etc/php-fast-cgi.ini (in my case since I used –with-config-file-path=/etc). Naming the file php-fast-cgi.ini rather than php.ini caused this cgi version of PHP to use that ini file rather than say another php.ini file there that Apache is using. This lets you run Apache mod PHP and cgi PHP on the same box and keep the ini files centaly located.
Step 3: Configure Lighttpd.

Lighttpd has aconfig file (similar to httpd.conf) and after a darwin ports install it can be found at /opt/local/etc/lighttpd/lighttpd.conf.default. First make a copy of that file

$ sudo cp /opt/local/etc/lighttpd/lighttpd.conf.default /opt/local/etc/lighttpd/lighttpd.conf

Now with you favorite editor, make these changes to lighttpd.conf.

within server.modules, uncomment (delete the ‘#’) for “mod_fastcgi”,

set the document-root to something appropriate, I used…

server.document-root = "/Users/sam/Sites/lighttpd/root/"

set the erorlog to something appropriate, I used…

server.errorlog = "/Users/sam/Sites/lighttpd/logs/lighttpd.error.log"

for OS X uncomment server.event-handler…

## set the event-handler (read the performance section in the manual)

server.event-handler = "freebsd-kqueue" # needed on OS X

Set the accesslog to something appropriate…

accesslog.filename = "/Users/sam/Sites/lighttpd/logs/access.log"

Set the server.port (I used 8181, you can use any available port)…

server.port = 8181

Set up the fast-cgi section. This is the minimum needed, see the lighttpd docs for more info.

fastcgi.server = ( ".php" => ((

"bin-path" => "/usr/local/php-fcgi/bin/php",

"socket" => "/tmp/phpfcgi.socket"

)))

Set up the user and group for lighttpd to run as. I chose www (should exist on OSX, you can use any ‘unprivileged’ user)

server.username = "www"

server.groupname = "www"

Okay, save that file and quickly create the directories we refered to in the config file (if they do not exist) and change there ownership to the server.username:server.groupname you chose above (www:www in my case)

$ mkdir -p /Users/sam/Sites/lighttpd/root /Users/sam/Sites/lighttpd/logs

$ sudo chgown -R www:www /Users/sam/Sites/lighttpd/logs

Now we are ready to test our config file with this command (-t signifies test and -f tells lighttpd which config file to use)

$ /opt/local/sbin/lighttpd -t -f /opt/local/etc/lighttpd/lighttpd.conf

If all is well, you should see “Syntax OK”. If not, google your error or look at the docs for lighttpd
Step 4: Start lighttpd.

From the terminal issue

$ sudo /opt/local/sbin/lighttpd -f /opt/local/etc/lighttpd/lighttpd.conf

Now place a test php file in your lighttpd web-root (/Users/sam/Sites/lighttpd/root/index.php in my case) and put these lines in it.


Save the file and then open abrowser and go to http://localhost:8181/index.php and you should see something like

php-fcgi.png

Ths important thing here is that you go to localhost:8181 (not localhost or localhost:80). The nice thing is, if you already had PHP/Apache installed, you can still use that on whatever local URL you had setup (i.e. http://localhost)

So welcome to lighttpd. There is a vast amount on lighttpd that was not covered here, so be sure to check out the lighttpd site for more details.

Insta’ favicon

Dynamic Drive has a very nice favicon tool. I’ve been a fan of the Drive for many years now. Great place to find DHTML widgets

Wednesday, February 14, 2007

Cache_lite Wrapper Class

Cache_lite is a very usefull PEAR library for implementing lightweight caching mechanisims. Two things it is capable of doing is;

* caching the return from any function
* caching any datastructure you pass to it.

One issue with Cache_lites implementation of these two capabilities is that they come from different classes. If you need to cache a function return, you need to require Cache/Lite/Function.php first. If you want to cache a data structure, you need to require Cache/Lite.php prior. Also the function caching syntax seemed a bit unintuative to me.
To aleviate these issues I wrote a smaller wrapper class that unifies and symplifies the usage of the Cache Lite class(es).

This allows the client code of cache wrapper to simply call getCall(), getData(), and saveData() without needing to know that different base CacheLite classes are involved.
Also I put in a bit of code to allow for a bit of leway on the accepted formatting of function string parameters given to getCall();
Example Usage

For any function you want to add 'cacheability' to, you can add an optional param of...

$cacheTTL = CachWrapper::DEFAULT_CACHE_TTL

You then code similar to the first two lines of the exaple method below. Note you can supply a cacheTTL of 0 to essentually make an un-cahced call to the method.

Password Policies

Have you ever seen a more pathetic password policy (I’m sure you have but this one is pretty bad). Black-hat hackers must love this if they are able to use brute force type attacks. What poor excuse for a backend can only support passwords in this format?? I never understand why sites don’t allow you to use any (or a VERY limited set) of special characters in you password. Passwords should ALWAYS be hashed (md5, sha, etc…) before persisted so you should be able to use any ’special’ printable character you want. Be wary of the site that can retrieve your actual password!!!

  • Passwords must either be 7 or 8 characters in
    length.
  • You must use at least one number in your password.
  • You must use either the # or $ character in your password.
  • You may not use any of your previous (8) eight
    passwords.
    * If you experience a network or
    printer problem after
    resetting your password using Passport, please shutdown
    and restart your computer.