appconfig.php 6.7 KB

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