├── LICENSE.txt ├── README.md ├── config ├── gravatar.php └── index.html ├── helpers ├── gravatar_helper.php └── index.html └── libraries ├── Gravatar.php └── index.html /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 - 2023 Ivan Tcholakov 4 | Copyright (c) 2011 - 2015 Ryan Marshall 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Gravatar Library for Codeigniter 2 | ================================ 3 | 4 | Usage 5 | ----- 6 | 7 | Before usage simply load the library as usual in your controller or other context. 8 | 9 | ```php 10 | $this->load->library('gravatar'); 11 | ``` 12 | 13 | After that you can call its public methods: 14 | 15 | ```php 16 | echo '
'; 17 | echo '
'; 18 | 19 | $email = 'put_email@here.com'; 20 | 21 | $gravatar_url = $this->gravatar->get($email); 22 | 23 | var_dump($gravatar_url); 24 | 25 | echo '
'; 26 | echo '
'; 27 | 28 | echo ''; 29 | 30 | echo '
'; 31 | echo '
'; 32 | 33 | $gravatar_profile = $this->gravatar->get_profile_data($email); 34 | 35 | echo '
';
36 | echo print_r($gravatar_profile, true);
37 | echo '
'; 38 | 39 | echo '
'; 40 | echo '
'; 41 | 42 | $last_error = $this->gravatar->last_error(); 43 | 44 | echo '
';
45 | echo print_r($last_error, true);
46 | echo '
'; 47 | 48 | echo '
'; 49 | echo '
'; 50 | ``` 51 | 52 | This library has been documented inside its source. For overall information about accessing gravatars see https://en.gravatar.com/site/implement/ 53 | 54 | Requirements 55 | ------------ 56 | 57 | CodeIgniter 2.x or CodeIgniter 3.x 58 | 59 | License Information 60 | ------------------- 61 | 62 | Copyright (c) 2015 - 2023 Ivan Tcholakov, ivantcholakov@gmail.com 63 | Copyright (c) 2011 - 2015 Ryan Marshall, http://irealms.co.uk 64 | License: The MIT License (MIT), http://opensource.org/licenses/MIT 65 | -------------------------------------------------------------------------------- /config/gravatar.php: -------------------------------------------------------------------------------- 1 | , 2015 - 2016 7 | * @license The MIT License, http://opensource.org/licenses/MIT 8 | */ 9 | 10 | // For more information see http://en.gravatar.com/site/implement/images/ 11 | 12 | $config['gravatar_base_url'] = 'http://www.gravatar.com/'; 13 | $config['gravatar_secure_base_url'] = 'https://secure.gravatar.com/'; 14 | $config['gravatar_image_extension'] = '.png'; // '', '.png' or '.jpg'. 15 | $config['gravatar_image_size'] = 80; 16 | 17 | $config['gravatar_default_image'] = ''; // '', '404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro', 'blank'. 18 | //$config['gravatar_default_image'] = DEFAULT_BASE_URL.'assets/img/lib/default-person.png'; // Another possible option: a custon image. 19 | 20 | $config['gravatar_force_default_image'] = false; 21 | $config['gravatar_rating'] = ''; // '', 'g' (default), 'pg', 'r', 'x'. 22 | 23 | // Useragent string for server-made requests. 24 | // It is for not getting 403 forbidden response. 25 | $config['gravatar_useragent'] = 'PHP Gravatar Library'; 26 | -------------------------------------------------------------------------------- /config/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 403 Forbidden 5 | 6 | 7 | 8 |

Directory access is forbidden.

9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /helpers/gravatar_helper.php: -------------------------------------------------------------------------------- 1 | , 2015 5 | * @license The MIT License, http://opensource.org/licenses/MIT 6 | */ 7 | 8 | if (!function_exists('gravatar')) { 9 | 10 | // This helper function has been added here for compatibility with PyroCMS. 11 | function gravatar($email = '', $size = 50, $rating = 'g', $url_only = false, $default = false) { 12 | 13 | $ci = & get_instance(); 14 | $ci->load->library('gravatar'); 15 | 16 | if (@ (string) $default == '') { 17 | $default = null; 18 | } 19 | 20 | $gravatar_url = $ci->gravatar->get($email, $size, $default, null, $rating); 21 | 22 | if ($url_only) { 23 | 24 | return $gravatar_url; 25 | } 26 | 27 | return 'Gravatar'; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /helpers/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 403 Forbidden 5 | 6 | 7 | 8 |

Directory access is forbidden.

9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /libraries/Gravatar.php: -------------------------------------------------------------------------------- 1 | , 2015 - 2023 7 | * @author Ryan Marshall , 2011 - 2015, @link http://irealms.co.uk 8 | * 9 | * Code repository: @link https://github.com/ivantcholakov/Codeigniter-Gravatar 10 | * 11 | * @version 1.2.0 12 | * 13 | * @license The MIT License (MIT) 14 | * @link http://opensource.org/licenses/MIT 15 | */ 16 | 17 | // Gravatar pofile error results. 18 | defined('GRAVATAR_NO_ERROR') OR define('GRAVATAR_NO_ERROR', 0); 19 | defined('GRAVATAR_CANT_CONNECT') OR define('GRAVATAR_CANT_CONNECT', 1); 20 | defined('GRAVATAR_INVALID_EMAIL') OR define('GRAVATAR_INVALID_EMAIL', 2); 21 | defined('GRAVATAR_PROFILE_DOES_NOT_EXIST') OR define('GRAVATAR_PROFILE_DOES_NOT_EXIST', 3); 22 | defined('GRAVATAR_INCORRECT_FORMAT') OR define('GRAVATAR_INCORRECT_FORMAT', 4); 23 | 24 | class Gravatar { 25 | 26 | protected $defaults; 27 | 28 | protected $gravatar_base_url; 29 | protected $gravatar_secure_base_url; 30 | protected $gravatar_image_extension; 31 | protected $gravatar_image_size; 32 | protected $gravatar_default_image; 33 | protected $gravatar_force_default_image; 34 | protected $gravatar_rating ; 35 | protected $gravatar_useragent; 36 | 37 | protected $last_error = GRAVATAR_NO_ERROR; 38 | 39 | protected $is_https; 40 | protected $curl_exists; 41 | protected $allow_url_fopen; 42 | 43 | public function __construct($config = array()) { 44 | 45 | $this->defaults = array( 46 | 'gravatar_base_url' => 'http://www.gravatar.com/', 47 | 'gravatar_secure_base_url' => 'https://secure.gravatar.com/', 48 | 'gravatar_image_extension' => '.png', 49 | 'gravatar_image_size' => 80, 50 | 'gravatar_default_image' => '', 51 | 'gravatar_force_default_image' => false, 52 | 'gravatar_rating' => '', 53 | 'gravatar_useragent' => 'PHP Gravatar Library', 54 | ); 55 | 56 | $this->is_https = $this->is_https(); 57 | $this->curl_exists = function_exists('curl_init'); 58 | $allow_url_fopen = @ini_get('allow_url_fopen'); 59 | $allow_url_fopen = $allow_url_fopen === false || in_array(strtolower((string) $allow_url_fopen), array('on', 'true', '1')); 60 | $this->allow_url_fopen = $allow_url_fopen; 61 | 62 | if (!is_array($config)) { 63 | $config = array(); 64 | } 65 | 66 | $this->defaults = array_merge($this->defaults, $config); 67 | $this->initialize($this->defaults); 68 | } 69 | 70 | public function initialize($config = array()) { 71 | 72 | if (!is_array($config)) { 73 | $config = array(); 74 | } 75 | 76 | foreach ($config as $key => $value) { 77 | $this->{$key} = $value; 78 | } 79 | 80 | $this->gravatar_base_url = (string) $this->gravatar_base_url; 81 | $this->gravatar_secure_base_url = (string) $this->gravatar_secure_base_url; 82 | $this->gravatar_image_extension = (string) $this->gravatar_image_extension; 83 | 84 | $this->gravatar_image_size = (int) $this->gravatar_image_size; 85 | 86 | if ($this->gravatar_image_size <= 0) { 87 | $this->gravatar_image_size = 80; 88 | } 89 | 90 | $this->gravatar_default_image = (string) $this->gravatar_default_image; 91 | $this->gravatar_force_default_image = !empty($this->gravatar_force_default_image); 92 | $this->gravatar_rating = (string) $this->gravatar_rating; 93 | $this->gravatar_useragent = (string) $this->gravatar_useragent; 94 | 95 | return $this; 96 | } 97 | 98 | public function reset() { 99 | 100 | $this->initialize($this->defaults); 101 | 102 | return $this; 103 | } 104 | 105 | public function get_defaults() { 106 | 107 | return $this->defaults; 108 | } 109 | 110 | /** 111 | * Creates a URL for requesting a Gravatar image. 112 | * @link http://en.gravatar.com/site/implement/images/ 113 | * 114 | * @param string $email A registered email. 115 | * @param int $size The requested size of the avarar in pixels (a square image). 116 | * @param string $default_image The fallback image option: '', '404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro', 'blank'. 117 | * @param bool $force_default_image Enforces the fallback image to be shown. 118 | * @param string $rating The level of allowed self-rate of the avatar: '', 'g' (default), 'pg', 'r', 'x'. 119 | * @return string Returns the URL of the avatar to be requested. 120 | * 121 | * When optional parameters are not set, their default values are taken 122 | * from the configuration file application/config/gravatar.php 123 | */ 124 | public function get($email, $size = null, $default_image = null, $force_default_image = null, $rating = null) { 125 | 126 | $url = ($this->is_https ? $this->gravatar_secure_base_url : $this->gravatar_base_url).'avatar/'.$this->create_hash($email).$this->gravatar_image_extension; 127 | 128 | $query = array(); 129 | 130 | $size = (int) $size; 131 | 132 | if ($size <= 0) { 133 | $size = $this->gravatar_image_size; 134 | } 135 | 136 | if ($size > 0) { 137 | $query['s'] = $size; 138 | } 139 | 140 | if (isset($default_image)) { 141 | $default_image = (string) $default_image; 142 | } else { 143 | $default_image = $this->gravatar_default_image; 144 | } 145 | 146 | if ($default_image != '') { 147 | $query['d'] = $default_image; 148 | } 149 | 150 | if (isset($force_default_image)) { 151 | $force_default_image = !empty($force_default_image); 152 | } else { 153 | $force_default_image = $this->gravatar_force_default_image; 154 | } 155 | 156 | if ($force_default_image) { 157 | $query['f'] = 'y'; 158 | } 159 | 160 | if (isset($rating)) { 161 | $rating = (string) $rating; 162 | } else { 163 | $rating = $this->gravatar_rating; 164 | } 165 | 166 | if ($rating != '') { 167 | $query['r'] = $rating; 168 | } 169 | 170 | if (!empty($query)) { 171 | $url = $url.'?'.http_build_query($query); 172 | } 173 | 174 | return $url; 175 | } 176 | 177 | /** 178 | * Executes a request for Gravatar profile data and returns it as a multidimensional array. 179 | * @link https://docs.gravatar.com/profiles/php/ 180 | * 181 | * @param string $email A registered email. 182 | * @return array/null Received profile data. 183 | */ 184 | public function get_profile_data($email) { 185 | 186 | $result = $this->execute_profile_request($email, 'php'); 187 | 188 | if ($this->last_error != GRAVATAR_NO_ERROR) { 189 | 190 | return null; 191 | } 192 | 193 | $result = @ unserialize($result); 194 | 195 | if ($result === false) { 196 | 197 | $this->last_error = GRAVATAR_INCORRECT_FORMAT; 198 | 199 | return null; 200 | } 201 | 202 | if (!is_array($result)) { 203 | 204 | $this->last_error = GRAVATAR_PROFILE_DOES_NOT_EXIST; 205 | 206 | return null; 207 | } 208 | 209 | if (!isset($result['entry']) || !isset($result['entry'][0])) { 210 | 211 | $this->last_error = GRAVATAR_INCORRECT_FORMAT; 212 | 213 | return null; 214 | } 215 | 216 | return $result['entry'][0]; 217 | } 218 | 219 | /** 220 | * Executes a request for Gravatar profile data and returns raw received response. 221 | * @link https://docs.gravatar.com/profiles/php/ 222 | * 223 | * @param string $email A registered email. 224 | * @param string $format '', 'json', 'xml', 'php', 'vcf', 'qr'. 225 | * @return string/null Received profile raw data. 226 | */ 227 | public function execute_profile_request($email, $format = null) { 228 | 229 | $this->last_error = GRAVATAR_NO_ERROR; 230 | 231 | if (function_exists('valid_email')) { 232 | 233 | if (!valid_email($email)) { 234 | 235 | $this->last_error = GRAVATAR_INVALID_EMAIL; 236 | 237 | return null; 238 | } 239 | 240 | } else { 241 | 242 | if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 243 | 244 | $this->last_error = GRAVATAR_INVALID_EMAIL; 245 | 246 | return null; 247 | } 248 | } 249 | 250 | $format = trim((string) $format); 251 | 252 | if ($format != '') { 253 | $format = '.'.ltrim($format, '.'); 254 | } 255 | 256 | $url = $this->gravatar_secure_base_url.$this->create_hash_sha256($email).$format; 257 | 258 | $result = null; 259 | 260 | if ($this->curl_exists) { 261 | 262 | $ch = curl_init(); 263 | 264 | $options = array( 265 | CURLOPT_USERAGENT => $this->gravatar_useragent, 266 | CURLOPT_RETURNTRANSFER => true, 267 | CURLOPT_URL => $url, 268 | CURLOPT_TIMEOUT => 5, 269 | CURLOPT_SSL_VERIFYPEER => false, 270 | ); 271 | 272 | if (!ini_get('safe_mode') && !ini_get('open_basedir')) { 273 | $options[CURLOPT_FOLLOWLOCATION] = true; 274 | } 275 | 276 | curl_setopt_array($ch, $options); 277 | 278 | $result = curl_exec($ch); 279 | 280 | $code = @ curl_getinfo($ch, CURLINFO_HTTP_CODE); 281 | 282 | @ curl_close($ch); 283 | 284 | if ($code != 200) { 285 | 286 | $this->last_error = GRAVATAR_CANT_CONNECT; 287 | 288 | return null; 289 | } 290 | 291 | } elseif ($this->allow_url_fopen) { 292 | 293 | $options = array( 294 | 'http' => array( 295 | 'method' => 'GET', 296 | 'useragent' => $this->gravatar_useragent, 297 | ), 298 | ); 299 | 300 | $context = stream_context_create($options); 301 | 302 | $result = @ file_get_contents($url, false, $context); 303 | 304 | } else { 305 | 306 | $this->last_error = GRAVATAR_CANT_CONNECT; 307 | 308 | return null; 309 | } 310 | 311 | if ($result === false) { 312 | 313 | $this->last_error = GRAVATAR_CANT_CONNECT; 314 | 315 | return null; 316 | } 317 | 318 | return $result; 319 | } 320 | 321 | /** 322 | * Returns the error code as a result of the last profile request operation. 323 | * 324 | * @return int GRAVATAR_NO_ERROR - the last operation was successfull, 325 | * other returned value indicates failure. 326 | */ 327 | public function last_error() { 328 | 329 | return $this->last_error; 330 | } 331 | 332 | /** 333 | * Creates a hash value from a provided e-mail address. 334 | * @link https://docs.gravatar.com/gravatar-images/php/ 335 | * 336 | * @param string $email A registered email. 337 | * @return string/null The hash for accessing the avatar or profile data. 338 | */ 339 | public function create_hash($email) { 340 | 341 | return md5(strtolower(trim((string) $email))); 342 | } 343 | 344 | protected function is_https() { 345 | 346 | if (function_exists('is_https')) { 347 | return is_https(); 348 | } 349 | 350 | if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { 351 | 352 | return true; 353 | 354 | } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { 355 | 356 | return true; 357 | 358 | } elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') { 359 | 360 | return true; 361 | } 362 | 363 | return false; 364 | } 365 | 366 | /** 367 | * Creates a sha256 hash value from a provided e-mail address. 368 | * @link https://docs.gravatar.com/general/hash/ 369 | * 370 | * @param string $email A registered email. 371 | * @return string/null The hash for accessing the avatar or profile data. 372 | */ 373 | public function create_hash_sha256($email) { 374 | 375 | return hash('sha256', strtolower(trim((string) $email))); 376 | } 377 | 378 | } 379 | -------------------------------------------------------------------------------- /libraries/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 403 Forbidden 5 | 6 | 7 | 8 |

Directory access is forbidden.

9 | 10 | 11 | 12 | --------------------------------------------------------------------------------