HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/phpmyfaq/src/phpMyFAQ/Helper/HttpHelper.php
<?php

/**
 * HTTP Helper class for phpMyFAQ.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/.
 *
 * @package   phpMyFAQ\Helper
 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
 * @author    Florian Anderiasch <florian@phpmyfaq.de>
 * @copyright 2009-2022 phpMyFAQ Team
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      https://www.phpmyfaq.de
 * @since     2009-09-13
 */

namespace phpMyFAQ\Helper;

use phpMyFAQ\Helper;

/**
 * Class HttpHelper
 *
 * @package phpMyFAQ\Helper
 */
class HttpHelper extends Helper
{
    /** @var string Content type */
    private string $contentType = '';

    /** @var int HTTP status code */
    private int $statusCode = 200;

    /** @var string[] Array of HTTP header entries */
    private array $headers = [];

    /**
     * Setter for content type.
     *
     * @param string $contentType Content type
     */
    public function setContentType(string $contentType): void
    {
        $this->contentType = $contentType;
    }

    /**
     * Sets some Header.
     */
    public function addHeader(): void
    {
        header('Expires: Thu, 07 Apr 1977 14:47:00 GMT');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Cache-Control: no-store');
        header('Pragma: no-cache');
        header('Vary: Negotiate,Accept');
        header('Content-type: ' . $this->contentType);
        header('X-Frame-Options: SAMEORIGIN');
    }

    /**
     * Adds an extra header.
     *
     * @param string $header
     */
    public function addExtraHeader(string $header): void
    {
        header($header);
    }

    /**
     * Starts Gzip compression.
     */
    public function startCompression(): void
    {
        if (false === $this->config->get('main.enableGzipCompression') || !DEBUG) {
            ob_start('ob_gzhandler');
        }
    }

    /**
     *  Sends the CORS header.
     */
    public function sendCorsHeader(): void
    {
        header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
    }

    /**
     * Fetch all HTTP request headers.
     */
    public function fetchAllHeaders(): void
    {
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) === 'HTTP_') {
                $this->headers[str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($name, 5))))] = $value;
            }
        }
    }

    /**
     * Returns the HTTP status code
     *
     * @return int
     */
    public function getStatusCode(): int
    {
        return $this->statusCode;
    }

    /**
     * Returns the HTTP header value for "X-PMF-Token"
     *
     * @return string
     */
    public function getClientApiToken(): string
    {
        return $this->headers['x-pmf-token'] ?? '';
    }

    /**
     * URL to redirect
     *
     * @param string $url
     */
    public function redirect(string $url): void
    {
        // Before a redirection we must force the PHP session update
        // for preventing data loss
        session_write_close();
        $this->setStatus(301);
        header('Location: ' . $url);
    }

    /**
     * Sets a HTTP status header.
     *
     * @param int $code HTTP status code
     */
    public function setStatus(int $code): void
    {
        $this->statusCode = $code;
        http_response_code($code);
    }

    /**
     * Sends any kind of data with optional HTTP headers as JSON.
     *
     * @param mixed        $payload What to send
     * @param string|array $headers Which headers to send
     */
    public function sendJsonWithHeaders($payload, $headers = ''): void
    {
        $this->sendWithHeaders($payload, $headers, true);
    }

    /**
     * Sends any kind of data with optional HTTP headers as text or JSON.
     *
     * @param mixed        $payload What to send
     * @param string|array $headers Which headers to send
     * @param bool         $isJson  Send as JSON?
     */
    public function sendWithHeaders($payload, $headers = '', bool $isJson = false): void
    {
        $validHeaders = [];

        if (is_string($headers) && strlen($headers) > 0) {
            $validHeaders[] = $headers;
        } elseif (is_array($headers)) {
            foreach ($headers as $header) {
                if (strlen($header) > 0) {
                    $validHeaders[] = $header;
                }
            }
        }

        foreach ($validHeaders as $header) {
            header($header);
        }

        if ($isJson) {
            header('Content-Type: application/json');
            echo json_encode($payload);
        } else {
            echo $payload;
        }
    }
}