preferences.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 `preferences` (
  29. * `userid` VARCHAR( 255 ) NOT NULL ,
  30. * `appid` VARCHAR( 255 ) NOT NULL ,
  31. * `configkey` VARCHAR( 255 ) NOT NULL ,
  32. * `configvalue` VARCHAR( 255 ) NOT NULL
  33. * )
  34. *
  35. */
  36. /**
  37. * This class provides an easy way for storing user preferences.
  38. */
  39. class OC_Preferences{
  40. /**
  41. * @brief Get all users using the preferences
  42. * @return array with user ids
  43. *
  44. * This function returns a list of all users that have at least one entry
  45. * in the preferences table.
  46. */
  47. public static function getUsers() {
  48. // No need for more comments
  49. $query = OC_DB::prepare( 'SELECT DISTINCT( `userid` ) FROM `*PREFIX*preferences`' );
  50. $result = $query->execute();
  51. $users = array();
  52. while( $row = $result->fetchRow()) {
  53. $users[] = $row["userid"];
  54. }
  55. return $users;
  56. }
  57. /**
  58. * @brief Get all apps of an user
  59. * @param string $user user
  60. * @return array with app ids
  61. *
  62. * This function returns a list of all apps of the user that have at least
  63. * one entry in the preferences table.
  64. */
  65. public static function getApps( $user ) {
  66. // No need for more comments
  67. $query = OC_DB::prepare( 'SELECT DISTINCT( `appid` ) FROM `*PREFIX*preferences` WHERE `userid` = ?' );
  68. $result = $query->execute( array( $user ));
  69. $apps = array();
  70. while( $row = $result->fetchRow()) {
  71. $apps[] = $row["appid"];
  72. }
  73. return $apps;
  74. }
  75. /**
  76. * @brief Get the available keys for an app
  77. * @param string $user user
  78. * @param string $app the app we are looking for
  79. * @return array with key names
  80. *
  81. * This function gets all keys of an app of an user. Please note that the
  82. * values are not returned.
  83. */
  84. public static function getKeys( $user, $app ) {
  85. // No need for more comments
  86. $query = OC_DB::prepare( 'SELECT `configkey` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?' );
  87. $result = $query->execute( array( $user, $app ));
  88. $keys = array();
  89. while( $row = $result->fetchRow()) {
  90. $keys[] = $row["configkey"];
  91. }
  92. return $keys;
  93. }
  94. /**
  95. * @brief Gets the preference
  96. * @param string $user user
  97. * @param string $app app
  98. * @param string $key key
  99. * @param string $default = null, default value if the key does not exist
  100. * @return string the value or $default
  101. *
  102. * This function gets a value from the preferences table. If the key does
  103. * not exist the default value will be returned
  104. */
  105. public static function getValue( $user, $app, $key, $default = null ) {
  106. // Try to fetch the value, return default if not exists.
  107. $query = OC_DB::prepare( 'SELECT `configvalue` FROM `*PREFIX*preferences`'
  108. .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?' );
  109. $result = $query->execute( array( $user, $app, $key ));
  110. $row = $result->fetchRow();
  111. if($row) {
  112. return $row["configvalue"];
  113. }else{
  114. return $default;
  115. }
  116. }
  117. /**
  118. * @brief sets a value in the preferences
  119. * @param string $user user
  120. * @param string $app app
  121. * @param string $key key
  122. * @param string $value value
  123. * @return bool
  124. *
  125. * Adds a value to the preferences. If the key did not exist before, it
  126. * will be added automagically.
  127. */
  128. public static function setValue( $user, $app, $key, $value ) {
  129. // Check if the key does exist
  130. $query = OC_DB::prepare( 'SELECT `configvalue` FROM `*PREFIX*preferences`'
  131. .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?' );
  132. $values=$query->execute(array($user, $app, $key))->fetchAll();
  133. $exists=(count($values)>0);
  134. if( !$exists ) {
  135. $query = OC_DB::prepare( 'INSERT INTO `*PREFIX*preferences`'
  136. .' ( `userid`, `appid`, `configkey`, `configvalue` ) VALUES( ?, ?, ?, ? )' );
  137. $query->execute( array( $user, $app, $key, $value ));
  138. }
  139. else{
  140. $query = OC_DB::prepare( 'UPDATE `*PREFIX*preferences` SET `configvalue` = ?'
  141. .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?' );
  142. $query->execute( array( $value, $user, $app, $key ));
  143. }
  144. return true;
  145. }
  146. /**
  147. * @brief Deletes a key
  148. * @param string $user user
  149. * @param string $app app
  150. * @param string $key key
  151. * @return bool
  152. *
  153. * Deletes a key.
  154. */
  155. public static function deleteKey( $user, $app, $key ) {
  156. // No need for more comments
  157. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences`'
  158. .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?' );
  159. $query->execute( array( $user, $app, $key ));
  160. return true;
  161. }
  162. /**
  163. * @brief Remove app of user from preferences
  164. * @param string $user user
  165. * @param string $app app
  166. * @return bool
  167. *
  168. * Removes all keys in appconfig belonging to the app and the user.
  169. */
  170. public static function deleteApp( $user, $app ) {
  171. // No need for more comments
  172. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?' );
  173. $query->execute( array( $user, $app ));
  174. return true;
  175. }
  176. /**
  177. * @brief Remove user from preferences
  178. * @param string $user user
  179. * @return bool
  180. *
  181. * Removes all keys in appconfig belonging to the user.
  182. */
  183. public static function deleteUser( $user ) {
  184. // No need for more comments
  185. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?' );
  186. $query->execute( array( $user ));
  187. return true;
  188. }
  189. /**
  190. * @brief Remove app from all users
  191. * @param string $app app
  192. * @return bool
  193. *
  194. * Removes all keys in preferences belonging to the app.
  195. */
  196. public static function deleteAppFromAllUsers( $app ) {
  197. // No need for more comments
  198. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `appid` = ?' );
  199. $query->execute( array( $app ));
  200. return true;
  201. }
  202. }