Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 72
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
gravatar
0.00% covered (danger)
0.00%
0 / 72
0.00% covered (danger)
0.00%
0 / 6
2070
0.00% covered (danger)
0.00%
0 / 1
 get_data
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 get_custom_html
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 prepare_form
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
110
 process_form
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
702
 get_template_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_gravatar_url
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\avatar\driver;
15
16/**
17* Handles avatars hosted at gravatar.com
18*/
19class gravatar extends \phpbb\avatar\driver\driver
20{
21    /**
22    * The URL for the gravatar service
23    */
24    const GRAVATAR_URL = '//secure.gravatar.com/avatar/';
25
26    /**
27    * {@inheritdoc}
28    */
29    public function get_data($row)
30    {
31        return array(
32            'src' => $row['avatar'],
33            'width' => $row['avatar_width'],
34            'height' => $row['avatar_height'],
35        );
36    }
37
38    /**
39    * {@inheritdoc}
40    */
41    public function get_custom_html($user, $row, $alt = '')
42    {
43        return '<img class="gravatar" src="' . $this->get_gravatar_url($row) . '" ' .
44            ($row['avatar_width'] ? ('width="' . $row['avatar_width'] . '" ') : '') .
45            ($row['avatar_height'] ? ('height="' . $row['avatar_height'] . '" ') : '') .
46            'alt="' . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . '" />';
47    }
48
49    /**
50    * {@inheritdoc}
51    */
52    public function prepare_form($request, $template, $user, $row, &$error)
53    {
54        $template->assign_vars(array(
55            'AVATAR_GRAVATAR_WIDTH' => (($row['avatar_type'] == $this->get_name() || $row['avatar_type'] == 'gravatar') && $row['avatar_width']) ? $row['avatar_width'] : $request->variable('avatar_gravatar_width', ''),
56            'AVATAR_GRAVATAR_HEIGHT' => (($row['avatar_type'] == $this->get_name() || $row['avatar_type'] == 'gravatar') && $row['avatar_height']) ? $row['avatar_height'] : $request->variable('avatar_gravatar_width', ''),
57            'AVATAR_GRAVATAR_EMAIL' => (($row['avatar_type'] == $this->get_name() || $row['avatar_type'] == 'gravatar') && $row['avatar']) ? $row['avatar'] : '',
58        ));
59
60        return true;
61    }
62
63    /**
64    * {@inheritdoc}
65    */
66    public function process_form($request, $template, $user, $row, &$error)
67    {
68        $row['avatar'] = $request->variable('avatar_gravatar_email', '');
69        $row['avatar_width'] = $request->variable('avatar_gravatar_width', 0);
70        $row['avatar_height'] = $request->variable('avatar_gravatar_height', 0);
71
72        if (empty($row['avatar']))
73        {
74            return false;
75        }
76
77        if (!function_exists('validate_data'))
78        {
79            require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
80        }
81
82        $validate_array = validate_data(
83            array(
84                'email' => $row['avatar'],
85            ),
86            array(
87                'email' => array(
88                    array('string', false, 6, 60),
89                    array('email'),
90                ),
91            )
92        );
93
94        $error = array_merge($error, $validate_array);
95
96        if (!empty($error))
97        {
98            return false;
99        }
100
101        // Get image dimensions if they are not set
102        if ($row['avatar_width'] <= 0 || $row['avatar_height'] <= 0)
103        {
104            /**
105            * default to the minimum of the maximum allowed avatar size if the size
106            * is not or only partially entered
107            */
108            $row['avatar_width'] = $row['avatar_height'] = min($this->config['avatar_max_width'], $this->config['avatar_max_height']);
109            $url = $this->get_gravatar_url($row);
110
111            if (($row['avatar_width'] <= 0 || $row['avatar_height'] <= 0) && (($image_data = $this->imagesize->getImageSize($url)) === false))
112            {
113                $error[] = 'UNABLE_GET_IMAGE_SIZE';
114                return false;
115            }
116
117            if (!empty($image_data) && ($image_data['width'] <= 0 || $image_data['height'] <= 0))
118            {
119                $error[] = 'AVATAR_NO_SIZE';
120                return false;
121            }
122
123            $row['avatar_width'] = ($row['avatar_width'] && $row['avatar_height']) ? $row['avatar_width'] : $image_data['width'];
124            $row['avatar_height'] = ($row['avatar_width'] && $row['avatar_height']) ? $row['avatar_height'] : $image_data['height'];
125        }
126
127        if ($row['avatar_width'] <= 0 || $row['avatar_height'] <= 0)
128        {
129            $error[] = 'AVATAR_NO_SIZE';
130            return false;
131        }
132
133        if ($this->config['avatar_max_width'] || $this->config['avatar_max_height'])
134        {
135            if ($row['avatar_width'] > $this->config['avatar_max_width'] || $row['avatar_height'] > $this->config['avatar_max_height'])
136            {
137                $error[] = array('AVATAR_WRONG_SIZE', $this->config['avatar_min_width'], $this->config['avatar_min_height'], $this->config['avatar_max_width'], $this->config['avatar_max_height'], $row['avatar_width'], $row['avatar_height']);
138                return false;
139            }
140        }
141
142        if ($this->config['avatar_min_width'] || $this->config['avatar_min_height'])
143        {
144            if ($row['avatar_width'] < $this->config['avatar_min_width'] || $row['avatar_height'] < $this->config['avatar_min_height'])
145            {
146                $error[] = array('AVATAR_WRONG_SIZE', $this->config['avatar_min_width'], $this->config['avatar_min_height'], $this->config['avatar_max_width'], $this->config['avatar_max_height'], $row['avatar_width'], $row['avatar_height']);
147                return false;
148            }
149        }
150
151        return array(
152            'avatar' => $row['avatar'],
153            'avatar_width' => $row['avatar_width'],
154            'avatar_height' => $row['avatar_height'],
155        );
156    }
157
158    /**
159    * {@inheritdoc}
160    */
161    public function get_template_name()
162    {
163        return 'ucp_avatar_options_gravatar.html';
164    }
165
166    /**
167    * Build gravatar URL for output on page
168    *
169    * @param array $row User data or group data that has been cleaned with
170    *        \phpbb\avatar\manager::clean_row
171    * @return string Gravatar URL
172    */
173    protected function get_gravatar_url($row)
174    {
175        global $phpbb_dispatcher;
176
177        $url = self::GRAVATAR_URL;
178        $url .=  md5(strtolower(trim($row['avatar'])));
179
180        if ($row['avatar_width'] || $row['avatar_height'])
181        {
182            $url .= '?s=' . max($row['avatar_width'], $row['avatar_height']);
183        }
184
185        /**
186        * Modify gravatar url
187        *
188        * @event core.get_gravatar_url_after
189        * @var    string    row    User data or group data
190        * @var    string    url    Gravatar URL
191        * @since 3.1.7-RC1
192        */
193        $vars = array('row', 'url');
194        extract($phpbb_dispatcher->trigger_event('core.get_gravatar_url_after', compact($vars)));
195
196        return $url;
197    }
198}