| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the box project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (c) Kevin Herrera <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  *     Théo Fidry <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * This source file is subject to the MIT license that is bundled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * with this source code in the file LICENSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | function get_prefix(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     $gitHubToken = getenv('GITHUB_TOKEN'); | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     $headerOption = false === $gitHubToken || '' === $gitHubToken | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         ? '' | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         : '-H "Authorization: token $GITHUB_TOKEN"' | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     ; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     $lastReleaseEndpointContents = shell_exec(<<<BASH | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | curl -s $headerOption https://api.github.com/repos/humbug/box/releases/latest | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | BASH | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     if (null === $lastReleaseEndpointContents) { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         throw new RuntimeException('Could not retrieve the last release endpoint.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     $contents = json_decode($lastReleaseEndpointContents, false, 512, JSON_PRETTY_PRINT); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     if (JSON_ERROR_NONE !== json_last_error()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         // TODO: switch to safe json parsing in the future | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         throw new RuntimeException( | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |                 'Could not parse the request contents: "%d: %s"', | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |                 json_last_error(), | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |                 json_last_error_msg() | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     if (false === isset($contents->tag_name) || false === is_string($contents->tag_name)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         throw new RuntimeException( | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 'No tag name could be found in: %s', | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 $contents | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     $lastRelease = trim($contents->tag_name); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     if ('' === $lastRelease) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         throw new RuntimeException('Invalid tag name found.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     return 'HumbugBox'.str_replace('.', '', $lastRelease); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     'prefix' => get_prefix(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     'whitelist-global-classes' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     'whitelist-global-functions' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     'whitelist' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         \Composer\Semver\Semver::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     'patchers' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // `stream_isatty()` is a PHP 7.2 function hence not defined in PHP 7.1. Unlike its function call, the string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         // in `function_exists()` is prefixed because the name can be resolved to a FQCN and appears as a user-land | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         // function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         // Function whitelisting is not used here since most checks are in the form of: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // if (function_exists('stream_isatty') return @stream_isatty($stream); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         // ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         // If the function is simply whitelisted, then it will check if the function `Humbug\stream_isatty` exists which | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // it will always even though the underlying function may not exists. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         // The following patcher can be safely removed however once https://github.com/humbug/php-scoper/issues/278 is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         // fixed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         static function (string $filePath, string $prefix, string $contents): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $files = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 'vendor/symfony/console/Output/StreamOutput.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 'vendor/symfony/phpunit-bridge/DeprecationErrorHandler.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 'vendor/symfony/polyfill-php72/bootstrap.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 'vendor/symfony/polyfill-php72/Php72.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 'vendor/symfony/var-dumper/Dumper/CliDumper.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 'vendor/composer/xdebug-handler/src/Process.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             if (false === in_array($filePath, $files, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 return $contents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $contents = preg_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 '/function_exists\(\''.$prefix.'\\\\(\\\\)?stream_isatty\'\)/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 "function_exists('stream_isatty')", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $contents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $contents = preg_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 '/function_exists\(\''.$prefix.'\\\\(\\\\)?sapi_windows_vt100_support\'\)/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 "function_exists('sapi_windows_vt100_support')", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $contents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             return $contents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     ], | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  | ]; | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |  |