| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * hirak/prestissimo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @author Hiraku NAKANO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license MIT https://github.com/hirak/prestissimo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Hirak\Prestissimo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Composer\IO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Composer\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class CopyRequest extends BaseRequest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private $destination; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var resource<stream<plainfile>> */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $fp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $success = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected static $defaultCurlOptions = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         CURLOPT_HTTPGET => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         CURLOPT_FOLLOWLOCATION => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         CURLOPT_MAXREDIRS => 20, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         CURLOPT_ENCODING => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $destination | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param bool $useRedirector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param IO\IOInterface $io | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param Config $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 7 |  |     public function __construct($url, $destination, $useRedirector, IO\IOInterface $io, Config $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 7 |  |         $this->setURL($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 7 |  |         $this->setDestination($destination); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 6 |  |         $this->setCA($config->get('capath'), $config->get('cafile')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 6 |  |         $this->setupAuthentication($io, $useRedirector, $config->get('github-domains'), $config->get('gitlab-domains')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 6 |  |     public function __destruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 6 |  |         if ($this->fp) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 6 |  |             fclose($this->fp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 6 |  |         if (!$this->success) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 6 |  |             if (file_exists($this->destination)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 6 |  |                 unlink($this->destination); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |     public function makeSuccess() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         $this->success = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 2 |  |     public function getCurlOptions() | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 2 |  |         $curlOpts = parent::getCurlOptions(); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 2 |  |         $curlOpts[CURLOPT_FILE] = $this->fp; | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 2 |  |         return $curlOpts; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 7 |  |     public function setDestination($destination) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 7 |  |         $this->destination = $destination; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 7 |  |         if (is_dir($destination)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |             throw new FetchException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |                 'The file could not be written to ' . $destination . '. Directory exists.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 6 |  |         $this->createDir($destination); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 6 |  |         $this->fp = fopen($destination, 'wb'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 6 |  |         if (!$this->fp) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             throw new FetchException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 'The file could not be written to ' . $destination | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 6 |  |     private function createDir($fileName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 6 |  |         $targetdir = dirname($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 6 |  |         if (!file_exists($targetdir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             if (!mkdir($targetdir, 0766, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 throw new FetchException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                     'The file could not be written to ' . $fileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 6 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |