TestClient.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. class ApiClient {
  3. private string $apiUrl;
  4. private string $apiKey;
  5. /**
  6. * @param $apiUrl
  7. * @param $apiKey
  8. */
  9. public function __construct($apiUrl, $apiKey) {
  10. $this->apiUrl = rtrim($apiUrl, '/');
  11. $this->apiKey = $apiKey;
  12. }
  13. /**
  14. * Initially deploy the development site for the customer
  15. *
  16. * @param $userName : The username under which the domain is deployed
  17. * @param $domain : The Domain to migrate
  18. * @param $adminName : The Super-Admin User of the CRM System (usually the e-mail of the customer
  19. * @param $adminPassword : A self randomly generated password
  20. *
  21. * @return array a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  22. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  23. *
  24. * Attention: The given parameters username, adminName and adminPassword must be
  25. * stored locally for the Single Sign on from whcms plugin
  26. */
  27. public function deployDev($userName, $domain, $adminName, $adminPassword): array {
  28. $url = "$this->apiUrl/deploydev/$userName/$domain";
  29. $data = [
  30. 'admin_name' => $adminName,
  31. 'admin_password' => $adminPassword
  32. ];
  33. return $this->sendRequest('POST', $url, $data);
  34. }
  35. /**
  36. * Migrate dev site to prod site
  37. *
  38. * @param $userName: The username under which the domain is deployed
  39. * @param $domain: The Domain to migrate
  40. * @param $adminName: The Super-Admin User of the CRM System (usually the e-mail of the customer
  41. * @param $adminPassword: A self randomly generated password
  42. *
  43. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  44. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  45. *
  46. * Attention: The given parameters adminName and adminPassword must be stored locally for the
  47. * Single Sign on from whcms plugin
  48. */
  49. public function migrateprod($userName, $domain, $adminName, $adminPassword): array {
  50. $url = "$this->apiUrl/migrateprod/$userName/$domain";
  51. $data = [
  52. 'admin_name' => $adminName,
  53. 'admin_password' => $adminPassword
  54. ];
  55. return $this->sendRequest('POST', $url, $data);
  56. }
  57. /**
  58. * Disables the prod webpage
  59. *
  60. * @param username: The username under which the domain is deployed
  61. * @param domain: The Domain to migrate
  62. *
  63. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  64. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  65. */
  66. public function disableprod($domain, $userName): array {
  67. $url = "$this->apiUrl/disableprod/$userName/$domain";
  68. return $this->sendRequest('GET', $url);
  69. }
  70. /**
  71. * Enables the prod webpage
  72. *
  73. * @param username: The username under which the domain is deployed
  74. * @param domain: The Domain to migrate
  75. *
  76. * @return a json with ['status' => $httpCode,'response' => ['isenabled' => 'YES']];
  77. * or a json with ['status' => $httpCode,'response' => ['isenabled' => 'NO']];
  78. */
  79. public function enableprod($domain, $userName): array {
  80. $url = "$this->apiUrl/enableprod/$userName/$domain";
  81. return $this->sendRequest('GET', $url);
  82. }
  83. /**
  84. * Disables the prod webpage
  85. *
  86. * @param username: The username under which the domain is deployed
  87. * @param domain: The Domain to migrate
  88. *
  89. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  90. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  91. */
  92. public function isprodenabled($domain, $userName): array {
  93. $url = "$this->apiUrl/isprodenabled/$userName/$domain";
  94. return $this->sendRequest('GET', $url);
  95. }
  96. /**
  97. * Disables the prod webpage
  98. *
  99. * @param username: The username under which the domain is deployed
  100. * @param domain: The Domain to migrate
  101. *
  102. * @return a json with ['status' => $httpCode,'response' => ['ssl_expiry' => 'Datum des Ablaufs des Zertifikats', 'ssl_remaining' => 'Anzahl der Tage bis zum Ablauf des Zertifikats']];
  103. */
  104. public function getSSLDays($domain, $userName): array {
  105. $url = "$this->apiUrl/getssldays/$userName/$domain";
  106. return $this->sendRequest('GET', $url);
  107. }
  108. /**
  109. * Lists the Prod Backups for the prod webpage
  110. *
  111. * @param username: The username under which the domain is deployed
  112. * @param domain: The Domain to migrate
  113. *
  114. * @return a json with ['status' => $httpCode,'response' => [
  115. * 'backups' =>
  116. * [
  117. * ['backup_date' => 'ISO Backup Datum',
  118. * 'swiss_date' => 'Datum im Schweizer Format',
  119. * 'size_mb' => 'Grösse in Megabyte',
  120. * 'filename' => 'Dateiname des tar.gz's'
  121. * ]
  122. * ];
  123. *
  124. */
  125. public function listbackups($domain, $userName): array {
  126. $url = "$this->apiUrl/listbackups/$userName/$domain";
  127. return $this->sendRequest('GET', $url);
  128. }
  129. /**
  130. * Restores a Backup with the given ISO Date
  131. *
  132. * @param username: The username under which the domain is deployed
  133. * @param domain: The Domain to migrate
  134. * @param backupDate: The ISO-Date of the backup (backup_date from listBackups) to restore
  135. *
  136. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  137. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  138. */
  139. public function restorebackup($domain, $userName, $backupDate): array {
  140. $url = "$this->apiUrl/restorebackup/$userName/$domain";
  141. $data = [
  142. 'backup_date' => $backupDate
  143. ];
  144. return $this->sendRequest('POST', $url, $data);
  145. }
  146. /**
  147. * Terminates a Website permanently
  148. *
  149. * @param $domain : The domain to terminate (terminates dev and prod website)
  150. * @param $userName : The username under which the domain is deployed
  151. * @return array : a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  152. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  153. */
  154. public function terminate($domain, $userName): array {
  155. $url = "$this->apiUrl/terminate/$userName/$domain";
  156. return $this->sendRequest('GET', $url);
  157. }
  158. private function sendRequest($method, $url, $data = []): array {
  159. $ch = curl_init();
  160. error_log("SEND REQUEST: " . print_r($data, true));
  161. $options = [
  162. CURLOPT_URL => $url,
  163. CURLOPT_RETURNTRANSFER => true,
  164. CURLOPT_HTTPHEADER => [
  165. "X-Api-Key: $this->apiKey",
  166. "Content-Type: application/json"
  167. ],
  168. CURLOPT_CONNECTTIMEOUT => 0,
  169. CURLOPT_TIMEOUT => 400,
  170. //CURLOPT_CUSTOMREQUEST => $method,
  171. CURLOPT_SSL_VERIFYPEER => false, // Deaktiviert die Überprüfung des SSL-Zertifikats
  172. CURLOPT_SSL_VERIFYHOST => false // Akzeptiert alle Hostnamen
  173. ];
  174. if ($method === 'POST' && !empty($data)) {
  175. $options[CURLOPT_POST] = true;
  176. // $options[CURLOPT_POSTFIELDS] = $data;
  177. $options[CURLOPT_POSTFIELDS] = json_encode($data,true);
  178. }
  179. curl_setopt_array($ch, $options);
  180. $response = curl_exec($ch);
  181. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  182. curl_close($ch);
  183. return [
  184. 'status' => $httpCode,
  185. 'response' => json_decode($response, true)
  186. ];
  187. }
  188. }
  189. // Beispielnutzung mit HTTPS
  190. $apiClient = new ApiClient('https://vm-dc-builderhost-01.thurdata.local', 'your-secure-password');
  191. //$response = $apiClient->getSSLDays('exampleuser', 'example.com');
  192. $response = $apiClient->deployDev('roka101', 'tech-design.ch', 'info@tech-design.ch', 'Technics2312');
  193. print_r($response);