]> git.wh0rd.org - tt-rss.git/blobdiff - include/functions.php
Merge branch 'pda-rss-now' of richm/tt-rss into pdo-experimental
[tt-rss.git] / include / functions.php
index e366a49650306d93a849f4a9896681608dadff1b..e1e63c2a36f097584fbaf95b8c876a069b7ea36e 100644 (file)
@@ -57,7 +57,7 @@
        // do not cache files smaller than that (bytes)
        define_default('CACHE_MAX_DAYS', 7);
        // max age in days for various automatically cached (temporary) files
-    define_default('MAX_CONDITIONAL_INTERVAL', 3600*6);
+    define_default('MAX_CONDITIONAL_INTERVAL', 3600*12);
     // max interval between forced unconditional updates for servers
     // not complying with http if-modified-since (seconds)
 
                $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;
 
                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 ?");
-                       $sth->execute([$feed_id, "$purge_interval days"]);
+                               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 = "";
 
-               ////db_query("BEGIN");
-
         $pdo = DB::pdo();
+        $in_nested_tr = false;
+
+        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
                        }
                }
 
-               ////db_query("COMMIT");
+               if (!$in_nested_tr) $pdo->commit();
 
        }
 
                }
        }
 
+       // this is used for user http parameters unless HTML code is actually needed
+       function clean($param) {
+               if (is_array($param)) {
+                       return array_map(strip_tags, $param);
+               } else if (is_string($param)) {
+                       return strip_tags($param);
+               } else {
+                       return $param;
+               }
+       }
+
        function make_password($length = 8) {
 
                $password = "";
        }
 
        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 $s ? 1 : 0;
        }
 
        // Session caching removed due to causing wrong redirects to upgrade
                        $error_code = 5;
                }
 
-               if (db_escape_string("testTEST") != "testTEST") {
-                       $error_code = 12;
-               }
-
                return array("code" => $error_code, "message" => $ERRORS[$error_code]);
        }
 
        }
 
        function checkbox_to_sql_bool($val) {
-               return ($val == "on") ? "true" : "false";
+               return ($val == "on") ? 1 : 0;
        }
 
        function uniqid_short() {
                $params["default_view_limit"] = (int) get_pref("_DEFAULT_VIEW_LIMIT");
                $params["default_view_order_by"] = get_pref("_DEFAULT_VIEW_ORDER_BY");
                $params["bw_limit"] = (int) $_SESSION["bw_limit"];
+               $params["is_default_pw"] = Pref_Prefs::isdefaultpassword();
                $params["label_base_index"] = (int) LABEL_BASE_INDEX;
 
                $theme = get_pref( "USER_CSS_THEME", false, false);
                $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";
 
                        switch ($commandpair[0]) {
                                case "title":
                                        if ($commandpair[1]) {
-                                               array_push($query_keywords, "($not (LOWER(ttrss_entries.title) LIKE '%".
-                                                       db_escape_string(mb_strtolower($commandpair[1]))."%'))");
+                                               array_push($query_keywords, "($not (LOWER(ttrss_entries.title) LIKE ".
+                                                       $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%'))");
+                                                               OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
                                                array_push($search_words, $k);
                                        }
                                        break;
                                case "author":
                                        if ($commandpair[1]) {
-                                               array_push($query_keywords, "($not (LOWER(author) LIKE '%".
-                                                       db_escape_string(mb_strtolower($commandpair[1]))."%'))");
+                                               array_push($query_keywords, "($not (LOWER(author) LIKE ".
+                                                       $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%'))");
+                                                               OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
                                                array_push($search_words, $k);
                                        }
                                        break;
                                                else if ($commandpair[1] == "false")
                                                        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]))."%'))");
+                                                       array_push($query_keywords, "($not (LOWER(note) LIKE ".
+                                                               $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, "(UPPER(ttrss_entries.title) $not LIKE UPPER(".$pdo->quote("%$k%").")
+                                                               OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
                                                if (!$not) array_push($search_words, $k);
                                        }
                                        break;
                                                else
                                                        array_push($query_keywords, "($not (marked = false))");
                                        } 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, "(UPPER(ttrss_entries.title) $not LIKE UPPER(".$pdo->quote("%$k%").")
+                                                               OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
                                                if (!$not) array_push($search_words, $k);
                                        }
                                        break;
 
                                        } else {
                                                array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
-                                                               OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
+                                                               OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
                                                if (!$not) array_push($search_words, $k);
                                        }
                                        break;
                                                        array_push($query_keywords, "($not (unread = false))");
 
                                        } 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, "(UPPER(ttrss_entries.title) $not LIKE UPPER(".$pdo->quote("%$k%").")
+                                                               OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
                                                if (!$not) array_push($search_words, $k);
                                        }
                                        break;
                                                        $k = mb_strtolower($k);
                                                        array_push($search_query_leftover, $not ? "!$k" : $k);
                                                } 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, "(UPPER(ttrss_entries.title) $not LIKE UPPER(".$pdo->quote("%$k%").")
+                                                               OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
                                                }
 
                                                if (!$not) array_push($search_words, $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,
-                                       "(tsvector_combined @@ to_tsquery('$search_language', '$search_query_leftover'))");
+                                       "(tsvector_combined @@ to_tsquery($search_language, $search_query_leftover))");
                        }
 
                }
        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)
                }
        }
 
-       function feed_has_icon($id) {
-               return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
-       }
-
        function init_plugins() {
                PluginHost::getInstance()->load(PLUGINS, PluginHost::KIND_ALL);
 
 
                if (!$feed_cat) return false;
 
-               ////db_query("BEGIN");
-
                $feed_cat = mb_substr($feed_cat, 0, 250);
+               if (!$parent_cat_id) $parent_cat_id = null;
 
                $pdo = Db::pdo();
+               $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]);
 
-                       //db_query("COMMIT");
+                       if (!$tr_in_progress) $pdo->commit();
 
                        return true;
                }
 
+        $pdo->commit();
+
                return false;
        }
 
 
                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]);
+               $sth->execute([$feed_id, (int)$is_cat, $owner_uid]);
 
                if ($row = $sth->fetch()) {
                        return $row["access_key"];
                                        (access_key, feed_id, is_cat, owner_uid)
                                        VALUES (?, ?, ?, ?)");
 
-                       $sth->execute([$key, $feed_id, $is_cat, $owner_uid]);
+                       $sth->execute([$key, $feed_id, (int)$is_cat, $owner_uid]);
 
                        return $key;
                }
        function filter_to_sql($filter, $owner_uid) {
                $query = array();
 
+               $pdo = Db::pdo();
+
                if (DB_TYPE == "pgsql")
                        $reg_qpart = "~";
                else
 
                        if ($regexp_valid) {
 
-                               $rule['reg_exp'] = db_escape_string($rule['reg_exp']);
+                               $rule['reg_exp'] = $pdo->quote($rule['reg_exp']);
 
                                switch ($rule["type"]) {
                                        case "title":
                                if (isset($rule['inverse'])) $qpart = "NOT ($qpart)";
 
                                if (isset($rule["feed_id"]) && $rule["feed_id"] > 0) {
-                                       $qpart .= " AND feed_id = " . db_escape_string($rule["feed_id"]);
+                                       $qpart .= " AND feed_id = " . $pdo->quote($rule["feed_id"]);
                                }
 
                                if (isset($rule["cat_id"])) {
 
                foreach ($files as $js) {
                        if (!isset($_GET['debug'])) {
-                               $cached_file = CACHE_DIR . "/js/".basename($js).".js";
+                               $cached_file = CACHE_DIR . "/js/".basename($js);
 
-                               if (file_exists($cached_file) && is_readable($cached_file) && filemtime($cached_file) >= filemtime("js/$js.js")) {
+                               if (file_exists($cached_file) && is_readable($cached_file) && filemtime($cached_file) >= filemtime("js/$js")) {
 
                                        list($header, $contents) = explode("\n", file_get_contents($cached_file), 2);
 
                                        }
                                }
 
-                               $minified = JShrink\Minifier::minify(file_get_contents("js/$js.js"));
+                               $minified = JShrink\Minifier::minify(file_get_contents("js/$js"));
                                file_put_contents($cached_file, "tt-rss:" . VERSION . "\n" . $minified);
                                $rv .= $minified;
 
                        } else {
-                               $rv .= file_get_contents("js/$js.js"); // no cache in debug mode
+                               $rv .= file_get_contents("js/$js"); // no cache in debug mode
                        }
                }
 
        }
 
        function get_theme_path($theme) {
+               if ($theme == "default.php")
+                       return "css/default.css";
+
                $check = "themes/$theme";
                if (file_exists($check)) return $check;
 
                return $bad_tables;
        }
 
+       function validate_field($string, $allowed, $default = "") {
+               if (in_array($string, $allowed))
+                       return $string;
+               else
+                       return $default;
+       }
+
     function arr_qmarks($arr) {
         return str_repeat('?,', count($arr) - 1) . '?';
     }