|
| 1 | +<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | + |
| 3 | +require 'vendor/autoload.php'; |
| 4 | + |
| 5 | +use forxer\Gravatar\Gravatar as FGravatar; |
| 6 | + |
| 7 | +/* |
| 8 | +Copyright (C) 2004 - 2016 EllisLab, Inc. |
| 9 | +
|
| 10 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | +of this software and associated documentation files (the "Software"), to deal |
| 12 | +in the Software without restriction, including without limitation the rights |
| 13 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | +copies of the Software, and to permit persons to whom the Software is |
| 15 | +furnished to do so, subject to the following conditions: |
| 16 | +
|
| 17 | +The above copyright notice and this permission notice shall be included in |
| 18 | +all copies or substantial portions of the Software. |
| 19 | +
|
| 20 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 23 | +ELLISLAB, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 24 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 25 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | +
|
| 27 | +Except as contained in this notice, the name of EllisLab, Inc. shall not be |
| 28 | +used in advertising or otherwise to promote the sale, use or other dealings |
| 29 | +in this Software without prior written authorization from EllisLab, Inc. |
| 30 | +*/ |
| 31 | + |
| 32 | +/** |
| 33 | + * Gravatar Class |
| 34 | + * |
| 35 | + * @package ExpressionEngine |
| 36 | + * @category Plugin |
| 37 | + * @author EllisLab |
| 38 | + * @copyright Copyright (c) 2016, EllisLab, Inc. |
| 39 | + * @link https://github.com/EllisLab/Gravatar |
| 40 | + */ |
| 41 | +class Gravatar { |
| 42 | + |
| 43 | + /* |
| 44 | + * @var string The plugin return data, since we use the constructor for our lifting |
| 45 | + */ |
| 46 | + public $return_data; |
| 47 | + |
| 48 | + /* |
| 49 | + * @var string Email address identifier |
| 50 | + */ |
| 51 | + private $email; |
| 52 | + |
| 53 | + /* |
| 54 | + * @var int Default image size in pixels |
| 55 | + */ |
| 56 | + private $size = 80; |
| 57 | + |
| 58 | + /* |
| 59 | + * @var string Default image URL |
| 60 | + */ |
| 61 | + private $default = 'mm'; |
| 62 | + |
| 63 | + /* |
| 64 | + * @var string Maximum allowed rating |
| 65 | + */ |
| 66 | + private $rating = 'pg'; |
| 67 | + |
| 68 | + /* |
| 69 | + * @var string File type extension |
| 70 | + */ |
| 71 | + private $extension = 'png'; |
| 72 | + |
| 73 | + /* |
| 74 | + * @var bool https |
| 75 | + */ |
| 76 | + private $https = TRUE; |
| 77 | + |
| 78 | + /* |
| 79 | + * @var bool Force Default Image |
| 80 | + */ |
| 81 | + private $force_default = FALSE; |
| 82 | + |
| 83 | + /* |
| 84 | + * @var string Base URL for Adorable Avatars, http://avatars.adorable.io/ |
| 85 | + */ |
| 86 | + private $adorable_base_url = 'https://api.adorable.io/avatars/'; |
| 87 | + |
| 88 | + /** |
| 89 | + * Constructor |
| 90 | + * |
| 91 | + * @access public |
| 92 | + * @return void |
| 93 | + */ |
| 94 | + public function __construct($str = '') |
| 95 | + { |
| 96 | + if ( ! $this->setOptionsFromParameters()) |
| 97 | + { |
| 98 | + ee()->TMPL->log_item('<b>Gravatar:</b> Aborted!'); |
| 99 | + return FALSE; |
| 100 | + } |
| 101 | + |
| 102 | + $gravatar = FGravatar::image( |
| 103 | + $this->email, |
| 104 | + $this->size, |
| 105 | + $this->default, |
| 106 | + $this->rating, |
| 107 | + $this->extension, |
| 108 | + $this->https, |
| 109 | + $this->force_default |
| 110 | + ); |
| 111 | + |
| 112 | + // bug in current forxer\Gravatar |
| 113 | + // Full URLs given as the default image are double encoded |
| 114 | + if (strncasecmp($this->default, 'http', 4) === 0) |
| 115 | + { |
| 116 | + $url = parse_url($gravatar); |
| 117 | + parse_str(html_entity_decode($url['query']), $default_url); |
| 118 | + $default_url['d'] = rawurldecode($default_url['d']); |
| 119 | + $gravatar = $url['scheme'].'://'.$url['host'].$url['path'].'?'.http_build_query($default_url); |
| 120 | + } |
| 121 | + |
| 122 | + $this->return_data = $gravatar; |
| 123 | + } |
| 124 | + |
| 125 | + // ------------------------------------------------------------------------ |
| 126 | + |
| 127 | + /** |
| 128 | + * Set Options from Parameters |
| 129 | + * |
| 130 | + * @return bool FALSE when required parameters are not present |
| 131 | + */ |
| 132 | + private function setOptionsFromParameters() |
| 133 | + { |
| 134 | + // order here is imporant! |
| 135 | + |
| 136 | + // Email |
| 137 | + if ( ! $this->email = ee()->TMPL->fetch_param('email')) |
| 138 | + { |
| 139 | + ee()->TMPL->log_item('<b>Gravatar:</b> Email required, nothing to do'); |
| 140 | + return FALSE; |
| 141 | + } |
| 142 | + |
| 143 | + // Size |
| 144 | + $this->size = (ee()->TMPL->fetch_param('size')) ?: $this->size; |
| 145 | + |
| 146 | + // Default Image |
| 147 | + $default = ee()->TMPL->fetch_param('default'); |
| 148 | + switch ($default) |
| 149 | + { |
| 150 | + case FALSE: |
| 151 | + $this->default = $this->default; |
| 152 | + break; |
| 153 | + case 'adorable': |
| 154 | + $this->default = $this->adorable_base_url.$this->size.'/'.$this->email; |
| 155 | + break; |
| 156 | + case 'mm': |
| 157 | + case 'identicon': |
| 158 | + case 'monsterid': |
| 159 | + case 'wavatar': |
| 160 | + case 'retro': |
| 161 | + case 'blank': |
| 162 | + default: |
| 163 | + $this->default = $default; |
| 164 | + } |
| 165 | + |
| 166 | + // Max Rating |
| 167 | + if (in_array(strtolower(ee()->TMPL->fetch_param('rating')), ['g', 'pg', 'r', 'x'])) |
| 168 | + { |
| 169 | + $this->rating = ee()->TMPL->fetch_param('rating'); |
| 170 | + } |
| 171 | + |
| 172 | + // Image Extension |
| 173 | + if (in_array(strtolower(ee()->TMPL->fetch_param('extension')), ['jpg', 'jpeg', 'gif', 'png'])) |
| 174 | + { |
| 175 | + $this->rating = ee()->TMPL->fetch_param('extension'); |
| 176 | + } |
| 177 | + |
| 178 | + // HTTPS |
| 179 | + if (ee()->TMPL->fetch_param('https')) |
| 180 | + { |
| 181 | + $this->https = get_bool_from_string(ee()->TMPL->fetch_param('https')); |
| 182 | + } |
| 183 | + |
| 184 | + // Force Default Image |
| 185 | + if (ee()->TMPL->fetch_param('force_default')) |
| 186 | + { |
| 187 | + $this->force_default = get_bool_from_string(ee()->TMPL->fetch_param('force_default')); |
| 188 | + } |
| 189 | + |
| 190 | + return TRUE; |
| 191 | + } |
| 192 | + |
| 193 | + // ------------------------------------------------------------------------ |
| 194 | +} |
| 195 | +// END CLASS |
| 196 | + |
| 197 | +// EOF |
0 commit comments