]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/htmloutputter.php
Merge branch '1.0.x' into schema-x
[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         $this->extraHeaders();
112         if (preg_match("/.*\/.*xml/", $type)) {
113             // Required for XML documents
114             $this->xw->startDocument('1.0', 'UTF-8');
115         }
116         $this->xw->writeDTD('html',
117                             '-//W3C//DTD XHTML 1.0 Strict//EN',
118                             'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
119
120         $language = $this->getLanguage();
121
122         $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
123                                           'xml:lang' => $language,
124                                           'lang' => $language));
125     }
126
127     function getLanguage()
128     {
129         // FIXME: correct language for interface
130         return common_language();
131     }
132
133     /**
134     *  Ends an HTML document
135     *
136     *  @return void
137     */
138     function endHTML()
139     {
140         $this->elementEnd('html');
141         $this->endXML();
142     }
143
144     /**
145     *  To specify additional HTTP headers for the action
146     *
147     *  @return void
148     */
149     function extraHeaders()
150     {
151         // Needs to be overloaded
152     }
153
154     /**
155      * Output an HTML text input element
156      *
157      * Despite the name, it is specifically for outputting a
158      * text input element, not other <input> elements. It outputs
159      * a cluster of elements, including a <label> and an associated
160      * instructions span.
161      *
162      * @param string $id           element ID, must be unique on page
163      * @param string $label        text of label for the element
164      * @param string $value        value of the element, default null
165      * @param string $instructions instructions for valid input
166      *
167      * @todo add a $name parameter
168      * @todo add a $maxLength parameter
169      * @todo add a $size parameter
170      *
171      * @return void
172      */
173
174     function input($id, $label, $value=null, $instructions=null)
175     {
176         $this->element('label', array('for' => $id), $label);
177         $attrs = array('name' => $id,
178                        'type' => 'text',
179                        'id' => $id);
180         if (!is_null($value)) { // value can be 0 or ''
181             $attrs['value'] = $value;
182         }
183         $this->element('input', $attrs);
184         if ($instructions) {
185             $this->element('p', 'form_guide', $instructions);
186         }
187     }
188
189     /**
190      * output an HTML checkbox and associated elements
191      *
192      * Note that the value is default 'true' (the string), which can
193      * be used by Action::boolean()
194      *
195      * @param string $id           element ID, must be unique on page
196      * @param string $label        text of label for the element
197      * @param string $checked      if the box is checked, default false
198      * @param string $instructions instructions for valid input
199      * @param string $value        value of the checkbox, default 'true'
200      * @param string $disabled     show the checkbox disabled, default false
201      *
202      * @return void
203      *
204      * @todo add a $name parameter
205      */
206
207     function checkbox($id, $label, $checked=false, $instructions=null,
208                       $value='true', $disabled=false)
209     {
210         $attrs = array('name' => $id,
211                        'type' => 'checkbox',
212                        'class' => 'checkbox',
213                        'id' => $id);
214         if ($value) {
215             $attrs['value'] = $value;
216         }
217         if ($checked) {
218             $attrs['checked'] = 'checked';
219         }
220         if ($disabled) {
221             $attrs['disabled'] = 'true';
222         }
223         $this->element('input', $attrs);
224         $this->text(' ');
225         $this->element('label', array('class' => 'checkbox',
226                                       'for' => $id),
227                        $label);
228         $this->text(' ');
229         if ($instructions) {
230             $this->element('p', 'form_guide', $instructions);
231         }
232     }
233
234     /**
235      * output an HTML combobox/select and associated elements
236      *
237      * $content is an array of key-value pairs for the dropdown, where
238      * the key is the option value attribute and the value is the option
239      * text. (Careful on the overuse of 'value' here.)
240      *
241      * @param string $id           element ID, must be unique on page
242      * @param string $label        text of label for the element
243      * @param array  $content      options array, value => text
244      * @param string $instructions instructions for valid input
245      * @param string $blank_select whether to have a blank entry, default false
246      * @param string $selected     selected value, default null
247      *
248      * @return void
249      *
250      * @todo add a $name parameter
251      */
252
253     function dropdown($id, $label, $content, $instructions=null,
254                       $blank_select=false, $selected=null)
255     {
256         $this->element('label', array('for' => $id), $label);
257         $this->elementStart('select', array('id' => $id, 'name' => $id));
258         if ($blank_select) {
259             $this->element('option', array('value' => ''));
260         }
261         foreach ($content as $value => $option) {
262             if ($value == $selected) {
263                 $this->element('option', array('value' => $value,
264                                                'selected' => 'selected'),
265                                $option);
266             } else {
267                 $this->element('option', array('value' => $value), $option);
268             }
269         }
270         $this->elementEnd('select');
271         if ($instructions) {
272             $this->element('p', 'form_guide', $instructions);
273         }
274     }
275
276     /**
277      * output an HTML hidden element
278      *
279      * $id is re-used as name
280      *
281      * @param string $id    element ID, must be unique on page
282      * @param string $value hidden element value, default null
283      * @param string $name  name, if different than ID
284      *
285      * @return void
286      */
287
288     function hidden($id, $value, $name=null)
289     {
290         $this->element('input', array('name' => ($name) ? $name : $id,
291                                       'type' => 'hidden',
292                                       'id' => $id,
293                                       'value' => $value));
294     }
295
296     /**
297      * output an HTML password input and associated elements
298      *
299      * @param string $id           element ID, must be unique on page
300      * @param string $label        text of label for the element
301      * @param string $instructions instructions for valid input
302      *
303      * @return void
304      *
305      * @todo add a $name parameter
306      */
307
308     function password($id, $label, $instructions=null)
309     {
310         $this->element('label', array('for' => $id), $label);
311         $attrs = array('name' => $id,
312                        'type' => 'password',
313                        'class' => 'password',
314                        'id' => $id);
315         $this->element('input', $attrs);
316         if ($instructions) {
317             $this->element('p', 'form_guide', $instructions);
318         }
319     }
320
321     /**
322      * output an HTML submit input and associated elements
323      *
324      * @param string $id    element ID, must be unique on page
325      * @param string $label text of the button
326      * @param string $cls   class of the button, default 'submit'
327      * @param string $name  name, if different than ID
328      * @param string $title  title text for the submit button
329      *
330      * @return void
331      *
332      * @todo add a $name parameter
333      */
334
335     function submit($id, $label, $cls='submit', $name=null, $title=null)
336     {
337         $this->element('input', array('type' => 'submit',
338                                       'id' => $id,
339                                       'name' => ($name) ? $name : $id,
340                                       'class' => $cls,
341                                       'value' => $label,
342                                       'title' => $title));
343     }
344
345     /**
346      * output a script (almost always javascript) tag
347      *
348      * @param string $src          relative or absolute script path
349      * @param string $type         'type' attribute value of the tag
350      *
351      * @return void
352      */
353     function script($src, $type='text/javascript')
354     {
355         if (Event::handle('StartScriptElement', array($this,&$src,&$type))) {
356
357             $url = parse_url($src);
358
359             if (empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment'])) {
360
361                 // XXX: this seems like a big assumption
362
363                 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
364
365                     $src = common_path($src, StatusNet::isHTTPS()) . '?version=' . STATUSNET_VERSION;
366
367                 } else {
368
369                     if (StatusNet::isHTTPS()) {
370
371                         $sslserver = common_config('javascript', 'sslserver');
372
373                         if (empty($sslserver)) {
374                             if (is_string(common_config('site', 'sslserver')) &&
375                                 mb_strlen(common_config('site', 'sslserver')) > 0) {
376                                 $server = common_config('site', 'sslserver');
377                             } else if (common_config('site', 'server')) {
378                                 $server = common_config('site', 'server');
379                             }
380                             $path   = common_config('site', 'path') . '/js/';
381                         } else {
382                             $server = $sslserver;
383                             $path   = common_config('javascript', 'sslpath');
384                             if (empty($path)) {
385                                 $path = common_config('javascript', 'path');
386                             }
387                         }
388
389                         $protocol = 'https';
390
391                     } else {
392
393                         $path = common_config('javascript', 'path');
394
395                         if (empty($path)) {
396                             $path = common_config('site', 'path') . '/js/';
397                         }
398
399                         $server = common_config('javascript', 'server');
400
401                         if (empty($server)) {
402                             $server = common_config('site', 'server');
403                         }
404
405                         $protocol = 'http';
406                     }
407
408                     if ($path[strlen($path)-1] != '/') {
409                         $path .= '/';
410                     }
411
412                     if ($path[0] != '/') {
413                         $path = '/'.$path;
414                     }
415
416                     $src = $protocol.'://'.$server.$path.$src . '?version=' . STATUSNET_VERSION;
417                 }
418             }
419
420             $this->element('script', array('type' => $type,
421                                            'src' => $src),
422                            ' ');
423
424             Event::handle('EndScriptElement', array($this,$src,$type));
425         }
426     }
427
428     /**
429      * output a script (almost always javascript) tag with inline
430      * code.
431      *
432      * @param string $code         code to put in the script tag
433      * @param string $type         'type' attribute value of the tag
434      *
435      * @return void
436      */
437
438     function inlineScript($code, $type='text/javascript')
439     {
440         if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
441             $this->elementStart('script', array('type' => $type));
442             if($type == 'text/javascript') {
443                 $this->raw('/*<![CDATA[*/ '); // XHTML compat
444             }
445             $this->raw($code);
446             if($type == 'text/javascript') {
447                 $this->raw(' /*]]>*/'); // XHTML compat
448             }
449             $this->elementEnd('script');
450             Event::handle('EndInlineScriptElement', array($this,$code,$type));
451         }
452     }
453
454     /**
455      * output a css link
456      *
457      * @param string $src     relative path within the theme directory, or an absolute path
458      * @param string $theme        'theme' that contains the stylesheet
459      * @param string media         'media' attribute of the tag
460      *
461      * @return void
462      */
463     function cssLink($src,$theme=null,$media=null)
464     {
465         if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
466             $url = parse_url($src);
467             if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
468             {
469                 if(file_exists(Theme::file($src,$theme))){
470                    $src = Theme::path($src, $theme);
471                 }else{
472                     $src = common_path($src, StatusNet::isHTTPS());
473                 }
474                 $src.= '?version=' . STATUSNET_VERSION;
475             }
476             $this->element('link', array('rel' => 'stylesheet',
477                                     'type' => 'text/css',
478                                     'href' => $src,
479                                     'media' => $media));
480             Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
481         }
482     }
483
484     /**
485      * output a style (almost always css) tag with inline
486      * code.
487      *
488      * @param string $code         code to put in the style tag
489      * @param string $type         'type' attribute value of the tag
490      * @param string $media        'media' attribute value of the tag
491      *
492      * @return void
493      */
494
495     function style($code, $type = 'text/css', $media = null)
496     {
497         if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
498             $this->elementStart('style', array('type' => $type, 'media' => $media));
499             $this->raw($code);
500             $this->elementEnd('style');
501             Event::handle('EndStyleElement', array($this,$code,$type,$media));
502         }
503     }
504
505     /**
506      * output an HTML textarea and associated elements
507      *
508      * @param string $id           element ID, must be unique on page
509      * @param string $label        text of label for the element
510      * @param string $content      content of the textarea, default none
511      * @param string $instructions instructions for valid input
512      *
513      * @return void
514      *
515      * @todo add a $name parameter
516      * @todo add a $cols parameter
517      * @todo add a $rows parameter
518      */
519
520     function textarea($id, $label, $content=null, $instructions=null)
521     {
522         $this->element('label', array('for' => $id), $label);
523         $this->element('textarea', array('rows' => 3,
524                                          'cols' => 40,
525                                          'name' => $id,
526                                          'id' => $id),
527                        ($content) ? $content : '');
528         if ($instructions) {
529             $this->element('p', 'form_guide', $instructions);
530         }
531     }
532
533     /**
534     * Internal script to autofocus the given element on page onload.
535     *
536     * @param string $id element ID, must refer to an existing element
537     *
538     * @return void
539     *
540     */
541     function autofocus($id)
542     {
543         $this->inlineScript(
544                    ' $(document).ready(function() {'.
545                    ' var el = $("#' . $id . '");'.
546                    ' if (el.length) { el.focus(); }'.
547                    ' });');
548     }
549 }