]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig.php
API: fix sender/recipient of PMs: check api_user before get user info.
[friendica.git] / src / Core / PConfig.php
1 <?php
2 /**
3  * User Configuration Class
4  *
5  * @file include/Core/PConfig.php
6  *
7  * @brief Contains the class with methods for user configuration
8  */
9 namespace Friendica\Core;
10
11 use Friendica\BaseObject;
12 use Friendica\Core\Config;
13
14 require_once 'include/dba.php';
15
16 /**
17  * @brief Management of user configuration storage
18  * Note:
19  * Please do not store booleans - convert to 0/1 integer values
20  * The PConfig::get() functions return boolean false for keys that are unset,
21  * and this could lead to subtle bugs.
22  */
23 class PConfig extends BaseObject
24 {
25         /**
26          * @var Friendica\Core\Config\IPConfigAdapter
27          */
28         private static $adapter = null;
29
30         public static function init($uid)
31         {
32                 if (Config::get('system', 'config_adapter') == 'preload') {
33                         self::$adapter = new Config\PreloadPConfigAdapter($uid);
34                 } else {
35                         self::$adapter = new Config\JITPConfigAdapter($uid);
36                 }
37         }
38
39         /**
40          * @brief Loads all configuration values of a user's config family into a cached storage.
41          *
42          * All configuration values of the given user are stored in global cache
43          * which is available under the global variable $a->config[$uid].
44          *
45          * @param string $uid    The user_id
46          * @param string $family The category of the configuration value
47          *
48          * @return void
49          */
50         public static function load($uid, $family)
51         {
52                 if (empty(self::$adapter)) {
53                         self::init($uid);
54                 }
55
56                 self::$adapter->load($uid, $family);
57         }
58
59         /**
60          * @brief Get a particular user's config variable given the category name
61          * ($family) and a key.
62          *
63          * Get a particular user's config value from the given category ($family)
64          * and the $key from a cached storage in $a->config[$uid].
65          *
66          * @param string  $uid           The user_id
67          * @param string  $family        The category of the configuration value
68          * @param string  $key           The configuration key to query
69          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
70          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
71          *
72          * @return mixed Stored value or null if it does not exist
73          */
74         public static function get($uid, $family, $key, $default_value = null, $refresh = false)
75         {
76                 if (empty(self::$adapter)) {
77                         self::init($uid);
78                 }
79
80                 return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
81         }
82
83         /**
84          * @brief Sets a configuration value for a user
85          *
86          * Stores a config value ($value) in the category ($family) under the key ($key)
87          * for the user_id $uid.
88          *
89          * @note Please do not store booleans - convert to 0/1 integer values!
90          *
91          * @param string $uid    The user_id
92          * @param string $family The category of the configuration value
93          * @param string $key    The configuration key to set
94          * @param string $value  The value to store
95          *
96          * @return mixed Stored $value or false
97          */
98         public static function set($uid, $family, $key, $value)
99         {
100                 if (empty(self::$adapter)) {
101                         self::init($uid);
102                 }
103
104                 return self::$adapter->set($uid, $family, $key, $value);
105         }
106
107         /**
108          * @brief Deletes the given key from the users's configuration.
109          *
110          * Removes the configured value from the stored cache in $a->config[$uid]
111          * and removes it from the database.
112          *
113          * @param string $uid    The user_id
114          * @param string $family The category of the configuration value
115          * @param string $key    The configuration key to delete
116          *
117          * @return mixed
118          */
119         public static function delete($uid, $family, $key)
120         {
121                 if (empty(self::$adapter)) {
122                         self::init($uid);
123                 }
124
125                 return self::$adapter->delete($uid, $family, $key);
126         }
127 }