| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Composer\Package\Package; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Composer\Package\PackageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Downloads an add-on and builds more details information about it. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | class AddonBuilder { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     const ADDONS_DIR = 'add-ons'; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     const SCREENSHOTS_DIR = 'screenshots'; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     private $packagist; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function __construct(PackagistService $packagist) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $this->packagist = $packagist; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function build(Addon $addon) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $composer = $this->packagist->getComposer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $downloader = $composer->getDownloadManager(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $packageVersions = $this->packagist->getPackageVersions($addon->Name); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $time = time(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if (!$packageVersions) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             throw new Exception('Could not find corresponding Packagist versions'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         // Get the latest local and packagist version pair. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $version = $addon->Versions()->filter('Development', true)->first(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         foreach ($packageVersions as $packageVersion) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             if ($packageVersion->getVersionNormalized() != $version->Version) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $path = implode('/', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 TEMP_FOLDER, self::ADDONS_DIR, $addon->Name | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             // Convert PackagistAPI result into class compatible with Composer logic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $package = new Package( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 $addon->Name, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 $packageVersion->getVersionNormalized(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 $packageVersion->getVersion() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $package->setExtra($packageVersion->getExtra()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             if($source = $packageVersion->getSource()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 $package->setSourceUrl($source->getUrl()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 $package->setSourceType($source->getType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 $package->setSourceReference($source->getReference()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             if($dist = $packageVersion->getDist()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $package->setDistUrl($dist->getUrl()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 $package->setDistType($dist->getType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 $package->setDistReference($dist->getReference()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->download($package, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $this->buildReadme($addon, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $this->buildScreenshots($addon, $package, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $addon->LastBuilt = $time; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $addon->write(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     protected function download(PackageInterface $package, $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->packagist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ->getComposer() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             ->getDownloadManager() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ->download($package, $path); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     private function buildReadme(Addon $addon, $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $candidates = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             'README.md', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             'README.markdown', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             'README.mdown', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'docs/en/index.md' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         foreach ($candidates as $candidate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $lower = strtolower($candidate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $paths = array("$path/$candidate", "$path/$lower"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             foreach ($paths as $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 if (!file_exists($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $parser = GitHubMarkdownService::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 if ($context = $this->getGitHubContext($addon)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     $parser->setContext($context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $readme = $parser->toHtml(file_get_contents($path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 if (empty($readme)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $purifier = new HTMLPurifier(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 $readme = $purifier->purify($readme, array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     'Cache.SerializerPath' => TEMP_FOLDER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 $addon->Readme = $readme; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * Determine if the repository is from GitHub, and if so then return the "context" (vendor/module) from the path | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * @param  Addon $addon | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * @return string|false | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function getGitHubContext(Addon $addon) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $repository = $addon->Repository; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         if (stripos($repository, '://github.com/') === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         preg_match('/^http(?:s?):\/\/github\.com\/(?<module>.*)(\.git)?$/U', $repository, $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         if (isset($matches['module'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             return $matches['module']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         return false; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     private function buildScreenshots(Addon $addon, PackageInterface $package, $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $extra = $package->getExtra(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $screenshots = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $target = self::SCREENSHOTS_DIR . '/' . $addon->Name; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         if (isset($extra['screenshots'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             $screenshots = (array) $extra['screenshots']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } elseif (isset($extra['screenshot'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $screenshots = (array) $extra['screenshot']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         // Delete existing screenshots. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         foreach ($addon->Screenshots() as $screenshot) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             $screenshot->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $addon->Screenshots()->removeAll(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         foreach ($screenshots as $screenshot) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             if (!is_string($screenshot)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             $scheme = parse_url($screenshot, PHP_URL_SCHEME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             // Handle absolute image URLs. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             if ($scheme == 'http' || $scheme == 'https') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 $temp = TEMP_FOLDER . '/' . md5($screenshot); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                 if (!copy($screenshot, $temp)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 $data = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                     'name' => basename($screenshot), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                     'size' => filesize($temp), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                     'tmp_name' => $temp, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                     'error' => 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             // Handle images that are included in the repository. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 $source = $path . '/' . ltrim($screenshot, '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |                 // Prevent directory traversal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |                 if ($source != realpath($source)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                 if (!file_exists($source)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                 $data = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |                     'name' => basename($source), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |                     'size' => filesize($source), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |                     'tmp_name' => $source, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                     'error' => 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |             $upload = new Upload(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |             $upload->setValidator(new AddonBuilderScreenshotValidator()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |             $upload->load($data, $target); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             if($file = $upload->getFile()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |                 $addon->Screenshots()->add($file); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 205 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 207 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 208 |  |  |  | 
            
                        
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.