ttrss/include/functions.php

598 lines
16 KiB
PHP
Raw Normal View History

2006-08-19 07:04:45 +00:00
<?php
define('SCHEMA_VERSION', 141);
define('LABEL_BASE_INDEX', -1024);
define('PLUGIN_FEED_BASE_INDEX', -128);
$fetch_last_error = false;
2013-03-30 11:10:53 +00:00
$fetch_last_error_code = false;
$fetch_last_content_type = false;
2014-08-12 15:36:45 +00:00
$fetch_last_error_content = false; // curl only for the time being
$fetch_effective_url = false;
$fetch_curl_used = false;
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
libxml_disable_entity_loader(true);
}
libxml_use_internal_errors(true);
// separate test because this is included before sanity checks
if (function_exists("mb_internal_encoding")) mb_internal_encoding("UTF-8");
date_default_timezone_set('UTC');
if (defined('E_DEPRECATED')) {
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
} else {
error_reporting(E_ALL & ~E_NOTICE);
}
2021-02-22 11:41:09 +00:00
ini_set('display_errors', "false");
ini_set('display_startup_errors', "false");
// config.php is optional
if (stream_resolve_include_path("config.php"))
require_once "config.php";
2021-02-22 18:47:48 +00:00
require_once "autoload.php";
if (Config::get(Config::DB_TYPE) == "pgsql") {
define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE');
} else {
define('SUBSTRING_FOR_DATE', 'SUBSTRING');
}
function get_pref(string $pref_name, int $owner_uid = null) {
return Prefs::get($pref_name, $owner_uid ? $owner_uid : $_SESSION["uid"], $_SESSION["profile"] ?? null);
}
function set_pref(string $pref_name, $value, int $owner_uid = null, bool $strip_tags = true) {
return Prefs::set($pref_name, $value, $owner_uid ? $owner_uid : $_SESSION["uid"], $_SESSION["profile"] ?? null, $strip_tags);
}
2007-08-11 14:25:51 +00:00
function get_translations() {
$t = array(
2019-01-30 14:57:28 +00:00
"auto" => __("Detect automatically"),
"ar_SA" => "العربيّة (Arabic)",
2015-03-11 12:36:45 +00:00
"bg_BG" => "Bulgarian",
"da_DA" => "Dansk",
2009-11-22 20:18:54 +00:00
"ca_CA" => "Català",
2013-03-19 13:39:08 +00:00
"cs_CZ" => "Česky",
2007-08-11 15:40:27 +00:00
"en_US" => "English",
"el_GR" => "Ελληνικά",
"es_ES" => "Español (España)",
"es_LA" => "Español",
2009-04-26 12:42:33 +00:00
"de_DE" => "Deutsch",
2020-10-01 07:19:04 +00:00
"fa" => "Persian (Farsi)",
2007-08-11 15:40:27 +00:00
"fr_FR" => "Français",
2008-02-26 07:57:09 +00:00
"hu_HU" => "Magyar (Hungarian)",
2008-11-02 16:42:39 +00:00
"it_IT" => "Italiano",
2008-09-25 03:56:59 +00:00
"ja_JP" => "日本語 (Japanese)",
2013-03-18 21:13:30 +00:00
"lv_LV" => "Latviešu",
"nb_NO" => "Norwegian bokmål",
2013-03-23 05:21:57 +00:00
"nl_NL" => "Dutch",
"pl_PL" => "Polski",
"ru_RU" => "Русский",
2007-10-26 07:19:54 +00:00
"pt_BR" => "Portuguese/Brazil",
2014-03-21 11:56:26 +00:00
"pt_PT" => "Portuguese/Portugal",
"zh_CN" => "Simplified Chinese",
2014-02-12 12:30:24 +00:00
"zh_TW" => "Traditional Chinese",
2019-01-30 14:14:07 +00:00
"uk_UA" => "Українська",
2013-04-01 17:35:00 +00:00
"sv_SE" => "Svenska",
"fi_FI" => "Suomi",
"tr_TR" => "Türkçe");
2007-08-11 14:25:51 +00:00
return $t;
2007-08-11 14:25:51 +00:00
}
require_once "lib/gettext/gettext.inc.php";
2007-03-06 10:33:06 +00:00
2011-03-18 16:25:06 +00:00
function startup_gettext() {
$selected_locale = "";
// https://www.codingwithjesse.com/blog/use-accept-language-header/
if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$valid_langs = [];
$translations = array_keys(get_translations());
array_shift($translations); // remove "auto"
2007-08-10 16:16:43 +00:00
// full locale first
foreach ($translations as $t) {
$lang = strtolower(str_replace("_", "-", (string)$t));
$valid_langs[$lang] = $t;
$lang = substr($lang, 0, 2);
if (!isset($valid_langs[$lang]))
$valid_langs[$lang] = $t;
}
// break up string into pieces (languages and q factors)
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
$_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
if (count($lang_parse[1])) {
// create a list like "en" => 0.8
$langs = array_combine($lang_parse[1], $lang_parse[4]);
if (is_array($langs)) {
// set default to 1 for any without q factor
foreach ($langs as $lang => $val) {
if ($val === '') $langs[$lang] = 1;
}
// sort list based on value
arsort($langs, SORT_NUMERIC);
foreach (array_keys($langs) as $lang) {
$lang = strtolower($lang);
foreach ($valid_langs as $vlang => $vlocale) {
if ($vlang == $lang) {
$selected_locale = $vlocale;
break 2;
}
}
}
}
}
2011-03-18 16:25:06 +00:00
}
2007-08-10 16:16:43 +00:00
if (!empty($_SESSION["uid"]) && get_schema_version() >= 120) {
2021-02-25 11:49:58 +00:00
$pref_locale = get_pref(Prefs::USER_LANGUAGE, $_SESSION["uid"]);
if (!empty($pref_locale) && $pref_locale != 'auto') {
$selected_locale = $pref_locale;
}
2011-03-18 16:25:06 +00:00
}
if ($selected_locale) {
2011-03-18 16:25:06 +00:00
if (defined('LC_MESSAGES')) {
_setlocale(LC_MESSAGES, $selected_locale);
2011-03-18 16:25:06 +00:00
} else if (defined('LC_ALL')) {
_setlocale(LC_ALL, $selected_locale);
}
2007-03-06 10:33:06 +00:00
2013-03-21 19:47:44 +00:00
_bindtextdomain("messages", "locale");
2011-03-18 16:25:06 +00:00
_textdomain("messages");
_bind_textdomain_codeset("messages", "UTF-8");
}
2011-03-18 16:25:06 +00:00
}
require_once 'controls.php';
require_once 'controls_compat.php';
2005-08-21 15:35:22 +00:00
define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . get_version() . ' (http://tt-rss.org/)');
2010-11-17 09:52:17 +00:00
ini_set('user_agent', SELF_USER_AGENT);
2011-08-04 15:38:25 +00:00
$schema_version = false;
/* compat shims */
function _debug($msg) {
Debug::log($msg);
}
// @deprecated
function getFeedUnread($feed, $is_cat = false) {
2021-02-15 12:43:07 +00:00
return Feeds::_get_counters($feed, $is_cat, true, $_SESSION["uid"]);
}
// @deprecated
function sanitize($str, $force_remove_images = false, $owner = false, $site_url = false, $highlight_words = false, $article_id = false) {
return Sanitizer::sanitize($str, $force_remove_images, $owner, $site_url, $highlight_words, $article_id);
}
// @deprecated
function fetch_file_contents($params) {
return UrlHelper::fetch($params);
}
2005-08-25 06:46:24 +00:00
// @deprecated
function rewrite_relative_url($url, $rel_url) {
return UrlHelper::rewrite_relative($url, $rel_url);
}
2005-11-18 05:17:17 +00:00
// @deprecated
function validate_url($url) {
return UrlHelper::validate($url);
}
2005-11-18 05:17:17 +00:00
// @deprecated
function authenticate_user($login, $password, $check_only = false, $service = false) {
return UserHelper::authenticate($login, $password, $check_only, $service);
2005-11-18 05:17:17 +00:00
}
2006-05-16 07:33:51 +00:00
// @deprecated
function smart_date_time($timestamp, $tz_offset = 0, $owner_uid = false, $eta_min = false) {
return TimeHelper::smart_date_time($timestamp, $tz_offset, $owner_uid, $eta_min);
}
// @deprecated
function make_local_datetime($timestamp, $long, $owner_uid = false, $no_smart_dt = false, $eta_min = false) {
return TimeHelper::make_local_datetime($timestamp, $long, $owner_uid, $no_smart_dt, $eta_min);
}
/* end compat shims */
function get_ssl_certificate_id() {
if ($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"] ?? false) {
return sha1($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"] .
$_SERVER["REDIRECT_SSL_CLIENT_V_START"] .
$_SERVER["REDIRECT_SSL_CLIENT_V_END"] .
$_SERVER["REDIRECT_SSL_CLIENT_S_DN"]);
}
if ($_SERVER["SSL_CLIENT_M_SERIAL"] ?? false) {
return sha1($_SERVER["SSL_CLIENT_M_SERIAL"] .
$_SERVER["SSL_CLIENT_V_START"] .
$_SERVER["SSL_CLIENT_V_END"] .
$_SERVER["SSL_CLIENT_S_DN"]);
}
return "";
}
// this is used for user http parameters unless HTML code is actually needed
function clean($param) {
if (is_array($param)) {
return array_map("trim", array_map("strip_tags", $param));
} else if (is_string($param)) {
return trim(strip_tags($param));
} else {
2021-02-22 11:41:09 +00:00
return $param;
}
}
function make_password($length = 12) {
2012-01-25 06:47:32 +00:00
$password = "";
$possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ*%+^";
2012-01-25 06:47:32 +00:00
$i = 0;
2012-01-25 06:47:32 +00:00
while ($i < $length) {
try {
$idx = function_exists("random_int") ? random_int(0, strlen($possible) - 1) : mt_rand(0, strlen($possible) - 1);
} catch (Exception $e) {
$idx = mt_rand(0, strlen($possible) - 1);
}
$char = substr($possible, $idx, 1);
2012-01-25 06:47:32 +00:00
if (!strstr($password, $char)) {
$password .= $char;
$i++;
}
}
2012-01-25 06:47:32 +00:00
return $password;
2005-11-18 09:00:18 +00:00
}
2011-12-26 08:02:52 +00:00
function validate_csrf($csrf_token) {
2021-02-12 18:21:23 +00:00
return isset($csrf_token) && hash_equals($_SESSION['csrf_token'] ?? "", $csrf_token);
2011-12-26 08:02:52 +00:00
}
2010-11-25 09:05:48 +00:00
function truncate_string($str, $max_len, $suffix = '&hellip;') {
if (mb_strlen($str, "utf-8") > $max_len) {
2010-11-25 09:05:48 +00:00
return mb_substr($str, 0, $max_len, "utf-8") . $suffix;
2005-11-24 07:25:09 +00:00
} else {
return $str;
}
}
2005-11-26 06:40:47 +00:00
2018-12-24 09:44:10 +00:00
function mb_substr_replace($original, $replacement, $position, $length) {
$startString = mb_substr($original, 0, $position, "UTF-8");
$endString = mb_substr($original, $position + $length, mb_strlen($original), "UTF-8");
$out = $startString . $replacement . $endString;
return $out;
}
function truncate_middle($str, $max_len, $suffix = '&hellip;') {
2018-12-24 09:44:10 +00:00
if (mb_strlen($str) > $max_len) {
return mb_substr_replace($str, $suffix, $max_len / 2, mb_strlen($str) - $max_len);
} else {
return $str;
}
}
function sql_bool_to_bool($s) {
2017-12-02 19:39:34 +00:00
return $s && ($s !== "f" && $s !== "false"); //no-op for PDO, backwards compat for legacy layer
}
function bool_to_sql_bool($s) {
return $s ? 1 : 0;
}
// Session caching removed due to causing wrong redirects to upgrade
// script when get_schema_version() is called on an obsolete session
// created on a previous schema version.
function get_schema_version($nocache = false) {
2011-08-04 15:38:25 +00:00
global $schema_version;
$pdo = Db::pdo();
2017-11-30 09:28:02 +00:00
if (!$schema_version && !$nocache) {
2017-11-30 09:28:02 +00:00
$row = $pdo->query("SELECT schema_version FROM ttrss_version")->fetch();
$version = $row["schema_version"];
2011-08-04 15:38:25 +00:00
$schema_version = $version;
return $version;
2011-08-04 15:38:25 +00:00
} else {
return $schema_version;
}
}
function file_is_locked($filename) {
2021-02-22 18:47:48 +00:00
if (file_exists(Config::get(Config::LOCK_DIRECTORY) . "/$filename")) {
2013-05-29 03:46:14 +00:00
if (function_exists('flock')) {
2021-02-22 18:47:48 +00:00
$fp = @fopen(Config::get(Config::LOCK_DIRECTORY) . "/$filename", "r");
2013-05-29 03:46:14 +00:00
if ($fp) {
if (flock($fp, LOCK_EX | LOCK_NB)) {
flock($fp, LOCK_UN);
fclose($fp);
return false;
}
2007-09-25 03:23:29 +00:00
fclose($fp);
2013-05-29 03:46:14 +00:00
return true;
} else {
2007-09-25 03:23:29 +00:00
return false;
}
}
2013-05-29 03:46:14 +00:00
return true; // consider the file always locked and skip the test
} else {
return false;
}
}
2006-02-12 07:21:52 +00:00
function make_lockfile($filename) {
2021-02-22 18:47:48 +00:00
$fp = fopen(Config::get(Config::LOCK_DIRECTORY) . "/$filename", "w");
2006-02-12 07:21:52 +00:00
if ($fp && flock($fp, LOCK_EX | LOCK_NB)) {
$stat_h = fstat($fp);
2021-02-22 18:47:48 +00:00
$stat_f = stat(Config::get(Config::LOCK_DIRECTORY) . "/$filename");
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
if ($stat_h["ino"] != $stat_f["ino"] ||
$stat_h["dev"] != $stat_f["dev"]) {
return false;
}
}
if (function_exists('posix_getpid')) {
fwrite($fp, posix_getpid() . "\n");
}
2006-02-12 07:21:52 +00:00
return $fp;
} else {
return false;
}
}
function checkbox_to_sql_bool($val) {
return ($val == "on") ? 1 : 0;
}
function uniqid_short() {
2021-02-22 11:41:09 +00:00
return uniqid(base_convert((string)rand(), 10, 36));
}
function T_sprintf() {
$args = func_get_args();
return vsprintf(__(array_shift($args)), $args);
}
function T_nsprintf() {
$args = func_get_args();
return vsprintf(_ngettext(array_shift($args), array_shift($args), array_shift($args)), $args);
}
function is_server_https() {
return (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off')) ||
(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
}
function is_prefix_https() {
2021-02-22 18:47:48 +00:00
return parse_url(Config::get(Config::SELF_URL_PATH), PHP_URL_SCHEME) == 'https';
}
2021-02-22 18:47:48 +00:00
// this returns Config::get(Config::SELF_URL_PATH) sans ending slash
function get_self_url_prefix() {
2021-02-22 18:47:48 +00:00
if (strrpos(Config::get(Config::SELF_URL_PATH), "/") === strlen(Config::get(Config::SELF_URL_PATH))-1) {
return substr(Config::get(Config::SELF_URL_PATH), 0, strlen(Config::get(Config::SELF_URL_PATH))-1);
} else {
2021-02-22 18:47:48 +00:00
return Config::get(Config::SELF_URL_PATH);
}
}
function encrypt_password($pass, $salt = '', $mode2 = false) {
if ($salt && $mode2) {
return "MODE2:" . hash('sha256', $salt . $pass);
} else if ($salt) {
return "SHA1X:" . sha1("$salt:$pass");
} else {
return "SHA1:" . sha1($pass);
}
} // function encrypt_password
function init_plugins() {
2021-02-22 18:47:48 +00:00
PluginHost::getInstance()->load(Config::get(Config::PLUGINS), PluginHost::KIND_ALL);
return true;
}
if (!function_exists('gzdecode')) {
function gzdecode($string) { // no support for 2nd argument
return file_get_contents('compress.zlib://data:who/cares;base64,'.
base64_encode($string));
}
}
function get_random_bytes($length) {
if (function_exists('random_bytes')) {
return random_bytes($length);
} else if (function_exists('openssl_random_pseudo_bytes')) {
return openssl_random_pseudo_bytes($length);
} else {
$output = "";
for ($i = 0; $i < $length; $i++)
$output .= chr(mt_rand(0, 255));
return $output;
}
}
function read_stdin() {
$fp = fopen("php://stdin", "r");
if ($fp) {
$line = trim(fgets($fp));
fclose($fp);
return $line;
}
return null;
}
function implements_interface($class, $interface) {
return in_array($interface, class_implements($class));
}
function T_js_decl($s1, $s2) {
if ($s1 && $s2) {
$s1 = preg_replace("/\n/", "", $s1);
$s2 = preg_replace("/\n/", "", $s2);
$s1 = preg_replace("/\"/", "\\\"", $s1);
$s2 = preg_replace("/\"/", "\\\"", $s2);
return "T_messages[\"$s1\"] = \"$s2\";\n";
}
}
function init_js_translations() {
print 'var T_messages = new Object();
function __(msg) {
if (T_messages[msg]) {
return T_messages[msg];
} else {
return msg;
}
}
function ngettext(msg1, msg2, n) {
return __((parseInt(n) > 1) ? msg2 : msg1);
}';
2019-03-01 11:25:24 +00:00
global $text_domains;
2019-03-01 11:25:24 +00:00
foreach (array_keys($text_domains) as $domain) {
$l10n = _get_reader($domain);
for ($i = 0; $i < $l10n->total; $i++) {
$orig = $l10n->get_original_string($i);
if(strpos($orig, "\000") !== false) { // Plural forms
2019-03-01 11:25:24 +00:00
$key = explode(chr(0), $orig);
print T_js_decl($key[0], _ngettext($key[0], $key[1], 1)); // Singular
print T_js_decl($key[1], _ngettext($key[0], $key[1], 2)); // Plural
} else {
$translation = _dgettext($domain,$orig);
print T_js_decl($orig, $translation);
}
}
2019-03-01 11:25:24 +00:00
}
}
function get_theme_path($theme) {
$check = "themes/$theme";
if (file_exists($check)) return $check;
$check = "themes.local/$theme";
if (file_exists($check)) return $check;
}
function theme_exists($theme) {
return file_exists("themes/$theme") || file_exists("themes.local/$theme");
}
2018-01-30 07:44:31 +00:00
function arr_qmarks($arr) {
return str_repeat('?,', count($arr) - 1) . '?';
}
function get_scripts_timestamp() {
$files = glob("js/*.js");
$ts = 0;
foreach ($files as $file) {
$file_ts = filemtime($file);
if ($file_ts > $ts) $ts = $file_ts;
}
return $ts;
}
/* for package maintainers who don't use git: if version_static.txt exists in tt-rss root
directory, its contents are displayed instead of git commit-based version, this could be generated
based on source git tree commit used when creating the package */
function get_version(&$git_commit = false, &$git_timestamp = false, &$last_error = false) {
global $ttrss_version;
if (is_array($ttrss_version) && isset($ttrss_version['version'])) {
$git_commit = $ttrss_version['commit'];
$git_timestamp = $ttrss_version['timestamp'];
$last_error = $ttrss_version['last_error'] ?? "";
return $ttrss_version['version'];
} else {
$ttrss_version = [];
}
$ttrss_version['version'] = "UNKNOWN (Unsupported)";
date_default_timezone_set('UTC');
2021-02-22 14:38:46 +00:00
$root_dir = dirname(__DIR__);
if (PHP_OS === "Darwin") {
$ttrss_version['version'] = "UNKNOWN (Unsupported, Darwin)";
} else if (file_exists("$root_dir/version_static.txt")) {
$ttrss_version['version'] = trim(file_get_contents("$root_dir/version_static.txt")) . " (Unsupported)";
} else if (is_dir("$root_dir/.git")) {
$rc = 0;
$output = [];
$cwd = getcwd();
chdir($root_dir);
exec('git --no-pager log --pretty="version: %ct %h" -n1 HEAD 2>&1', $output, $rc);
chdir($cwd);
if (is_array($output) && count($output) > 0) {
list ($test, $timestamp, $commit) = explode(" ", $output[0], 3);
if ($test == "version:") {
$git_commit = $commit;
$git_timestamp = $timestamp;
2021-02-25 11:51:13 +00:00
$ttrss_version['version'] = strftime("%y.%m", (int)$timestamp) . "-$commit";
$ttrss_version['commit'] = $commit;
$ttrss_version['timestamp'] = $timestamp;
}
}
if (!isset($ttrss_version['commit'])) {
$last_error = "Unable to determine version (using $root_dir): RC=$rc; OUTPUT=" . implode("\n", $output);
$ttrss_version["last_error"] = $last_error;
user_error($last_error, E_USER_WARNING);
}
}
return $ttrss_version['version'];
}