Example #1
0
function get_installed_version($postinstall = false)
{
    global $database_upgrade_matrix, $PROGRAM_VERSION, $settings, $show_all_errors;
    // Set this as the default value.
    $_SESSION['application_name'] = 'Title';
    $_SESSION['blank_database'] = '';
    // We will append the db_type to come up te proper filename.
    $_SESSION['install_file'] = 'tables';
    $_SESSION['old_program_version'] = $postinstall ? $PROGRAM_VERSION : 'new_install';
    // Suppress errors based on $show_all_errors.
    if (!$show_all_errors) {
        show_errors(false);
    }
    // This data is read from file upgrade_matrix.php.
    for ($i = 0, $dbCntStr = count($database_upgrade_matrix); $i < $dbCntStr; $i++) {
        $sql = $database_upgrade_matrix[$i][0];
        if ($sql != '') {
            $res = dbi_execute($sql, array(), false, $show_all_errors);
        }
        if ($res) {
            $_SESSION['old_program_version'] = $database_upgrade_matrix[$i + 1][2];
            $_SESSION['install_file'] = $database_upgrade_matrix[$i + 1][3];
            $res = '';
            $sql = $database_upgrade_matrix[$i][1];
            if ($sql != '') {
                dbi_execute($sql, array(), false, $show_all_errors);
            }
        }
    }
    $response_msg = $_SESSION['old_program_version'] == 'pre-v0.9.07' ? translate('Perl script required') : translate('previous version requires updating several tables');
    // v1.1 and after will have an entry in webcal_config to make this easier
    // $res = dbi_execute ( 'SELECT cal_value FROM webcal_config
    //   WHERE cal_setting = \'WEBCAL_PROGRAM_VERSION\'', array (), false, false );
    // if ( $res ) {
    // $row = dbi_fetch_row ( $res );
    // if ( ! empty ( $row[0] ) ) {
    // $_SESSION['old_program_version'] = $row[0];
    // $_SESSION['install_file'] = 'upgrade_' . $row[0];
    // }
    // dbi_free_result ( $res );
    // }
    // We need to determine if this is a blank database.
    // This may be due to a manual table setup.
    $res = dbi_execute('SELECT COUNT( cal_value ) FROM webcal_config', array(), false, $show_all_errors);
    if ($res) {
        $row = dbi_fetch_row($res);
        if (isset($row[0]) && $row[0] == 0) {
            $_SESSION['blank_database'] = true;
        } else {
            // Make sure all existing values in config and pref tables are UPPERCASE.
            make_uppercase();
            // Clear db_cache. This will prevent looping when launching WebCalendar
            // if upgrading and WEBCAL_PROGRAM_VERSION is cached.
            if (!empty($settings['db_cachedir'])) {
                dbi_init_cache($settings['db_cachedir']);
            } else {
                if (!empty($settings['cachedir'])) {
                    dbi_init_cache($settings['cachedir']);
                }
            }
            // Delete existing WEBCAL_PROGRAM_VERSION number.
            dbi_execute('DELETE FROM webcal_config
        WHERE cal_setting = \'WEBCAL_PROGRAM_VERSION\'');
        }
        dbi_free_result($res);
        // Insert webcal_config values only if blank.
        db_load_config();
        // Check if an Admin account exists.
        $_SESSION['admin_exists'] = db_check_admin();
    }
    // Determine if old data has been converted to GMT.
    // This seems lke a good place to put this.
    $res = dbi_execute('SELECT cal_value FROM webcal_config
    WHERE cal_setting = \'WEBCAL_TZ_CONVERSION\'', array(), false, $show_all_errors);
    if ($res) {
        $row = dbi_fetch_row($res);
        dbi_free_result($res);
        // If not 'Y', prompt user to do conversion from server time to GMT time.
        if (!empty($row[0])) {
            $_SESSION['tz_conversion'] = $row[0];
        } else {
            // We'll test if any events even exist.
            $res = dbi_execute('SELECT COUNT( cal_id ) FROM webcal_entry ', array(), false, $show_all_errors);
            if ($res) {
                $row = dbi_fetch_row($res);
                dbi_free_result($res);
            }
            $_SESSION['tz_conversion'] = $row[0] > 0 ? 'NEEDED' : 'Y';
        }
        dbi_free_result($res);
    }
    // Don't show TZ conversion if blank database.
    if ($_SESSION['blank_database'] == true) {
        $_SESSION['tz_conversion'] = 'Y';
    }
    // Get existing server URL.
    // We could use the self-discvery value, but this may be a custom value.
    $res = dbi_execute('SELECT cal_value FROM webcal_config
    WHERE cal_setting = \'SERVER_URL\'', array(), false, $show_all_errors);
    if ($res) {
        $row = dbi_fetch_row($res);
        if (!empty($row[0]) && strlen($row[0])) {
            $_SESSION['server_url'] = $row[0];
        }
        dbi_free_result($res);
    }
    // Get existing application name.
    $res = dbi_execute('SELECT cal_value FROM webcal_config
    WHERE cal_setting = \'APPLICATION_NAME\'', array(), false, $show_all_errors);
    if ($res) {
        $row = dbi_fetch_row($res);
        if (!empty($row[0])) {
            $_SESSION['application_name'] = $row[0];
        }
        dbi_free_result($res);
    }
    // Enable warnings.
    show_errors(true);
}
function do_config($fileLoc)
{
    global $db_database, $db_host, $db_login, $db_password, $db_persistent, $db_type, $NONUSER_PREFIX, $phpdbiVerbose, $PROGRAM_DATE, $PROGRAM_NAME, $PROGRAM_URL, $PROGRAM_VERSION, $readonly, $run_mode, $settings, $single_user, $single_user_login, $TROUBLE_URL, $use_http_auth, $user_inc;
    $PROGRAM_VERSION = 'v1.2.7';
    $PROGRAM_DATE = '22 Feb 2013';
    $PROGRAM_NAME = 'WebCalendar ' . "{$PROGRAM_VERSION} ({$PROGRAM_DATE})";
    $PROGRAM_URL = 'http://www.k5n.us/webcalendar.php';
    $TROUBLE_URL = 'docs/WebCalendar-SysAdmin.html#trouble';
    // Open settings file to read.
    $settings = array();
    if (file_exists($fileLoc)) {
        $fd = @fopen($fileLoc, 'rb', true);
    }
    if (empty($fd) && defined('__WC_INCLUDEDIR')) {
        $fd = @fopen(__WC_INCLUDEDIR . '/settings.php', 'rb', true);
        if ($fd) {
            $fileLoc = __WC_INCLUDEDIR . '/settings.php';
        }
    }
    // If still empty.... use __FILE__.
    if (empty($fd)) {
        $testName = get_full_include_path("settings.php");
        $fd = @fopen($fileLoc, 'rb', true);
        if ($fd) {
            $fileLoc = $testName;
        }
    }
    if (empty($fd) || filesize($fileLoc) == 0) {
        // There is no settings.php file.
        // Redirect user to install page if it exists.
        if (file_exists('install/index.php')) {
            header('Location: install/index.php');
            exit;
        } else {
            die_miserable_death(translate('Could not find settings.php file...'));
        }
    }
    // We don't use fgets () since it seems to have problems with Mac-formatted
    // text files. Instead, we read in the entire file, and split the lines manually.
    $data = '';
    while (!feof($fd)) {
        $data .= fgets($fd, 4096);
    }
    fclose($fd);
    // Replace any combination of carriage return (\r) and new line (\n)
    // with a single new line.
    $data = preg_replace("/[\r\n]+/", "\n", $data);
    // Split the data into lines.
    $configLines = explode("\n", $data);
    for ($n = 0, $cnt = count($configLines); $n < $cnt; $n++) {
        $buffer = trim($configLines[$n], "\r\n ");
        if (preg_match('/^#|\\/\\*/', $buffer) || preg_match('/^<\\?/', $buffer) || preg_match('/^\\?>/', $buffer)) {
            // end PHP code
            continue;
        }
        if (preg_match('/(\\S+):\\s*(\\S+)/', $buffer, $matches)) {
            $settings[$matches[1]] = $matches[2];
        }
        // echo "settings $matches[1] => $matches[2]<br />";
    }
    $configLines = $data = '';
    // Extract db settings into global vars.
    $db_database = $settings['db_database'];
    $db_host = $settings['db_host'];
    $db_login = $settings['db_login'];
    $db_password = $settings['db_password'];
    $db_persistent = preg_match('/(1|yes|true|on)/i', $settings['db_persistent']) ? '1' : '0';
    $db_type = $settings['db_type'];
    // If no db settings, then user has likely started install but not yet
    // completed.  So, send them back to the install script.
    if (empty($db_type)) {
        if (file_exists('install/index.php')) {
            header('Location: install/index.php');
            exit;
        } else {
            die_miserable_death(translate('Incomplete settings.php file...'));
        }
    }
    // Use 'db_cachedir' if found, otherwise look for 'cachedir'.
    if (!empty($settings['db_cachedir'])) {
        dbi_init_cache($settings['db_cachedir']);
    } else {
        if (!empty($settings['cachedir'])) {
            dbi_init_cache($settings['cachedir']);
        }
    }
    if (!empty($settings['db_debug']) && preg_match('/(1|true|yes|enable|on)/i', $settings['db_debug'])) {
        dbi_set_debug(true);
    }
    foreach (array('db_type', 'db_host', 'db_login', 'db_password') as $s) {
        if (empty($settings[$s])) {
            die_miserable_death(str_replace('XXX', $s, translate('Could not find XXX defined in...')));
        }
    }
    // Allow special settings of 'none' in some settings[] values.
    // This can be used for db servers not using TCP port for connection.
    $db_host = $db_host == 'none' ? '' : $db_host;
    $db_password = $db_password == 'none' ? '' : $db_password;
    $readonly = preg_match('/(1|yes|true|on)/i', $settings['readonly']) ? 'Y' : 'N';
    if (empty($settings['mode'])) {
        $settings['mode'] = 'prod';
    }
    $run_mode = preg_match('/(dev)/i', $settings['mode']) ? 'dev' : 'prod';
    $phpdbiVerbose = $run_mode == 'dev';
    $single_user = preg_match('/(1|yes|true|on)/i', $settings['single_user']) ? 'Y' : 'N';
    if ($single_user == 'Y') {
        $single_user_login = $settings['single_user_login'];
    }
    if ($single_user == 'Y' && empty($single_user_login)) {
        die_miserable_death(str_replace('XXX', 'single_user_login', translate('You must define XXX in')));
    }
    $use_http_auth = preg_match('/(1|yes|true|on)/i', $settings['use_http_auth']) ? true : false;
    // Type of user authentication.
    $user_inc = $settings['user_inc'];
    // If sqlite, the db file is in the include directory
    if ($db_type == 'sqlite') {
        $db_database = get_full_include_path($db_database);
    }
    // Check the current installation version.
    // Redirect user to install page if it is different from stored value.
    // This will prevent running WebCalendar until UPGRADING.html has been
    // read and required upgrade actions completed.
    $c = @dbi_connect($db_host, $db_login, $db_password, $db_database, false);
    if ($c) {
        $rows = dbi_get_cached_rows('SELECT cal_value FROM webcal_config
       WHERE cal_setting = \'WEBCAL_PROGRAM_VERSION\'');
        if (!$rows) {
            // &amp; does not work here...leave it as &.
            header('Location: install/index.php?action=mismatch&version=UNKNOWN');
            exit;
        } else {
            $row = $rows[0];
            if (empty($row) || $row[0] != $PROGRAM_VERSION) {
                // &amp; does not work here...leave it as &.
                header('Location: install/index.php?action=mismatch&version=' . (empty($row) ? 'UNKNOWN' : $row[0]));
                exit;
            }
        }
        dbi_close($c);
    } else {
        // Must mean we don't have a settings.php file.
        // NOTE: if we get a connect error when running send_reminders.php,
        // we may want to show that error message here.
        // &amp; does not work here...leave it as &.
        header('Location: install/index.php?action=mismatch&version=UNKNOWN');
        exit;
    }
    // We can add extra 'nonuser' calendars such as a holiday, corporate,
    // departmental, etc. We need a unique prefix for these calendars
    // so we don't get them mixed up with real logins. This prefix should be
    // a maximum of 5 characters and should NOT change once set!
    $NONUSER_PREFIX = '_NUC_';
    if ($single_user != 'Y') {
        $single_user_login = '';
    }
}