/libs/Nette/Web/HttpContext.php

https://github.com/GunioRobot/IconStore · PHP · 103 lines · 50 code · 27 blank · 26 comment · 13 complexity · 4e0eb9b1748eb1f9273353526e5c10ff MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Nette Framework.
  4. *
  5. * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license", and/or
  8. * GPL license. For more information please see http://nette.org
  9. */
  10. namespace Nette\Web;
  11. use Nette;
  12. /**
  13. * HTTP-specific tasks.
  14. *
  15. * @author David Grudl
  16. */
  17. class HttpContext extends Nette\Object
  18. {
  19. /**
  20. * Attempts to cache the sent entity by its last modification date
  21. * @param string|int|DateTime last modified time
  22. * @param string strong entity tag validator
  23. * @return bool
  24. */
  25. public function isModified($lastModified = NULL, $etag = NULL)
  26. {
  27. $response = $this->getResponse();
  28. $request = $this->getRequest();
  29. if ($lastModified) {
  30. $response->setHeader('Last-Modified', $response->date($lastModified));
  31. }
  32. if ($etag) {
  33. $response->setHeader('ETag', '"' . addslashes($etag) . '"');
  34. }
  35. $ifNoneMatch = $request->getHeader('If-None-Match');
  36. if ($ifNoneMatch === '*') {
  37. $match = TRUE; // match, check if-modified-since
  38. } elseif ($ifNoneMatch !== NULL) {
  39. $etag = $response->getHeader('ETag');
  40. if ($etag == NULL || strpos(' ' . strtr($ifNoneMatch, ",\t", ' '), ' ' . $etag) === FALSE) {
  41. return TRUE;
  42. } else {
  43. $match = TRUE; // match, check if-modified-since
  44. }
  45. }
  46. $ifModifiedSince = $request->getHeader('If-Modified-Since');
  47. if ($ifModifiedSince !== NULL) {
  48. $lastModified = $response->getHeader('Last-Modified');
  49. if ($lastModified != NULL && strtotime($lastModified) <= strtotime($ifModifiedSince)) {
  50. $match = TRUE;
  51. } else {
  52. return TRUE;
  53. }
  54. }
  55. if (empty($match)) {
  56. return TRUE;
  57. }
  58. $response->setCode(IHttpResponse::S304_NOT_MODIFIED);
  59. return FALSE;
  60. }
  61. /********************* backend ****************d*g**/
  62. /**
  63. * @return Nette\Web\IHttpRequest
  64. */
  65. public function getRequest()
  66. {
  67. return Nette\Environment::getHttpRequest();
  68. }
  69. /**
  70. * @return Nette\Web\IHttpResponse
  71. */
  72. public function getResponse()
  73. {
  74. return Nette\Environment::getHttpResponse();
  75. }
  76. }