Explorar o código

* http headers

Visman %!s(int64=6) %!d(string=hai) anos
pai
achega
badc4de958
Modificáronse 4 ficheiros con 43 adicións e 22 borrados
  1. 1 1
      app/Core/View.php
  2. 38 17
      app/Models/Page.php
  3. 3 1
      app/Models/Pages/Maintenance.php
  4. 1 3
      app/Models/Pages/Redirect.php

+ 1 - 1
app/Core/View.php

@@ -88,7 +88,7 @@ EOD;
     public function rendering(Page $p)
     {
         foreach ($p->httpHeaders as $header) {
-            \header($header);
+            \header($header[0], $header[1]);
         }
 
         if (null === $p->nameTpl) {

+ 38 - 17
app/Models/Page.php

@@ -202,6 +202,30 @@ abstract class Page extends Model
         return $this;
     }
 
+    /**
+     * Добавляет HTTP заголовок
+     *
+     * @param string $key
+     * @param string $value
+     * @param bool   $replace
+     *
+     * @return Page
+     */
+    public function header($key, $value, $replace = true)
+    {
+        if ('HTTP/' === \substr($key, 0, 5)) {
+            if (\preg_match('%^HTTP/\d\.\d%', $_SERVER['SERVER_PROTOCOL'], $match)) {
+                $key = $match[0];
+            } else {
+                $key = 'HTTP/1.1';
+            }
+        } else {
+            $key .= ':';
+        }
+        $this->a['httpHeaders'][] = ["{$key} {$value}", $replace];
+        return $this;
+    }
+
     /**
      * Возвращает HTTP заголовки страницы
      * $this->httpHeaders
@@ -210,18 +234,22 @@ abstract class Page extends Model
      */
     protected function gethttpHeaders()
     {
-        $headers = $this->a['httpHeaders'];
-        if (! empty($status = $this->httpStatus())) {
-            $headers[] = $status;
-        }
-#       $headers[] = 'X-Frame-Options: DENY';
-        return $headers;
+        $now = gmdate('D, d M Y H:i:s') . ' GMT';
+
+        $this->httpStatus()
+            ->header('Cache-Control', 'no-cache, no-store, must-revalidate')
+            ->header('Content-type', 'text/html; charset=utf-8')
+            ->header('Date', $now)
+            ->header('Last-Modified', $now)
+            ->header('Expires', $now);
+
+        return $this->a['httpHeaders'];
     }
 
     /**
-     * Возвращает HTTP статус страницы или null
+     * Устанавливает HTTP статус страницы
      *
-     * @return null|string
+     * @return Page
      */
     protected function httpStatus()
     {
@@ -234,16 +262,9 @@ abstract class Page extends Model
         ];
 
         if (isset($list[$this->httpStatus])) {
-            $status = 'HTTP/1.0 ';
-
-            if (isset($_SERVER['SERVER_PROTOCOL'])
-                && \preg_match('%^HTTP/([12]\.[01])%', $_SERVER['SERVER_PROTOCOL'], $match)
-            ) {
-                $status = 'HTTP/' . $match[1] . ' ';
-            }
-
-            return $status . $list[$this->httpStatus];
+            $this->header('HTTP/1.0', $list[$this->httpStatus]);
         }
+        return $this;
     }
 
     /**

+ 3 - 1
app/Models/Pages/Maintenance.php

@@ -9,7 +9,7 @@ class Maintenance extends Page
 {
     /**
      * Конструктор
-     * 
+     *
      * @param Container $container
      */
     public function __construct(Container $container)
@@ -25,6 +25,8 @@ class Maintenance extends Page
         $this->titles             = \ForkBB\__('Maintenance');
 #       $this->fNavigation        = null; //????
         $this->maintenanceMessage = $this->c->config->o_maintenance_message;
+
+        $this->header('Retry-After', '3600');
     }
 
     /**

+ 1 - 3
app/Models/Pages/Redirect.php

@@ -75,9 +75,7 @@ class Redirect extends Page
     protected function getHttpHeaders()
     {
         if (null === $this->nameTpl) {
-            $this->httpHeaders = [
-                'Location: ' . $this->link, //????
-            ];
+            $this->header('Location', $this->link);
         }
         return parent::getHttpHeaders();
     }