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: * Avatars provider for service manager
18: *
19: * @category class
20: * @package AvatarsProvider
21: * @author Richard Griffith <richard@geekwright.com>
22: * @copyright 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 AvatarsProvider extends AbstractContract implements AvatarInterface
28: {
29: protected $xoops_url;
30: protected $xoops_upload_url;
31:
32: public function __construct()
33: {
34: $this->xoops_url = \XoopsBaseConfig::get('url');
35: $this->xoops_upload_url = \XoopsBaseConfig::get('uploads-url');
36: }
37:
38: /**
39: * getName - get a short name for this service provider. This should be unique within the
40: * scope of the named service, so using module dirname is suggested.
41: *
42: * @return string - a unique name for the service provider
43: */
44: public function getName()
45: {
46: return 'avatars';
47: }
48:
49: /**
50: * getDescription - get human readable description of the service provider
51: *
52: * @return string
53: */
54: public function getDescription()
55: {
56: return 'Traditional XOOPS avatars.';
57: }
58:
59: /**
60: * getUserById - get a user object from a user id
61: *
62: * @param int $uid a user id
63: *
64: * @return object|null
65: */
66: private function getUserById($uid)
67: {
68: $user = \Xoops::getInstance()->getHandlerMember()->getUser((int) $uid);
69: return (is_object($user)) ? $user : null;
70: }
71:
72: /**
73: * getAvatarUrl - given user info return absolute URL to avatar image
74: *
75: * @param Response $response \Xoops\Core\Service\Response object
76: * @param mixed $userinfo XoopsUser object for user or
77: * array of user info, 'uid', 'uname' and 'email' required
78: *
79: * @return void - response->value set to absolute URL to avatar image
80: */
81: public function getAvatarUrl($response, $userinfo)
82: {
83: $noInfo = true;
84: if (is_object($userinfo)) {
85: if ($userinfo instanceof XoopsUser) {
86: if ($userinfo->getVar('user_avatar')
87: && 'blank.gif' !== $userinfo->getVar('user_avatar')
88: ) {
89: $response->setValue($this->xoops_upload_url . "/" . $userinfo->getVar('user_avatar'));
90: }
91: $noInfo = false;
92: }
93: } elseif (is_array($userinfo)) {
94: if (!empty($userinfo['user_avatar']) && $userinfo['user_avatar'] !== 'blank.gif') {
95: $response->setValue($this->xoops_upload_url . "/" . $userinfo['user_avatar']);
96: $noInfo = false;
97: }
98: } elseif (is_scalar($userinfo)) {
99: $user = $this->getUserById((int) $userinfo);
100: if (is_object($user) && ($user instanceof XoopsUser)) {
101: if ($user->getVar('user_avatar')
102: && 'blank.gif' !== $user->getVar('user_avatar')
103: ) {
104: $response->setValue($this->xoops_upload_url . "/" . $user->getVar('user_avatar'));
105: }
106: $noInfo = false;
107: }
108: }
109: if ($noInfo) {
110: $response->setSuccess(false)->addErrorMessage('User info is invalid');
111: }
112: }
113:
114: /**
115: * getAvatarEditUrl - given user info return absolute URL to edit avatar data
116: *
117: * @param Response $response \Xoops\Core\Service\Response object
118: * @param XoopsUser $userinfo XoopsUser object for user
119: *
120: * @return void - response->value set to absolute URL to editing function for avatar data
121: */
122: public function getAvatarEditUrl($response, XoopsUser $userinfo)
123: {
124: $noInfo = true;
125: if ($userinfo instanceof XoopsUser) {
126: $link = $this->xoops_url . '/modules/avatars/editavatar.php';
127: $response->setValue($link);
128: $noInfo = false;
129: }
130: if ($noInfo) {
131: $response->setSuccess(false)->addErrorMessage('User info is invalid');
132: }
133: }
134: }
135: