Skip to content

Commit

Permalink
RELEASE 1.2.0: Correctionsq removing deprecated code. Tested on PHP 8.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivantcholakov committed Dec 5, 2023
1 parent 9db68e4 commit 478fa3d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 168 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 - 2022 Ivan Tcholakov <[email protected]>
Copyright (c) 2015 - 2023 Ivan Tcholakov <[email protected]>
Copyright (c) 2011 - 2015 Ryan Marshall <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,9 @@ Requirements

CodeIgniter 2.x or CodeIgniter 3.x

Live Demo
---------

http://iridadesign.com/starter-public-edition-4/www/playground/gravatar-test

License Information
-------------------

Copyright (c) 2015 - 2022 Ivan Tcholakov, [email protected]
Copyright (c) 2015 - 2023 Ivan Tcholakov, [email protected]
Copyright (c) 2011 - 2015 Ryan Marshall, http://irealms.co.uk
License: The MIT License (MIT), http://opensource.org/licenses/MIT
1 change: 1 addition & 0 deletions helpers/gravatar_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function gravatar($email = '', $size = 50, $rating = 'g', $url_only = false, $de
$gravatar_url = $ci->gravatar->get($email, $size, $default, null, $rating);

if ($url_only) {

return $gravatar_url;
}

Expand Down
191 changes: 30 additions & 161 deletions libraries/Gravatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
/**
* Gravatar Library for CodeIgniter
*
* @author Ivan Tcholakov <[email protected]>, 2015 - 2022
* @author Ivan Tcholakov <[email protected]>, 2015 - 2023
* @author Ryan Marshall <[email protected]>, 2011 - 2015, @link http://irealms.co.uk
*
* Code repository: @link https://github.com/ivantcholakov/Codeigniter-Gravatar
*
* @version 1.1.3
* @version 1.2.0
*
* @license The MIT License (MIT)
* @link http://opensource.org/licenses/MIT
Expand Down Expand Up @@ -56,7 +56,7 @@ public function __construct($config = array()) {
$this->is_https = $this->is_https();
$this->curl_exists = function_exists('curl_init');
$allow_url_fopen = @ini_get('allow_url_fopen');
$allow_url_fopen = $allow_url_fopen === false || in_array(strtolower($allow_url_fopen), array('on', 'true', '1'));
$allow_url_fopen = $allow_url_fopen === false || in_array(strtolower((string) $allow_url_fopen), array('on', 'true', '1'));
$this->allow_url_fopen = $allow_url_fopen;

if (!is_array($config)) {
Expand Down Expand Up @@ -176,7 +176,7 @@ public function get($email, $size = null, $default_image = null, $force_default_

/**
* Executes a request for Gravatar profile data and returns it as a multidimensional array.
* @link https://en.gravatar.com/site/implement/profiles/
* @link https://docs.gravatar.com/profiles/php/
*
* @param string $email A registered email.
* @return array/null Received profile data.
Expand All @@ -186,6 +186,7 @@ public function get_profile_data($email) {
$result = $this->execute_profile_request($email, 'php');

if ($this->last_error != GRAVATAR_NO_ERROR) {

return null;
}

Expand All @@ -194,18 +195,21 @@ public function get_profile_data($email) {
if ($result === false) {

$this->last_error = GRAVATAR_INCORRECT_FORMAT;

return null;
}

if (!is_array($result)) {

$this->last_error = GRAVATAR_PROFILE_DOES_NOT_EXIST;

return null;
}

if (!isset($result['entry']) || !isset($result['entry'][0])) {

$this->last_error = GRAVATAR_INCORRECT_FORMAT;

return null;
}

Expand All @@ -214,7 +218,7 @@ public function get_profile_data($email) {

/**
* Executes a request for Gravatar profile data and returns raw received response.
* @link https://en.gravatar.com/site/implement/profiles/
* @link https://docs.gravatar.com/profiles/php/
*
* @param string $email A registered email.
* @param string $format '', 'json', 'xml', 'php', 'vcf', 'qr'.
Expand All @@ -229,6 +233,7 @@ public function execute_profile_request($email, $format = null) {
if (!valid_email($email)) {

$this->last_error = GRAVATAR_INVALID_EMAIL;

return null;
}

Expand All @@ -237,6 +242,7 @@ public function execute_profile_request($email, $format = null) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

$this->last_error = GRAVATAR_INVALID_EMAIL;

return null;
}
}
Expand All @@ -247,21 +253,20 @@ public function execute_profile_request($email, $format = null) {
$format = '.'.ltrim($format, '.');
}

$url = $this->gravatar_secure_base_url.$this->create_hash_sha256($email).$format;

$result = null;

if ($this->curl_exists) {

$url = $this->gravatar_secure_base_url.$this->create_hash($email).$format;

$ch = curl_init();

$options = array(
CURLOPT_USERAGENT => $this->gravatar_useragent,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array(),
CURLOPT_URL => $url,
CURLOPT_TIMEOUT => 3,
CURLOPT_TIMEOUT => 5,
CURLOPT_SSL_VERIFYPEER => false,
);

if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
Expand All @@ -279,13 +284,12 @@ public function execute_profile_request($email, $format = null) {
if ($code != 200) {

$this->last_error = GRAVATAR_CANT_CONNECT;

return null;
}

} elseif ($this->allow_url_fopen) {

$url = $this->gravatar_base_url.$this->create_hash($email).$format;

$options = array(
'http' => array(
'method' => 'GET',
Expand All @@ -300,12 +304,14 @@ public function execute_profile_request($email, $format = null) {
} else {

$this->last_error = GRAVATAR_CANT_CONNECT;

return null;
}

if ($result === false) {

$this->last_error = GRAVATAR_CANT_CONNECT;

return null;
}

Expand All @@ -325,7 +331,7 @@ public function last_error() {

/**
* Creates a hash value from a provided e-mail address.
* @link https://en.gravatar.com/site/implement/hash/
* @link https://docs.gravatar.com/gravatar-images/php/
*
* @param string $email A registered email.
* @return string/null The hash for accessing the avatar or profile data.
Expand All @@ -342,168 +348,31 @@ protected function is_https() {
}

if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {

return true;

} elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {

return true;

} elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {

return true;
}

return false;
}

//--------------------------------------------------------------------------
// The following original methods are kept here for backward compatibility.
// Consider them as deprecated.
//--------------------------------------------------------------------------


/**
* Set the email to be used, converting it into an md5 hash as required by gravatar.com
* Creates a sha256 hash value from a provided e-mail address.
* @link https://docs.gravatar.com/general/hash/
*
* @param string $email
*
* @return string|null Email hash or if email didn't validate then return NULL
*
* @deprecated
*/
public function set_email($email)
{
$email = trim(strtolower((string) $email));

if ( ! filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
{
return md5($email);
}

return NULL;
}

/**
* get_gravatar_url
*
* @see http://en.gravatar.com/site/implement/images/ for available options
*
* @param string $rating defaults to g
* @param string $size defaults to 80
* @param string $default_image default sets can be found on the above link
* @param boolean $secure set to TRUE if a secure url is required
*
* @return string gratavar url
*
* @deprecated
*/
public function get_gravatar($email, $rating = NULL, $size = NULL, $default_image = NULL, $secure = NULL)
{
$hash = $this->set_email($email);

if ($hash === NULL)
{
// $hash has to be set to a value so the gravatar site can return a default image
$hash = 'invalid_email';
}

$query_string = NULL;
$options = array();

if ($rating !== NULL)
{
$options['r'] = $rating;
}

if ($size !== NULL)
{
$options['s'] = $size;
}

if ($default_image !== NULL)
{
$options['d'] = urlencode($default_image);
}

if (count($options) > 0)
{
$query_string = '?'. http_build_query($options);
}

if ($secure !== NULL)
{
$base = $this->gravatar_secure_base_url;
}
else
{
$base = $this->gravatar_base_url;
}

return $base .'avatar/'. $hash . $query_string;
}

/**
* Grab the full profile data for a given email from gravatar.com in xml format
*
* @param string $email
* @param string fetch_method defaults to file, 'curl' is the other option
*
* @return object|null $xml->entry on success, NULL on an error
*
* @deprecated
* @param string $email A registered email.
* @return string/null The hash for accessing the avatar or profile data.
*/
public function get_profile($email, $fetch_method = 'file')
{
$hash = $this->set_email($email);

if ($hash === NULL)
{
// A hash value of NULL will return no xml so the method returns NULL
return NULL;
}

libxml_use_internal_errors(TRUE);

if ($fetch_method === 'file')
{
if (ini_get('allow_url_fopen') == FALSE)
{
return NULL;
}

$str = file_get_contents($this->gravatar_base_url . $hash .'.xml');
}

if ($fetch_method === 'curl')
{
if ( ! function_exists('curl_init'))
{
return NULL;
}

$ch = curl_init();
$options = array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_URL => $this->gravatar_secure_base_url . $hash .'.xml',
CURLOPT_TIMEOUT => 3
);
curl_setopt_array($ch, $options);
$str = curl_exec($ch);
}
public function create_hash_sha256($email) {

$xml = simplexml_load_string($str);

if ($xml === FALSE)
{
$errors = array();
foreach(libxml_get_errors() as $error)
{
$errors[] = $error->message.'\n';
}
$error_string = implode('\n', $errors);
throw new Exception('Failed loading XML\n'. $error_string);
}
else
{
return $xml->entry;
}
return hash('sha256', strtolower(trim((string) $email)));
}

}

0 comments on commit 478fa3d

Please sign in to comment.