diff options
author | Jakob Sack <kde@jakobsack.de> | 2011-04-08 16:54:12 +0200 |
---|---|---|
committer | Jakob Sack <kde@jakobsack.de> | 2011-04-08 16:54:12 +0200 |
commit | f66d3ab208e056bac699fef17c6e9ad6257f368b (patch) | |
tree | 1bbbcc7b61ce93030742c6a0858d977bec37fd48 /lib | |
parent | 908e377246cd770b7a3f9f2f4d929e29ffb22f25 (diff) | |
download | nextcloud-server-f66d3ab208e056bac699fef17c6e9ad6257f368b.tar.gz nextcloud-server-f66d3ab208e056bac699fef17c6e9ad6257f368b.zip |
Implementation of OC_APPCONFIG, OC_CONFIG and OC_PREFERENCES
Diffstat (limited to 'lib')
-rw-r--r-- | lib/appconfig.php | 61 | ||||
-rw-r--r-- | lib/config.php | 95 | ||||
-rw-r--r-- | lib/preferences.php | 83 |
3 files changed, 210 insertions, 29 deletions
diff --git a/lib/appconfig.php b/lib/appconfig.php index e8c6960e674..3c37a9d77d5 100644 --- a/lib/appconfig.php +++ b/lib/appconfig.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; } } diff --git a/lib/config.php b/lib/config.php index 3e660a5fb55..1b7783403ca 100644 --- a/lib/config.php +++ b/lib/config.php @@ -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; } } diff --git a/lib/preferences.php b/lib/preferences.php index a7a2911cac7..0bc4092de4b 100644 --- a/lib/preferences.php +++ b/lib/preferences.php @@ -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; } } |