]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - index.php
Add translator documentation.
[quix0rs-gnu-social.git] / index.php
index 9501e2275da6d610b9263bcb5a9e911e721d64de..850208c9d1501a80337a7a8d568a85aa88100527 100644 (file)
--- a/index.php
+++ b/index.php
  * @license  GNU Affero General Public License http://www.gnu.org/licenses/
  */
 
+$_startTime = microtime(true);
+$_perfCounters = array();
+
 define('INSTALLDIR', dirname(__FILE__));
 define('STATUSNET', true);
 define('LACONICA', true); // compatibility
 
-require_once INSTALLDIR . '/lib/common.php';
-
 $user = null;
 $action = null;
 
@@ -72,52 +73,68 @@ function getPath($req)
  */
 function handleError($error)
 {
-    if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
-        return;
-    }
+    try {
 
-    $logmsg = "PEAR error: " . $error->getMessage();
-    if (common_config('site', 'logdebug')) {
-        $logmsg .= " : ". $error->getDebugInfo();
-    }
-    // DB queries often end up with a lot of newlines; merge to a single line
-    // for easier grepability...
-    $logmsg = str_replace("\n", " ", $logmsg);
-    common_log(LOG_ERR, $logmsg);
-
-    // @fixme backtrace output should be consistent with exception handling
-    if (common_config('site', 'logdebug')) {
-        $bt = $error->getBacktrace();
-        foreach ($bt as $n => $line) {
-            common_log(LOG_ERR, formatBacktraceLine($n, $line));
+        if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
+            return;
         }
-    }
-    if ($error instanceof DB_DataObject_Error
-        || $error instanceof DB_Error
-    ) {
-        $msg = sprintf(
-            _(
-                'The database for %s isn\'t responding correctly, '.
-                'so the site won\'t work properly. '.
-                'The site admins probably know about the problem, '.
-                'but you can contact them at %s to make sure. '.
-                'Otherwise, wait a few minutes and try again.'
-            ),
-            common_config('site', 'name'),
-            common_config('site', 'email')
-        );
-    } else {
-        $msg = _(
-            'An important error occured, probably related to email setup. '.
-            'Check logfiles for more info..'
-        );
-    }
 
-    $dac = new DBErrorAction($msg, 500);
-    $dac->showPage();
+        $logmsg = "PEAR error: " . $error->getMessage();
+        if ($error instanceof PEAR_Exception && common_config('site', 'logdebug')) {
+            $logmsg .= " : ". $error->toText();
+        }
+        // DB queries often end up with a lot of newlines; merge to a single line
+        // for easier grepability...
+        $logmsg = str_replace("\n", " ", $logmsg);
+        common_log(LOG_ERR, $logmsg);
+
+        // @fixme backtrace output should be consistent with exception handling
+        if (common_config('site', 'logdebug')) {
+            $bt = $error->getTrace();
+            foreach ($bt as $n => $line) {
+                common_log(LOG_ERR, formatBacktraceLine($n, $line));
+            }
+        }
+        if ($error instanceof DB_DataObject_Error
+            || $error instanceof DB_Error
+            || ($error instanceof PEAR_Exception && $error->getCode() == -24)
+        ) {
+            //If we run into a DB error, assume we can't connect to the DB at all
+            //so set the current user to null, so we don't try to access the DB
+            //while rendering the error page.
+            global $_cur;
+            $_cur = null;
+
+            $msg = sprintf(
+                // TRANS: Database error message.
+                _('The database for %1$s is not responding correctly, '.
+                  'so the site will not work properly. '.
+                  'The site admins probably know about the problem, '.
+                  'but you can contact them at %2$s to make sure. '.
+                  'Otherwise, wait a few minutes and try again.'
+                ),
+                common_config('site', 'name'),
+                common_config('site', 'email')
+            );
+
+            $dac = new DBErrorAction($msg, 500);
+            $dac->showPage();
+        } else {
+            $sac = new ServerErrorAction($error->getMessage(), 500, $error);
+            $sac->showPage();
+        }
+
+    } catch (Exception $e) {
+        // TRANS: Error message.
+        echo _('An error occurred.');
+    }
     exit(-1);
 }
 
+set_exception_handler('handleError');
+
+require_once INSTALLDIR . '/lib/common.php';
+
 /**
  * Format a backtrace line for debug output roughly like debug_print_backtrace() does.
  * Exceptions already have this built in, but PEAR error objects just give us the array.
@@ -156,15 +173,24 @@ function setupRW()
 
     static $alwaysRW = array('session', 'remember_me');
 
-    // We ensure that these tables always are used
-    // on the master DB
+    $rwdb = $config['db']['database'];
+
+    if (Event::handle('StartReadWriteTables', array(&$alwaysRW, &$rwdb))) {
 
-    $config['db']['database_rw'] = $config['db']['database'];
-    $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
+        // We ensure that these tables always are used
+        // on the master DB
 
-    foreach ($alwaysRW as $table) {
-        $config['db']['table_'.$table] = 'rw';
+        $config['db']['database_rw'] = $rwdb;
+        $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
+
+        foreach ($alwaysRW as $table) {
+            $config['db']['table_'.$table] = 'rw';
+        }
+
+        Event::handle('EndReadWriteTables', array($alwaysRW, $rwdb));
     }
+
+    return;
 }
 
 function checkMirror($action_obj, $args)
@@ -189,7 +215,7 @@ function checkMirror($action_obj, $args)
 
 function isLoginAction($action)
 {
-    static $loginActions =  array('login', 'recoverpassword', 'api', 'doc', 'register', 'publicxrds', 'otp', 'opensearch', 'rsd');
+    static $loginActions =  array('login', 'recoverpassword', 'api', 'doc', 'register', 'publicxrds', 'otp', 'opensearch', 'rsd', 'hostmeta');
 
     $login = null;
 
@@ -204,7 +230,7 @@ function main()
 {
     // fake HTTP redirects using lighttpd's 404 redirects
     if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
-        $_lighty_url = $base_url.$_SERVER['REQUEST_URI'];
+        $_lighty_url = $_SERVER['REQUEST_URI'];
         $_lighty_url = @parse_url($_lighty_url);
 
         if ($_lighty_url['path'] != '/index.php' && $_lighty_url['path'] != '/') {
@@ -232,9 +258,9 @@ function main()
 
     if (!_have_config()) {
         $msg = sprintf(
-            _(
-                "No configuration file found. Try running ".
-                "the installation program first."
+            // TRANS: Error message displayed when there is no StatusNet configuration file.
+            _("No configuration file found. Try running ".
+              "the installation program first."
             )
         );
         $sac = new ServerErrorAction($msg);
@@ -242,10 +268,6 @@ function main()
         return;
     }
 
-    // For database errors
-
-    PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
-
     // Make sure RW database is setup
 
     setupRW();
@@ -267,11 +289,20 @@ function main()
     $args = $r->map($path);
 
     if (!$args) {
+        // TRANS: Error message displayed when trying to access a non-existing page.
         $cac = new ClientErrorAction(_('Unknown page'), 404);
         $cac->showPage();
         return;
     }
 
+    $site_ssl = common_config('site', 'ssl');
+
+    // If the request is HTTP and it should be HTTPS...
+    if ($site_ssl != 'never' && !StatusNet::isHTTPS() && common_is_sensitive($args['action'])) {
+        common_redirect(common_local_url($args['action'], $args));
+        return;
+    }
+
     $args = array_merge($args, $_REQUEST);
 
     Event::handle('ArgsInitialize', array(&$args));
@@ -313,6 +344,7 @@ function main()
     $action_class = ucfirst($action).'Action';
 
     if (!class_exists($action_class)) {
+        // TRANS: Error message displayed when trying to perform an undefined action.
         $cac = new ClientErrorAction(_('Unknown action'), 404);
         $cac->showPage();
     } else {