]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - index.php
Merge commit 'origin/testing' into 0.9.x
[quix0rs-gnu-social.git] / index.php
index 59805f60009f94a0f973f11e1ee324b3af9b1cb7..65f251bcce6144db63bf3e6870fbda4d311da0fb 100644 (file)
--- a/index.php
+++ b/index.php
@@ -37,8 +37,6 @@ define('INSTALLDIR', dirname(__FILE__));
 define('STATUSNET', true);
 define('LACONICA', true); // compatibility
 
-require_once INSTALLDIR . '/lib/common.php';
-
 $user = null;
 $action = null;
 
@@ -68,52 +66,69 @@ 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(
+                _(
+                    '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();
+
+    } catch (Exception $e) {
+        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.
@@ -146,11 +161,26 @@ function formatBacktraceLine($n, $line)
     return $out;
 }
 
-function checkMirror($action_obj, $args)
+function setupRW()
 {
     global $config;
 
-    static $alwaysRW = array('session', 'remember_me', 'inbox');
+    static $alwaysRW = array('session', 'remember_me');
+
+    // We ensure that these tables always are used
+    // on the master DB
+
+    $config['db']['database_rw'] = $config['db']['database'];
+    $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
+
+    foreach ($alwaysRW as $table) {
+        $config['db']['table_'.$table] = 'rw';
+    }
+}
+
+function checkMirror($action_obj, $args)
+{
+    global $config;
 
     if (common_config('db', 'mirror') && $action_obj->isReadOnly($args)) {
         if (is_array(common_config('db', 'mirror'))) {
@@ -162,16 +192,6 @@ function checkMirror($action_obj, $args)
             $mirror = common_config('db', 'mirror');
         }
 
-        // We ensure that these tables always are used
-        // on the master DB
-
-        $config['db']['database_rw'] = $config['db']['database'];
-        $config['db']['ini_rw'] = INSTALLDIR.'/classes/statusnet.ini';
-
-        foreach ($alwaysRW as $table) {
-            $config['db']['table_'.$table] = 'rw';
-        }
-
         // everyone else uses the mirror
 
         $config['db']['database'] = $mirror;
@@ -180,7 +200,7 @@ function checkMirror($action_obj, $args)
 
 function isLoginAction($action)
 {
-    static $loginActions =  array('login', 'recoverpassword', 'api', 'doc', 'register', 'publicxrds');
+    static $loginActions =  array('login', 'recoverpassword', 'api', 'doc', 'register', 'publicxrds', 'otp');
 
     $login = null;
 
@@ -233,13 +253,13 @@ function main()
         return;
     }
 
-    // For database errors
+    // Make sure RW database is setup
 
-    PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
+    setupRW();
 
     // XXX: we need a little more structure in this script
 
-    // get and cache current user
+    // get and cache current user (may hit RW!)
 
     $user = common_current_user();
 
@@ -276,8 +296,9 @@ function main()
     if (!$user && common_config('site', 'private')
         && !isLoginAction($action)
         && !preg_match('/rss$/', $action)
-        && !preg_match('/^Api/', $action)
-    ) {
+        && $action != 'robotstxt'
+        && !preg_match('/^Api/', $action)) {
+
         // set returnto
         $rargs =& common_copy_args($args);
         unset($rargs['action']);