You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

appconfig.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * @return 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 string $app the app we are looking for
  60. * @return 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 string $app app
  78. * @param string $key key
  79. * @param string $default = null, default value if the key does not exist
  80. * @return string 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 returned
  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`'
  88. .' WHERE `appid` = ? AND `configkey` = ?' );
  89. $result = $query->execute( array( $app, $key ));
  90. $row = $result->fetchRow();
  91. if($row) {
  92. return $row["configvalue"];
  93. }else{
  94. return $default;
  95. }
  96. }
  97. /**
  98. * @brief check if a key is set in the appconfig
  99. * @param string $app
  100. * @param string $key
  101. * @return bool
  102. */
  103. public static function hasKey($app, $key) {
  104. $exists = self::getKeys( $app );
  105. return in_array( $key, $exists );
  106. }
  107. /**
  108. * @brief sets a value in the appconfig
  109. * @param string $app app
  110. * @param string $key key
  111. * @param string $value value
  112. * @return bool
  113. *
  114. * Sets a value. If the key did not exist before it will be created.
  115. */
  116. public static function setValue( $app, $key, $value ) {
  117. // Does the key exist? yes: update. No: insert
  118. if(! self::hasKey($app, $key)) {
  119. $query = OC_DB::prepare( 'INSERT INTO `*PREFIX*appconfig` ( `appid`, `configkey`, `configvalue` )'
  120. .' VALUES( ?, ?, ? )' );
  121. $query->execute( array( $app, $key, $value ));
  122. }
  123. else{
  124. $query = OC_DB::prepare( 'UPDATE `*PREFIX*appconfig` SET `configvalue` = ?'
  125. .' WHERE `appid` = ? AND `configkey` = ?' );
  126. $query->execute( array( $value, $app, $key ));
  127. }
  128. }
  129. /**
  130. * @brief Deletes a key
  131. * @param string $app app
  132. * @param string $key key
  133. * @return bool
  134. *
  135. * Deletes a key.
  136. */
  137. public static function deleteKey( $app, $key ) {
  138. // Boring!
  139. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?' );
  140. $query->execute( array( $app, $key ));
  141. return true;
  142. }
  143. /**
  144. * @brief Remove app from appconfig
  145. * @param string $app app
  146. * @return bool
  147. *
  148. * Removes all keys in appconfig belonging to the app.
  149. */
  150. public static function deleteApp( $app ) {
  151. // Nothing special
  152. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?' );
  153. $query->execute( array( $app ));
  154. return true;
  155. }
  156. /**
  157. * get multiply values, either the app or key can be used as wildcard by setting it to false
  158. * @param app
  159. * @param key
  160. * @return array
  161. */
  162. public static function getValues($app, $key) {
  163. if($app!==false and $key!==false) {
  164. return false;
  165. }
  166. $fields='`configvalue`';
  167. $where='WHERE';
  168. $params=array();
  169. if($app!==false) {
  170. $fields.=', `configkey`';
  171. $where.=' `appid` = ?';
  172. $params[]=$app;
  173. $key='configkey';
  174. }else{
  175. $fields.=', `appid`';
  176. $where.=' `configkey` = ?';
  177. $params[]=$key;
  178. $key='appid';
  179. }
  180. $queryString='SELECT '.$fields.' FROM `*PREFIX*appconfig` '.$where;
  181. $query=OC_DB::prepare($queryString);
  182. $result=$query->execute($params);
  183. $values=array();
  184. while($row=$result->fetchRow()) {
  185. $values[$row[$key]]=$row['configvalue'];
  186. }
  187. return $values;
  188. }
  189. }