icondirect.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Elgg profile icon cache/bypass
  4. *
  5. *
  6. * @package ElggProfile
  7. */
  8. // won't be able to serve anything if no joindate or guid
  9. if (!isset($_GET['joindate']) || !isset($_GET['guid'])) {
  10. header("HTTP/1.1 404 Not Found");
  11. exit;
  12. }
  13. $join_date = (int)$_GET['joindate'];
  14. $last_cache = empty($_GET['lastcache']) ? 0 : (int)$_GET['lastcache']; // icontime
  15. $guid = (int)$_GET['guid'];
  16. // If is the same ETag, content didn't changed.
  17. $etag = $last_cache . $guid;
  18. if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) == "\"$etag\"") {
  19. header("HTTP/1.1 304 Not Modified");
  20. exit;
  21. }
  22. $base_dir = dirname(dirname(dirname(__FILE__)));
  23. // Get DB settings
  24. require_once $base_dir . '/engine/settings.php';
  25. require_once $base_dir . '/vendor/autoload.php';
  26. global $CONFIG;
  27. $size = "medium";
  28. if (!empty($_GET['size'])) {
  29. $size = strtolower($_GET['size']);
  30. if (!in_array($size, array('large', 'medium', 'small', 'tiny', 'master', 'topbar'))) {
  31. $size = "medium";
  32. }
  33. }
  34. $path = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR;
  35. $data_root = call_user_func(function () use ($CONFIG) {
  36. if (isset($CONFIG->dataroot)) {
  37. return rtrim($CONFIG->dataroot, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  38. }
  39. // must get from DB
  40. $conf = new \Elgg\Database\Config($CONFIG);
  41. $db = new \Elgg\Database($conf, new \Elgg\Logger(new \Elgg\PluginHooksService()));
  42. try {
  43. $row = $db->getDataRow("
  44. SELECT `value`
  45. FROM {$db->getTablePrefix()}datalists
  46. WHERE `name` = 'dataroot'
  47. ");
  48. if (!$row) {
  49. return "";
  50. }
  51. } catch (\DatabaseException $e) {
  52. // we're going to let the engine figure out what's happening...
  53. return '';
  54. }
  55. return rtrim($row->value, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  56. });
  57. if ($data_root) {
  58. $locator = new \Elgg\EntityDirLocator($guid);
  59. $user_path = $data_root . $locator->getPath();
  60. $filename = $user_path . "profile/{$guid}{$size}.jpg";
  61. $filesize = @filesize($filename);
  62. if ($filesize) {
  63. header("Content-type: image/jpeg");
  64. header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+6 months")), true);
  65. header("Pragma: public");
  66. header("Cache-Control: public");
  67. header("Content-Length: $filesize");
  68. header("ETag: \"$etag\"");
  69. readfile($filename);
  70. exit;
  71. }
  72. }
  73. // something went wrong so load engine and try to forward to default icon
  74. require_once $base_dir . "/engine/start.php";
  75. elgg_log("Profile icon direct failed.", "WARNING");
  76. forward("_graphics/icons/user/default{$size}.gif");