]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/htmloutputter.php
Introduced common_location_shared() to check if location sharing is always,
[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('GNUSOCIAL')) { exit(1); }
32
33 // Can include XHTML options but these are too fragile in practice.
34 define('PAGE_TYPE_PREFS', 'text/html');
35
36 /**
37  * Low-level generator for HTML
38  *
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.
43  *
44  * @category Output
45  * @package  StatusNet
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/
50  *
51  * @see      Action
52  * @see      XMLOutputter
53  */
54
55 class HTMLOutputter extends XMLOutputter
56 {
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');
60     /**
61      * Constructor
62      *
63      * Just wraps the XMLOutputter constructor.
64      *
65      * @param string  $output URI to output to, default = stdout
66      * @param boolean $indent Whether to indent output, default true
67      */
68
69     function __construct($output='php://output', $indent=null)
70     {
71         parent::__construct($output, $indent);
72     }
73
74     /**
75      * Start an HTML document
76      *
77      * If $type isn't specified, will attempt to do content negotiation.
78      *
79      * Attempts to do content negotiation for language, also.
80      *
81      * @param string $type MIME type to use; default is to do negotation.
82      *
83      * @todo extract content negotiation code to an HTTP module or class.
84      *
85      * @return void
86      */
87
88     function startHTML($type=null)
89     {
90         if (!$type) {
91             $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
92               $_SERVER['HTTP_ACCEPT'] : null;
93
94             // XXX: allow content negotiation for RDF, RSS, or XRDS
95
96             $cp = common_accept_to_prefs($httpaccept);
97             $sp = common_accept_to_prefs(PAGE_TYPE_PREFS);
98
99             $type = common_negotiate_type($cp, $sp);
100
101             if (!$type) {
102                 // TRANS: Client exception 406
103                 throw new ClientException(_('This page is not available in a '.
104                                             'media type you accept'), 406);
105             }
106         }
107
108         header('Content-Type: '.$type);
109
110         // Output anti-framing headers to prevent clickjacking (respected by newer
111         // browsers).
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
115         }
116
117         $this->extraHeaders();
118         if (preg_match("/.*\/.*xml/", $type)) {
119             // Required for XML documents
120             $this->startXML();
121         }
122
123         $this->writeDTD();
124
125         $language = $this->getLanguage();
126
127         $attrs = array(
128             'xmlns' => 'http://www.w3.org/1999/xhtml',
129             'xml:lang' => $language,
130             'lang' => $language
131         );
132
133         if (Event::handle('StartHtmlElement', array($this, &$attrs))) {
134             $this->elementStart('html', $attrs);
135             Event::handle('EndHtmlElement', array($this, &$attrs));
136         }
137     }
138
139     public function setDTD($doctype, $spec, $uri)
140     {
141         $this->DTD = array('doctype' => $doctype, 'spec' => $spec, 'uri' => $uri);
142     }
143
144     protected function writeDTD()
145     {
146         $this->xw->writeDTD($this->DTD['doctype'],
147                             $this->DTD['spec'],
148                             $this->DTD['uri']);
149     }
150
151     function getLanguage()
152     {
153         // FIXME: correct language for interface
154         return common_language();
155     }
156
157     /**
158     *  Ends an HTML document
159     *
160     *  @return void
161     */
162     function endHTML()
163     {
164         $this->elementEnd('html');
165         $this->endXML();
166     }
167
168     /**
169     *  To specify additional HTTP headers for the action
170     *
171     *  @return void
172     */
173     function extraHeaders()
174     {
175         // Needs to be overloaded
176     }
177
178     /**
179      * Output an HTML text input element
180      *
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
184      * instructions span.
185      *
186      * If $attrs['type'] does not exist it will be set to 'text'.
187      *
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
193      *                             be used
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)
196      *
197      * @todo add a $maxLength parameter
198      * @todo add a $size parameter
199      *
200      * @return void
201      */
202
203     function input($id, $label, $value=null, $instructions=null, $name=null, $required=false, array $attrs=array())
204     {
205         $this->element('label', array('for' => $id), $label);
206         if (!array_key_exists('type', $attrs)) {
207             $attrs['type'] = 'text';
208         }
209         $attrs['id'] = $id;
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']);
214         } else {
215             // If the placeholder is set use it, or use the label as fallback.
216             $attrs['placeholder'] = isset($attrs['placeholder']) ? $attrs['placeholder'] : $label;
217         }
218
219         if (!is_null($value)) { // value can be 0 or ''
220             $attrs['value'] = $value;
221         }
222         if (!empty($required)) {
223             $attrs['required'] = 'required';
224         }
225         $this->element('input', $attrs);
226         if ($instructions) {
227             $this->element('p', 'form_guide', $instructions);
228         }
229     }
230
231     /**
232      * output an HTML checkbox and associated elements
233      *
234      * Note that the value is default 'true' (the string), which can
235      * be used by Action::boolean()
236      *
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
243      *
244      * @return void
245      *
246      * @todo add a $name parameter
247      */
248
249     function checkbox($id, $label, $checked=false, $instructions=null,
250                       $value='true', $disabled=false)
251     {
252         $attrs = array('name' => $id,
253                        'type' => 'checkbox',
254                        'class' => 'checkbox',
255                        'id' => $id);
256         if ($value) {
257             $attrs['value'] = $value;
258         }
259         if ($checked) {
260             $attrs['checked'] = 'checked';
261         }
262         if ($disabled) {
263             $attrs['disabled'] = 'true';
264         }
265         $this->element('input', $attrs);
266         $this->text(' ');
267         $this->element('label', array('class' => 'checkbox',
268                                       'for' => $id),
269                        $label);
270         $this->text(' ');
271         if ($instructions) {
272             $this->element('p', 'form_guide', $instructions);
273         }
274     }
275
276     /**
277      * output an HTML combobox/select and associated elements
278      *
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.)
282      *
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
289      *
290      * @return void
291      *
292      * @todo add a $name parameter
293      */
294
295     function dropdown($id, $label, $content, $instructions=null,
296                       $blank_select=false, $selected=null)
297     {
298         $this->element('label', array('for' => $id), $label);
299         $this->elementStart('select', array('id' => $id, 'name' => $id));
300         if ($blank_select) {
301             $this->element('option', array('value' => ''));
302         }
303         foreach ($content as $value => $option) {
304             if ($value == $selected) {
305                 $this->element('option', array('value' => $value,
306                                                'selected' => 'selected'),
307                                $option);
308             } else {
309                 $this->element('option', array('value' => $value), $option);
310             }
311         }
312         $this->elementEnd('select');
313         if ($instructions) {
314             $this->element('p', 'form_guide', $instructions);
315         }
316     }
317
318     /**
319      * output an HTML hidden element
320      *
321      * $id is re-used as name
322      *
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
326      *
327      * @return void
328      */
329
330     function hidden($id, $value, $name=null)
331     {
332         $this->element('input', array('name' => $name ?: $id,
333                                       'type' => 'hidden',
334                                       'id' => $id,
335                                       'value' => $value));
336     }
337
338     /**
339      * output an HTML password input and associated elements
340      *
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
344      *
345      * @return void
346      *
347      * @todo add a $name parameter
348      */
349
350     function password($id, $label, $instructions=null)
351     {
352         $this->element('label', array('for' => $id), $label);
353         $attrs = array('name' => $id,
354                        'type' => 'password',
355                        'class' => 'password',
356                        'id' => $id);
357         $this->element('input', $attrs);
358         if ($instructions) {
359             $this->element('p', 'form_guide', $instructions);
360         }
361     }
362
363     /**
364      * output an HTML submit input and associated elements
365      *
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
371      *
372      * @return void
373      *
374      * @todo add a $name parameter
375      */
376
377     function submit($id, $label, $cls='submit', $name=null, $title=null)
378     {
379         $this->element('input', array('type' => 'submit',
380                                       'id' => $id,
381                                       'name'  => $name ?: $id,
382                                       'class' => $cls,
383                                       'value' => $label,
384                                       'title' => $title));
385     }
386
387     /**
388      * output a script (almost always javascript) tag
389      *
390      * @param string $src          relative or absolute script path
391      * @param string $type         'type' attribute value of the tag
392      *
393      * @return void
394      */
395     function script($src, $type='text/javascript')
396     {
397         if (Event::handle('StartScriptElement', array($this,&$src,&$type))) {
398
399             $url = parse_url($src);
400
401             if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
402
403                 // XXX: this seems like a big assumption
404
405                 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
406
407                     $src = common_path($src, GNUsocial::isHTTPS()) . '?version=' . GNUSOCIAL_VERSION;
408
409                 } else {
410
411                     if (GNUsocial::isHTTPS()) {
412
413                         $sslserver = common_config('javascript', 'sslserver');
414
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');
421                             }
422                             $path   = common_config('site', 'path') . '/js/';
423                         } else {
424                             $server = $sslserver;
425                             $path   = common_config('javascript', 'sslpath');
426                             if (empty($path)) {
427                                 $path = common_config('javascript', 'path');
428                             }
429                         }
430
431                         $protocol = 'https';
432
433                     } else {
434
435                         $path = common_config('javascript', 'path');
436
437                         if (empty($path)) {
438                             $path = common_config('site', 'path') . '/js/';
439                         }
440
441                         $server = common_config('javascript', 'server');
442
443                         if (empty($server)) {
444                             $server = common_config('site', 'server');
445                         }
446
447                         $protocol = 'http';
448                     }
449
450                     if ($path[strlen($path)-1] != '/') {
451                         $path .= '/';
452                     }
453
454                     if ($path[0] != '/') {
455                         $path = '/'.$path;
456                     }
457
458                     $src = $protocol.'://'.$server.$path.$src . '?version=' . GNUSOCIAL_VERSION;
459                 }
460             }
461
462             $this->element('script', array('type' => $type,
463                                            'src' => $src),
464                            ' ');
465
466             Event::handle('EndScriptElement', array($this,$src,$type));
467         }
468     }
469
470     /**
471      * output a script (almost always javascript) tag with inline
472      * code.
473      *
474      * @param string $code         code to put in the script tag
475      * @param string $type         'type' attribute value of the tag
476      *
477      * @return void
478      */
479
480     function inlineScript($code, $type='text/javascript')
481     {
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
486             }
487             $this->raw($code);
488             if($type == 'text/javascript') {
489                 $this->raw(' /*]]>*/'); // XHTML compat
490             }
491             $this->elementEnd('script');
492             Event::handle('EndInlineScriptElement', array($this,$code,$type));
493         }
494     }
495
496     /**
497      * output a css link
498      *
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
502      *
503      * @return void
504      */
505     function cssLink($src,$theme=null,$media=null)
506     {
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']))
510             {
511                 if(file_exists(Theme::file($src,$theme))){
512                    $src = Theme::path($src, $theme);
513                 }else{
514                     $src = common_path($src, GNUsocial::isHTTPS());
515                 }
516                 $src.= '?version=' . GNUSOCIAL_VERSION;
517             }
518             $this->element('link', array('rel' => 'stylesheet',
519                                     'type' => 'text/css',
520                                     'href' => $src,
521                                     'media' => $media));
522             Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
523         }
524     }
525
526     /**
527      * output a style (almost always css) tag with inline
528      * code.
529      *
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
533      *
534      * @return void
535      */
536
537     function style($code, $type = 'text/css', $media = null)
538     {
539         if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
540             $this->elementStart('style', array('type' => $type, 'media' => $media));
541             $this->raw($code);
542             $this->elementEnd('style');
543             Event::handle('EndStyleElement', array($this,$code,$type,$media));
544         }
545     }
546
547     /**
548      * output an HTML textarea and associated elements
549      *
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)
558      *
559      * @return void
560      */
561
562     function textarea(
563         $id,
564         $label,
565         $content      = null,
566         $instructions = null,
567         $name         = null,
568         $cols         = null,
569         $rows         = null,
570         $required     = false
571     ) {
572         $this->element('label', array('for' => $id), $label);
573         $attrs = array(
574             'rows' => 3,
575             'cols' => 40,
576             'id' => $id
577         );
578         $attrs['name'] = is_null($name) ? $id : $name;
579
580         if ($cols != null) {
581             $attrs['cols'] = $cols;
582
583         }
584         if ($rows != null) {
585             $attrs['rows'] = $rows;
586         }
587         $this->element(
588             'textarea',
589             $attrs,
590             is_null($content) ? '' : $content
591         );
592         if ($instructions) {
593             $this->element('p', 'form_guide', $instructions);
594         }
595     }
596
597    /**
598     * Internal script to autofocus the given element on page onload.
599     *
600     * @param string $id element ID, must refer to an existing element
601     *
602     * @return void
603     *
604     */
605     function autofocus($id)
606     {
607         $this->inlineScript(
608                    ' $(document).ready(function() {'.
609                    ' var el = $("#' . $id . '");'.
610                    ' if (el.length) { el.focus(); }'.
611                    ' });');
612     }
613 }