X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=include%2Ffunctions.php;h=8ede14a0bfa7a188b036d8ac704aff70dd7c22a9;hb=a33558a61efc244ad8c809748e6ee64413e3a217;hp=6ceb20adc470993b1a4c858b1aff98dff7d5ae45;hpb=dd90eb2c7a1164ed8924c83c13138d568db5ea4e;p=tt-rss.git diff --git a/include/functions.php b/include/functions.php index 6ceb20ad..8ede14a0 100644 --- a/include/functions.php +++ b/include/functions.php @@ -139,6 +139,8 @@ * @return void */ function _debug($msg, $show = true) { + if (defined('SUPPRESS_DEBUGGING')) + return false; $ts = strftime("%H:%M:%S", time()); if (function_exists('posix_getpid')) { @@ -153,7 +155,29 @@ $fp = fopen(LOGFILE, 'a+'); if ($fp) { + $locked = false; + + if (function_exists("flock")) { + $tries = 0; + + // try to lock logfile for writing + while ($tries < 5 && !$locked = flock($fp, LOCK_EX | LOCK_NB)) { + sleep(1); + ++$tries; + } + + if (!$locked) { + fclose($fp); + return; + } + } + fputs($fp, "[$ts] $msg\n"); + + if (function_exists("flock")) { + flock($fp, LOCK_UN); + } + fclose($fp); } } @@ -2760,14 +2784,11 @@ } - function sanitize($str, $force_remove_images = false, $owner = false, $site_url = false, $highlight_words = false) { + function sanitize($str, $force_remove_images = false, $owner = false, $site_url = false, $highlight_words = false, $article_id = false) { if (!$owner) $owner = $_SESSION["uid"]; $res = trim($str); if (!$res) return ''; - if (strpos($res, "href=") === false) - $res = rewrite_urls($res); - $charset_hack = ' '; @@ -2849,7 +2870,7 @@ $disallowed_attributes = array('id', 'style', 'class'); foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SANITIZE) as $plugin) { - $retval = $plugin->hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes); + $retval = $plugin->hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id); if (is_array($retval)) { $doc = $retval[0]; $allowed_elements = $retval[1]; @@ -2865,31 +2886,29 @@ if ($highlight_words) { foreach ($highlight_words as $word) { - $elements = $xpath->query('//*[contains(.,"'.$word.'")]'); + // http://stackoverflow.com/questions/4081372/highlight-keywords-in-a-paragraph - foreach ($elements as $element) { - foreach ($element->childNodes as $child) { + $elements = $xpath->query("//*/text()"); - if (!$child instanceof DomText) continue; + foreach ($elements as $child) { - $fragment = $doc->createDocumentFragment(); - $text = $child->textContent; - $stubs = array(); + $fragment = $doc->createDocumentFragment(); + $text = $child->textContent; + $stubs = array(); - while (($pos = stripos($text, $word)) !== false) { - $fragment->appendChild(new DomText(substr($text, 0, $pos))); - $word = substr($text, $pos, strlen($word)); - $highlight = $doc->createElement('span'); - $highlight->appendChild(new DomText($word)); - $highlight->setAttribute('class', 'highlight'); - $fragment->appendChild($highlight); - $text = substr($text, $pos + strlen($word)); - } + while (($pos = mb_stripos($text, $word)) !== false) { + $fragment->appendChild(new DomText(mb_substr($text, 0, $pos))); + $word = mb_substr($text, $pos, mb_strlen($word)); + $highlight = $doc->createElement('span'); + $highlight->appendChild(new DomText($word)); + $highlight->setAttribute('class', 'highlight'); + $fragment->appendChild($highlight); + $text = mb_substr($text, $pos + mb_strlen($word)); + } - if (!empty($text)) $fragment->appendChild(new DomText($text)); + if (!empty($text)) $fragment->appendChild(new DomText($text)); - $element->replaceChild($fragment, $child); - } + $child->parentNode->replaceChild($fragment, $child); } } } @@ -3044,10 +3063,6 @@ if (preg_match("/^[0-9]*$/", $tag)) return false; if (mb_strlen($tag) > 250) return false; - if (function_exists('iconv')) { - $tag = iconv("utf-8", "utf-8", $tag); - } - if (!$tag) return false; return true; @@ -3188,7 +3203,7 @@ $line["content"] = sanitize($line["content"], sql_bool_to_bool($line['hide_images']), - $owner_uid, $line["site_url"]); + $owner_uid, $line["site_url"], false, $line["id"]); foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE) as $p) { $line = $p->hook_render_article($line); @@ -3420,6 +3435,11 @@ $cat_id = (int)getFeedCategory($feed_id); + if ($cat_id == 0) + $null_cat_qpart = "cat_id IS NULL OR"; + else + $null_cat_qpart = ""; + $result = db_query("SELECT * FROM ttrss_filters2 WHERE owner_uid = $owner_uid AND enabled = true ORDER BY order_id, title"); @@ -3435,7 +3455,7 @@ FROM ttrss_filters2_rules AS r, ttrss_filter_types AS t WHERE - (cat_id IS NULL OR cat_id IN ($check_cats)) AND + ($null_cat_qpart (cat_id IS NULL AND cat_filter = false) OR cat_id IN ($check_cats)) AND (feed_id IS NULL OR feed_id = '$feed_id') AND filter_type = t.id AND filter_id = '$filter_id'"); @@ -3781,6 +3801,7 @@ $url = $line["content_url"]; $ctype = $line["content_type"]; + $title = $line["title"]; if (!$ctype) $ctype = __("unknown type"); @@ -3803,6 +3824,7 @@ $entry["type"] = $ctype; $entry["filename"] = $filename; $entry["url"] = $url; + $entry["title"] = $title; array_push($entries, $entry); } @@ -3824,7 +3846,10 @@ $rv .= "

" .htmlspecialchars($entry["url"]) . "

"; + } + if ($entry['title']) { + $rv.= "
${entry['title']}
"; } } } @@ -3841,7 +3866,12 @@ ""; foreach ($entries as $entry) { - $rv .= ""; + if ($entry["title"]) + $title = "— " . truncate_string($entry["title"], 30); + else + $title = ""; + + $rv .= ""; }; @@ -3990,52 +4020,6 @@ } - function rewrite_urls($html) { - libxml_use_internal_errors(true); - - $charset_hack = ' - - '; - - $doc = new DOMDocument(); - $doc->loadHTML($charset_hack . $html); - $xpath = new DOMXPath($doc); - - $entries = $xpath->query('//*/text()'); - - foreach ($entries as $entry) { - if (strstr($entry->wholeText, "://") !== false) { - $text = preg_replace("/((?\\1", $entry->wholeText); - - if ($text != $entry->wholeText) { - $cdoc = new DOMDocument(); - $cdoc->loadHTML($charset_hack . $text); - - - foreach ($cdoc->childNodes as $cnode) { - $cnode = $doc->importNode($cnode, true); - - if ($cnode) { - $entry->parentNode->insertBefore($cnode); - } - } - - $entry->parentNode->removeChild($entry); - - } - } - } - - $node = $doc->getElementsByTagName('body')->item(0); - - // http://tt-rss.org/forum/viewtopic.php?f=1&t=970 - if ($node) - return $doc->saveXML($node); - else - return $html; - } - function filter_to_sql($filter, $owner_uid) { $query = array(); @@ -4103,6 +4087,8 @@ $qpart .= " AND $cat_qpart"; } + $qpart .= " AND feed_id IS NOT NULL"; + array_push($query, "($qpart)"); }