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
/*-----8<--------------------------------------------------------------------
*
* BEdita - a semantic content management framework
*
* Copyright 2015 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BEdita is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with BEdita (see LICENSE.LGPL).
* If not, see <http://gnu.org/licenses/lgpl-3.0.html>.
*
*------------------------------------------------------------------->8-----
*/
/**
* Gravatar helper
*
* Class to easily handle Gravatar image
*/
class GravatarHelper extends AppHelper {
/**
* Helpers used
* @var array
*/
public $helpers = array('Html');
/**
* Gravatar image endpoint
* @var string
*/
private $imageEndpoint = 'https://secure.gravatar.com/avatar/';
/**
* Image options used in url query string
*
* - 'd' is the default image used if missing gravatar user image
* - 's' the size of the image
*
* @see https://it.gravatar.com/site/implement/images
* @var array
*/
private $imageOptions = array(
'd' => 'retro',
's' => null
);
/**
* Constructor to override self::$imageOptions
*
* @param array $options
*/
public function __construct($options = array()) {
parent::__construct($options);
$this->setImageOptions($options);
}
/**
* Get self::$imageOptions
*
* @param array $options
* @return array
*/
public function getImageOptions() {
return $this->imageOptions;
}
/**
* Set self::$imageOptions
*
* @param array $options
* @return void
*/
public function setImageOptions(array $options = array()) {
$this->imageOptions = array_merge(
$this->imageOptions,
array_intersect_key($options, $this->imageOptions)
);
return $this->imageOptions;
}
/**
* Starting from user return the url of gravatar image
*
* @param string|array $user an email or an array containing 'email' key
* @param array $options override self::$imageOptions
* @return string
*/
public function imageUrl($user, array $options = array()) {
$email = $this->userEmail($user);
$hash = md5(strtolower(trim($email)));
$options = array_intersect_key($options, $this->imageOptions);
$options += $this->imageOptions;
$url = $this->imageEndpoint . $hash . Router::queryString($options);
return $url;
}
/**
* Return a well formatted <img> tag using gravatar image.
* It can be configured with $options that can contain key to override self::imageOptions
* - override of self::$imageOptions
* - 'html' an array of HTML attribute to use in <img>
*
* @see self::$imageOptions
* @param string|array $user an email or an array containing 'email' key
* @param array $options image options
* @return string
*/
public function image($user, array $options = array()) {
$options += array('html' => array());
$url = $this->imageUrl($user, $options);
return $this->Html->image($url, $options['html']);
}
/**
* Given a $user return an email
* $user is expected to be a string (email) or an array with 'email' key
* @param string|u $user
* @return [type] [description]
*/
private function userEmail($user) {
$email = '';
if (is_string($user)) {
$email = $user;
} elseif (is_array($user) && !empty($user['email'])) {
$email = $user['email'];
}
return $email;
}
}