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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. namespace OC;
  36. use \OC\DB\Connection;
  37. /**
  38. * This class provides an easy way for apps to store config values in the
  39. * database.
  40. */
  41. class AppConfig implements \OCP\IAppConfig {
  42. /**
  43. * @var \OC\DB\Connection $conn
  44. */
  45. protected $conn;
  46. private $cache = array();
  47. private $appsLoaded = array();
  48. /**
  49. * @param \OC\DB\Connection $conn
  50. */
  51. public function __construct(Connection $conn) {
  52. $this->conn = $conn;
  53. }
  54. /**
  55. * @param string $app
  56. * @return string[]
  57. */
  58. private function getAppCache($app) {
  59. if (!isset($this->cache[$app])) {
  60. $this->cache[$app] = array();
  61. }
  62. return $this->cache[$app];
  63. }
  64. /**
  65. * @param string $app
  66. * @return \string[]
  67. */
  68. private function getAppValues($app) {
  69. $appCache = $this->getAppCache($app);
  70. if (array_search($app, $this->appsLoaded) === false) {
  71. $query = 'SELECT `configvalue`, `configkey` FROM `*PREFIX*appconfig`'
  72. . ' WHERE `appid` = ?';
  73. $result = $this->conn->executeQuery($query, array($app));
  74. while ($row = $result->fetch()) {
  75. $appCache[$row['configkey']] = $row['configvalue'];
  76. }
  77. $this->appsLoaded[] = $app;
  78. }
  79. $this->cache[$app] = $appCache;
  80. return $appCache;
  81. }
  82. /**
  83. * @brief Get all apps using the config
  84. * @return array with app ids
  85. *
  86. * This function returns a list of all apps that have at least one
  87. * entry in the appconfig table.
  88. */
  89. public function getApps() {
  90. $query = 'SELECT DISTINCT `appid` FROM `*PREFIX*appconfig` ORDER BY `appid`';
  91. $result = $this->conn->executeQuery($query);
  92. $apps = array();
  93. while ($appid = $result->fetchColumn()) {
  94. $apps[] = $appid;
  95. }
  96. return $apps;
  97. }
  98. /**
  99. * @brief Get the available keys for an app
  100. * @param string $app the app we are looking for
  101. * @return array with key names
  102. *
  103. * This function gets all keys of an app. Please note that the values are
  104. * not returned.
  105. */
  106. public function getKeys($app) {
  107. $values = $this->getAppValues($app);
  108. $keys = array_keys($values);
  109. sort($keys);
  110. return $keys;
  111. }
  112. /**
  113. * @brief Gets the config value
  114. * @param string $app app
  115. * @param string $key key
  116. * @param string $default = null, default value if the key does not exist
  117. * @return string the value or $default
  118. *
  119. * This function gets a value from the appconfig table. If the key does
  120. * not exist the default value will be returned
  121. */
  122. public function getValue($app, $key, $default = null) {
  123. $values = $this->getAppValues($app);
  124. if (isset($values[$key])) {
  125. return $values[$key];
  126. } else {
  127. return $default;
  128. }
  129. }
  130. /**
  131. * @brief check if a key is set in the appconfig
  132. * @param string $app
  133. * @param string $key
  134. * @return bool
  135. */
  136. public function hasKey($app, $key) {
  137. $values = $this->getAppValues($app);
  138. return array_key_exists($key, $values);
  139. }
  140. /**
  141. * @brief sets a value in the appconfig
  142. * @param string $app app
  143. * @param string $key key
  144. * @param string $value value
  145. *
  146. * Sets a value. If the key did not exist before it will be created.
  147. */
  148. public function setValue($app, $key, $value) {
  149. // Does the key exist? no: insert, yes: update.
  150. if (!$this->hasKey($app, $key)) {
  151. $data = array(
  152. 'appid' => $app,
  153. 'configkey' => $key,
  154. 'configvalue' => $value,
  155. );
  156. $this->conn->insert('*PREFIX*appconfig', $data);
  157. } else {
  158. $data = array(
  159. 'configvalue' => $value,
  160. );
  161. $where = array(
  162. 'appid' => $app,
  163. 'configkey' => $key,
  164. );
  165. $this->conn->update('*PREFIX*appconfig', $data, $where);
  166. }
  167. if (!isset($this->cache[$app])) {
  168. $this->cache[$app] = array();
  169. }
  170. $this->cache[$app][$key] = $value;
  171. }
  172. /**
  173. * @brief Deletes a key
  174. * @param string $app app
  175. * @param string $key key
  176. * @return boolean|null
  177. */
  178. public function deleteKey($app, $key) {
  179. $where = array(
  180. 'appid' => $app,
  181. 'configkey' => $key,
  182. );
  183. $this->conn->delete('*PREFIX*appconfig', $where);
  184. if (isset($this->cache[$app]) and isset($this->cache[$app][$key])) {
  185. unset($this->cache[$app][$key]);
  186. }
  187. }
  188. /**
  189. * @brief Remove app from appconfig
  190. * @param string $app app
  191. * @return boolean|null
  192. *
  193. * Removes all keys in appconfig belonging to the app.
  194. */
  195. public function deleteApp($app) {
  196. $where = array(
  197. 'appid' => $app,
  198. );
  199. $this->conn->delete('*PREFIX*appconfig', $where);
  200. unset($this->cache[$app]);
  201. }
  202. /**
  203. * get multiply values, either the app or key can be used as wildcard by setting it to false
  204. *
  205. * @param string|false $app
  206. * @param string|false $key
  207. * @return array
  208. */
  209. public function getValues($app, $key) {
  210. if (($app !== false) == ($key !== false)) {
  211. return false;
  212. }
  213. $fields = '`configvalue`';
  214. $where = 'WHERE';
  215. $params = array();
  216. if ($app !== false) {
  217. $fields .= ', `configkey`';
  218. $where .= ' `appid` = ?';
  219. $params[] = $app;
  220. $key = 'configkey';
  221. } else {
  222. $fields .= ', `appid`';
  223. $where .= ' `configkey` = ?';
  224. $params[] = $key;
  225. $key = 'appid';
  226. }
  227. $query = 'SELECT ' . $fields . ' FROM `*PREFIX*appconfig` ' . $where;
  228. $result = $this->conn->executeQuery($query, $params);
  229. $values = array();
  230. while ($row = $result->fetch((\PDO::FETCH_ASSOC))) {
  231. $values[$row[$key]] = $row['configvalue'];
  232. }
  233. return $values;
  234. }
  235. }