Inspirated

 
 

May 16, 2010

HOWTO: Query WordPress posts in CMS Made Simple

Filed under: Blog — admin @ 3:53 pm

While I run my blog at inspirated.com, I aggregate posts related to coding at the subdomain code.inspirated.com. The websites are run through WordPress and CMS Made Simple respectively.

For the latter, I needed to find a way of fetching blog posts from the main site for linking. Initially, I used tag feeds for this purpose. That is, I used the RSS module for CMS-MS and fetched the feed for a particular tag (e.g., inspirated.com/tag/code/feed). This worked well for a while until the feeds became large and I noticed that only the most recent 10 posts were showing up in the listings.

Digging around, I found this piece of documentation which explains how one can use custom queries for collecting WordPress posts from a blog. There was a catch however as the code for doing so could only be run globally. In other words, if I tried running the code mentioned on the page inside a User Defined Tag in CMS-MS I would get strange errors.

The solution was to run the code in a separate PHP file. Here’s how:

  1. Create a file named wp.php in your CMS-MS folder with the following code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    <?php
    require_once('/path/to/wordpress/wp-blog-header.php');
    // edit the path in the line above to point to your wp-blog-header.php
     
    $tag = isset($_REQUEST['--tag']) ? $_REQUEST['--tag'] : 'code';
    $count = isset($_REQUEST['--count']) ? $_REQUEST['--count'] : '-1';
    $after = isset($_REQUEST['--after']) ? $_REQUEST['--after'] : '1970-01-01';
     
    function filter_where($where = '') {
        global $after;
        $where .= " AND post_date >= '".$after."'";
        return $where;
    }
    add_filter('posts_where', 'filter_where');
     
    query_posts('tag='.$tag.'&posts_per_page='.$count);
     
    echo "<ul>";
    if ( have_posts() ) : while ( have_posts() ) : the_post();
        echo "<li>";
        echo "<a href=\"";
        echo the_permalink();
        echo "\">";
        echo the_title();
        echo "</a>";
        echo " (";
        echo the_time('F jS, Y');
        echo ")";
        echo "</li>\n";
    endwhile; else:
        echo "<li>No posts found</li>\n";
    endif;
    echo "</ul>";
     
    wp_reset_query();
    ?>
  2. Add a User Defined Tag in CMS-MS with the following code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    $path = 'whoami | php -q /path/to/wp.php';
    // edit the path in the line above to point to the wp.php created in previous step
     
    // whoami command piped for no reason because my script wasn't
    // producing any output without it
     
    if(isset($params['tag'])) {
        $path .= ' --tag='.$params['tag'];
    }
     
     
    if(isset($params['count'])) {
        $path .= ' --count='.$params['count'];
    }
     
    if(isset($params['after'])) {
        $path .= ' --after='.$params['after'];
    }
     
    echo `$path`;
  3. Use the tag in any CMS-MS page with any of the following combinations:
    • List all posts with the tag code:
      {wp_posts_with_tag tag="code"}
    • List 10 posts with the tag code:
      {wp_posts_with_tag tag="code" count="10"}
    • List all posts with the tag code after May 1st, 2009:
      {wp_posts_with_tag tag="code" after="2009-05-01"}

Custom queries are very powerful once you get them working. Anyone planning on using them should take a look at the function reference for getting to grips with the flexibility they offer.

Tags: , , , , ,

March 2, 2010

GoDaddy/WordPress ninoplas Base64 virus and the fix

Filed under: Blog — admin @ 7:40 pm

Update: The virus seems to have affected only GoDaddy websites, hence the change in title.

Few hours ago I opened my website and noticed some rather strange Javascript hanging around the bottom. After some inspection, it became evident that every page on my blog was trying to load an IFrame to some place called ninoplas.com. Turns out, I wasn’t alone and there are other users as well who are affected by this. Judging by the fact that different blogs were attacked at the same time, this was in all probability the result of a security hole in some plugin or the core itself.

The virus acted by adding a piece of encrypted code on the first line of all PHP files on the server. It’s rather unsettling to consider the extend of damage that could have been caused with the write access to those files. Still, the damage could be rectified by simply deleting those lines. I wrote a tiny script for doing this job which cleans the ninoplas virus from all the PHP files in the current directory:

clean-ninoplas.sh

Warning: While this script has worked for me, I am in no way providing any guarantee for how it behaves on other blogs. Backup your blog as well as database before executing this script.
You have been warned.

Using the fix is a simple matter of:

-bash-$ cd wordpress
-bash-$ wget http://inspirated.com/uploads/clean-ninoplas.sh
-bash-$ sh clean-ninoplas.sh

And don’t forget to backup everything again after cleaning up. The security hole — if there is one — has still not been tracked and if it’s in the core or some plugin which you’re still using, the virus might not be so benevolent next time.

Tags: , , , , , , ,

August 29, 2009

TweetMeme and XHTML

Filed under: Blog — admin @ 1:47 pm

The last tweet on my Twitter account is dated around 2 years ago. I stopped using the service in late 2007 because I didn’t see a point in “microblogging” anymore. Somehow though, I found myself doing something similar on my Facebook status messages for over an year now. Accordingly, I thought it wouldn’t be a bad idea to go back and take a look at the current state of tweeting affairs. Meanwhile, I searched for a plugin to link Twitter with WordPress and found TweetMeme. The nifty little code places “Tweet” buttons on the blog posts. Unfortunately, after I activated it my site broke down on account of invalid XHTML (since the pages get delivered to FF as application/xhtml+xml, that was expected too). A quick look at the code resulted in the following fix:

diff -Naur tweetmeme/tweetmeme.php tweetmeme.xhtml/tweetmeme.php
--- tweetmeme/tweetmeme.php	2009-07-17 20:52:11.000000000 +0600
+++ tweetmeme.xhtml/tweetmeme.php	2009-08-29 03:03:00.000000000 +0600
@@ -26,21 +26,21 @@
     $button .= '<iframe src="http://api.tweetmeme.com/button.js?url=' . urlencode($url);
 
     if (get_option('tm_source')) {
-        $button .= '&source=' . urlencode(get_option('tm_source'));
+        $button .= '&amp;source=' . urlencode(get_option('tm_source'));
     } 
 
     if (get_option('tm_version') == 'compact') {
-        $button .= '&style=compact';
+        $button .= '&amp;style=compact';
     } else {
-		$button .= '&style=normal';
+		$button .= '&amp;style=normal';
 	}
 
 	if (get_option('tm_url_shortner')) {
-    	$button .= '&service=' . urlencode(get_option('tm_url_shortner')) . '';
+    	$button .= '&amp;service=' . urlencode(get_option('tm_url_shortner')) . '';
 	}
 
 	if (get_option('tm_api_key')) {
-		$button .= '&service_api=' . urlencode(get_option('tm_api_key'));
+		$button .= '&amp;service_api=' . urlencode(get_option('tm_api_key'));
 	}
 
 	$button .= '" ';

At the same time, I also encourage people to take a look at “Wordr” — the nanoblogging startup that’s going to make Twitter totally obsolete within months to come.

Tags: , , , , ,

July 30, 2009

Blog structure change

Filed under: Blog — admin @ 5:45 am

While starting this blog back in 2007, I didn’t care much about the URL being redundantly long in form of http://www.inspirated.com/wordpress/. Truth be told — being the lazy bum that I am — I didn’t want to go through the administrative troubles of updating the site structure to have something brief. That is, until today. As I was registering this blog as my OpenID at Slashdot I really got fed up with the sub-directory being part of my claimed identity. The result of this major aggravation was a regular Apache nutjob. First of all, I followed the instructions on this page. Then, I put together a quick .htaccess rewrite to ensure that old addresses (e.g. /wordpress/anything) still get permanently redirected to new ones (i.e. /anything):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*wordpress\/(.*)$ \/$1 [R=301,L]
</IfModule>

And just as everything seemed to work perfectly, the fgallery plugin insisted on ignoring the new URL. Digging up a bit, the culprit turned out to be a bug in fgallery‘s implementation which constructed gallery links with the WordPress address instead of the Blog address (the difference is critical in installations such as mine where WP’s core files resides in a subdirectory but the blog uses root directory for friendly URLs). The fix was trivial (had to replace a few get_bloginfo('wpurl') calls with get_bloginfo('url') instead), and lo and behold! Everything’s functional again. I should have done this little exercise a long while ago but well, better late than never. As a sidenote, this does not mean that I am reconsidering my slothful position on other pending tasks for the blog. Since for most of them I adhere to Georgy Shaw’s version of the quote instead, i.e., better never than late.

Tags: , ,

February 17, 2009

Rough patches

Filed under: Blog — admin @ 3:20 pm

A little while ago I had to code a few fixes for the fGallery WordPress plugin. The patch was released as version 2.4.1-1 and it wasn’t until yesterday that I noticed the “Previous” and “Next” links behaving erratically for my images. Fixing it required a little hair-pulling dance with SQL sub-queries. Anyhow-way, the end product does work as expected. Here’s the incremental diff with the bugfix which should be applied to the 2.4.1-1 release. If you don’t understand what was just said in the last line, you can use the already patched zip archive to have things automatically sorted out.

Tags: , , , , , , , ,

September 21, 2008

Fixing WordPress fGallery plugin

Filed under: Blog — admin @ 9:22 pm

For the past two years, I have been using Fredrick Fahlstad’s fGallery plugin for managing images in my WordPress blog. Unfortunately, this excellent piece of software was left unmaintained with the last stable release taking place way back in 2006. For a while now, I’ve had my gripes with some of the things in fGallery. The two options I had were to either (A) fix what was broken or to (B) use another plugin from WordPress Extend.

(B) seemed like too much trouble so I decided to go for the former option and get my hands wet with PHP again. Here’s a list of stuff that I modified in the original 2.4.1 release:

  • Fixed output of special HTML characters in album and image titles to conform with XHTML standards.
  • Modified to send HTTP response “Status:200 OK” back to the client in fim_photos.php. Without this, all of my fGallery pages were returning 404 error code for nice URLs even though they were working in the browser.
  • Fixed an SQL injection vulnerability in fim_rss.php.
  • Fixed image order on album pages and RSS feeds. Without using the table name in the ORDER queries, images were being returned in random order.
  • Fixed date issues in album RSS feeds.
  • Images are now shown in their original sizes in case their width is smaller than 600 pixels. In case they overflow this limit, they are shown in a 600 pixels wide frame with an option to click them for viewing in original size. This was to prevent larger image from messing up blog themes.

Here’s a diff file with the mentioned changes, which should be applied to the 2.4.1 release. I’ve also uploaded a modified zip archive for convenience of those who don’t have access to the patch command.

Tags: , , , , , , , , ,

July 3, 2008

“Will you bite the hand that feeds?”

Filed under: Blog — admin @ 9:23 pm

Today, I woke up to find that my FeedBurner feed for Inspirated has stopped updating because the source feed is timing out. After digging around a little, the source of the problem turned out to be long posts containing (hundreds of lines of) code.

At this point, I could change settings in WordPress to display “summarized” texts instead of full ones in my feeds. The downside of which was the fact that it would require readers to click through their feed aggregators even for posts that weren’t long enough to cause server delays. The better solution was to use the wp-cache plugin so that WordPress wouldn’t have to “process” the feeds at each request. The plugin itseld works like a charm, but getting things working again with FeedBurner was a bit of PITA. Here’s the order of steps that worked:

  • Disable the FeedBurner FeedSmith plugin in WordPress administration panel.
  • Access the original feed to make wp-cache do its magic.
  • Resync the feed in FeedsBurner administration panel.
  • Enable the FeedSmith plugin again.

It took me almost a couple of hours to sort out the whole picture, and ironically, one of the songs in my playlist at the time had lyrics that I’ve quoted in the post-title.

Tags: , , , ,

March 16, 2007

Summer of Code now accepting student applications

Filed under: Blog — admin @ 1:44 pm

Things are finally in motion. I’m keeping my fingers crossed as I’m going to submit an application for a project based on the very same blogging engine that runs this site i.e. WordPress. My aim would be to implement an easier and efficient upgrading system in WP’s administration dashboard. I will submit my application within the next few days and keep updating my blog with its status. (more…)

Tags: ,

January 27, 2007

When one world ends, something else begins

Filed under: Blog — admin @ 3:25 am

Finally, Inspirated.com is up and running. (more…)

Tags: