/**
 * @name Gravatar 
 * @version 2
 * @author Oleg Pryadko <oleg@websitezen.com>
 * @description Gravatar snippet for MODx Revolution
 * @license Public Domain, use as you like (except possibly class file, which is based on http://www.talkphp.com/script-giveaway/1905-gravatar-wrapper-class.html)
 * @history version 1 by lossendae <lossendae@gmail.com>
*/

// Variables
$email = $modx->getOption('email', $scriptProperties, $modx->getOption('emailsender', null, ''));
$size = $modx->getOption('size', $scriptProperties, '80');
$site_default = $modx->getOption('gravatar_default',null,'');
if (!$site_default) {
  $default = $modx->getOption('default', $scriptProperties, $modx->getOption('image', $scriptProperties, $modx->getOption('icon', $scriptProperties, 'wavatar')));
} else {$default = $site_default;}
$secure = $modx->getOption('secure', $scriptProperties, '');
$rating = $modx->getOption('rating', $scriptProperties, 'g');
$profile = $modx->getOption('profile', $scriptProperties, false);
$extraParams = $modx->fromJSON($modx->getOption('extraParams', $scriptProperties, '{}'));

/* Output Filter Properties */
if(isset($scriptProperties['input'])){
  $email = $scriptProperties['input'];
}
if(isset($scriptProperties['options'])){
  if (is_numeric($scriptProperties['options'])) {
    $size = $scriptProperties['options'];
  } else {
    $profile = $scriptProperties['options'];
  }
}

/* check for email, or get user's email if email is a number (userid) */
if (!empty($email) && is_numeric($email)) {
  $email = intval($email);
  $userObject = $modx->getObject('modUser',$email);
  $userProfile = $userObject->getOne('Profile');
  $email = $userProfile->get('email');
}
if (empty($email)) {return '';}

/* Include Class Files and create class instance*/
$base_path = MODX_CORE_PATH.'/components/gravatar/';  
$f = $base_path.'gravatar_class.php';
if (file_exists($f)) {include_once $f;} else {$modx->log(modX::LOG_LEVEL_ERROR,'Gravatar class not found at: '.$f);}
$pAvatar = new TalkPHP_Gravatar();

/* Set Email */
$pAvatar->setEmail($email);
if(!$profile) {$pAvatar->setSize($size);}

/* Set Default Type */
if ($default == 1 || strtolower($default) == 'identicon') {
  $pAvatar->setDefaultImageAsIdentIcon();
} elseif ($default == 2 || strtolower($default) == 'monster' || strtolower($default) == 'monsterid') {
  $pAvatar->setDefaultImageAsMonsterId();
} elseif ($default == 3 || strtolower($default) == 'wavatar') {
  $pAvatar->setDefaultImageAsWavatar();
} elseif ($default == 4 || strtolower($default) == 'mystery' || strtolower($default) == 'mm') {
  $pAvatar->setDefaultImageAsMystery();
} elseif ($default == 5 || strtolower($default) == 'retro') {
  $pAvatar->setDefaultImageAsRetro();
} elseif (is_string($default) && !$profile) {
  $pAvatar->setDefaultImageAsCustom($default);
}

/* Secure URLs */
if ($secure) {
  $pAvatar->setSecure();
} elseif ($secure != '0' && $secure != 'false') {
  $url = $_SERVER['REQUEST_URI'];
  $ssl = FALSE;
  if($_SERVER['HTTPS'] == 1)  {
    $ssl =  TRUE;
  } elseif ($_SERVER['HTTPS'] == 'on'){
    $ssl =  TRUE;
  } elseif ($_SERVER['SERVER_PORT'] == 443) {
    $ssl = TRUE;
  }
  if ($ssl) {
    $pAvatar->setSecure();
  }
}

/* Set Rating */
if (strtolower($rating) == 'g') {$pAvatar->setRatingAsG();}
elseif (strtolower($rating) == 'pg') {$pAvatar->setRatingAsPG();}
elseif (strtolower($rating) == 'r') {$pAvatar->setRatingAsR();}
elseif (strtolower($rating) == 'x') {$pAvatar->setRatingAsX();}
  
/* append extra options to the URL (making sure to start with ? if we are outputting a profile URL) */
if (is_array($extraParams) && !empty($extraParams)) {
  if (!$profile) {$isstart = 0;} else {$isstart = 1;}
  $pAvatar->setExtraParams($extraParams,$isstart);
}

/* Output URL */
if ($profile) {
  $pAvatar->setFiletype($profile);
  $output = $pAvatar->getProfile();
} else {
  $output = $pAvatar->getAvatar();
}
unset($pAvatar);
return $output;