Enabling Gravatars on pMachine Pro

by on Feb.27, 2006, under Tech Tips

This is a simple hack that will get gravatars displayed on you pMachine pro comments, it will also cache them locally incase gravatar.com goes down.

open your pMachine/pm/lib/comments.fns.php
find the function weblog_comments()

insert this line

$gravatarlink=GetGravatar($email);

at about line 612

it should look something like

$if_email				= '';
$if_email_as_link		= '';
$if_email_or_url_as_link	= '';
$if_email_as_name		= $name;
$if_email_or_url_as_name	= $name;
$if_url_or_email_as_name	= $name;
$if_url_as_name		= $name;

$gravitarlink=GetGravatar($email);

further down in the function find the bit that defines the tags to go in the comment template around line 780 and add

'%%gravatar%%'				=> $gravatarlink

so it will look like

'0'					=> date("Z",$t_stamp),
''			=> $avatar,
'Member'		=> $moderation_level,
'%%gravatar%%'			=> $gravatarlink
);

once thats done you can now include the %%gravatar%% tag into your comment display template to display the commentors gravatar

Add these functions to the bottom of the file

function wp_gravatar_info($md5 = '') {
//taken from gravatar plugin for wordpress
if ('' == $md5) { return false; }
$r = array();
$foo = @file("http://www.gravatar.com/info/md5/$md5");
if (! $foo) return false;
array_shift($foo); // strip leading  declaration
array_shift($foo); // strip opening
array_pop($foo);   // strip closing
foreach ($foo as $bar) {
$matched = array();
preg_match_all("/([^])+/", $bar, $matched);
$r[$matched[0][1]] = $matched[0][2];
}
return $r;
}

function Download_Gravatar($id, $local, $remote)
{
// we don't know about this gravatar yet, let's look for it
$response = wp_gravatar_info($id);
if ('200' == $response
)

{

// it's not an error, so let's make a local copy

if ( (is_writeable($local)) && (ini_get('allow_url_fopen')) )

{

$cached = copy ($remote.$id, $local.$id.".TMP");

if (! $cached)

{

// looks like the copy failed, delete the TMP

unlink($local.$id.".TMP");

return 0;

} else

{

// we copied successfully

rename($local.$id.".TMP", $local.$id);

return 1;

}

}

} else {

return 0;

}

}



function Gravitar_Cached($id, $local, $remote)

{

$cachetime="34200";

if(file_exists($local.$id))

//if the file exists on the server

{

//check its date

$now=time();

$ftime=filemtime($local.$id);

//echo "boo".filectime($remote.$id);

if($now-$ftime>$cachetime)

//if the time difference is more then the cache time then get a newer copy

{

//download it

Download_Gravatar($id, $local, $remote);

}

return 1;

}else

{

//download it its new

return Download_Gravatar($id, $local, $remote);

}

}



function GetGravatar($email)

//function to return the gravitar requiered

{

$gravatar_url="http://www.gravatar.com/avatar.php?gravatar_id=";

$gravatar_id=md5($email);

$gravatar_local=$_SERVER['DOCUMENT_ROOT'] . "/pMachine/gravatarcache/";

$gravatar_local_url=$_SERVER['SERVER_ROOT'] . "/pMachine/gravatarcache/";

$gravatar_none=$_SERVER['SERVER_ROOT'] . "/pMachine/gravatarcache/none.gif";

if(Gravitar_Cached($gravatar_id, $gravatar_local, $gravatar_url))

{

$gravatar_path=$gravatar_local_url.$gravatar_id;

$gravatar = "Gravatar";

}else

{

$gravatar_path=$gravatar_none;

$gravatar = "<a xhref='http://www.gravatar.com' target='_blank' title='Get your own gravatar from www.gravatar.com'><img  class='gravatar' alt='Get your own gravatar from www.gravatar.com' width=80 height=80 xsrc='$gravatar_path'>";

}

return $gravatar;

}

you will need to create a /pMachine/gravatarcache directory on your server and add a none.gif to represents an unknown gravatar in the even one isn’t returned by the server (I suspect theres a flaw here but I havnt been able to test it)

You might want to modify the image using css and modify the template code to get things to show how you want


Comments are closed.