| 1 | <?php␊ |
| 2 | /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */␊ |
| 3 | /*␊ |
| 4 | # ***** BEGIN LICENSE BLOCK *****␊ |
| 5 | # This file is part of Plume Framework, a simple PHP Application Framework.␊ |
| 6 | # Copyright (C) 2001-2007 Loic d'Anterroches and contributors.␊ |
| 7 | #␊ |
| 8 | # Plume Framework is free software; you can redistribute it and/or modify␊ |
| 9 | # it under the terms of the GNU Lesser General Public License as published by␊ |
| 10 | # the Free Software Foundation; either version 2.1 of the License, or␊ |
| 11 | # (at your option) any later version.␊ |
| 12 | #␊ |
| 13 | # Plume Framework is distributed in the hope that it will be useful,␊ |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of␊ |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␊ |
| 16 | # GNU Lesser General Public License for more details.␊ |
| 17 | #␊ |
| 18 | # You should have received a copy of the GNU Lesser General Public License␊ |
| 19 | # along with this program; if not, write to the Free Software␊ |
| 20 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA␊ |
| 21 | #␊ |
| 22 | # ***** END LICENSE BLOCK ***** */␊ |
| 23 | ␊ |
| 24 | /**␊ |
| 25 | * Response object to be constructed by the views.␊ |
| 26 | *␊ |
| 27 | * When constructing a view, the response object must be populated and␊ |
| 28 | * returned. The response is then displayed to the visitor. ␊ |
| 29 | * The interest of using a response object is that we can run a post␊ |
| 30 | * filter action on the response. For example you can run a filter that␊ |
| 31 | * is checking that all the output is valid HTML and write a logfile if␊ |
| 32 | * this is not the case.␊ |
| 33 | */␊ |
| 34 | class Pluf_HTTP_Response␊ |
| 35 | {␊ |
| 36 | /**␊ |
| 37 | * Content of the response.␊ |
| 38 | */␊ |
| 39 | public $content = '';␊ |
| 40 | ␊ |
| 41 | /**␊ |
| 42 | * Array of the headers to add.␊ |
| 43 | *␊ |
| 44 | * For example $this->headers['Content-Type'] = 'text/html; charset=utf-8';␊ |
| 45 | */␊ |
| 46 | public $headers = array();␊ |
| 47 | ␊ |
| 48 | /**␊ |
| 49 | * Status code of the answer.␊ |
| 50 | */␊ |
| 51 | public $status_code = 200;␊ |
| 52 | ␊ |
| 53 | /**␊ |
| 54 | * Cookies to send.␊ |
| 55 | *␊ |
| 56 | * $this->cookies['my_cookie'] = 'content of the cookie';␊ |
| 57 | */␊ |
| 58 | public $cookies = array();␊ |
| 59 | ␊ |
| 60 | /**␊ |
| 61 | * Status code list.␊ |
| 62 | *␊ |
| 63 | * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html␊ |
| 64 | */␊ |
| 65 | public $status_code_list = array(␊ |
| 66 | '100' => 'CONTINUE',␊ |
| 67 | '101' => 'SWITCHING PROTOCOLS',␊ |
| 68 | '200' => 'OK',␊ |
| 69 | '201' => 'CREATED',␊ |
| 70 | '202' => 'ACCEPTED',␊ |
| 71 | '203' => 'NON-AUTHORITATIVE INFORMATION',␊ |
| 72 | '204' => 'NO CONTENT',␊ |
| 73 | '205' => 'RESET CONTENT',␊ |
| 74 | '206' => 'PARTIAL CONTENT',␊ |
| 75 | '300' => 'MULTIPLE CHOICES',␊ |
| 76 | '301' => 'MOVED PERMANENTLY',␊ |
| 77 | '302' => 'FOUND',␊ |
| 78 | '303' => 'SEE OTHER',␊ |
| 79 | '304' => 'NOT MODIFIED',␊ |
| 80 | '305' => 'USE PROXY',␊ |
| 81 | '306' => 'RESERVED',␊ |
| 82 | '307' => 'TEMPORARY REDIRECT',␊ |
| 83 | '400' => 'BAD REQUEST',␊ |
| 84 | '401' => 'UNAUTHORIZED',␊ |
| 85 | '402' => 'PAYMENT REQUIRED',␊ |
| 86 | '403' => 'FORBIDDEN',␊ |
| 87 | '404' => 'NOT FOUND',␊ |
| 88 | '405' => 'METHOD NOT ALLOWED',␊ |
| 89 | '406' => 'NOT ACCEPTABLE',␊ |
| 90 | '407' => 'PROXY AUTHENTICATION REQUIRED',␊ |
| 91 | '408' => 'REQUEST TIMEOUT',␊ |
| 92 | '409' => 'CONFLICT',␊ |
| 93 | '410' => 'GONE',␊ |
| 94 | '411' => 'LENGTH REQUIRED',␊ |
| 95 | '412' => 'PRECONDITION FAILED',␊ |
| 96 | '413' => 'REQUEST ENTITY TOO LARGE',␊ |
| 97 | '414' => 'REQUEST-URI TOO LONG',␊ |
| 98 | '415' => 'UNSUPPORTED MEDIA TYPE',␊ |
| 99 | '416' => 'REQUESTED RANGE NOT SATISFIABLE',␊ |
| 100 | '417' => 'EXPECTATION FAILED',␊ |
| 101 | '500' => 'INTERNAL SERVER ERROR',␊ |
| 102 | '501' => 'NOT IMPLEMENTED',␊ |
| 103 | '502' => 'BAD GATEWAY',␊ |
| 104 | '503' => 'SERVICE UNAVAILABLE',␊ |
| 105 | '504' => 'GATEWAY TIMEOUT',␊ |
| 106 | '505' => 'HTTP VERSION NOT SUPPORTED'␊ |
| 107 | );␊ |
| 108 | ␊ |
| 109 | ␊ |
| 110 | /**␊ |
| 111 | * Constructor of the response.␊ |
| 112 | *␊ |
| 113 | * @param string Content of the response ('')␊ |
| 114 | * @param string MimeType of the response (null) if not given will␊ |
| 115 | * default to the one given in the configuration 'mimetype'␊ |
| 116 | */␊ |
| 117 | function __construct($content='', $mimetype=null)␊ |
| 118 | {␊ |
| 119 | if (is_null($mimetype)) {␊ |
| 120 | $mimetype = Pluf::f('mimetype', 'text/html').'; charset=utf-8';␊ |
| 121 | }␊ |
| 122 | $this->content = $content;␊ |
| 123 | $this->headers['Content-Type'] = $mimetype;␊ |
| 124 | $this->headers['X-Powered-By'] = 'Pluf - http://pluf.org/';␊ |
| 125 | $this->status_code = 200;␊ |
| 126 | $this->cookies = array();␊ |
| 127 | }␊ |
| 128 | ␊ |
| 129 | /**␊ |
| 130 | * Render a response object.␊ |
| 131 | */␊ |
| 132 | function render($output_body=true)␊ |
| 133 | {␊ |
| 134 | if ($this->status_code >= 200 ␊ |
| 135 | && $this->status_code != 204 ␊ |
| 136 | && $this->status_code != 304) {␊ |
| 137 | $this->headers['Content-Length'] = strlen($this->content);␊ |
| 138 | }␊ |
| 139 | $this->outputHeaders();␊ |
| 140 | if ($output_body) {␊ |
| 141 | echo $this->content;␊ |
| 142 | }␊ |
| 143 | }␊ |
| 144 | ␊ |
| 145 | /**␊ |
| 146 | * Output headers.␊ |
| 147 | */␊ |
| 148 | function outputHeaders()␊ |
| 149 | {␊ |
| 150 | if (!defined('IN_UNIT_TESTS')) {␊ |
| 151 | header('HTTP/1.0 '.$this->status_code.' '␊ |
| 152 | .$this->status_code_list[$this->status_code],␊ |
| 153 | true, $this->status_code);␊ |
| 154 | foreach ($this->headers as $header => $ch) {␊ |
| 155 | header($header.': '.$ch);␊ |
| 156 | }␊ |
| 157 | foreach ($this->cookies as $cookie => $data) {␊ |
| 158 | // name, data, expiration, path, domain, secure, http only␊ |
| 159 | $expire = (null == $data) ? time()-31536000 : time()+31536000;␊ |
| 160 | $data = (null == $data) ? '' : $data;␊ |
| 161 | setcookie($cookie, $data, $expire,␊ |
| 162 | Pluf::f('cookie_path', '/'), ␊ |
| 163 | Pluf::f('cookie_domain', null), ␊ |
| 164 | Pluf::f('cookie_secure', false), ␊ |
| 165 | Pluf::f('cookie_httponly', true)); ␊ |
| 166 | }␊ |
| 167 | } else {␊ |
| 168 | $_COOKIE = array();␊ |
| 169 | foreach ($this->cookies as $cookie => $data) {␊ |
| 170 | $_COOKIE[$cookie] = $data;␊ |
| 171 | }␊ |
| 172 | }␊ |
| 173 | }␊ |
| 174 | }␊ |
| 175 | |