Refactored to provide clearer display of issues.

This commit is contained in:
Andrew McMillan 2011-01-01 21:23:25 +13:00
parent fbd28407ea
commit fe095d009d

View File

@ -1,85 +1,108 @@
<?php
/** @TODO: work out something more than true/false returns for dependency checks */
function i18n($s) { return $s; };
class CheckResult {
private $ok;
private $use_class;
private $description;
function __construct( $success, $description=null, $use_class=null ) {
$this->ok = (boolean) $success;
$this->description = (isset($description)?$description : ($success===true? i18n('Passed') : i18n('Fail')));
$this->use_class = (isset($use_class)?$use_class:($success===true?'dep_ok' : 'dep_fail'));
}
public function getClass() {
return $this->use_class;
}
public function setClass( $new_class ) {
$this->use_class = $new_class;
}
public function getOK() {
return $this->ok;
}
public function getDescription() {
return translate($this->description);
}
public function setDescription( $new_desc ) {
$this->description = $new_desc;
}
}
/**
* We put many of these checks before we even try to load always.php so that we
* can try and do some diagnostic work to ensure it will load OK.
*/
function check_pgsql() {
return function_exists('pg_connect');
return new CheckResult(function_exists('pg_connect'));
}
function check_pdo() {
return class_exists('PDO');
return new CheckResult(class_exists('PDO'));
}
function check_pdo_pgsql() {
global $loaded_extensions;
if ( !check_pdo() ) return false;
return isset($loaded_extensions['pdo_pgsql']);
if ( !check_pdo() ) return new CheckResult(false);
return new CheckResult(isset($loaded_extensions['pdo_pgsql']));
}
function check_gettext() {
global $phpinfo, $loaded_extensions;
if ( !function_exists('gettext') ) return false;
return isset($loaded_extensions['gettext']);
if ( !function_exists('gettext') ) return new CheckResult(false);
return new CheckResult(isset($loaded_extensions['gettext']));
}
function check_suhosin_server_strip() {
global $loaded_extensions;
if ( !isset($loaded_extensions['suhosin']) ) return true;
if ( ini_get('suhosin.server.strip') == "0" ) {
return true;
}
return false;
if ( !isset($loaded_extensions['suhosin']) ) return new CheckResult(true);
return new CheckResult( ini_get('suhosin.server.strip') == "0" );
}
function check_magic_quotes_gpc() {
global $loaded_extensions;
if ( ini_get('magic_quotes_gpc') == "0" ) {
return true;
}
return false;
return new CheckResult( (get_magic_quotes_gpc() == 0) );
}
function check_magic_quotes_runtime() {
global $loaded_extensions;
if ( ini_get('magic_quotes_runtime') == "0" ) {
return true;
}
return false;
return new CheckResult( (get_magic_quotes_runtime() == 0) );
}
function do_error( $errormessage ) {
printf("<p class='error'>%s</p>", $errormessage );
printf("<p class='error'>%s</p>", translate($errormessage) );
}
$loaded_extensions = array_flip(get_loaded_extensions());
if ( !check_pgsql() ) do_error("PHP 'pgsql' functions are not available" );
if ( !check_pdo() ) do_error("PHP 'PDO' module is not available" );
if ( !check_pdo_pgsql() ) do_error("The PDO drivers for PostgreSQL are not available" );
$loaded_extensions = array_flip(get_loaded_extensions());
if ( !check_pgsql()->getOK() ) do_error(i18n("PHP 'pgsql' functions are not available") );
if ( !check_pdo()->getOK() ) do_error(i18n("PHP 'PDO' module is not available") );
if ( !check_pdo_pgsql()->getOK() ) do_error(i18n("The PDO drivers for PostgreSQL are not available") );
function get_phpinfo() {
ob_start( );
phpinfo();
$phpinfo = ob_get_contents( );
ob_end_clean( );
$phpinfo = preg_replace( '{^.*?<body>}s', '', $phpinfo);
$phpinfo = preg_replace( '{</body>.*?$}s', '', $phpinfo);
return $phpinfo;
}
$phpinfo = get_phpinfo();
include("./always.php");
include("DAViCalSession.php");
ob_start( );
phpinfo();
$phpinfo = ob_get_contents( );
ob_end_clean( );
$phpinfo = preg_replace( '{^.*?<body>}s', '', $phpinfo);
$phpinfo = preg_replace( '{</body>.*?$}s', '', $phpinfo);
if ( check_pgsql() ) {
if ( check_pgsql()->GetOK() ) {
$session->LoginRequired( (isset($c->restrict_setup_to_admin) && $c->restrict_setup_to_admin ? 'Admin' : null ) );
}
@ -94,29 +117,51 @@ function check_schema_version() {
if ( $c->want_dbversion[0] == $c->schema_major
&& $c->want_dbversion[1] == $c->schema_minor
&& $c->want_dbversion[2] == $c->schema_patch ) {
return true;
return new CheckResult( true );
}
return false;
$result = new CheckResult(false);
if ( $c->want_dbversion[0] < $c->schema_major
|| ($c->want_dbversion[0] == $c->schema_major && $c->want_dbversion[1] < $c->schema_minor)
|| ($c->want_dbversion[0] == $c->schema_major
&& $c->want_dbversion[1] == $c->schema_minor
&& $c->want_dbversion[2] < $c->schema_patch)
)
{
$result->setClass('dep_warning');
}
$result->setDescription( sprintf(i18n('Want: %s, Currently: %s'), implode('.',$c->want_dbversion),
$c->schema_major.'.'.$c->schema_minor.'.'.$c->schema_patch));
return $result;
}
function check_davical_version() {
global $c;
$url = 'http://www.davical.org/current_davical_version?v='.$c->version_string;
$version_file = @fopen($url, 'r');
if ( ! $version_file ) return translate("Could not retrieve") . " '$url'";
if ( ! $version_file ) return new CheckResult( false, translate("Could not retrieve") . " '$url'", 'dep_warning' );
$current_version = trim(fread( $version_file,12));
fclose($version_file);
return ( $c->version_string == $current_version ? true : $current_version );
$result = new CheckResult($c->version_string == $current_version);
if ( ! $result->getOK() ) {
$result->setDescription( sprintf(i18n('Want: %s, Currently: %s'), $current_version, $c->version_string) );
if ( $c->version_string > $current_version ) $result->setClass('dep_warning');
}
return $result;
}
function check_awl_version() {
global $c;
if ( function_exists('awl_version') ) {
return ( $c->want_awl_version == awl_version() ? true : sprintf( "want %f", $c->awl_library_version) );
if ( !function_exists('awl_version') ) return new CheckResult(false);
$result = new CheckResult($c->want_awl_version == awl_version());
if ( ! $result->getOK() ) {
$result->setDescription( sprintf(i18n('Want: %s, Currently: %s'), $c->want_awl_version, awl_version()) );
if ( $c->want_awl_version < awl_version() ) $result->setClass('dep_warning');
}
return false;
return $result;
}
@ -148,36 +193,43 @@ EOTABLE;
}
$dependencies = array(
translate('Current DAViCal version '). $c->version_string => 'check_davical_version',
translate('DAViCal DB Schema version '). implode('.',$c->want_dbversion) => 'check_schema_version',
translate('AWL Library version '). $c->want_awl_version => 'check_awl_version',
translate('PHP PDO module available') => 'check_pdo',
translate('PDO PostgreSQL drivers') => 'check_pdo_pgsql',
translate('PHP PostgreSQL available') => 'check_pgsql',
translate('GNU gettext support') => 'check_gettext',
translate('Suhosin "server.strip" disabled') => 'check_suhosin_server_strip',
translate('PHP Magic Quotes GPC off') => 'check_magic_quotes_gpc',
translate('PHP Magic Quotes runtime off') => 'check_magic_quotes_runtime' /*,
'YAML' => 'php5-syck' */
);
$dependencies_table = '';
$dep_tpl = '<tr class="%s">
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 PDO module available') => 'check_pdo',
translate('PDO PostgreSQL drivers') => 'check_pdo_pgsql',
translate('PHP PostgreSQL available') => 'check_pgsql',
translate('GNU gettext support') => 'check_gettext',
translate('Suhosin "server.strip" disabled') => 'check_suhosin_server_strip',
translate('PHP Magic Quotes GPC off') => 'check_magic_quotes_gpc',
translate('PHP Magic Quotes runtime off') => 'check_magic_quotes_runtime'
);
$dependencies_table = '';
$dep_tpl = '<tr class="%s">
<td>%s</td>
<td>%s</td>
</tr>
';
foreach( $dependencies AS $k => $v ) {
$ok = $v();
$dependencies_table .= sprintf( $dep_tpl, ($ok === true ? 'dep_ok' : 'dep_fail'), $k, (is_string($ok) ? $ok : ($ok ? translate('OK') : translate('Failed'))) );
foreach( $dependencies AS $k => $v ) {
$check_result = $v();
$dependencies_table .= sprintf( $dep_tpl, $check_result->getClass(),
$k,
$check_result->getDescription()
);
}
return $dependencies_table;
}
$want_dbversion = implode('.',$c->want_dbversion);
$heading_setup = translate('Setup');
$paragraph_setup = translate('This page primarily checks the environment needed for DAViCal to work correctly. Suggestions or patches to make it do more useful stuff will be gratefully received.');
/*
$want_dbversion = implode('.',$c->want_dbversion);
$heading_versions = translate('Current Versions');
if ( check_schema_version() != true )
{
@ -187,24 +239,23 @@ if ( check_schema_version() != true )
$paragraph_versions = translate('You are currently running DAViCal version %s. The database schema is at version %d.%d.%d.');
$paragraph_versions = sprintf( $paragraph_versions, $c->version_string, $c->schema_major, $c->schema_minor, $c->schema_patch);
}
*/
$heading_dependencies = translate('Dependencies');
$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();
$heading_config_clients = translate('Configuring Calendar Clients for DAViCal');
$heading_config_davical = translate('Configuring DAViCal');
$davical_configuration_errors = '';
if ( $config_warnings != '' ) {
$davical_configuration_errors = '<div class="error"><h3 class="error">' . translate('Your configuration produced PHP errors which should be corrected') . '</h3>
<pre>
'.$config_warnings.'
</pre></div>
';
}
$davical_configuration_errors = ( $config_warnings == '' ? '' : '<div class="error"><h3 class="error">'
. translate('Your configuration produced PHP errors which should be corrected') . '</h3><pre>'
. $config_warnings.'</pre></div>'
);
echo <<<EOBODY
<style>
@ -212,7 +263,10 @@ tr.dep_ok {
background-color:#80ff80;
}
tr.dep_fail {
background-color:#ffc0c0;
background-color:#ff8080;
}
tr.dep_warning {
background-color:#ffb040;
}
table, table.dependencies {
border: 1px grey solid;
@ -232,11 +286,6 @@ p {
<h1>$heading_setup</h1>
<p>$paragraph_setup
<h2>$heading_versions</h2>
<p>$paragraph_versions
<br>&nbsp;
</p>
<h2>$heading_dependencies</h2>
<p>
<table class="dependencies">
@ -246,9 +295,22 @@ p {
</tr>
$dependencies_table
</table>
<br>&nbsp;
</p>
<h2>$heading_config_davical</h2>
<p>If you can read this then things must be mostly working already.</p>
$davical_configuration_errors
<p>The <a href="http://www.davical.org/installation.php">installation page on the DAViCal website</a> has
some further information on how to install and configure this application.</p>
<h2>$heading_config_clients</h2>
<p>The <a href="http://www.davical.org/clients.php">client setup page on the DAViCal website</a> has information on how
to configure Evolution, Sunbird, Lightning and Mulberry to use remotely hosted calendars.</p>
<p>The administrative interface has no facility for viewing or modifying calendar data.</p>
<h2>$heading_site_statistics</h2>
<p>$site_statistics_table</p>
<h2>PHP Information</h2>
<script language="javascript">
function toggle_visible() {
var argv = toggle_visible.arguments;
@ -273,19 +335,6 @@ function toggle_visible() {
</script><p><label>Show phpinfo() output:<input type="checkbox" value="1" id="fld_show_phpinfo" onclick="toggle_visible('fld_show_phpinfo','=phpinfo')"></label></p>
<div style="display:none" id="phpinfo">$phpinfo</div>
<h2>$heading_site_statistics</h2>
<p>$site_statistics_table</p>
<h2>$heading_config_clients</h2>
<p>The <a href="http://www.davical.org/clients.php">client setup page on the DAViCal website</a> has information on how
to configure Evolution, Sunbird, Lightning and Mulberry to use remotely hosted calendars.</p>
<p>The administrative interface has no facility for viewing or modifying calendar data.</p>
<h2>$heading_config_davical</h2>
<p>If you can read this then things must be mostly working already.</p>
$davical_configuration_errors
<p>The <a href="http://www.davical.org/installation.php">installation page on the DAViCal website</a> has
some further information on how to install and configure this application.</p>
EOBODY;
include("page-footer.php");