HEX
Server: LiteSpeed
System: Linux d8 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64
User: wbwebdes (3015)
PHP: 8.1.31
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/wbwebdes/domains/files.wb-cloud.nl/private_html/3rdparty/icewind/smb/src/KerberosTicket.php
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2022 Robin Appelman <[email protected]>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Icewind\SMB;

use Icewind\SMB\Exception\InvalidTicket;
use KRB5CCache;

class KerberosTicket {
	/** @var KRB5CCache */
	private $krb5;
	/** @var string */
	private $cacheName;

	public function __construct(KRB5CCache $krb5, string $cacheName) {
		$this->krb5 = $krb5;
		$this->cacheName = $cacheName;
	}

	public function getCacheName(): string {
		return $this->cacheName;
	}

	public function getName(): string{
		return $this->krb5->getName();
	}

	public function isValid(): bool {
		return count($this->krb5->getEntries()) > 0;
	}

	public function validate(): void {
		if (!$this->isValid()) {
			throw new InvalidTicket("No kerberos ticket found.");
		}
	}

	/**
	 * Load the ticket from the cache specified by the KRB5CCNAME variable.
	 *
	 * @return KerberosTicket|null
	 */
	public static function fromEnv(): ?KerberosTicket {
		$ticketName = getenv("KRB5CCNAME");
		if (!$ticketName) {
			return null;
		}
		$krb5 = new KRB5CCache();
		$krb5->open($ticketName);
		return new KerberosTicket($krb5, $ticketName);
	}

	public static function load(string $ticket): KerberosTicket {
		$tmpFilename = tempnam(sys_get_temp_dir(), "krb5cc_php_");
		file_put_contents($tmpFilename, $ticket);
		register_shutdown_function(function () use ($tmpFilename) {
			if (file_exists($tmpFilename)) {
				unlink($tmpFilename);
			}
		});

		$ticketName = "FILE:" . $tmpFilename;
		$krb5 = new KRB5CCache();
		$krb5->open($ticketName);
		return new KerberosTicket($krb5, $ticketName);
	}

	public function save(): string {
		if (substr($this->cacheName, 0, 5) === 'FILE:') {
			$ticket = file_get_contents(substr($this->cacheName, 5));
		} else {
			$tmpFilename = tempnam(sys_get_temp_dir(), "krb5cc_php_");
			$tmpCacheFile = "FILE:" . $tmpFilename;
			$this->krb5->save($tmpCacheFile);
			$ticket = file_get_contents($tmpFilename);
			unlink($tmpFilename);
		}
		return $ticket;
	}
}