]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/action.php
Making us less dependant on javascript trust
[quix0rs-gnu-social.git] / lib / action.php
index 64661a46c1eeebd336ff49c56a99be5e709b19b7..74ced563fbbbc46e3951dc9dfc5431db7ac38f89 100644 (file)
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET') && !defined('LACONICA')) {
-    exit(1);
-}
-
-require_once INSTALLDIR.'/lib/noticeform.php';
-require_once INSTALLDIR.'/lib/htmloutputter.php';
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Base class for all actions
@@ -63,13 +58,15 @@ class Action extends HTMLOutputter // lawsuit
     protected $ajax   = false;
     protected $menus  = true;
     protected $needLogin = false;
+    protected $needPost = false;
 
-    // The currently scoped profile
+    // The currently scoped profile (normally Profile::current; from $this->auth_user for API)
     protected $scoped = null;
 
-    // Messages to the front-end user
-    protected $error = null;
-    protected $msg   = null;
+    // Related to front-end user representation
+    protected $format = null;
+    protected $error  = null;
+    protected $msg    = null;
 
     /**
      * Constructor
@@ -120,9 +117,16 @@ class Action extends HTMLOutputter // lawsuit
             common_config_set('db', 'database', $mirror);
         }
 
-        if ($this->prepare($args)) {
+        $status = $this->prepare($args);
+        if ($status) {
             $this->handle($args);
+        } else {
+            common_debug('Prepare failed for Action.');
         }
+
+        $this->flush();
+
+        Event::handle('EndActionExecute', array($status, $this));
     }
 
     /**
@@ -134,6 +138,11 @@ class Action extends HTMLOutputter // lawsuit
      */
     protected function prepare(array $args=array())
     {
+        if ($this->needPost && !$this->isPost()) {
+            // TRANS: Client error. POST is a HTTP command. It should not be translated.
+            $this->clientError(_('This method requires a POST.'), 405);
+        }
+
         $this->args = common_copy_args($args);
 
         $this->action = $this->trimmed('action');
@@ -147,11 +156,16 @@ class Action extends HTMLOutputter // lawsuit
             $this->checkLogin(); // if not logged in, this redirs/excepts
         }
 
-        $this->scoped = Profile::current();
+        $this->updateScopedProfile();
 
         return true;
     }
 
+    function updateScopedProfile() {
+        $this->scoped = Profile::current();
+        return $this->scoped;
+    }
+
     /**
      * Show page, a template method.
      *
@@ -283,28 +297,26 @@ class Action extends HTMLOutputter // lawsuit
 
             // Use old name for StatusNet for compatibility on events
 
-            if (Event::handle('StartShowStatusNetStyles', array($this)) &&
-                Event::handle('StartShowLaconicaStyles', array($this))) {
+            if (Event::handle('StartShowStylesheets', array($this))) {
                 $this->primaryCssLink(null, 'screen, projection, tv, print');
-                Event::handle('EndShowStatusNetStyles', array($this));
-                Event::handle('EndShowLaconicaStyles', array($this));
+                Event::handle('EndShowStylesheets', array($this));
             }
 
-            $this->cssLink(common_path('js/css/smoothness/jquery-ui.css', StatusNet::isHTTPS()));
+            $this->cssLink('js/extlib/jquery-ui/css/smoothness/jquery-ui.css');
 
             if (Event::handle('StartShowUAStyles', array($this))) {
                 $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
-                               'href="'.Theme::path('css/ie.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
+                               'href="'.Theme::path('css/ie.css', 'base').'?version='.GNUSOCIAL_VERSION.'" /><![endif]');
                 foreach (array(6,7) as $ver) {
                     if (file_exists(Theme::file('css/ie'.$ver.'.css', 'base'))) {
                         // Yes, IE people should be put in jail.
                         $this->comment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
-                                       'href="'.Theme::path('css/ie'.$ver.'.css', 'base').'?version='.STATUSNET_VERSION.'" /><![endif]');
+                                       'href="'.Theme::path('css/ie'.$ver.'.css', 'base').'?version='.GNUSOCIAL_VERSION.'" /><![endif]');
                     }
                 }
                 if (file_exists(Theme::file('css/ie.css'))) {
                     $this->comment('[if IE]><link rel="stylesheet" type="text/css" '.
-                               'href="'.Theme::path('css/ie.css', null).'?version='.STATUSNET_VERSION.'" /><![endif]');
+                               'href="'.Theme::path('css/ie.css', null).'?version='.GNUSOCIAL_VERSION.'" /><![endif]');
                 }
                 Event::handle('EndShowUAStyles', array($this));
             }
@@ -358,35 +370,20 @@ class Action extends HTMLOutputter // lawsuit
     {
         if (Event::handle('StartShowScripts', array($this))) {
             if (Event::handle('StartShowJQueryScripts', array($this))) {
-                if (common_config('site', 'minify')) {
-                    $this->script('jquery.min.js');
-                    $this->script('jquery.form.min.js');
-                    $this->script('jquery-ui.min.js');
-                    $this->script('jquery.cookie.min.js');
-                    $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/extlib/json2.min.js', StatusNet::isHTTPS()).'"); }');
-                    $this->script('jquery.joverlay.min.js');
-                    $this->script('jquery.infieldlabel.min.js');
-                } else {
-                    $this->script('jquery.js');
-                    $this->script('jquery.form.js');
-                    $this->script('jquery-ui.min.js');
-                    $this->script('jquery.cookie.js');
-                    $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/extlib/json2.js', StatusNet::isHTTPS()).'"); }');
-                    $this->script('jquery.joverlay.js');
-                    $this->script('jquery.infieldlabel.js');
-                }
+                $this->script('extlib/jquery.js');
+                $this->script('extlib/jquery.form.js');
+                $this->script('extlib/jquery-ui/jquery-ui.js');
+                $this->script('extlib/jquery.cookie.js');
+                $this->inlineScript('if (typeof window.JSON !== "object") { $.getScript("'.common_path('js/extlib/json2.js', StatusNet::isHTTPS()).'"); }');
+                $this->script('extlib/jquery.infieldlabel.js');
 
                 Event::handle('EndShowJQueryScripts', array($this));
             }
-            if (Event::handle('StartShowStatusNetScripts', array($this)) &&
-                Event::handle('StartShowLaconicaScripts', array($this))) {
-                if (common_config('site', 'minify')) {
-                    $this->script('util.min.js');
-                } else {
-                    $this->script('util.js');
-                    $this->script('xbImportNode.js');
-                    $this->script('geometa.js');
-                }
+            if (Event::handle('StartShowStatusNetScripts', array($this))) {
+                $this->script('util.js');
+                $this->script('xbImportNode.js');
+                $this->script('geometa.js');
+
                 // This route isn't available in single-user mode.
                 // Not sure why, but it causes errors here.
                 $this->inlineScript('var _peopletagAC = "' .
@@ -400,7 +397,6 @@ class Action extends HTMLOutputter // lawsuit
                     $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 = ""; }; }');
                 }
                 Event::handle('EndShowStatusNetScripts', array($this));
-                Event::handle('EndShowLaconicaScripts', array($this));
             }
             Event::handle('EndShowScripts', array($this));
         }
@@ -431,7 +427,7 @@ class Action extends HTMLOutputter // lawsuit
 
             $messages = array_merge($messages, $this->getScriptMessages());
 
-           Event::handle('EndScriptMessages', array($this, &$messages));
+            Event::handle('EndScriptMessages', array($this, &$messages));
         }
 
         if (!empty($messages)) {
@@ -685,7 +681,8 @@ class Action extends HTMLOutputter // lawsuit
     function showNoticeForm()
     {
         // TRANS: Tab on the notice form.
-        $tabs = array('status' => _m('TAB','Status'));
+        $tabs = array('status' => array('title' => _m('TAB','Status'),
+                                        'href'  => common_local_url('newnotice')));
 
         $this->elementStart('div', 'input_forms');
 
@@ -693,7 +690,8 @@ class Action extends HTMLOutputter // lawsuit
             $this->elementStart('ul', array('class' => 'nav',
                                             'id' => 'input_form_nav'));
 
-            foreach ($tabs as $tag => $title) {
+            foreach ($tabs as $tag => $data) {
+                $tag = htmlspecialchars($tag);
                 $attrs = array('id' => 'input_form_nav_'.$tag,
                                'class' => 'input_form_nav_tab');
 
@@ -706,8 +704,9 @@ class Action extends HTMLOutputter // lawsuit
                 $this->elementStart('li', $attrs);
 
                 $this->element('a',
-                               array('href' => 'javascript:SN.U.switchInputFormTab("'.$tag.'")'),
-                               $title);
+                               array('onclick' => 'return SN.U.switchInputFormTab("'.$tag.'");',
+                                     'href' => $data['href']),
+                               $data['title']);
                 $this->elementEnd('li');
             }
 
@@ -720,7 +719,7 @@ class Action extends HTMLOutputter // lawsuit
             $form->show();
             $this->elementEnd('div');
 
-            foreach ($tabs as $tag => $title) {
+            foreach ($tabs as $tag => $data) {
                 $attrs = array('class' => 'input_form',
                                'id' => 'input_form_'.$tag);
 
@@ -1045,33 +1044,33 @@ class Action extends HTMLOutputter // lawsuit
      */
     function showLicenses()
     {
-        $this->showStatusNetLicense();
+        $this->showGNUsocialLicense();
         $this->showContentLicense();
     }
 
     /**
-     * Show StatusNet license.
+     * Show GNU social license.
      *
      * @return nothing
      */
-    function showStatusNetLicense()
+    function showGNUsocialLicense()
     {
         if (common_config('site', 'broughtby')) {
-            // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is set.
+            // TRANS: First sentence of the GNU social site license. Used if 'broughtby' is set.
             // 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
             $instr = _('**%%site.name%%** is a social network, courtesy of [%%site.broughtby%%](%%site.broughtbyurl%%).');
         } else {
-            // TRANS: First sentence of the StatusNet site license. Used if 'broughtby' is not set.
+            // TRANS: First sentence of the GNU social site license. Used if 'broughtby' is not set.
             $instr = _('**%%site.name%%** is a social network.');
         }
         $instr .= ' ';
-        // TRANS: Second sentence of the StatusNet site license. Mentions the StatusNet source code license.
+        // TRANS: Second sentence of the GNU social site license. Mentions the GNU social source code license.
         // 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.
-        $instr .= sprintf(_('It runs on [GNU social](http://www.gnu.org/software/social/), version %s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), STATUSNET_VERSION);
+        // TRANS: [%1$s](%2$s) is a link description followed by the link itself
+        // TRANS: %3$s is the version of GNU social that is being used.
+        $instr .= sprintf(_('It runs on [%1$s](%2$s), version %3$s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), GNUSOCIAL_ENGINE, GNUSOCIAL_ENGINE_URL, GNUSOCIAL_VERSION);
         $output = common_markup_to_html($instr);
         $this->raw($output);
         // do it
@@ -1359,26 +1358,99 @@ class Action extends HTMLOutputter // lawsuit
      *
      * @return nothing
      */
-    function serverError($msg, $code=500)
+    function serverError($msg, $code=500, $format=null)
     {
-        $action = $this->trimmed('action');
-        common_debug("Server error '$code' on '$action': $msg", __FILE__);
-        throw new ServerException($msg, $code);
+        if ($format === null) {
+            $format = $this->format;
+        }
+
+        common_debug("Server error '{$code}' on '{$this->action}': {$msg}", __FILE__);
+
+        if (!array_key_exists($code, ServerErrorAction::$status)) {
+            $code = 500;
+        }
+
+        $status_string = ServerErrorAction::$status[$code];
+
+        switch ($format) {
+        case 'xml':
+            header("HTTP/1.1 {$code} {$status_string}");
+            $this->initDocument('xml');
+            $this->elementStart('hash');
+            $this->element('error', null, $msg);
+            $this->element('request', null, $_SERVER['REQUEST_URI']);
+            $this->elementEnd('hash');
+            $this->endDocument('xml');
+            break;
+        case 'json':
+            if (!isset($this->callback)) {
+                header("HTTP/1.1 {$code} {$status_string}");
+            }
+            $this->initDocument('json');
+            $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
+            print(json_encode($error_array));
+            $this->endDocument('json');
+            break;
+        default:
+            throw new ServerException($msg, $code);
+        }
+
+        exit((int)$code);
     }
 
     /**
      * Client error
      *
-     * @param string  $msg  error message to display
-     * @param integer $code http error code, 400 by default
+     * @param string  $msg    error message to display
+     * @param integer $code   http error code, 400 by default
+     * @param string  $format error format (json, xml, text) for ApiAction
      *
      * @return nothing
+     * @throws ClientException always
      */
-    function clientError($msg, $code=400)
-    {
-        $action = $this->trimmed('action');
-        common_debug("User error '$code' on '$action': $msg", __FILE__);
-        throw new ClientException($msg, $code);
+    function clientError($msg, $code=400, $format=null)
+    {
+        // $format is currently only relevant for an ApiAction anyway
+        if ($format === null) {
+            $format = $this->format;
+        }
+
+        common_debug("User error '{$code}' on '{$this->action}': {$msg}", __FILE__);
+
+        if (!array_key_exists($code, ClientErrorAction::$status)) {
+            $code = 400;
+        }
+        
+        $status_string = ClientErrorAction::$status[$code];
+
+        switch ($format) {
+        case 'xml':
+            header("HTTP/1.1 {$code} {$status_string}");
+            $this->initDocument('xml');
+            $this->elementStart('hash');
+            $this->element('error', null, $msg);
+            $this->element('request', null, $_SERVER['REQUEST_URI']);
+            $this->elementEnd('hash');
+            $this->endDocument('xml');
+            break;
+        case 'json':
+            if (!isset($this->callback)) {
+                header("HTTP/1.1 {$code} {$status_string}");
+            }
+            $this->initDocument('json');
+            $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
+            $this->text(json_encode($error_array));
+            $this->endDocument('json');
+            break;
+        case 'text':
+            header("HTTP/1.1 {$code} {$status_string}");
+            header('Content-Type: text/plain; charset=utf-8');
+            echo $msg;
+            break;
+        default:
+            throw new ClientException($msg, $code);
+        }
+        exit((int)$code);
     }
 
     /**