PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/profile/icondirect.php

https://github.com/fragilbert/Elgg
PHP | 82 lines | 58 code | 14 blank | 10 comment | 16 complexity | 625c6f37645b7315b0d8925b7115830e MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Elgg profile icon cache/bypass
  4. *
  5. *
  6. * @package ElggProfile
  7. */
  8. // Get DB settings
  9. require_once(dirname(dirname(dirname(__FILE__))). '/engine/settings.php');
  10. global $CONFIG;
  11. // won't be able to serve anything if no joindate or guid
  12. if (!isset($_GET['joindate']) || !isset($_GET['guid'])) {
  13. header("HTTP/1.1 404 Not Found");
  14. exit;
  15. }
  16. $join_date = (int)$_GET['joindate'];
  17. $last_cache = (int)$_GET['lastcache']; // icontime
  18. $guid = (int)$_GET['guid'];
  19. // If is the same ETag, content didn't changed.
  20. $etag = $last_cache . $guid;
  21. if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) == "\"$etag\"") {
  22. header("HTTP/1.1 304 Not Modified");
  23. exit;
  24. }
  25. $size = strtolower($_GET['size']);
  26. if (!in_array($size, array('large', 'medium', 'small', 'tiny', 'master', 'topbar'))) {
  27. $size = "medium";
  28. }
  29. $mysql_dblink = @mysql_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, true);
  30. if ($mysql_dblink) {
  31. if (@mysql_select_db($CONFIG->dbname, $mysql_dblink)) {
  32. $q = "SELECT name, value FROM {$CONFIG->dbprefix}datalists WHERE name in ('dataroot', 'path')";
  33. $result = mysql_query($q, $mysql_dblink);
  34. if ($result) {
  35. $row = mysql_fetch_object($result);
  36. while ($row) {
  37. if ($row->name == 'dataroot') {
  38. $data_root = $row->value;
  39. } elseif ($row->name == 'path') {
  40. $elgg_path = $row->value;
  41. }
  42. $row = mysql_fetch_object($result);
  43. }
  44. }
  45. @mysql_close($mysql_dblink);
  46. if (isset($data_root) && isset($elgg_path)) {
  47. require_once "{$elgg_path}engine/classes/ElggFilestore.php";
  48. require_once "{$elgg_path}engine/classes/ElggDiskFilestore.php";
  49. $user_path = ElggDiskFilestore::getLowerBucketBound($guid) . "/$guid";
  50. $filename = "$data_root$user_path/profile/{$guid}{$size}.jpg";
  51. $size = @filesize($filename);
  52. if ($size) {
  53. header("Content-type: image/jpeg");
  54. header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+6 months")), true);
  55. header("Pragma: public");
  56. header("Cache-Control: public");
  57. header("Content-Length: $size");
  58. header("ETag: \"$etag\"");
  59. readfile($filename);
  60. exit;
  61. }
  62. }
  63. }
  64. }
  65. // something went wrong so load engine and try to forward to default icon
  66. require_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");
  67. elgg_log("Profile icon direct failed.", "WARNING");
  68. forward("_graphics/icons/user/default{$size}.gif");