]> git.wh0rd.org - tt-rss.git/blobdiff - include/functions.php
pdo pdo pdo
[tt-rss.git] / include / functions.php
index 58f4ba31cdc172bfc8412cc61d79a00a8dc4f7ed..377d4964741ed2ed7015bc20df32a584843863dd 100644 (file)
                $pdo = Db::pdo();
 
                $sth = $pdo->prepare("SELECT owner_uid FROM ttrss_feeds WHERE id = ?");
-               $sth->execute($feed_id);
+               $sth->execute([$feed_id]);
 
                $owner_uid = false;
 
 
                if (get_schema_version() < 63) $profile_qpart = "";
 
-               ////db_query("BEGIN");
-
         $pdo = DB::pdo();
 
+        $pdo->beginTransaction();
+
                $sth = $pdo->query("SELECT pref_name,def_value FROM ttrss_prefs");
 
         $profile = $profile ? $profile : null;
 
                $u_sth = $pdo->prepare("SELECT pref_name
                        FROM ttrss_user_prefs WHERE owner_uid = :uid AND 
-                               (:profile IS NULL AND profile is NULL OR profile = :profile)");
-               $u_sth->execute(['uid' => $uid, 'profile' => $profile]);
+                               profile = :profile OR (:profile IS NULL AND profile IS NULL)");
+               $u_sth->execute([':uid' => $uid, ':profile' => $profile]);
 
                $active_prefs = array();
 
                        }
                }
 
-               ////db_query("COMMIT");
+               $pdo->commit();
 
        }
 
                                                (SELECT COUNT(id) FROM ttrss_feeds WHERE
                                                        ttrss_feeds.id = feed_id) = 0");
 
-                               $sth->execute($_SESSION['uid']);
+                               $sth->execute([$_SESSION['uid']]);
 
                                $sth = $pdo->prepare("DELETE FROM ttrss_cat_counters_cache WHERE owner_uid = ? 
                     AND
                                                (SELECT COUNT(id) FROM ttrss_feed_categories WHERE
                                                        ttrss_feed_categories.id = feed_id) = 0");
 
-                $sth->execute($_SESSION['uid']);
+                $sth->execute([$_SESSION['uid']]);
                        }
 
                }
 
                if (!$feed_cat) return false;
 
-               ////db_query("BEGIN");
-
-               if ($parent_cat_id) {
-                       $parent_qpart = "parent_cat = '$parent_cat_id'";
-                       $parent_insert = "'$parent_cat_id'";
-               } else {
-                       $parent_qpart = "parent_cat IS NULL";
-                       $parent_insert = "NULL";
-               }
-
                $feed_cat = mb_substr($feed_cat, 0, 250);
 
                $pdo = Db::pdo();
+               $pdo->beginTransaction();
 
                $sth = $pdo->prepare("SELECT id FROM ttrss_feed_categories
-                               WHERE parent_cat = ? AND title = '$feed_cat' AND owner_uid = ?");
+                               WHERE (:parent IS NULL AND parent_cat IS NULL OR parent_cat = :parent) 
+                               AND title = :cat AND owner_uid = :uid");
+               $sth->execute([':parent' => $parent_cat_id, ':title' => $feed_cat, ':uid' => $_SESSION['uid']]);
 
-               if (db_num_rows($result) == 0) {
+               if ($sth->fetch()) {
 
-                       $result = db_query(
-                               "INSERT INTO ttrss_feed_categories (owner_uid,title,parent_cat)
-                                       VALUES ('".$_SESSION["uid"]."', '$feed_cat', $parent_insert)");
+                       $sth = $pdo->prepare("INSERT INTO ttrss_feed_categories (owner_uid,title,parent_cat)
+                                       VALUES (?, ?, ?)");
+                       $sth->execute([$_SESSION['uid'], $feed_cat, $parent_cat_id]);
 
-                       db_query("COMMIT");
+                       $pdo->commit();
 
                        return true;
                }
 
+        $pdo->commit();
+
                return false;
        }
 
 
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
-               $sql_is_cat = bool_to_sql_bool($is_cat);
+               $pdo = Db::pdo();
 
-               $result = db_query("SELECT access_key FROM ttrss_access_keys
-                               WHERE feed_id = '$feed_id'      AND is_cat = $sql_is_cat
-                               AND owner_uid = " . $owner_uid);
+               $sth = $dbh->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]);
 
-               if (db_num_rows($result) == 1) {
-                       return db_fetch_result($result, 0, "access_key");
+               if ($row = $sth->fetch()) {
+                       return $row["access_key"];
                } else {
-                       $key = db_escape_string(uniqid_short());
+                       $key = uniqid_short();
 
-                       $result = db_query("INSERT INTO ttrss_access_keys
+                       $sth = $pdo->prepare("INSERT INTO ttrss_access_keys
                                        (access_key, feed_id, is_cat, owner_uid)
-                                       VALUES ('$key', '$feed_id', $sql_is_cat, '$owner_uid')");
+                                       VALUES (?, ?, ?, ?)");
+
+                       $sth->execute([$key, $feed_id, $is_cat, $owner_uid]);
 
                        return $key;
                }
-               return false;
        }
 
        function get_feeds_from_html($url, $content)
 
        function cleanup_tags($days = 14, $limit = 1000) {
 
+           $days = (int) $days;
+
                if (DB_TYPE == "pgsql") {
                        $interval_query = "date_updated < NOW() - INTERVAL '$days days'";
                } else if (DB_TYPE == "mysql") {
 
                $tags_deleted = 0;
 
-               while ($limit > 0) {
+        $pdo = Db::pdo();
+
+        while ($limit > 0) {
                        $limit_part = 500;
 
-                       $query = "SELECT ttrss_tags.id AS id
+                       $sth = $pdo->prepare("SELECT ttrss_tags.id AS id
                                        FROM ttrss_tags, ttrss_user_entries, ttrss_entries
                                        WHERE post_int_id = int_id AND $interval_query AND
-                                       ref_id = ttrss_entries.id AND tag_cache != '' LIMIT $limit_part";
-
-                       $result = db_query($query);
+                                       ref_id = ttrss_entries.id AND tag_cache != '' LIMIT ?");
+                       $sth->execute([$limit]);
 
                        $ids = array();
 
-                       while ($line = db_fetch_assoc($result)) {
+                       while ($line = $sth->fetch()) {
                                array_push($ids, $line['id']);
                        }
 
                        if (count($ids) > 0) {
                                $ids = join(",", $ids);
 
-                               $tmp_result = db_query("DELETE FROM ttrss_tags WHERE id IN ($ids)");
-                               $tags_deleted += db_affected_rows($tmp_result);
+                               $usth = $pdo->query("DELETE FROM ttrss_tags WHERE id IN ($ids)");
+                               $tags_deleted = $usth->rowCount();
                        } else {
                                break;
                        }
        }
 
        function check_mysql_tables() {
-               $schema = db_escape_string(DB_NAME);
+               $pdo = Db::pdo();
 
-               $result = db_query("SELECT engine, table_name FROM information_schema.tables WHERE
-                       table_schema = '$schema' AND table_name LIKE 'ttrss_%' AND engine != 'InnoDB'");
+               $sth = $pdo->prepare("SELECT engine, table_name FROM information_schema.tables WHERE
+                       table_schema = ? AND table_name LIKE 'ttrss_%' AND engine != 'InnoDB'");
+               $sth->execute([DB_NAME]);
 
                $bad_tables = [];
 
-               while ($line = db_fetch_assoc($result)) {
+               while ($line = $sth->fetch()) {
                        array_push($bad_tables, $line);
                }
 
                return $bad_tables;
        }
 
+    function arr_qmarks($arr) {
+        return str_repeat('?,', count($arr) - 1) . '?';
+    }