Adding support for module shared model. If a module admin exists and there is a file...
[akelos.git] / lib / AkResponse.php
blob00a18133ea4ca8e610039ca461804bb133a62f94
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
11 if(!class_exists('AkResponse')){
13 /**
14 * @package ActionController
15 * @subpackage Response
16 * @author Bermi Ferrer <bermi a.t akelos c.om>
17 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
18 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
20 class AkResponse extends AkObject
22 var $_resutl_stack = array();
23 var $_headers = array();
24 var $_headers_sent = array();
25 var $body = '';
26 var $__Logger;
28 function set($data, $id = null)
30 if(isset($id)){
31 $this->_resutl_stack[$id] = $data;
32 }else{
33 $this->_resutl_stack[] = $data;
37 function &get($id)
39 if(isset($this->_resutl_stack[$id])){
40 return $this->_resutl_stack[$id];
42 return false;
45 function getAll()
47 return $this->_resutl_stack;
50 function addHeader()
52 $args = func_get_args();
53 if(!empty($args[1])){
54 $this->_headers[$args[0]] = $args[1];
55 }elseif (!empty($args[0]) && is_array($args[0])){
56 $this->_headers = array_merge($this->_headers,$args[0]);
57 }elseif (!empty($args[0])){
58 $this->_headers[] = $args[0];
62 function outputResults()
64 $this->sendHeaders();
65 if(is_object($this->body) && method_exists($this->body,'stream')){
66 AK_LOG_EVENTS && !empty($this->_Logger) ? $this->_Logger->message("Sending response as stream") : null;
67 $this->body->stream();
68 }else{
69 AK_LOG_EVENTS && !empty($this->_Logger) ? $this->_Logger->message("Sending response") : null;
70 echo $this->body;
74 function sendHeaders($terminate_if_redirected = true)
76 /**
77 * Fix a problem with IE 6.0 on opening downloaded files:
78 * If Cache-Control: IE removes the file it just downloaded from
79 * its cache immediately
80 * after it displays the "open/save" dialog, which means that if you
81 * hit "open" the file isn't there anymore when the application that
82 * is called for handling the download is run, so let's workaround that
84 if(isset($this->_headers['Cache-Control']) && $this->_headers['Cache-Control'] == 'no-cache'){
85 $this->_headers['Cache-Control'] = 'private';
87 if(!empty($this->_headers['Status'])){
88 $status = $this->_getStatusHeader($this->_headers['Status']);
89 array_unshift($this->_headers, $status ? $status : (strstr('HTTP/1.1 '.$this->_headers['Status'],'HTTP') ? $this->_headers['Status'] : 'HTTP/1.1 '.$this->_headers['Status']));
90 unset($this->_headers['Status']);
93 if(!empty($this->_headers) && is_array($this->_headers)){
94 $this->addHeader('Connection: close');
95 foreach ($this->_headers as $k=>$v){
96 $header = trim((!is_numeric($k) ? $k.': ' : '').$v);
97 $this->_headers_sent[] = $header;
98 if(strtolower(substr($header,0,9)) == 'location:'){
99 $_redirected = true;
100 if(AK_DESKTOP){
101 $javascript_redirection = '<title>'.Ak::t('Loading...').'</title><script type="text/javascript">location = "'.substr($header,9).'";</script>';
102 continue;
105 if(strtolower(substr($header,0,13)) == 'content-type:'){
106 $_has_content_type = true;
108 AK_LOG_EVENTS && !empty($this->_Logger) ? $this->_Logger->message("Sending header: $header") : null;
109 header($header);
113 if(empty($_has_content_type) && defined('AK_CHARSET') && (empty($_redirected) || (!empty($_redirected) && !empty($javascript_redirection)))){
114 header('Content-Type: text/html; charset='.AK_CHARSET);
115 $this->_headers_sent[] = 'Content-Type: text/html; charset='.AK_CHARSET;
118 if(!empty($javascript_redirection)){
119 echo $javascript_redirection;
122 $terminate_if_redirected ? (!empty($_redirected) ? exit() : null) : null;
125 function deleteHeader($header)
127 unset($this->_headers[$header]);
131 * Redirects to given $url, after turning off $this->autoRender.
133 * @param unknown_type $url
135 function redirect ($url)
137 $this->autoRender = false;
138 if(!empty($this->_headers['Status']) && substr($this->_headers['Status'],0,3) != '301'){
139 $this->_headers['Status'] = 302;
141 $this->addHeader('Location', $url);
142 $this->sendHeaders();
146 function _getStatusHeader($status_code)
148 $status_codes = array (
149 100 => "HTTP/1.1 100 Continue",
150 101 => "HTTP/1.1 101 Switching Protocols",
151 200 => "HTTP/1.1 200 OK",
152 201 => "HTTP/1.1 201 Created",
153 202 => "HTTP/1.1 202 Accepted",
154 203 => "HTTP/1.1 203 Non-Authoritative Information",
155 204 => "HTTP/1.1 204 No Content",
156 205 => "HTTP/1.1 205 Reset Content",
157 206 => "HTTP/1.1 206 Partial Content",
158 300 => "HTTP/1.1 300 Multiple Choices",
159 301 => "HTTP/1.1 301 Moved Permanently",
160 302 => "HTTP/1.1 302 Found",
161 303 => "HTTP/1.1 303 See Other",
162 304 => "HTTP/1.1 304 Not Modified",
163 305 => "HTTP/1.1 305 Use Proxy",
164 307 => "HTTP/1.1 307 Temporary Redirect",
165 400 => "HTTP/1.1 400 Bad Request",
166 401 => "HTTP/1.1 401 Unauthorized",
167 402 => "HTTP/1.1 402 Payment Required",
168 403 => "HTTP/1.1 403 Forbidden",
169 404 => "HTTP/1.1 404 Not Found",
170 405 => "HTTP/1.1 405 Method Not Allowed",
171 406 => "HTTP/1.1 406 Not Acceptable",
172 407 => "HTTP/1.1 407 Proxy Authentication Required",
173 408 => "HTTP/1.1 408 Request Time-out",
174 409 => "HTTP/1.1 409 Conflict",
175 410 => "HTTP/1.1 410 Gone",
176 411 => "HTTP/1.1 411 Length Required",
177 412 => "HTTP/1.1 412 Precondition Failed",
178 413 => "HTTP/1.1 413 Request Entity Too Large",
179 414 => "HTTP/1.1 414 Request-URI Too Large",
180 415 => "HTTP/1.1 415 Unsupported Media Type",
181 416 => "HTTP/1.1 416 Requested range not satisfiable",
182 417 => "HTTP/1.1 417 Expectation Failed",
183 500 => "HTTP/1.1 500 Internal Server Error",
184 501 => "HTTP/1.1 501 Not Implemented",
185 502 => "HTTP/1.1 502 Bad Gateway",
186 503 => "HTTP/1.1 503 Service Unavailable",
187 504 => "HTTP/1.1 504 Gateway Time-out"
189 return empty($status_codes[$status_code]) ? false : $status_codes[$status_code];
194 function &AkResponse()
196 $null = null;
197 $AkResponse =& Ak::singleton('AkResponse', $null);
198 AK_LOG_EVENTS && empty($AkResponse->_Logger) ? ($AkResponse->_Logger =& Ak::getLogger()) : null;
199 return $AkResponse;