avatars.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php namespace HashOver;
  2. // Copyright (C) 2015-2017 Jacob Barkdull
  3. // This file is part of HashOver.
  4. //
  5. // HashOver is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // HashOver 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 Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
  17. // Display source code
  18. if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
  19. if (isset ($_GET['source'])) {
  20. header ('Content-type: text/plain; charset=UTF-8');
  21. exit (file_get_contents (basename (__FILE__)));
  22. } else {
  23. exit ('<b>HashOver</b>: This is a class file.');
  24. }
  25. }
  26. class Avatars
  27. {
  28. public $setup;
  29. public $isHTTPS = false;
  30. public $http;
  31. public $subdomain;
  32. public $iconSize;
  33. public $avatar;
  34. public $fallback;
  35. public function __construct (Setup $setup)
  36. {
  37. $this->setup = $setup;
  38. $this->isHTTPS = $setup->isHTTPS ();
  39. // Get icon size from settings
  40. $this->iconSize = ($setup->isMobile === true) ? 256 : $setup->iconSize;
  41. // Default avatar
  42. $avatar = $setup->httpImages . '/avatar';
  43. $extension = ($setup->isMobile === true) ? 'svg' : 'png';
  44. $this->avatar = $avatar . '.' . $extension;
  45. // Use HTTPS if this file is requested with HTTPS
  46. $this->http = ($this->isHTTPS ? 'https' : 'http') . '://';
  47. $this->subdomain = $this->isHTTPS ? 'secure' : 'www';
  48. // If set to custom, direct 404s to local avatar image
  49. if ($setup->gravatarDefault === 'custom') {
  50. $fallback = $avatar . '.png';
  51. // Check if HashOver is being remotely accessed
  52. if ($setup->remoteAccess === false) {
  53. // If so, make avatar path absolute
  54. $fallback = $setup->absolutePath . $fallback;
  55. }
  56. // URL encode fallback URL
  57. $this->fallback = urlencode ($fallback);
  58. } else {
  59. // If not direct to a themed default
  60. $this->fallback = $setup->gravatarDefault;
  61. }
  62. // Gravatar URL
  63. $this->gravatar = $this->http . $this->subdomain;
  64. $this->gravatar .= '.gravatar.com/avatar/';
  65. }
  66. // Attempt to get Gravatar avatar image
  67. public function getGravatar ($hash)
  68. {
  69. // If no hash is given, return the default avatar
  70. if (empty ($hash)) {
  71. return $this->avatar;
  72. }
  73. // Gravatar URL
  74. $gravatar = $this->gravatar . $hash . '.png?r=pg';
  75. $gravatar .= '&s=' . $this->iconSize;
  76. $gravatar .= '&d=' . $this->fallback;
  77. // Force Gravatar default avatar if enabled
  78. if ($this->setup->gravatarForce === true) {
  79. $gravatar .= '&f=y';
  80. }
  81. // Redirect Gravatar image
  82. return $gravatar;
  83. }
  84. }