<?php

    /*
    Plugin Name: PlanetTwitter
    Plugin URI: http://saalonmuyo/planettiwtter/
    Description: Pulls in a feed of Planet Money related tweets and shows them
    Version: 0.1
    Author: Eric Sipple
    Author URI: http://saalonmuyo.com
    */
    
    /*  Copyright 2009  Eric Sipple  (email : saalon@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
    
    // This function receives text and if that text contains '[planettiwtter]'
    // it replaces that text with a feed of the top 15 tweets containing
    // the word 'planetmoney'
    function get_planet_money_chatter($text)
    {
    
        // If $text contains [planettwitter] then stick in the feed
        if (ereg('[planettwitter]',$text))
        {
            // We're going to cache our response for better performance
            $cacheFile = 'twitterResponse.json';
            
            // Get the last modified date of the cache file, and the time
            // it is right now
            $last = @filemtime($cacheFile);
            $now = time();
            
            // If the file is more than 60 seconds old, refresh it.
            if (!$last || ($now - $last) > 60)
            {
                // Grab a JSON file of the top tweets containing 'planetmoney'
                // and save it to the cache file.
                $ch = curl_init('http://search.twitter.com/search.json?q=planetmoney');
                $fp = fopen($cacheFile, "w");
                curl_setopt($ch, CURLOPT_FILE, $fp);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_exec($ch);
                curl_close($ch);
                fclose($fp);
            }
        
            // Open up the cache file
            $fp = @fopen($cacheFile,"r");
            $buffer = "";
        
            // As long as the cache file exists, stick it in the $buffer
            if(!$fp) {
                    return false;
            }
            else {
                    while(!feof($fp)) {
                            $buffer .= fgets($fp,4096);
                    }
            }
        
            fclose($fp);
            
            // Let JSON.php decode the JSON into an object so we can parse it.
            $searchResults = json_decode($buffer);
            // Set up our string format templates
            $img = '<img src="%s"/>';
            $bold = '<b>%s</b>';
            $fromUser = '<a href="http://www.twitter.com/%s"><b>%s</b></a> ';
            $planetMoneyFeed = ''; 
            
            // For each response we found, parse it into something pretty
            foreach ($searchResults->results as $result)
            {
                // Replace any @replies or links with hrefs so they link to
                // the right places.
                $twitterText = $result->text;
                $twitterText = preg_replace("#(^|[\n ])@([^ \'\"\t\n\r<]*)#ise", "'\\1@<a href=\"http://www.twitter.com/\\2\" >\\2</a>'", $twitterText);
                $twitterText = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", $twitterText);
                $twitterText = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://\\2\" >\\2</a>'", $twitterText); 
                
                // This gets the profile image, the from user and the created date
                // and sticks it into HTML to make it look attractive.
                $planetMoneyFeed = $planetMoneyFeed . '<div style="padding:5 5 5 5; width:400px;border-bottom:1px dashed #CCCCCC; clear:left;">';
                $planetMoneyFeed = $planetMoneyFeed . '<div style="padding: 0 5 0 5; float:left;">';
                $planetMoneyFeed = $planetMoneyFeed . sprintf($img,$result->profile_image_url);
                $planetMoneyFeed = $planetMoneyFeed . '</div>';
                $planetMoneyFeed = $planetMoneyFeed . '<div style="padding:0 5 0 5; display:block;">';
                $planetMoneyFeed = $planetMoneyFeed . sprintf($fromUser,$result->from_user,$result->from_user);
                $planetMoneyFeed = $planetMoneyFeed . $twitterText;
                $planetMoneyFeed = $planetMoneyFeed . '<span style="font-size:0.8em; text-align:right; font-style:italics; font-family:georgia; color:#999999;">';
                $planetMoneyFeed = $planetMoneyFeed . ' - at ' . $result->created_at;
                $planetMoneyFeed = $planetMoneyFeed . '</div>';
                $planetMoneyFeed = $planetMoneyFeed . '</span>';
                $planetMoneyFeed = $planetMoneyFeed . '</div>';
            }
            
            $text = str_replace('[planettwitter]',$planetMoneyFeed,$text);
        }
        return $text;
    }

    // This is here so it can be used as a Wordpress Plugin.  To use it elsewhere
    // and just call the above function, comment this out.
    add_filter ( 'the_content', 'get_planet_money_chatter');


?>