diff --git a/htdocs/always.php b/htdocs/always.php index 27b635a9..a2bd5a39 100644 --- a/htdocs/always.php +++ b/htdocs/always.php @@ -18,6 +18,8 @@ unset($session); unset($request); unset($dbconn); unset($_awl_dbconn); unset($in // An ultra-simple exception handler to catch errors that occur // before we get a more functional exception handler in place... function early_exception_handler($e) { + if ( !headers_sent() ) header("Content-type: text/plain"); else echo "
\n";
+  while ( ob_get_level() > 0 ) ob_end_flush();
   echo "Exception [".$e->getCode()."] ".$e->getmessage()."\n";
   echo "At line ", $e->getLine(), " of ", $e->getFile(), "\n";
   echo "================= Stack Trace ===================\n";
@@ -26,7 +28,6 @@ function early_exception_handler($e) {
   foreach( $trace AS $k => $v ) {
     printf( "%s[%d] %s%s%s()\n", $v['file'], $v['line'], (isset($v['class'])?$v['class']:''), (isset($v['type'])?$v['type']:''), (isset($v['function'])?$v['function']:'') );
   }
-  @ob_flush();
 }
 set_exception_handler('early_exception_handler');
 
@@ -55,6 +56,8 @@ $c->default_privileges = array('read-free-busy', 'schedule-deliver');
 
 $c->enable_auto_schedule = true;
 
+$c->schema_major = $c->schema_minor = $c->schema_patch = 0;
+
 $c->stylesheets = array( $c->base_url.'/davical.css' );
 $c->images      = $c->base_url . '/images';
 
@@ -159,7 +162,7 @@ if ( isset($_SERVER['HTTP_X_DAVICAL_TESTCASE']) ) {
 else if ( isset($c->dbg['script_start']) && $c->dbg['script_start'] ) {
   // Only log this if more than a little debugging of some sort is turned on, somewhere
   @dbg_error_log( 'LOG', '==========> method =%s= =%s= =%s= =%s= =%s=',
-         $_SERVER['REQUEST_METHOD'], $c->protocol_server_port_script, $_SERVER['PATH_INFO'], $c->base_url, $c->base_directory );
+         $_SERVER['REQUEST_METHOD'], $c->protocol_server_port_script, (isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '$_SERVER[PATH_INFO] undefined'), $c->base_url, $c->base_directory );
 }
 
 /**
diff --git a/htdocs/setup.php b/htdocs/setup.php
index 86604aa4..1d6ef178 100644
--- a/htdocs/setup.php
+++ b/htdocs/setup.php
@@ -1,10 +1,46 @@
 \n";
+  while ( ob_get_level() > 0 ) ob_end_flush();
+  echo "Error [".$errno."] ".$errstr."\n";
+  echo "At line ", $errline, " of ", $errfile, "\n";
+
+  $e = new Exception();
+  $trace = array_reverse($e->getTrace());
+  echo "================= Stack Trace ===================\n";
+  foreach( $trace AS $k => $v ) {
+    printf( "%s[%d] %s%s%s()\n", $v['file'], $v['line'], (isset($v['class'])?$v['class']:''), (isset($v['type'])?$v['type']:''), (isset($v['function'])?$v['function']:'') );
+  }
+}
+
+set_error_handler('catch_setup_errors', E_ALL);
+
 class CheckResult {
   private $ok;
   private $use_class;
@@ -57,6 +93,13 @@ function check_pdo_pgsql() {
   return new CheckResult(isset($loaded_extensions['pdo_pgsql']));
 }
 
+function check_database_connection() {
+  global $c;
+
+  if ( !check_pdo_pgsql() ) return new CheckResult(false);
+  return new CheckResult( $c->schema_major != 0 || $c->schema_minor != 0 );
+}
+
 function check_gettext() {
   global $phpinfo, $loaded_extensions;
 
@@ -127,6 +170,7 @@ if ( !check_gettext()->getOK() )   do_error("The GNU 'gettext' extension for PHP
 if ( !check_pgsql()->getOK() )     do_error("PHP 'pgsql' functions are not available");
 if ( !check_pdo()->getOK() )       do_error("PHP 'PDO' module is not available");
 if ( !check_pdo_pgsql()->getOK() ) do_error("The PDO drivers for PostgreSQL are not available");
+if ( !check_database_connection()->getOK() ) do_error("Unable to connect to database");
 if ( !check_iconv()->getOK() )     do_error("The 'iconv' extension for PHP is not available");
 
 function get_phpinfo() {
@@ -141,12 +185,17 @@ function get_phpinfo() {
 }
 $phpinfo = get_phpinfo();
 
-include("./always.php");
-include("DAViCalSession.php");
-
-
-if ( check_pgsql()->GetOK() ) {
-  $session->LoginRequired( (isset($c->restrict_setup_to_admin) && $c->restrict_setup_to_admin ? 'Admin' : null ) );
+try {
+  include("./always.php");
+  include("DAViCalSession.php");
+  if ( check_pgsql()->GetOK() ) {
+    $session->LoginRequired( (isset($c->restrict_setup_to_admin) && $c->restrict_setup_to_admin ? 'Admin' : null ) );
+  }
+}
+catch( Exception $e ) {
+  set_error_handler('catch_setup_errors', E_ALL);
+  include("DAViCalSession.php");
+//  $session = new FakeSession(1);
 }
 
 
@@ -228,7 +277,7 @@ function build_site_statistics() {
 
 EOTABLE;
 
-  if ( !check_pdo_pgsql() ) {
+  if ( !check_database_connection() ) {
     return sprintf( $table, ''.translate('Site Statistics require the database to be available!').'');
   }
   $sql = 'SELECT
@@ -250,12 +299,12 @@ function build_dependencies_table( ) {
   
   $dependencies = array(
     translate('Current DAViCal version ')         => 'check_davical_version',
-    translate('DAViCal DB Schema version ')       => 'check_schema_version',
     translate('AWL Library version ')             => 'check_awl_version',
     translate('PHP not using Apache Filter mode') => 'check_real_php',
     translate('PHP PDO module available')         => 'check_pdo',
     translate('PDO PostgreSQL drivers')           => 'check_pdo_pgsql',
-//    translate('PHP PostgreSQL available')         => 'check_pgsql',
+    translate('Database is Connected')            => 'check_database_connection',
+    translate('DAViCal DB Schema version ')       => 'check_schema_version',
     translate('GNU gettext support')              => 'check_gettext',
     translate('PHP iconv support')                => 'check_iconv',
     translate('PHP DateTime class')               => 'check_datetime',
@@ -311,8 +360,10 @@ $th_dependency = translate('Dependency');
 $th_status     = translate('Status');
 $dependencies_table = build_dependencies_table();
 
-$heading_site_statistics = translate('Site Statistics');
-$site_statistics_table = build_site_statistics();
+if ( check_database_connection()->GetOK() ) {
+  $heading_site_statistics = translate('Site Statistics');
+  $site_statistics_table = build_site_statistics();
+}
 
 $heading_config_clients = translate('Configuring Calendar Clients for DAViCal');
 $heading_config_davical = translate('Configuring DAViCal');
diff --git a/inc/DAViCalSession.php b/inc/DAViCalSession.php
index 68d0f97a..92dfb657 100644
--- a/inc/DAViCalSession.php
+++ b/inc/DAViCalSession.php
@@ -47,6 +47,8 @@ require('Session.php');
 class DAViCalSession extends Session
 {
 
+  public $principal_id;
+  
   /**
   * Create a new DAViCalSession object.
   *
@@ -56,6 +58,7 @@ class DAViCalSession extends Session
   * @param string $sid A session identifier.
   */
   function __construct( $sid='' ) {
+    $this->principal_id = null;
     $this->Session($sid);
   }
 
diff --git a/inc/always.php.in b/inc/always.php.in
index 95a743f6..5325dd96 100644
--- a/inc/always.php.in
+++ b/inc/always.php.in
@@ -18,6 +18,8 @@ unset($session); unset($request); unset($dbconn); unset($_awl_dbconn); unset($in
 // An ultra-simple exception handler to catch errors that occur
 // before we get a more functional exception handler in place...
 function early_exception_handler($e) {
+  if ( !headers_sent() ) header("Content-type: text/plain"); else echo "
\n";
+  while ( ob_get_level() > 0 ) ob_end_flush();
   echo "Exception [".$e->getCode()."] ".$e->getmessage()."\n";
   echo "At line ", $e->getLine(), " of ", $e->getFile(), "\n";
   echo "================= Stack Trace ===================\n";
@@ -26,7 +28,6 @@ function early_exception_handler($e) {
   foreach( $trace AS $k => $v ) {
     printf( "%s[%d] %s%s%s()\n", $v['file'], $v['line'], (isset($v['class'])?$v['class']:''), (isset($v['type'])?$v['type']:''), (isset($v['function'])?$v['function']:'') );
   }
-  @ob_flush();
 }
 set_exception_handler('early_exception_handler');
 
@@ -55,6 +56,8 @@ $c->default_privileges = array('read-free-busy', 'schedule-deliver');
 
 $c->enable_auto_schedule = true;
 
+$c->schema_major = $c->schema_minor = $c->schema_patch = 0;
+
 $c->stylesheets = array( $c->base_url.'/davical.css' );
 $c->images      = $c->base_url . '/images';
 
@@ -159,7 +162,7 @@ if ( isset($_SERVER['HTTP_X_DAVICAL_TESTCASE']) ) {
 else if ( isset($c->dbg['script_start']) && $c->dbg['script_start'] ) {
   // Only log this if more than a little debugging of some sort is turned on, somewhere
   @dbg_error_log( 'LOG', '==========> method =%s= =%s= =%s= =%s= =%s=',
-         $_SERVER['REQUEST_METHOD'], $c->protocol_server_port_script, $_SERVER['PATH_INFO'], $c->base_url, $c->base_directory );
+         $_SERVER['REQUEST_METHOD'], $c->protocol_server_port_script, (isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '$_SERVER[PATH_INFO] undefined'), $c->base_url, $c->base_directory );
 }
 
 /**