<?php

/* You must make sure that the information at these URLs is in sync. No point
 * in telling you there's a new version out when the download page doesn't
 * contain that version yet. This is very important for Internationalization:
 * don't provide your translated version of WordPress in one URL and not the
 * other.
*/

$resturl = __("http://www.freecharity.org.uk/versions/");
$upgradeurl = __("http://wordpress.org/download/");

$minutes_between_checks = 24*60;

$message[101] = __("This release fixes a critical security issue");
$message[102] = __("This release fixes a security issue");
$message[103] = __("This release fixes a minor security issue");
$message[201] = __("This release fixes a critical bug");
$message[202] = __("This release fixes a bug");
$message[203] = __("This release fixes a minor bug");
$message[301] = __("This release adds major new functionality");
$message[302] = __("This release adds new functionality");
$message[303] = __("This release adds minor new functionality");

function branch_from_version(){
	// Work out which branch from the version number
        global $wp_version;
        $t = preg_replace("/-|[a-zA-Z]/","",$wp_version);
        $c = preg_match("/\./",$t);
        $n = preg_split("/\./",$t);
        if ($c == 0) { return "$n[0].0"; }
        else { return "$n[0].$n[1]";}
}

function set_latest_version(){
	/* Set the latest version. Store the results in the options table.
	 * Store the time when it was fetched in the options table, use this
	 * cached result for the next $minutes_between_checks minutes. Returns
	 * false on failure.
	 *
	 * The REST server doesn't need to concern itself with versions before
	 * the inclusion of this code (since they'll not be querying it). When
	 * future branches are depriciated point the REST resource too the
	 * latest branch ('ln -s versions/2.2 versions/2.1' for example)
	 */

        global $minutes_between_checks;
	global $resturl;
	global $wp_version;

	$branch = branch_from_version();

        $last_version_check = get_option("wp_version_check_time");

        if($last_version_check == "" || time() > $minutes_between_checks*60 + $last_version_check){
		$c = curl_init($resturl.$branch);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 2);
		curl_setopt($c, CURLOPT_TIMEOUT, 3);
		$r = curl_exec($c);
		curl_close($c);
		if(!$r) return $r;
                update_option("wp_version_check_time",time());
                update_option("wp_version_check_results",$r);
        }
	
	/* do we need to trigger hooks? */
	list($latest,$m,$blog) = split(",",get_latest_version());
	if(version_compare($latest,$wp_version)==1 && !get_option("wp_version_notified")) {
		do_action('update_notification',$wp_version,$latest);
		update_option("wp_version_notified",1);
	}
	if(version_compare($latest,$wp_version)!=1) {
		update_option("wp_version_notified",0);
	}

	return true;
}

function get_latest_version(){
	// Retrieve the latet version from the options table. Return false if
	// it's not there.
	$version = get_option("wp_version_check_results");
	if($version == "") return false;
	else return $version;
}

function latest_version(){
        global $wp_version;
	global $upgradeurl;
	global $message;

        //Check to see if we're running a development version.
        if (preg_match("/[a-zA-Z]/",$wp_version)>0) {
        	echo "<div id=\"wpurg\">".__("\"Please do not shoot the developer. He is doing his best\" - you are runing an unsupported development version of WordPress.")."</div>";
                return;
        }
	//We're not. Check the options database, failing gracefully.
	set_latest_version(); //If we go with cron, remove this line
        list($latest,$m,$blog) = split(",",get_latest_version());
        if(!$latest) return;
        if(version_compare($latest,$wp_version)==1) echo "<div id=\"wpurg\">".__("Please ") . "<a href=\"$upgradeurl\">" . __("upgrade to version "). $latest. "</a>. $message[$m] " . "(<a href=\"$blog\">". __("detailed information") . "</a>)." ."</div>";
        return;
}

// This code schedules an hourly cron based update. If this is ever enabled,
// don't forget to remove the marked line above.

/*
 *add_action('set_latest_version_hook', 'set_latest_version');
 *if (!wp_next_scheduled('set_latest_version_hook')) {
 *	wp_schedule_event( time(), 'hourly', 'set_latest_version_hook' );
 *}
 */

?>

