Category Archives: General

Small fix for WordPress XML-RPC

I had the idea that I might photo blog more if there was an easy way to get photos off my iPhone and into a blog post.  The easiest way would be to e-mail the photo someplace for WordPress to get it.  Unfortunately, most of the e-mail to blog post plugins (and the built-in stuff) just sucks for my need.

I decided to look at the XML-RPC API to see if I could get it done that way, and then write a script to be called out of procmail to create the mail.  There is a function there that will do what I want, wp.uploadFile aka metaWeblog.newMediaObject.  The specification for that call says that the file being uploaded must be Base64 encoded.  No problem, a few line of ruby later and I have everything ready to test an upload.  The file goes up, but its unviewable in a browser.  A quick check of the file on the server and its revealed to be a text file filled with Baes64 looking data.  Somewhere along the line it wasn’t being decoded.

After about an hour of screwing around, I determined it wasn’t anything obvious in my code, so I thought I’d look at the code for WordPress 2.5.1 to see if I missed anything obvious.  In reading their code, I found that the WordPress folks forgot something obvious, their code never Base64 decodes it before it writes it to a file.  A quick one-line change later, and *boom* I’m off.

Here’s the patch in case its useful to anyone else. I’ll have the script up when I’m done with it.

(Also, in a small bit of embarrassment, I must admit fixing this took me 10 minutes longer than it should because I forgot ; is end of statement in PHP.)

Index: xmlrpc.php
===================================================================
--- xmlrpc.php  (revision 7840)
+++ xmlrpc.php  (working copy)
@@ -1880,6 +1880,10 @@
                        $name = "wpid{$old_file->ID}-{$filename}";
                }
 
+                # The specification says that this is base64 encoded,
+                # we should really decode it.
+                $bits = base64_decode($bits);
+
                $upload = wp_upload_bits($name, $type, $bits);
                if ( ! empty($upload['error']) ) {
                        $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);

New panic in the streets!

I read this in the dead tree edition of the Tribune yesterday and forgot to make a post bitching about it. Luckily, Chicagoist was there: Congressman Says Second Life Could Expose Kids To Porn, Predators.

Basically, Mark Kirk got his panties all up in a bunch because Second Life have some adult stuff going on and in his opionin its too easy for minors to get into that area. While I understand his concern, I disagree that the government should be stepping in here, even with something as minor as an FTP consumer alert. Parents should be monitoring what their children are doing on-line. If you can’t take the time and/or education commitment to do that as a parent, disable your internet access.

The part that really chaps my hide is the following (quoted from the Tribune):

Kirk said he knew of no cases in which children were targeted by sexual predators on Second Life, but he said he considers the virtual world an emerging danger.

So…there’s been no reported problems, or at least none visible to him, but OH MY GOD! RAISE THE ALARM BELLS!  THE CHILDRENS!  WON’T SOMEONE THINKS OF THEM??!?!?!!!  I hate election year fear-based  attention-getting pandering.

For the record, I have no particular love for Second Live either.  I’ve played with it a bit but I don’t get all the hype around it.  I must just be getting old.

Great syntax highlighting plugin for WordPress

At times, I think I spend more time writing about using wordpress than actually using it sometimes. I’ll need to fix that. Today, thanks to WordPress’s dashboard, I came across the WP-Codebox plugin. THanks to GeSHI, it can color up a lot of languages and fixes some problems I’ve had with other plugins. Honestly, I’ve never been satisfied with any of them. I think this could change that.

Just to show it off, I present to you some random ruby I wrote the other day when I was working on something. I’ll leave it to you to guess what I’m doing and why with this code. (And I also think I could do it better than using a while loop, but I was able to bang it out quickly for what I needed.)

#!/usr/bin/env ruby
require 'fileutils'

srand(Time.now.to_i)
offset = 214

files = Dir['*']

count = 1
while !files.empty?
  file = files.delete_at(rand(files.size))
  extention = File.extname(file).downcase
  name = sprintf('%d%s', count + offset, extention)
  puts "#{file} -> #{name}"
  FileUtils.move(file, name)
  count += 1
end