1919/* * ***************************Includes********************************* */
2020require_once __DIR__ . '/../php/core.inc.php ' ;
2121
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+ */
2231class utils {
2332 /* * *************************Attributs****************************** */
2433
34+ /** @var array<string, ReflectionProperty[]> Cached reflection properties by class */
2535 private static $ properties = array ();
36+
37+ /** @var string|null|false Encryption key */
2638 private static $ jeedom_encryption = null ;
2739
2840 /* * ***********************Methode static*************************** */
2941
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+ */
3050 public static function attrChanged ($ _changed , $ _old , $ _new ) {
3151 if ($ _changed ) {
3252 return true ;
@@ -40,11 +60,14 @@ public static function attrChanged($_changed, $_old, $_new) {
4060 return ($ _old != $ _new );
4161 }
4262
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+ */
4871 public static function o2a ($ _object , $ _noToArray = false ) {
4972 if (is_array ($ _object )) {
5073 $ return = array ();
@@ -81,6 +104,13 @@ public static function o2a($_object, $_noToArray = false) {
81104 return $ array ;
82105 }
83106
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+ */
84114 public static function a2o (&$ _object , $ _data ) {
85115 if (is_array ($ _data )) {
86116 foreach ($ _data as $ key => $ value ) {
@@ -114,6 +144,16 @@ public static function a2o(&$_object, $_data) {
114144 }
115145 }
116146
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+ */
117157 public static function processJsonObject ($ _class , $ _ajaxList , $ _dbList = null ,$ _remove = true ) {
118158 if (!is_array ($ _ajaxList )) {
119159 if (is_json ($ _ajaxList )) {
@@ -147,7 +187,14 @@ public static function processJsonObject($_class, $_ajaxList, $_dbList = null,$_
147187 }
148188 }
149189 }
150-
190+ /**
191+ * Sets JSON attribute value(s)
192+ *
193+ * @param array<mixed>|string|mixed $_attr Current attributes
194+ * @param array<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+ */
151198 public static function setJsonAttr ($ _attr , $ _key , $ _value = null ) {
152199 if ($ _value === null && !is_array ($ _key )) {
153200 if (!is_array ($ _attr )) {
@@ -167,6 +214,14 @@ public static function setJsonAttr($_attr, $_key, $_value = null) {
167214 return $ _attr ;
168215 }
169216
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+ */
170225 public static function getJsonAttr (&$ _attr , $ _key = '' , $ _default = '' ) {
171226 if (is_array ($ _attr )) {
172227 if ($ _key == '' ) {
@@ -198,6 +253,14 @@ public static function getJsonAttr(&$_attr, $_key = '', $_default = '') {
198253 }
199254
200255 /* * ******************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+ */
201264 public static function getEncryptionPassword () {
202265 if (self ::$ jeedom_encryption == null ) {
203266 if (!file_exists (__DIR__ . '/../../data/jeedom_encryption.key ' )) {
@@ -208,6 +271,15 @@ public static function getEncryptionPassword() {
208271 return self ::$ jeedom_encryption ;
209272 }
210273
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+ */
211283 public static function encrypt ($ plaintext , $ password = null ) {
212284 if ($ plaintext === '' ) {
213285 return null ;
@@ -227,6 +299,15 @@ public static function encrypt($plaintext, $password = null) {
227299 return 'crypt: ' . base64_encode ($ iv . $ hmac . $ ciphertext );
228300 }
229301
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+ */
230311 public static function decrypt ($ ciphertext , $ password = null ) {
231312 if (empty ($ ciphertext )) {
232313 return null ;
0 commit comments