lib_gravatar.js.php 5.7 KB

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