action.php 47.3 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
Evan Prodromou's avatar
Evan Prodromou committed
4
 *
5 6 7 8 9
 * Base class for all actions (~views)
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
Evan Prodromou's avatar
Evan Prodromou committed
10 11 12
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Evan Prodromou's avatar
Evan Prodromou committed
13
 *
Evan Prodromou's avatar
Evan Prodromou committed
14 15 16 17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
Evan Prodromou's avatar
Evan Prodromou committed
18
 *
Evan Prodromou's avatar
Evan Prodromou committed
19 20
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 22
 *
 * @category  Action
23
 * @package   StatusNet
24 25
 * @author    Evan Prodromou <evan@status.net>
 * @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/
Evan Prodromou's avatar
Evan Prodromou committed
29
 */
Evan Prodromou's avatar
Evan Prodromou committed
30

31
if (!defined('STATUSNET') && !defined('LACONICA')) {
32 33 34
    exit(1);
}

Evan Prodromou's avatar
Evan Prodromou committed
35
require_once INSTALLDIR.'/lib/noticeform.php';
Evan Prodromou's avatar
Evan Prodromou committed
36 37
require_once INSTALLDIR.'/lib/htmloutputter.php';

38 39 40 41 42 43 44 45 46 47
/**
 * Base class for all actions
 *
 * This is the base class for all actions in the package. An action is
 * more or less a "view" in an MVC framework.
 *
 * Actions are responsible for extracting and validating parameters; using
 * model classes to read and write to the database; and doing ouput.
 *
 * @category Output
48
 * @package  StatusNet
49 50
 * @author   Evan Prodromou <evan@status.net>
 * @author   Sarven Capadisli <csarven@status.net>
51
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52
 * @link     http://status.net/
53 54 55 56 57
 *
 * @see      HTMLOutputter
 */
class Action extends HTMLOutputter // lawsuit
{
58 59
    var $args;

60 61 62 63 64 65 66 67 68 69 70
    /**
     * Constructor
     *
     * Just wraps the HTMLOutputter constructor.
     *
     * @param string  $output URI to output to, default = stdout
     * @param boolean $indent Whether to indent output, default true
     *
     * @see XMLOutputter::__construct
     * @see HTMLOutputter::__construct
     */
71
    function __construct($output='php://output', $indent=null)
72
    {
73
        parent::__construct($output, $indent);
74 75
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
76 77 78 79 80 81 82
    /**
     * For initializing members of the class.
     *
     * @param array $argarray misc. arguments
     *
     * @return boolean true
     */
83 84 85
    function prepare($argarray)
    {
        $this->args =& common_copy_args($argarray);
86 87 88 89 90

        if ($this->boolean('ajax')) {
            StatusNet::setAjax(true);
        }

91 92 93
        return true;
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
94 95 96 97 98
    /**
     * Show page, a template method.
     *
     * @return nothing
     */
99 100
    function showPage()
    {
101 102
        if (Event::handle('StartShowHTML', array($this))) {
            $this->startHTML();
Evan Prodromou's avatar
Evan Prodromou committed
103
            $this->flush();
104 105
            Event::handle('EndShowHTML', array($this));
        }
Brian's avatar
Brian committed
106
        if (Event::handle('StartShowHead', array($this))) {
107
            $this->showHead();
Evan Prodromou's avatar
Evan Prodromou committed
108
            $this->flush();
Brian's avatar
Brian committed
109 110 111
            Event::handle('EndShowHead', array($this));
        }
        if (Event::handle('StartShowBody', array($this))) {
112
            $this->showBody();
Brian's avatar
Brian committed
113 114 115
            Event::handle('EndShowBody', array($this));
        }
        if (Event::handle('StartEndHTML', array($this))) {
116
            $this->endHTML();
Brian's avatar
Brian committed
117 118
            Event::handle('EndEndHTML', array($this));
        }
119 120
    }

121 122 123 124 125 126 127 128 129 130 131 132 133
    function endHTML()
    {
        global $_startTime;

        if (isset($_startTime)) {
            $endTime = microtime(true);
            $diff = round(($endTime - $_startTime) * 1000);
            $this->raw("<!-- ${diff}ms -->");
        }

        return parent::endHTML();
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
134 135 136 137 138
    /**
     * Show head, a template method.
     *
     * @return nothing
     */
139 140 141
    function showHead()
    {
        // XXX: attributes (profile?)
142
        $this->elementStart('head');
143
        if (Event::handle('StartShowHeadElements', array($this))) {
144 145 146 147
            if (Event::handle('StartShowHeadTitle', array($this))) {
                $this->showTitle();
                Event::handle('EndShowHeadTitle', array($this));
            }
148 149 150 151 152 153 154 155
            $this->showShortcutIcon();
            $this->showStylesheets();
            $this->showOpenSearch();
            $this->showFeeds();
            $this->showDescription();
            $this->extraHead();
            Event::handle('EndShowHeadElements', array($this));
        }
156
        $this->elementEnd('head');
157 158
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
159 160 161 162 163
    /**
     * Show title, a template method.
     *
     * @return nothing
     */
164 165 166
    function showTitle()
    {
        $this->element('title', null,
167
                       // TRANS: Page title. %1$s is the title, %2$s is the site name.
168
                       sprintf(_('%1$s - %2$s'),
169 170 171 172
                               $this->title(),
                               common_config('site', 'name')));
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
173 174 175 176 177 178 179
    /**
     * Returns the page title
     *
     * SHOULD overload
     *
     * @return string page title
     */
180 181 182

    function title()
    {
183
        // TRANS: Page title for a page without a title set.
184
        return _('Untitled page');
185 186
    }

187 188 189 190 191 192 193 194 195
    /**
     * Show themed shortcut icon
     *
     * @return nothing
     */
    function showShortcutIcon()
    {
        if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/favicon.ico')) {
            $this->element('link', array('rel' => 'shortcut icon',
196
                                         'href' => Theme::path('favicon.ico')));
197
        } else {
198
            // favicon.ico should be HTTPS if the rest of the page is
199
            $this->element('link', array('rel' => 'shortcut icon',
200
                                         'href' => common_path('favicon.ico', StatusNet::isHTTPS())));
201
        }
202 203 204 205

        if (common_config('site', 'mobile')) {
            if (is_readable(INSTALLDIR . '/theme/' . common_config('site', 'theme') . '/apple-touch-icon.png')) {
                $this->element('link', array('rel' => 'apple-touch-icon',
206
                                             'href' => Theme::path('apple-touch-icon.png')));
207 208 209 210 211
            } else {
                $this->element('link', array('rel' => 'apple-touch-icon',
                                             'href' => common_path('apple-touch-icon.png')));
            }
        }
212 213
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
214 215 216 217 218
    /**
     * Show stylesheets
     *
     * @return nothing
     */
219 220
    function showStylesheets()
    {
221
        if (Event::handle('StartShowStyles', array($this))) {
222

223 224 225 226
            // Use old name for StatusNet for compatibility on events

            if (Event::handle('StartShowStatusNetStyles', array($this)) &&
                Event::handle('StartShowLaconicaStyles', array($this))) {
227
                $this->primaryCssLink(null, 'screen, projection, tv, print');
228
                Event::handle('EndShowStatusNetStyles', array($this));
229
                Event::handle('EndShowLaconicaStyles', array($this));
230
            }
231

232 233
            $this->cssLink(common_path('js/css/smoothness/jquery-ui.css'));

234 235
            if (Event::handle('StartShowUAStyles', array($this))) {
                $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
236
                               'href="'.Theme::path('css/ie.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
237
                foreach (array(6,7) as $ver) {
238
                    if (file_exists(Theme::file('css/ie'.$ver.'.css', 'base'))) {
239 240
                        // Yes, IE people should be put in jail.
                        $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
241
                                       'href="'.Theme::path('css/ie'.$ver.'.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
242 243
                    }
                }
244 245
                if (file_exists(Theme::file('css/ie.css'))) {
                    $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
246
                               'href="'.Theme::path('css/ie.css', null).'?version='.STATUSNET_VERSION.'" /><![endif]');
247
                }
248 249
                Event::handle('EndShowUAStyles', array($this));
            }
250

251
            Event::handle('EndShowStyles', array($this));
252

253 254 255 256 257 258 259 260 261
            if (common_config('custom_css', 'enabled')) {
                $css = common_config('custom_css', 'css');
                if (Event::handle('StartShowCustomCss', array($this, &$css))) {
                    if (trim($css) != '') {
                        $this->style($css);
                    }
                    Event::handle('EndShowCustomCss', array($this));
                }
            }
262 263 264
        }
    }

265 266
    function primaryCssLink($mainTheme=null, $media=null)
    {
267 268 269 270 271 272 273 274
        $theme = new Theme($mainTheme);

        // Some themes may have external stylesheets, such as using the
        // Google Font APIs to load webfonts.
        foreach ($theme->getExternals() as $url) {
            $this->cssLink($url, $mainTheme, $media);
        }

275 276 277 278 279 280 281
        // If the currently-selected theme has dependencies on other themes,
        // we'll need to load their display.css files as well in order.
        $baseThemes = $theme->getDeps();
        foreach ($baseThemes as $baseTheme) {
            $this->cssLink('css/display.css', $baseTheme, $media);
        }
        $this->cssLink('css/display.css', $mainTheme, $media);
282 283 284

        // Additional styles for RTL languages
        if (is_rtl(common_language())) {
285 286 287
            if (file_exists(Theme::file('css/rtl.css'))) {
                $this->cssLink('css/rtl.css', $mainTheme, $media);
            }
288
        }
289 290
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
291 292 293 294 295
    /**
     * Show javascript headers
     *
     * @return nothing
     */
296 297
    function showScripts()
    {
298 299
        if (Event::handle('StartShowScripts', array($this))) {
            if (Event::handle('StartShowJQueryScripts', array($this))) {
300 301 302
                if (common_config('site', 'minify')) {
                    $this->script('jquery.min.js');
                    $this->script('jquery.form.min.js');
303
                    $this->script('jquery-ui.min.js');
304 305 306
                    $this->script('jquery.cookie.min.js');
                    $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/json2.min.js').'"); }');
                    $this->script('jquery.joverlay.min.js');
307
                    $this->script('jquery.infieldlabel.min.js');
308 309 310
                } else {
                    $this->script('jquery.js');
                    $this->script('jquery.form.js');
311
                    $this->script('jquery-ui.min.js');
312 313 314
                    $this->script('jquery.cookie.js');
                    $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/json2.js').'"); }');
                    $this->script('jquery.joverlay.js');
315
                    $this->script('jquery.infieldlabel.js');
316
                }
Shashi Gowda's avatar
Shashi Gowda committed
317

318 319
                Event::handle('EndShowJQueryScripts', array($this));
            }
320 321
            if (Event::handle('StartShowStatusNetScripts', array($this)) &&
                Event::handle('StartShowLaconicaScripts', array($this))) {
322 323 324 325 326 327 328
                if (common_config('site', 'minify')) {
                    $this->script('util.min.js');
                } else {
                    $this->script('util.js');
                    $this->script('xbImportNode.js');
                    $this->script('geometa.js');
                }
329 330 331 332 333 334
                // This route isn't available in single-user mode.
                // Not sure why, but it causes errors here.
                if (!common_config('singleuser', 'enabled')) {
                    $this->inlineScript('var _peopletagAC = "' .
                                        common_local_url('peopletagautocomplete') . '";');
                }
335
                $this->showScriptMessages();
336 337 338 339
                // Anti-framing code to avoid clickjacking attacks in older browsers.
                // This will show a blank page if the page is being framed, which is
                // consistent with the behavior of the 'X-Frame-Options: SAMEORIGIN'
                // header, which prevents framing in newer browser.
340
                if (common_config('javascript', 'bustframes')) {
341
                    $this->inlineScript('if (window.top !== window.self) { document.write = ""; window.top.location = window.self.location; setTimeout(function () { document.body.innerHTML = ""; }, 1); window.self.onload = function () { document.body.innerHTML = ""; }; }');
342
                }
343
                Event::handle('EndShowStatusNetScripts', array($this));
344 345 346 347
                Event::handle('EndShowLaconicaScripts', array($this));
            }
            Event::handle('EndShowScripts', array($this));
        }
348 349
    }

350 351 352 353 354 355 356 357 358 359
    /**
     * Exports a map of localized text strings to JavaScript code.
     *
     * Plugins can add to what's exported by hooking the StartScriptMessages or EndScriptMessages
     * events and appending to the array. Try to avoid adding strings that won't be used, as
     * they'll be added to HTML output.
     */
    function showScriptMessages()
    {
        $messages = array();
360

361 362 363 364 365 366
        if (Event::handle('StartScriptMessages', array($this, &$messages))) {
            // Common messages needed for timeline views etc...

            // TRANS: Localized tooltip for '...' expansion button on overlong remote messages.
            $messages['showmore_tooltip'] = _m('TOOLTIP', 'Show more');

367
            // TRANS: Inline reply form submit button: submits a reply comment.
368
            $messages['reply_submit'] = _m('BUTTON', 'Reply');
369

370
            // TRANS: Placeholder text for inline reply form. Clicking in this box will turn it into a mini notice form.
371
            $messages['reply_placeholder'] = _m('Write a reply...');
372

373
            $messages = array_merge($messages, $this->getScriptMessages());
374

375
	    Event::handle('EndScriptMessages', array($this, &$messages));
376
        }
377

378
        if (!empty($messages)) {
379 380
            $this->inlineScript('SN.messages=' . json_encode($messages));
        }
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
        return $messages;
    }

    /**
     * If the action will need localizable text strings, export them here like so:
     *
     * return array('pool_deepend' => _('Deep end'),
     *              'pool_shallow' => _('Shallow end'));
     *
     * The exported map will be available via SN.msg() to JS code:
     *
     *   $('#pool').html('<div class="deepend"></div><div class="shallow"></div>');
     *   $('#pool .deepend').text(SN.msg('pool_deepend'));
     *   $('#pool .shallow').text(SN.msg('pool_shallow'));
     *
     * Exports a map of localized text strings to JavaScript code.
     *
     * Plugins can add to what's exported on any action by hooking the StartScriptMessages or
     * EndScriptMessages events and appending to the array. Try to avoid adding strings that won't
     * be used, as they'll be added to HTML output.
     */
    function getScriptMessages()
    {
        return array();
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
408 409 410 411 412
    /**
     * Show OpenSearch headers
     *
     * @return nothing
     */
413 414
    function showOpenSearch()
    {
Robin Millette's avatar
phpdocs  
Robin Millette committed
415 416
        $this->element('link', array('rel' => 'search',
                                     'type' => 'application/opensearchdescription+xml',
417 418
                                     'href' =>  common_local_url('opensearch', array('type' => 'people')),
                                     'title' => common_config('site', 'name').' People Search'));
419
        $this->element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
420 421 422 423
                                     'href' =>  common_local_url('opensearch', array('type' => 'notice')),
                                     'title' => common_config('site', 'name').' Notice Search'));
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
424 425 426 427 428 429 430
    /**
     * Show feed headers
     *
     * MAY overload
     *
     * @return nothing
     */
431 432
    function showFeeds()
    {
433 434 435 436 437 438 439 440 441 442
        $feeds = $this->getFeeds();

        if ($feeds) {
            foreach ($feeds as $feed) {
                $this->element('link', array('rel' => $feed->rel(),
                                             'href' => $feed->url,
                                             'type' => $feed->mimeType(),
                                             'title' => $feed->title));
            }
        }
443 444
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
445 446 447 448 449 450 451
    /**
     * Show description.
     *
     * SHOULD overload
     *
     * @return nothing
     */
452 453 454 455 456
    function showDescription()
    {
        // does nothing by default
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
457 458 459 460 461 462 463
    /**
     * Show extra stuff in <head>.
     *
     * MAY overload
     *
     * @return nothing
     */
464 465 466 467 468
    function extraHead()
    {
        // does nothing by default
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
469 470 471 472 473 474 475
    /**
     * Show body.
     *
     * Calls template methods
     *
     * @return nothing
     */
476 477
    function showBody()
    {
478
        $this->elementStart('body', (common_current_user()) ? array('id' => strtolower($this->trimmed('action')),
479
                                                                    'class' => 'user_in')
480
                            : array('id' => strtolower($this->trimmed('action'))));
481
        $this->elementStart('div', array('id' => 'wrap'));
482 483
        if (Event::handle('StartShowHeader', array($this))) {
            $this->showHeader();
Evan Prodromou's avatar
Evan Prodromou committed
484
            $this->flush();
485 486
            Event::handle('EndShowHeader', array($this));
        }
487
        $this->showCore();
Evan Prodromou's avatar
Evan Prodromou committed
488
        $this->flush();
489 490
        if (Event::handle('StartShowFooter', array($this))) {
            $this->showFooter();
Evan Prodromou's avatar
Evan Prodromou committed
491
            $this->flush();
492 493
            Event::handle('EndShowFooter', array($this));
        }
sarven's avatar
Minor  
sarven committed
494
        $this->elementEnd('div');
495
        $this->showScripts();
496
        $this->elementEnd('body');
497 498
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
499 500 501 502 503 504 505
    /**
     * Show header of the page.
     *
     * Calls template methods
     *
     * @return nothing
     */
506 507
    function showHeader()
    {
sarven's avatar
sarven committed
508
        $this->elementStart('div', array('id' => 'header'));
509 510
        $this->showLogo();
        $this->showPrimaryNav();
511 512 513 514 515
        if (Event::handle('StartShowSiteNotice', array($this))) {
            $this->showSiteNotice();

            Event::handle('EndShowSiteNotice', array($this));
        }
516

sarven's avatar
sarven committed
517
        $this->elementEnd('div');
518 519
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
520 521 522 523 524
    /**
     * Show configured logo.
     *
     * @return nothing
     */
525 526
    function showLogo()
    {
527
        $this->elementStart('address', array('id' => 'site_contact',
528
                                             'class' => 'vcard'));
529
        if (Event::handle('StartAddressData', array($this))) {
Evan Prodromou's avatar
Evan Prodromou committed
530
            if (common_config('singleuser', 'enabled')) {
531
                $user = User::singleUser();
Evan Prodromou's avatar
Evan Prodromou committed
532
                $url = common_local_url('showstream',
533
                                        array('nickname' => $user->nickname));
534 535 536
            } else if (common_logged_in()) {
                $cur = common_current_user();
                $url = common_local_url('all', array('nickname' => $cur->nickname));
Evan Prodromou's avatar
Evan Prodromou committed
537 538 539
            } else {
                $url = common_local_url('public');
            }
540

541
            $this->elementStart('a', array('class' => 'url home bookmark',
Evan Prodromou's avatar
Evan Prodromou committed
542
                                           'href' => $url));
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

            if (StatusNet::isHTTPS()) {
                $logoUrl = common_config('site', 'ssllogo');
                if (empty($logoUrl)) {
                    // if logo is an uploaded file, try to fall back to HTTPS file URL
                    $httpUrl = common_config('site', 'logo');
                    if (!empty($httpUrl)) {
                        $f = File::staticGet('url', $httpUrl);
                        if (!empty($f) && !empty($f->filename)) {
                            // this will handle the HTTPS case
                            $logoUrl = File::url($f->filename);
                        }
                    }
                }
            } else {
                $logoUrl = common_config('site', 'logo');
            }

            if (empty($logoUrl) && file_exists(Theme::file('logo.png'))) {
                // This should handle the HTTPS case internally
                $logoUrl = Theme::path('logo.png');
            }

            if (!empty($logoUrl)) {
567
                $this->element('img', array('class' => 'logo photo',
568
                                            'src' => $logoUrl,
569 570
                                            'alt' => common_config('site', 'name')));
            }
571

572
            $this->text(' ');
573 574
            $this->element('span', array('class' => 'fn org'), common_config('site', 'name'));
            $this->elementEnd('a');
575

576
            Event::handle('EndAddressData', array($this));
sarven's avatar
sarven committed
577
        }
578
        $this->elementEnd('address');
579 580
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
581 582 583 584 585
    /**
     * Show primary navigation.
     *
     * @return nothing
     */
586 587
    function showPrimaryNav()
    {
Evan Prodromou's avatar
Evan Prodromou committed
588
        $this->elementStart('div', array('id' => 'site_nav_global_primary'));
Evan Prodromou's avatar
Evan Prodromou committed
589 590 591 592 593 594 595 596

        $user = common_current_user();

        if (!empty($user) || !common_config('site', 'private')) {
            $form = new SearchForm($this);
            $form->show();
        }

597 598
        $pn = new PrimaryNav($this);
        $pn->show();
Evan Prodromou's avatar
Evan Prodromou committed
599
        $this->elementEnd('div');
600
    }
601

Robin Millette's avatar
phpdocs  
Robin Millette committed
602 603 604 605 606
    /**
     * Show site notice.
     *
     * @return nothing
     */
607 608
    function showSiteNotice()
    {
Robin Millette's avatar
phpdocs  
Robin Millette committed
609
        // Revist. Should probably do an hAtom pattern here
610 611
        $text = common_config('site', 'notice');
        if ($text) {
612
            $this->elementStart('div', array('id' => 'site_notice',
613
                                            'class' => 'system_notice'));
614
            $this->raw($text);
615
            $this->elementEnd('div');
616
        }
617
    }
618

Robin Millette's avatar
phpdocs  
Robin Millette committed
619 620 621 622 623 624 625
    /**
     * Show notice form.
     *
     * MAY overload if no notice form needed... or direct message box????
     *
     * @return nothing
     */
626 627
    function showNoticeForm()
    {
628 629
        // TRANS: Tab on the notice form.
        $tabs = array('status' => _m('TAB','Status'));
630 631 632 633 634 635 636 637

        $this->elementStart('div', 'input_forms');

        if (Event::handle('StartShowEntryForms', array(&$tabs))) {
            $this->elementStart('ul', array('class' => 'nav',
                                            'id' => 'input_form_nav'));

            foreach ($tabs as $tag => $title) {
Evan Prodromou's avatar
Evan Prodromou committed
638 639
                $attrs = array('id' => 'input_form_nav_'.$tag,
                               'class' => 'input_form_nav_tab');
640

641 642 643 644 645 646
                if ($tag == 'status') {
                    // We're actually showing the placeholder form,
                    // but we special-case the 'Status' tab as if
                    // it were a small version of it.
                    $attrs['class'] .= ' current';
                }
647 648 649
                $this->elementStart('li', $attrs);

                $this->element('a',
Evan Prodromou's avatar
Evan Prodromou committed
650
                               array('href' => 'javascript:SN.U.switchInputFormTab("'.$tag.'")'),
651 652 653 654 655 656
                               $title);
                $this->elementEnd('li');
            }

            $this->elementEnd('ul');

657 658 659 660 661 662 663
            $attrs = array('class' => 'input_form current',
                           'id' => 'input_form_placeholder');
            $this->elementStart('div', $attrs);
            $form = new NoticePlaceholderForm($this);
            $form->show();
            $this->elementEnd('div');

664 665 666 667 668 669 670 671 672 673
            foreach ($tabs as $tag => $title) {
                $attrs = array('class' => 'input_form',
                               'id' => 'input_form_'.$tag);

                $this->elementStart('div', $attrs);

                $form = null;

                if (Event::handle('StartMakeEntryForm', array($tag, $this, &$form))) {
                    if ($tag == 'status') {
674 675
                        $options = $this->noticeFormOptions();
                        $form = new NoticeForm($this, $options);
676 677 678 679 680 681 682
                    }
                    Event::handle('EndMakeEntryForm', array($tag, $this, $form));
                }

                if (!empty($form)) {
                    $form->show();
                }
Evan Prodromou's avatar
Evan Prodromou committed
683 684

                $this->elementEnd('div');
685 686
            }
        }
Evan Prodromou's avatar
Evan Prodromou committed
687 688

        $this->elementEnd('div');
689
    }
690

691 692 693 694 695
    function noticeFormOptions()
    {
        return array();
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
696 697 698 699 700 701 702
    /**
     * Show anonymous message.
     *
     * SHOULD overload
     *
     * @return nothing
     */
703 704
    function showAnonymousMessage()
    {
705
        // needs to be defined by the class
706
    }
707

Robin Millette's avatar
phpdocs  
Robin Millette committed
708 709 710 711 712 713 714
    /**
     * Show core.
     *
     * Shows local navigation, content block and aside.
     *
     * @return nothing
     */
715 716
    function showCore()
    {
sarven's avatar
sarven committed
717
        $this->elementStart('div', array('id' => 'core'));
718 719 720
        $this->elementStart('div', array('id' => 'aside_primary_wrapper'));
        $this->elementStart('div', array('id' => 'content_wrapper'));
        $this->elementStart('div', array('id' => 'site_nav_local_views_wrapper'));
Sarven Capadisli's avatar
Sarven Capadisli committed
721 722
        if (Event::handle('StartShowLocalNavBlock', array($this))) {
            $this->showLocalNavBlock();
Evan Prodromou's avatar
Evan Prodromou committed
723
            $this->flush();
Sarven Capadisli's avatar
Sarven Capadisli committed
724 725
            Event::handle('EndShowLocalNavBlock', array($this));
        }
726 727
        if (Event::handle('StartShowContentBlock', array($this))) {
            $this->showContentBlock();
Evan Prodromou's avatar
Evan Prodromou committed
728
            $this->flush();
729 730
            Event::handle('EndShowContentBlock', array($this));
        }
731 732
        if (Event::handle('StartShowAside', array($this))) {
            $this->showAside();
Evan Prodromou's avatar
Evan Prodromou committed
733
            $this->flush();
734 735
            Event::handle('EndShowAside', array($this));
        }
736
        $this->elementEnd('div');
737 738 739
        $this->elementEnd('div');
        $this->elementEnd('div');
        $this->elementEnd('div');
740 741
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
742 743 744 745 746
    /**
     * Show local navigation block.
     *
     * @return nothing
     */
747 748
    function showLocalNavBlock()
    {
749 750 751
        // Need to have this ID for CSS; I'm too lazy to add it to
        // all menus
        $this->elementStart('div', array('id' => 'site_nav_local_views'));
752
        // Cheat cheat cheat!
753
        $this->showLocalNav();
754
        $this->elementEnd('div');
755 756
    }

757 758 759 760 761 762 763 764 765 766 767 768 769
    /**
     * If there's a logged-in user, show a bit of login context
     *
     * @return nothing
     */
    function showProfileBlock()
    {
        if (common_logged_in()) {
            $block = new DefaultProfileBlock($this);
            $block->show();
        }
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
770 771 772 773 774 775 776
    /**
     * Show local navigation.
     *
     * SHOULD overload
     *
     * @return nothing
     */
777
    function showLocalNav()
778
    {
779 780
        $nav = new DefaultLocalNav($this);
        $nav->show();
781 782
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
783
    /**
784 785
     * Show menu for an object (group, profile)
     *
786 787 788
     * This block will only show if a subclass has overridden
     * the showObjectNav() method.
     *
789 790 791 792
     * @return nothing
     */
    function showObjectNavBlock()
    {
793 794 795 796 797 798 799 800 801 802 803
        $rmethod = new ReflectionMethod($this, 'showObjectNav');
        $dclass = $rmethod->getDeclaringClass()->getName();

        if ($dclass != 'Action') {
            // Need to have this ID for CSS; I'm too lazy to add it to
            // all menus
            $this->elementStart('div', array('id' => 'site_nav_object',
                                             'class' => 'section'));
            $this->showObjectNav();
            $this->elementEnd('div');
        }
804 805 806 807 808 809 810 811 812 813 814 815
    }

    /**
     * Show object navigation.
     *
     * If there are things to do with this object, show it here.
     *
     * @return nothing
     */
    function showObjectNav()
    {
        /* Nothing here. */
816 817
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
818 819 820 821 822
    /**
     * Show content block.
     *
     * @return nothing
     */
823 824
    function showContentBlock()
    {
825
        $this->elementStart('div', array('id' => 'content'));
826 827 828 829 830 831
        if (common_logged_in()) {
            if (Event::handle('StartShowNoticeForm', array($this))) {
                $this->showNoticeForm();
                Event::handle('EndShowNoticeForm', array($this));
            }
        }
832 833 834 835
        if (Event::handle('StartShowPageTitle', array($this))) {
            $this->showPageTitle();
            Event::handle('EndShowPageTitle', array($this));
        }
836
        $this->showPageNoticeBlock();
Evan Prodromou's avatar
Evan Prodromou committed
837 838
        $this->elementStart('div', array('id' => 'content_inner'));
        // show the actual content (forms, lists, whatever)
839
        $this->showContent();
sarven's avatar
sarven committed
840
        $this->elementEnd('div');
Evan Prodromou's avatar
Evan Prodromou committed
841
        $this->elementEnd('div');
842 843
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
844 845 846 847 848 849 850 851
    /**
     * Show page title.
     *
     * @return nothing
     */
    function showPageTitle()
    {
        $this->element('h1', null, $this->title());
852 853
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
854 855 856
    /**
     * Show page notice block.
     *
857 858 859 860 861 862
     * Only show the block if a subclassed action has overrided
     * Action::showPageNotice(), or an event handler is registered for
     * the StartShowPageNotice event, in which case we assume the
     * 'page_notice' definition list is desired.  This is to prevent
     * empty 'page_notice' definition lists from being output everywhere.
     *
Robin Millette's avatar
phpdocs  
Robin Millette committed
863 864
     * @return nothing
     */
865 866
    function showPageNoticeBlock()
    {
867 868 869 870 871
        $rmethod = new ReflectionMethod($this, 'showPageNotice');
        $dclass = $rmethod->getDeclaringClass()->getName();

        if ($dclass != 'Action' || Event::hasHandler('StartShowPageNotice')) {

872
            $this->elementStart('div', array('id' => 'page_notice',
873 874 875 876 877
                                            'class' => 'system_notice'));
            if (Event::handle('StartShowPageNotice', array($this))) {
                $this->showPageNotice();
                Event::handle('EndShowPageNotice', array($this));
            }
878
            $this->elementEnd('div');
879
        }
Robin Millette's avatar
phpdocs  
Robin Millette committed
880
    }
881

Robin Millette's avatar
phpdocs  
Robin Millette committed
882 883 884 885 886 887 888
    /**
     * Show page notice.
     *
     * SHOULD overload (unless there's not a notice)
     *
     * @return nothing
     */
889 890 891
    function showPageNotice()
    {
    }
892

Robin Millette's avatar
phpdocs  
Robin Millette committed
893 894 895 896 897 898 899
    /**
     * Show content.
     *
     * MUST overload (unless there's not a notice)
     *
     * @return nothing
     */
900 901 902 903
    function showContent()
    {
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
904 905 906 907 908
    /**
     * Show Aside.
     *
     * @return nothing
     */
909 910
    function showAside()
    {
sarven's avatar
sarven committed
911 912
        $this->elementStart('div', array('id' => 'aside_primary',
                                         'class' => 'aside'));
913
        $this->showProfileBlock();
Brion Vibber's avatar
Brion Vibber committed
914 915 916 917
        if (Event::handle('StartShowObjectNavBlock', array($this))) {
            $this->showObjectNavBlock();
            Event::handle('EndShowObjectNavBlock', array($this));
        }
918 919 920 921
        if (Event::handle('StartShowSections', array($this))) {
            $this->showSections();
            Event::handle('EndShowSections', array($this));
        }
922 923 924 925
        if (Event::handle('StartShowExportData', array($this))) {
            $this->showExportData();
            Event::handle('EndShowExportData', array($this));
        }
sarven's avatar
sarven committed
926
        $this->elementEnd('div');
927 928
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
929 930 931
    /**
     * Show export data feeds.
     *
932
     * @return void
Robin Millette's avatar
phpdocs  
Robin Millette committed
933
     */
934 935
    function showExportData()
    {
936 937 938 939 940
        $feeds = $this->getFeeds();
        if ($feeds) {
            $fl = new FeedList($this);
            $fl->show($feeds);
        }
941 942
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
943 944 945 946 947 948 949 950 951
    /**
     * Show sections.
     *
     * SHOULD overload
     *
     * @return nothing
     */
    function showSections()
    {
952 953 954
        // for each section, show it
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
955 956 957 958 959
    /**
     * Show footer.
     *
     * @return nothing
     */
960 961
    function showFooter()
    {
sarven's avatar
sarven committed
962
        $this->elementStart('div', array('id' => 'footer'));
963 964 965 966 967
        if (Event::handle('StartShowInsideFooter', array($this))) {
            $this->showSecondaryNav();
            $this->showLicenses();
            Event::handle('EndShowInsideFooter', array($this));
        }
sarven's avatar
sarven committed
968
        $this->elementEnd('div');
969 970
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
971 972 973 974 975
    /**
     * Show secondary navigation.
     *
     * @return nothing
     */
976 977
    function showSecondaryNav()
    {
978 979
        $sn = new SecondaryNav($this);
        $sn->show();
980 981
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
982 983 984 985 986
    /**
     * Show licenses.
     *
     * @return nothing
     */
987 988
    function showLicenses()
    {
989
        $this->showStatusNetLicense();
990 991 992
        $this->showContentLicense();
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
993
    /**
994
     * Show StatusNet license.
Robin Millette's avatar
phpdocs  
Robin Millette committed
995 996 997
     *
     * @return nothing
     */
998
    function showStatusNetLicense()
999 1000
    {
        if (common_config('site', 'broughtby')) {
1001
            // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is set.
1002 1003 1004
            // TRANS: Text between [] is a link description, text between () is the link itself.
            // TRANS: Make sure there is no whitespace between "]" and "(".
            // TRANS: "%%site.broughtby%%" is the value of the variable site.broughtby
1005
            $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%).');
1006
        } else {
1007 1008
            // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is not set.
            $instr = _('**%%site.name%%** is a microblogging service.');
1009
        }
1010
        $instr .= ' ';
1011
        // TRANS: Second sentence of the StatusNet site license. Mentions the StatusNet source code license.
1012 1013 1014
        // TRANS: Make sure there is no whitespace between "]" and "(".
        // TRANS: Text between [] is a link description, text between () is the link itself.
        // TRANS: %s is the version of StatusNet that is being used.
Evan Prodromou's avatar
Evan Prodromou committed
1015
        $instr .= sprintf(_('It runs the [StatusNet](http://status.net/) microblogging software, version %s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), STATUSNET_VERSION);
1016
        $output = common_markup_to_html($instr);
1017
        $this->raw($output);
1018 1019 1020
        // do it
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
1021 1022 1023 1024 1025
    /**
     * Show content license.
     *
     * @return nothing
     */
1026 1027
    function showContentLicense()
    {
1028
        if (Event::handle('StartShowContentLicense', array($this))) {
1029 1030
            switch (common_config('license', 'type')) {
            case 'private':
1031 1032
                // TRANS: Content license displayed when license is set to 'private'.
                // TRANS: %1$s is the site name.
1033 1034 1035 1036 1037
                $this->element('p', null, sprintf(_('Content and data of %1$s are private and confidential.'),
                                                  common_config('site', 'name')));
                // fall through
            case 'allrightsreserved':
                if (common_config('license', 'owner')) {
1038 1039
                    // TRANS: Content license displayed when license is set to 'allrightsreserved'.
                    // TRANS: %1$s is the copyright owner.
1040 1041 1042
                    $this->element('p', null, sprintf(_('Content and data copyright by %1$s. All rights reserved.'),
                                                      common_config('license', 'owner')));
                } else {
1043
                    // TRANS: Content license displayed when license is set to 'allrightsreserved' and no owner is set.
1044 1045 1046 1047 1048 1049
                    $this->element('p', null, _('Content and data copyright by contributors. All rights reserved.'));
                }
                break;
            case 'cc': // fall through
            default:
                $this->elementStart('p');
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067

                $image    = common_config('license', 'image');
                $sslimage = common_config('license', 'sslimage');

                if (StatusNet::isHTTPS()) {
                    if (!empty($sslimage)) {
                        $url = $sslimage;
                    } else if (preg_match('#^http://i.creativecommons.org/#', $image)) {
                        // CC support HTTPS on their images
                        $url = preg_replace('/^http/', 'https', $image);
                    } else {
                        // Better to show mixed content than no content
                        $url = $image;
                    }
                } else {
                    $url = $image;
                }

1068
                $this->element('img', array('id' => 'license_cc',
1069
                                            'src' => $url,
1070 1071 1072
                                            'alt' => common_config('license', 'title'),
                                            'width' => '80',
                                            'height' => '15'));
1073
                $this->text(' ');
1074 1075
                // TRANS: license message in footer.
                // TRANS: %1$s is the site name, %2$s is a link to the license URL, with a licence name set in configuration.
1076 1077 1078 1079 1080 1081 1082 1083 1084
                $notice = _('All %1$s content and data are available under the %2$s license.');
                $link = "<a class=\"license\" rel=\"external license\" href=\"" .
                        htmlspecialchars(common_config('license', 'url')) .
                        "\">" .
                        htmlspecialchars(common_config('license', 'title')) .
                        "</a>";
                $this->raw(sprintf(htmlspecialchars($notice),
                                   htmlspecialchars(common_config('site', 'name')),
                                   $link));
1085 1086 1087 1088
                $this->elementEnd('p');
                break;
            }

1089 1090
            Event::handle('EndShowContentLicense', array($this));
        }
1091 1092
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
1093 1094 1095 1096 1097 1098 1099
    /**
     * Return last modified, if applicable.
     *
     * MAY override
     *
     * @return string last modified http header
     */
1100
    function lastModified()
1101
    {
Robin Millette's avatar
phpdocs  
Robin Millette committed
1102 1103
        // For comparison with If-Last-Modified
        // If not applicable, return null
1104 1105 1106
        return null;
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
1107 1108 1109 1110 1111 1112 1113
    /**
     * Return etag, if applicable.
     *
     * MAY override
     *
     * @return string etag http header
     */
1114 1115 1116 1117 1118
    function etag()
    {
        return null;
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
1119 1120 1121 1122 1123
    /**
     * Return true if read only.
     *
     * MAY override
     *
1124 1125
     * @param array $args other arguments
     *
Robin Millette's avatar
phpdocs  
Robin Millette committed
1126 1127
     * @return boolean is read only action?
     */
Evan Prodromou's avatar
Evan Prodromou committed
1128
    function isReadOnly($args)
1129 1130 1131 1132
    {
        return false;
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
1133 1134 1135 1136 1137 1138 1139 1140
    /**
     * Returns query argument or default value if not found
     *
     * @param string $key requested argument
     * @param string $def default value to return if $key is not provided
     *
     * @return boolean is read only action?
     */
1141 1142 1143 1144 1145 1146 1147 1148 1149
    function arg($key, $def=null)
    {
        if (array_key_exists($key, $this->args)) {
            return $this->args[$key];
        } else {
            return $def;
        }
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
1150 1151 1152 1153 1154 1155 1156 1157
    /**
     * Returns trimmed query argument or default value if not found
     *
     * @param string $key requested argument
     * @param string $def default value to return if $key is not provided
     *
     * @return boolean is read only action?
     */
1158 1159 1160
    function trimmed($key, $def=null)
    {
        $arg = $this->arg($key, $def);
Robin Millette's avatar
phpdocs  
Robin Millette committed
1161
        return is_string($arg) ? trim($arg) : $arg;
1162 1163
    }

Robin Millette's avatar
phpdocs  
Robin Millette committed
1164 1165 1166 1167 1168 1169 1170
    /**
     * Handler method
     *
     * @param array $argarray is ignored since it's now passed in in prepare()
     *
     * @return boolean is read only action?
     */
1171 1172
    function handle($argarray=null)
    {
1173
        header('Vary: Accept-Encoding,Cookie');
1174

Robin Millette's avatar
phpdocs  
Robin Millette committed
1175
        $lm   = $this->lastModified();
1176
        $etag = $this->etag();
1177

1178 1179 1180
        if ($etag) {
            header('ETag: ' . $etag);
        }
1181

1182 1183
        if ($lm) {
            header('Last-Modified: ' . date(DATE_RFC1123, $lm));
Evan Prodromou's avatar
Evan Prodromou committed
1184 1185 1186
            if ($this->isCacheable()) {
                header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
                header( "Cache-Control: private, must-revalidate, max-age=0" );
1187
                header( "Pragma:");
1188 1189 1190
            }
        }

1191
        $checked = false;
1192 1193 1194
        if ($etag) {
            $if_none_match = (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER)) ?
              $_SERVER['HTTP_IF_NONE_MATCH'] : null;
1195 1196 1197 1198 1199 1200 1201 1202
            if ($if_none_match) {
                // If this check fails, ignore the if-modified-since below.
                $checked = true;
                if ($this->_hasEtag($etag, $if_none_match)) {
                    header('HTTP/1.1 304 Not Modified');
                    // Better way to do this?
                    exit(0);
                }
1203 1204 1205
            }
        }

1206
        if (!$checked && $lm && array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
1207 1208 1209 1210 1211 1212
            $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
            $ims = strtotime($if_modified_since);
            if ($lm <= $ims) {
                header('HTTP/1.1 304 Not Modified');
                // Better way to do this?
                exit(0);
Evan Prodromou's avatar
Evan Prodromou committed
1213
            }