Classes

File io/http/HTTPResponseCustom.class.php

File io/http/HTTPResponseCustom.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 
<?php
/**
 * Manages response via the HTTP protocol
 * @package     IO
 * @subpackage  HTTP
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Regis VIARRE <crowkait@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2017 03 23
 * @since       PHPBoost 3.0 - 2010 01 23
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class HTTPResponseCustom
{
    const PROTOCOL = 'HTTP/1.1';

    private static $status_list = array(
        '101' => 'Switching Protocols',
        '200' => 'OK',
        '201' => 'Created',
        '202' => 'Accepted',
        '203' => 'Non-Authoritative Information',
        '204' => 'No Content',
        '205' => 'Reset Content',
        '206' => 'Partial Content',
        '300' => 'Multiple Choices',
        '301' => 'Moved Permanently',
        '302' => 'Found',
        '303' => 'See Other',
        '304' => 'Not Modified',
        '305' => 'Use Proxy',
        '306' => '(Unused)',
        '307' => 'Temporary Redirect',
        '400' => 'Bad Request',
        '401' => 'Unauthorized',
        '402' => 'Payment Required',
        '403' => 'Forbidden',
        '404' => 'Not Found',
        '405' => 'Method Not Allowed',
        '406' => 'Not Acceptable',
        '407' => 'Proxy Authentication Required',
        '408' => 'Request Timeout',
        '409' => 'Conflict',
        '410' => 'Gone',
        '411' => 'Length Required',
        '412' => 'Precondition Failed',
        '413' => 'Request Entity Too Large',
        '414' => 'Request-URI Too Long',
        '415' => 'Unsupported Media Type',
        '416' => 'Requested Range Not Satisfiable',
        '417' => 'Expectation Failed',
        '500' => 'Internal Server Error',
        '501' => 'Not Implemented',
        '502' => 'Bad Gateway',
        '503' => 'Service Unavailable',
        '504' => 'Gateway Timeout',
        '505' => 'HTTP Version Not Supported');

    private $last_ob_content_before_clean = '';

    public function __construct($status_code = 200)
    {
        $this->set_status_code($status_code);
    }

    /**
     * Send header to client.
     * @param string $url
     */
    public function set_header($name, $value)
    {
        header($name . ': ' . $value);
    }

    /**
     * Set defaut headers for the response.
     * @param string $url
     */
    public function set_default_attributes()
    {
        $this->set_header('Expires', '-1');
        $this->set_header('Last-Modified', gmdate("D, d M Y H:i:s") . " GMT");
        $this->set_header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        $this->set_header('Pragma', 'no-cache');
    }

    /**
     * Redirects the user to the URL and stops purely the script execution (database deconnexion...).
     * @param string $url URL at which you want to redirect the user.
     */
    public function redirect($url, $message = '', $message_type = MessageHelper::SUCCESS, $message_duration = 5)
    {
        if (!($url instanceof Url))
        {
            $url = new Url($url);
        }
        $url = $url->absolute();

        if (!empty($message))
        {
            $this->set_cookie(new HTTPCookie('message', $message, time() + 3600));
            $this->set_cookie(new HTTPCookie('message_type', $message_type, time() + 3600));
            $this->set_cookie(new HTTPCookie('message_duration', $message_duration, time() + 3600));
        }

        header('Location:' . $url);
        exit;
    }

    /**
     * Set cookie for the application's client.
     * @param HTTPCookie $cookie
     */
    public function set_cookie(HTTPCookie $cookie)
    {
        setcookie($cookie->get_name(), $cookie->get_value(), $cookie->get_expiration_date(), $cookie->get_path(), $cookie->get_domain(), $cookie->get_secure(), $cookie->get_httponly());
    }

    /**
     * Deletes the cookie
     * @param string $cookie_name the name of the cookie to delete
     */
    public function delete_cookie($cookie_name)
    {
        $this->set_cookie(new HTTPCookie($cookie_name, '', -1));
    }

    /**
     * Clean the output buffer.
     */
    public function clean_output()
    {
        $this->last_ob_content_before_clean = ob_get_contents();
        ob_end_clean();
    }

    /**
     * Returns the previous output buffer content.
     * @return string the previous output buffer content.
     */
    public function get_previous_ob_content()
    {
        return $this->last_ob_content_before_clean;
    }

    /**
     * Send the status code
     * @param int $status_code
     */
    public function set_status_code($status_code)
    {
        if (isset(self::$status_list[$status_code]))
        {
            header(self::PROTOCOL . ' ' . $status_code . ' ' . self::$status_list[$status_code]);
        }
    }
}
?>
Annonces