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