VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /var/www/html/webtrees/app/GedcomFilters/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/html/webtrees/app/GedcomFilters/GedcomEncodingFilter.php
<?php

/**
 * @copyright 2021 Greg Roach <greg@subaqua.co.uk>
 * @license   GPLv3+
 */

declare(strict_types=1);

namespace Fisharebest\Webtrees\GedcomFilters;

use Fisharebest\Webtrees\Encodings\EncodingInterface;
use Fisharebest\Webtrees\Registry;
use php_user_filter;

use function stream_bucket_append;
use function stream_bucket_make_writeable;
use function stream_bucket_new;
use function substr;

use const PSFS_FEED_ME;
use const PSFS_PASS_ON;

/**
 * Filter a GEDCOM data stream, converting to UTF-8.
 *
 * These properties are created after the class is instantiated.
 *
 * @property string               $filtername
 * @property array<string,string> $params
 * @property resource             $stream
 */
class GedcomEncodingFilter extends php_user_filter
{
    private string $data = '';

    private ?EncodingInterface $src_encoding = null;

    private ?EncodingInterface $dst_encoding = null;

    /**
     * Initialization
     */
    public function onCreate(): bool
    {
        parent::onCreate();

        $src_encoding = $this->params['src_encoding'] ?? '';
        $dst_encoding = $this->params['dst_encoding'] ?? 'UTF-8';

        if ($src_encoding !== '') {
            $this->src_encoding = Registry::encodingFactory()->make($src_encoding);
        }

        $this->dst_encoding = Registry::encodingFactory()->make($dst_encoding);

        return true;
    }

    /**
     * Filter some data.
     *
     * @param resource $in       Read from this input stream
     * @param resource $out      Write to this output stream
     * @param int      $consumed Count of bytes processed
     * @param bool     $closing  Is the input about to end?
     *
     * @return int PSFS_PASS_ON / PSFS_FEED_ME / PSFS_ERR_FATAL
     */
    public function filter($in, $out, &$consumed, $closing): int
    {
        $return = PSFS_FEED_ME;

        // While input data is available, continue to read it.
        while ($bucket_in = stream_bucket_make_writeable($in)) {
            $this->data .= $bucket_in->data;
            $consumed   += $bucket_in->datalen;

            $this->src_encoding ??= Registry::encodingFactory()->detect($this->data);

            if ($this->src_encoding instanceof EncodingInterface) {
                $bytes      = $this->src_encoding->convertibleBytes($this->data);
                $data_in    = substr($this->data, 0, $bytes);
                $data_out   = $this->dst_encoding->fromUtf8($this->src_encoding->toUtf8($data_in));
                $bucket_out = stream_bucket_new($this->stream, $data_out);
                $this->data = substr($this->data, $bytes);
                $return     = PSFS_PASS_ON;
                stream_bucket_append($out, $bucket_out);
            }
        }

        // Process the final record.
        if ($closing && $this->data !== '') {
            $this->src_encoding ??= Registry::encodingFactory()->make('UTF-8');
            $data_out           = $this->dst_encoding->fromUtf8($this->src_encoding->toUtf8($this->data));
            $bucket_out         = stream_bucket_new($this->stream, $data_out);
            $return             = PSFS_PASS_ON;
            stream_bucket_append($out, $bucket_out);
        }

        return $return;
    }
}

VaKeR 2022