summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2021-03-05 21:14:35 +0300
committerAndrew Dolgov <[email protected]>2021-03-05 21:14:35 +0300
commit2b8b845abe7c13ecbb266613910484310cffe8e1 (patch)
tree90bd2e93737c2aad17cfb09496cc57cf3f9968cd /classes
parentb2341679d53b227fc90fba34c3a7e6453e3cad6e (diff)
* use ORM for trivial queries
* environment-based configuration * useradm.php -> update.php with new options * support for schema migrations * various fixes
Diffstat (limited to 'classes')
-rw-r--r--classes/config.php404
-rw-r--r--classes/db.php33
-rw-r--r--classes/db/migrations.php189
-rw-r--r--classes/debug.php91
4 files changed, 717 insertions, 0 deletions
diff --git a/classes/config.php b/classes/config.php
new file mode 100644
index 0000000..95b0ccc
--- /dev/null
+++ b/classes/config.php
@@ -0,0 +1,404 @@
+<?php
+class Config {
+ private const _ENVVAR_PREFIX = "EPUBE_";
+
+ const T_BOOL = 1;
+ const T_STRING = 2;
+ const T_INT = 3;
+
+ // override defaults, defined below in _DEFAULTS[], via environment: DB_TYPE becomes EPUBE_DB_TYPE, etc
+
+ const DB_TYPE = "DB_TYPE";
+ const SCRATCH_DB = "SCRATCH_DB";
+ const CALIBRE_DB = "CALIBRE_DB";
+ const BOOKS_DIR = "BOOKS_DIR";
+ const DICT_SERVER = "DICT_SERVER";
+ const SESSION_LIFETIME = "SESSION_LIFETIME";
+
+ private const _DEFAULTS = [
+ Config::DB_TYPE => [ "sqlite", Config::T_STRING ],
+ Config::SCRATCH_DB => [ "db/scratch.db", Config::T_STRING ],
+ Config::CALIBRE_DB => [ "", Config::T_STRING ],
+ Config::BOOKS_DIR => [ "", Config::T_STRING ],
+ Config::DICT_SERVER => [ "", Config::T_STRING ],
+ Config::SESSION_LIFETIME => [ 86400*30, Config::T_INT ],
+ ];
+
+ private static $instance;
+
+ private $params = [];
+ private $schema_version = null;
+ private $version = [];
+
+ /** @var Db_Migrations $migrations */
+ private $migrations;
+
+ public static function get_instance() : Config {
+ if (self::$instance == null)
+ self::$instance = new self();
+
+ return self::$instance;
+ }
+
+ private function __clone() {
+ //
+ }
+
+ function __construct() {
+ $ref = new ReflectionClass(get_class($this));
+
+ foreach ($ref->getConstants() as $const => $cvalue) {
+ if (isset($this::_DEFAULTS[$const])) {
+ $override = getenv($this::_ENVVAR_PREFIX . $const);
+
+ list ($defval, $deftype) = $this::_DEFAULTS[$const];
+
+ $this->params[$cvalue] = [ self::cast_to(!empty($override) ? $override : $defval, $deftype), $deftype ];
+ }
+ }
+ }
+
+ /* 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 */
+
+ static function get_version(bool $as_string = true) {
+ return self::get_instance()->_get_version($as_string);
+ }
+
+ private function _get_version(bool $as_string = true) {
+ $root_dir = dirname(__DIR__);
+
+ if (empty($this->version)) {
+ $this->version["status"] = -1;
+
+ if (PHP_OS === "Darwin") {
+ $ttrss_version["version"] = "UNKNOWN (Unsupported, Darwin)";
+ } else if (file_exists("$root_dir/version_static.txt")) {
+ $this->version["version"] = trim(file_get_contents("$root_dir/version_static.txt")) . " (Unsupported)";
+ } else if (is_dir("$root_dir/.git")) {
+ $this->version = self::get_version_from_git($root_dir);
+
+ if ($this->version["status"] != 0) {
+ user_error("Unable to determine version: " . $this->version["version"], E_USER_WARNING);
+
+ $this->version["version"] = "UNKNOWN (Unsupported, Git error)";
+ }
+ } else {
+ $this->version["version"] = "UNKNOWN (Unsupported)";
+ }
+ }
+
+ return $as_string ? $this->version["version"] : $this->version;
+ }
+
+ static function get_version_from_git(string $dir) {
+ $descriptorspec = [
+ 1 => ["pipe", "w"], // STDOUT
+ 2 => ["pipe", "w"], // STDERR
+ ];
+
+ $rv = [
+ "status" => -1,
+ "version" => "",
+ "commit" => "",
+ "timestamp" => 0,
+ ];
+
+ $proc = proc_open("git --no-pager log --pretty=\"version-%ct-%h\" -n1 HEAD",
+ $descriptorspec, $pipes, $dir);
+
+ if (is_resource($proc)) {
+ $stdout = trim(stream_get_contents($pipes[1]));
+ $stderr = trim(stream_get_contents($pipes[2]));
+ $status = proc_close($proc);
+
+ $rv["status"] = $status;
+
+ list($check, $timestamp, $commit) = explode("-", $stdout);
+
+ if ($check == "version") {
+
+ $rv["version"] = strftime("%y.%m", (int)$timestamp) . "-$commit";
+ $rv["commit"] = $commit;
+ $rv["timestamp"] = $timestamp;
+
+ // proc_close() may return -1 even if command completed successfully
+ // so if it looks like we got valid data, we ignore it
+
+ if ($rv["status"] == -1)
+ $rv["status"] = 0;
+
+ } else {
+ $rv["version"] = T_sprintf("Git error [RC=%d]: %s", $status, $stderr);
+ }
+ }
+
+ return $rv;
+ }
+
+ static function get_migrations() : Db_Migrations {
+ return self::get_instance()->_get_migrations();
+ }
+
+ private function _get_migrations() : Db_Migrations {
+ if (empty($this->migrations)) {
+ $this->migrations = new Db_Migrations();
+ $this->migrations->initialize(dirname(__DIR__) . "/sql", "epube_migrations", true);
+ }
+
+ return $this->migrations;
+ }
+
+ static function is_migration_needed() : bool {
+ return self::get_migrations()->is_migration_needed();
+ }
+
+ static function get_schema_version() : int {
+ return self::get_migrations()->get_version();
+ }
+
+ static function cast_to(string $value, int $type_hint) {
+ switch ($type_hint) {
+ case self::T_BOOL:
+ return sql_bool_to_bool($value);
+ case self::T_INT:
+ return (int) $value;
+ default:
+ return $value;
+ }
+ }
+
+ private function _get(string $param) {
+ list ($value, $type_hint) = $this->params[$param];
+
+ return $this->cast_to($value, $type_hint);
+ }
+
+ private function _add(string $param, string $default, int $type_hint) {
+ $override = getenv($this::_ENVVAR_PREFIX . $param);
+
+ $this->params[$param] = [ self::cast_to(!empty($override) ? $override : $default, $type_hint), $type_hint ];
+ }
+
+ static function add(string $param, string $default, int $type_hint = Config::T_STRING) {
+ $instance = self::get_instance();
+
+ return $instance->_add($param, $default, $type_hint);
+ }
+
+ static function get(string $param) {
+ $instance = self::get_instance();
+
+ return $instance->_get($param);
+ }
+
+ static function is_server_https() : bool {
+ return (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off')) ||
+ (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
+ }
+
+ /** generates reference self_url_path (no trailing slash) */
+ static function make_self_url() : string {
+ $proto = self::is_server_https() ? 'https' : 'http';
+ $self_url_path = $proto . '://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
+
+ $self_url_path = preg_replace("/\w+\.php(\?.*$)?$/", "", $self_url_path);
+
+ if (substr($self_url_path, -1) === "/") {
+ return substr($self_url_path, 0, -1);
+ } else {
+ return $self_url_path;
+ }
+ }
+
+ /** also initializes Db and ORM */
+ static function sanity_check() {
+
+ /*
+ we don't actually need the DB object right now but some checks below might use ORM which won't be initialized
+ because it is set up in the Db constructor, which is why it's a good idea to invoke it as early as possible
+
+ it is a bit of a hack, maybe ORM should be initialized somewhere else (functions.php?)
+ */
+
+ $pdo = Db::pdo();
+
+ $errors = [];
+
+ /*if (strpos(self::get(Config::PLUGINS), "auth_") === false) {
+ array_push($errors, "Please enable at least one authentication module via PLUGINS");
+ }*/
+
+ if (function_exists('posix_getuid') && posix_getuid() == 0) {
+ array_push($errors, "Please don't run this script as root.");
+ }
+
+ if (version_compare(PHP_VERSION, '7.1.0', '<')) {
+ array_push($errors, "PHP version 7.1.0 or newer required. You're using " . PHP_VERSION . ".");
+ }
+
+ // TODO: add some relevant stuff
+
+ /*if (!class_exists("UConverter")) {
+ array_push($errors, "PHP UConverter class is missing, it's provided by the Internationalization (intl) module.");
+ }
+
+ if (!is_writable(self::get(Config::CACHE_DIR) . "/images")) {
+ array_push($errors, "Image cache is not writable (chmod -R 777 ".self::get(Config::CACHE_DIR)."/images)");
+ }
+
+ if (!is_writable(self::get(Config::CACHE_DIR) . "/upload")) {
+ array_push($errors, "Upload cache is not writable (chmod -R 777 ".self::get(Config::CACHE_DIR)."/upload)");
+ }
+
+ if (!is_writable(self::get(Config::CACHE_DIR) . "/export")) {
+ array_push($errors, "Data export cache is not writable (chmod -R 777 ".self::get(Config::CACHE_DIR)."/export)");
+ }
+
+ if (self::get(Config::SINGLE_USER_MODE) && class_exists("PDO")) {
+ if (UserHelper::get_login_by_id(1) != "admin") {
+ array_push($errors, "SINGLE_USER_MODE is enabled but default admin account (ID: 1) is not found.");
+ }
+ }*/
+
+ if (php_sapi_name() != "cli") {
+
+ if (self::get_schema_version() < 0) {
+ array_push($errors, "Base database schema is missing. Either load it manually or perform a migration (<code>update.php --update-schema</code>)");
+ }
+
+/* $ref_self_url_path = self::make_self_url();
+
+ if ($ref_self_url_path) {
+ $ref_self_url_path = preg_replace("/\w+\.php$/", "", $ref_self_url_path);
+ }
+
+ if (self::get_self_url() == "http://example.org/tt-rss") {
+ $hint = $ref_self_url_path ? "(possible value: <b>$ref_self_url_path</b>)" : "";
+ array_push($errors,
+ "Please set SELF_URL_PATH to the correct value for your server: $hint");
+ }
+
+ if (self::get_self_url() != $ref_self_url_path) {
+ array_push($errors,
+ "Please set SELF_URL_PATH to the correct value detected for your server: <b>$ref_self_url_path</b> (you're using: <b>" . self::get_self_url() . "</b>)");
+ } */
+ }
+
+ /* if (!is_writable(self::get(Config::ICONS_DIR))) {
+ array_push($errors, "ICONS_DIR defined in config.php is not writable (chmod -R 777 ".self::get(Config::ICONS_DIR).").\n");
+ }
+
+ if (!is_writable(self::get(Config::LOCK_DIRECTORY))) {
+ array_push($errors, "LOCK_DIRECTORY is not writable (chmod -R 777 ".self::get(Config::LOCK_DIRECTORY).").\n");
+ }
+
+ if (!function_exists("curl_init") && !ini_get("allow_url_fopen")) {
+ array_push($errors, "PHP configuration option allow_url_fopen is disabled, and CURL functions are not present. Either enable allow_url_fopen or install PHP extension for CURL.");
+ }
+
+ if (!function_exists("json_encode")) {
+ array_push($errors, "PHP support for JSON is required, but was not found.");
+ }
+
+ if (!class_exists("PDO")) {
+ array_push($errors, "PHP support for PDO is required but was not found.");
+ }
+
+ if (!function_exists("mb_strlen")) {
+ array_push($errors, "PHP support for mbstring functions is required but was not found.");
+ }
+
+ if (!function_exists("hash")) {
+ array_push($errors, "PHP support for hash() function is required but was not found.");
+ }
+
+ if (ini_get("safe_mode")) {
+ array_push($errors, "PHP safe mode setting is obsolete and not supported by tt-rss.");
+ }
+
+ if (!function_exists("mime_content_type")) {
+ array_push($errors, "PHP function mime_content_type() is missing, try enabling fileinfo module.");
+ }
+
+ if (!class_exists("DOMDocument")) {
+ array_push($errors, "PHP support for DOMDocument is required, but was not found.");
+ }
+
+ if (self::get(Config::DB_TYPE) == "mysql") {
+ $bad_tables = self::check_mysql_tables();
+
+ if (count($bad_tables) > 0) {
+ $bad_tables_fmt = [];
+
+ foreach ($bad_tables as $bt) {
+ array_push($bad_tables_fmt, sprintf("%s (%s)", $bt['table_name'], $bt['engine']));
+ }
+
+ $msg = "<p>The following tables use an unsupported MySQL engine: <b>" .
+ implode(", ", $bad_tables_fmt) . "</b>.</p>";
+
+ $msg .= "<p>The only supported engine on MySQL is InnoDB. MyISAM lacks functionality to run
+ tt-rss.
+ Please backup your data (via OPML) and re-import the schema before continuing.</p>
+ <p><b>WARNING: importing the schema would mean LOSS OF ALL YOUR DATA.</b></p>";
+
+
+ array_push($errors, $msg);
+ }
+ } */
+
+ if (count($errors) > 0 && php_sapi_name() != "cli") { ?>
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <link href="lib/bootstrap/v3/css/bootstrap.min.css" rel="stylesheet" media="screen">
+ <link href="lib/bootstrap/v3/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <script src="dist/app-libs.min.js"></script>
+ <title>Startup failed</title>
+ <link type="text/css" rel="stylesheet" media="screen" href="dist/app.min.css" />
+ <link rel="shortcut icon" type="image/png" href="img/favicon.png" />
+ <link rel="manifest" href="manifest.json">
+ <meta name="mobile-web-app-capable" content="yes">
+ <script type="text/javascript">
+ $(document).ready(function() {
+ /* global EpubeApp */
+
+ if (typeof EpubeApp != "undefined") {
+ EpubeApp.setPage("PAGE_LOGIN");
+ }
+ });
+ </script>
+ </head>
+ <body class="epube-sanity-failed">
+ <div class="container">
+ <h1>Startup failed</h1>
+
+ <p>Please fix errors indicated by the following messages:</p>
+
+ <?php foreach ($errors as $error) { echo self::format_error($error); } ?>
+
+ </div>
+ </body>
+ </html>
+
+ <?php
+ die;
+ } else if (count($errors) > 0) {
+ echo "Please fix errors indicated by the following messages:\n\n";
+
+ foreach ($errors as $error) {
+ echo " * " . strip_tags($error)."\n";
+ }
+
+ exit(1);
+ }
+ }
+
+ private static function format_error($msg) {
+ return "<div class=\"alert alert-danger\">$msg</div>";
+ }
+}
diff --git a/classes/db.php b/classes/db.php
new file mode 100644
index 0000000..cc835b7
--- /dev/null
+++ b/classes/db.php
@@ -0,0 +1,33 @@
+<?php
+class Db {
+ private static $instance;
+ private $pdo;
+
+ private function __construct() {
+ try {
+ $this->pdo = new PDO(self::get_dsn());
+ } catch (Exception $e) {
+ die("Unable to initialize database driver (SQLite): $e");
+ }
+ //$this->dbh->busyTimeout(30*1000);
+ $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ $this->pdo->query('PRAGMA journal_mode = wal;');
+
+ ORM::configure(self::get_dsn());
+ ORM::configure('return_result_sets', true);
+ }
+
+ public static function get_dsn() {
+ return Config::get(Config::DB_TYPE) . ':' . Config::get(Config::SCRATCH_DB);
+ }
+
+ public static function pdo() : PDO {
+ if (self::$instance == null)
+ self::$instance = new self();
+
+ return self::$instance->pdo;
+ }
+
+};
+
+?>
diff --git a/classes/db/migrations.php b/classes/db/migrations.php
new file mode 100644
index 0000000..495dfc6
--- /dev/null
+++ b/classes/db/migrations.php
@@ -0,0 +1,189 @@
+<?php
+class Db_Migrations {
+
+ private $base_filename = "schema.sql";
+ private $base_path;
+ private $migrations_path;
+ private $migrations_table;
+ private $base_is_latest;
+ private $pdo;
+
+ private $cached_version;
+ private $cached_max_version;
+ private $max_version_override;
+
+ function __construct() {
+ $this->pdo = Db::pdo();
+ }
+
+ function initialize(string $root_path, string $migrations_table, bool $base_is_latest = true, int $max_version_override = 0) {
+ $this->base_path = "$root_path/" . Config::get(Config::DB_TYPE);
+ $this->migrations_path = $this->base_path . "/migrations";
+ $this->migrations_table = $migrations_table;
+ $this->base_is_latest = $base_is_latest;
+ $this->max_version_override = $max_version_override;
+ }
+
+ private function set_version(int $version) {
+ Debug::log("Updating table {$this->migrations_table} with version ${version}...", Debug::LOG_EXTENDED);
+
+ $sth = $this->pdo->query("SELECT * FROM {$this->migrations_table}");
+
+ if ($res = $sth->fetch()) {
+ $sth = $this->pdo->prepare("UPDATE {$this->migrations_table} SET schema_version = ?");
+ } else {
+ $sth = $this->pdo->prepare("INSERT INTO {$this->migrations_table} (schema_version) VALUES (?)");
+ }
+
+ $sth->execute([$version]);
+
+ $this->cached_version = $version;
+ }
+
+ function get_version() : int {
+ if (isset($this->cached_version))
+ return $this->cached_version;
+
+ try {
+ $sth = $this->pdo->query("SELECT * FROM {$this->migrations_table}");
+
+ if ($res = $sth->fetch()) {
+ return (int) $res['schema_version'];
+ } else {
+ return -1;
+ }
+ } catch (PDOException $e) {
+ $this->create_migrations_table();
+
+ return -1;
+ }
+ }
+
+ private function create_migrations_table() {
+ $this->pdo->query("CREATE TABLE IF NOT EXISTS {$this->migrations_table} (schema_version integer not null)");
+ }
+
+ private function migrate_to(int $version) {
+ try {
+ if ($version <= $this->get_version()) {
+ Debug::log("Refusing to apply version $version: current version is higher", Debug::LOG_VERBOSE);
+ return false;
+ }
+
+ if ($version == 0)
+ Debug::log("Loading base database schema...", Debug::LOG_VERBOSE);
+ else
+ Debug::log("Starting migration to $version...", Debug::LOG_VERBOSE);
+
+ $lines = $this->get_lines($version);
+
+ if (count($lines) > 0) {
+ // mysql doesn't support transactions for DDL statements
+ if (Config::get(Config::DB_TYPE) != "mysql")
+ $this->pdo->beginTransaction();
+
+ foreach ($lines as $line) {
+ Debug::log($line, Debug::LOG_EXTENDED);
+ try {
+ $this->pdo->query($line);
+ } catch (PDOException $e) {
+ Debug::log("Failed on line: $line", Debug::LOG_VERBOSE);
+ throw $e;
+ }
+ }
+
+ if ($version == 0 && $this->base_is_latest)
+ $this->set_version($this->get_max_version());
+ else
+ $this->set_version($version);
+
+ if (Config::get(Config::DB_TYPE) != "mysql")
+ $this->pdo->commit();
+
+ Debug::log("Migration finished, current version: " . $this->get_version(), Debug::LOG_VERBOSE);
+ } else {
+ Debug::log("Migration failed: schema file is empty or missing.", Debug::LOG_VERBOSE);
+ }
+
+ } catch (PDOException $e) {
+ Debug::log("Migration failed: " . $e->getMessage(), Debug::LOG_VERBOSE);
+ try {
+ $this->pdo->rollback();
+ } catch (PDOException $ie) {
+ //
+ }
+ throw $e;
+ }
+ }
+
+ function get_max_version() : int {
+ if ($this->max_version_override > 0)
+ return $this->max_version_override;
+
+ if (isset($this->cached_max_version))
+ return $this->cached_max_version;
+
+ $migrations = glob("{$this->migrations_path}/*.sql");
+
+ if (count($migrations) > 0) {
+ natsort($migrations);
+
+ $this->cached_max_version = (int) basename(array_pop($migrations), ".sql");
+
+ } else {
+ $this->cached_max_version = 0;
+ }
+
+ return $this->cached_max_version;
+ }
+
+ function is_migration_needed() : bool {
+ return $this->get_version() != $this->get_max_version();
+ }
+
+ function migrate() : bool {
+
+ if ($this->get_version() == -1) {
+ try {
+ $this->migrate_to(0);
+ } catch (PDOException $e) {
+ user_error("Failed to load base schema for {$this->migrations_table}: " . $e->getMessage(), E_USER_WARNING);
+ return false;
+ }
+ }
+
+ for ($i = $this->get_version() + 1; $i <= $this->get_max_version(); $i++) {
+ try {
+ $this->migrate_to($i);
+ } catch (PDOException $e) {
+ user_error("Failed to apply migration ${i} for {$this->migrations_table}: " . $e->getMessage(), E_USER_WARNING);
+ return false;
+ //throw $e;
+ }
+ }
+
+ return !$this->is_migration_needed();
+ }
+
+ private function get_lines(int $version) : array {
+ if ($version > 0)
+ $filename = "{$this->migrations_path}/${version}.sql";
+ else
+ $filename = "{$this->base_path}/{$this->base_filename}";
+
+ if (file_exists($filename)) {
+ $lines = array_filter(preg_split("/[\r\n]/", file_get_contents($filename)),
+ function ($line) {
+ return strlen(trim($line)) > 0 && strpos($line, "--") !== 0;
+ });
+
+ return array_filter(explode(";", implode("", $lines)), function ($line) {
+ return strlen(trim($line)) > 0 && !in_array(strtolower($line), ["begin", "commit"]);
+ });
+
+ } else {
+ user_error("Requested schema file ${filename} not found.", E_USER_ERROR);
+ return [];
+ }
+ }
+}
diff --git a/classes/debug.php b/classes/debug.php
new file mode 100644
index 0000000..a0dcac3
--- /dev/null
+++ b/classes/debug.php
@@ -0,0 +1,91 @@
+<?php
+class Debug {
+ const LOG_DISABLED = -1;
+ const LOG_NORMAL = 0;
+ const LOG_VERBOSE = 1;
+ const LOG_EXTENDED = 2;
+
+ public static $LOG_DISABLED = -1;
+ public static $LOG_NORMAL = 0;
+ public static $LOG_VERBOSE = 1;
+ public static $LOG_EXTENDED = 2;
+
+ private static $enabled = false;
+ private static $quiet = false;
+ private static $logfile = false;
+ private static $loglevel = 0;
+
+ public static function set_logfile($logfile) {
+ self::$logfile = $logfile;
+ }
+
+ public static function enabled() {
+ return self::$enabled;
+ }
+
+ public static function set_enabled($enable) {
+ self::$enabled = $enable;
+ }
+
+ public static function set_quiet($quiet) {
+ self::$quiet = $quiet;
+ }
+
+ public static function set_loglevel($level) {
+ self::$loglevel = $level;
+ }
+
+ public static function get_loglevel() {
+ return self::$loglevel;
+ }
+
+ public static function log($message, $level = 0) {
+
+ if (!self::$enabled || self::$loglevel < $level) return false;
+
+ $ts = strftime("%H:%M:%S", time());
+ if (function_exists('posix_getpid')) {
+ $ts = "$ts/" . posix_getpid();
+ }
+
+ if (self::$logfile) {
+ $fp = fopen(self::$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);
+ user_error("Unable to lock debugging log file: " . self::$logfile, E_USER_WARNING);
+ return;
+ }
+ }
+
+ fputs($fp, "[$ts] $message\n");
+
+ if (function_exists("flock")) {
+ flock($fp, LOCK_UN);
+ }
+
+ fclose($fp);
+
+ if (self::$quiet)
+ return;
+
+ } else {
+ user_error("Unable to open debugging log file: " . self::$logfile, E_USER_WARNING);
+ }
+ }
+
+ print "[$ts] $message\n";
+ }
+}