]> git.wh0rd.org - tt-rss.git/blobdiff - include/functions.php
more boolean fixes
[tt-rss.git] / include / functions.php
index e7d74df04e23727b33c30c9e5d86f351ad38edec..6637bd5d1a1a1d253a508351a2434ee961ca5d39 100644 (file)
                else
                    $query_limit = "";
 
+               $purge_interval = (int) $purge_interval;
+
                if (DB_TYPE == "pgsql") {
                        $sth = $pdo->prepare("DELETE FROM ttrss_user_entries
                                USING ttrss_entries
                                marked = false AND
                                feed_id = ? AND
                                $query_limit
-                               ttrss_entries.date_updated < NOW() - INTERVAL ? days");
-                       $sth->execute([$feed_id, $purge_interval]);
+                               ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'");
+                       $sth->execute([$feed_id]);
 
                } else {
             $sth  = $pdo->prepare("DELETE FROM ttrss_user_entries
                                marked = false AND
                                feed_id = ? AND
                                $query_limit
-                               ttrss_entries.date_updated < DATE_SUB(NOW(), INTERVAL ? DAY)");
-            $sth->execute([$feed_id, $purge_interval]);
+                               ttrss_entries.date_updated < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
+            $sth->execute([$feed_id]);
 
                }
 
 
        function initialize_user_prefs($uid, $profile = false) {
 
-               $uid = db_escape_string($uid);
-
                if (get_schema_version() < 63) $profile_qpart = "";
 
         $pdo = DB::pdo();
+        $in_nested_tr = false;
 
-        $pdo->beginTransaction();
+        try {
+                       $pdo->beginTransaction();
+               } catch (Exception $e) {
+               $in_nested_tr = true;
+               }
 
                $sth = $pdo->query("SELECT pref_name,def_value FROM ttrss_prefs");
 
 
                $u_sth = $pdo->prepare("SELECT pref_name
                        FROM ttrss_user_prefs WHERE owner_uid = :uid AND 
-                               profile = :profile OR (:profile IS NULL AND profile IS NULL)");
+                               (profile = :profile OR (:profile IS NULL AND profile IS NULL))");
                $u_sth->execute([':uid' => $uid, ':profile' => $profile]);
 
                $active_prefs = array();
                        if (array_search($line["pref_name"], $active_prefs) === FALSE) {
 //                             print "adding " . $line["pref_name"] . "<br>";
 
-                               $line["def_value"] = db_escape_string($line["def_value"]);
-                               $line["pref_name"] = db_escape_string($line["pref_name"]);
-
                                if (get_schema_version() < 63) {
                                        $i_sth = $pdo->prepare("INSERT INTO ttrss_user_prefs
                                                (owner_uid,pref_name,value) VALUES
                        }
                }
 
-               $pdo->commit();
+               if (!$in_nested_tr) $pdo->commit();
 
        }
 
        }
 
        function sql_bool_to_bool($s) {
-               if ($s == "t" || $s == "1" || strtolower($s) == "true") {
-                       return true;
-               } else {
-                       return false;
-               }
+               return $s && ($s != "f" && $s != "false"); //no-op for PDO, backwards compat for legacy layer
        }
 
        function bool_to_sql_bool($s) {
-               if ($s) {
-                       return "true";
-               } else {
-                       return "false";
-               }
+               return (bool)$s; //no-op for PDO
        }
 
        // Session caching removed due to causing wrong redirects to upgrade
        }
 
        function checkbox_to_sql_bool($val) {
-               return ($val == "on") ? "true" : "false";
+               return ($val == "on") ? true : false;
        }
 
        function uniqid_short() {
                $search_words = array();
                $search_query_leftover = array();
 
+               $pdo = Db::pdo();
+               
                if ($search_language)
-                       $search_language = db_escape_string(mb_strtolower($search_language));
+                       $search_language = $pdo->quote(mb_strtolower($search_language));
                else
                        $search_language = "english";
 
                                case "title":
                                        if ($commandpair[1]) {
                                                array_push($query_keywords, "($not (LOWER(ttrss_entries.title) LIKE '%".
-                                                       db_escape_string(mb_strtolower($commandpair[1]))."%'))");
+                                                       $pdo->quote(mb_strtolower($commandpair[1]))."%'))");
                                        } else {
                                                array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
                                                                OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
                                case "author":
                                        if ($commandpair[1]) {
                                                array_push($query_keywords, "($not (LOWER(author) LIKE '%".
-                                                       db_escape_string(mb_strtolower($commandpair[1]))."%'))");
+                                                       $pdo->quote(mb_strtolower($commandpair[1]))."%'))");
                                        } else {
                                                array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
                                                                OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
                                                        array_push($query_keywords, "($not (note IS NULL OR note = ''))");
                                                else
                                                        array_push($query_keywords, "($not (LOWER(note) LIKE '%".
-                                                               db_escape_string(mb_strtolower($commandpair[1]))."%'))");
+                                                               $pdo->quote(mb_strtolower($commandpair[1]))."%'))");
                                        } else {
                                                array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
                                                                OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
                }
 
                if (count($search_query_leftover) > 0) {
-                       $search_query_leftover = db_escape_string(implode(" & ", $search_query_leftover));
+                       $search_query_leftover = $pdo->quote(implode(" & ", $search_query_leftover));
 
                        if (DB_TYPE == "pgsql") {
                                array_push($query_keywords,
        function load_filters($feed_id, $owner_uid) {
                $filters = array();
 
+               $feed_id = (int) $feed_id;
                $cat_id = (int)Feeds::getFeedCategory($feed_id);
 
                if ($cat_id == 0)
                if (!$feed_cat) return false;
 
                $feed_cat = mb_substr($feed_cat, 0, 250);
+               if (!$parent_cat_id) $parent_cat_id = null;
 
                $pdo = Db::pdo();
-               $pdo->beginTransaction();
+               $tr_in_progress = false;
+
+               try {
+                       $pdo->beginTransaction();
+               } catch (Exception $e) {
+                       $tr_in_progress = true;
+               }
 
                $sth = $pdo->prepare("SELECT id FROM ttrss_feed_categories
-                               WHERE (:parent IS NULL AND parent_cat IS NULL OR parent_cat = :parent
-                               AND title = :cat AND owner_uid = :uid");
+                               WHERE (parent_cat = :parent OR (:parent IS NULL AND parent_cat IS NULL)
+                               AND title = :title AND owner_uid = :uid");
                $sth->execute([':parent' => $parent_cat_id, ':title' => $feed_cat, ':uid' => $_SESSION['uid']]);
 
-               if ($sth->fetch()) {
+               if (!$sth->fetch()) {
 
                        $sth = $pdo->prepare("INSERT INTO ttrss_feed_categories (owner_uid,title,parent_cat)
                                        VALUES (?, ?, ?)");
                        $sth->execute([$_SESSION['uid'], $feed_cat, $parent_cat_id]);
 
-                       $pdo->commit();
+                       if (!$tr_in_progress) $pdo->commit();
 
                        return true;
                }
 
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
+               $is_cat = bool_to_sql_bool($is_cat);
+
                $pdo = Db::pdo();
 
-               $sth = $dbh->prepare("SELECT access_key FROM ttrss_access_keys
-                               WHERE feed_id = ?       AND is_cat = ?
+               $sth = $pdo->prepare("SELECT access_key FROM ttrss_access_keys
+                               WHERE feed_id = ? AND is_cat = ?
                                AND owner_uid = ?");
                $sth->execute([$feed_id, $is_cat, $owner_uid]);