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')) {
35 // Can include XHTML options but these are too fragile in practice.
36 define('PAGE_TYPE_PREFS', 'text/html');
39 * Low-level generator for HTML
41 * Abstracts some of the code necessary for HTML generation. Especially
42 * has methods for generating HTML form elements. Note that these have
43 * been created kind of haphazardly, not with an eye to making a general
44 * HTML-creation class.
48 * @author Evan Prodromou <evan@status.net>
49 * @author Sarven Capadisli <csarven@status.net>
50 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
51 * @link http://status.net/
56 class HTMLOutputter extends XMLOutputter
58 protected $DTD = ['doctype' => 'html',
59 'spec' => '-//W3C//DTD XHTML 1.0 Strict//EN',
60 'uri' => 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'];
65 * Just wraps the XMLOutputter constructor.
67 * @param string $output URI to output to, default = stdout
68 * @param boolean $indent Whether to indent output, default true
71 public function __construct($output = 'php://output', $indent = null)
73 parent::__construct($output, $indent);
77 * Start an HTML document
79 * If $type isn't specified, will attempt to do content negotiation.
81 * Attempts to do content negotiation for language, also.
83 * @param string $type MIME type to use; default is to do negotation.
86 * @throws ClientException
87 * @todo extract content negotiation code to an HTTP module or class.
91 public function startHTML($type = null)
94 $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
95 $_SERVER['HTTP_ACCEPT'] : null;
97 // XXX: allow content negotiation for RDF, RSS, or XRDS
99 $cp = common_accept_to_prefs($httpaccept);
100 $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
102 $type = common_negotiate_type($cp, $sp);
105 // TRANS: Client exception 406
106 throw new ClientException(_('This page is not available in a ' .
107 'media type you accept'), 406);
111 header('Content-Type: ' . $type);
113 // Output anti-framing headers to prevent clickjacking (respected by newer
115 if (common_config('javascript', 'bustframes')) {
116 header('X-XSS-Protection: 1; mode=block'); // detect XSS Reflection attacks
117 header('X-Frame-Options: SAMEORIGIN'); // no rendering if origin mismatch
120 $this->extraHeaders();
121 if (preg_match("/.*\/.*xml/", $type)) {
122 // Required for XML documents
128 $language = $this->getLanguage();
131 'xmlns' => 'http://www.w3.org/1999/xhtml',
132 'xml:lang' => $language,
136 if (Event::handle('StartHtmlElement', [$this, &$attrs])) {
137 $this->elementStart('html', $attrs);
138 Event::handle('EndHtmlElement', [$this, &$attrs]);
143 * To specify additional HTTP headers for the action
147 public function extraHeaders()
149 // Needs to be overloaded
152 protected function writeDTD()
155 $this->DTD['doctype'],
161 public function getLanguage()
163 // FIXME: correct language for interface
164 return common_language();
167 public function setDTD($doctype, $spec, $uri)
169 $this->DTD = ['doctype' => $doctype, 'spec' => $spec, 'uri' => $uri];
173 * Ends an HTML document
177 public function endHTML()
179 $this->elementEnd('html');
184 * Output an HTML text input element
186 * Despite the name, it is specifically for outputting a
187 * text input element, not other <input> elements. It outputs
188 * a cluster of elements, including a <label> and an associated
191 * If $attrs['type'] does not exist it will be set to 'text'.
193 * @param string $id element ID, must be unique on page
194 * @param string $label text of label for the element
195 * @param string $value value of the element, default null
196 * @param string $instructions instructions for valid input
197 * @param string $name name of the element; if null, the id will be used
198 * @param bool $required HTML5 required attribute (exclude when false)
199 * @param array $attrs Initial attributes manually set in an array (overwritten by previous options)
202 * @todo add a $maxLength parameter
203 * @todo add a $size parameter
207 public function input($id, $label, $value = null, $instructions = null, $name = null, $required = false, array $attrs = [])
209 $this->element('label', ['for' => $id], $label);
210 if (!array_key_exists('type', $attrs)) {
211 $attrs['type'] = 'text';
214 $attrs['name'] = is_null($name) ? $id : $name;
215 if (array_key_exists('placeholder', $attrs) && (is_null($attrs['placeholder']) || $attrs['placeholder'] === '')) {
216 // If placeholder is type-aware equal to '' or null, unset it as we apparently don't want a placeholder value
217 unset($attrs['placeholder']);
219 // If the placeholder is set use it, or use the label as fallback.
220 $attrs['placeholder'] = isset($attrs['placeholder']) ? $attrs['placeholder'] : $label;
223 if (!is_null($value)) { // value can be 0 or ''
224 $attrs['value'] = $value;
226 if (!empty($required)) {
227 $attrs['required'] = 'required';
229 $this->element('input', $attrs);
231 $this->element('p', 'form_guide', $instructions);
236 * output an HTML checkbox and associated elements
238 * Note that the value is default 'true' (the string), which can
239 * be used by Action::boolean()
241 * @param string $id element ID, must be unique on page
242 * @param string $label text of label for the element
243 * @param bool $checked if the box is checked, default false
244 * @param string $instructions instructions for valid input
245 * @param string $value value of the checkbox, default 'true'
246 * @param bool $disabled show the checkbox disabled, default false
250 * @todo add a $name parameter
253 public function checkbox(
257 $instructions = null,
262 $attrs = ['name' => $id,
263 'type' => 'checkbox',
264 'class' => 'checkbox',
267 $attrs['value'] = $value;
270 $attrs['checked'] = 'checked';
273 $attrs['disabled'] = 'true';
275 $this->element('input', $attrs);
279 ['class' => 'checkbox',
285 $this->element('p', 'form_guide', $instructions);
290 * output an HTML combobox/select and associated elements
292 * $content is an array of key-value pairs for the dropdown, where
293 * the key is the option value attribute and the value is the option
294 * text. (Careful on the overuse of 'value' here.)
296 * @param string $id element ID, must be unique on page
297 * @param string $label text of label for the element
298 * @param array $content options array, value => text
299 * @param string $instructions instructions for valid input
300 * @param bool $blank_select whether to have a blank entry, default false
301 * @param string $selected selected value, default null
305 * @todo add a $name parameter
308 public function dropdown(
312 $instructions = null,
313 $blank_select = false,
317 $this->element('label', ['for' => $id], $label);
318 $this->elementStart('select', ['id' => $id, 'name' => $id]);
320 $this->element('option', ['value' => '']);
322 foreach ($content as $value => $option) {
323 if ($value == $selected) {
327 'selected' => 'selected'],
331 $this->element('option', ['value' => $value], $option);
334 $this->elementEnd('select');
336 $this->element('p', 'form_guide', $instructions);
341 * output an HTML hidden element
343 * $id is re-used as name
345 * @param string $id element ID, must be unique on page
346 * @param string $value hidden element value, default null
347 * @param string $name name, if different than ID
352 public function hidden($id, $value, $name = null)
354 $this->element('input', ['name' => $name ?: $id,
361 * output an HTML password input and associated elements
363 * @param string $id element ID, must be unique on page
364 * @param string $label text of label for the element
365 * @param string $instructions instructions for valid input
369 * @todo add a $name parameter
372 public function password($id, $label, $instructions = null)
374 $this->element('label', ['for' => $id], $label);
375 $attrs = ['name' => $id,
376 'type' => 'password',
377 'class' => 'password',
379 $this->element('input', $attrs);
381 $this->element('p', 'form_guide', $instructions);
386 * output an HTML submit input and associated elements
388 * @param string $id element ID, must be unique on page
389 * @param string $label text of the button
390 * @param string $cls class of the button, default 'submit'
391 * @param string $name name, if different than ID
392 * @param string $title title text for the submit button
396 * @todo add a $name parameter
399 public function submit($id, $label, $cls = 'submit', $name = null, $title = null)
401 $this->element('input', ['type' => 'submit',
403 'name' => $name ?: $id,
410 * output a script (almost always javascript) tag
412 * @param string $src relative or absolute script path
413 * @param string $type 'type' attribute value of the tag
417 public function script($src, $type = 'text/javascript')
419 if (Event::handle('StartScriptElement', [$this, &$src, &$type])) {
420 $url = parse_url($src);
422 if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
424 // XXX: this seems like a big assumption
426 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
427 $src = common_path($src, GNUsocial::isHTTPS()) . '?version=' . GNUSOCIAL_VERSION;
429 if (GNUsocial::isHTTPS()) {
430 $server = common_config('javascript', 'sslserver');
432 if (empty($server)) {
433 if (is_string(common_config('site', 'sslserver')) &&
434 mb_strlen(common_config('site', 'sslserver')) > 0) {
435 $server = common_config('site', 'sslserver');
436 } elseif (common_config('site', 'server')) {
437 $server = common_config('site', 'server');
439 $path = common_config('site', 'path') . '/js/';
441 $path = common_config('javascript', 'sslpath');
443 $path = common_config('javascript', 'path');
449 $path = common_config('javascript', 'path');
452 $path = common_config('site', 'path') . '/js/';
455 $server = common_config('javascript', 'server');
457 if (empty($server)) {
458 $server = common_config('site', 'server');
464 if ($path[strlen($path) - 1] != '/') {
468 if ($path[0] != '/') {
472 $src = $protocol . '://' . $server . $path . $src . '?version=' . GNUSOCIAL_VERSION;
483 Event::handle('EndScriptElement', [$this, $src, $type]);
490 * @param string $src relative path within the theme directory, or an absolute path
491 * @param string $theme 'theme' that contains the stylesheet
492 * @param string media 'media' attribute of the tag
496 public function cssLink($src, $theme = null, $media = null)
498 if (Event::handle('StartCssLinkElement', [$this, &$src, &$theme, &$media])) {
499 $url = parse_url($src);
500 if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
501 if (file_exists(Theme::file($src, $theme))) {
502 $src = Theme::path($src, $theme);
504 $src = common_path($src, GNUsocial::isHTTPS());
506 $src .= '?version=' . GNUSOCIAL_VERSION;
508 $this->element('link', ['rel' => 'stylesheet',
509 'type' => 'text/css',
512 Event::handle('EndCssLinkElement', [$this, $src, $theme, $media]);
517 * output a style (almost always css) tag with inline
520 * @param string $code code to put in the style tag
521 * @param string $type 'type' attribute value of the tag
522 * @param string $media 'media' attribute value of the tag
527 public function style($code, $type = 'text/css', $media = null)
529 if (Event::handle('StartStyleElement', [$this, &$code, &$type, &$media])) {
530 $this->elementStart('style', ['type' => $type, 'media' => $media]);
532 $this->elementEnd('style');
533 Event::handle('EndStyleElement', [$this, $code, $type, $media]);
538 * output an HTML textarea and associated elements
540 * @param string $id element ID, must be unique on page
541 * @param string $label text of label for the element
542 * @param string $content content of the textarea, default none
543 * @param string $instructions instructions for valid input
544 * @param string $name name of textarea; if null, $id will be used
545 * @param int $cols number of columns
546 * @param int $rows number of rows
547 * @param bool $required HTML5 required attribute (exclude when false)
552 public function textarea(
556 $instructions = null,
563 $this->element('label', ['for' => $id], $label);
569 $attrs['name'] = is_null($name) ? $id : $name;
572 $attrs['cols'] = $cols;
575 $attrs['rows'] = $rows;
578 if (!empty($required)) {
579 $attrs['required'] = 'required';
588 $this->element('p', 'form_guide', $instructions);
593 * Internal script to autofocus the given element on page onload.
595 * @param string $id element ID, must refer to an existing element
600 public function autofocus($id)
603 ' $(document).ready(function() {' .
604 ' var el = $("#' . $id . '");' .
605 ' if (el.length) { el.focus(); }' .
611 * output a script (almost always javascript) tag with inline
614 * @param string $code code to put in the script tag
615 * @param string $type 'type' attribute value of the tag
620 public function inlineScript($code, $type = 'text/javascript')
622 if (Event::handle('StartInlineScriptElement', [$this, &$code, &$type])) {
623 $this->elementStart('script', ['type' => $type]);
624 if ($type == 'text/javascript') {
625 $this->raw('/*<![CDATA[*/ '); // XHTML compat
628 if ($type == 'text/javascript') {
629 $this->raw(' /*]]>*/'); // XHTML compat
631 $this->elementEnd('script');
632 Event::handle('EndInlineScriptElement', [$this, $code, $type]);