2023-09-03 09:33:39 -04:00
|
|
|
<?php
|
|
|
|
|
2023-09-07 10:17:04 -04:00
|
|
|
// Core file for the client-side library for Blueprint Extensions
|
2023-09-03 09:33:39 -04:00
|
|
|
|
2023-09-07 10:17:04 -04:00
|
|
|
|
|
|
|
namespace Pterodactyl\BlueprintFramework\Libraries\ExtensionLibrary\Client;
|
2023-09-03 09:33:39 -04:00
|
|
|
|
|
|
|
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
|
|
|
|
|
2023-09-07 10:17:04 -04:00
|
|
|
class BlueprintClientLibrary
|
2023-09-03 09:33:39 -04:00
|
|
|
{
|
2023-09-07 10:17:04 -04:00
|
|
|
// Construct core
|
2023-09-03 09:33:39 -04:00
|
|
|
public function __construct(
|
|
|
|
private SettingsRepositoryInterface $settings,
|
|
|
|
) {
|
|
|
|
}
|
2024-04-27 09:17:09 -04:00
|
|
|
|
2024-04-27 09:09:34 -04:00
|
|
|
/**
|
|
|
|
* Fetch a record from the database.
|
|
|
|
*
|
|
|
|
* @param string $table Database table
|
|
|
|
* @param string $record Database record
|
|
|
|
* @return mixed Database value
|
|
|
|
*
|
|
|
|
* [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint)
|
|
|
|
*/
|
2024-04-27 09:17:09 -04:00
|
|
|
public function dbGet($table, $record): mixed {
|
2023-09-03 09:33:39 -04:00
|
|
|
return $this->settings->get($table."::".$record);
|
|
|
|
}
|
|
|
|
|
2024-04-27 09:09:34 -04:00
|
|
|
/**
|
|
|
|
* Set a database record.
|
|
|
|
*
|
|
|
|
* @param string $table Database table
|
|
|
|
* @param string $record Database record
|
|
|
|
* @param string $value Value to store
|
|
|
|
*
|
|
|
|
* [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint)
|
|
|
|
*/
|
2023-09-03 09:33:39 -04:00
|
|
|
public function dbSet($table, $record, $value) {
|
|
|
|
return $this->settings->set($table."::".$record, $value);
|
|
|
|
}
|
|
|
|
|
2024-04-27 09:17:09 -04:00
|
|
|
/**
|
|
|
|
* Delete/forget a database record.
|
|
|
|
*
|
|
|
|
* @param string $table Database table
|
|
|
|
* @param string $record Database record
|
|
|
|
*
|
|
|
|
* [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint)
|
|
|
|
*/
|
|
|
|
public function dbForget($table, $record) {
|
|
|
|
return $this->settings->forget($table."::".$record);
|
|
|
|
}
|
|
|
|
|
2024-04-27 09:09:34 -04:00
|
|
|
/**
|
|
|
|
* Read and returns the content of a given file.
|
|
|
|
*
|
|
|
|
* @param string $path Path to file
|
|
|
|
* @return string File contents
|
2024-09-08 14:16:56 -04:00
|
|
|
* @throws string File cannot be read or found
|
2024-04-27 09:09:34 -04:00
|
|
|
*
|
|
|
|
* [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint)
|
|
|
|
*/
|
2023-09-03 09:33:39 -04:00
|
|
|
public function fileRead($path) {
|
2024-09-08 14:16:56 -04:00
|
|
|
if (!file_exists($path)) {
|
|
|
|
return "File not found: " . $path;
|
|
|
|
}
|
|
|
|
if (!is_readable($path)) {
|
|
|
|
return "File is not readable: " . $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_get_contents($path);
|
2023-09-03 09:33:39 -04:00
|
|
|
}
|
2023-09-03 09:45:54 -04:00
|
|
|
|
2024-04-27 09:09:34 -04:00
|
|
|
/**
|
2024-09-08 14:12:31 -04:00
|
|
|
* Attempts to create a file.
|
2024-04-27 09:09:34 -04:00
|
|
|
*
|
|
|
|
* @param string $path File name/path
|
2024-09-08 14:08:08 -04:00
|
|
|
* @return void
|
2024-04-27 09:09:34 -04:00
|
|
|
*
|
|
|
|
* [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint)
|
|
|
|
*/
|
2024-09-08 14:08:08 -04:00
|
|
|
public function fileMake($path): void {
|
2024-06-22 10:30:00 -04:00
|
|
|
$file = fopen($path, "w");
|
|
|
|
fclose($file);
|
2024-09-08 14:08:08 -04:00
|
|
|
return;
|
2023-09-03 09:45:54 -04:00
|
|
|
}
|
|
|
|
|
2024-04-27 09:09:34 -04:00
|
|
|
/**
|
2024-09-08 14:12:31 -04:00
|
|
|
* Attempts to remove a file or directory.
|
2024-04-27 09:09:34 -04:00
|
|
|
*
|
2024-09-08 14:12:31 -04:00
|
|
|
* @param string $path Path to file/directory
|
|
|
|
* @return void
|
2024-04-27 09:09:34 -04:00
|
|
|
*
|
|
|
|
* [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint)
|
|
|
|
*/
|
2023-09-03 09:45:54 -04:00
|
|
|
public function fileWipe($path) {
|
2024-09-08 14:12:31 -04:00
|
|
|
if(is_dir($path)) {
|
|
|
|
$files = array_diff(scandir($path), ['.', '..']);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$this->fileWipe($path . DIRECTORY_SEPARATOR . $file);
|
|
|
|
}
|
|
|
|
rmdir($path);
|
|
|
|
} elseif (is_file($path)) {
|
|
|
|
unlink($path);
|
|
|
|
}
|
2023-09-03 09:45:54 -04:00
|
|
|
}
|
2024-02-27 16:34:10 -05:00
|
|
|
|
2024-04-27 09:09:34 -04:00
|
|
|
/**
|
|
|
|
* Check if an extension is installed based on it's identifier.
|
|
|
|
*
|
|
|
|
* @param string $identifier Extension identifier
|
|
|
|
* @return bool Boolean
|
|
|
|
*
|
|
|
|
* [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint)
|
|
|
|
*/
|
|
|
|
public function extension($identifier): bool {
|
2024-06-11 16:21:55 -04:00
|
|
|
if(str_contains($this->fileRead(base_path(".blueprint/extensions/blueprint/private/db/installed_extensions")), $identifier.',')) {
|
2024-02-27 16:34:10 -05:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-08-19 06:19:40 -04:00
|
|
|
|
2024-09-08 14:05:22 -04:00
|
|
|
/**
|
|
|
|
* Returns an array containing all installed extensions's identifiers.
|
|
|
|
*
|
|
|
|
* @return array Array
|
|
|
|
*
|
|
|
|
* [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint)
|
|
|
|
*/
|
|
|
|
public function extensionList(): array {
|
|
|
|
$array = explode(',', $this->fileRead(base_path(".blueprint/extensions/blueprint/private/db/installed_extensions")));
|
|
|
|
$extensions = array_filter($array, function($value) {
|
|
|
|
return !empty($value);
|
|
|
|
});
|
|
|
|
return $extensions;
|
|
|
|
}
|
|
|
|
|
2024-08-19 06:19:40 -04:00
|
|
|
/**
|
|
|
|
* Returns a HTML link tag importing the specified stylesheet with additional URL params to avoid issues with stylesheet cache.
|
|
|
|
*
|
|
|
|
* @param string $url Stylesheet URL
|
|
|
|
* @return string HTML <link> tag
|
|
|
|
*
|
|
|
|
* [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint)
|
|
|
|
*/
|
|
|
|
public function importStylesheet($url): string {
|
|
|
|
$cache = $this->dbGet("blueprint", "cache");
|
|
|
|
return "<link rel=\"stylesheet\" href=\"$url?v=$cache\">";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a HTML script tag importing the specified script with additional URL params to avoid issues with script cache.
|
|
|
|
*
|
|
|
|
* @param string $url Script URL
|
|
|
|
* @return string HTML <script> tag
|
|
|
|
*
|
|
|
|
* [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint)
|
|
|
|
*/
|
|
|
|
public function importScript($url): string {
|
|
|
|
$cache = $this->dbGet("blueprint", "cache");
|
|
|
|
return "<script src=\"$url?v=$cache\"></script>";
|
|
|
|
}
|
2023-09-03 09:33:39 -04:00
|
|
|
}
|