This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
forked from hharnett/cakephp-gravatar-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gravatar.php
139 lines (124 loc) · 2.38 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Gravatar Helper
*
* @author Hayden Harnett
* @version 1.1
* @lastupdated 20091002
* @url http://hayden.id.au/
*/
/**
* Import the Security Component
*/
App::import(array('Security'));
class GravatarHelper extends AppHelper
{
/**
* Array of helpers needed
*
* @var array
*/
public $helpers = array('Html');
/**
* Base gravatar url
*
* @var string
*/
private $_url = 'http://www.gravatar.com/avatar/';
/**
* Size of the image
*
* @var string
*/
private $_size = 80;
/**
* Gravatar rating
*
* @var string
*/
private $_rating = 'pg';
/**
* Array of possible ratings
*
* @var string
*/
private $_possibleRatings = array('g', 'pg', 'r', 'x');
/**
* Default email hash
*
* @var string
*/
private $_default = 'ad516503a11cd5ca435acc9bb6523536';
/**
* Hash type used
*
* @var string
*/
private $_hashType = 'md5';
/**
* Validate the options array
*
* @param string $options
* @return void
* @author Hayden Harnett
*/
private function _validateOptions($options)
{
if (isset($options['rating']))
{
if (in_array($this->_rating, $this->_possibleRatings))
{
$this->_rating = $options['rating'];
}
}
if (isset($options['size']))
{
if (is_numeric($options['size']) && $options['size'] >=1 && $options['size'] <= 512)
{
$this->_size = $options['size'];
}
}
if (isset($options['default']))
{
if (strpos('http://', $options['default']) == 0 || strpos('https://', $options['default'] == 0))
{
$this->_default = h($options['default']);
}
else
{
$this->_default = _hashEmail($options['default']);
}
}
}
/**
* Hash the email address
*
* @param string $email
* @return hash
* @author Hayden Harnett
*/
private function _hashEmail($email)
{
return Security::hash(strtolower($email), $this->_hashType);
}
/**
* undocumented function
*
* @param string $email
* @param string $options
* @param string $html_options
* @return HTML image tag of the gravatar
* @author Hayden Harnett
*/
public function image($email, $options, $html_options = array())
{
$this->_validateOptions($options);
$get = "?s=".$this->_size."&r=".$this->_rating;
if (!empty($this->_default))
{
$get .= "&d=".$this->_default;
}
return $this->Html->image($this->_url.$this->_hashEmail($email).$get, $html_options);
}
}
?>