]> git.wh0rd.org - tt-rss.git/blobdiff - include/functions.php
only get USER_LANGUAGE on schema 120 and up
[tt-rss.git] / include / functions.php
index 4cc8f134da6962b90557ba9507cad915d6701ca5..c39b4938c4d1d1cc42e7b3ca144001bf77e54f80 100644 (file)
@@ -1,10 +1,12 @@
 <?php
        define('EXPECTED_CONFIG_VERSION', 26);
-       define('SCHEMA_VERSION', 118);
+       define('SCHEMA_VERSION', 120);
 
        define('LABEL_BASE_INDEX', -1024);
        define('PLUGIN_FEED_BASE_INDEX', -128);
 
+       define('COOKIE_LIFETIME_LONG', 86400*365);
+
        $fetch_last_error = false;
        $fetch_last_error_code = false;
        $fetch_last_content_type = false;
                        $lang = _TRANSLATION_OVERRIDE_DEFAULT;
                }
 
-               if ($_SESSION["language"] && $_SESSION["language"] != "auto") {
-                       $lang = $_SESSION["language"];
+               if ($_SESSION["uid"] && get_schema_version() >= 120) {
+                       $pref_lang = get_pref("USER_LANGUAGE", $_SESSION["uid"]);
+
+                       if ($pref_lang) {
+                               $lang = $pref_lang;
+                       }
                }
 
                if ($lang) {
                }
        }
 
-       startup_gettext();
-
        require_once 'db-prefs.php';
        require_once 'version.php';
        require_once 'ccache.php';
 
        require_once 'lib/pubsubhubbub/publisher.php';
 
-       $tz_offset = -1;
-       $utc_tz = new DateTimeZone('UTC');
        $schema_version = false;
 
        /**
         * @param string $msg The debug message.
         * @return void
         */
-       function _debug($msg) {
+       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";
                }
 
                                $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;
                }
                                @session_start();
 
                                $_SESSION["uid"] = $user_id;
-                               $_SESSION["version"] = VERSION;
+                               $_SESSION["version"] = VERSION_STATIC;
 
                                $result = db_query("SELECT login,access_level,pwd_hash FROM ttrss_users
                                        WHERE id = '$user_id'");
                                $_SESSION["last_login_update"] = time();
                        }
 
-                       if ($_SESSION["uid"] && $_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) {
-                               setcookie("ttrss_lang", $_SESSION["language"],
-                                       time() + SESSION_COOKIE_LIFETIME);
-                       }
-
                        if ($_SESSION["uid"]) {
+                               startup_gettext();
                                load_user_plugins($_SESSION["uid"]);
 
                                /* cleanup ccache */
                if (!$timestamp) $timestamp = '1970-01-01 0:00';
 
                global $utc_tz;
-               global $tz_offset;
+               global $user_tz;
+
+               if (!$utc_tz) $utc_tz = new DateTimeZone('UTC');
+
+               $timestamp = substr($timestamp, 0, 19);
 
                # We store date in UTC internally
                $dt = new DateTime($timestamp, $utc_tz);
 
-               if ($tz_offset == -1) {
+               $user_tz_string = get_pref('USER_TIMEZONE', $owner_uid);
 
-                       $user_tz_string = get_pref('USER_TIMEZONE', $owner_uid);
+               if ($user_tz_string != 'Automatic') {
 
                        try {
-                               $user_tz = new DateTimeZone($user_tz_string);
+                               if (!$user_tz) $user_tz = new DateTimeZone($user_tz_string);
                        } catch (Exception $e) {
                                $user_tz = $utc_tz;
                        }
 
                        $tz_offset = $user_tz->getOffset($dt);
+               } else {
+                       $tz_offset = (int) -$_SESSION["clientTzOffset"];
                }
 
                $user_timestamp = $dt->format('U') + $tz_offset;
        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;
         *                     Here you should call extractfeedurls in rpc-backend
         *                     to get all possible feeds.
         *                 5 - Couldn't download the URL content.
+        *                 6 - Content is an invalid XML.
         */
        function subscribe_to_feed($url, $cat_id = 0,
                        $auth_login = '', $auth_pass = '') {
                        $url = key($feedUrls);
                }
 
+               /* libxml_use_internal_errors(true);
+               $doc = new DOMDocument();
+               $doc->loadXML($contents);
+               $error = libxml_get_last_error();
+               libxml_clear_errors();
+
+               if ($error) {
+                       $error_message = format_libxml_error($error);
+
+                       return array("code" => 6, "message" => $error_message);
+               } */
+
                if ($cat_id == "0" || !$cat_id) {
                        $cat_qpart = "NULL";
                } else {
                                        $feed_title = getCategoryTitle($feed);
                                } else {
                                        if (is_numeric($feed) && $feed > 0) {
-                                               $result = db_query("SELECT title,site_url,last_error
+                                               $result = db_query("SELECT title,site_url,last_error,last_updated
                                                        FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = $owner_uid");
 
                                                $feed_title = db_fetch_result($result, 0, "title");
                                                $feed_site_url = db_fetch_result($result, 0, "site_url");
                                                $last_error = db_fetch_result($result, 0, "last_error");
+                                               $last_updated = db_fetch_result($result, 0, "last_updated");
                                        } else {
                                                $feed_title = getFeedTitle($feed);
                                        }
                                $result = db_query($select_qpart . $from_qpart . $where_qpart);
                        }
 
-                       return array($result, $feed_title, $feed_site_url, $last_error);
+                       return array($result, $feed_title, $feed_site_url, $last_error, $last_updated);
 
        }
 
        }
 
        function strip_harmful_tags($doc, $allowed_elements, $disallowed_attributes) {
-               $entries = $doc->getElementsByTagName("*");
+               $xpath = new DOMXPath($doc);
+               $entries = $xpath->query('//*');
 
                foreach ($entries as $entry) {
                        if (!in_array($entry->nodeName, $allowed_elements)) {
 
        function format_article_labels($labels, $id) {
 
-               if (is_array($labels)) return '';
+               if (!is_array($labels)) return '';
 
                $labels_str = "";
 
                return LABEL_BASE_INDEX - 1 + abs($feed);
        }
 
+       function format_libxml_error($error) {
+               return T_sprintf("LibXML error %s at line %d (column %d): %s",
+                               $error->code, $error->line, $error->column,
+                               $error->message);
+       }
+
 ?>