19
19
/* * ***************************Includes********************************* */
20
20
require_once __DIR__ . '/../php/core.inc.php ' ;
21
21
22
+ /**
23
+ * Utility functions for Jeedom core system
24
+ *
25
+ * Provides helper methods for object conversion, JSON manipulation,
26
+ * encryption/decryption and asynchronous execution
27
+ *
28
+ * @see config For system configuration
29
+ * @see cron For async execution
30
+ */
22
31
class utils {
23
32
/* * *************************Attributs****************************** */
24
33
34
+ /** @var array<string, ReflectionProperty[]> Cached reflection properties by class */
25
35
private static $ properties = array ();
36
+
37
+ /** @var string|null|false Encryption key */
26
38
private static $ jeedom_encryption = null ;
27
39
28
40
/* * ***********************Methode static*************************** */
29
41
42
+ /**
43
+ * Checks if attributes have changed between old and new values
44
+ *
45
+ * @param bool $_changed Current change status
46
+ * @param array|mixed $_old Previous value
47
+ * @param array|mixed $_new New value
48
+ * @return bool True if values are different
49
+ */
30
50
public static function attrChanged ($ _changed , $ _old , $ _new ) {
31
51
if ($ _changed ) {
32
52
return true ;
@@ -40,11 +60,14 @@ public static function attrChanged($_changed, $_old, $_new) {
40
60
return ($ _old != $ _new );
41
61
}
42
62
43
- /**
44
- * @param object $_object
45
- * @param bool $_noToArray
46
- * @return array
47
- */
63
+ /**
64
+ * Converts object(s) to array representation
65
+ *
66
+ * @param object[]|object|mixed $_object Object(s) to convert
67
+ * @param bool $_noToArray Skip toArray() method if true
68
+ * @return array<string, mixed>[]|array<string, mixed> Array representation
69
+ * @throws ReflectionException
70
+ */
48
71
public static function o2a ($ _object , $ _noToArray = false ) {
49
72
if (is_array ($ _object )) {
50
73
$ return = array ();
@@ -81,6 +104,13 @@ public static function o2a($_object, $_noToArray = false) {
81
104
return $ array ;
82
105
}
83
106
107
+ /**
108
+ * Populates object properties from array data
109
+ *
110
+ * @param object $_object Object to populate
111
+ * @param iterable<string, mixed> $_data Source data
112
+ * @return void
113
+ */
84
114
public static function a2o (&$ _object , $ _data ) {
85
115
if (is_array ($ _data )) {
86
116
foreach ($ _data as $ key => $ value ) {
@@ -114,6 +144,16 @@ public static function a2o(&$_object, $_data) {
114
144
}
115
145
}
116
146
147
+ /**
148
+ * Processes JSON objects for class synchronization
149
+ *
150
+ * @param string $_class Target class name
151
+ * @param array<array<string, mixed>>|string $_ajaxList New objects data
152
+ * @param array|null $_dbList Existing objects
153
+ * @param bool $_remove Remove missing objects
154
+ * @return void
155
+ * @throws Exception On invalid input
156
+ */
117
157
public static function processJsonObject ($ _class , $ _ajaxList , $ _dbList = null ,$ _remove = true ) {
118
158
if (!is_array ($ _ajaxList )) {
119
159
if (is_json ($ _ajaxList )) {
@@ -147,7 +187,14 @@ public static function processJsonObject($_class, $_ajaxList, $_dbList = null,$_
147
187
}
148
188
}
149
189
}
150
-
190
+ /**
191
+ * Sets JSON attribute value(s)
192
+ *
193
+ * @param array<string, mixed>|string|mixed $_attr Current attributes
194
+ * @param array<string, mixed>|string|int $_key Key to set or array of key/values
195
+ * @param mixed|null $_value Value to set if key is string
196
+ * @return array|bool|mixed Updated attributes
197
+ */
151
198
public static function setJsonAttr ($ _attr , $ _key , $ _value = null ) {
152
199
if ($ _value === null && !is_array ($ _key )) {
153
200
if (!is_array ($ _attr )) {
@@ -167,6 +214,14 @@ public static function setJsonAttr($_attr, $_key, $_value = null) {
167
214
return $ _attr ;
168
215
}
169
216
217
+ /**
218
+ * Gets JSON attribute value(s)
219
+ *
220
+ * @param mixed $_attr Source attributes
221
+ * @param string[]|string $_key Key(s) to retrieve
222
+ * @param mixed $_default Default value if not found
223
+ * @return array|bool|mixed|string Attribute value(s)
224
+ */
170
225
public static function getJsonAttr (&$ _attr , $ _key = '' , $ _default = '' ) {
171
226
if (is_array ($ _attr )) {
172
227
if ($ _key == '' ) {
@@ -198,6 +253,14 @@ public static function getJsonAttr(&$_attr, $_key = '', $_default = '') {
198
253
}
199
254
200
255
/* * ******************Encrypt/decrypt*************************** */
256
+
257
+ /**
258
+ * Gets encryption password from configuration
259
+ *
260
+ * @return false|string|null Encryption key
261
+ * @throws Exception If key file cannot be read
262
+ * @see config::genKey() For key generation
263
+ */
201
264
public static function getEncryptionPassword () {
202
265
if (self ::$ jeedom_encryption == null ) {
203
266
if (!file_exists (__DIR__ . '/../../data/jeedom_encryption.key ' )) {
@@ -208,6 +271,15 @@ public static function getEncryptionPassword() {
208
271
return self ::$ jeedom_encryption ;
209
272
}
210
273
274
+ /**
275
+ * Encrypts plaintext using AES-256-CBC
276
+ *
277
+ * @param string $plaintext Text to encrypt
278
+ * @param string|null $password Custom password (uses system key if null)
279
+ * @return string|null Encrypted text prefixed with 'crypt:'
280
+ * @throws Exception On encryption failure
281
+ * @see self::getEncryptionPassword() For default key
282
+ */
211
283
public static function encrypt ($ plaintext , $ password = null ) {
212
284
if ($ plaintext === '' ) {
213
285
return null ;
@@ -227,6 +299,15 @@ public static function encrypt($plaintext, $password = null) {
227
299
return 'crypt: ' . base64_encode ($ iv . $ hmac . $ ciphertext );
228
300
}
229
301
302
+ /**
303
+ * Decrypts ciphertext using AES-256-CBC
304
+ *
305
+ * @param string $ciphertext Encrypted text
306
+ * @param string|null $password Custom password (uses system key if null)
307
+ * @return false|string|null Decrypted text
308
+ * @throws Exception On decryption failure
309
+ * @see self::getEncryptionPassword() For default key
310
+ */
230
311
public static function decrypt ($ ciphertext , $ password = null ) {
231
312
if (empty ($ ciphertext )) {
232
313
return null ;
0 commit comments