<?php
/**
* @author Edin.O
* @copyright 2007
*/
/*
$r = new Registry();
$r->CreateSection('system/shoutbox/main/');
$r->WriteKey ('allowGuestPost',1);
$r->WriteKey ('minimumTime', 20);
$r->WriteKey ('badwords', 20);
*/
define ('HKEY_ROOT',0);
class Registry{
var $rootKey;
var $section;
var $section_id;
/**
* Read settings from registry
* Simple access to registry values, use if you don't have much keys to read
*/
function ReadSettings($section, $key, $default=""){
$Reg = new Registry();
$Reg->OpenSection($section);
return $Reg->ReadKey($key,$default);
}
/**
* Write settings to registry
* Simple access to registry values, use if you don't have much keys to write
*/
function WriteSettings($section, $key, $value){
$Reg = new Registry();
$Reg->CreateSection($section);
$Reg->WriteKey($key,$value);
}
function __construct ($rootKey = HKEY_ROOT){
$this->rootKey = $rootKey;
$this->section_id = $this->rootKey;
}
function GetKeyByID($id){
$r = Select ('*', SC_TABLE_REGISTRY, "id=$id");
$rs = new Recordset($r);
return $rs->Next();
}
function SetValueByID($id, $value)
{
if ($id == "") return false;
$id = (int)$id;
$f['value'] = $value;
Update(SC_TABLE_REGISTRY, $f, "id=$id");
return true;
}
/* returns array with with key's values, array key is the key name */
function GetKeys(){
$id = $this->section_id;
$r = Select('id, name,value', SC_TABLE_REGISTRY, "parent={$id}" );
$rs = CreateRecordset($r);
$keys = array();
while ($row = $rs->Next())
{
$key = strtolower($row['name']);
$value = $row['value'];
$keys[$key] = $value;
}
return $keys;
}
/* returns array with with keys names, array key is the id of registry key */
function GetKeysNames(){
$id = $this->section_id;
$r = Select('id, name', SC_TABLE_REGISTRY, "parent={$id}" );
$rs = new Recordset($r);
$keys = array();
while ($row = $rs->Next())
{
$key = $row['id'];
$value = $row['name'];
$keys[$key] = $value;
}
return $keys;
}
function SubKeysCount($id){
$c = SelectValue ('Count(*)', SC_TABLE_REGISTRY , "parent = $id" );
return $c;
}
function DeleteKey($key){
$key = strtolower($key);
if ( is_numeric($this->section_id) ==FALSE ){
//return FALSE;
}
$id = $this->section_id;
if (is_numeric($key) == FALSE ){
$id = SelectValue('id', SC_TABLE_REGISTRY, "parent=$id AND (name like '$key')");
}else{
$id = $key;
}
if ($id > 0){
$c = $this->SubKeysCount($id);
if ($c == 0)
{
Delete(SC_TABLE_REGISTRY, "id = $id");
}else{
//Load all subkeys using Nested class
$n = new Nested(SC_TABLE_REGISTRY);
$n->FieldPosition ='id';
$n->FieldParent ='parent';
$n->FieldID ='id';
//Load as flat list
$n->LoadNodesFlat($id);
//Delete all loaded keys
if (is_array($n->Items)){
foreach ($n->Items as $Item){
$idk = $n->ItemId($Item);
Delete(SC_TABLE_REGISTRY, "id = $idk");
}
}
Delete(SC_TABLE_REGISTRY, "id = $id");
}
}
}
function WriteKey( $key, $value )
{
if ($this->section_id < 1){
return FALSE;
}
$f['parent'] = $this->section_id;
$f['name'] = $key;
$f['value'] = $value;
$id = $this->section_id;
$id = SelectValue('id', SC_TABLE_REGISTRY, "parent=$id AND (name like '$key')") ;
if ($id> 0 ){
$f['id'] = $id ;
Update(SC_TABLE_REGISTRY, $f, "id = $id");
}else{
Insert(SC_TABLE_REGISTRY, $f);
}
}
function ReadKey($key, $default = '')
{
$id = $this->section_id;
if ($id < 1) return $default;
$value = SelectValue('value', SC_TABLE_REGISTRY, "(parent = $id) and name like '{$key}'");
if (trim($value) !=""){
return $value;
}else{
return $default;
}
}
function OpenSection($section)
{
if (trim($section) == "") return false;
$keys = split('[/\]', $section );
if (is_array($keys)){
$id = $this->rootKey;
foreach ($keys as $key){
$key = SqlEscapeString($key);
if ( (string)$id != "")
{
$newid = SelectValue('id' , SC_TABLE_REGISTRY, "(parent = $id) And (name LIKE '$key')");
if ($newid < 1) {
return false;
}
$id = $newid ;
}else{
return false;
}
}
$this->section_id = $id ;
return $id ;
}else{
return false;
}
}
function CreateSection($section){
if (trim($section) == "") return false;
$keys = split('[/\]', $section );
if (is_array($keys)){
$id = $this->rootKey;
foreach ($keys as $key){
$key = SqlEscapeString($key);
$newid = SelectValue('id' , SC_TABLE_REGISTRY, "(parent = $id) And (name LIKE '$key') ");
if ($newid < 1) {
$newid = $this->CreateKey($id, $key , "");
}
$id = $newid ;
}
$this->section_id = $id;
return $id ;
}else{
return false;
}
}
function CreateKey($sid, $key, $value){
if ($sid < 1 ){
//Logs::InsertLog("Registry","Section is not open (key = $key)");
}
$f['parent'] =$sid;
$f['name'] = $key;
$f['value'] = $value;
return Insert(SC_TABLE_REGISTRY, $f);
}
function KeyExists($key){
if ($this->section_id < 1 ){
die('Registry::Section is not open.');
}
$key = SqlEscapeString($key);
$r = SelectValue('id', SC_TABLE_REGISTRY, " key LIKE '$key' And ( id = {$this->section_id} )" );
if ($r > 0 ){
return $r ;
}else{
return FALSE;
}
}
};
?>