<?php
namespace EasyChat\Library\Messenger;









final class MaxPrivateFileLock {

    


    public static function open(string $path) {
        if ($path === '' || strpos($path, "\0") !== false) {
            return false;
        }

        $dir = dirname($path);
        if (!is_dir($dir) && !self::createDirectory($dir)) {
            return false;
        }
        if (!is_dir($dir) || !is_writable($dir) || is_link($path) || is_dir($path)) {
            return false;
        }
        if (file_exists($path)) {
            $existing = false;
            self::withoutWarnings(static function () use ($path, &$existing): void {
                $existing = lstat($path);
            });
            if (
                !is_array($existing)
                || (($existing['mode'] ?? 0) & 0170000) !== 0100000
                || (int)($existing['nlink'] ?? 0) !== 1
            ) {
                return false;
            }
        }

        $handle = false;
        self::withoutWarnings(static function () use ($path, &$handle): void {
            $previous_umask = umask(0077);
            try {
                $handle = fopen($path, 'x+b');
            } finally {
                umask($previous_umask);
            }

            if (!is_resource($handle) && !is_link($path) && !is_dir($path)) {
                $handle = fopen($path, 'c+b');
            }
        });
        if (!is_resource($handle)) {
            return false;
        }

        $safe = false;
        self::withoutWarnings(static function () use ($path, $handle, &$safe): void {
            clearstatcache(true, $path);
            $path_stat = lstat($path);
            $handle_stat = fstat($handle);
            $safe = is_array($path_stat)
                && is_array($handle_stat)
                && (($path_stat['mode'] ?? 0) & 0170000) === 0100000
                && (($handle_stat['mode'] ?? 0) & 0170000) === 0100000
                && (int)($path_stat['nlink'] ?? 0) === 1
                && (int)($handle_stat['nlink'] ?? 0) === 1
                && (int)($path_stat['dev'] ?? -1) === (int)($handle_stat['dev'] ?? -2)
                && (int)($path_stat['ino'] ?? -1) === (int)($handle_stat['ino'] ?? -2)
                 
                 
                && (((int)($handle_stat['mode'] ?? 0) & 0022) === 0);
        });

        if (!$safe) {
            fclose($handle);
            return false;
        }

        return $handle;
    }

    private static function createDirectory(string $dir): bool {
        $created = false;
        self::withoutWarnings(static function () use ($dir, &$created): void {
            $previous_umask = umask(0077);
            try {
                $created = mkdir($dir, 0700, true);
            } finally {
                umask($previous_umask);
            }
        });
        return $created || is_dir($dir);
    }

    private static function withoutWarnings(callable $callback): void {
        set_error_handler(static function (): bool {
            return true;
        });
        try {
            $callback();
        } catch (\Throwable $e) {
             
        } finally {
            restore_error_handler();
        }
    }
}
