]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/htmloutputter.php
Improved type-hint for following methods:
[quix0rs-gnu-social.git] / lib / htmloutputter.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Low-level generator for HTML
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Output
23  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/xmloutputter.php';
36
37 // Can include XHTML options but these are too fragile in practice.
38 define('PAGE_TYPE_PREFS', 'text/html');
39
40 /**
41  * Low-level generator for HTML
42  *
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.
47  *
48  * @category Output
49  * @package  StatusNet
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/
54  *
55  * @see      Action
56  * @see      XMLOutputter
57  */
58
59 class HTMLOutputter extends XMLOutputter
60 {
61     /**
62      * Constructor
63      *
64      * Just wraps the XMLOutputter constructor.
65      *
66      * @param string  $output URI to output to, default = stdout
67      * @param boolean $indent Whether to indent output, default true
68      */
69
70     function __construct($output='php://output', $indent=null)
71     {
72         parent::__construct($output, $indent);
73     }
74
75     /**
76      * Start an HTML document
77      *
78      * If $type isn't specified, will attempt to do content negotiation.
79      *
80      * Attempts to do content negotiation for language, also.
81      *
82      * @param string $type MIME type to use; default is to do negotation.
83      *
84      * @todo extract content negotiation code to an HTTP module or class.
85      *
86      * @return void
87      */
88
89     function startHTML($type=null)
90     {
91         if (!$type) {
92             $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
93               $_SERVER['HTTP_ACCEPT'] : null;
94
95             // XXX: allow content negotiation for RDF, RSS, or XRDS
96
97             $cp = common_accept_to_prefs($httpaccept);
98             $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
99
100             $type = common_negotiate_type($cp, $sp);
101
102             if (!$type) {
103                 // TRANS: Client exception 406
104                 throw new ClientException(_('This page is not available in a '.
105                                             'media type you accept'), 406);
106             }
107         }
108
109         header('Content-Type: '.$type);
110
111         // Output anti-framing headers to prevent clickjacking (respected by newer
112         // browsers).
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
116         }
117
118         $this->extraHeaders();
119         if (preg_match("/.*\/.*xml/", $type)) {
120             // Required for XML documents
121             $this->startXML();
122         }
123         $this->xw->writeDTD('html',
124                             '-//W3C//DTD XHTML 1.0 Strict//EN',
125                             'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
126
127         $language = $this->getLanguage();
128
129         $attrs = array(
130             'xmlns' => 'http://www.w3.org/1999/xhtml',
131             'xml:lang' => $language,
132             'lang' => $language
133         );
134
135         if (Event::handle('StartHtmlElement', array($this, &$attrs))) {
136             $this->elementStart('html', $attrs);
137             Event::handle('EndHtmlElement', array($this, &$attrs));
138         }
139     }
140
141     function getLanguage()
142     {
143         // FIXME: correct language for interface
144         return common_language();
145     }
146
147     /**
148     *  Ends an HTML document
149     *
150     *  @return void
151     */
152     function endHTML()
153     {
154         $this->elementEnd('html');
155         $this->endXML();
156     }
157
158     /**
159     *  To specify additional HTTP headers for the action
160     *
161     *  @return void
162     */
163     function extraHeaders()
164     {
165         // Needs to be overloaded
166     }
167
168     /**
169      * Output an HTML text input element
170      *
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
174      * instructions span.
175      *
176      * @param string $id           element ID, must be unique on page
177      * @param string $label        text of label for the element
178      * @param string $value        value of the element, default null
179      * @param string $instructions instructions for valid input
180      * @param string $name         name of the element; if null, the id will
181      *                             be used
182      * @param bool   $required     HTML5 required attribute (exclude when false)
183      *
184      * @todo add a $maxLength parameter
185      * @todo add a $size parameter
186      *
187      * @return void
188      */
189
190     function input($id, $label, $value=null, $instructions=null, $name=null, $required=false)
191     {
192         $this->element('label', array('for' => $id), $label);
193         $attrs = array('type' => 'text',
194                        'id'   => $id);
195         $attrs['name'] = is_null($name) ? $id : $name;
196         if (!is_null($value)) { // value can be 0 or ''
197             $attrs['value'] = $value;
198         }
199         if (!empty($required)) {
200             $attrs['required'] = 'required';
201         }
202         $this->element('input', $attrs);
203         if ($instructions) {
204             $this->element('p', 'form_guide', $instructions);
205         }
206     }
207
208     /**
209      * output an HTML checkbox and associated elements
210      *
211      * Note that the value is default 'true' (the string), which can
212      * be used by Action::boolean()
213      *
214      * @param string $id           element ID, must be unique on page
215      * @param string $label        text of label for the element
216      * @param string $checked      if the box is checked, default false
217      * @param string $instructions instructions for valid input
218      * @param string $value        value of the checkbox, default 'true'
219      * @param string $disabled     show the checkbox disabled, default false
220      *
221      * @return void
222      *
223      * @todo add a $name parameter
224      */
225
226     function checkbox($id, $label, $checked=false, $instructions=null,
227                       $value='true', $disabled=false)
228     {
229         $attrs = array('name' => $id,
230                        'type' => 'checkbox',
231                        'class' => 'checkbox',
232                        'id' => $id);
233         if ($value) {
234             $attrs['value'] = $value;
235         }
236         if ($checked) {
237             $attrs['checked'] = 'checked';
238         }
239         if ($disabled) {
240             $attrs['disabled'] = 'true';
241         }
242         $this->element('input', $attrs);
243         $this->text(' ');
244         $this->element('label', array('class' => 'checkbox',
245                                       'for' => $id),
246                        $label);
247         $this->text(' ');
248         if ($instructions) {
249             $this->element('p', 'form_guide', $instructions);
250         }
251     }
252
253     /**
254      * output an HTML combobox/select and associated elements
255      *
256      * $content is an array of key-value pairs for the dropdown, where
257      * the key is the option value attribute and the value is the option
258      * text. (Careful on the overuse of 'value' here.)
259      *
260      * @param string $id           element ID, must be unique on page
261      * @param string $label        text of label for the element
262      * @param array  $content      options array, value => text
263      * @param string $instructions instructions for valid input
264      * @param string $blank_select whether to have a blank entry, default false
265      * @param string $selected     selected value, default null
266      *
267      * @return void
268      *
269      * @todo add a $name parameter
270      */
271
272     function dropdown($id, $label, $content, $instructions=null,
273                       $blank_select=false, $selected=null)
274     {
275         $this->element('label', array('for' => $id), $label);
276         $this->elementStart('select', array('id' => $id, 'name' => $id));
277         if ($blank_select) {
278             $this->element('option', array('value' => ''));
279         }
280         foreach ($content as $value => $option) {
281             if ($value == $selected) {
282                 $this->element('option', array('value' => $value,
283                                                'selected' => 'selected'),
284                                $option);
285             } else {
286                 $this->element('option', array('value' => $value), $option);
287             }
288         }
289         $this->elementEnd('select');
290         if ($instructions) {
291             $this->element('p', 'form_guide', $instructions);
292         }
293     }
294
295     /**
296      * output an HTML hidden element
297      *
298      * $id is re-used as name
299      *
300      * @param string $id    element ID, must be unique on page
301      * @param string $value hidden element value, default null
302      * @param string $name  name, if different than ID
303      *
304      * @return void
305      */
306
307     function hidden($id, $value, $name=null)
308     {
309         $this->element('input', array('name' => $name ?: $id,
310                                       'type' => 'hidden',
311                                       'id' => $id,
312                                       'value' => $value));
313     }
314
315     /**
316      * output an HTML password input and associated elements
317      *
318      * @param string $id           element ID, must be unique on page
319      * @param string $label        text of label for the element
320      * @param string $instructions instructions for valid input
321      *
322      * @return void
323      *
324      * @todo add a $name parameter
325      */
326
327     function password($id, $label, $instructions=null)
328     {
329         $this->element('label', array('for' => $id), $label);
330         $attrs = array('name' => $id,
331                        'type' => 'password',
332                        'class' => 'password',
333                        'id' => $id);
334         $this->element('input', $attrs);
335         if ($instructions) {
336             $this->element('p', 'form_guide', $instructions);
337         }
338     }
339
340     /**
341      * output an HTML submit input and associated elements
342      *
343      * @param string $id    element ID, must be unique on page
344      * @param string $label text of the button
345      * @param string $cls   class of the button, default 'submit'
346      * @param string $name  name, if different than ID
347      * @param string $title  title text for the submit button
348      *
349      * @return void
350      *
351      * @todo add a $name parameter
352      */
353
354     function submit($id, $label, $cls='submit', $name=null, $title=null)
355     {
356         $this->element('input', array('type' => 'submit',
357                                       'id' => $id,
358                                       'name'  => $name ?: $id,
359                                       'class' => $cls,
360                                       'value' => $label,
361                                       'title' => $title));
362     }
363
364     /**
365      * output a script (almost always javascript) tag
366      *
367      * @param string $src          relative or absolute script path
368      * @param string $type         'type' attribute value of the tag
369      *
370      * @return void
371      */
372     function script($src, $type='text/javascript')
373     {
374         if (Event::handle('StartScriptElement', array($this,&$src,&$type))) {
375
376             $url = parse_url($src);
377
378             if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
379
380                 // XXX: this seems like a big assumption
381
382                 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
383
384                     $src = common_path($src, StatusNet::isHTTPS()) . '?version=' . GNUSOCIAL_VERSION;
385
386                 } else {
387
388                     if (StatusNet::isHTTPS()) {
389
390                         $sslserver = common_config('javascript', 'sslserver');
391
392                         if (empty($sslserver)) {
393                             if (is_string(common_config('site', 'sslserver')) &&
394                                 mb_strlen(common_config('site', 'sslserver')) > 0) {
395                                 $server = common_config('site', 'sslserver');
396                             } else if (common_config('site', 'server')) {
397                                 $server = common_config('site', 'server');
398                             }
399                             $path   = common_config('site', 'path') . '/js/';
400                         } else {
401                             $server = $sslserver;
402                             $path   = common_config('javascript', 'sslpath');
403                             if (empty($path)) {
404                                 $path = common_config('javascript', 'path');
405                             }
406                         }
407
408                         $protocol = 'https';
409
410                     } else {
411
412                         $path = common_config('javascript', 'path');
413
414                         if (empty($path)) {
415                             $path = common_config('site', 'path') . '/js/';
416                         }
417
418                         $server = common_config('javascript', 'server');
419
420                         if (empty($server)) {
421                             $server = common_config('site', 'server');
422                         }
423
424                         $protocol = 'http';
425                     }
426
427                     if ($path[strlen($path)-1] != '/') {
428                         $path .= '/';
429                     }
430
431                     if ($path[0] != '/') {
432                         $path = '/'.$path;
433                     }
434
435                     $src = $protocol.'://'.$server.$path.$src . '?version=' . GNUSOCIAL_VERSION;
436                 }
437             }
438
439             $this->element('script', array('type' => $type,
440                                            'src' => $src),
441                            ' ');
442
443             Event::handle('EndScriptElement', array($this,$src,$type));
444         }
445     }
446
447     /**
448      * output a script (almost always javascript) tag with inline
449      * code.
450      *
451      * @param string $code         code to put in the script tag
452      * @param string $type         'type' attribute value of the tag
453      *
454      * @return void
455      */
456
457     function inlineScript($code, $type='text/javascript')
458     {
459         if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
460             $this->elementStart('script', array('type' => $type));
461             if($type == 'text/javascript') {
462                 $this->raw('/*<![CDATA[*/ '); // XHTML compat
463             }
464             $this->raw($code);
465             if($type == 'text/javascript') {
466                 $this->raw(' /*]]>*/'); // XHTML compat
467             }
468             $this->elementEnd('script');
469             Event::handle('EndInlineScriptElement', array($this,$code,$type));
470         }
471     }
472
473     /**
474      * output a css link
475      *
476      * @param string $src     relative path within the theme directory, or an absolute path
477      * @param string $theme        'theme' that contains the stylesheet
478      * @param string media         'media' attribute of the tag
479      *
480      * @return void
481      */
482     function cssLink($src,$theme=null,$media=null)
483     {
484         if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
485             $url = parse_url($src);
486             if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
487             {
488                 if(file_exists(Theme::file($src,$theme))){
489                    $src = Theme::path($src, $theme);
490                 }else{
491                     $src = common_path($src, StatusNet::isHTTPS());
492                 }
493                 $src.= '?version=' . GNUSOCIAL_VERSION;
494             }
495             $this->element('link', array('rel' => 'stylesheet',
496                                     'type' => 'text/css',
497                                     'href' => $src,
498                                     'media' => $media));
499             Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
500         }
501     }
502
503     /**
504      * output a style (almost always css) tag with inline
505      * code.
506      *
507      * @param string $code         code to put in the style tag
508      * @param string $type         'type' attribute value of the tag
509      * @param string $media        'media' attribute value of the tag
510      *
511      * @return void
512      */
513
514     function style($code, $type = 'text/css', $media = null)
515     {
516         if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
517             $this->elementStart('style', array('type' => $type, 'media' => $media));
518             $this->raw($code);
519             $this->elementEnd('style');
520             Event::handle('EndStyleElement', array($this,$code,$type,$media));
521         }
522     }
523
524     /**
525      * output an HTML textarea and associated elements
526      *
527      * @param string $id           element ID, must be unique on page
528      * @param string $label        text of label for the element
529      * @param string $content      content of the textarea, default none
530      * @param string $instructions instructions for valid input
531      * @param string $name         name of textarea; if null, $id will be used
532      * @param int    $cols         number of columns
533      * @param int    $rows         number of rows
534      * @param bool   $required     HTML5 required attribute (exclude when false)
535      *
536      * @return void
537      */
538
539     function textarea(
540         $id,
541         $label,
542         $content      = null,
543         $instructions = null,
544         $name         = null,
545         $cols         = null,
546         $rows         = null,
547         $required     = false
548     ) {
549         $this->element('label', array('for' => $id), $label);
550         $attrs = array(
551             'rows' => 3,
552             'cols' => 40,
553             'id' => $id
554         );
555         $attrs['name'] = is_null($name) ? $id : $name;
556
557         if ($cols != null) {
558             $attrs['cols'] = $cols;
559
560         }
561         if ($rows != null) {
562             $attrs['rows'] = $rows;
563         }
564         $this->element(
565             'textarea',
566             $attrs,
567             is_null($content) ? '' : $content
568         );
569         if ($instructions) {
570             $this->element('p', 'form_guide', $instructions);
571         }
572     }
573
574    /**
575     * Internal script to autofocus the given element on page onload.
576     *
577     * @param string $id element ID, must refer to an existing element
578     *
579     * @return void
580     *
581     */
582     function autofocus($id)
583     {
584         $this->inlineScript(
585                    ' $(document).ready(function() {'.
586                    ' var el = $("#' . $id . '");'.
587                    ' if (el.length) { el.focus(); }'.
588                    ' });');
589     }
590 }