]> git.wh0rd.org - tt-rss.git/blobdiff - include/functions2.php
fix various issues reported by static analysis
[tt-rss.git] / include / functions2.php
index f0e352ed100cf47692b8dfe6c719fed16f083fbc..f9ee8245f2bfd4df85eae76532420ea5836bfc82 100644 (file)
 
                $params['simple_update'] = defined('SIMPLE_UPDATE_MODE') && SIMPLE_UPDATE_MODE;
 
+               $params["icon_alert"] = base64_img("images/alert.png");
+               $params["icon_information"] = base64_img("images/information.png");
+               $params["icon_cross"] = base64_img("images/cross.png");
+               $params["icon_indicator_white"] = base64_img("images/indicator_white.gif");
+
                return $params;
        }
 
                $doc->loadHTML($charset_hack . $res);
                $xpath = new DOMXPath($doc);
 
-               $entries = $xpath->query('(//a[@href]|//img[@src])');
-
                $ttrss_uses_https = parse_url(get_self_url_prefix(), PHP_URL_SCHEME) === 'https';
+               $rewrite_base_url = $site_url ? $site_url : SELF_URL_PATH;
+
+               $entries = $xpath->query('(//a[@href]|//img[@src]|//video/source[@src]|//audio/source[@src])');
 
                foreach ($entries as $entry) {
 
-                       if ($site_url) {
+                       if ($entry->hasAttribute('href')) {
+                               $entry->setAttribute('href',
+                                       rewrite_relative_url($rewrite_base_url, $entry->getAttribute('href')));
+
+                               $entry->setAttribute('rel', 'noopener noreferrer');
+                       }
+
+                       if ($entry->hasAttribute('src')) {
+                               $src = rewrite_relative_url($rewrite_base_url, $entry->getAttribute('src'));
+                               $cached_filename = CACHE_DIR . '/images/' . sha1($src);
+
+                               if (file_exists($cached_filename)) {
+
+                                       // this is strictly cosmetic
+                                       if ($entry->tagName == 'img') {
+                                               $suffix = ".png";
+                                       } else if ($entry->parentNode && $entry->parentNode->tagName == "video") {
+                                               $suffix = ".mp4";
+                                       } else if ($entry->parentNode && $entry->parentNode->tagName == "audio") {
+                                               $suffix = ".ogg";
+                                       } else {
+                                               $suffix = "";
+                                       }
+
+                                       $src = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($src) . $suffix;
 
-                               if ($entry->hasAttribute('href')) {
-                                       $entry->setAttribute('href',
-                                               rewrite_relative_url($site_url, $entry->getAttribute('href')));
+                                       if ($entry->hasAttribute('srcset')) {
+                                               $entry->removeAttribute('srcset');
+                                       }
 
-                                       $entry->setAttribute('rel', 'noopener noreferrer');
+                                       if ($entry->hasAttribute('sizes')) {
+                                               $entry->removeAttribute('sizes');
+                                       }
                                }
 
-                               if ($entry->hasAttribute('src')) {
-                                       $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
+                               $entry->setAttribute('src', $src);
+                       }
 
-                                       $cached_filename = CACHE_DIR . '/images/' . sha1($src) . '.png';
+                       if ($entry->nodeName == 'img') {
 
-                                       if (file_exists($cached_filename)) {
-                                               $src = SELF_URL_PATH . '/public.php?op=cached_image&hash=' . sha1($src);
+                               if ($entry->hasAttribute('src')) {
+                                       $is_https_url = parse_url($entry->getAttribute('src'), PHP_URL_SCHEME) === 'https';
+
+                                       if ($ttrss_uses_https && !$is_https_url) {
 
                                                if ($entry->hasAttribute('srcset')) {
                                                        $entry->removeAttribute('srcset');
                                                        $entry->removeAttribute('sizes');
                                                }
                                        }
-
-                                       $entry->setAttribute('src', $src);
                                }
 
-                               if ($entry->nodeName == 'img') {
-                                       if ($entry->hasAttribute('src')) {
-                                               $is_https_url = parse_url($entry->getAttribute('src'), PHP_URL_SCHEME) === 'https';
+                               if (($owner && get_pref("STRIP_IMAGES", $owner)) ||
+                                               $force_remove_images || $_SESSION["bw_limit"]) {
 
-                                               if ($ttrss_uses_https && !$is_https_url) {
+                                       $p = $doc->createElement('p');
 
-                                                       if ($entry->hasAttribute('srcset')) {
-                                                               $entry->removeAttribute('srcset');
-                                                       }
+                                       $a = $doc->createElement('a');
+                                       $a->setAttribute('href', $entry->getAttribute('src'));
 
-                                                       if ($entry->hasAttribute('sizes')) {
-                                                               $entry->removeAttribute('sizes');
-                                                       }
-                                               }
-                                       }
-
-                                       if (($owner && get_pref("STRIP_IMAGES", $owner)) ||
-                                                       $force_remove_images || $_SESSION["bw_limit"]) {
-
-                                               $p = $doc->createElement('p');
+                                       $a->appendChild(new DOMText($entry->getAttribute('src')));
+                                       $a->setAttribute('target', '_blank');
+                                       $a->setAttribute('rel', 'noopener noreferrer');
 
-                                               $a = $doc->createElement('a');
-                                               $a->setAttribute('href', $entry->getAttribute('src'));
+                                       $p->appendChild($a);
 
-                                               $a->appendChild(new DOMText($entry->getAttribute('src')));
-                                               $a->setAttribute('target', '_blank');
-
-                                               $p->appendChild($a);
-
-                                               $entry->parentNode->replaceChild($p, $entry);
-                                       }
+                                       $entry->parentNode->replaceChild($p, $entry);
                                }
                        }
 
                        if (strtolower($entry->nodeName) == "a") {
                                $entry->setAttribute("target", "_blank");
+                               $entry->setAttribute("rel", "noopener noreferrer");
                        }
                }
 
                $allowed_elements = array('a', 'address', 'acronym', 'audio', 'article', 'aside',
                        'b', 'bdi', 'bdo', 'big', 'blockquote', 'body', 'br',
                        'caption', 'cite', 'center', 'code', 'col', 'colgroup',
-                       'data', 'dd', 'del', 'details', 'description', 'div', 'dl', 'font',
+                       'data', 'dd', 'del', 'details', 'description', 'dfn', 'div', 'dl', 'font',
                        'dt', 'em', 'footer', 'figure', 'figcaption',
                        'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'html', 'i',
                        'img', 'ins', 'kbd', 'li', 'main', 'mark', 'nav', 'noscript',
                                        </object>";
                        }
 
-                       if ($entry) $entry .= "&nbsp; <a target=\"_blank\"
+                       if ($entry) $entry .= "&nbsp; <a target=\"_blank\" rel=\"noopener noreferrer\"
                                href=\"$url\">" . basename($url) . "</a>";
 
                        return $entry;
 
 /*             $filename = substr($url, strrpos($url, "/")+1);
 
-               $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
+               $entry .= " <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"" . htmlspecialchars($url) . "\">" .
                        $filename . " (" . $ctype . ")" . "</a>"; */
 
        }
                        num_comments,
                        tag_cache,
                        author,
+                       guid,
                        orig_feed_id,
                        note
                        FROM ttrss_entries,ttrss_user_entries
                                        $comments_url = htmlspecialchars($line["link"]);
                                }
                                $entry_comments = "<a class=\"postComments\"
-                                       target='_blank' href=\"$comments_url\">$num_comments ".
+                                       target='_blank' rel=\"noopener noreferrer\" href=\"$comments_url\">$num_comments ".
                                        _ngettext("comment", "comments", $num_comments)."</a>";
 
                        } else {
                                if ($line["comments"] && $line["link"] != $line["comments"]) {
-                                       $entry_comments = "<a class=\"postComments\" target='_blank' href=\"".htmlspecialchars($line["comments"])."\">".__("comments")."</a>";
+                                       $entry_comments = "<a class=\"postComments\" target='_blank' rel=\"noopener noreferrer\" href=\"".htmlspecialchars($line["comments"])."\">".__("comments")."</a>";
                                }
                        }
 
                                $rv['content'] .= "<div class=\"postDate\">$parsed_updated</div>";
 
                        if ($line["link"]) {
-                               $rv['content'] .= "<div class='postTitle'><a target='_blank'
+                               $rv['content'] .= "<div class='postTitle'><a target='_blank' rel='noopener noreferrer'
                                        title=\"".htmlspecialchars($line['title'])."\"
                                        href=\"" .
                                        htmlspecialchars($line["link"]) . "\">" .
 
                                        $tmp_line = db_fetch_assoc($tmp_result);
 
-                                       $rv['content'] .= "<a target='_blank'
+                                       $rv['content'] .= "<a target='_blank' rel='noopener noreferrer'
                                                href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
                                                $tmp_line['title'] . "</a>";
 
                                        $rv['content'] .= "&nbsp;";
 
-                                       $rv['content'] .= "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
+                                       $rv['content'] .= "<a target='_blank' rel='noopener noreferrer' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
                                        $rv['content'] .= "<img title='".__('Feed URL')."' class='tinyFeedIcon' src='images/pub_set.png'></a>";
 
                                        $rv['content'] .= "</div>";
                }
        } // function encrypt_password
 
-       function load_filters($feed_id, $owner_uid, $action_id = false) {
+       function load_filters($feed_id, $owner_uid) {
                $filters = array();
 
                $cat_id = (int)getFeedCategory($feed_id);
                return true;
        }
 
-       function format_tags_string($tags, $id) {
+       function format_tags_string($tags) {
                if (!is_array($tags) || count($tags) == 0) {
                        return __("no tags");
                } else {
                }
        }
 
-       function format_article_labels($labels, $id) {
+       function format_article_labels($labels) {
 
                if (!is_array($labels)) return '';
 
 
                if (db_num_rows($result) > 0) {
                        while ($line = db_fetch_assoc($result)) {
+
+                               if (file_exists(CACHE_DIR . '/images/' . sha1($line["content_url"]))) {
+                                       $line["content_url"] = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($line["content_url"]);
+                               }
+
                                array_push($rv, $line);
                        }
                }
                        WHERE owner_uid = '".$_SESSION["uid"]."' ORDER BY caption");
 
                print "<select default=\"$value\" name=\"" . htmlspecialchars($name) .
-                       "\" $attributes onchange=\"labelSelectOnChange(this)\" >";
+                       "\" $attributes>";
 
                while ($line = db_fetch_assoc($result)) {
 
 
                        foreach ($result as $line) {
 
+                               foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ENCLOSURE_ENTRY) as $plugin) {
+                                       $line = $plugin->hook_enclosure_entry($line);
+                               }
+                               
                                $url = $line["content_url"];
                                $ctype = $line["content_type"];
                                $title = $line["title"];
 
                                if ($player) array_push($entries_inline, $player);
 
-#                              $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
+#                              $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\" rel=\"noopener noreferrer\">" .
 #                                      $filename . " (" . $ctype . ")" . "</a>";
 
-                               $entry = "<div onclick=\"window.open('".htmlspecialchars($url)."')\"
+                               $entry = "<div onclick=\"openUrlPopup('".htmlspecialchars($url)."')\"
                                        dojoType=\"dijit.MenuItem\">$filename ($ctype)</div>";
 
                                array_push($entries_html, $entry);
                                                        $rv .= $retval;
                                                } else {
 
-                                                       if (preg_match("/image/", $entry["type"]) ||
-                                                                       preg_match("/\.(jpg|png|gif|bmp)/i", $entry["filename"])) {
+                                                       if (preg_match("/image/", $entry["type"])) {
 
                                                                        if (!$hide_images) {
                                                                                $encsize = '';
                                                                                src=\"" .htmlspecialchars($entry["url"]) . "\"
                                                                                " . $encsize . " /></p>";
                                                                        } else {
-                                                                               $rv .= "<p><a target=\"_blank\"
+                                                                               $rv .= "<p><a target=\"_blank\" rel=\"noopener noreferrer\"
                                                                                href=\"".htmlspecialchars($entry["url"])."\"
                                                                                >" .htmlspecialchars($entry["url"]) . "</a></p>";
                                                                        }
                                else
                                        $filename = "";
 
-                               $rv .= "<div onclick='window.open(\"".htmlspecialchars($entry["url"])."\")'
+                               $rv .= "<div onclick='openUrlPopup(\"".htmlspecialchars($entry["url"])."\")'
                                        dojoType=\"dijit.MenuItem\">".$filename . $title."</div>";
 
                        };
                return $parts['scheme'] . "://" . $parts['host'] . $parts['path'];
        }
 
+       function cleanup_url_path($path) {
+               $path = str_replace("/./", "/", $path);
+               $path = str_replace("//", "/", $path);
+
+               return $path;
+       }
+
        /**
         * Converts a (possibly) relative URL to a absolute one.
         *
                } else if (strpos($rel_url, "/") === 0) {
                        $parts = parse_url($url);
                        $parts['path'] = $rel_url;
+                       $parts['path'] = cleanup_url_path($parts['path']);
 
                        return build_url($parts);
 
                                $dir !== '/' && $dir .= '/';
                        }
                        $parts['path'] = $dir . $rel_url;
+                       $parts['path'] = cleanup_url_path($parts['path']);
 
                        return build_url($parts);
                }
        }
 
        function theme_valid($theme) {
-               if ($theme == "default.css" || $theme == "night.css") return true; // needed for array_filter
+               $bundled_themes = [ "default.php", "night.css", "compact.css" ];
+               
+               if (in_array($theme, $bundled_themes)) return true;
+
                $file = "themes/" . basename($theme);
 
                if (!file_exists($file)) $file = "themes.local/" . basename($theme);
                return false;
        }
 
+       /**
+        * @SuppressWarnings(unused)
+        */
        function error_json($code) {
                require_once "errors.php";
 
 
                return $errors[$code];
        }
+
+       function base64_img($filename) {
+               if (file_exists($filename)) {
+                        $ext = pathinfo($filename, PATHINFO_EXTENSION);
+
+                       return "data:image/$ext;base64," . base64_encode(file_get_contents($filename));
+               } else {
+                       return "";
+               }
+       }
 ?>