Implementation of OC_APPCONFIG, OC_CONFIG and OC_PREFERENCES

remotes/origin/stable
Jakob Sack 14 years ago
parent 908e377246
commit f66d3ab208
  1. 61
      lib/appconfig.php
  2. 95
      lib/config.php
  3. 83
      lib/preferences.php

@ -46,8 +46,16 @@ class OC_APPCONFIG{
* entry in the appconfig table.
*/
public static function getApps(){
// TODO: write function
return array();
// No magic in here!
$query = OC_DB::prepare( 'SELECT DISTINCT( `appid` ) FROM `*PREFIX*appconfig`' );
$result = $query->execute();
$apps = array();
while( $result->fetchRow()){
$apps[] = $row["appid"];
}
return $apps;
}
/**
@ -59,8 +67,16 @@ class OC_APPCONFIG{
* not returned.
*/
public static function getKeys( $app ){
// TODO: write function
return array();
// No magic in here as well
$query = OC_DB::prepare( 'SELECT `key` FROM `*PREFIX*appconfig` WHERE `appid` = ?' );
$result = $query->execute( $app );
$keys = array();
while( $result->fetchRow()){
$keys[] = $row["key"];
}
return $keys;
}
/**
@ -74,8 +90,17 @@ class OC_APPCONFIG{
* not exist the default value will be returnes
*/
public static function getValue( $app, $key, $default = null ){
// OC_DB::query( $query);
return $default;
// At least some magic in here :-)
$query = OC_DB::prepare( 'SELECT `value` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `key` = ?' );
$result = $query->execute( $app, $key );
if( !$result->numRows()){
return $default;
}
$row = $result->fetchRow();
return $row["value"];
}
/**
@ -88,8 +113,18 @@ class OC_APPCONFIG{
* Sets a value. If the key did not exist before it will be created.
*/
public static function setValue( $app, $key, $value ){
// TODO: write function
return true;
// Does the key exist? yes: update. No: insert
$exists = self::getValue( $app, $key, null );
// null: does not exist
if( is_null( $exists )){
$query = OC_DB::prepare( 'INSERT INTO `*PREFIX*appconfig` ( `appid`, `key`, `value` ) VALUES( ?, ?, ? )' );
$query->execute( $app, $key, $value );
}
else{
$query = OC_DB::prepare( 'UPDATE `*PREFIX*appconfig` SET `value` = ? WHERE `appid` = ? AND `key` = ?' );
$query->execute( $value, $app, $key );
}
}
/**
@ -101,7 +136,10 @@ class OC_APPCONFIG{
* Deletes a key.
*/
public static function deleteKey( $app, $key ){
// TODO: write function
// Boring!
$query = OC_DB::prepare( 'DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `key` = ?' );
$query->execute( $app, $key );
return true;
}
@ -113,7 +151,10 @@ class OC_APPCONFIG{
* Removes all keys in appconfig belonging to the app.
*/
public static function deleteApp( $app ){
// TODO: write function
// Nothing special
$query = OC_DB::prepare( 'DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?' );
$query->execute( $app );
return true;
}
}

@ -53,8 +53,9 @@ class OC_CONFIG{
* does not return the values.
*/
public static function getKeys(){
// TODO: write function
return array();
self::readData();
return array_keys( self::$cache );
}
/**
@ -67,7 +68,12 @@ class OC_CONFIG{
* $default will be returned.
*/
public static function getValue( $key, $default = null ){
// TODO: write function
self::readData();
if( array_key_exists( $key, self::$cache )){
return self::$cache[$key];
}
return $default;
}
@ -81,7 +87,14 @@ class OC_CONFIG{
* not be written, false will be returned.
*/
public static function setValue( $key, $value ){
// TODO: write function
self::readData();
// Add change
self::$cache[$key] = $value;
// Write changes
self::writeData();
return true;
}
@ -94,7 +107,79 @@ class OC_CONFIG{
* write access to config.php, the function will return false.
*/
public static function deleteKey( $key ){
// TODO: write function
self::readData();
if( array_key_exists( $key, self::$cache )){
// Delete key from cache
unset( self::$cache[$key] );
// Write changes
self::writeData();
}
return true;
}
/**
* @brief Loads the config file
* @returns true/false
*
* Reads the config file and saves it to the cache
*/
private static function readData( $key ){
if( !self::$init ){
return true;
}
global $SERVERROOT;
if( !file_exists( "$SERVERROOT/config/config.php" )){
return false;
}
// Include the file, save the data from $CONFIG
include( "$SERVERROOT/config/config.php" );
self::$cache = $CONFIG;
// We cached everything
self::$init = true;
return true;
}
/**
* @brief Writes the config file
* @returns true/false
*
* Saves the config to the config file.
*
* Known flaws: Strings are not escaped properly
*/
public static function writeData( $key ){
// We need the serverroot path
global $SERVERROOT;
// Create a php file ...
$content = "<?php\n\$CONFIG = array(\n";
foreach( self::$cache as $key => $value ){
if( is_bool( $value )){
$value = $value ? 'true' : 'false';
$content .= "\"$key\" => $value,\n";
}
elseif( is_numeric( $value )){
$content .= "\"$key\" => $value,\n";
}
else{
$value = str_replace( "'", "\\'", $value );
$configContent .= "\"$key\" => '$value',\n";
}
}
$content .= ");\n?>\n";
// Write the file
file_put_contents( "$SERVERROOT/config/config.php", $content );
return true;
}
}

@ -46,8 +46,16 @@ class OC_PREFERENCES{
* in the preferences table.
*/
public static function getUsers(){
// TODO: write function
return array();
// No need for more comments
$query = OC_DB::prepare( 'SELECT DISTINCT( `userid` ) FROM `*PREFIX*preferences`' );
$result = $query->execute();
$users = array();
while( $result->fetchRow()){
$users[] = $row["userid"];
}
return $users;
}
/**
@ -59,8 +67,16 @@ class OC_PREFERENCES{
* one entry in the preferences table.
*/
public static function getApps( $user ){
// TODO: write function
return array();
// No need for more comments
$query = OC_DB::prepare( 'SELECT DISTINCT( `appid` ) FROM `*PREFIX*preferences` WHERE `userid` = ?' );
$result = $query->execute( $user );
$apps = array();
while( $result->fetchRow()){
$apps[] = $row["appid"];
}
return $apps;
}
/**
@ -73,8 +89,16 @@ class OC_PREFERENCES{
* values are not returned.
*/
public static function getKeys( $user, $app ){
// TODO: write function
return array();
// No need for more comments
$query = OC_DB::prepare( 'SELECT `key` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?' );
$result = $query->execute( $user, $app );
$keys = array();
while( $result->fetchRow()){
$keys[] = $row["key"];
}
return $keys;
}
/**
@ -89,8 +113,17 @@ class OC_PREFERENCES{
* not exist the default value will be returnes
*/
public static function getValue( $user, $app, $key, $default = null ){
// OC_DB::query( $query);
return $default;
// Try to fetch the value, return default if not exists.
$query = OC_DB::prepare( 'SELECT `value` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `key` = ?' );
$result = $query->execute( $user, $app, $key );
if( !$result->numRows()){
return $default;
}
$row = $result->fetchRow();
return $row["value"];
}
/**
@ -105,8 +138,18 @@ class OC_PREFERENCES{
* will be added automagically.
*/
public static function setValue( $user, $app, $key, $value ){
// TODO: write function
return true;
// Check if the key does exist
$exists = self::getValue( $user, $app, $key, null );
// null: does not exist. Insert.
if( is_null( $exists )){
$query = OC_DB::prepare( 'INSERT INTO `*PREFIX*preferences` ( `userid`, `appid`, `key`, `value` ) VALUES( ?, ?, ?, ? )' );
$query->execute( $user, $app, $key, $value );
}
else{
$query = OC_DB::prepare( 'UPDATE `*PREFIX*preferences` SET `value` = ? WHERE `userid` = ? AND `appid` = ? AND `key` = ?' );
$query->execute( $value, $user, $app, $key );
}
}
/**
@ -119,7 +162,10 @@ class OC_PREFERENCES{
* Deletes a key.
*/
public static function deleteKey( $user, $app, $key ){
// TODO: write function
// No need for more comments
$query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `key` = ?' );
$result = $query->execute( $user, $app, $key );
return true;
}
@ -132,7 +178,10 @@ class OC_PREFERENCES{
* Removes all keys in appconfig belonging to the app and the user.
*/
public static function deleteApp( $user, $app ){
// TODO: write function
// No need for more comments
$query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?' );
$result = $query->execute( $user, $app );
return true;
}
@ -144,7 +193,10 @@ class OC_PREFERENCES{
* Removes all keys in appconfig belonging to the user.
*/
public static function deleteUser( $user ){
// TODO: write function
// No need for more comments
$query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?' );
$result = $query->execute( $user );
return true;
}
@ -156,7 +208,10 @@ class OC_PREFERENCES{
* Removes all keys in preferences belonging to the app.
*/
public static function deleteAppFromAllUsers( $app ){
// TODO: write function
// No need for more comments
$query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `appid` = ?' );
$result = $query->execute( $app );
return true;
}
}

Loading…
Cancel
Save