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