├── phpunit.xml ├── .github ├── dependabot.yml └── workflows │ └── code_checks.yaml ├── composer.json ├── .gitignore ├── tests ├── MinecraftJSONColorsTest.php ├── MinecraftVotifierVoteTest.php └── MinecraftColorsTest.php ├── README.md ├── src ├── MinecraftJSONColors.php ├── MinecraftVotifierVote.php ├── MinecraftVotifier.php └── MinecraftColors.php └── LICENSE /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./tests 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "composer" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | - package-ecosystem: "github-actions" 8 | directory: "/" 9 | schedule: 10 | interval: "weekly" 11 | -------------------------------------------------------------------------------- /.github/workflows/code_checks.yaml: -------------------------------------------------------------------------------- 1 | name: Code_Checks 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | tests: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | matrix: 10 | php: ['8.1', '8.2', '8.3', '8.4', '8.5'] 11 | 12 | name: PHP ${{ matrix.php }} tests 13 | steps: 14 | - uses: actions/checkout@v6 15 | 16 | - uses: shivammathur/setup-php@v2 17 | with: 18 | php-version: ${{ matrix.php }} 19 | coverage: none 20 | 21 | - run: composer install --no-progress 22 | 23 | - run: vendor/bin/phpunit 24 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spirit55555/php-minecraft", 3 | "keywords": [ 4 | "minecraft", 5 | "votifier", 6 | "chat" 7 | ], 8 | "description": "Useful PHP classes for Minecraft", 9 | "type": "library", 10 | "homepage": "https://github.com/Spirit55555/PHP-Minecraft/", 11 | "support": { 12 | "issues": "https://github.com/Spirit55555/PHP-Minecraft/issues", 13 | "source": "https://github.com/Spirit55555/PHP-Minecraft" 14 | }, 15 | "require": { 16 | "php": ">=8.1", 17 | "ext-mbstring": "*", 18 | "ext-openssl": "*" 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit": "^10" 22 | }, 23 | "license": "GPL-3.0-or-later", 24 | "autoload": { 25 | "psr-4": { 26 | "Spirit55555\\Minecraft\\": "src" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/phpunit,composer,visualstudiocode 2 | # Edit at https://www.gitignore.io/?templates=phpunit,composer,visualstudiocode 3 | 4 | ### Composer ### 5 | composer.phar 6 | /vendor/ 7 | 8 | # Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control 9 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file 10 | composer.lock 11 | 12 | ### PHPUnit ### 13 | # Covers PHPUnit 14 | # Reference: https://phpunit.de/ 15 | 16 | # Generated files 17 | .phpunit.result.cache 18 | 19 | # PHPUnit 20 | /app/phpunit.xml 21 | /phpunit.xml 22 | 23 | # Build data 24 | /build/ 25 | 26 | ### VisualStudioCode ### 27 | .vscode/* 28 | !.vscode/settings.json 29 | !.vscode/tasks.json 30 | !.vscode/launch.json 31 | !.vscode/extensions.json 32 | 33 | ### VisualStudioCode Patch ### 34 | # Ignore all local history of files 35 | .history 36 | 37 | # End of https://www.gitignore.io/api/phpunit,composer,visualstudiocode 38 | -------------------------------------------------------------------------------- /tests/MinecraftJSONColorsTest.php: -------------------------------------------------------------------------------- 1 | . 17 | */ 18 | 19 | declare(strict_types=1); 20 | 21 | use PHPUnit\Framework\TestCase; 22 | use \Spirit55555\Minecraft\MinecraftJSONColors; 23 | 24 | final class MinecraftJSONColorsTest extends TestCase { 25 | public function testConvertToLegacy(): void { 26 | $json = []; 27 | 28 | $components[] = ['text' => 'second ', 'color' => 'red']; 29 | $components[] = ['text' => 'third ', 'strikethrough' => true]; 30 | $components[] = ['text' => 'forth ', 'color' => '#AA0000']; 31 | 32 | $json['text'] = 'first '; 33 | $json['extra'] = $components; 34 | 35 | $this->assertSame('first §r§csecond §r§mthird §rforth §r', MinecraftJSONColors::convertToLegacy($json)); 36 | $this->assertSame('first &r&csecond &r&mthird &rforth &r', MinecraftJSONColors::convertToLegacy($json, '&')); 37 | $this->assertSame('first §r§csecond §r§mthird §r§#AA0000forth §r', MinecraftJSONColors::convertToLegacy($json, '§', true)); 38 | } 39 | 40 | public function testConvertToLegacyInherit(): void { 41 | $json = []; 42 | 43 | $forth = ['text' => 'forth ', 'color' => '#AA0000']; 44 | $third = ['text' => 'third ', 'strikethrough' => true, 'extra' => [$forth, 'fifth ']]; 45 | $second = ['text' => 'second ', 'color' => 'red', 'extra' => [$third]]; 46 | 47 | 48 | $json['text'] = 'first '; 49 | $json['extra'] = [$second]; 50 | 51 | $this->assertSame('first §r§csecond §r§c§mthird §r§mforth §r§c§mfifth §r', MinecraftJSONColors::convertToLegacy($json)); 52 | $this->assertSame('first &r&csecond &r&c&mthird &r&mforth &r&c&mfifth &r', MinecraftJSONColors::convertToLegacy($json, '&')); 53 | $this->assertSame('first §r§csecond §r§c§mthird §r§#AA0000§mforth §r§c§mfifth §r', MinecraftJSONColors::convertToLegacy($json, '§', true)); 54 | } 55 | } 56 | ?> 57 | -------------------------------------------------------------------------------- /tests/MinecraftVotifierVoteTest.php: -------------------------------------------------------------------------------- 1 | . 17 | */ 18 | 19 | declare(strict_types=1); 20 | 21 | use PHPUnit\Framework\TestCase; 22 | use Spirit55555\Minecraft\MinecraftVotifierVote; 23 | use Spirit55555\Minecraft\MinecraftVotifierVoteException; 24 | 25 | final class MinecraftVotifierVoteTest extends TestCase { 26 | public function testValidVote(): void { 27 | $vote = new MinecraftVotifierVote('Testing', '127.0.0.1', 'Spirit55555', 'f6792ad3-cbb4-4596-8296-749ee4158f97'); 28 | 29 | $this->assertTrue($vote->isValid()); 30 | } 31 | 32 | public function testValidVoteWithIPv6Address(): void { 33 | $vote = new MinecraftVotifierVote('Testing', '2001:db8:3333:4444:5555:6666:7777:8888', 'Spirit55555'); 34 | 35 | $this->assertTrue($vote->isValid()); 36 | } 37 | 38 | public function testInvalidIPv4(): void { 39 | $this->expectException(MinecraftVotifierVoteException::class); 40 | 41 | new MinecraftVotifierVote('Testing', '127.0.0', 'Spirit55555'); 42 | } 43 | 44 | public function testInvalidIPv6(): void { 45 | $this->expectException(MinecraftVotifierVoteException::class); 46 | 47 | new MinecraftVotifierVote('Testing', '2001:db8:3333:4444:5555:6666:7777', 'Spirit55555'); 48 | } 49 | 50 | public function testInvalidUsername(): void { 51 | $this->expectException(MinecraftVotifierVoteException::class); 52 | 53 | new MinecraftVotifierVote('Testing', '127.0.0.1', 'Spirit@55555'); 54 | } 55 | 56 | public function testInvalidUUID(): void { 57 | $this->expectException(MinecraftVotifierVoteException::class); 58 | 59 | new MinecraftVotifierVote('Testing', '127.0.0.1', 'Spirit@55555', 'f6792ad3-cbb4-4596-8296'); 60 | } 61 | 62 | public function testInvalidTimestamp(): void { 63 | $this->expectException(MinecraftVotifierVoteException::class); 64 | 65 | new MinecraftVotifierVote('Testing', '127.0.0.1', 'Spirit55555', 'f6792ad3-cbb4-4596-8296-749ee4158f97', -1); 66 | } 67 | } 68 | ?> 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/spirit55555/php-minecraft/v)](https://packagist.org/packages/spirit55555/php-minecraft) [![Total Downloads](https://poser.pugx.org/spirit55555/php-minecraft/downloads)](https://packagist.org/packages/spirit55555/php-minecraft) [![License](https://poser.pugx.org/spirit55555/php-minecraft/license)](https://packagist.org/packages/spirit55555/php-minecraft) [![PHP Version Require](https://poser.pugx.org/spirit55555/php-minecraft/require/php)](https://packagist.org/packages/spirit55555/php-minecraft) 2 | 3 | # PHP-Minecraft 4 | ## Useful PHP classes for Minecraft 5 | 6 | ### Using Composer? 7 | 8 | First require it like this: 9 | ``` 10 | composer require spirit55555/php-minecraft 11 | ``` 12 | 13 | and then use it like this: 14 | ```php 15 | 21 | ``` 22 | 23 | ### Not using Composer? 24 | 25 | Just download the files and include them. 26 | 27 | ## MinecraftColors.php 28 | 29 | Convert [Minecraft color codes](https://minecraft.wiki/w/Formatting_codes) to HTML/CSS. Can also remove the color codes. 30 | 31 | ### Usage 32 | 33 | ```php 34 | 49 | echo MinecraftColors::convertToHTML($text, true); 50 | 51 | //Same as above, but will use CSS classes instead of inline styles 52 | echo MinecraftColors::convertToHTML($text, true, true, 'mc-motd--'); 53 | 54 | //Will be compatible with the server.properties file 55 | echo MinecraftColors::convertToMOTD($text); 56 | 57 | //Will be compatible with BungeeCord's config.yml file 58 | echo MinecraftColors::convertToMOTD($text, '&'); 59 | 60 | //Will also output RGB/HEX colors, if they exist (�) 61 | //NOTE: Not supported in Vanilla Minecraft 62 | echo MinecraftColors::convertToMOTD($text, '&', true); 63 | 64 | //Same as above, but RGB/HEX in a long format (&x&0&0&0&0&0&0) 65 | //NOTE: Not supported in Vanilla Minecraft 66 | echo MinecraftColors::convertToMOTD($text, '&', true, true); 67 | 68 | //Remove all color codes 69 | echo MinecraftColors::clean($text); 70 | 71 | ################### 72 | # BEDROCK EDITION # 73 | ################### 74 | 75 | //Convert to HTML with CSS colors 76 | echo MinecraftColors::convertToBedrockHTML($text); 77 | 78 | //Same as above, but will use CSS classes instead of inline styles 79 | echo MinecraftColors::convertToBedrockHTML($text, true, 'mc-motd--'); 80 | 81 | //Will be compatible with the server.properties file 82 | echo MinecraftColors::convertToBedrockMOTD($text); 83 | 84 | //Remove all color codes 85 | echo MinecraftColors::cleanBedrock($text); 86 | 87 | ?> 88 | ``` 89 | 90 | ## MinecraftJSONColors.php 91 | 92 | Converts [Minecraft JSON](https://minecraft.wiki/w/Text_component_format) text to legacy format ('§aHello') 93 | 94 | ### Usage 95 | 96 | ```php 97 | 'first ']; 102 | $second_component = ['text' => 'second ', 'color' => 'red']; 103 | $third_component = ['text' => 'third ', 'strikethrough' => true]; 104 | $json = ['extra' => [$first_component, $second_component, $third_component]]; 105 | 106 | echo MinecraftJSONColors::convertToLegacy($json); 107 | ?> 108 | ``` 109 | 110 | ## MinecraftVotifier.php 111 | 112 | Send Votifier votes to a Minecraft server. 113 | 114 | This supports v2 (token) and v1 (public key) versions of the protocol. 115 | 116 | If both are supplied, it will try v2 first and fall back to v1. 117 | 118 | ### Usage 119 | 120 | ```php 121 | sendVote($vote); 131 | } 132 | 133 | catch (Exception $e) { 134 | echo $e->getMessage(); 135 | } 136 | ?> 137 | ``` 138 | 139 | More information about the Votifier protocols: https://github.com/NuVotifier/NuVotifier/wiki/Technical-QA 140 | -------------------------------------------------------------------------------- /src/MinecraftJSONColors.php: -------------------------------------------------------------------------------- 1 | . 17 | */ 18 | 19 | declare(strict_types=1); 20 | 21 | namespace Spirit55555\Minecraft; 22 | 23 | /** 24 | * Convert Minecraft JSON text to legacy format. 25 | * 26 | * Based on https://minecraft.wiki/w/Text_component_format 27 | */ 28 | class MinecraftJSONColors { 29 | static private $color_char; 30 | static private $hex_colors; 31 | 32 | /** 33 | * Color names types mapped to legacy codes. 34 | * 35 | * @var array 36 | */ 37 | static private $colors = [ 38 | 'black' => '0', 39 | 'dark_blue' => '1', 40 | 'dark_green' => '2', 41 | 'dark_aqua' => '3', 42 | 'dark_red' => '4', 43 | 'dark_purple' => '5', 44 | 'gold' => '6', 45 | 'gray' => '7', 46 | 'dark_gray' => '8', 47 | 'blue' => '9', 48 | 'green' => 'a', 49 | 'aqua' => 'b', 50 | 'red' => 'c', 51 | 'light_purple' => 'd', 52 | 'yellow' => 'e', 53 | 'white' => 'f' 54 | ]; 55 | 56 | /** 57 | * Formatting names mapped to legacy codes. 58 | * 59 | * @var array 60 | */ 61 | static private $formatting = [ 62 | 'obfuscated' => 'k', 63 | 'bold' => 'l', 64 | 'strikethrough' => 'm', 65 | 'underline' => 'n', 66 | 'italic' => 'o', 67 | 'reset' => 'r' 68 | ]; 69 | 70 | /** 71 | * Convert Minecraft JSON text to legacy format. 72 | * 73 | * @param string|array $json JSON as a string or an array. 74 | * @param string $color_char The text to prepend all color codes. 75 | * @param bool $hex_colors Should HEX colors be converted as well? If not, they will be skipped. 76 | * @return string 77 | */ 78 | public static function convertToLegacy($json, string $color_char = '§', bool $hex_colors = false): string { 79 | self::$color_char = $color_char; 80 | self::$hex_colors = $hex_colors; 81 | 82 | if (!empty($json) && is_string($json)) { 83 | $json = json_decode($json, true); 84 | 85 | //Just return an empty string, if JSON was invalid. 86 | if (json_last_error() != JSON_ERROR_NONE) 87 | return ''; 88 | } 89 | 90 | $legacy = ''; 91 | 92 | if (is_array($json)) 93 | $legacy .= self::parseElement($json); 94 | 95 | //If nothing was parsed until here, it's an array of components. 96 | if (empty($legacy) && is_array($json)) { 97 | foreach ($json as $item) 98 | $legacy .= self::convertToLegacy($item, self::$color_char, self::$hex_colors); 99 | } 100 | 101 | return $legacy; 102 | } 103 | 104 | /** 105 | * Parse an array to a legacy string with color codes. 106 | * 107 | * @param array $json 108 | * @return string 109 | */ 110 | private static function parseElement(array $json, string $legacy = '', array $parent = []): string { 111 | if (!empty($parent)) { 112 | //Remove the parts we don't want to inherit 113 | unset($parent['text'], $parent['extra']); 114 | 115 | $json = array_merge($parent, $json); 116 | } 117 | 118 | if (isset($json['color'])) { 119 | //Minecraft 1.16+ added support for RGB/HEX colors. Only add it when enabled. 120 | if (preg_match('/^#[0-9a-f]{6}$/i', $json['color'])) { 121 | if (self::$hex_colors) 122 | $legacy .= self::$color_char.$json['color']; 123 | } 124 | 125 | else if (isset(self::$colors[$json['color']])) 126 | $legacy .= self::$color_char.self::$colors[$json['color']]; 127 | } 128 | 129 | foreach (self::$formatting as $name => $code) { 130 | if (isset($json[$name]) && $json[$name]) 131 | $legacy .= self::$color_char.$code; 132 | } 133 | 134 | if (isset($json['text'])) 135 | $legacy .= $json['text']; 136 | 137 | //Add reset after we have done the formatting 138 | $legacy .= self::$color_char.self::$formatting['reset']; 139 | 140 | if (isset($json['extra'])) { 141 | foreach ($json['extra'] as $component) { 142 | //Convert string components to array so they can inherit 143 | if (is_string($component)) 144 | $component = ['text' => $component]; 145 | 146 | $legacy = self::parseElement($component, $legacy, $json); 147 | } 148 | } 149 | 150 | return $legacy; 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /src/MinecraftVotifierVote.php: -------------------------------------------------------------------------------- 1 | . 17 | */ 18 | 19 | declare(strict_types=1); 20 | 21 | namespace Spirit55555\Minecraft; 22 | 23 | /** 24 | * MinecraftVotifierVote 25 | * 26 | * Helper class to format votes used by MinecraftVotifier->sendVote() 27 | * @package Spirit55555\Minecraft 28 | */ 29 | class MinecraftVotifierVote { 30 | const USERNAME_FORMAT = '/^[a-zA-Z0-9_]{3,16}$/'; 31 | const UUID_FORMAT_FULL = '/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i'; 32 | const UUID_FORMAT_SHORT = '/^[0-9A-F]{8}[0-9A-F]{4}[4][0-9A-F]{3}[89AB][0-9A-F]{3}[0-9A-F]{12}$/i'; 33 | 34 | private $service_name; 35 | private $address; 36 | private $username; 37 | private $uuid; 38 | private $timestamp; 39 | 40 | /** 41 | * Create a new vote 42 | * @param string $service_name Service name 43 | * @param string $address IP address 44 | * @param string $username Minecraft username 45 | * @param null|string $uuid Minecraft UUID 46 | * @param null|float $timestamp Timestamp in milliseconds 47 | * @throws MinecraftVotifierVoteException 48 | */ 49 | public function __construct(string $service_name, string $address, string $username, ?string $uuid = '', ?float $timestamp = 0) { 50 | $this->service_name = $service_name; 51 | $this->address = $this->validateAddress($address); 52 | $this->username = $this->validateUsername($username); 53 | $this->uuid = $this->validateUUID($uuid); 54 | $this->timestamp = $this->validateTimestamp($timestamp); 55 | } 56 | 57 | public function __get(string $name) { 58 | return isset($this->$name) ? $this->$name : null; 59 | } 60 | 61 | public function __set(string $name, mixed $value) { 62 | switch ($name) { 63 | case 'address': 64 | $this->address = $this->validateAddress($value); 65 | break; 66 | case 'username': 67 | $this->username = $this->validateUsername($value); 68 | break; 69 | case 'uuid': 70 | $this->uuid = $this->validateUUID($value); 71 | break; 72 | case 'timestamp': 73 | $this->timestamp = $this->validateTimestamp($value); 74 | break; 75 | default: 76 | $this->$name = $value; 77 | } 78 | } 79 | 80 | /** 81 | * Checks if a vote is considered valid and ready to be sent 82 | * @return bool 83 | */ 84 | public function isValid(): bool { 85 | //UUID is optional and timestamp will always be set, so don't check for those 86 | if (!empty($this->service_name) && !empty($this->address) && !empty($this->username)) 87 | return true; 88 | 89 | return false; 90 | } 91 | 92 | /** 93 | * Validate the IP address 94 | * @param string $address IP address to validate 95 | * @return string Valid IP address 96 | * @throws MinecraftVotifierVoteException 97 | */ 98 | private function validateAddress(string $address): string { 99 | if (filter_var($address, FILTER_VALIDATE_IP) === false) 100 | throw new MinecraftVotifierVoteException('Address is not a valid IP address'); 101 | 102 | return $address; 103 | } 104 | 105 | /** 106 | * Validate the username 107 | * @param string $username Username to validate 108 | * @return string Valid username 109 | * @throws MinecraftVotifierVoteException 110 | */ 111 | private function validateUsername(string $username): string { 112 | if (!preg_match(self::USERNAME_FORMAT, $username)) 113 | throw new MinecraftVotifierVoteException('Username is not valid'); 114 | 115 | return $username; 116 | } 117 | 118 | /** 119 | * Validate a UUID 120 | * 121 | * Short UUIDs (without dashes) will be formatted to the correct format. 122 | * @param string $uuid UUID to validate 123 | * @return string Valid UUID 124 | * @throws MinecraftVotifierVoteException 125 | */ 126 | private function validateUUID(string $uuid): string { 127 | //Optional, so allow to be empty. 128 | if (empty($uuid)) 129 | return $uuid; 130 | 131 | //Convert short UUID to full version 132 | if (preg_match(self::UUID_FORMAT_SHORT, $uuid)) { 133 | $parts[] = substr($uuid, 0, 8); 134 | $parts[] = '-'.substr($uuid, 8, 4); 135 | $parts[] = '-'.substr($uuid, 12, 4); 136 | $parts[] = '-'.substr($uuid, 16, 4); 137 | $parts[] = '-'.substr($uuid, 20); 138 | 139 | $uuid = implode($parts); 140 | } 141 | 142 | if (!preg_match(self::UUID_FORMAT_FULL, $uuid)) 143 | throw new MinecraftVotifierVoteException('UUID is not valid'); 144 | 145 | return $uuid; 146 | } 147 | 148 | /** 149 | * Validate a timestamp 150 | * 151 | * If set to 0 (the default), it will be set to the current time (in milliseconds) 152 | * @param float $timestamp Timestamp to validate 153 | * @return float Valid UUID 154 | * @throws MinecraftVotifierVoteException 155 | */ 156 | private function validateTimestamp(float $timestamp): float { 157 | if (!is_float($timestamp) || $timestamp < 0) 158 | throw new MinecraftVotifierVoteException('Timestamp is not valid'); 159 | 160 | if ($timestamp == 0) 161 | return round(microtime(true) * 1000); 162 | 163 | return $timestamp; 164 | } 165 | } 166 | 167 | class MinecraftVotifierVoteException extends \Exception {} 168 | ?> 169 | -------------------------------------------------------------------------------- /src/MinecraftVotifier.php: -------------------------------------------------------------------------------- 1 | . 17 | */ 18 | 19 | declare(strict_types=1); 20 | 21 | namespace Spirit55555\Minecraft; 22 | 23 | /** 24 | * MinecraftVotifier 25 | * 26 | * Class for sending Votifier votes. 27 | * Supports token (v2) and public key (v1) protocols. 28 | * 29 | * More information about the Votifier protocols: 30 | * https://github.com/NuVotifier/NuVotifier/wiki/Technical-QA 31 | * @package Spirit55555\Minecraft 32 | */ 33 | class MinecraftVotifier { 34 | const LEGACY_VOTE_FORMAT = "VOTE\n%s\n%s\n%s\n%d\n"; 35 | const PUBLIC_KEY_FORMAT = "-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----"; 36 | 37 | private $stream; 38 | private $challenge; 39 | 40 | private $host; 41 | private $port; 42 | private $token; 43 | private $public_key; 44 | 45 | /** 46 | * Create a new Votifier instance 47 | * @param string $host IP address or hostname 48 | * @param int $port Port 49 | * @param null|string $token Token 50 | * @param null|string $public_key Public key 51 | * @return void 52 | * @throws MinecraftVotifierException 53 | */ 54 | public function __construct(string $host, int $port = 8192, ?string $token = '', ?string $public_key = '') { 55 | $this->host = $host; 56 | $this->port = $port; 57 | $this->token = $token; 58 | $this->public_key = $this->formatPublicKey($public_key); 59 | } 60 | 61 | public function __destruct() { 62 | //Close the stream, if it's open 63 | if (is_resource($this->stream)) 64 | fclose($this->stream); 65 | } 66 | 67 | public function __get(string $name) { 68 | return isset($this->$name) ? $this->$name : null; 69 | } 70 | 71 | public function __set(string $name, mixed $value): void { 72 | if ($name == 'public_key') 73 | $this->public_key = $this->formatPublicKey($value); 74 | else 75 | $this->$name = $value; 76 | } 77 | 78 | /** 79 | * Parse a header from a server 80 | * 81 | * This header is only returned by servers that support v2 of the protocol. 82 | * @param string $header 83 | * @return void 84 | */ 85 | private function parseHeader(string $header): void { 86 | $parts = explode(' ', trim($header)); 87 | 88 | if (count($parts) != 3) 89 | return; 90 | 91 | if ($parts[0] != 'VOTIFIER') 92 | return; 93 | 94 | if ($parts[1] !== '2') 95 | return; 96 | 97 | $this->challenge = $parts[2]; 98 | } 99 | 100 | /** 101 | * Format a public key, so it can be used with OpenSSL 102 | * 103 | * This will also check if it is a valid public key. 104 | * @param string $public_key 105 | * @return string 106 | * @throws MinecraftVotifierException 107 | */ 108 | private function formatPublicKey(string $public_key): string { 109 | if (empty($public_key)) 110 | return ''; 111 | 112 | $public_key = wordwrap($public_key, 65, "\n", true); 113 | $public_key = sprintf(self::PUBLIC_KEY_FORMAT, $public_key); 114 | 115 | if (!openssl_pkey_get_public($public_key)) 116 | throw new MinecraftVotifierException('Public key is not valid'); 117 | 118 | return $public_key; 119 | } 120 | 121 | /** 122 | * Attempt to send a token based (v2) vote 123 | * 124 | * The v2 protocol returns an answer from the server and we only consider it a success, if we get an ok and no errors back. 125 | * @param MinecraftVotifierVote $vote 126 | * @return bool If the vote was successfully sent or not 127 | * @throws MinecraftVotifierException 128 | */ 129 | private function sendTokenVote(MinecraftVotifierVote $vote): bool { 130 | if (empty($this->token) || empty($this->challenge)) 131 | return false; 132 | 133 | $payload_data = [ 134 | 'challenge' => $this->challenge, 135 | 'serviceName' => $vote->service_name, 136 | 'address' => $vote->address, 137 | 'username' => $vote->username, 138 | 'timestamp' => $vote->timestamp 139 | ]; 140 | 141 | if (!empty($vote->uuid)) 142 | $payload_data['uuid'] = $vote->uuid; 143 | 144 | $payload_json = json_encode($payload_data); 145 | $signature = base64_encode(hash_hmac('sha256', $payload_json, $this->token, true)); 146 | $message_json = json_encode([ 147 | 'signature' => $signature, 148 | 'payload' => $payload_json 149 | ]); 150 | 151 | $payload = pack('nn', 0x733a, strlen($message_json)).$message_json; 152 | 153 | if (fwrite($this->stream, $payload) === false) 154 | throw new MinecraftVotifierException('Could not write to remote host'); 155 | 156 | $response = fread($this->stream, 256); 157 | 158 | if (!$response) 159 | throw new MinecraftVotifierException('Could not read server response'); 160 | 161 | $result = json_decode($response); 162 | 163 | if ($result->status != 'ok') 164 | throw new MinecraftVotifierException('Votifier server error: '.$result->cause.': '.$result->error); 165 | 166 | return true; 167 | } 168 | 169 | /** 170 | * Attempt to send a public key (v1) based vote 171 | * 172 | * The v1 protocol does not allow us to check if the vote was received or not, so if we can send it, we consider that a success. 173 | * @param MinecraftVotifierVote $vote 174 | * @return bool If the vote was successfully sent or not 175 | */ 176 | private function sendPublickeyVote(MinecraftVotifierVote $vote): bool { 177 | if (empty($this->public_key)) 178 | return false; 179 | 180 | $legacy_vote = sprintf(self::LEGACY_VOTE_FORMAT, $this->service_name, $vote->username, $vote->address, round($vote->timestamp / 1000)); 181 | 182 | if (@openssl_public_encrypt($legacy_vote, $encrypted_data, $this->public_key)) { 183 | if ($this->stream) { 184 | if (fwrite($this->stream, $encrypted_data)) 185 | return true; 186 | } 187 | } 188 | 189 | return false; 190 | } 191 | 192 | /** 193 | * Send a vote 194 | * 195 | * If token based (v2) vote fails, it will attempt public key (v1) based vote, if there is a public key defined. 196 | * 197 | * If no token is defined, it will only attempt public key based vote. 198 | * @param MinecraftVotifierVote $vote 199 | * @return bool If the vote was successfully sent or not 200 | * @throws MinecraftVotifierException 201 | */ 202 | public function sendVote(MinecraftVotifierVote $vote): bool { 203 | if (!$vote->isValid()) 204 | throw new MinecraftVotifierException('Vote is not valid'); 205 | 206 | $this->stream = @stream_socket_client('tcp://' . $this->host.':'.$this->port, $errno, $errstr, 3); 207 | 208 | if (!$this->stream) 209 | throw new MinecraftVotifierException('Could not connect: '.$errstr); 210 | 211 | $header = fread($this->stream, 64); 212 | $this->parseHeader($header); 213 | 214 | if ($this->sendTokenVote($vote)) 215 | return true; 216 | 217 | else { 218 | if ($this->sendPublickeyVote($vote)) 219 | return true; 220 | } 221 | 222 | return false; 223 | } 224 | } 225 | 226 | class MinecraftVotifierException extends \Exception {} 227 | ?> 228 | -------------------------------------------------------------------------------- /tests/MinecraftColorsTest.php: -------------------------------------------------------------------------------- 1 | . 17 | */ 18 | 19 | declare(strict_types=1); 20 | 21 | use PHPUnit\Framework\TestCase; 22 | use \Spirit55555\Minecraft\MinecraftColors; 23 | 24 | final class MinecraftColorsTest extends TestCase { 25 | public function testClean(): void { 26 | $text = '§4Lorem §3§lipsum §rdolor &nsit &c&mamet'; 27 | $text_hex = '§#AA0000Lorem §3§lipsum §rdolor &nsit &#FF5555&kamet'; 28 | 29 | $this->assertSame('Lorem ipsum dolor sit amet', MinecraftColors::clean($text)); 30 | $this->assertSame('Lorem ipsum dolor sit amet', MinecraftColors::clean($text_hex)); 31 | } 32 | 33 | public function testCleanBedrock(): void { 34 | $text = '§mLorem §v§lipsum §rdolor &nsit &c&mamet'; 35 | 36 | $this->assertSame('Lorem ipsum dolor sit amet', MinecraftColors::cleanBedrock($text)); 37 | } 38 | 39 | public function testConvertToHTML(): void { 40 | $text = '§4Lorem §3§lipsum'."\n".'§rdolor &nsit &c&kamet'; 41 | $text_hex = '§#AA0000Lorem §3§lipsum'."\n".'§rdolor &nsit &#FF5555&kamet'; 42 | 43 | $this->assertSame('Lorem ipsum'."\n".'dolor sit amet', MinecraftColors::convertToHTML($text)); 44 | $this->assertSame('Lorem ipsum
dolor sit amet', MinecraftColors::convertToHTML($text, true)); 45 | $this->assertSame('Lorem ipsum
dolor sit amet', MinecraftColors::convertToHTML($text, true, true)); 46 | $this->assertSame('Lorem ipsum
dolor sit amet', MinecraftColors::convertToHTML($text, true, true, 'mc-motd--')); 47 | 48 | $this->assertSame('Lorem ipsum'."\n".'dolor sit amet', MinecraftColors::convertToHTML($text_hex)); 49 | $this->assertSame('Lorem ipsum
dolor sit amet', MinecraftColors::convertToHTML($text_hex, true)); 50 | $this->assertSame('Lorem ipsum
dolor sit amet', MinecraftColors::convertToHTML($text_hex, true, true)); 51 | $this->assertSame('Lorem ipsum
dolor sit amet', MinecraftColors::convertToHTML($text_hex, true, true, 'mc-motd--')); 52 | } 53 | 54 | public function testConvertToBedrockHTML(): void { 55 | $text = '§4Lorem §l§3ipsum §rdolor &nsit &c&kamet'; 56 | 57 | $this->assertSame('Lorem ipsum dolor sit amet', MinecraftColors::convertToBedrockHTML($text)); 58 | $this->assertSame('Lorem ipsum dolor sit amet', MinecraftColors::convertToBedrockHTML($text, true)); 59 | $this->assertSame('Lorem ipsum dolor sit amet', MinecraftColors::convertToBedrockHTML($text, true, 'mc-motd--')); 60 | } 61 | 62 | public function testConvertToHTMLEmptyTags(): void { 63 | $text = '§7 §4Lorem §3§lipsum§7 '; 64 | $this->assertSame(' Lorem ipsum ', MinecraftColors::convertToHTML($text)); 65 | } 66 | 67 | public function testConvertToMOTD(): void { 68 | $text = '§4Lorem §3§lipsum §Rdolor &nsit &C&mamet'; 69 | $text_hex = '§#aa0000Lorem §3§lipsum §Rdolor &nsit &#FF5555&mamet'; 70 | 71 | $this->assertSame('\u00A74Lorem \u00A73\u00A7lipsum \u00A7rdolor \u00A7nsit \u00A7c\u00A7mamet', MinecraftColors::convertToMOTD($text)); 72 | $this->assertSame('&4Lorem &3&lipsum &rdolor &nsit &c&mamet', MinecraftColors::convertToMOTD($text, '&')); 73 | 74 | $this->assertSame('Lorem \u00A73\u00A7lipsum \u00A7rdolor \u00A7nsit \u00A7mamet', MinecraftColors::convertToMOTD($text_hex, '\u00A7')); 75 | $this->assertSame('&#AA0000Lorem &3&lipsum &rdolor &nsit &#FF5555&mamet', MinecraftColors::convertToMOTD($text_hex, '&', true)); 76 | $this->assertSame('&x&A&A&0&0&0&0Lorem &3&lipsum &rdolor &nsit &x&F&F&5&5&5&5&mamet', MinecraftColors::convertToMOTD($text_hex, '&', true, true)); 77 | } 78 | 79 | public function testConvertToBedrockMOTD(): void { 80 | $text = '§4Lorem §3§lipsum §Rdolor &nsit &C&mamet'; 81 | 82 | $this->assertSame('\u00A74Lorem \u00A73\u00A7lipsum \u00A7rdolor \u00A7nsit \u00A7c\u00A7mamet', MinecraftColors::convertToBedrockMOTD($text)); 83 | $this->assertSame('&4Lorem &3&lipsum &rdolor &nsit &c&mamet', MinecraftColors::convertToBedrockMOTD($text, '&')); 84 | } 85 | 86 | public function testLongHEXFormat(): void { 87 | $text = '§x§a§A§0§0§0§0Lorem &x&f&F&5&5&5&5ipsum'; 88 | 89 | $this->assertSame('Lorem ipsum', MinecraftColors::clean($text)); 90 | $this->assertSame('Lorem ipsum', MinecraftColors::convertToMOTD($text, '&')); 91 | $this->assertSame('&#AA0000Lorem &#FF5555ipsum', MinecraftColors::convertToMOTD($text, '&', true)); 92 | $this->assertSame('Lorem ipsum', MinecraftColors::convertToHTML($text)); 93 | } 94 | } 95 | ?> 96 | -------------------------------------------------------------------------------- /src/MinecraftColors.php: -------------------------------------------------------------------------------- 1 | . 17 | */ 18 | 19 | declare(strict_types=1); 20 | 21 | namespace Spirit55555\Minecraft; 22 | 23 | /** 24 | * Convert Minecraft color codes to HTML/CSS. Can also remove the color codes. 25 | * 26 | * More info: https://minecraft.wiki/w/Formatting_codes 27 | */ 28 | class MinecraftColors { 29 | const REGEX_JAVA = '/(?:§|&)([0-9a-fklmnor])/i'; 30 | const REGEX_BEDROCK = '/(?:§|&)([0-9a-v])/i'; 31 | const REGEX_HEX_SHORT = '/(?:§|&)(#[0-9a-f]{6})/i'; 32 | const REGEX_HEX_LONG = '/(?:§|&)x(?:§|&)([0-9a-f])(?:§|&)([0-9a-f])(?:§|&)([0-9a-f])(?:§|&)([0-9a-f])(?:§|&)([0-9a-f])(?:§|&)([0-9a-f])/i'; 33 | const REGEX_JAVA_ALL = '/(?:§|&)([0-9a-fklmnor]|#[0-9a-f]{6})/i'; 34 | 35 | const START_TAG_WITH_STYLE = ''; 36 | const START_TAG_WITH_CLASS = ''; 37 | const START_TAG = ''; 38 | const CLOSE_TAG = ''; 39 | const STYLE_ATTR = 'style="%s"'; 40 | const CLASS_ATTR = 'class="%s"'; 41 | 42 | const CSS_COLOR = 'color: #%s;'; 43 | const EMPTY_TAGS = '/<[^\/>]*><\/[^>]*>/'; 44 | const LINE_BREAK = '
'; 45 | 46 | /** 47 | * Java color codes mapped to HEX colors. 48 | * 49 | * @var array 50 | */ 51 | static private $java_colors = [ 52 | '0' => '000000', //Black 53 | '1' => '0000AA', //Dark Blue 54 | '2' => '00AA00', //Dark Green 55 | '3' => '00AAAA', //Dark Aqua 56 | '4' => 'AA0000', //Dark Red 57 | '5' => 'AA00AA', //Dark Purple 58 | '6' => 'FFAA00', //Gold 59 | '7' => 'AAAAAA', //Gray 60 | '8' => '555555', //Dark Gray 61 | '9' => '5555FF', //Blue 62 | 'a' => '55FF55', //Green 63 | 'b' => '55FFFF', //Aqua 64 | 'c' => 'FF5555', //Red 65 | 'd' => 'FF55FF', //Light Purple 66 | 'e' => 'FFFF55', //Yellow 67 | 'f' => 'FFFFFF' //White 68 | ]; 69 | 70 | /** 71 | * Bedrock color codes mapped to HEX colors. 72 | * Has more colors and a lighter Gray. 73 | * 74 | * @var array 75 | */ 76 | static private $bedrock_colors = [ 77 | '0' => '000000', //Black 78 | '1' => '0000AA', //Dark Blue 79 | '2' => '00AA00', //Dark Green 80 | '3' => '00AAAA', //Dark Aqua 81 | '4' => 'AA0000', //Dark Red 82 | '5' => 'AA00AA', //Dark Purple 83 | '6' => 'FFAA00', //Gold 84 | '7' => 'C6C6C6', //Gray (Not the same as Java) 85 | '8' => '555555', //Dark Gray 86 | '9' => '5555FF', //Blue 87 | 'a' => '55FF55', //Green 88 | 'b' => '55FFFF', //Aqua 89 | 'c' => 'FF5555', //Red 90 | 'd' => 'FF55FF', //Light Purple 91 | 'e' => 'FFFF55', //Yellow 92 | 'f' => 'FFFFFF', //White 93 | 'g' => 'DDD605', //Minecoin Gold 94 | 'h' => 'E3D4D1', //Material Quartz 95 | 'i' => 'CECACA', //Material Iron 96 | 'j' => '443A3B', //Material Netherite 97 | 'm' => '971607', //Material Redstone 98 | 'n' => 'B4684D', //Material Copper 99 | 'p' => 'DEB12D', //Material Gold 100 | 'q' => '47A036', //Material Emerald 101 | 's' => '2CBAA8', //Material Diamond 102 | 't' => '21497B', //Material Lapis 103 | 'u' => '9A5CC6', //Material Amethyst 104 | 'v' => 'EB7114' //Material Resin 105 | ]; 106 | 107 | /** 108 | * Java formatting codes mapped to CSS style. 109 | * Some codes intentionally have no CSS. 110 | * 111 | * @var array 112 | */ 113 | static private $java_formatting = [ 114 | 'k' => '', //Obfuscated 115 | 'l' => 'font-weight: bold;', //Bold 116 | 'm' => 'text-decoration: line-through;', //Strikethrough 117 | 'n' => 'text-decoration: underline;', //Underline 118 | 'o' => 'font-style: italic;', //Italic 119 | 'r' => '' //Reset 120 | ]; 121 | 122 | /** 123 | * Bedrock formatting codes mapped to CSS style. 124 | * Does not have Strikethrough and Underline 125 | * Some codes intentionally have no CSS. 126 | * 127 | * @var array 128 | */ 129 | static private $bedrock_formatting = [ 130 | 'k' => '', //Obfuscated 131 | 'l' => 'font-weight: bold;', //Bold 132 | 'o' => 'font-style: italic;', //Italic 133 | 'r' => '' //Reset 134 | ]; 135 | 136 | /** 137 | * Java colors and formatting codes mapped to CSS classes. 138 | * 139 | * @var array 140 | */ 141 | static private $java_css_classnames = [ 142 | '0' => 'black', 143 | '1' => 'dark-blue', 144 | '2' => 'dark-green', 145 | '3' => 'dark-aqua', 146 | '4' => 'dark-red', 147 | '5' => 'dark-purple', 148 | '6' => 'gold', 149 | '7' => 'gray', 150 | '8' => 'dark-gray', 151 | '9' => 'blue', 152 | 'a' => 'green', 153 | 'b' => 'aqua', 154 | 'c' => 'red', 155 | 'd' => 'light-purple', 156 | 'e' => 'yellow', 157 | 'f' => 'white', 158 | 'k' => 'obfuscated', 159 | 'l' => 'bold', 160 | 'm' => 'line-strikethrough', 161 | 'n' => 'underline', 162 | 'o' => 'italic' 163 | ]; 164 | 165 | /** 166 | * Bedrock colors and formatting codes mapped to CSS classes. 167 | * 168 | * @var array 169 | */ 170 | static private $bedrock_css_classnames = [ 171 | '0' => 'black', 172 | '1' => 'dark-blue', 173 | '2' => 'dark-green', 174 | '3' => 'dark-aqua', 175 | '4' => 'dark-red', 176 | '5' => 'dark-purple', 177 | '6' => 'gold', 178 | '7' => 'gray', 179 | '8' => 'dark-gray', 180 | '9' => 'blue', 181 | 'a' => 'green', 182 | 'b' => 'aqua', 183 | 'c' => 'red', 184 | 'd' => 'light-purple', 185 | 'e' => 'yellow', 186 | 'f' => 'white', 187 | 'g' => 'minecoin-gold', 188 | 'h' => 'material-quartz', 189 | 'i' => 'material-iron', 190 | 'j' => 'material-netherite', 191 | 'k' => 'obfuscated', 192 | 'l' => 'bold', 193 | 'm' => 'material-redstone', 194 | 'n' => 'material-copper', 195 | 'o' => 'italic', 196 | 'p' => 'material-gold', 197 | 'q' => 'material-emerald', 198 | 's' => 'material-diamond', 199 | 't' => 'material-lapis', 200 | 'u' => 'material-amethyst', 201 | 'v' => 'material-resin' 202 | ]; 203 | 204 | /** 205 | * Encode text in UTF-8. 206 | * 207 | * @param mixed $text 208 | * @return string 209 | */ 210 | static private function UTF8Encode(string $text): string { 211 | //Encode the text in UTF-8, but only if it's not already. 212 | if (mb_detect_encoding($text) != 'UTF-8') 213 | $text = mb_convert_encoding($text, 'UTF-8', mb_detect_encoding($text)); 214 | 215 | return $text; 216 | } 217 | 218 | /** 219 | * Convert the long HEX format to the short. 220 | * Example of long HEX format: §x§a§A§0§0§0§0 221 | * 222 | * @param string $text 223 | * @return string 224 | */ 225 | static private function convertLongHEXtoShortHEX(string $text): string { 226 | if (preg_match_all(self::REGEX_HEX_LONG, $text, $matches, PREG_SET_ORDER)) { 227 | foreach ($matches as $match) { 228 | $hex_color = '§#'.$match[1].$match[2].$match[3].$match[4].$match[5].$match[6]; 229 | $text = str_replace($match[0], $hex_color, $text); 230 | } 231 | } 232 | 233 | return $text; 234 | } 235 | 236 | /** 237 | * Clean a string from all Java colors and formatting codes. 238 | * 239 | * @param string $text 240 | * @return string 241 | */ 242 | static public function clean(string $text): string { 243 | $text = self::UTF8Encode($text); 244 | $text = htmlspecialchars($text); 245 | 246 | $text = preg_replace(self::REGEX_HEX_LONG, '', $text); 247 | 248 | return preg_replace(self::REGEX_JAVA_ALL, '', $text); 249 | } 250 | 251 | /** 252 | * Clean a string from all Bedrock colors and formatting codes. 253 | * 254 | * @param string $text 255 | * @return string 256 | */ 257 | static public function cleanBedrock(string $text): string { 258 | $text = self::UTF8Encode($text); 259 | $text = htmlspecialchars($text); 260 | 261 | return preg_replace(self::REGEX_BEDROCK, '', $text); 262 | } 263 | 264 | /** 265 | * Convert a string with Java colors and formatting codes to a MOTD format. 266 | * 267 | * @param string $text 268 | * @param string $sign The text to prepend all color codes. 269 | * @param bool $hex_colors Should HEX colors be converted as well? If not, they will be removed. 270 | * @param bool $hex_long_format Should HEX colors be returned in the long format? 271 | * @return string 272 | */ 273 | static public function convertToMOTD(string $text, string $sign = '\u00A7', bool $hex_colors = false, bool $hex_long_format = false): string { 274 | $text = self::UTF8Encode($text); 275 | $text = str_replace("&", "&", $text); 276 | 277 | if ($hex_colors) { 278 | $text = self::convertLongHEXtoShortHEX($text); 279 | 280 | $text = preg_replace_callback( 281 | self::REGEX_JAVA, 282 | function($matches) use ($sign) { 283 | return $sign.strtolower($matches[1]); 284 | }, 285 | $text 286 | ); 287 | 288 | $text = preg_replace_callback( 289 | self::REGEX_HEX_SHORT, 290 | function($matches) use ($sign, $hex_long_format) { 291 | if ($hex_long_format) { 292 | $color = ltrim(strtoupper($matches[1]), '#'); 293 | $chars = str_split($color); 294 | 295 | return $sign.'x'.$sign.$chars[0].$sign.$chars[1].$sign.$chars[2].$sign.$chars[3].$sign.$chars[4].$sign.$chars[5]; 296 | } 297 | 298 | else 299 | return $sign.strtoupper($matches[1]); 300 | }, 301 | $text 302 | ); 303 | } 304 | 305 | else { 306 | $text = preg_replace(self::REGEX_HEX_SHORT, '', $text); 307 | $text = preg_replace(self::REGEX_HEX_LONG, '', $text); 308 | 309 | $text = preg_replace_callback( 310 | self::REGEX_JAVA, 311 | function($matches) use ($sign) { 312 | return $sign.strtolower($matches[1]); 313 | }, 314 | $text 315 | ); 316 | } 317 | 318 | $text = str_replace("\n", '\n', $text); 319 | $text = str_replace("&", "&", $text); 320 | 321 | return $text; 322 | } 323 | 324 | /** 325 | * Convert a string with Bedrock colors and formatting codes to a MOTD format. 326 | * 327 | * @param string $text 328 | * @param string $sign The text to prepend all color codes. 329 | * @return string 330 | */ 331 | static public function convertToBedrockMOTD(string $text, string $sign = '\u00A7'): string { 332 | $text = self::UTF8Encode($text); 333 | $text = str_replace("&", "&", $text); 334 | 335 | $text = preg_replace_callback( 336 | self::REGEX_BEDROCK, 337 | function($matches) use ($sign) { 338 | return $sign.strtolower($matches[1]); 339 | }, 340 | $text 341 | ); 342 | 343 | $text = str_replace("\n", '\n', $text); 344 | $text = str_replace("&", "&", $text); 345 | 346 | return $text; 347 | } 348 | 349 | /** 350 | * Convert a string with Java colors and formatting codes to HTML. 351 | * 352 | * @param string $text 353 | * @param bool $line_break_element Should new lines be converted to br tags? 354 | * @param bool $css_classes Should CSS classes be used instead of inline styes? 355 | * @param string $css_prefix The prefix for CSS classes. 356 | * @return string 357 | */ 358 | static public function convertToHTML(string $text, bool $line_break_element = false, bool $css_classes = false, string $css_prefix = 'minecraft-formatted--'): string { 359 | $text = self::UTF8Encode($text); 360 | $text = htmlspecialchars($text); 361 | 362 | $text = self::convertLongHEXtoShortHEX($text); 363 | 364 | preg_match_all(self::REGEX_JAVA_ALL, $text, $offsets); 365 | 366 | $colors = $offsets[0]; //This is what we are going to replace with HTML. 367 | $color_codes = $offsets[1]; //This is the color numbers/characters only. 368 | 369 | //No colors? Just return the text. 370 | if (empty($colors)) 371 | return $text; 372 | 373 | $open_tags = 0; 374 | 375 | foreach ($colors as $index => $color) { 376 | $color_code = strtolower($color_codes[$index]); 377 | 378 | $html = ''; 379 | 380 | $is_reset = $color_code === 'r'; 381 | $is_color = isset(self::$java_colors[$color_code]); 382 | $is_hex = strlen($color_code) === 7; //#RRGGBB 383 | 384 | if ($is_reset || $is_color || $is_hex) { 385 | // New colors or the reset char: reset all other colors and formatting. 386 | if ($open_tags != 0) { 387 | $html = str_repeat(self::CLOSE_TAG, $open_tags); 388 | $open_tags = 0; 389 | } 390 | } 391 | 392 | if ($css_classes && !$is_reset) { 393 | //No reason to give HEX colors a CSS class. 394 | if ($is_hex) { 395 | $html .= sprintf(self::START_TAG_WITH_STYLE, sprintf(self::CSS_COLOR, ltrim(strtoupper($color_code), '#'))); 396 | $open_tags++; 397 | } 398 | 399 | else { 400 | $css_classname = $css_prefix.self::$java_css_classnames[$color_code]; 401 | $html .= sprintf(self::START_TAG_WITH_CLASS, $css_classname); 402 | $open_tags++; 403 | } 404 | } 405 | 406 | else { 407 | if ($is_color) { 408 | $html .= sprintf(self::START_TAG_WITH_STYLE, sprintf(self::CSS_COLOR, self::$java_colors[$color_code])); 409 | $open_tags++; 410 | } 411 | 412 | else if ($is_hex) { 413 | $html .= sprintf(self::START_TAG_WITH_STYLE, sprintf(self::CSS_COLOR, ltrim(strtoupper($color_code), '#'))); 414 | $open_tags++; 415 | } 416 | 417 | //Special case for obfuscated, always add a CSS class for this. 418 | else if ($color_code === 'k') { 419 | $css_classname = $css_prefix.self::$java_css_classnames[$color_code]; 420 | $html .= sprintf(self::START_TAG_WITH_CLASS, $css_classname); 421 | $open_tags++; 422 | } 423 | 424 | else if (!$is_reset) { 425 | $html .= sprintf(self::START_TAG_WITH_STYLE, self::$java_formatting[$color_code]); 426 | $open_tags++; 427 | } 428 | } 429 | 430 | //Replace the color with the HTML code. We use preg_replace because of the limit parameter. 431 | $text = preg_replace('/'.$color.'/', $html, $text, 1); 432 | } 433 | 434 | //Still open tags? Close them! 435 | if ($open_tags != 0) 436 | $text = $text.str_repeat(self::CLOSE_TAG, $open_tags); 437 | 438 | //Move newline endings outside elements. 439 | while (strpos($text, "\n".self::CLOSE_TAG) !== false) 440 | $text = str_replace("\n".self::CLOSE_TAG, self::CLOSE_TAG."\n", $text); 441 | 442 | //Replace \n with
443 | if ($line_break_element) 444 | $text = str_replace(['\n', "\n"], self::LINE_BREAK, $text); 445 | 446 | //Return the text without empty HTML tags. Only to clean up bad color formatting from the user. 447 | return preg_replace(self::EMPTY_TAGS, '', $text); 448 | } 449 | 450 | /** 451 | * Convert a string with Bedrock colors and formatting codes to HTML. 452 | * 453 | * @param string $text 454 | * @param bool $line_break_element Should new lines be converted to br tags? 455 | * @param bool $css_classes Should CSS classes be used instead of inline styes? 456 | * @param string $css_prefix The prefix for CSS classes. 457 | * @return string 458 | */ 459 | static public function convertToBedrockHTML(string $text, bool $css_classes = false, string $css_prefix = 'minecraft-formatted--'): string { 460 | $text = self::UTF8Encode($text); 461 | $text = htmlspecialchars($text); 462 | 463 | preg_match_all(self::REGEX_BEDROCK, $text, $offsets); 464 | 465 | $colors = $offsets[0]; //This is what we are going to replace with HTML. 466 | $color_codes = $offsets[1]; //This is the color numbers/characters only. 467 | 468 | //No colors? Just return the text. 469 | if (empty($colors)) 470 | return $text; 471 | 472 | $open_tag = false; 473 | $current_color = ''; 474 | $current_formatting = []; 475 | 476 | foreach ($colors as $index => $color) { 477 | $color_code = strtolower($color_codes[$index]); 478 | 479 | $html = ''; 480 | $attributes = []; 481 | $styles = []; 482 | $classes = []; 483 | 484 | $is_reset = $color_code === 'r'; 485 | $is_color = isset(self::$bedrock_colors[$color_code]); 486 | 487 | //Close an open tag, if any 488 | if ($open_tag) { 489 | $html .= self::CLOSE_TAG; 490 | $open_tag = false; 491 | } 492 | 493 | //Reset all other colors and formatting. 494 | if ($is_reset) { 495 | $current_color = ''; //Reset current color code 496 | $current_formatting = []; //Reset current formatting codes 497 | } 498 | 499 | //Normal color code 500 | else if ($is_color) { 501 | if ($css_classes) 502 | $classes[] = $css_prefix.self::$bedrock_css_classnames[$color_code]; 503 | else 504 | $styles[] = sprintf(self::CSS_COLOR, self::$bedrock_colors[$color_code]); 505 | 506 | $current_color = $color_code; 507 | $open_tag = true; 508 | } 509 | 510 | //Formatting code 511 | else { 512 | //Special case for obfuscated, always add a CSS class for this 513 | if ($css_classes || $color_code === 'k') 514 | $classes[] = $css_prefix.self::$bedrock_css_classnames[$color_code]; 515 | else 516 | $styles[] = self::$bedrock_formatting[$color_code]; 517 | 518 | $current_formatting[$color_code] = true; 519 | $open_tag = true; 520 | } 521 | 522 | //Check if we need to add the current color, if not the current code. 523 | if (!empty($current_color) && $current_color != $color_code) { 524 | if ($css_classes) 525 | $classes[] = $css_prefix.self::$bedrock_css_classnames[$current_color]; 526 | else 527 | $styles[] = sprintf(self::CSS_COLOR, self::$bedrock_colors[$current_color]); 528 | } 529 | 530 | foreach ($current_formatting as $formatting => $is_enabled) { 531 | //Check if the formatting has already been added 532 | if ($is_enabled && $formatting != $color_code) { 533 | //Special case for obfuscated, always add a CSS class for this. 534 | if ($css_classes || $formatting === 'k') 535 | $classes[] = $css_prefix.self::$bedrock_css_classnames[$formatting]; 536 | else 537 | $styles[] = self::$bedrock_formatting[$formatting]; 538 | } 539 | } 540 | 541 | if (!empty($styles)) 542 | $attributes[] = sprintf(self::STYLE_ATTR, implode(' ', $styles)); 543 | 544 | if (!empty($classes)) 545 | $attributes[] = sprintf(self::CLASS_ATTR, implode(' ', $classes)); 546 | 547 | if (!empty($attributes)) 548 | $html .= sprintf(self::START_TAG, implode(' ', $attributes)); 549 | 550 | //Replace the color with the HTML code. We use preg_replace because of the limit parameter. 551 | $text = preg_replace('/'.$color.'/', $html, $text, 1); 552 | } 553 | 554 | if ($open_tag) 555 | $text .= self::CLOSE_TAG; 556 | 557 | //Return the text without empty HTML tags. Only to clean up bad color formatting from the user. 558 | return preg_replace(self::EMPTY_TAGS, '', $text); 559 | } 560 | } 561 | ?> 562 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. {http://fsf.org/} 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see {http://www.gnu.org/licenses/}. 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | PHP-MinecraftColors Copyright (C) 2013 Anders G. Jørgensen 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | {http://www.gnu.org/licenses/}. 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | {http://www.gnu.org/philosophy/why-not-lgpl.html}. 675 | --------------------------------------------------------------------------------