]> git.wh0rd.org - tt-rss.git/blobdiff - include/functions.php
unify houskeeping stuff, increase spawn interval
[tt-rss.git] / include / functions.php
index 004f4db2f1bb77c0ee0ec5f3c9efd5e9feddff96..a41d3a950c6ba393fd827b0389ba778e08271169 100644 (file)
        $utc_tz = new DateTimeZone('UTC');
        $schema_version = false;
 
-       global $_debug_enabled;
-       $_debug_enabled = true;
-
        /**
         * Print a timestamped debug message.
         *
         * @param string $msg The debug message.
         * @return void
         */
-       function _debug($msg) {
-               global $_debug_enabled;
-
-               if (!$_debug_enabled) return;
+       function _debug($msg, $show = true) {
 
                $ts = strftime("%H:%M:%S", time());
                if (function_exists('posix_getpid')) {
                        $ts = "$ts/" . posix_getpid();
                }
 
-               if (!(defined('QUIET') && QUIET)) {
+               if ($show && !(defined('QUIET') && QUIET)) {
                        print "[$ts] $msg\n";
                }
 
 
        } // function _debug
 
-       function _debug_enable($enabled) {
-               global $_debug_enabled;
-
-               $old = $_debug_enabled;
-               $_debug_enabled = $enabled;
-
-               return $old;
-       }
-
        /**
         * Purge a feed old posts.
         *
                                $ch = curl_init($url);
                        }
 
-                       if ($timestamp) {
+                       if ($timestamp && !$post_query) {
                                curl_setopt($ch, CURLOPT_HTTPHEADER,
                                        array("If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T', $timestamp)));
                        }
                                }
                        }
 
-                       $data = @file_get_contents($url);
+                       if (!$post_query && $timestamp) {
+                               $context = stream_context_create(array(
+                                       'http' => array(
+                                               'method' => 'GET',
+                                               'header' => "If-Modified-Since: ".gmdate("D, d M Y H:i:s \\G\\M\\T\r\n", $timestamp)
+                                       )));
+                       } else {
+                               $context = NULL;
+                       }
+
+                       $old_error = error_get_last();
+
+                       $data = @file_get_contents($url, false, $context);
 
                        $fetch_last_content_type = false;  // reset if no type was sent from server
-                       if (is_array($http_response_header)) {
+                       if (isset($http_response_header) && is_array($http_response_header)) {
                                foreach ($http_response_header as $h) {
                                        if (substr(strtolower($h), 0, 13) == 'content-type:') {
                                                $fetch_last_content_type = substr($h, 14);
                                                // don't abort here b/c there might be more than one
                                                // e.g. if we were being redirected -- last one is the right one
                                        }
+
+                                       if (substr(strtolower($h), 0, 7) == 'http/1.') {
+                                               $fetch_last_error_code = (int) substr($h, 9, 3);
+                                       }
                                }
                        }
 
-                       if (!$data && function_exists('error_get_last')) {
+                       if (!$data) {
                                $error = error_get_last();
-                               $fetch_last_error = $error["message"];
+
+                               if ($error['message'] != $old_error['message']) {
+                                       $fetch_last_error = $error["message"];
+                               } else {
+                                       $fetch_last_error = "HTTP Code: $fetch_last_error_code";
+                               }
                        }
                        return $data;
                }
        function get_schema_version($nocache = false) {
                global $schema_version;
 
-               if (!$schema_version) {
+               if (!$schema_version && !$nocache) {
                        $result = db_query("SELECT schema_version FROM ttrss_version");
                        $version = db_fetch_result($result, 0, "schema_version");
                        $schema_version = $version;
                        $url = key($feedUrls);
                }
 
-               libxml_use_internal_errors(true);
+               /* libxml_use_internal_errors(true);
                $doc = new DOMDocument();
-               $doc->loadXML(html_entity_decode($contents));
+               $doc->loadXML($contents);
                $error = libxml_get_last_error();
                libxml_clear_errors();
 
                        $error_message = format_libxml_error($error);
 
                        return array("code" => 6, "message" => $error_message);
-               }
+               } */
 
                if ($cat_id == "0" || !$cat_id) {
                        $cat_qpart = "NULL";