1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: use Xoops\Core\Kernel\Handlers\XoopsUser;
13: use Xoops\Core\Service\AbstractContract;
14: use Xoops\Core\Service\Contract\AvatarInterface;
15:
16: /**
17: * Gravatars provider for service manager
18: *
19: * @category class
20: * @package GravatarsProvider
21: * @author Richard Griffith <richard@geekwright.com>
22: * @copyright 2013-2014 XOOPS Project (http://xoops.org)
23: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24: * @link http://xoops.org
25: * @since 2.6.0
26: */
27: class GravatarsProvider extends AbstractContract implements AvatarInterface
28: {
29: /**
30: * Get a Gravatar URL for a specified email address.
31: *
32: * @param string $email The email address
33: *
34: * @return String containing either just a URL or a complete image tag
35: *
36: * @source http://gravatar.com/site/implement/images/php/
37: */
38: private static function getGravatar($email)
39: {
40: $s = 80; // Size in pixels, defaults to 80px [ 1 - 2048 ]
41: $d = 'mm'; // Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
42: $r = 'g'; // Maximum rating (inclusive) [ g | pg | r | x ]
43: if ($helper = Xoops\Module\Helper::getHelper('gravatars')) {
44: $v = $helper->getConfig('pixel_size');
45: $s = (empty($v)) ? $s : $v;
46: $v = $helper->getConfig('default_imageset');
47: $d = (empty($v)) ? $d : $v;
48: $d = ($d==='default') ? '' : $d; // preferences does not like empty string
49: $v = $helper->getConfig('max_rating');
50: $r = (empty($v)) ? $r : $v;
51: }
52:
53: $scheme = \Xoops\Core\HttpRequest::getInstance()->getScheme();
54: if ($scheme === 'https') {
55: $url = 'https://secure.gravatar.com/avatar/';
56: } else {
57: $url = 'http://www.gravatar.com/avatar/';
58: }
59: $url .= md5(strtolower(trim($email)));
60: $url .= "?s=$s&d=$d&r=$r";
61:
62: return $url;
63: }
64:
65: /**
66: * getUserById - get a user object from a user id
67: *
68: * @param int $uid a user id
69: *
70: * @return object|null
71: */
72: private function getUserById($uid)
73: {
74: $user = \Xoops::getInstance()->getHandlerMember()->getUser((int) $uid);
75: return (is_object($user)) ? $user : null;
76: }
77:
78: /**
79: * getName - get a short name for this service provider. This should be unique within the
80: * scope of the named service, so using module dirname is suggested.
81: *
82: * @return string - a unique name for the service provider
83: */
84: public function getName()
85: {
86: return 'gravatars';
87: }
88:
89: /**
90: * getDescription - get human readable description of the service provider
91: *
92: * @return string
93: */
94: public function getDescription()
95: {
96: return 'Use gravatar.com for system avatars.';
97: }
98:
99: /**
100: * getAvatarUrl - given user info return absolute URL to avatar image
101: *
102: * @param Response $response \Xoops\Core\Service\Response object
103: * @param mixed $userinfo XoopsUser object for user or
104: * array user info, 'uid', 'uname' and 'email' required
105: * int user uid
106: *
107: * @return void - response->value set to absolute URL to avatar image
108: */
109: public function getAvatarUrl($response, $userinfo)
110: {
111: $noInfo = true;
112: if (is_object($userinfo)) {
113: if ($userinfo instanceof XoopsUser) {
114: $email = $userinfo->getVar('email', 'e');
115: $response->setValue(self::getGravatar($email));
116: $noInfo = false;
117: }
118: } elseif (is_array($userinfo)) {
119: if (!empty($userinfo['email'])) {
120: $response->setValue(self::getGravatar($userinfo['email']));
121: $noInfo = false;
122: }
123: } elseif (is_scalar($userinfo)) {
124: $user = $this->getUserById((int) $userinfo);
125: if (is_object($user) && ($user instanceof XoopsUser)) {
126: $email = $user->getVar('email', 'e');
127: $response->setValue(self::getGravatar($email));
128: $noInfo = false;
129: }
130: }
131: if ($noInfo) {
132: $response->setSuccess(false)->addErrorMessage('User info is invalid');
133: }
134: }
135:
136: /**
137: * getAvatarEditUrl - given user info return absolute URL to edit avatar data
138: *
139: * @param Response $response \Xoops\Core\Service\Response object
140: * @param XoopsUser $userinfo XoopsUser object for user
141: *
142: * @return void - response->value set to absolute URL to editing function for avatar data
143: */
144: public function getAvatarEditUrl($response, XoopsUser $userinfo)
145: {
146: $noInfo = true;
147:
148: if (is_object($userinfo)) {
149: if ($userinfo instanceof XoopsUser) {
150: $email = $userinfo->getVar('email', 'e');
151: $link = 'http://www.gravatar.com/' . md5(strtolower(trim($email)));
152: $response->setValue($link);
153: $noInfo = false;
154: }
155: }
156: if ($noInfo) {
157: $response->setSuccess(false)->addErrorMessage('User info is invalid');
158: }
159: }
160: }
161: