<?php

if (!function_exists('sv') || preg_match('#/plugins/realblog/include/flatfile.php#i', $_SERVER['SCRIPT_NAME']))
{
	die('no direct access');
}

/* utf-8 marker äöüß */

/* 
prepared for php 8.2 by ge-webdesign 2023-01 
prepared for php 8.1.3 by ge-webdesign 2022-04 
*/

/*
Copyright (c) 2005 Luke Plant <L.Plant.98@cantab.net>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject 
to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
 portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 * Simple but powerful flatfile database
 * See http://lukeplant.me.uk/resources/flatfile/ for documentation and examples
 *
 * @tutorial flatfile.pkg
 * @package flatfile
 * @license http://www.opensource.org/licenses/mit-license.php
 */
 
if(!class_exists('Flatfile')){

/** Used to indicate a comparison should be done as a string comparison */
define('STRING_COMPARISON', 'strcmp');
/** Used to indicate a comparison should be done as an integer comparison */
define('INTEGER_COMPARISON', 'intcmp');
/** Used to indicate a comparison should be done as a numeric (float) comparison */
define('NUMERIC_COMPARISON', 'numcmp');

/** Indicates ascending order */
define('ASCENDING', 1);
/** Indicates descending order */
define('DESCENDING', -1);

/**
 * Provides simple but powerful flatfile database storage and retrieval
 *
 * Includes equivalents to SELECT * FROM table WHERE..., DELETE WHERE ...
 * UPDATE and more.  All files are stored in the {@link Flatfile::$datadir $datadir} directory,
 * and table names are just filenames in that directory.  Subdirectories
 * can be used just by specifying a table name that includes the directory name.
 * @package flatfile
 */
class Flatfile {
	/** @access private */
	var $tables;
	
	/** The directory to store files in. 
	 * @var string
	 */
	var $datadir;

	/**
	 * Get all rows from a table
	 * @param string $tablename	The table to get rows from
	 * @return array The table as an array of rows, where each row is an array of columns
	 */
	function selectAll ($tablename) {
		if (!isset($this->tables[$tablename]))
			$this->loadTable($tablename);
		return $this->tables[$tablename];	
	}
	
	/**
	 * Selects rows from a table that match the specified criteria
	 *
	 * This simulates the following SQL query:
	 * <pre>
	 *   SELECT LIMIT $limit * FROM  $tablename
	 *   WHERE $whereclause
	 *   ORDER BY $orderBy [ASC | DESC] [, $orderBy2 ...]
	 * </pre>
	 *
	 * @param string $tablename	The table (file) to get the data from
	 * @param object $whereClause 	Either a {@link WhereClause WhereClause} object to do selection of rows, or NULL to select all
	 * @param mixed $limit  	Specifies limits for the rows returned:
	 * - use -1 or omitted to return all rows
	 * - use an integer n to return the first n rows
	 * - use a two item array ($startrow, $endrow) to return rows $startrow to $endrow - 1 (zero indexed)
	 * - use a two item array ($startrow, -1) to return rows $startrow to the end (zero indexed)
	 * @param mixed $orderBy	Either an {@link OrderBy} object or an array of them, defining the sorting that should be applied (if an array, then the first object in the array is the first key to sort on etc).  Use NULL for no sorting.
	 * @return array The matching data, as an array of rows, where each row is an array of columns
	 */
	function selectWhere ($tablename, $whereClause, $limit = -1, $orderBy = NULL) {
		if (!isset($this->tables[$tablename]))
			$this->loadTable($tablename);
		
		$table = $this->selectAll($tablename); // Get a copy
		
		if ($orderBy !== NULL)
			usort($table, $this->getOrderByFunction($orderBy));
		
		$results = array();
		$count = 0;

		if ($limit == -1)
			$limit = array(0, -1);
		else if (!is_array($limit))
			$limit = array(0, $limit);
		
		foreach ($table as $row) {
			if ($whereClause === NULL || $whereClause->testRow($row)) {
				if (($count >= $limit[1]) && ($limit[1] != -1))
					break;
				if ($count >= $limit[0]) 
					$results[] = $row;
				++$count;
			}
		}
		return $results;
	}
	
	/**
	 * Select a row using a unique ID
	 * @param string $tablename 	The table to get data from
	 * @param string $idField	The index of the field containing the ID
	 * @param string $id		The ID to search for
	 * @return array 	The row of the table as an array
	 */
	function selectUnique ($tablename, $idField, $id) {
		if (!isset($this->tables[$tablename]))
			$this->loadTable($tablename);
		foreach ($this->tables[$tablename] as $row) {
			if ($row[$idField] == $id) {	
				return $row;
			}
		}
		return array();
	}
	
	/*
	 * To correctly write a file, and not overwrite the changes
	 * another process is making, we need to:
	 *  - get a lock for writing
	 *  - read its contents from disc
	 *  - modify the contents in memory
	 *  - write the contents
	 *  - release lock
	 * Because opening for writing truncates the file, we must get
	 * the lock on a different file.  getLock and releaseLock
	 * are helper functions to allow us to do this with little fuss
	 */
	 
	/** Get a lock for writing a file
	 * @access private
	 */
	function getLock ($tablename)
	{
		ignore_user_abort(true);
		$fp = fopen($this->datadir . $tablename.'.lock','w');
		if (!flock($fp, LOCK_EX)) {
		   // log error?
		}
		$this->loadTable($tablename);
		return $fp;
	}
	
	/** Release a lock
	 * @access private
	 */
	function releaseLock ($lockfp)
	{
		flock($lockfp, LOCK_UN);
		ignore_user_abort(false);
	}
	
	/**
	 * Inserts a row with an automatically generated ID
	 *
	 * The autogenerated ID will be the highest ID in the column so far plus one. The
	 * supplied row should include all fields required for the table, and the 
	 * ID field it contains will just be ignored
	 *
	 * @param string $tablename	The table to insert data into
	 * @param int $idField		The index of the field which is the ID field
	 * @param array $newRow		The new row to add to the table
	 * @return int		The newly assigned ID	 
	 */
	function insertWithAutoId ($tablename, $idField, $newRow) 
	{
		$lockfp = $this->getLock($tablename);
		$rows = $this->selectWhere($tablename, null, 1, 
				new OrderBy($idField, DESCENDING, INTEGER_COMPARISON));
		if ($rows) {
			$newId = $rows[0][$idField] + 1;
		} else {
			$newId = 1;
		}
		$newRow[$idField] = $newId;
		$this->tables[$tablename][] = $newRow;
		$this->writeTable($tablename);
		$this->releaseLock($lockfp);
		return $newId;
	}

	/**
	 * Inserts a row in a table
	 *
	 * @param string $tablename	The table to insert data into
	 * @param array $newRow		The new row to add to the table
	 */
	function insert ($tablename, $newRow) 
	{
		$lockfp = $this->getLock($tablename);			
		$this->tables[$tablename][] = $newRow;
		$this->writeTable($tablename);
		$this->releaseLock($lockfp);
	}
	
	/**
	 * Updates an existing row using a unique ID
	 *
	 * @param string $tablename	The table to update
	 * @param int $idField		The index of the field which is the ID field
	 * @param array $updatedRow	The updated row to add to the table
	 */
	function updateRowById ($tablename, $idField, $updatedRow)
	{
		$this->updateSetWhere($tablename, $updatedRow, 
			new SimpleWhereClause($idField, '=', $updatedRow[$idField]));
	}
	
	/**
	 * Updates fields in a table for rows that match the provided criteria
	 * 
	 * $newFields can be a complete row or it can be a sparsely populated
	 * hashtable of values (where the keys are integers which are the column
	 * indexes to update)
	 *
	 * @param string $tablename	The table to update
	 * @param array $newFields	A hashtable (with integer keys) of fields to update
	 * @param WhereClause $whereClause	The criteria or NULL to update all rows 
	 */
	function updateSetWhere ($tablename, $newFields, $whereClause)
	{
		$lockfp = $this->getLock($tablename);
		for ($i = 0; $i < count($this->tables[$tablename]); ++$i) {
			if ($whereClause === NULL ||
			    $whereClause->testRow($this->tables[$tablename][$i])) {
				foreach ($newFields as $k => $v)
				{
					$this->tables[$tablename][$i][$k] = $v;
				}
			}
		}
		$this->writeTable($tablename);
		$this->releaseLock($lockfp);
		$this->loadTable($tablename);
	}
	/**
	 * Deletes all rows in a table that match specified criteria
	 *
	 * @param string $tablename	The table to alter
	 * @param object $whereClause.  {@link WhereClause WhereClause} object that will select 
	 * rows to be deleted.  All rows are deleted if $whereClause === NULL
	 */
	function deleteWhere ($tablename, $whereClause) {
		$lockfp = $this->getLock($tablename);		
		for ($i = count($this->tables[$tablename]) - 1; $i >= 0 ; --$i) {
			if ($whereClause === NULL ||
			    $whereClause->testRow($this->tables[$tablename][$i])) {
				unset($this->tables[$tablename][$i]);
			}
		}
		$this->writeTable($tablename);
		$this->releaseLock($lockfp);
		$this->loadTable($tablename); // reset array indexes
	}
	
	/**
	 * Delete all rows in a table
	 *
	 * @param string $tablename	The table to alter
	 */
	function deleteAll ($tablename) {
		$this->deleteWhere($tablename, NULL);
	}

	/**#@+
	 * @access private
	 */
	
	/** Gets a function that can be passed to usort to do the ORDER BY clause
	 * @param mixed $orderBy	Either an OrderBy object or an array of them
	 * @return string function name
	 */
	function getOrderByFunction ($orderBy)
	{
		$orderer = new Orderer($orderBy);
		return array(&$orderer, 'compare');
	}

	function loadTable ($tablename) {
		$filedata = @file($this->datadir . $tablename);
		$table = array();
		if (is_array($filedata)) {
			foreach ($filedata as $line) {
				$line = rtrim($line, "\n");
				$table[] = explode("\t", $line);
			}
		}
		$this->tables[$tablename] = $table;
	}

	function writeTable ($tablename) {
		$output = '';
		
		foreach ($this->tables[$tablename] as $row) {
			for ($i = 0; $i < count($row); ++$i) {
				if ($i > 0) $output .= "\t";
				if($row[$i] != null) $output .= str_replace(array("\t","\r","\n"), array(''), $row[$i]);
			}
			$output .= "\n";
		}
		$fp = @fopen($this->datadir . $tablename, "w");
		fwrite($fp, $output, strlen($output));
		fclose($fp);
	}
	/**#@-*/
}
}
/////////////////////////// UTILITY FUNCTIONS ////////////////////////////////////

/** 
 * equivalent of strcmp for comparing integers, used internally for sorting and comparing 
 */
if(!function_exists("intcmp")){
function intcmp ($a, $b)
{
	return (int)$a - (int)$b;
}
}
/** 
 * equivalent of strcmp for comparing floats, used internally for sorting and comparing 
 */
if(!function_exists("numcmp")){
function numcmp ($a, $b)
{
	return (float)$a - (float)$b;
}
}
/////////////////////////// WHERE CLAUSE CLASSES ////////////////////////////////////

/**
 * Used to test rows in a database table, like the WHERE clause in an SQL statement.
 *
 * @abstract
 * @package flatfile
 */
if (!class_exists("WhereClause")){
#[AllowDynamicProperties]
class WhereClause 
{
	/**
	 * Tests a table row object
	 * @abstract
	 * @param array $row   The row to test
	 * @return bool True if the $row passes the WhereClause
	 * selection criteria, false otherwise
	 */
	function testRow ($row) {}
}
}
if (!class_exists("LikeWhereClause")){
#[AllowDynamicProperties]
class LikeWhereClause extends WhereClause
{
function __construct ($field, $value,$operator=NULL)
{
$this->field = $field;
// Original code from Luke Plant
//$this->regexp = '/^' . str_replace('%','.*', preg_quote($value)) . '$/';
//$start='';
//$end='';
// Modified code by JAT
if($operator==NULL||$operator==2){$start='';$end='';}
if($operator==1){$start='^';$end='';}
if($operator==3){$start='';$end='$';}

$this->regexp = '/'.$start.preg_quote(trim($value)).$end.'/i';
}

function testRow ($tablerow) {
return preg_match($this->regexp, $tablerow[$this->field]);
}
}
}





/**
 * Negates a where clause
 * @package flatfile
 */
if (!class_exists("NotWhere")){
#[AllowDynamicProperties]
class NotWhere extends WhereClause
{
	/** @access private */
	var $clause;
	
	/**
	 * Contructs a new NotWhere object
	 *
	 * The constructed WhereClause will return the negation 
	 * of the WhereClause object passed in when testing rows.
	 * @param WhereClause $whereclause The WhereClause object to negate
	 */
	function __construct ($whereclause)
	{
		$this->clause = $whereclause;
	}
	
	function testRow ($row) {
		return !$this->clause->testRow($row);
	}
}
}
/**
 * Implements a single WHERE clause that does simple comparisons of a field
 * with a value.
 *
 * @package flatfile
 */
if (!class_exists("SimpleWhereClause")){
#[AllowDynamicProperties]
class SimpleWhereClause extends WhereClause 
{
	/**#@+
	 * @access private
	 */
	var $field;
	var $operator;
	var $value;
	var $compare_type;
	/**#@-*/

	/**
	 * Creates a new {@link WhereClause WhereClause} object that does a comparison
	 * of a field and a value.
	 *
	 * This will be the most commonly used type of WHERE clause.  It can do comparisons
	 * of the sort "$tablerow[$field] operator $value"
	 * where 'operator' is one of:<br>
	 * - = (equals)
	 * - != (not equals)
	 * - > (greater than)
	 * - < (less than)
	 * - >= (greater than or equal to)
	 * - <= (less than or equal to)
	 * There are 3 pre-defined constants (STRING_COMPARISON, NUMERIC COMPARISON and
	 * INTEGER_COMPARISON) that modify the behaviour of these operators to do the comparison
	 * as strings, floats and integers respectively.  Howevers, these constants are 
	 * just the names of functions that do the comparison (the first being the builtin
	 * function {@link strcmp strcmp()}, so you can supply your own function here to customise the 
	 * behaviour of this class.
	 * 
	 * @param int $field        	The index (in the table row) of the field to test
	 * @param string $operator  	The comparison operator, one of "=", "!=", "<", ">", "<=", ">="
	 * @param mixed $value		The value to compare to.
	 * @param string $compare_type	The comparison method to use - either
	 * STRING_COMPARISON (default), NUMERIC COMPARISON or INTEGER_COMPARISON
	 *
	 */
	function __construct ($field, $operator, $value, $compare_type = STRING_COMPARISON)
	{
		$this->field = $field;
		$this->operator = $operator;
		$this->value = $value;
		$this->compare_type = $compare_type;
	}
	
	function testRow ($tablerow) {
		if ($this->field < 0)
			return TRUE;
		
		$cmpfunc = $this->compare_type;
		$cmp = $cmpfunc($tablerow[$this->field], $this->value);
		if ($this->operator == '=')
			return ($cmp == 0);
		else if ($this->operator == '!=')
			return ($cmp != 0);
		else if ($this->operator == '>')
			return ($cmp > 0);
		else if ($this->operator == '<')
			return ($cmp < 0);
		else if ($this->operator == '<=')
			return ($cmp <= 0);
		else if ($this->operator == '>=')
			return ($cmp >= 0);
			
		return FALSE;
	}
}
}
/**
 * {@link WhereClause WhereClause} class to match a value from a list of items
 * @package flatfile
 */
if (!class_exists("ListWhereClause")){
#[AllowDynamicProperties]
class ListWhereClause extends WhereClause {

	/** @access private */
	var $field;
	/** @access private */
	var $list;
	/** @access private */
	var $compareAs;
	
	/**
	 * Creates a new ListWhereClause object
	 *
	 * The resulting WhereClause will pass rows (return true) if the value of the specified
	 * field is in the array.
	 * 
	 * @param int $field		Field to match
	 * @param array $list		List of items
	 * @param string $compare_type Comparison type, string by default.
	 */
	function __construct ($field, $list, $compare_type = STRING_COMPARISON) {
		$this->list = $list;
		$this->field = (int)$field;
		$this->compareAs = $compare_type;
	}
	
	function testRow ($tablerow) {
		$func = $this->compareAs;
		foreach ($this->list as $item)
		{
			if  ($func($tablerow[$this->field], $item) == 0)
				return true;
		}
		return false;
	}
}
}
/**
 * Abstract class that combines zero or more {@link WhereClause WhereClause} objects
 * together.
 * @package flatfile
 */ 
if (!class_exists("CompositeWhereClause")){
#[AllowDynamicProperties]
class CompositeWhereClause extends WhereClause
{
	/** 
	 * @var array Stores the child clauses
	 * @access protected 
	 */
	var $clauses = array();
	
	/**
	 * Add a {@link WhereClause WhereClause} to the list of clauses to be used for testing
	 * @param WhereClause $whereClause	The WhereClause object to add
	 */ 
	function add ($whereClause)
	{
		$this->clauses[] = $whereClause;
	}
}
}
/**
 * {@link CompositeWhereClause CompositeWhereClause} that does an OR on all its
 * child WhereClauses.
 *
 * Use the {@link CompositeWhereClause::add() add()} method and/or the constructor
 * to add WhereClause objects
 * to the list of clauses to check.  The testRow function of the resulting object
 * will then return true if any of its child clauses return true (and returns
 * false if no clauses have been added for consistency).
 * @package flatfile
 */
if (!class_exists("OrWhereClause")){
#[AllowDynamicProperties]
class OrWhereClause extends CompositeWhereClause 
{
	function testRow ($tablerow) {
		foreach ($this->clauses as $clause) {
			if ($clause->testRow($tablerow))
				return true;
		}
		return false;
	}
	
	/**
	 * Creates a new OrWhereClause 
	 * @param WhereClause $whereClause,... optional unlimited list of WhereClause objects to be added
	 */ 
	function __construct() {
		$this->clauses = func_get_args();
	}
}
}
/**
 * {@link CompositeWhereClause CompositeWhereClause} that does an AND on all its
 * child WhereClauses.
 *
 * Use the {@link CompositeWhereClause::add() add()} method to add WhereClause objects
 * to the list of clauses to check.  The testRow function of the resulting object
 * will then return false if any of its child clauses return false (and returns
 * true if no clauses have been added for consistency).
 * @package flatfile
 */
if (!class_exists("AndWhereClause")){
#[AllowDynamicProperties]
class AndWhereClause extends CompositeWhereClause 
{
	function testRow ($tablerow) {
		foreach ($this->clauses as $clause) {
			if (!$clause->testRow($tablerow))
				return false;
		}
		return true;
	}
	
	/**
	 * Creates a new AndWhereClause 
	 * @param WhereClause $whereClause,... optional unlimited list of WhereClause objects to be added
	 */ 
	function __construct() {
		$this->clauses = func_get_args();
	}
}
}

/////////////////////////// ORDER BY CLASSES ////////////////////////////////////

/**
 * Stores information about an ORDER BY clause
 *
 * Can be passed to selectWhere to order the output.  It is easiest to use
 * the constructor to set the fields, rather than setting each individually
 */
if (!class_exists("OrderBy")){
#[AllowDynamicProperties]
class OrderBy {
	/** @var int Index of field to order by */
	var $field;
	/** @var int Order type - ASCENDING or DESCENDING */
	var $orderType;
	/** @var string Comparison type  - usually either STRING_COMPARISON, INTEGER_COMPARISION, or NUMERIC_COMPARISON*/
	var $compareAs;
	
	/** Creates a new OrderBy structure
	 *
	 * The $compareAs parameter can be supplied using one of the pre-defined constants, but 
	 * this is actually implemented by defining the constants as names of functions to do the
	 *  comparison.  You can therefore supply the name of any function that works like 
	 * {@link strcmp strcmp()} to implement custom ordering.
	 * @param int $field	The index of the field to order by
	 * @param int $orderType	ASCENDING or DESCENDING
	 * @param int $compareAs	Comparison type: STRING_COMPARISON, INTEGER_COMPARISION,
	 * or NUMERIC_COMPARISON, or the name of a user defined function that you want to use for doing the comparison.
	 */
	function __construct ($field, $orderType, $compareAs = STRING_COMPARISON)
	{
		$this->field = $field;
		$this->orderType = $orderType;
		$this->compareAs = $compareAs;
	}
}
}
/**
 * Implements the sorting defined by an array of OrderBy objects.  This class
 * is used by {@link Flatfile::selectWhere()}
 * @access private 
 */
if (!class_exists("Orderer")){
#[AllowDynamicProperties]
class Orderer {
	/** 
	 * @var array Stores the OrderBy objects
	 * @access private 
	 */
	var $orderByList;
	
	/**
	 * Creates new Orderer that will provide a sort function
	 * @param mixed $orderBy	An OrderBy object or an array of them
	 */
	function __construct ($orderBy) {
		if (!is_array($orderBy))
			$orderBy = array($orderBy);
		$this->orderByList = $orderBy;
	}
	
	/**
	 * Compares two table rows using the comparisons defined by the OrderBy 
	 * objects.  This function is of the type that can be used passed to usort().
	 */
	function compare($row1, $row2) {
		return $this->compare_priv($row1, $row2, 0);
	}
	
	/**
	 * @access private
	 */
	function compare_priv($row1, $row2, $index)
	{
		$orderBy = $this->orderByList[$index];
		$cmpfunc = $orderBy->compareAs;
		$cmp = $orderBy->orderType * $cmpfunc($row1[$orderBy->field], $row2[$orderBy->field]);
		if($cmp == 0) {
			if ($index == (count($this->orderByList) - 1))
				return 0;
			else
				return $this->compare_priv($row1, $row2, $index + 1);
		} else
			return $cmp;
	}
}
}
