appconfig.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Jakob Sack <mail@jakobsack.de>
  6. * @author Joas Schilling <nickvergessen@owncloud.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Lukas Reschke <lukas@owncloud.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <icewind@owncloud.com>
  11. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  12. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @copyright Copyright (c) 2015, ownCloud, Inc.
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. /*
  32. *
  33. * The following SQL statement is just a help for developers and will not be
  34. * executed!
  35. *
  36. * CREATE TABLE `appconfig` (
  37. * `appid` VARCHAR( 255 ) NOT NULL ,
  38. * `configkey` VARCHAR( 255 ) NOT NULL ,
  39. * `configvalue` VARCHAR( 255 ) NOT NULL
  40. * )
  41. *
  42. */
  43. namespace OC;
  44. use OC\DB\Connection;
  45. use OCP\IAppConfig;
  46. /**
  47. * This class provides an easy way for apps to store config values in the
  48. * database.
  49. */
  50. class AppConfig implements IAppConfig {
  51. /**
  52. * @var \OC\DB\Connection $conn
  53. */
  54. protected $conn;
  55. private $cache = array();
  56. private $appsLoaded = array();
  57. /**
  58. * @var string[]
  59. */
  60. private $apps = null;
  61. /**
  62. * @param Connection $conn
  63. */
  64. public function __construct(Connection $conn) {
  65. $this->conn = $conn;
  66. }
  67. /**
  68. * @param string $app
  69. * @return string[]
  70. */
  71. private function getAppCache($app) {
  72. if (!isset($this->cache[$app])) {
  73. $this->cache[$app] = array();
  74. }
  75. return $this->cache[$app];
  76. }
  77. /**
  78. * @param string $app
  79. * @return \string[]
  80. */
  81. private function getAppValues($app) {
  82. $appCache = $this->getAppCache($app);
  83. if (array_search($app, $this->appsLoaded) === false) {
  84. $query = 'SELECT `configvalue`, `configkey` FROM `*PREFIX*appconfig`'
  85. . ' WHERE `appid` = ?';
  86. $result = $this->conn->executeQuery($query, array($app));
  87. while ($row = $result->fetch()) {
  88. $appCache[$row['configkey']] = $row['configvalue'];
  89. }
  90. $this->appsLoaded[] = $app;
  91. }
  92. $this->cache[$app] = $appCache;
  93. return $appCache;
  94. }
  95. /**
  96. * Get all apps using the config
  97. *
  98. * @return array an array of app ids
  99. *
  100. * This function returns a list of all apps that have at least one
  101. * entry in the appconfig table.
  102. */
  103. public function getApps() {
  104. if (is_array($this->apps)) {
  105. return $this->apps;
  106. }
  107. $query = 'SELECT DISTINCT `appid` FROM `*PREFIX*appconfig` ORDER BY `appid`';
  108. $result = $this->conn->executeQuery($query);
  109. $apps = array();
  110. while ($appid = $result->fetchColumn()) {
  111. $apps[] = $appid;
  112. }
  113. $this->apps = $apps;
  114. return $apps;
  115. }
  116. /**
  117. * Get the available keys for an app
  118. *
  119. * @param string $app the app we are looking for
  120. * @return array an array of key names
  121. *
  122. * This function gets all keys of an app. Please note that the values are
  123. * not returned.
  124. */
  125. public function getKeys($app) {
  126. $values = $this->getAppValues($app);
  127. $keys = array_keys($values);
  128. sort($keys);
  129. return $keys;
  130. }
  131. /**
  132. * Gets the config value
  133. *
  134. * @param string $app app
  135. * @param string $key key
  136. * @param string $default = null, default value if the key does not exist
  137. * @return string the value or $default
  138. *
  139. * This function gets a value from the appconfig table. If the key does
  140. * not exist the default value will be returned
  141. */
  142. public function getValue($app, $key, $default = null) {
  143. $values = $this->getAppValues($app);
  144. if (isset($values[$key])) {
  145. return $values[$key];
  146. } else {
  147. return $default;
  148. }
  149. }
  150. /**
  151. * check if a key is set in the appconfig
  152. *
  153. * @param string $app
  154. * @param string $key
  155. * @return bool
  156. */
  157. public function hasKey($app, $key) {
  158. $values = $this->getAppValues($app);
  159. return array_key_exists($key, $values);
  160. }
  161. /**
  162. * Sets a value. If the key did not exist before it will be created.
  163. *
  164. * @param string $app app
  165. * @param string $key key
  166. * @param string $value value
  167. * @return void
  168. */
  169. public function setValue($app, $key, $value) {
  170. $inserted = false;
  171. // Does the key exist? no: insert, yes: update.
  172. if (!$this->hasKey($app, $key)) {
  173. $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [
  174. 'appid' => $app,
  175. 'configkey' => $key,
  176. 'configvalue' => $value,
  177. ], [
  178. 'appid',
  179. 'configkey',
  180. ]);
  181. }
  182. if (!$inserted) {
  183. $oldValue = $this->getValue($app, $key);
  184. if($oldValue === strval($value)) {
  185. return;
  186. }
  187. $data = array(
  188. 'configvalue' => $value,
  189. );
  190. $where = array(
  191. 'appid' => $app,
  192. 'configkey' => $key,
  193. );
  194. $this->conn->update('*PREFIX*appconfig', $data, $where);
  195. }
  196. if (!isset($this->cache[$app])) {
  197. $this->cache[$app] = array();
  198. }
  199. if (is_array($this->apps) and array_search($app, $this->apps) === false) {
  200. $this->apps[$app] = $app;
  201. }
  202. $this->cache[$app][$key] = $value;
  203. }
  204. /**
  205. * Deletes a key
  206. *
  207. * @param string $app app
  208. * @param string $key key
  209. * @return boolean|null
  210. */
  211. public function deleteKey($app, $key) {
  212. $where = array(
  213. 'appid' => $app,
  214. 'configkey' => $key,
  215. );
  216. $this->conn->delete('*PREFIX*appconfig', $where);
  217. if (isset($this->cache[$app]) and isset($this->cache[$app][$key])) {
  218. unset($this->cache[$app][$key]);
  219. }
  220. }
  221. /**
  222. * Remove app from appconfig
  223. *
  224. * @param string $app app
  225. * @return boolean|null
  226. *
  227. * Removes all keys in appconfig belonging to the app.
  228. */
  229. public function deleteApp($app) {
  230. $where = array(
  231. 'appid' => $app,
  232. );
  233. $this->conn->delete('*PREFIX*appconfig', $where);
  234. unset($this->cache[$app]);
  235. unset($this->apps[$app]);
  236. }
  237. /**
  238. * get multiply values, either the app or key can be used as wildcard by setting it to false
  239. *
  240. * @param string|false $app
  241. * @param string|false $key
  242. * @return array|false
  243. */
  244. public function getValues($app, $key) {
  245. if (($app !== false) == ($key !== false)) {
  246. return false;
  247. }
  248. if ($app !== false) {
  249. return $this->getAppValues($app);
  250. } else {
  251. $query = 'SELECT `configvalue`, `appid` FROM `*PREFIX*appconfig` WHERE `configkey` = ?';
  252. $result = $this->conn->executeQuery($query, array($key));
  253. $values = array();
  254. while ($row = $result->fetch((\PDO::FETCH_ASSOC))) {
  255. $values[$row['appid']] = $row['configvalue'];
  256. }
  257. return $values;
  258. }
  259. }
  260. }