From 36bbba2eb0cba86fa91edabb9f6030ef541c8f99 Mon Sep 17 00:00:00 2001 From: supahgreg Date: Thu, 13 Nov 2025 00:10:21 +0000 Subject: [PATCH] Allow cloning the default profile. --- classes/Pref_Prefs.php | 64 +++++++++++++++++++++++++++--------------- classes/Prefs.php | 20 ++++++++++--- 2 files changed, 57 insertions(+), 27 deletions(-) diff --git a/classes/Pref_Prefs.php b/classes/Pref_Prefs.php index 88f0f8ebe..3410f218f 100644 --- a/classes/Pref_Prefs.php +++ b/classes/Pref_Prefs.php @@ -1403,33 +1403,51 @@ class Pref_Prefs extends Handler_Protected { } } + /** + * @todo this should result in an error on failures + */ function cloneprofile(): void { - $old_profile = $_REQUEST["old_profile"] ?? 0; - $new_title = clean($_REQUEST["new_title"]); + $old_profile_id = $_REQUEST['old_profile'] ?? ''; - if ($old_profile && $new_title) { - $new_profile = ORM::for_table('ttrss_settings_profiles')->create(); - $new_profile->title = $new_title; - $new_profile->owner_uid = $_SESSION['uid']; + if (ctype_digit($old_profile_id)) + $old_profile_id = (int) $old_profile_id; + else + return; - if ($new_profile->save()) { - $sth = $this->pdo->prepare("INSERT INTO ttrss_user_prefs2 - (owner_uid, pref_name, profile, value) - SELECT - :uid, - pref_name, - :new_profile, - value - FROM ttrss_user_prefs2 - WHERE owner_uid = :uid AND profile = :old_profile"); + $new_title = clean($_REQUEST['new_title']); - $sth->execute([ - "uid" => $_SESSION["uid"], - "new_profile" => $new_profile->id, - "old_profile" => $old_profile, - ]); - } - } + if (!$new_title) + return; + + $new_profile = ORM::for_table('ttrss_settings_profiles')->create(); + $new_profile->title = $new_title; + $new_profile->owner_uid = $_SESSION['uid']; + + if (!$new_profile->save()) + return; + + // NOTE: In 'ttrss_user_prefs2' the default profile is represented by 'profile' being null, + // but 0 is what gets used as its representative ID on the frontend. + $sth = $this->pdo->prepare('INSERT INTO ttrss_user_prefs2 + (owner_uid, pref_name, profile, value) + SELECT + :uid, + pref_name, + :new_profile, + value + FROM ttrss_user_prefs2 + WHERE owner_uid = :uid + AND ' . ($old_profile_id === 0 ? 'profile IS NULL' : 'profile = :old_profile_id')); + + $params = [ + 'uid' => $_SESSION['uid'], + 'new_profile' => $new_profile->id, + ]; + + if ($old_profile_id !== 0) + $params['old_profile_id'] = $old_profile_id; + + $sth->execute($params); } function remprofiles(): void { diff --git a/classes/Prefs.php b/classes/Prefs.php index 9f841c363..2c8015901 100644 --- a/classes/Prefs.php +++ b/classes/Prefs.php @@ -216,7 +216,10 @@ class Prefs { } private function cache_all(int $owner_uid, ?int $profile_id): void { - if (!$profile_id) $profile_id = null; + // If explicitly null, 0 (e.g. from the frontend), or otherwise falsy, + // normalize to null to represent the default profile. + if (!$profile_id) + $profile_id = null; // fill cache with defaults $ref = new ReflectionClass(static::class); @@ -303,7 +306,10 @@ class Prefs { } private function _set(string $pref_name, bool|int|string $value, int $owner_uid, ?int $profile_id, bool $strip_tags = true): bool { - if (!$profile_id) $profile_id = null; + // If explicitly null, 0 (e.g. from the frontend), or otherwise falsy, + // normalize to null to represent the default profile. + if (!$profile_id) + $profile_id = null; if ($profile_id && in_array($pref_name, self::_PROFILE_BLACKLIST)) return false; @@ -355,7 +361,10 @@ class Prefs { if (Config::get_schema_version() < 141) return; - if (!$profile_id) $profile_id = null; + // If explicitly null, 0 (e.g. from the frontend), or otherwise falsy, + // normalize to null to represent the default profile. + if (!$profile_id) + $profile_id = null; if (!$this->_get(Prefs::_PREFS_MIGRATED, $owner_uid, $profile_id)) { @@ -394,7 +403,10 @@ class Prefs { } static function reset(int $owner_uid, ?int $profile_id): void { - if (!$profile_id) $profile_id = null; + // If explicitly null, 0 (e.g. from the frontend), or otherwise falsy, + // normalize to null to represent the default profile. + if (!$profile_id) + $profile_id = null; $sth = Db::pdo()->prepare("DELETE FROM ttrss_user_prefs2 WHERE owner_uid = :uid AND pref_name != :mig_key AND