Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
59.26% covered (warning)
59.26%
16 / 27
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Gravatar
59.26% covered (warning)
59.26%
16 / 27
75.00% covered (warning)
75.00%
3 / 4
36.54
0.00% covered (danger)
0.00%
0 / 1
 getImage
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
8
 getImageUrl
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
56
 getUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHash
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Service class for Gravatar support.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2013-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2013-01-14
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Service;
21
22/**
23 * Class Gravatar
24 *
25 * @package phpMyFAQ\Services
26 */
27class Gravatar
28{
29    private string $httpBaseUrl = 'https://secure.gravatar.com/';
30
31    /**
32     * Returns an image or the URL to the image of a Gravatar based off an email
33     * address.
34     *
35     * @param string $email Email address
36     * @param string[] $params Allows multiple keys with values to give more control
37     */
38    public function getImage(string $email, array $params = []): string
39    {
40        $imageUrl = $this->getUrl() . 'avatar/' . $this->getHash($email);
41        $opts = [];
42
43        if (array_key_exists('default', $params)) {
44            $opts[] = 'default=' . $params['default'];
45        }
46
47        if (array_key_exists('size', $params)) {
48            $opts[] = 'size=' . $params['size'];
49        }
50
51        if (array_key_exists('rating', $params)) {
52            $opts[] = 'rating=' . $params['rating'];
53        }
54
55        if (array_key_exists('force_default', $params) && $params['force_default']) {
56            $opts[] = 'forcedefault=y';
57        }
58
59        if (!array_key_exists('class', $params)) {
60            $params['class'] = '';
61        }
62
63        $gravatar = $imageUrl . ($opts !== [] ? '?' . implode('&', $opts) : '');
64
65        return sprintf('<img src="%s" class="%s" alt="Gravatar">', htmlspecialchars($gravatar), $params['class']);
66    }
67
68    /**
69     * Returns the URL to a Gravatar image based on an email address.
70     *
71     * @param string $email Email address
72     * @param string[] $params Allows multiple keys with values to give more control
73     */
74    public function getImageUrl(string $email, array $params = []): string
75    {
76        $imageUrl = $this->getUrl() . 'avatar/' . $this->getHash($email);
77        $opts = [];
78
79        if (array_key_exists('default', $params)) {
80            $opts[] = 'default=' . $params['default'];
81        }
82
83        if (array_key_exists('size', $params)) {
84            $opts[] = 'size=' . $params['size'];
85        }
86
87        if (array_key_exists('rating', $params)) {
88            $opts[] = 'rating=' . $params['rating'];
89        }
90
91        if (array_key_exists('force_default', $params) && $params['force_default']) {
92            $opts[] = 'forcedefault=y';
93        }
94
95        return $imageUrl . ($opts !== [] ? '?' . implode('&', $opts) : '');
96    }
97
98    /**
99     * Returns the base URL
100     */
101    private function getUrl(): string
102    {
103        return $this->httpBaseUrl;
104    }
105
106    /**
107     * Returns a MD5 hash of an email address.
108     *
109     * @param string $email Email address
110     */
111    private function getHash(string $email): string
112    {
113        return md5($email);
114    }
115}