-
Notifications
You must be signed in to change notification settings - Fork 2
/
gravatar.php
116 lines (101 loc) · 3.23 KB
/
gravatar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
use Laravel\Config;
use Laravel\HTML;
/**
* Gravatar bundle for the Laravel PHP framework.
*
* @author Drew Johnston
* @copyright (c) 2012 - Drew Johnston
*
* More info at: http://github.com/drewjoh/laravel-gravatar
*/
class Gravatar
{
/**
* @var string - A temporary internal cache of the URL parameters to use.
*/
protected static $config_cache = null;
/*
* @var string - URL constants for the avatar images
*/
const HTTP_URL = 'http://www.gravatar.com/avatar/';
const HTTPS_URL = 'https://secure.gravatar.com/avatar/';
/**
* Get a specific config option value
* @return string - the request config option if exists.
*/
public static function config($item)
{
return ( Config::has( 'gravatar.'.$item ) ) ? Config::get( 'gravatar.'.$item ) : null;
}
/**
* Build the avatar URL based on the provided email address and optional arguments.
*
* @param string $email - The email to get the gravatar for.
* @return string - The XHTML-safe URL to the gravatar.
*/
public static function build_url( $email, $size = null, $secure = null)
{
// If we haven't used this class yet, pre-load our config cache
if(self::$config_cache === null)
self::$config_cache = Config::get('common::gravatar');
// Start building the URL, and deciding if we're doing this via HTTPS or HTTP.
if(
(self::$config_cache['auto_detect_ssl'] AND
(isset($_SERVER['HTTPS']) AND $_SERVER['HTTPS'])
) OR
self::$config_cache['use_secure_url']
OR
$secure == true)
$url = static::HTTPS_URL;
else
$url = static::HTTP_URL;
// Tack the email hash onto the end of our Gravatar URL, then add our additional options.
$url .= self::get_email_hash($email)
.'?'
.(self::$config_cache['force_default'] != false ? '&f=y' : '')
.(self::$config_cache['max_rating'] != false ? '&r='.self::$config_cache['max_rating'] : '')
.((self::$config_cache['size'] != false OR $size != null) ? '&s='.($size !== null ? $size : self::$config_cache['size']) : '')
.(self::$config_cache['default_image'] != false ? '&d='.self::$config_cache['default_image'] : '');
return $url;
}
/**
* Get the email hash to use (after cleaning the string).
*
* @param string $email - The email to get the hash for.
* @return string - The hashed form of the email, post cleaning.
*/
public static function get_email_hash($email)
{
// Hash created based on gravatar docs.
return hash( 'md5', strtolower( trim( $email ) ) );
}
/**
* Get the gravatar
*/
public static function get( $email, $size = null )
{
return self::build_url( $email, $size );
}
/**
* Get the gravatar with forced secure connection
*/
public static function get_secure( $email, $size = null )
{
return self::build_url( $email, $size, true );
}
/**
* Get the gravatar and return as image
*/
public static function get_image( $email, $size = null, $alt = '', $attributes = array() )
{
return HTML::image( self::build_url( $email, $size ), $alt, $attributes );
}
/**
* Get the gravatar with forced secure connection and return as image
*/
public static function get_secure_image( $email, $size = null, $alt = '', $attributes = array() )
{
return HTML::image( self::build_url( $email, $size, true ), $alt, $attributes );
}
}