Organizing the spice drawer.

We try to keep our code clean and commented but every once in a while we do some maintenance that falls into what I call “Organizing the spice drawer.” It’s not something that has to be done and the amount of time lost by not doing it is small, but it’s just one sign of a well organized shop. The same applies to our server, backups, and the office in general. This time of year is perfect for these endeavors because there’s not usually much else going on.

Log files
We set up the server logs to automatically archive and then delete themselves so we don’t have to worry about them. We do have some custom logging programs that write data to the MySQL database. They’re not huge, but we’ll never use more than a year’s worth, so they get manually cleaned from time to time. I just deleted 45,000 records from one database.

/tmp
It’s a good idea to check what’s in here from time to time. We has some session cookies that weren’t being properly expired so we wrote a cron job to remove them. There were also some files created by a shell script that weren’t getting deleted, so we adjusted the script to remove them.

php.ini
Every once in a while, it’s a good idea to review the settings in the php.ini file to make sure you are happy with them. In our case, we were getting warnings from two deprecated commands, register_long_arrays and magic_quotes_gpc and we decided to turn them both to Off since we don’t have any code that relies on them and the code that triggered the warnings is in WordPress. The WprdPress code works fine if the settings are Off.

BAK files and databases
Before we do any major change to a file or database we make a backup copy. These usually get deleted when we’re happy with the result, but sometime they stay around when they aren’t needed any more. For the database, I make a complete dump of the database to a .sql file and then remove databases that are backups. If I ever need them, I’ve got the backup in storage.

Orphans
Our websites have been up since 1998 and we sometimes end up with files that are no longer referenced. If we suspect that a file is not used anymore we grep for its name in the web directory and if it’s not found we put a line of code in it that writes to the server when it is referenced.


$refer=mysql_real_escape_string($_SERVER['HTTP_REFERER']);
error_log("Accessing Orphan File with referrer $refer");

If we reference it from one of our pages, the referrer will be our site. If not, then either it’s being accessed by a spam-bot or another website that has an old link to us. Either way, we can then deal with it.

Spambots
Lately, spambots have been hitting just about every page with a submit button on it. They can’t do anything on those pages but I’ve started logging their attempts using the method described in Orphans above. If it’s a real problem we may start requiring CAPTCHAs or simple radio buttons that default to “I am a robot, please ignore this request”.

Error logs
When we updated to the latest version of PHP our database access method, PEAR, was deprecated. That generated literally thousands of error messages. Over time we updated the code, but there were still lots of errors generated for pages that aren’t accessed that much. With so many error messages, it’s hard to see real errors. So we spent a half day finishing off the update. Now the error logs only show the log messages that we want it to show and we can quickly check for errors.

Wikis
We have a wiki for office procedures, how-to’s for ordering supplies, adding users, version numbers for our products, etc. This is the kind of thing that can quickly become out of date if it is not updated regularly. A couple of times a year, we review the wiki and make updates for changes to our processes that were made, but didn’t make it into the wiki.

Office Electronics
Do we really need 12 VGA monitor cables? Three turntables? The 12 year old computer that only runs OS9? The box of old hard drives—the largest of which is 5GB? We don’t keep a lot of stuff we don’t use but things get shoved under desks and put in boxes on the bottom shelf and forgotten about. Every once in a while we make a big pile of stuff and take it to the electronics recycling center. I have yet to regret throwing away anything.

Books
We got rid of a complete shelf of books. I will never need the manual for Photoshop 4 or Flash 5. In fact, I haven’t used a manual for years, relying instead on the web for the answers to most of my questions. My guess is next year the other two shelves of books will be gone.

Using an animated gif in iOS

Kite gif

You can’t use gif’s in iOS but you can do the same thing with a series of png’s. Here’s the code that I use to put a kite on the screen and then animate the tail.

- (void)animatedReward {
    UIImageView *staticView = [[UIImageView alloc] initWithFrame:self.view.frame];
    staticView.image = [UIImage imageNamed:@"kite.png"];
    [self.view addSubview:staticView];
    
    NSArray *imagesArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"kite-tail1.png"], [UIImage imageNamed:@"kite-tail2.png"], [UIImage imageNamed:@"kite-tail3.png"], [UIImage imageNamed:@"kite-tail2.png"], [UIImage imageNamed:@"kite-tail1.png"], nil];
   
    UIImageView *animatedView = [[UIImageView alloc] initWithFrame:staticView.frame];
    
    animatedView.animationImages = imagesArray;
    animatedView.animationDuration = 1;
    [animatedView startAnimating];
    [self.view addSubview:animatedView];
}

Hiding and showing files on OSX

OSX Mountain Lion hides a lot of files that were visible in previous versions. You make them visible in the finder with a single line of code in the Terminal.

For example, this line makes the contents of the /usr directory visible.

sudo chflags -R nohidden /usr

And this one makes your Library folder and its contents visible.
sudo chflags -R nohidden ~/Library

And this one makes the main Library folder and its contents visible.
sudo chflags -R nohidden /Library

If you are setting up Apache on you computer so you can test websites before deployment, you might want to make the private folder visible.
sudo chflags -R nohidden /private/

The first part of the command, sudo, just means that you will be prompted for your computer password before the command is executed.

Sometimes I want to see all the files in the Finder so I enter this line in the terminal.
sudo defaults write com.apple.Finder AppleShowAllFiles TRUE
Restart the Finder with
killall Finder
or use Force Quit to relaunch the finder.
After I’ve done whatever it was that required access to all the files, I usually turn them back off by changing TRUE to FALSE

Audacity on OSX Mountain Lion

Audacity is a free and open sound editor that works on many platforms, including OSX. It is capable of recording and editing vocals and music. However, because of the broken patent system in the US, you cannot open or save files in MP3 or M4A format. The workaround is to download the encoders from a country where the patent system isn’t broken and install them on your computer. The download process is simple. From the Preferences-Libraries section of Audacity click on the Download button for either of the Libraries. It should take you to this page. Follow the links to the download site and install the libraries. Unfortunately, Audacity doesn’t find the new libraries when using Mountain Lion (and I suspect the same is true on Lion). And when you try to find them manually, you can’t see the /usr directory where they are located. There is a simple fix that requires a single line in the terminal.

Open the terminal and you’ll see a command prompt. It should look something like this.
macintosh-users-computer$
Copy text after the $ on the line below and past it after the $ in the terminal.
macintosh-users-computer$ sudo defaults write com.apple.Finder AppleShowAllFiles TRUE
You will get a prompt for your password. Enter your user password for the Mac.
Now restart the finder by going to the Apple in the top left of the screen and clicking on Force Quit option. Click on Finder and then choose Relaunch. The screen will go blank and all the icons will disappear. When they return open the Preferences-Libraries section of Audacity, click on the Locate button to locate the MP3 library and browse to the folder /usr/local/lib/audacity/. You’ll see a file called libmp3lame.dylib. Choose it. Restart Audacity and both the MP3 and FFmpeg libraries should show up.

Now go back to the finder and use the up arrow key to show the previous line. Backspace to change TRUE to FALSE and hit Return. Restart the finder and everything will be back the way it was.