]> git.wh0rd.org - tt-rss.git/blobdiff - classes/rssutils.php
add a workaround to support numeric tags
[tt-rss.git] / classes / rssutils.php
old mode 100644 (file)
new mode 100755 (executable)
index bd59914..d125a50
@@ -16,6 +16,11 @@ class RSSUtils {
                return sha1(implode(",", $pluginhost->get_plugin_names()) . $tmp);
        }
 
+       // Strips utf8mb4 characters (i.e. emoji) for mysql
+       static function strip_utf8mb4($str) {
+               return preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $str);
+       }
+
        static function update_feedbrowser_cache() {
 
                $pdo = Db::pdo();
@@ -304,7 +309,7 @@ class RSSUtils {
         */
        static function update_rss_feed($feed, $no_cache = false) {
 
-               $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug'];
+               $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || clean($_REQUEST['xdebug']);
 
                _debug_suppress(!$debug_enabled);
                _debug("start", $debug_enabled);
@@ -417,15 +422,15 @@ class RSSUtils {
                                _debug("not using CURL due to open_basedir restrictions");
                        }
 
-            if (time() - strtotime($last_unconditional) > MAX_CONDITIONAL_INTERVAL) {
-                _debug("maximum allowed interval for conditional requests exceeded, forcing refetch");
+                       if (time() - strtotime($last_unconditional) > MAX_CONDITIONAL_INTERVAL) {
+                               _debug("maximum allowed interval for conditional requests exceeded, forcing refetch");
 
-                $force_refetch = true;
-            } else {
-                _debug("stored last modified for conditional request: $stored_last_modified", $debug_enabled);
-            }
+                               $force_refetch = true;
+                       } else {
+                               _debug("stored last modified for conditional request: $stored_last_modified", $debug_enabled);
+                       }
 
-            _debug("fetching [$fetch_url] (force_refetch: $force_refetch)...", $debug_enabled);
+                       _debug("fetching [$fetch_url] (force_refetch: $force_refetch)...", $debug_enabled);
 
                        $feed_data = fetch_file_contents([
                                "url" => $fetch_url,
@@ -567,8 +572,8 @@ class RSSUtils {
                        $filters = load_filters($feed, $owner_uid);
 
                        if ($debug_enabled) {
-                           print_r($filters);
-            }
+                               print_r($filters);
+                       }
 
                        _debug("" . count($filters) . " filters loaded.", $debug_enabled);
 
@@ -591,7 +596,7 @@ class RSSUtils {
                        foreach ($items as $item) {
                                $pdo->beginTransaction();
 
-                               if ($_REQUEST['xdebug'] == 3) {
+                               if (clean($_REQUEST['xdebug']) == 3) {
                                        print_r($item);
                                }
 
@@ -640,7 +645,7 @@ class RSSUtils {
                                $entry_content = $item->get_content();
                                if (!$entry_content) $entry_content = $item->get_description();
 
-                               if ($_REQUEST["xdebug"] == 2) {
+                               if (clean($_REQUEST["xdebug"]) == 2) {
                                        print "content: ";
                                        print htmlspecialchars($entry_content);
                                        print "\n";
@@ -670,9 +675,13 @@ class RSSUtils {
 
                                $entry_tags = array_unique($additional_tags);
 
-                               for ($i = 0; $i < count($entry_tags); $i++)
+                               for ($i = 0; $i < count($entry_tags); $i++) {
                                        $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8');
 
+                                       // we don't support numeric tags, let's prefix them
+                                       if (is_numeric($entry_tags[$i])) $entry_tags[$i] = 't:' . $entry_tags[$i];
+                               }
+
                                _debug("tags found: " . join(",", $entry_tags), $debug_enabled);
 
                                _debug("done collecting data.", $debug_enabled);
@@ -749,7 +758,7 @@ class RSSUtils {
                                        $entry_plugin_data .= mb_strtolower(get_class($plugin)) . ",";
                                }
 
-                               if ($_REQUEST["xdebug"] == 2) {
+                               if (clean($_REQUEST["xdebug"]) == 2) {
                                        print "processed content: ";
                                        print htmlspecialchars($article["content"]);
                                        print "\n";
@@ -760,10 +769,9 @@ class RSSUtils {
                                // Workaround: 4-byte unicode requires utf8mb4 in MySQL. See https://tt-rss.org/forum/viewtopic.php?f=1&t=3377&p=20077#p20077
                                if (DB_TYPE == "mysql") {
                                        foreach ($article as $k => $v) {
-
                                                // i guess we'll have to take the risk of 4byte unicode labels & tags here
                                                if (is_string($article[$k])) {
-                                                       $article[$k] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $v);
+                                                       $article[$k] = RSSUtils::strip_utf8mb4($v);
                                                }
                                        }
                                }
@@ -930,7 +938,7 @@ class RSSUtils {
                                                        $last_read_qpart = null;
                                                } else {
                                                        $unread = 0;
-                                                       $last_read_qpart = 'NOW()';
+                                                       $last_read_qpart = date("Y-m-d H:i"); // we can't use NOW() here because it gets quoted
                                                }
 
                                                if (RSSUtils::find_article_filter($article_filters, 'mark') || $score > 1000) {
@@ -945,18 +953,18 @@ class RSSUtils {
                                                        $published = 0;
                                                }
 
-                                               $last_marked = ($marked == 'true') ? 'NOW()' : null;
-                                               $last_published = ($published == 'true') ? 'NOW()' : null;
+                                               $last_marked = ($marked == 1) ? 'NOW()' : 'NULL';
+                                               $last_published = ($published == 1) ? 'NOW()' : 'NULL';
 
                                                $sth = $pdo->prepare(
                                                        "INSERT INTO ttrss_user_entries
                                                                (ref_id, owner_uid, feed_id, unread, last_read, marked,
                                                                published, score, tag_cache, label_cache, uuid,
                                                                last_marked, last_published)
-                                                       VALUES (?, ?, ?, ?, ?, ?, ?, ?, '', '', '', ?, ?)");
+                                                       VALUES (?, ?, ?, ?, ?, ?, ?, ?, '', '', '', ".$last_marked.", ".$last_published.")");
 
                                                $sth->execute([$ref_id, $owner_uid, $feed, $unread, $last_read_qpart, $marked,
-                                                       $published, $score, $last_marked, $last_published]);
+                                                       $published, $score]);
 
                                                $sth = $pdo->prepare("SELECT int_id FROM ttrss_user_entries WHERE
                                                                ref_id = ? AND owner_uid = ? AND
@@ -1043,6 +1051,16 @@ class RSSUtils {
                                                $e_item = array(
                                                        rewrite_relative_url($site_url, $e->link),
                                                        $e->type, $e->length, $e->title, $e->width, $e->height);
+
+                                               // Yet another episode of "mysql utf8_general_ci is gimped"
+                                               if (DB_TYPE == "mysql") {
+                                                       for ($i = 0; $i < count($e_item); $i++) {
+                                                               if (is_string($e_item[$i])) {
+                                                                       $e_item[$i] = RSSUtils::strip_utf8mb4($e_item[$i]);
+                                                               }
+                                                       }
+                                               }
+
                                                array_push($enclosures, $e_item);
                                        }
                                }