<?php     
/*
Plugin Name: Ambient Information
Plugin URI: http://0blog.com/ambinfo-wp-plugin.php
Description: Adds ability to embed ambient information into web pages.
Version: 0.02 alpha
Author: Toivo Lainevool
Author URI: http://0blog.com/
*/ 
/**
 * ambinfo.php
 * This plugin is used to add ambient information into Wordpress.
 * Currently the only type of information available is changing color based
 * on the age of the post.
 
 * To use this plugin, first change the $ambinfo_stale_time, $ambinfo_fresh_color,
 *  and $ambinfo_stale_color to your desired setting.
 * Then all you have to do is call ambinfo_echo_age_color() or ambinfo_get_age_color()
 *  to retrive a color based on the age of a post.
 *
 * For questions, comment, support and encouraging words use: ambinfo-wp-plugin@0blog.com
 *
 * This work is licensed under the Creative Commons Attribution License. To view a copy 
 * of this license, visit http://creativecommons.org/licenses/by/2.0/ or send a letter 
 * to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.   
 */


//Change this to reflect what you want to consider a 'stale' post.
$ambinfo_stale_time 60 60 24 4;//four days

//This is the color returned when things are freshest
$ambinfo_fresh_color '#90a090';

//This is the color returned when $ambinfo_stale_time is reached
$ambinfo_stale_color '#000000';


/**
  * Use this function to echo the aged color of a post. 
  *
  * @param post_id the id of the post to get the aged color for,
  *   if it is blank, the aged color for the newest post will be used
  */
function ambinfo_echo_age_color($post_id '') {
    print 
ambinfo_get_age_color($post_id);
}

/**
  * Use this function to get the aged color of a post. 
  *
  * @param post_id the id of the post to get the aged color for,
  *   if it is blank, the aged color for the newest post will be used
  */
function ambinfo_get_age_color($post_id '') {
    global 
$ambinfo_stale_time$ambinfo_fresh_color$ambinfo_stale_color;
    
$newest _ambinfo_get_newest_post_time($post_id);
    
$age_in_millis = (time() - $newest);
    if (
$age_in_millis >= $ambinfo_stale_time) {
        
$color $ambinfo_stale_color;
    } else {
        if (
$age_in_millis == 0) {
            
$div 1;
        } else {
            
$div $ambinfo_stale_time / ($ambinfo_stale_time $age_in_millis);
        }
        
$red _get_aged_color_part('red'$div);
        
$green _get_aged_color_part('green'$div);
        
$blue _get_aged_color_part('blue'$div);
        
        
$color strtoupper("#"._ambinfo_to_hex($red)._ambinfo_to_hex($green)._ambinfo_to_hex($blue));
    }
    return 
$color;
}

/**
  * private function, get a single value for an rgb part
  */
function _get_aged_color_part$color_part$div ) {
    global 
$ambinfo_fresh_color$ambinfo_stale_color;
    
$fresh _ambinfo_get_color_part$color_part$ambinfo_fresh_color );
    
$stale _ambinfo_get_color_part$color_part$ambinfo_stale_color );
    return  
abs($fresh - (abs($fresh $stale) - (abs($fresh $stale) / $div)));
}    
/**
  *Take in a decimal number and return a 2 digit hex number.
  */
function _ambinfo_to_hex$color ) {
    
$result dechex($color);
    if( 
strlen($result) == 1) {
        
$result "0" $result;
    }
    return 
$result;
}

/** 
  * Gets the RGB part of the given color.  Format can be '#rrggbb', 'rrggbb', '#rgb', 'rgb'
  */
function _ambinfo_get_color_part($rgb$color) {
    if( 
preg_match("/#.*/"$color)) {
        
$color substr$color);
    }
    if( 
strlen($color) == ) {
        
$color preg_replace"/(.)(.)(.)/""$1$1$2$2$3$3"$color);
    }
    switch (
$rgb) {
        case 
'red':
            
$hex substr($color02);
            break;
        case 
'green':
            
$hex substr($color22);
            break;
        case 
'blue':
            
$hex substr($color42);
            break;
        default:
            
$hex "ff";
            break;
    }
    return 
hexdec($hex);
}

/**
  * Get the post id for the post with the atest time.
  */
function _ambinfo_get_newest_post_time($post_id) {
    global 
$wpdb$tableposts;
    if( 
$post_id == "") {
        return 
$wpdb->get_var("SELECT UNIX_TIMESTAMP(post_modified) FROM $tableposts ORDER BY post_date DESC LIMIT 1");
    } else {
        return 
$wpdb->get_var("SELECT UNIX_TIMESTAMP(post_modified) FROM $tableposts WHERE ID = $post_id");
    }
}


?>