]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/htmloutputter.php
Misses this file to merge. I like the comments.
[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      * If $attrs['type'] does not exist it will be set to 'text'.
177      *
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
183      *                             be used
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)
186      *
187      * @todo add a $maxLength parameter
188      * @todo add a $size parameter
189      *
190      * @return void
191      */
192
193     function input($id, $label, $value=null, $instructions=null, $name=null, $required=false, array $attrs=array())
194     {
195         $this->element('label', array('for' => $id), $label);
196         if (!array_key_exists('type', $attrs)) {
197             $attrs['type'] = 'text';
198         }
199         $attrs['id'] = $id;
200         $attrs['name'] = is_null($name) ? $id : $name;
201         if (array_key_exists('placeholder', $attrs) && (is_null($attrs['placeholder']) || $attrs['placeholder'] === '')) {
202             // If placeholder is type-aware equal to '' or null, unset it as we apparently don't want a placeholder value
203             unset($attrs['placeholder']);
204         } else {
205             // If the placeholder is set use it, or use the label as fallback.
206             $attrs['placeholder'] = isset($attrs['placeholder']) ? $attrs['placeholder'] : $label;
207         }
208
209         if (!is_null($value)) { // value can be 0 or ''
210             $attrs['value'] = $value;
211         }
212         if (!empty($required)) {
213             $attrs['required'] = 'required';
214         }
215         $this->element('input', $attrs);
216         if ($instructions) {
217             $this->element('p', 'form_guide', $instructions);
218         }
219     }
220
221     /**
222      * output an HTML checkbox and associated elements
223      *
224      * Note that the value is default 'true' (the string), which can
225      * be used by Action::boolean()
226      *
227      * @param string $id           element ID, must be unique on page
228      * @param string $label        text of label for the element
229      * @param string $checked      if the box is checked, default false
230      * @param string $instructions instructions for valid input
231      * @param string $value        value of the checkbox, default 'true'
232      * @param string $disabled     show the checkbox disabled, default false
233      *
234      * @return void
235      *
236      * @todo add a $name parameter
237      */
238
239     function checkbox($id, $label, $checked=false, $instructions=null,
240                       $value='true', $disabled=false)
241     {
242         $attrs = array('name' => $id,
243                        'type' => 'checkbox',
244                        'class' => 'checkbox',
245                        'id' => $id);
246         if ($value) {
247             $attrs['value'] = $value;
248         }
249         if ($checked) {
250             $attrs['checked'] = 'checked';
251         }
252         if ($disabled) {
253             $attrs['disabled'] = 'true';
254         }
255         $this->element('input', $attrs);
256         $this->text(' ');
257         $this->element('label', array('class' => 'checkbox',
258                                       'for' => $id),
259                        $label);
260         $this->text(' ');
261         if ($instructions) {
262             $this->element('p', 'form_guide', $instructions);
263         }
264     }
265
266     /**
267      * output an HTML combobox/select and associated elements
268      *
269      * $content is an array of key-value pairs for the dropdown, where
270      * the key is the option value attribute and the value is the option
271      * text. (Careful on the overuse of 'value' here.)
272      *
273      * @param string $id           element ID, must be unique on page
274      * @param string $label        text of label for the element
275      * @param array  $content      options array, value => text
276      * @param string $instructions instructions for valid input
277      * @param string $blank_select whether to have a blank entry, default false
278      * @param string $selected     selected value, default null
279      *
280      * @return void
281      *
282      * @todo add a $name parameter
283      */
284
285     function dropdown($id, $label, $content, $instructions=null,
286                       $blank_select=false, $selected=null)
287     {
288         $this->element('label', array('for' => $id), $label);
289         $this->elementStart('select', array('id' => $id, 'name' => $id));
290         if ($blank_select) {
291             $this->element('option', array('value' => ''));
292         }
293         foreach ($content as $value => $option) {
294             if ($value == $selected) {
295                 $this->element('option', array('value' => $value,
296                                                'selected' => 'selected'),
297                                $option);
298             } else {
299                 $this->element('option', array('value' => $value), $option);
300             }
301         }
302         $this->elementEnd('select');
303         if ($instructions) {
304             $this->element('p', 'form_guide', $instructions);
305         }
306     }
307
308     /**
309      * output an HTML hidden element
310      *
311      * $id is re-used as name
312      *
313      * @param string $id    element ID, must be unique on page
314      * @param string $value hidden element value, default null
315      * @param string $name  name, if different than ID
316      *
317      * @return void
318      */
319
320     function hidden($id, $value, $name=null)
321     {
322         $this->element('input', array('name' => $name ?: $id,
323                                       'type' => 'hidden',
324                                       'id' => $id,
325                                       'value' => $value));
326     }
327
328     /**
329      * output an HTML password input and associated elements
330      *
331      * @param string $id           element ID, must be unique on page
332      * @param string $label        text of label for the element
333      * @param string $instructions instructions for valid input
334      *
335      * @return void
336      *
337      * @todo add a $name parameter
338      */
339
340     function password($id, $label, $instructions=null)
341     {
342         $this->element('label', array('for' => $id), $label);
343         $attrs = array('name' => $id,
344                        'type' => 'password',
345                        'class' => 'password',
346                        'id' => $id);
347         $this->element('input', $attrs);
348         if ($instructions) {
349             $this->element('p', 'form_guide', $instructions);
350         }
351     }
352
353     /**
354      * output an HTML submit input and associated elements
355      *
356      * @param string $id    element ID, must be unique on page
357      * @param string $label text of the button
358      * @param string $cls   class of the button, default 'submit'
359      * @param string $name  name, if different than ID
360      * @param string $title  title text for the submit button
361      *
362      * @return void
363      *
364      * @todo add a $name parameter
365      */
366
367     function submit($id, $label, $cls='submit', $name=null, $title=null)
368     {
369         $this->element('input', array('type' => 'submit',
370                                       'id' => $id,
371                                       'name'  => $name ?: $id,
372                                       'class' => $cls,
373                                       'value' => $label,
374                                       'title' => $title));
375     }
376
377     /**
378      * output a script (almost always javascript) tag
379      *
380      * @param string $src          relative or absolute script path
381      * @param string $type         'type' attribute value of the tag
382      *
383      * @return void
384      */
385     function script($src, $type='text/javascript')
386     {
387         if (Event::handle('StartScriptElement', array($this,&$src,&$type))) {
388
389             $url = parse_url($src);
390
391             if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
392
393                 // XXX: this seems like a big assumption
394
395                 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
396
397                     $src = common_path($src, GNUsocial::isHTTPS()) . '?version=' . GNUSOCIAL_VERSION;
398
399                 } else {
400
401                     if (GNUsocial::isHTTPS()) {
402
403                         $sslserver = common_config('javascript', 'sslserver');
404
405                         if (empty($sslserver)) {
406                             if (is_string(common_config('site', 'sslserver')) &&
407                                 mb_strlen(common_config('site', 'sslserver')) > 0) {
408                                 $server = common_config('site', 'sslserver');
409                             } else if (common_config('site', 'server')) {
410                                 $server = common_config('site', 'server');
411                             }
412                             $path   = common_config('site', 'path') . '/js/';
413                         } else {
414                             $server = $sslserver;
415                             $path   = common_config('javascript', 'sslpath');
416                             if (empty($path)) {
417                                 $path = common_config('javascript', 'path');
418                             }
419                         }
420
421                         $protocol = 'https';
422
423                     } else {
424
425                         $path = common_config('javascript', 'path');
426
427                         if (empty($path)) {
428                             $path = common_config('site', 'path') . '/js/';
429                         }
430
431                         $server = common_config('javascript', 'server');
432
433                         if (empty($server)) {
434                             $server = common_config('site', 'server');
435                         }
436
437                         $protocol = 'http';
438                     }
439
440                     if ($path[strlen($path)-1] != '/') {
441                         $path .= '/';
442                     }
443
444                     if ($path[0] != '/') {
445                         $path = '/'.$path;
446                     }
447
448                     $src = $protocol.'://'.$server.$path.$src . '?version=' . GNUSOCIAL_VERSION;
449                 }
450             }
451
452             $this->element('script', array('type' => $type,
453                                            'src' => $src),
454                            ' ');
455
456             Event::handle('EndScriptElement', array($this,$src,$type));
457         }
458     }
459
460     /**
461      * output a script (almost always javascript) tag with inline
462      * code.
463      *
464      * @param string $code         code to put in the script tag
465      * @param string $type         'type' attribute value of the tag
466      *
467      * @return void
468      */
469
470     function inlineScript($code, $type='text/javascript')
471     {
472         if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
473             $this->elementStart('script', array('type' => $type));
474             if($type == 'text/javascript') {
475                 $this->raw('/*<![CDATA[*/ '); // XHTML compat
476             }
477             $this->raw($code);
478             if($type == 'text/javascript') {
479                 $this->raw(' /*]]>*/'); // XHTML compat
480             }
481             $this->elementEnd('script');
482             Event::handle('EndInlineScriptElement', array($this,$code,$type));
483         }
484     }
485
486     /**
487      * output a css link
488      *
489      * @param string $src     relative path within the theme directory, or an absolute path
490      * @param string $theme        'theme' that contains the stylesheet
491      * @param string media         'media' attribute of the tag
492      *
493      * @return void
494      */
495     function cssLink($src,$theme=null,$media=null)
496     {
497         if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
498             $url = parse_url($src);
499             if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
500             {
501                 if(file_exists(Theme::file($src,$theme))){
502                    $src = Theme::path($src, $theme);
503                 }else{
504                     $src = common_path($src, GNUsocial::isHTTPS());
505                 }
506                 $src.= '?version=' . GNUSOCIAL_VERSION;
507             }
508             $this->element('link', array('rel' => 'stylesheet',
509                                     'type' => 'text/css',
510                                     'href' => $src,
511                                     'media' => $media));
512             Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
513         }
514     }
515
516     /**
517      * output a style (almost always css) tag with inline
518      * code.
519      *
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
523      *
524      * @return void
525      */
526
527     function style($code, $type = 'text/css', $media = null)
528     {
529         if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
530             $this->elementStart('style', array('type' => $type, 'media' => $media));
531             $this->raw($code);
532             $this->elementEnd('style');
533             Event::handle('EndStyleElement', array($this,$code,$type,$media));
534         }
535     }
536
537     /**
538      * output an HTML textarea and associated elements
539      *
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)
548      *
549      * @return void
550      */
551
552     function textarea(
553         $id,
554         $label,
555         $content      = null,
556         $instructions = null,
557         $name         = null,
558         $cols         = null,
559         $rows         = null,
560         $required     = false
561     ) {
562         $this->element('label', array('for' => $id), $label);
563         $attrs = array(
564             'rows' => 3,
565             'cols' => 40,
566             'id' => $id
567         );
568         $attrs['name'] = is_null($name) ? $id : $name;
569
570         if ($cols != null) {
571             $attrs['cols'] = $cols;
572
573         }
574         if ($rows != null) {
575             $attrs['rows'] = $rows;
576         }
577         $this->element(
578             'textarea',
579             $attrs,
580             is_null($content) ? '' : $content
581         );
582         if ($instructions) {
583             $this->element('p', 'form_guide', $instructions);
584         }
585     }
586
587    /**
588     * Internal script to autofocus the given element on page onload.
589     *
590     * @param string $id element ID, must refer to an existing element
591     *
592     * @return void
593     *
594     */
595     function autofocus($id)
596     {
597         $this->inlineScript(
598                    ' $(document).ready(function() {'.
599                    ' var el = $("#' . $id . '");'.
600                    ' if (el.length) { el.focus(); }'.
601                    ' });');
602     }
603 }