<?php
function smarty_cms_function_gravatar($params, &$smarty)
{
  $url = 'http://www.gravatar.com/avatar/';

  if( !isset($params['email']) )
    {
      // required parameter.
      return;
    }

  $email = strtolower(trim($params['email']));
  $md5 = md5($email);
  $url .= $md5;

  $parms = array();
  if( isset($params['size']) )
    {
      $parms[] = 's='.(int)$params['size'];
    }
  if( isset($params['rating']) )
    {
      $tmp = strtolower(trim($params['rating']));
      $ratings = array('g','pg','r','x');
      if( in_array($tmp,$ratings) )
	{
	  $parms[] = 'r='.$tmp;
	}
    }
  if( isset($params['default']) )
    {
      $tmp = trim($params['default']);
      $defaults = array('identicon','monsterid','wavatar');
      if( in_array($tmp,$defaults) )
	{
	  $parms[] = 'd='.$tmp;
	}
      else
	{
	  $parms[] = 'd='.urlencode($tmp);
	}
    }
  if( count($parms) )
    {
      $url .= '?'.implode('&amp;',$parms);
    }
  if( !endswith($url,'jpg') && !isset($params['noext']) )
    {
      $url .= '.jpg';
    }

  $txt = $url;
  if( !isset($params['urlonly']) )
    {
      $pretxt = '<img src="'.$url.'"';
      if( isset($params['id']) )
	{
	  $pretxt .= ' id="'.trim($params['id']).'"';
	}
      if( isset($params['class']) )
	{
	  $pretxt .= ' class="'.trim($params['class']).'"';
	}
      if( isset($params['width']) )
	{
	  $pretxt .= ' width="'.trim($params['width']).'"';
	}
      if( isset($params['height']) )
	{
	  $pretxt .= ' height="'.trim($params['height']).'"';
	}
      $txt = $pretxt.'/>';
    }

  if( isset($params['assign']) )
    {
      $smarty->assign(trim($params['assign']),$txt);
      return;
    }
  return $txt;
}


function smarty_cms_help_function_gravatar()
{
?>
<h3>What does this do?</h3>
<p>This plugin allows displaying your <a href="http://www.gravatar.com" title="gravatar">globally recognized avatar</a> in a CMS Made Simple page.</p>
<h3>How do I use it?</h3>
<p>Just insert the tag into your page like: <code>{gravatar email='example@somewhere.com'}</code>.</p>
<h3>What parameters does it take?</h3>
<ul>
 <li><strong>(required)</strong> email - The email address that is registered with gravatar.com</li>
 <li>size - an integer value representing the desired size of the image to request from gravatar.  The default value is 80</li>
 <li>rating - the rating of the avatar to request from gravatar.  Possible values are g,pg,r,x.  The default value is g.</li>
 <li>default - the default avatar to use.<br/>
     possible values are identicon,monsterid,wavatar, or a custom url to a jpg.</li>
 <li>noext - if this parameter is not specified, the plugin will add .jpg to the generated url.</li>
 <li>urlonly - do not generate an image tag, return only the generated url.</li>
 <li>id - specify an id for the image tag.</li>
 <li>class - specify a css class for the image tag.</li>
 <li>height - specify the height of the image tag.</li>
 <li>width - specify the width of the image tag.</li>
</ul>
<?php
}

function smarty_cms_about_function_gravatar() {
	?>
	<p>Author: Robert Campbell&lt;calguy1000@cmsmadesimple.org&gt;</p>
	<p>Version: 1.0</p>
	<?php
}

#
# EOF
#
?>
