<?php

// This is basic include file for one-purpose scripts that need to use CBS's
// database, data etc. This launches CBS, loads configuration and libraries
// and connects to the database. Include it in Your script.

header('X-Cbs-Launcher: CbsInclude');

$cbs_path      = __DIR__;
$cbs_datapath  = realpath('/var/www/vhosts/pubcbs.orcave.com/htdocs/cbsdev/data');
$cbs_opts_path = realpath('/var/www/vhosts/pubcbs.orcave.com/dev');

require($cbs_path.'/Cbs.php');

// call this before starting the work
function cbsInclude_init(&$st) {

    $st = new cbsState;
    cbsInitState($st);
    CbsServices($st)->setCbsOptions($GLOBALS['cbsOptions']);

    // read settigns
    $st->SettingsFile = CbsServices($st)->getCbsOptions()->cbs_settings_file;
    if (($settings = @file_get_contents($st->SettingsFile)) === false)
        return 'no settings file';

    $st->Settings = cbsSettingsCheck($st, unserialize($settings));
    
    // connect to Postgres...
    if ($err = db_connect($st, $GLOBALS['cbs_db_addr'], $GLOBALS['cbs_db_name'], $GLOBALS['cbs_db_user'], $GLOBALS['cbs_db_pass'], true))
        return 'db connect: '.$err;

    // ok
    return null;
}

// call this upon any kind of exit
function cbsInclude_close(cbsState $st) {

    db_close($st);
}

// go on, userman... :-)
