From c7c35b8b5a64e44f01e7557a43d13e518dc62aa8 Mon Sep 17 00:00:00 2001 From: "M.Dent" Date: Sun, 28 Oct 2018 17:20:29 -0400 Subject: DB Updates to prep for pconfig timestamps. --- Zotlabs/Update/_1225.php | 26 ++++++++++++++++++++++++++ boot.php | 2 +- vendor/composer/autoload_static.php | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 Zotlabs/Update/_1225.php diff --git a/Zotlabs/Update/_1225.php b/Zotlabs/Update/_1225.php new file mode 100644 index 000000000..a7d866154 --- /dev/null +++ b/Zotlabs/Update/_1225.php @@ -0,0 +1,26 @@ + __DIR__ . '/../..' . '/Zotlabs/Update/_1222.php', 'Zotlabs\\Update\\_1223' => __DIR__ . '/../..' . '/Zotlabs/Update/_1223.php', 'Zotlabs\\Update\\_1224' => __DIR__ . '/../..' . '/Zotlabs/Update/_1224.php', + 'Zotlabs\\Update\\_1225' => __DIR__ . '/../..' . '/Zotlabs/Update/_1225.php', 'Zotlabs\\Web\\CheckJS' => __DIR__ . '/../..' . '/Zotlabs/Web/CheckJS.php', 'Zotlabs\\Web\\Controller' => __DIR__ . '/../..' . '/Zotlabs/Web/Controller.php', 'Zotlabs\\Web\\HTTPHeaders' => __DIR__ . '/../..' . '/Zotlabs/Web/HTTPHeaders.php', -- cgit v1.2.3 From 3ab0ef1902a9ff1f33cf5540a866745dc98a11a9 Mon Sep 17 00:00:00 2001 From: "M.Dent" Date: Sun, 28 Oct 2018 17:23:31 -0400 Subject: Add timestamp and associated logic to pconfig --- Zotlabs/Lib/PConfig.php | 65 ++++++++++++++++++++++++++++++++++++++++--------- include/config.php | 4 +-- include/zot.php | 37 ++++++++++++++++++++++++++-- 3 files changed, 90 insertions(+), 16 deletions(-) diff --git a/Zotlabs/Lib/PConfig.php b/Zotlabs/Lib/PConfig.php index ec0792ce1..31c0ee87b 100644 --- a/Zotlabs/Lib/PConfig.php +++ b/Zotlabs/Lib/PConfig.php @@ -57,6 +57,7 @@ class PConfig { \App::$config[$uid][$c]['config_loaded'] = true; } \App::$config[$uid][$c][$k] = $rr['v']; + \App::$config[$uid][$c]['pcfgud:'.$k] = $rr['updated']; } } } @@ -113,7 +114,7 @@ class PConfig { * The value to store * @return mixed Stored $value or false */ - static public function Set($uid, $family, $key, $value) { + static public function Set($uid, $family, $key, $value, $updated=NULL) { // this catches subtle errors where this function has been called // with local_channel() when not logged in (which returns false) @@ -130,27 +131,43 @@ class PConfig { $dbvalue = ((is_array($value)) ? serialize($value) : $value); $dbvalue = ((is_bool($dbvalue)) ? intval($dbvalue) : $dbvalue); + if (! $updated) { + $updated = datetime_convert(); + } + + if(self::Get($uid, $family, $key) === false) { if(! array_key_exists($uid, \App::$config)) \App::$config[$uid] = array(); if(! array_key_exists($family, \App::$config[$uid])) \App::$config[$uid][$family] = array(); - $ret = q("INSERT INTO pconfig ( uid, cat, k, v ) VALUES ( %d, '%s', '%s', '%s' ) ", + $ret = q("INSERT INTO pconfig ( uid, cat, k, v, updated ) VALUES ( %d, '%s', '%s', '%s', '%s' ) ", intval($uid), dbesc($family), dbesc($key), - dbesc($dbvalue) + dbesc($dbvalue), + dbesc($updated) ); + + \App::$config[$uid][$family]['pcfgud:'.$key] = $updated; + } else { - - $ret = q("UPDATE pconfig SET v = '%s' WHERE uid = %d and cat = '%s' AND k = '%s'", - dbesc($dbvalue), - intval($uid), - dbesc($family), - dbesc($key) - ); + $new = (\App::$config[$uid][$family]['pcfgud:'.$key] < $updated); + + if ($new) { + $ret = q("UPDATE pconfig SET v = '%s' WHERE uid = %d and cat = '%s' AND k = '%s' AND updated = '%s'", + dbesc($dbvalue), + intval($uid), + dbesc($family), + dbesc($key), + dbesc($updated) + ); + } else { + logger('Refusing to update pconfig with outdated info.', LOGGER_NORMAL, LOG_ERR); + return self::Get($uid, $family, $key); + } } // keep a separate copy for all variables which were @@ -163,7 +180,11 @@ class PConfig { \App::$config[$uid]['transient'][$family] = array(); \App::$config[$uid][$family][$key] = $value; - \App::$config[$uid]['transient'][$family][$key] = $value; + + if ($new) { + \App::$config[$uid]['transient'][$family][$key] = $value; + \App::$config[$uid]['transient'][$family]['pcfgud:'.$key] = $updated; + } if($ret) return $value; @@ -186,11 +207,20 @@ class PConfig { * The configuration key to delete * @return mixed */ - static public function Delete($uid, $family, $key) { + static public function Delete($uid, $family, $key, $updated = NULL) { if(is_null($uid) || $uid === false) return false; + $updated = ($updated) ? $updated : datetime_convert(); + + $newer = (\App::$config[$uid][$family]['pcfgud:'.$key] < $updated); + + if (! $newer) { + logger('Refusing to delete pconfig with outdated delete request.', LOGGER_NORMAL, LOG_ERR); + return false; + } + $ret = false; if(array_key_exists($uid,\App::$config) @@ -205,6 +235,17 @@ class PConfig { dbesc($key) ); + // Synchronize delete with clones. + + if(! array_key_exists('transient', \App::$config[$uid])) + \App::$config[$uid]['transient'] = array(); + if(! array_key_exists($family, \App::$config[$uid]['transient'])) + \App::$config[$uid]['transient'][$family] = array(); + + if ($new) { + \App::$config[$uid]['transient'][$family]['pcfgdel:'.$key] = $updated; + } + return $ret; } diff --git a/include/config.php b/include/config.php index 0be791715..ec3547a82 100644 --- a/include/config.php +++ b/include/config.php @@ -59,8 +59,8 @@ function set_pconfig($uid, $family, $key, $value) { return Zlib\PConfig::Set($uid,$family,$key,$value); } -function del_pconfig($uid, $family, $key) { - return Zlib\PConfig::Delete($uid,$family,$key); +function del_pconfig($uid, $family, $key, $updated = NULL) { + return Zlib\PConfig::Delete($uid,$family,$key,$updated); } function load_xconfig($xchan) { diff --git a/include/zot.php b/include/zot.php index 1a632cf87..a5202d01b 100644 --- a/include/zot.php +++ b/include/zot.php @@ -3507,8 +3507,41 @@ function process_channel_sync_delivery($sender, $arr, $deliveries) { if(array_key_exists('config',$arr) && is_array($arr['config']) && count($arr['config'])) { foreach($arr['config'] as $cat => $k) { - foreach($arr['config'][$cat] as $k => $v) - set_pconfig($channel['channel_id'],$cat,$k,$v); + + $pconfig_updated = []; + $pconfig_del = []; + + foreach($arr['config'][$cat] as $k => $v) { + + if (strpos($k,'pcfgud:')==0) { + + $realk = substr($k,7); + $pconfig_updated[$realk] = $v; + unset($arr['config'][$cat][$k]); + + } + + if (strpos($k,'pcfgdel:')==0) { + $realk = substr($k,8); + $pconfig_del[$realk] = $v; + unset($arr['config'][$cat][$k]); + } + } + + foreach($arr['config'][$cat] as $k => $v) { + + if (!isset($pconfig_updated[$k])) { + $pconfig_updated[$k] = NULL; + } + + set_pconfig($channel['channel_id'],$cat,$k,$v,$pconfig_updated[$k]); + + } + + foreach($pconfig_del as $k => $updated) { + del_pconfig($channel['channel_id'],$cat,$k,$updated); + } + } } -- cgit v1.2.3 From 1241e778f9073b5ba50d966eea20b28b9400339b Mon Sep 17 00:00:00 2001 From: "M.Dent" Date: Sun, 28 Oct 2018 17:30:22 -0400 Subject: Fix to set update time in pconfig on update --- Zotlabs/Lib/PConfig.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Zotlabs/Lib/PConfig.php b/Zotlabs/Lib/PConfig.php index 31c0ee87b..8bc5681f0 100644 --- a/Zotlabs/Lib/PConfig.php +++ b/Zotlabs/Lib/PConfig.php @@ -157,12 +157,12 @@ class PConfig { $new = (\App::$config[$uid][$family]['pcfgud:'.$key] < $updated); if ($new) { - $ret = q("UPDATE pconfig SET v = '%s' WHERE uid = %d and cat = '%s' AND k = '%s' AND updated = '%s'", + $ret = q("UPDATE pconfig SET v = '%s', updated = '%s' WHERE uid = %d and cat = '%s' AND k = '%s' ", dbesc($dbvalue), + dbesc($updated), intval($uid), dbesc($family), - dbesc($key), - dbesc($updated) + dbesc($key) ); } else { logger('Refusing to update pconfig with outdated info.', LOGGER_NORMAL, LOG_ERR); -- cgit v1.2.3 From 0060f88c4599c5cda1c6d5a7f6eb345a496e382f Mon Sep 17 00:00:00 2001 From: "M.Dent" Date: Mon, 29 Oct 2018 23:04:13 -0400 Subject: Add tracking of deleted pconfig variables --- Zotlabs/Lib/PConfig.php | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/Zotlabs/Lib/PConfig.php b/Zotlabs/Lib/PConfig.php index 8bc5681f0..b9384cf6b 100644 --- a/Zotlabs/Lib/PConfig.php +++ b/Zotlabs/Lib/PConfig.php @@ -135,6 +135,16 @@ class PConfig { $updated = datetime_convert(); } + $hash = hash('sha256',$family.':'.$key); + + if (self::Get($uid, 'hz_delpconfig', $hash) !== false) { + if (Get($uid, 'hz_delpconfig', $hash) > $updated) { + logger('Refusing to update pconfig with outdated info (Item deleted more recently).', LOGGER_NORMAL, LOG_ERR); + return self::Get($uid,$family,$key); + } else { + self::Delete($uid,'hz_delpconfig',$hash); + } + } if(self::Get($uid, $family, $key) === false) { if(! array_key_exists($uid, \App::$config)) @@ -142,6 +152,7 @@ class PConfig { if(! array_key_exists($family, \App::$config[$uid])) \App::$config[$uid][$family] = array(); + $ret = q("INSERT INTO pconfig ( uid, cat, k, v, updated ) VALUES ( %d, '%s', '%s', '%s', '%s' ) ", intval($uid), dbesc($family), @@ -150,6 +161,14 @@ class PConfig { dbesc($updated) ); + // There is a possible race condition if another process happens + // to insert something after this thread has Loaded and now. We should + // at least make a note of it if it happens. + + if (!$ret) { + logger("Error: Insert to pconfig failed.",LOGGER_NORMAL, LOG_ERR); + } + \App::$config[$uid][$family]['pcfgud:'.$key] = $updated; } @@ -157,6 +176,12 @@ class PConfig { $new = (\App::$config[$uid][$family]['pcfgud:'.$key] < $updated); if ($new) { + + // @NOTE There is still a possible race condition under limited circumstances + // where a value will be updated by another thread with more current data than + // we have. At this point there is no easy way to test for it, so we update + // and hope for the best. + $ret = q("UPDATE pconfig SET v = '%s', updated = '%s' WHERE uid = %d and cat = '%s' AND k = '%s' ", dbesc($dbvalue), dbesc($updated), @@ -164,12 +189,16 @@ class PConfig { dbesc($family), dbesc($key) ); + + \App::$config[$uid][$family]['pcfgud:'.$key] = $updated; + } else { logger('Refusing to update pconfig with outdated info.', LOGGER_NORMAL, LOG_ERR); return self::Get($uid, $family, $key); } } + // keep a separate copy for all variables which were // set in the life of this page. We need this to // synchronise channel clones. @@ -223,11 +252,13 @@ class PConfig { $ret = false; - if(array_key_exists($uid,\App::$config) - && is_array(\App::$config['uid']) - && array_key_exists($family,\App::$config['uid']) - && array_key_exists($key, \App::$config[$uid][$family])) + if (isset(\App::$config[$uid][$family][$key])) { unset(\App::$config[$uid][$family][$key]); + } + + if (isset(\App::$config[$uid][$family]['pcfgud:'.$key])) { + unset(\App::$config[$uid][$family]['pcfgud:'.$key]); + } $ret = q("DELETE FROM pconfig WHERE uid = %d AND cat = '%s' AND k = '%s'", intval($uid), @@ -235,6 +266,11 @@ class PConfig { dbesc($key) ); + if ($family != 'hz_delpconfig') { + $hash = hash('sha256',$family.':'.$key); + set_pconfig($uid,'hz_delpconfig',$hash,$updated); + } + // Synchronize delete with clones. if(! array_key_exists('transient', \App::$config[$uid])) -- cgit v1.2.3 From 346a4f593332009014fc8b3595bd56c6ca9ff9b5 Mon Sep 17 00:00:00 2001 From: "M.Dent" Date: Thu, 1 Nov 2018 20:23:22 -0400 Subject: strpos needs to test with ===0 not ==0 --- include/zot.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/zot.php b/include/zot.php index a5202d01b..49fc89e33 100644 --- a/include/zot.php +++ b/include/zot.php @@ -3513,7 +3513,7 @@ function process_channel_sync_delivery($sender, $arr, $deliveries) { foreach($arr['config'][$cat] as $k => $v) { - if (strpos($k,'pcfgud:')==0) { + if (strpos($k,'pcfgud:')===0) { $realk = substr($k,7); $pconfig_updated[$realk] = $v; @@ -3521,9 +3521,9 @@ function process_channel_sync_delivery($sender, $arr, $deliveries) { } - if (strpos($k,'pcfgdel:')==0) { + if (strpos($k,'pcfgdel:')===0) { $realk = substr($k,8); - $pconfig_del[$realk] = $v; + $pconfig_del[$realk] = datetime_convert(); unset($arr['config'][$cat][$k]); } } -- cgit v1.2.3