| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace CSFCloud\Azure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use CSFCloud\Shell\CommandRunner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use MicrosoftAzure\Storage\Blob\BlobRestProxy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class Storage { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $connection_string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $account_name = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private $account_key = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $exec; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function __construct(string $container, string $connection_string) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->exec = new CommandRunner(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->connection_string = $connection_string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->container = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $dt = explode(";", $this->connection_string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         foreach ($dt as $pair) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             $pdt = explode("=", $pair); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             if ($pdt[0] == "AccountName") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                 $this->account_name = str_ireplace("AccountName=", "", $pdt[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             } else if ($pdt[0] == "AccountKey") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 $this->account_key = str_ireplace("AccountKey=", "", $pdt[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected function run($cmd) : string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $logfile = $this->exec->run(CommandRunner::COMMAND_SYNC, __DIR__, $cmd, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $log = $logfile->getText(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $logfile->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return $log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function getContainer() : string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return $this->container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function getAccountName() : string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->account_name; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function uploadToBlob(string $file, string $name, string $content_type = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (!file_exists($file) || !is_file($file) || !is_readable($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             throw new Exception("Invalid file"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $blobClient = BlobRestProxy::createBlobService($this->connection_string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $options = new CreateBlockBlobOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if ($content_type !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $options->setContentType($content_type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $options->setContentType(mime_content_type($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $blobClient->createBlockBlob($this->container, $name, file_get_contents($file), $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         /*$cmd = "az storage blob upload"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $cmd .= " -c " . escapeshellarg($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $cmd .= " -f " . escapeshellarg($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $cmd .= " -n " . escapeshellarg($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if ($content_type !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $cmd .= " --content-type " . escapeshellarg($content_type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $cmd .= " --content-type " . escapeshellarg(mime_content_type($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $cmd .= " --connection-string " . escapeshellarg($this->connection_string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $log = $this->run($cmd); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $log;*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function GenerateBlobSas(string $name, int $expire = 5 * 60 * 60, string $ip = null) : string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $signedPermissions = "r"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         //$signedStart = gmdate("Y-m-d\TH:i\Z", time()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $signedExpiry = gmdate("Y-m-d\TH:i\Z", time() + $expire); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $signedProtocol = "https"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $signedVersion = "2017-07-29"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $parameters = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $parameters[] = $signedPermissions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $parameters[] = ""; // signedStart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $parameters[] = $signedExpiry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $parameters[] = "/blob/" . $this->account_name . "/" . $this->container . "/" . $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $parameters[] = ""; // signedIdentifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         if ($ip === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $parameters[] = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $parameters[] = $ip; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $parameters[] = $signedProtocol; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $parameters[] = $signedVersion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $parameters[] = ""; // cacheControl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $parameters[] = ""; // contentDisposition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $parameters[] = ""; // contentEncoding | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $parameters[] = ""; // contentLanguage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $parameters[] = ""; // contentType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $stringToSign = utf8_encode(implode("\n", $parameters)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $decodedAccountKey = base64_decode($this->account_key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $signature = hash_hmac("sha256", $stringToSign, $decodedAccountKey, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $sig = urlencode(base64_encode($signature)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $sas  = 'sv=' . $signedVersion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $sas .= '&sr=' . "b"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         //$sas .= $buildOptQueryStr($cacheControl, '&rscc='); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         //$sas .= $buildOptQueryStr($contentDisposition, '&rscd='); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         //$sas .= $buildOptQueryStr($contentEncoding, '&rsce='); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         //$sas .= $buildOptQueryStr($contentLanguage, '&rscl='); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         //$sas .= $buildOptQueryStr($contentType, '&rsct='); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         //$sas .= $buildOptQueryStr($signedStart, '&st='); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $sas .= '&se=' . $signedExpiry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $sas .= '&sp=' . $signedPermissions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if ($ip !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $sas .= '&sip=' . $ip; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $sas .= '&spr=' . $signedProtocol; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         //$sas .= $buildOptQueryStr($signedIdentifier, '&si='); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $sas .= '&sig=' . $sig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         return $sas; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |     public function getBlobUrl(string $name) { | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         $url = "https://" . $this->account_name . ".blob.core.windows.net/" . $this->container . "/" . $name; | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |         return $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 141 |  |  | } | 
            
                        
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: