Fixing Byte-Order-Mark issues with Wordpress on IIS

One common problem – it seems – with Wordpress just plain acting funky on IIS is that occasionally, byte-order-marks get inserted into the UTF-8 PHP documents that make up the Wordpress code.

Sometimes these come in as part of templates and plugins, other times they just magically* appear.

If you’ve got Visual C# (or Visual C# express), here’s a little nugget of code you can use to strip them out. No warranties. No expressed suitability for a given purpose. Just free code I wrote. Free, crappy, one-off, single-purpose code. But it works. Compile it up, and run it, and watch it go.

I recommend running it in two passes:

  • Generate a list of files to check using DIR /S *.* /B /A-D > out.cmd
  • Modify that list to call BOMFix on each file (e.g. BOMFix c:\myfile\app.php ). Make a note of which files have a BOM mark
  • Run it again, with /F as the second argument (e.g. BOMFix c:\myfile\app.php /F ). This will strip the BOM from the files.
  • Throw your files back up onto the server.

And for your viewing pleasure, here’s the code:

using System;
using System.IO;

namespace BOMFix
{
    class Program
    {
        static void Main( string[] args )
        {
            if ( args.Length == 0 )
                return;

            bool bHasBom = FileHasBOM( args[ 0 ] );

            if ( bHasBom )
            {
                Console.Out.WriteLine( "{0} has a BOM", args[ 0 ] );
                if ( args.Length == 2 && (args[1] == "/F" || 
                     args[1] == "/f") )
                {
                   StripBOM( args[0] );
                   Console.Out.WriteLine( "Removed BOM from {0}",
                                           args[ 0 ] );
                }
            }
        }

        const long READSIZE = 8192;

        public static bool FileHasBOM( string path )
        {
            FileStream s = new FileStream( path, FileMode.Open,
                                FileAccess.Read, FileShare.Read );
            long fileLen = s.Length;
            if ( fileLen < 3 )
                return false;

            byte[] file = new byte[ 3 ];
            s.Read( file, 0, 3 );
            s.Close();


            return ( file[ 0 ] == 0xEF && file[ 1 ] == 0xBB &&
                     file[ 2 ] == 0xBF );
        }

        public static void StripBOM( string path )
        {
            FileStream s = new FileStream( path, FileMode.Open,
                               FileAccess.Read, FileShare.Read );
            s.Seek( 3, SeekOrigin.Begin );
            long readleft = s.Length - s.Position;
            byte[] buffer = new byte[ READSIZE ];

            string tempFileName = Path.GetTempFileName();
            FileStream outStream = new FileStream( tempFileName,
                FileMode.Truncate, FileAccess.Write, FileShare.None,
                8192, FileOptions.SequentialScan );
            
            while ( readleft > 0 )
            {
                int chunkSize = (int)Math.Min( READSIZE, readleft );
                if ( s.Read( buffer, 0, chunkSize ) != chunkSize )
                {
                    throw new Exception( "Not enough data! File error?" );
                }

                outStream.Write( buffer, 0, chunkSize );

                readleft -= chunkSize;
            }

            outStream.Flush();
            outStream.Close();

            s.Close();

            File.Replace( tempFileName, path, null );
        }
    }
}

*Yes, I know, not actually magically… but no-one seems to have root-caused it.

(more...)

Coming Soon: HangoverCentral.com

One of the largest drivers of people to my blog is my infrequent posts on my experiments with hangover cures. I get a thousand people a month who come here to look for a way to stop their head throbbing.

So I've got an idea. I'm going to put together a site dedicated to the topic. I bought the domain yesterday - it's http://hangovercentral.com and I'm aiming to have something up soon.

The idea is to have:

  • Advice on how to fix your head or stomach hangover
  • Advice on how to not get so drunk in the first place (ever wanted to sit down for vodka shots with a Russian General? Now you'll know how)
  • Supplements/Foods/Etc that help - and exactly what's in them that help you
  • Possibly some information on how to cure hangovers not caused by alcohol... (we're talking nicotine here... but possibly also other substances. Theoretically 1st Amendment law protects me here, but it's still a somewhat illegal topic. I've not tried any of these other substances, but I do have friends who have - and I know that my "after party mix" certainly helps them)
  • A Bulletin Board where people can discuss drinking, hangovers, hangover cures etcetera

Thoughts?

(more...)
#web development, #hangover cures
This entry was posted under Me. Bookmark the permalink.

The BipBuffer Rides Again

So a while back, I invented a datastructure I call the Bip-Buffer. It's a rather neat little thing that if you pump data through it fast enough works pretty much as fast as a true circular buffer. It has a nice data-access pattern too, based around a reserve/commit transaction to push data in one side, and to pull data out of the other side.

It works. Lots of people use it. Some people claim they did it first back in the 90s on the Amiga, but they didn't publish. (Which means, sadly for them, they don't count. No point inventing something if you don't share the knowledge). The price you pay for fame and fortune is letting other people know what you did ;-)

The moral of the story? Give your algorithms unique names. You'll be able to see where they proliferate to. As a result, I can stake a claim on my own tiny little corner of computer science.

Anyway, this is all beside the point. Why am I posting this?

Because Age of Empires Online uses my BipBuffer code & algorithm. A friend of mine is working on the source right now, and he sent me a quick email to let me know it was in there. It's great to know that it's being used :)

(And because he sent me an email, he's actually meeting terms of the licensing agreement :-) )

(more...)

New Blog from Jason Weiler (Airtight Games)

My good pal Jason Weiler has posted up his first article on his new blog - Myopic Topics (http://myopictopics.com).

This one's the first part in a series about writing an add-in for Visual Studio's Evaluation Engine (the bit that tells the locals/autos/watch window how to format and display variables).

He's also the author of the FName-Addin for Unreal Engine 3. This is an awesome, donation-ware tool for Unreal Engine, which allows you to view the actual string values of FNames in the watch window. It's an absolute must-have for anyone doing Unreal development - so download it right now!

And if you do download it, please, make a donation. I pay $150 or so (plus renewal fees every year) for Visual Assist - if I'm working on Unreal Engine, I'm pretty sure that I can throw $50 his way for something which massively takes the pain away from Unreal development.

Anyway, check it out and subscribe to his blog! He's one of the good ones :)

(more...)

The Blog Moves (Again)

Well, finally, I think I've got this up and running again. Of course, to do it, I had to move to arvixe.com for my hosting (away from GoDaddy, whose inability to properly host multiple sites on IIS means that it's time to say GoodByeDaddy).

Now that the blog's working again, I need to do a bit of work to get the style back to something I like... and then... hopefully it'll all be nice and tickety-boo.

(I can't believe I just said "tickety-boo").

Anyway, a few other changes:

  • My lovely significant other - Darci Morales - has her own blog at justdarci.com now. Hopefully she'll post something on it, or I'll need to end up turning it into a resume/portfolio site or something. She is available for consulting work btw ;-)
  • My homepage is finally moving from earthlink.net, where its been sitting since about 1998 when I moved to Seattle. (Before that it was on a variety of other sites, and it's one of the oldest continuously running pages on the web; the first version went up in 1993 IIRC). It'll save me money ($21.95 for 10Mb of hosting, plus dialup access per month. Woooooh! 1998 prices are awesome. I'm not sure I can imagine anything having an 10Mb!!! quota any more but now you know... And Arvixe has theoretically unlimited space). Anyway, it's now up on http://simoncooke.com
  • Finally, the bastard who has been sitting on the simoncooke.com domain name let it go. :) ThankyouverymuchI'llhavethat.

More sites, more stuff to follow. But with a bit of luck I might actually be able to get everything up and running this time - without fighting web hosting issues.

I still have paid up until December 2012 for my GoDaddy hosting though. Wonder what I should put there.

Anyway... the Blog is Back! Hello again! It's been too long!

(more...)
#accidental scientist
This entry was posted under Me, Meta. Bookmark the permalink.

subscribe via RSS