ktk_mediaserve/modules/inactive/SimpleDB.php

53 lines
1.4 KiB
PHP

<?php
namespace ktk\MusicServe;
class SimpleDB {
static private $data_file = "sdb.ini";
static private $data = array();
// CORE
static public function onInit() {
if (!file_exists(self::$data_file)) {
if ($fp = fopen(self::$data_file, 'w')) {
fwrite($fp, "");
fclose($fp);
} else {
MusicServe::reportError(__CLASS__, __FUNCTION__, "Could not create \"".self::$data_file."\", please check your permissions!");
}
return NULL;
}
self::$data = parse_ini_file(self::$data_file, true);
}
static public function onClose() {
if ($fp = fopen(self::$data_file, 'w')) {
self::writeData_r($fp, self::$data);
fclose($fp);
} else {
MusicServe::reportError(__CLASS__, __FUNCTION__, "Could not write to \"".self::$data_file."\", please check your permissions!");
return NULL;
}
}
// ACCESS
static public function getData($var) {
if (self::$data[$var]) {
return self::$data[$var];
} else {
return NULL;
}
}
static public function storeData($var, $value) {
self::$data[$var] = $value;
}
// INTERNAL
static private function writeData_r($handle, $data) {
foreach ($data as $key=>$value) {
if (is_array($value)) {
fwrite($handle, '['.$key.']'.PHP_EOL);
self::writeData_r($handle, $value);
} else {
fwrite($handle, $key.'='.$value.PHP_EOL);
}
}
}
}
?>