dolibarr 21.0.0-beta
lib_gravatar.js.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
26//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1'); // Not disabled cause need to load personalized language
27//if (! defined('NOREQUIREDB')) define('NOREQUIREDB', '1');
28if (!defined('NOREQUIRESOC')) {
29 define('NOREQUIRESOC', '1');
30}
31//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1'); // Not disabled cause need to do translations
32if (!defined('NOCSRFCHECK')) {
33 define('NOCSRFCHECK', 1);
34}
35if (!defined('NOTOKENRENEWAL')) {
36 define('NOTOKENRENEWAL', 1);
37}
38if (!defined('NOLOGIN')) {
39 define('NOLOGIN', 1);
40}
41if (!defined('NOREQUIREMENU')) {
42 define('NOREQUIREMENU', 1);
43}
44if (!defined('NOREQUIREHTML')) {
45 define('NOREQUIREHTML', 1);
46}
47if (!defined('NOREQUIREAJAX')) {
48 define('NOREQUIREAJAX', '1');
49}
50
51session_cache_limiter('public');
52
53require_once '../../main.inc.php';
57// Define javascript type
58top_httphead('text/javascript; charset=UTF-8');
59// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
60if (empty($dolibarr_nocache)) {
61 header('Cache-Control: max-age=10800, public, must-revalidate');
62} else {
63 header('Cache-Control: no-cache');
64}
65
66?>
67
68function get_avatar_from_service(service, userid, size) {
69 // this return the url that redirects to the according user image/avatar/profile picture
70 // implemented services: google profiles, facebook, gravatar, twitter, tumblr, default fallback
71 // for google use get_avatar_from_service('google', profile-name or user-id , size-in-px )
72 // for facebook use get_avatar_from_service('facebook', vanity url or user-id , size-in-px or size-as-word )
73 // for gravatar use get_avatar_from_service('gravatar', md5 hash email@address, size-in-px )
74 // for twitter use get_avatar_from_service('twitter', username, size-in-px or size-as-word )
75 // for tumblr use get_avatar_from_service('tumblr', blog-url, size-in-px )
76 // everything else will go to the fallback
77 // google and gravatar scale the avatar to any site, others will guided to the next best version
78 var url = '';
79
80 switch (service) {
81
82 case "google":
83 // see http://googlesystem.blogspot.com/2011/03/unedited-google-profile-pictures.html (couldn't find a better link)
84 // available sizes: all, google rescales for you
85 url = "http://profiles.google.com/s2/photos/profile/" + userid + "?sz=" + size;
86 break;
87
88 case "facebook":
89 // see https://developers.facebook.com/docs/reference/api/
90 // available sizes: square (50x50), small (50xH) , normal (100xH), large (200xH)
91 var sizeparam = '';
92 if (isNumberOrNot(size)) {
93 if (size >= 200) {
94 sizeparam = 'large'
95 }
96 if (size >= 100 && size < 200) {
97 sizeparam = 'normal'
98 }
99 if (size >= 50 && size < 100) {
100 sizeparam = 'small'
101 }
102 if (size < 50) {
103 sizeparam = 'square'
104 }
105 } else {
106 sizeparam = size;
107 }
108 url = "https://graph.facebook.com/" + userid + "/picture?type=" + sizeparam;
109 break;
110
111 case "gravatar":
112 // see http://en.gravatar.com/site/implement/images/
113 // available sizes: all, gravatar rescales for you
114 url = "http://www.gravatar.com/avatar/" + userid + "?s=" + size
115 break;
116
117 case "twitter":
118 // see https://dev.twitter.com/docs/api/1/get/users/profile_image/%3Ascreen_name
119 // available sizes: bigger (73x73), normal (48x48), mini (24x24), no param will give you full size
120 var sizeparam = '';
121 if (isNumberOrNot(size)) {
122 if (size >= 73) {
123 sizeparam = 'bigger'
124 }
125 if (size >= 48 && size < 73) {
126 sizeparam = 'normal'
127 }
128 if (size < 48) {
129 sizeparam = 'mini'
130 }
131 } else {
132 sizeparam = size;
133 }
134
135 url = "http://api.twitter.com/1/users/profile_image?screen_name=" + userid + "&size=" + sizeparam;
136 break;
137
138 case "tumblr":
139 // see http://www.tumblr.com/docs/en/api/v2#blog-avatar
140 //TODO do something smarter with the ranges
141 // available sizes: 16, 24, 30, 40, 48, 64, 96, 128, 512
142 var sizeparam = '';
143 if (size >= 512) {
144 sizeparam = 512
145 }
146 if (size >= 128 && size < 512) {
147 sizeparam = 128
148 }
149 if (size >= 96 && size < 128) {
150 sizeparam = 96
151 }
152 if (size >= 64 && size < 96) {
153 sizeparam = 64
154 }
155 if (size >= 48 && size < 64) {
156 sizeparam = 48
157 }
158 if (size >= 40 && size < 48) {
159 sizeparam = 40
160 }
161 if (size >= 30 && size < 40) {
162 sizeparam = 30
163 }
164 if (size >= 24 && size < 30) {
165 sizeparam = 24
166 }
167 if (size < 24) {
168 sizeparam = 16
169 }
170
171 url = "http://api.tumblr.com/v2/blog/" + userid + "/avatar/" + sizeparam;
172 break;
173
174 default:
175 // http://www.iconfinder.com/icondetails/23741/128/avatar_devil_evil_green_monster_vampire_icon
176 // find your own
177 url = "http://i.imgur.com/RLiDK.png"; // 48x48
178 }
179
180
181 return url;
182}
183
184
185// helper methods
186
187function isNumberOrNot(n) {
188 // see http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric
189 return !isNaN(parseFloat(n)) && isFinite(n);
190}
191
192// End of lib_gravatar.js.php
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.