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