Aleksander Machniak
2013-03-11 ec6a77bab27984ce05b003af07ac9f42ca410d94
commit | author | age
0c2596 1 <?php
A 2
3 /*
4  +-----------------------------------------------------------------------+
5  | This file is part of the Roundcube PHP suite                          |
6  | Copyright (C) 2005-2012 The Roundcube Dev Team                        |
7  |                                                                       |
8  | Licensed under the GNU General Public License version 3 or            |
9  | any later version with exceptions for skins & plugins.                |
10  | See the README file for a full license statement.                     |
d2534c 11  |                                                                       |
0c2596 12  | CONTENTS:                                                             |
A 13  |   Abstract class for output generation                                |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  | Author: Aleksander Machniak <alec@alec.pl>                            |
17  +-----------------------------------------------------------------------+
18 */
19
20 /**
21  * Class for output generation
22  *
9ab346 23  * @package    Framework
AM 24  * @subpackage View
0c2596 25  */
A 26 abstract class rcube_output
27 {
28     public $browser;
29
30     protected $app;
31     protected $config;
63f130 32     protected $charset = RCUBE_CHARSET;
0c2596 33     protected $env = array();
A 34
35
36     /**
37      * Object constructor
38      */
60226a 39     public function __construct()
0c2596 40     {
38b6aa 41         $this->app     = rcube::get_instance();
0c2596 42         $this->config  = $this->app->config;
A 43         $this->browser = new rcube_browser();
44     }
45
46
47     /**
69baee 48      * Magic getter
T 49      */
50     public function __get($var)
51     {
52         // allow read-only access to $env
53         if ($var == 'env')
54             return $this->env;
55
56         return null;
0c2596 57     }
A 58
59
60     /**
61      * Setter for output charset.
62      * To be specified in a meta tag and sent as http-header
63      *
64      * @param string $charset Charset name
65      */
66     public function set_charset($charset)
67     {
68         $this->charset = $charset;
69     }
70
71
72     /**
73      * Getter for output charset
74      *
75      * @return string Output charset name
76      */
77     public function get_charset()
78     {
79         return $this->charset;
80     }
81
82
83     /**
84      * Set environment variable
85      *
86      * @param string $name   Property name
87      * @param mixed  $value  Property value
88      */
89     public function set_env($name, $value)
90     {
91         $this->env[$name] = $value;
92     }
93
94
95     /**
96      * Environment variable getter.
97      *
98      * @param string $name  Property name
99      *
100      * @return mixed Property value
101      */
102     public function get_env($name)
103     {
104         return $this->env[$name];
105     }
106
107
108     /**
109      * Delete all stored env variables and commands
110      */
111     public function reset()
112     {
113         $this->env = array();
114     }
115
116
117     /**
118      * Invoke display_message command
119      *
120      * @param string  $message  Message to display
121      * @param string  $type     Message type [notice|confirm|error]
122      * @param array   $vars     Key-value pairs to be replaced in localized text
123      * @param boolean $override Override last set message
124      * @param int     $timeout  Message displaying time in seconds
125      */
126     abstract function show_message($message, $type = 'notice', $vars = null, $override = true, $timeout = 0);
127
128
129     /**
130      * Redirect to a certain url.
131      *
132      * @param mixed $p     Either a string with the action or url parameters as key-value pairs
133      * @param int   $delay Delay in seconds
134      */
135     abstract function redirect($p = array(), $delay = 1);
136
137
138     /**
139      * Send output to the client.
140      */
141     abstract function send();
142
143
144     /**
145      * Send HTTP headers to prevent caching a page
146      */
147     public function nocacheing_headers()
148     {
149         if (headers_sent()) {
150             return;
151         }
152
153         header("Expires: ".gmdate("D, d M Y H:i:s")." GMT");
154         header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
155
156         // Request browser to disable DNS prefetching (CVE-2010-0464)
157         header("X-DNS-Prefetch-Control: off");
158
159         // We need to set the following headers to make downloads work using IE in HTTPS mode.
1aceb9 160         if ($this->browser->ie && rcube_utils::https_check()) {
0c2596 161             header('Pragma: private');
A 162             header("Cache-Control: private, must-revalidate");
163         }
164         else {
165             header("Cache-Control: private, no-cache, must-revalidate, post-check=0, pre-check=0");
166             header("Pragma: no-cache");
167         }
168     }
169
988a80 170     /**
T 171      * Send header with expire date 30 days in future
172      *
173      * @param int Expiration time in seconds
174      */
175     public function future_expire_header($offset = 2600000)
176     {
177         if (headers_sent())
178             return;
179
180         header("Expires: " . gmdate("D, d M Y H:i:s", time()+$offset) . " GMT");
181         header("Cache-Control: max-age=$offset");
182         header("Pragma: ");
183     }
184
0c2596 185
A 186     /**
187      * Show error page and terminate script execution
188      *
189      * @param int    $code     Error code
190      * @param string $message  Error message
191      */
192     public function raise_error($code, $message)
193     {
194         // STUB: to be overloaded by specific output classes
195         fputs(STDERR, "Error $code: $message\n");
196         exit(-1);
197     }
198
199
200     /**
60226a 201      * Create an edit field for inclusion on a form
TB 202      *
203      * @param string col field name
204      * @param string value field value
205      * @param array attrib HTML element attributes for field
206      * @param string type HTML element type (default 'text')
207      *
208      * @return string HTML field definition
209      */
210     public static function get_edit_field($col, $value, $attrib, $type = 'text')
211     {
212         static $colcounts = array();
213
214         $fname = '_'.$col;
215         $attrib['name']  = $fname . ($attrib['array'] ? '[]' : '');
216         $attrib['class'] = trim($attrib['class'] . ' ff_' . $col);
217
218         if ($type == 'checkbox') {
219             $attrib['value'] = '1';
220             $input = new html_checkbox($attrib);
221         }
222         else if ($type == 'textarea') {
223             $attrib['cols'] = $attrib['size'];
224             $input = new html_textarea($attrib);
225         }
226         else if ($type == 'select') {
227             $input = new html_select($attrib);
228             $input->add('---', '');
229             $input->add(array_values($attrib['options']), array_keys($attrib['options']));
230         }
231         else if ($attrib['type'] == 'password') {
232             $input = new html_passwordfield($attrib);
233         }
234         else {
235             if ($attrib['type'] != 'text' && $attrib['type'] != 'hidden') {
236                 $attrib['type'] = 'text';
237             }
238             $input = new html_inputfield($attrib);
239         }
240
241         // use value from post
242         if (isset($_POST[$fname])) {
243             $postvalue = rcube_utils::get_input_value($fname, rcube_utils::INPUT_POST, true);
244             $value = $attrib['array'] ? $postvalue[intval($colcounts[$col]++)] : $postvalue;
245         }
246
247         $out = $input->show($value);
248
249         return $out;
250     }
251
252
253     /**
0c2596 254      * Convert a variable into a javascript object notation
A 255      *
256      * @param mixed Input value
257      *
258      * @return string Serialized JSON string
259      */
260     public static function json_serialize($input)
261     {
262         $input = rcube_charset::clean($input);
263
264         // sometimes even using rcube_charset::clean() the input contains invalid UTF-8 sequences
265         // that's why we have @ here
266         return @json_encode($input);
267     }
268
269 }