3 * StatusNet, the distributed open-source microblogging tool
5 * Low-level generator for HTML
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @author Sarven Capadisli <csarven@status.net>
26 * @copyright 2008 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/xmloutputter.php';
37 // Can include XHTML options but these are too fragile in practice.
38 define('PAGE_TYPE_PREFS', 'text/html');
41 * Low-level generator for HTML
43 * Abstracts some of the code necessary for HTML generation. Especially
44 * has methods for generating HTML form elements. Note that these have
45 * been created kind of haphazardly, not with an eye to making a general
46 * HTML-creation class.
50 * @author Evan Prodromou <evan@status.net>
51 * @author Sarven Capadisli <csarven@status.net>
52 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
53 * @link http://status.net/
59 class HTMLOutputter extends XMLOutputter
64 * Just wraps the XMLOutputter constructor.
66 * @param string $output URI to output to, default = stdout
67 * @param boolean $indent Whether to indent output, default true
70 function __construct($output='php://output', $indent=null)
72 parent::__construct($output, $indent);
76 * Start an HTML document
78 * If $type isn't specified, will attempt to do content negotiation.
80 * Attempts to do content negotiation for language, also.
82 * @param string $type MIME type to use; default is to do negotation.
84 * @todo extract content negotiation code to an HTTP module or class.
89 function startHTML($type=null)
92 $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
93 $_SERVER['HTTP_ACCEPT'] : null;
95 // XXX: allow content negotiation for RDF, RSS, or XRDS
97 $cp = common_accept_to_prefs($httpaccept);
98 $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
100 $type = common_negotiate_type($cp, $sp);
103 // TRANS: Client exception 406
104 throw new ClientException(_('This page is not available in a '.
105 'media type you accept'), 406);
109 header('Content-Type: '.$type);
111 // Output anti-framing headers to prevent clickjacking (respected by newer
113 if (common_config('javascript', 'bustframes')) {
114 header('X-XSS-Protection: 1; mode=block'); // detect XSS Reflection attacks
115 header('X-Frame-Options: SAMEORIGIN'); // no rendering if origin mismatch
118 $this->extraHeaders();
119 if (preg_match("/.*\/.*xml/", $type)) {
120 // Required for XML documents
123 $this->xw->writeDTD('html',
124 '-//W3C//DTD XHTML 1.0 Strict//EN',
125 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
127 $language = $this->getLanguage();
130 'xmlns' => 'http://www.w3.org/1999/xhtml',
131 'xml:lang' => $language,
135 if (Event::handle('StartHtmlElement', array($this, &$attrs))) {
136 $this->elementStart('html', $attrs);
137 Event::handle('EndHtmlElement', array($this, &$attrs));
141 function getLanguage()
143 // FIXME: correct language for interface
144 return common_language();
148 * Ends an HTML document
154 $this->elementEnd('html');
159 * To specify additional HTTP headers for the action
163 function extraHeaders()
165 // Needs to be overloaded
169 * Output an HTML text input element
171 * Despite the name, it is specifically for outputting a
172 * text input element, not other <input> elements. It outputs
173 * a cluster of elements, including a <label> and an associated
176 * If $attrs['type'] does not exist it will be set to 'text'.
178 * @param string $id element ID, must be unique on page
179 * @param string $label text of label for the element
180 * @param string $value value of the element, default null
181 * @param string $instructions instructions for valid input
182 * @param string $name name of the element; if null, the id will
184 * @param bool $required HTML5 required attribute (exclude when false)
185 * @param array $attrs Initial attributes manually set in an array (overwritten by previous options)
187 * @todo add a $maxLength parameter
188 * @todo add a $size parameter
193 function input($id, $label, $value=null, $instructions=null, $name=null, $required=false, array $attrs=array())
195 $this->element('label', array('for' => $id), $label);
196 if (!array_key_exists('type', $attrs)) {
197 $attrs['type'] = 'text';
200 $attrs['name'] = is_null($name) ? $id : $name;
201 if (!is_null($value)) { // value can be 0 or ''
202 $attrs['value'] = $value;
204 if (!empty($required)) {
205 $attrs['required'] = 'required';
207 $this->element('input', $attrs);
209 $this->element('p', 'form_guide', $instructions);
214 * output an HTML checkbox and associated elements
216 * Note that the value is default 'true' (the string), which can
217 * be used by Action::boolean()
219 * @param string $id element ID, must be unique on page
220 * @param string $label text of label for the element
221 * @param string $checked if the box is checked, default false
222 * @param string $instructions instructions for valid input
223 * @param string $value value of the checkbox, default 'true'
224 * @param string $disabled show the checkbox disabled, default false
228 * @todo add a $name parameter
231 function checkbox($id, $label, $checked=false, $instructions=null,
232 $value='true', $disabled=false)
234 $attrs = array('name' => $id,
235 'type' => 'checkbox',
236 'class' => 'checkbox',
239 $attrs['value'] = $value;
242 $attrs['checked'] = 'checked';
245 $attrs['disabled'] = 'true';
247 $this->element('input', $attrs);
249 $this->element('label', array('class' => 'checkbox',
254 $this->element('p', 'form_guide', $instructions);
259 * output an HTML combobox/select and associated elements
261 * $content is an array of key-value pairs for the dropdown, where
262 * the key is the option value attribute and the value is the option
263 * text. (Careful on the overuse of 'value' here.)
265 * @param string $id element ID, must be unique on page
266 * @param string $label text of label for the element
267 * @param array $content options array, value => text
268 * @param string $instructions instructions for valid input
269 * @param string $blank_select whether to have a blank entry, default false
270 * @param string $selected selected value, default null
274 * @todo add a $name parameter
277 function dropdown($id, $label, $content, $instructions=null,
278 $blank_select=false, $selected=null)
280 $this->element('label', array('for' => $id), $label);
281 $this->elementStart('select', array('id' => $id, 'name' => $id));
283 $this->element('option', array('value' => ''));
285 foreach ($content as $value => $option) {
286 if ($value == $selected) {
287 $this->element('option', array('value' => $value,
288 'selected' => 'selected'),
291 $this->element('option', array('value' => $value), $option);
294 $this->elementEnd('select');
296 $this->element('p', 'form_guide', $instructions);
301 * output an HTML hidden element
303 * $id is re-used as name
305 * @param string $id element ID, must be unique on page
306 * @param string $value hidden element value, default null
307 * @param string $name name, if different than ID
312 function hidden($id, $value, $name=null)
314 $this->element('input', array('name' => $name ?: $id,
321 * output an HTML password input and associated elements
323 * @param string $id element ID, must be unique on page
324 * @param string $label text of label for the element
325 * @param string $instructions instructions for valid input
329 * @todo add a $name parameter
332 function password($id, $label, $instructions=null)
334 $this->element('label', array('for' => $id), $label);
335 $attrs = array('name' => $id,
336 'type' => 'password',
337 'class' => 'password',
339 $this->element('input', $attrs);
341 $this->element('p', 'form_guide', $instructions);
346 * output an HTML submit input and associated elements
348 * @param string $id element ID, must be unique on page
349 * @param string $label text of the button
350 * @param string $cls class of the button, default 'submit'
351 * @param string $name name, if different than ID
352 * @param string $title title text for the submit button
356 * @todo add a $name parameter
359 function submit($id, $label, $cls='submit', $name=null, $title=null)
361 $this->element('input', array('type' => 'submit',
363 'name' => $name ?: $id,
370 * output a script (almost always javascript) tag
372 * @param string $src relative or absolute script path
373 * @param string $type 'type' attribute value of the tag
377 function script($src, $type='text/javascript')
379 if (Event::handle('StartScriptElement', array($this,&$src,&$type))) {
381 $url = parse_url($src);
383 if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
385 // XXX: this seems like a big assumption
387 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
389 $src = common_path($src, StatusNet::isHTTPS()) . '?version=' . GNUSOCIAL_VERSION;
393 if (StatusNet::isHTTPS()) {
395 $sslserver = common_config('javascript', 'sslserver');
397 if (empty($sslserver)) {
398 if (is_string(common_config('site', 'sslserver')) &&
399 mb_strlen(common_config('site', 'sslserver')) > 0) {
400 $server = common_config('site', 'sslserver');
401 } else if (common_config('site', 'server')) {
402 $server = common_config('site', 'server');
404 $path = common_config('site', 'path') . '/js/';
406 $server = $sslserver;
407 $path = common_config('javascript', 'sslpath');
409 $path = common_config('javascript', 'path');
417 $path = common_config('javascript', 'path');
420 $path = common_config('site', 'path') . '/js/';
423 $server = common_config('javascript', 'server');
425 if (empty($server)) {
426 $server = common_config('site', 'server');
432 if ($path[strlen($path)-1] != '/') {
436 if ($path[0] != '/') {
440 $src = $protocol.'://'.$server.$path.$src . '?version=' . GNUSOCIAL_VERSION;
444 $this->element('script', array('type' => $type,
448 Event::handle('EndScriptElement', array($this,$src,$type));
453 * output a script (almost always javascript) tag with inline
456 * @param string $code code to put in the script tag
457 * @param string $type 'type' attribute value of the tag
462 function inlineScript($code, $type='text/javascript')
464 if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
465 $this->elementStart('script', array('type' => $type));
466 if($type == 'text/javascript') {
467 $this->raw('/*<![CDATA[*/ '); // XHTML compat
470 if($type == 'text/javascript') {
471 $this->raw(' /*]]>*/'); // XHTML compat
473 $this->elementEnd('script');
474 Event::handle('EndInlineScriptElement', array($this,$code,$type));
481 * @param string $src relative path within the theme directory, or an absolute path
482 * @param string $theme 'theme' that contains the stylesheet
483 * @param string media 'media' attribute of the tag
487 function cssLink($src,$theme=null,$media=null)
489 if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
490 $url = parse_url($src);
491 if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
493 if(file_exists(Theme::file($src,$theme))){
494 $src = Theme::path($src, $theme);
496 $src = common_path($src, StatusNet::isHTTPS());
498 $src.= '?version=' . GNUSOCIAL_VERSION;
500 $this->element('link', array('rel' => 'stylesheet',
501 'type' => 'text/css',
504 Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
509 * output a style (almost always css) tag with inline
512 * @param string $code code to put in the style tag
513 * @param string $type 'type' attribute value of the tag
514 * @param string $media 'media' attribute value of the tag
519 function style($code, $type = 'text/css', $media = null)
521 if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
522 $this->elementStart('style', array('type' => $type, 'media' => $media));
524 $this->elementEnd('style');
525 Event::handle('EndStyleElement', array($this,$code,$type,$media));
530 * output an HTML textarea and associated elements
532 * @param string $id element ID, must be unique on page
533 * @param string $label text of label for the element
534 * @param string $content content of the textarea, default none
535 * @param string $instructions instructions for valid input
536 * @param string $name name of textarea; if null, $id will be used
537 * @param int $cols number of columns
538 * @param int $rows number of rows
539 * @param bool $required HTML5 required attribute (exclude when false)
548 $instructions = null,
554 $this->element('label', array('for' => $id), $label);
560 $attrs['name'] = is_null($name) ? $id : $name;
563 $attrs['cols'] = $cols;
567 $attrs['rows'] = $rows;
572 is_null($content) ? '' : $content
575 $this->element('p', 'form_guide', $instructions);
580 * Internal script to autofocus the given element on page onload.
582 * @param string $id element ID, must refer to an existing element
587 function autofocus($id)
590 ' $(document).ready(function() {'.
591 ' var el = $("#' . $id . '");'.
592 ' if (el.length) { el.focus(); }'.