appconfig.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @author Jakob Sack
  7. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /*
  24. *
  25. * The following SQL statement is just a help for developers and will not be
  26. * executed!
  27. *
  28. * CREATE TABLE `appconfig` (
  29. * `appid` VARCHAR( 255 ) NOT NULL ,
  30. * `configkey` VARCHAR( 255 ) NOT NULL ,
  31. * `configvalue` VARCHAR( 255 ) NOT NULL
  32. * )
  33. *
  34. */
  35. /**
  36. * This class provides an easy way for apps to store config values in the
  37. * database.
  38. */
  39. class OC_Appconfig{
  40. /**
  41. * @brief Get all apps using the config
  42. * @returns array with app ids
  43. *
  44. * This function returns a list of all apps that have at least one
  45. * entry in the appconfig table.
  46. */
  47. public static function getApps(){
  48. // No magic in here!
  49. $query = OC_DB::prepare( 'SELECT DISTINCT `appid` FROM `*PREFIX*appconfig`' );
  50. $result = $query->execute();
  51. $apps = array();
  52. while( $row = $result->fetchRow()){
  53. $apps[] = $row["appid"];
  54. }
  55. return $apps;
  56. }
  57. /**
  58. * @brief Get the available keys for an app
  59. * @param $app the app we are looking for
  60. * @returns array with key names
  61. *
  62. * This function gets all keys of an app. Please note that the values are
  63. * not returned.
  64. */
  65. public static function getKeys( $app ){
  66. // No magic in here as well
  67. $query = OC_DB::prepare( 'SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?' );
  68. $result = $query->execute( array( $app ));
  69. $keys = array();
  70. while( $row = $result->fetchRow()){
  71. $keys[] = $row["configkey"];
  72. }
  73. return $keys;
  74. }
  75. /**
  76. * @brief Gets the config value
  77. * @param $app app
  78. * @param $key key
  79. * @param $default = null, default value if the key does not exist
  80. * @returns the value or $default
  81. *
  82. * This function gets a value from the appconfig table. If the key does
  83. * not exist the default value will be returnes
  84. */
  85. public static function getValue( $app, $key, $default = null ){
  86. // At least some magic in here :-)
  87. $query = OC_DB::prepare( 'SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?' );
  88. $result = $query->execute( array( $app, $key ));
  89. $row = $result->fetchRow();
  90. if($row){
  91. return $row["configvalue"];
  92. }else{
  93. return $default;
  94. }
  95. }
  96. /**
  97. * @brief check if a key is set in the appconfig
  98. * @param string $app
  99. * @param string $key
  100. * @return bool
  101. */
  102. public static function hasKey($app,$key){
  103. $exists = self::getKeys( $app );
  104. return in_array( $key, $exists );
  105. }
  106. /**
  107. * @brief sets a value in the appconfig
  108. * @param $app app
  109. * @param $key key
  110. * @param $value value
  111. * @returns true/false
  112. *
  113. * Sets a value. If the key did not exist before it will be created.
  114. */
  115. public static function setValue( $app, $key, $value ){
  116. // Does the key exist? yes: update. No: insert
  117. if(! self::hasKey($app,$key)){
  118. $query = OC_DB::prepare( 'INSERT INTO `*PREFIX*appconfig` ( `appid`, `configkey`, `configvalue` ) VALUES( ?, ?, ? )' );
  119. $query->execute( array( $app, $key, $value ));
  120. }
  121. else{
  122. $query = OC_DB::prepare( 'UPDATE `*PREFIX*appconfig` SET `configvalue` = ? WHERE `appid` = ? AND `configkey` = ?' );
  123. $query->execute( array( $value, $app, $key ));
  124. }
  125. }
  126. /**
  127. * @brief Deletes a key
  128. * @param $app app
  129. * @param $key key
  130. * @returns true/false
  131. *
  132. * Deletes a key.
  133. */
  134. public static function deleteKey( $app, $key ){
  135. // Boring!
  136. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?' );
  137. $query->execute( array( $app, $key ));
  138. return true;
  139. }
  140. /**
  141. * @brief Remove app from appconfig
  142. * @param $app app
  143. * @returns true/false
  144. *
  145. * Removes all keys in appconfig belonging to the app.
  146. */
  147. public static function deleteApp( $app ){
  148. // Nothing special
  149. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?' );
  150. $query->execute( array( $app ));
  151. return true;
  152. }
  153. /**
  154. * get multiply values, either the app or key can be used as wildcard by setting it to false
  155. * @param app
  156. * @param key
  157. * @return array
  158. */
  159. public static function getValues($app,$key){
  160. if($app!==false and $key!==false){
  161. return false;
  162. }
  163. $fields='`configvalue`';
  164. $where='WHERE';
  165. $params=array();
  166. if($app!==false){
  167. $fields.=', `configkey`';
  168. $where.=' `appid` = ?';
  169. $params[]=$app;
  170. $key='configkey';
  171. }else{
  172. $fields.=', `appid`';
  173. $where.=' `configkey` = ?';
  174. $params[]=$key;
  175. $key='appid';
  176. }
  177. $queryString='SELECT '.$fields.' FROM `*PREFIX*appconfig` '.$where;
  178. $query=OC_DB::prepare($queryString);
  179. $result=$query->execute($params);
  180. $values=array();
  181. while($row=$result->fetchRow()){
  182. $values[$row[$key]]=$row['configvalue'];
  183. }
  184. return $values;
  185. }
  186. }