<? // QSL - Quick Save / Load Database System

// Note: This functions library uses one or two
// functions from the PHPmyGallery functions library.


###################################################
### Database config vars (used to connect etc.) ###
###################################################

$fsdb_conf['path2db']       = $admindir.'db/';
$fsdb_conf['report_errors'] = false;
$fsdb_conf['tables'] = array(
    'db/gallerysettings/',
    'db/mainsetup/',
    'db/picsdescriptions/',
    'db/plugins/',
    'db/statistics/',
);


##################################################
### Functions to communicate with the database ###
##################################################

//  Global function                      _________________________                                 
//\_____________________________________/ Peter Krebs (PeKa)      \_____________________________   
///  allocateData                       \              2004-05-01 /                             \  
//                                       ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ                               \ 
// Looks up if a database table exists
// returns true if exists, else returns false
// 
// $DB_table           - Table from the database
// $DB_request         - Request ID in form of a string (should be descriptive)
// $DB_arrInstructions - Further instructions
function allocateData($DB_table,$DB_request = 'data',$DB_arrInstructions = array()) {
    global $fsdb_conf;
    
    $fullFilename = $fsdb_conf['path2db'].$DB_table.'/'.$DB_request.'.php';
    
    $file_exists = (file_exists($fullFilename) && !is_dir($fullFilename));
    if ($file_exists) { return true;  }
    else              { return false; }
} // allocateData();


//  Global function                      _________________________                                 
//\_____________________________________/ Peter Krebs (PeKa)      \_____________________________   
///  tableEmpty                         \              2004-05-01 /                             \  
//                                       ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ                               \ 
// Looks up if a database table contains data,
// returns true if table is empty
// returns false if it contains data
// 
// $DB_table           - Table from the database
// $DB_arrInstructions - Further instructions
function tableEmpty($DB_table,$DB_arrInstructions = array()) {
    global $fsdb_conf;
    
    $dirObj = dir($fsdb_conf['path2db'].$DB_table.'/');
    while ($filename = $dirObj->read()) {
        if ($filename!='.'&&$filename!='..'&&$filename[0]!='_') { // ignore directories and _empty.txt files
            return false;
        } // if
    } // while
    
    // No file found 
    return true;
}


//  Global function                      _________________________                                 
//\_____________________________________/ Peter Krebs (PeKa)      \_____________________________   
///  getData                            \              2004-05-01 /                             \  
//                                       ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ                               \ 
// Returns an array of the requested data (if only one entry is returned, too)
// $DB_table           - Table from the database
// $DB_request         - Request ID in form of a string (should be descriptive)
// $DB_arrInstructions - Further instructions
function getData($DB_table,$DB_request = 'data',$DB_arrInstructions = array()) {
    global $fsdb_conf;
    @include($fsdb_conf['path2db'].$DB_table.'/'.$DB_request.'.php');
    
    if(isset($data)) {
        return $data;
    }
    else {
        if($fsdb_conf['report_errors'])
            echo 'Requested data \''.$DB_request.'\' was not found in table \''.$DB_table.'\'.<br />';
        
        return array();
    }
} // getData();


//  Global function                      _________________________                                 
//\_____________________________________/ Peter Krebs (PeKa)      \_____________________________   
///  putData                            \              2004-05-01 /                             \  
//                                       ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ                               \ 
// Saves an array into a table within the database
// Return values  0: OK  -1: is_writable returned false
//               -2: fopen() failed  -3: fwrite() failed
// $data               - Mixed array of data which will be written to the database
// $DB_table           - Table from the database to which the array will be saved
// $DB_request         - Request ID in form of a string (should be descriptive)
// $DB_arrInstructions - Further instructions
function putData($data,$DB_table,$DB_request = 'data',$DB_arrInstructions = array()) {
    global $fsdb_conf;
    $t = '    '; // tab
    $nl= "\n";   // newline
    
    $filename = $fsdb_conf['path2db'].$DB_table.'/'.$DB_request.'.php';
    
    if (is_string($data)) {
        $data_varcode = '$data = \''.addslashes($data).'\';';
    } // if
    elseif (is_numeric($data)) {
        $data_varcode = '$data = '.$data.';';
    } // elseif
    elseif (is_array($data)) {
        $data_varcode = arr2php($data,'data');
    } // elseif
    else {
        $data_varcode = 'Operation halted because of bad calling of putData (only Strings, Numbers and Arrays allowed). Prevented data loss but did not save anything. Check your variable names.<br><i>Exiting...</i>';
        echo($data_varcode); exit;
    } // els
    
    $PHPcode = '<? // PHPmyGallery - filesystem DB - data file'.$nl.$nl.$t.$data_varcode.$nl.'?>';
    
    if(!file_exists($filename) || is_writable($filename)) {
        $fd = fopen($filename,'w+'); // open file for writing PHP code
        if($fd) {
            $result = fwrite($fd,$PHPcode); // write PHP code
            fclose($fd); // close file
            
            if(!$result)
                return -3;
            else
                return 0;
        } // if
        else {
            return -2;
        } // else
    } // if
    else {
        return -1;
    } // else
    
    return true;
} // putData();


//  Global function                      _________________________                                 
//\_____________________________________/ Peter Krebs (PeKa)      \_____________________________   
///  delData                            \              2005-07-07 /                             \  
//                                       ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ                               \
//  Sends a database entry to hell.
// Return values:  0: OK  -  -1: Data not found (wrong table, misspelled name?)
// $DB_table   - Table from the database where the data is
// $DB_request - Request ID in form of a string (should be descriptive)
function delData($DB_table,$DB_request = 'data') {
    global $fsdb_conf;
    
    $filename = $fsdb_conf['path2db'].$DB_table.'/'.$DB_request.'.php';
    
    if (file_exists($filename)) {
        @unlink($filename);
        return 0;
    } // if
    else {
        return -1;
    } // else
} // delData();


###########################
### Connect to database ###
###########################

// 
// Not needed for file system
// 


###################################
### Initiate commonly used vars ###
###################################

// 
// None used yet
// 

?>