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('GNUSOCIAL')) { exit(1); }
33 // Can include XHTML options but these are too fragile in practice.
34 define('PAGE_TYPE_PREFS', 'text/html');
37 * Low-level generator for HTML
39 * Abstracts some of the code necessary for HTML generation. Especially
40 * has methods for generating HTML form elements. Note that these have
41 * been created kind of haphazardly, not with an eye to making a general
42 * HTML-creation class.
46 * @author Evan Prodromou <evan@status.net>
47 * @author Sarven Capadisli <csarven@status.net>
48 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49 * @link http://status.net/
55 class HTMLOutputter extends XMLOutputter
57 protected $DTD = array('doctype' => 'html',
58 'spec' => '-//W3C//DTD XHTML 1.0 Strict//EN',
59 'uri' => 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
63 * Just wraps the XMLOutputter constructor.
65 * @param string $output URI to output to, default = stdout
66 * @param boolean $indent Whether to indent output, default true
69 function __construct($output='php://output', $indent=null)
71 parent::__construct($output, $indent);
75 * Start an HTML document
77 * If $type isn't specified, will attempt to do content negotiation.
79 * Attempts to do content negotiation for language, also.
81 * @param string $type MIME type to use; default is to do negotation.
83 * @todo extract content negotiation code to an HTTP module or class.
88 function startHTML($type=null)
91 $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
92 $_SERVER['HTTP_ACCEPT'] : null;
94 // XXX: allow content negotiation for RDF, RSS, or XRDS
96 $cp = common_accept_to_prefs($httpaccept);
97 $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
99 $type = common_negotiate_type($cp, $sp);
102 // TRANS: Client exception 406
103 throw new ClientException(_('This page is not available in a '.
104 'media type you accept'), 406);
108 header('Content-Type: '.$type);
110 // Output anti-framing headers to prevent clickjacking (respected by newer
112 if (common_config('javascript', 'bustframes')) {
113 header('X-XSS-Protection: 1; mode=block'); // detect XSS Reflection attacks
114 header('X-Frame-Options: SAMEORIGIN'); // no rendering if origin mismatch
117 $this->extraHeaders();
118 if (preg_match("/.*\/.*xml/", $type)) {
119 // Required for XML documents
125 $language = $this->getLanguage();
128 'xmlns' => 'http://www.w3.org/1999/xhtml',
129 'xml:lang' => $language,
133 if (Event::handle('StartHtmlElement', array($this, &$attrs))) {
134 $this->elementStart('html', $attrs);
135 Event::handle('EndHtmlElement', array($this, &$attrs));
139 public function setDTD($doctype, $spec, $uri)
141 $this->DTD = array('doctype' => $doctype, 'spec' => $spec, 'uri' => $uri);
144 protected function writeDTD()
146 $this->xw->writeDTD($this->DTD['doctype'],
151 function getLanguage()
153 // FIXME: correct language for interface
154 return common_language();
158 * Ends an HTML document
164 $this->elementEnd('html');
169 * To specify additional HTTP headers for the action
173 function extraHeaders()
175 // Needs to be overloaded
179 * Output an HTML text input element
181 * Despite the name, it is specifically for outputting a
182 * text input element, not other <input> elements. It outputs
183 * a cluster of elements, including a <label> and an associated
186 * If $attrs['type'] does not exist it will be set to 'text'.
188 * @param string $id element ID, must be unique on page
189 * @param string $label text of label for the element
190 * @param string $value value of the element, default null
191 * @param string $instructions instructions for valid input
192 * @param string $name name of the element; if null, the id will
194 * @param bool $required HTML5 required attribute (exclude when false)
195 * @param array $attrs Initial attributes manually set in an array (overwritten by previous options)
197 * @todo add a $maxLength parameter
198 * @todo add a $size parameter
203 function input($id, $label, $value=null, $instructions=null, $name=null, $required=false, array $attrs=array())
205 $this->element('label', array('for' => $id), $label);
206 if (!array_key_exists('type', $attrs)) {
207 $attrs['type'] = 'text';
210 $attrs['name'] = is_null($name) ? $id : $name;
211 if (array_key_exists('placeholder', $attrs) && (is_null($attrs['placeholder']) || $attrs['placeholder'] === '')) {
212 // If placeholder is type-aware equal to '' or null, unset it as we apparently don't want a placeholder value
213 unset($attrs['placeholder']);
215 // If the placeholder is set use it, or use the label as fallback.
216 $attrs['placeholder'] = isset($attrs['placeholder']) ? $attrs['placeholder'] : $label;
219 if (!is_null($value)) { // value can be 0 or ''
220 $attrs['value'] = $value;
222 if (!empty($required)) {
223 $attrs['required'] = 'required';
225 $this->element('input', $attrs);
227 $this->element('p', 'form_guide', $instructions);
232 * output an HTML checkbox and associated elements
234 * Note that the value is default 'true' (the string), which can
235 * be used by Action::boolean()
237 * @param string $id element ID, must be unique on page
238 * @param string $label text of label for the element
239 * @param string $checked if the box is checked, default false
240 * @param string $instructions instructions for valid input
241 * @param string $value value of the checkbox, default 'true'
242 * @param string $disabled show the checkbox disabled, default false
246 * @todo add a $name parameter
249 function checkbox($id, $label, $checked=false, $instructions=null,
250 $value='true', $disabled=false)
252 $attrs = array('name' => $id,
253 'type' => 'checkbox',
254 'class' => 'checkbox',
257 $attrs['value'] = $value;
260 $attrs['checked'] = 'checked';
263 $attrs['disabled'] = 'true';
265 $this->element('input', $attrs);
267 $this->element('label', array('class' => 'checkbox',
272 $this->element('p', 'form_guide', $instructions);
277 * output an HTML combobox/select and associated elements
279 * $content is an array of key-value pairs for the dropdown, where
280 * the key is the option value attribute and the value is the option
281 * text. (Careful on the overuse of 'value' here.)
283 * @param string $id element ID, must be unique on page
284 * @param string $label text of label for the element
285 * @param array $content options array, value => text
286 * @param string $instructions instructions for valid input
287 * @param string $blank_select whether to have a blank entry, default false
288 * @param string $selected selected value, default null
292 * @todo add a $name parameter
295 function dropdown($id, $label, $content, $instructions=null,
296 $blank_select=false, $selected=null)
298 $this->element('label', array('for' => $id), $label);
299 $this->elementStart('select', array('id' => $id, 'name' => $id));
301 $this->element('option', array('value' => ''));
303 foreach ($content as $value => $option) {
304 if ($value == $selected) {
305 $this->element('option', array('value' => $value,
306 'selected' => 'selected'),
309 $this->element('option', array('value' => $value), $option);
312 $this->elementEnd('select');
314 $this->element('p', 'form_guide', $instructions);
319 * output an HTML hidden element
321 * $id is re-used as name
323 * @param string $id element ID, must be unique on page
324 * @param string $value hidden element value, default null
325 * @param string $name name, if different than ID
330 function hidden($id, $value, $name=null)
332 $this->element('input', array('name' => $name ?: $id,
339 * output an HTML password input and associated elements
341 * @param string $id element ID, must be unique on page
342 * @param string $label text of label for the element
343 * @param string $instructions instructions for valid input
347 * @todo add a $name parameter
350 function password($id, $label, $instructions=null)
352 $this->element('label', array('for' => $id), $label);
353 $attrs = array('name' => $id,
354 'type' => 'password',
355 'class' => 'password',
357 $this->element('input', $attrs);
359 $this->element('p', 'form_guide', $instructions);
364 * output an HTML submit input and associated elements
366 * @param string $id element ID, must be unique on page
367 * @param string $label text of the button
368 * @param string $cls class of the button, default 'submit'
369 * @param string $name name, if different than ID
370 * @param string $title title text for the submit button
374 * @todo add a $name parameter
377 function submit($id, $label, $cls='submit', $name=null, $title=null)
379 $this->element('input', array('type' => 'submit',
381 'name' => $name ?: $id,
388 * output a script (almost always javascript) tag
390 * @param string $src relative or absolute script path
391 * @param string $type 'type' attribute value of the tag
395 function script($src, $type='text/javascript')
397 if (Event::handle('StartScriptElement', array($this,&$src,&$type))) {
399 $url = parse_url($src);
401 if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
403 // XXX: this seems like a big assumption
405 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
407 $src = common_path($src, GNUsocial::isHTTPS()) . '?version=' . GNUSOCIAL_VERSION;
411 if (GNUsocial::isHTTPS()) {
413 $sslserver = common_config('javascript', 'sslserver');
415 if (empty($sslserver)) {
416 if (is_string(common_config('site', 'sslserver')) &&
417 mb_strlen(common_config('site', 'sslserver')) > 0) {
418 $server = common_config('site', 'sslserver');
419 } else if (common_config('site', 'server')) {
420 $server = common_config('site', 'server');
422 $path = common_config('site', 'path') . '/js/';
424 $server = $sslserver;
425 $path = common_config('javascript', 'sslpath');
427 $path = common_config('javascript', 'path');
435 $path = common_config('javascript', 'path');
438 $path = common_config('site', 'path') . '/js/';
441 $server = common_config('javascript', 'server');
443 if (empty($server)) {
444 $server = common_config('site', 'server');
450 if ($path[strlen($path)-1] != '/') {
454 if ($path[0] != '/') {
458 $src = $protocol.'://'.$server.$path.$src . '?version=' . GNUSOCIAL_VERSION;
462 $this->element('script', array('type' => $type,
466 Event::handle('EndScriptElement', array($this,$src,$type));
471 * output a script (almost always javascript) tag with inline
474 * @param string $code code to put in the script tag
475 * @param string $type 'type' attribute value of the tag
480 function inlineScript($code, $type='text/javascript')
482 if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
483 $this->elementStart('script', array('type' => $type));
484 if($type == 'text/javascript') {
485 $this->raw('/*<![CDATA[*/ '); // XHTML compat
488 if($type == 'text/javascript') {
489 $this->raw(' /*]]>*/'); // XHTML compat
491 $this->elementEnd('script');
492 Event::handle('EndInlineScriptElement', array($this,$code,$type));
499 * @param string $src relative path within the theme directory, or an absolute path
500 * @param string $theme 'theme' that contains the stylesheet
501 * @param string media 'media' attribute of the tag
505 function cssLink($src,$theme=null,$media=null)
507 if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
508 $url = parse_url($src);
509 if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
511 if(file_exists(Theme::file($src,$theme))){
512 $src = Theme::path($src, $theme);
514 $src = common_path($src, GNUsocial::isHTTPS());
516 $src.= '?version=' . GNUSOCIAL_VERSION;
518 $this->element('link', array('rel' => 'stylesheet',
519 'type' => 'text/css',
522 Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
527 * output a style (almost always css) tag with inline
530 * @param string $code code to put in the style tag
531 * @param string $type 'type' attribute value of the tag
532 * @param string $media 'media' attribute value of the tag
537 function style($code, $type = 'text/css', $media = null)
539 if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
540 $this->elementStart('style', array('type' => $type, 'media' => $media));
542 $this->elementEnd('style');
543 Event::handle('EndStyleElement', array($this,$code,$type,$media));
548 * output an HTML textarea and associated elements
550 * @param string $id element ID, must be unique on page
551 * @param string $label text of label for the element
552 * @param string $content content of the textarea, default none
553 * @param string $instructions instructions for valid input
554 * @param string $name name of textarea; if null, $id will be used
555 * @param int $cols number of columns
556 * @param int $rows number of rows
557 * @param bool $required HTML5 required attribute (exclude when false)
566 $instructions = null,
572 $this->element('label', array('for' => $id), $label);
578 $attrs['name'] = is_null($name) ? $id : $name;
581 $attrs['cols'] = $cols;
585 $attrs['rows'] = $rows;
590 is_null($content) ? '' : $content
593 $this->element('p', 'form_guide', $instructions);
598 * Internal script to autofocus the given element on page onload.
600 * @param string $id element ID, must refer to an existing element
605 function autofocus($id)
608 ' $(document).ready(function() {'.
609 ' var el = $("#' . $id . '");'.
610 ' if (el.length) { el.focus(); }'.