| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace CSFCloud\Containers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use CSFCloud\KeyManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use CSFCloud\Resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use CSFCloud\Files\RecursiveFileListing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Httpful\Request; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | class Container extends Resource { | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     private $containerId; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     private $statusCache; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 | 2 |  |     public function __construct (KeyManager $km, string $id) { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 | 2 |  |         parent::__construct($km); | 
            
                                                                        
                            
            
                                    
            
            
                | 17 | 2 |  |         $this->containerId = $id; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 2 |  |         $this->FetchStatus(); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 2 |  |     private function BuildUrl (string $api, array $query = []) : string { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 2 |  |         $query["key"] = $this->keymanager->GetServerKey(); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 2 |  |         $url = "https://api.csfcloud.com/container/" . urlencode($this->containerId) . $api . "?" . http_build_query($query); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         //echo "URL: " . $url . PHP_EOL; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 2 |  |         return $url; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 2 |  |     public function FetchStatus () { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 2 |  |         $request = Request::get($this->BuildUrl(""))->expectsText()->send(); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 2 |  |         $this->statusCache = json_decode($request->body, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 2 |  |     public function GetId () : string { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 2 |  |         return $this->containerId; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public function GetConfiguration () : array { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         return $this->statusCache["configuration"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     public function SetConfiguration (array $cnf) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $this->statusCache["configuration"] = $cnf; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 2 |  |     public function UpdateChanges () { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 2 |  |         Request::put($this->BuildUrl("")) | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 2 |  |             ->sendsJson()->body(json_encode($this->statusCache["configuration"]))->send(); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 2 |  |     public function GetContainerName () : string { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 2 |  |         return $this->statusCache["configuration"]["name"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 2 |  |     public function SetContainerName (string $newname) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 2 |  |         $this->statusCache["configuration"]["name"] = $newname; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     public function IsRunning () : bool { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         return $this->statusCache["running"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     public function GetLastLogId () : ?string { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         return $this->statusCache["last_log"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     public function GetLastLogWebSocketUrl () : string { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         return "wss://dashboard-logs.csfcloud.com/log?id=" . $this->statusCache["last_log"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     public function Build () : string { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         $response = Request::get($this->BuildUrl("/build"))->expectsJson()->send(); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         $this->statusCache["last_log"] = $response->body->run_id; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         return $this->statusCache["last_log"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     public function Run () : string { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         $response = Request::get($this->BuildUrl("/run"))->expectsJson()->send(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $this->statusCache["last_log"] = $response->body->run_id; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         return $this->statusCache["last_log"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     public function Stop () { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         Request::get($this->BuildUrl("/stop"))->expectsJson()->send(); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $this->statusCache["last_log"] = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 2 |  |     public function Delete () { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 2 |  |         Request::delete($this->BuildUrl(""))->send(); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 2 |  |     private function GetFileUrl(string $name) : string { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 2 |  |         return $this->BuildUrl("/files", [ | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 2 |  |             "filename" => $name | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 2 |  |     public function UploadFile (string $file, string $name) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 2 |  |         Request::post($this->GetFileUrl($name))->body(file_get_contents($file))->send(); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     public function DownloadFile (string $name, string $file) { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         shell_exec("wget " . escapeshellarg($this->GetFileUrl($name)) . " -O " . escapeshellarg($file)); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 2 |  |     public function GetFileContents (string $name) : string { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 2 |  |         $response = Request::get($this->GetFileUrl($name))->send(); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 | 2 |  |         return $response->body; | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     public function DeleteFile (string $name) { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         $url = $this->BuildUrl("/files", [ | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |             "filename" => $name | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         Request::delete($url)->send(); | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function UploadDirectory (string $localdir, string $remotedir) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->DeleteFile($remotedir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $localdir = realpath($localdir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $filelister = new RecursiveFileListing($localdir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $files = $filelister->scan(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         foreach ($files as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $relativefile = str_replace($localdir, "", $file); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 127 |  |  |             $this->UploadFile($relativefile, $file); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 131 |  |  | } |