1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10: 
 11: 
 12: namespace Nette\Web;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18:  19:  20:  21:  22: 
 23: class HttpContext extends Nette\Object
 24: {
 25: 
 26: 
 27:      28:  29:  30:  31:  32: 
 33:     public function isModified($lastModified = NULL, $etag = NULL)
 34:     {
 35:         $response = $this->getResponse();
 36:         $request = $this->getRequest();
 37: 
 38:         if ($lastModified) {
 39:             $response->setHeader('Last-Modified', $response->date($lastModified));
 40:         }
 41:         if ($etag) {
 42:             $response->setHeader('ETag', '"' . addslashes($etag) . '"');
 43:         }
 44: 
 45:         $ifNoneMatch = $request->getHeader('If-None-Match');
 46:         if ($ifNoneMatch === '*') {
 47:             $match = TRUE; 
 48: 
 49:         } elseif ($ifNoneMatch !== NULL) {
 50:             $etag = $response->getHeader('ETag');
 51: 
 52:             if ($etag == NULL || strpos(' ' . strtr($ifNoneMatch, ",\t", '  '), ' ' . $etag) === FALSE) {
 53:                 return TRUE;
 54: 
 55:             } else {
 56:                 $match = TRUE; 
 57:             }
 58:         }
 59: 
 60:         $ifModifiedSince = $request->getHeader('If-Modified-Since');
 61:         if ($ifModifiedSince !== NULL) {
 62:             $lastModified = $response->getHeader('Last-Modified');
 63:             if ($lastModified != NULL && strtotime($lastModified) <= strtotime($ifModifiedSince)) {
 64:                 $match = TRUE;
 65: 
 66:             } else {
 67:                 return TRUE;
 68:             }
 69:         }
 70: 
 71:         if (empty($match)) {
 72:             return TRUE;
 73:         }
 74: 
 75:         $response->setCode(IHttpResponse::S304_NOT_MODIFIED);
 76:         return FALSE;
 77:     }
 78: 
 79: 
 80: 
 81:     
 82: 
 83: 
 84: 
 85:      86:  87: 
 88:     public function getRequest()
 89:     {
 90:         return Nette\Environment::getHttpRequest();
 91:     }
 92: 
 93: 
 94: 
 95:      96:  97: 
 98:     public function getResponse()
 99:     {
100:         return Nette\Environment::getHttpResponse();
101:     }
102: 
103: }
104: