|
| 1 | +<?php |
| 2 | + |
| 3 | +// Core file for the console library for Blueprint Extensions |
| 4 | + |
| 5 | + |
| 6 | +namespace Pterodactyl\BlueprintFramework\Libraries\ExtensionLibrary\Console; |
| 7 | + |
| 8 | +use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; |
| 9 | + |
| 10 | +class BlueprintConsoleLibrary |
| 11 | +{ |
| 12 | + // Construct core |
| 13 | + public function __construct( |
| 14 | + private SettingsRepositoryInterface $settings, |
| 15 | + ) { |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Fetch a record from the database. |
| 20 | + * |
| 21 | + * @param string $table Database table |
| 22 | + * @param string $record Database record |
| 23 | + * @return mixed Database value |
| 24 | + * |
| 25 | + * [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint) |
| 26 | + */ |
| 27 | + public function dbGet($table, $record): mixed { |
| 28 | + return $this->settings->get($table."::".$record); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Set a database record. |
| 33 | + * |
| 34 | + * @param string $table Database table |
| 35 | + * @param string $record Database record |
| 36 | + * @param string $value Value to store |
| 37 | + * |
| 38 | + * [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint) |
| 39 | + */ |
| 40 | + public function dbSet($table, $record, $value) { |
| 41 | + return $this->settings->set($table."::".$record, $value); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Delete/forget a database record. |
| 46 | + * |
| 47 | + * @param string $table Database table |
| 48 | + * @param string $record Database record |
| 49 | + * |
| 50 | + * [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint) |
| 51 | + */ |
| 52 | + public function dbForget($table, $record) { |
| 53 | + return $this->settings->forget($table."::".$record); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Read and returns the content of a given file. |
| 58 | + * |
| 59 | + * @param string $path Path to file |
| 60 | + * @return string File contents |
| 61 | + * @throws string Errors encountered by `cat` shell utility |
| 62 | + * |
| 63 | + * [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint) |
| 64 | + */ |
| 65 | + public function fileRead($path) { |
| 66 | + return shell_exec("cat ".escapeshellarg($path).";"); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Attempts to (non-recursively) create a file. |
| 71 | + * |
| 72 | + * @param string $path File name/path |
| 73 | + * @return string Empty string unless error |
| 74 | + * @throws string Errors encountered by `touch` shell utility |
| 75 | + * |
| 76 | + * [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint) |
| 77 | + */ |
| 78 | + public function fileMake($path) { |
| 79 | + $file = fopen($path, "w"); |
| 80 | + fclose($file); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Attempts to (recursively) remove a file/folder. |
| 85 | + * It's good practice to terminate this function after a certain timeout, to prevent infinite page loads upon input. Blueprint attempts to use `yes` as an input for all questions, but might not succeed. |
| 86 | + * |
| 87 | + * @param string $path Path to file/folder |
| 88 | + * @return string Empty string unless error |
| 89 | + * @throws string Errors encountered by `rm` shell utility |
| 90 | + * |
| 91 | + * [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint) |
| 92 | + */ |
| 93 | + public function fileWipe($path) { |
| 94 | + return shell_exec("yes | rm -r ".escapeshellarg($path).";"); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Check if an extension is installed based on it's identifier. |
| 99 | + * |
| 100 | + * @param string $identifier Extension identifier |
| 101 | + * @return bool Boolean |
| 102 | + * |
| 103 | + * [BlueprintExtensionLibrary documentation](https://blueprint.zip/docs/?page=documentation/$blueprint) |
| 104 | + */ |
| 105 | + public function extension($identifier): bool { |
| 106 | + if(str_contains($this->fileRead(base_path(".blueprint/extensions/blueprint/private/db/installed_extensions")), $identifier.',')) { |
| 107 | + return true; |
| 108 | + } else { |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments