sitebuilder.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. class ApiClient {
  3. private $apiUrl;
  4. private $apiKey;
  5. public function __construct($apiUrl, $apiKey) {
  6. $this->apiUrl = rtrim($apiUrl, '/');
  7. $this->apiKey = $apiKey;
  8. }
  9. /**
  10. * Check accessibility to the Server
  11. *
  12. * @param adminName: The Super-Admin User of the CRM System (usually the e-mail of the customer
  13. * @param adminPassword: A self randomly generated password
  14. *
  15. * @return string 'pong' on success
  16. *
  17. * Attention: The given parameters username, adminName and adminPassword must be
  18. * stored locally for the Single Sign on from whcms plugin
  19. *
  20. */
  21. public function ping($adminName, $adminPassword) {
  22. $url = "$this->apiUrl/ping/test/test";
  23. $data = [
  24. 'admin_name' => $adminName,
  25. 'admin_password' => $adminPassword
  26. ];
  27. return $this->sendRequest('POST', $url, $data);
  28. }
  29. /**
  30. * Initially deploy the development site for the customer
  31. *
  32. * @param username: The username under which the domain is deployed
  33. * @param domain: The Domain to migrate
  34. * @param adminName: The Super-Admin User of the CRM System (usually the e-mail of the customer
  35. * @param adminPassword: A self randomly generated password
  36. *
  37. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  38. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  39. *
  40. * Attention: The given parameters username, adminName and adminPassword must be
  41. * stored locally for the Single Sign on from whcms plugin
  42. *
  43. */
  44. public function deploy($username, $domain, $adminName, $adminPassword) {
  45. $url = "$this->apiUrl/deploy/$username/$domain";
  46. $data = [
  47. 'admin_name' => $adminName,
  48. 'admin_password' => $adminPassword
  49. ];
  50. return $this->sendRequest('POST', $url, $data);
  51. }
  52. /**
  53. * Initially deploy the placeholder site and config for the customer
  54. *
  55. * @param username: The username under which the domain is deployed
  56. * @param domain: The Domain to migrate
  57. * @param adminName: The Super-Admin User of the CRM System (usually the e-mail of the customer
  58. * @param adminPassword: A self randomly generated password
  59. *
  60. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  61. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  62. *
  63. * Attention: The given parameters username, adminName and adminPassword must be
  64. * stored locally for the Single Sign on from whcms plugin
  65. *
  66. */
  67. public function init($username, $domain, $adminName, $adminPassword) {
  68. $url = "$this->apiUrl/init/$username/$domain";
  69. $data = [
  70. 'admin_name' => $adminName,
  71. 'admin_password' => $adminPassword
  72. ];
  73. return $this->sendRequest('POST', $url, $data);
  74. }
  75. /**
  76. * Revert the development site for the customer
  77. *
  78. * @param username: The username under which the domain is deployed
  79. * @param domain: The Domain to migrate
  80. * @param adminName: The Super-Admin User of the CRM System (usually the e-mail of the customer
  81. * @param adminPassword: A self randomly generated password
  82. *
  83. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  84. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  85. *
  86. * Attention: The given parameters username, adminName and adminPassword must be
  87. * stored locally for the Single Sign on from whcms plugin
  88. *
  89. */
  90. public function revert($username, $domain, $adminName, $adminPassword) {
  91. $url = "$this->apiUrl/revert/$username/$domain";
  92. $data = [
  93. 'admin_name' => $adminName,
  94. 'admin_password' => $adminPassword
  95. ];
  96. return $this->sendRequest('POST', $url, $data);
  97. }
  98. /**
  99. * Removes the productive site for the customer
  100. *
  101. * @param username: The username under which the domain is deployed
  102. * @param domain: The Domain to migrate
  103. * @param adminName: The Super-Admin User of the CRM System (usually the e-mail of the customer
  104. * @param adminPassword: A self randomly generated password
  105. *
  106. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  107. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  108. *
  109. * Attention: The given parameters username, adminName and adminPassword must be
  110. * stored locally for the Single Sign on from whcms plugin
  111. *
  112. */
  113. public function undeploy($username, $domain, $adminName, $adminPassword) {
  114. $url = "$this->apiUrl/undeploy/$username/$domain";
  115. $data = [
  116. 'admin_name' => $adminName,
  117. 'admin_password' => $adminPassword
  118. ];
  119. return $this->sendRequest('POST', $url, $data);
  120. }
  121. /**
  122. * Migrate dev site to prod site
  123. *
  124. * @param username: The username under which the domain is deployed
  125. * @param domain: The Domain to migrate
  126. * @param adminName: The Super-Admin User of the CRM System (usually the e-mail of the customer
  127. * @param adminPassword: A self randomly generated password
  128. *
  129. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  130. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  131. *
  132. * Attention: The given parameters adminName and adminPassword must be stored locally for the
  133. * Single Sign on from whcms plugin
  134. */
  135. public function migrate($username, $domain, $adminName, $adminPassword) {
  136. $url = "$this->apiUrl/migrate/$username/$domain";
  137. $data = [
  138. 'admin_name' => $adminName,
  139. 'admin_password' => $adminPassword
  140. ];
  141. return $this->sendRequest('POST', $url, $data);
  142. }
  143. /**
  144. * Disables the prod webpage
  145. *
  146. * @param username: The username under which the domain is deployed
  147. * @param domain: The Domain to migrate
  148. *
  149. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  150. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  151. */
  152. public function disable($domain,$username) {
  153. $url = "$this->apiUrl/disable/$username/$domain";
  154. return $this->sendRequest('GET', $url);
  155. }
  156. /**
  157. * Creates a user and placeholder webpage
  158. *
  159. * @param username: The username under which the domain is deployed
  160. * @param domain: The Domain to create
  161. *
  162. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  163. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  164. */
  165. public function create($domain,$username, $adminName, $adminPassword) {
  166. $url = "$this->apiUrl/create/$username/$domain";
  167. $data = [
  168. 'admin_name' => $adminName,
  169. 'admin_password' => $adminPassword
  170. ];
  171. return $this->sendRequest('POST', $url, $data);
  172. }
  173. /**
  174. * Removes a user and placeholder webpage
  175. *
  176. * @param username: The username under which the domain is deployed
  177. * @param domain: The Domain to delete
  178. *
  179. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  180. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  181. */
  182. public function terminate($domain,$username) {
  183. $url = "$this->apiUrl/terminate/$username/$domain";
  184. return $this->sendRequest('GET', $url);
  185. }
  186. /**
  187. * Enables the prod webpage
  188. *
  189. * @param username: The username under which the domain is deployed
  190. * @param domain: The Domain to migrate
  191. *
  192. * @return a json with ['status' => $httpCode,'response' => ['isenabled' => 'YES']];
  193. * or a json with ['status' => $httpCode,'response' => ['isenabled' => 'NO']];
  194. */
  195. public function enable($domain,$username) {
  196. $url = "$this->apiUrl/enable/$username/$domain";
  197. return $this->sendRequest('GET', $url);
  198. }
  199. /**
  200. * Disables the prod webpage
  201. *
  202. * @param username: The username under which the domain is deployed
  203. * @param domain: The Domain to migrate
  204. *
  205. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  206. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  207. */
  208. public function isenabled($domain,$username) {
  209. $url = "$this->apiUrl/isenabled/$username/$domain";
  210. return $this->sendRequest('GET', $url);
  211. }
  212. /**
  213. * Disables the prod webpage
  214. *
  215. * @param username: The username under which the domain is deployed
  216. * @param domain: The Domain to migrate
  217. *
  218. * @return a json with ['status' => $httpCode,'response' => ['ssl_expiry' => 'Datum des Ablaufs des Zertifikats', 'ssl_remaining' => 'Anzahl der Tage bis zum Ablauf des Zertifikats']];
  219. */
  220. public function getSSLDays($domain,$username) {
  221. $url = "$this->apiUrl/getssldays/$username/$domain";
  222. return $this->sendRequest('GET', $url);
  223. }
  224. /**
  225. * Lists the Prod Backups for the prod webpage
  226. *
  227. * @param username: The username under which the domain is deployed
  228. * @param domain: The Domain to migrate
  229. *
  230. * @return a json with ['status' => $httpCode,'response' => [
  231. * 'backups' =>
  232. * [
  233. * ['backup_date' => 'ISO Backup Datum',
  234. * 'swiss_date' => 'Datum im Schweizer Format',
  235. * 'size_mb' => 'Grösse in Megabyte',
  236. * 'filename' => 'Dateiname des tar.gz's'
  237. * ]
  238. * ];
  239. *
  240. */
  241. public function listbackups($domain,$username) {
  242. $url = "$this->apiUrl/listbackups/$username/$domain";
  243. return $this->sendRequest('GET', $url);
  244. }
  245. /**
  246. * Restores a Backup with the given ISO Date
  247. *
  248. * @param username: The username under which the domain is deployed
  249. * @param domain: The Domain to migrate
  250. * @param backupDate: The ISO-Date of the backup (backup_date from listBackups) to restore
  251. *
  252. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  253. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  254. */
  255. public function restorebackup($domain,$username,$backupDate) {
  256. $url = "$this->apiUrl/restorebackup/$username/$domain";
  257. $data = [
  258. 'backup_date' => $backupDate
  259. ];
  260. return $this->sendRequest('POST', $url, $data);
  261. }
  262. private function sendRequest($method, $url, $data = []) {
  263. $ch = curl_init();
  264. $options = [
  265. CURLOPT_URL => $url,
  266. CURLOPT_RETURNTRANSFER => true,
  267. CURLOPT_HTTPHEADER => [
  268. "X-Api-Key: $this->apiKey",
  269. "Content-Type: application/json"
  270. ],
  271. CURLOPT_CUSTOMREQUEST => $method,
  272. CURLOPT_SSL_VERIFYPEER => false, // Deaktiviert die Überprüfung des SSL-Zertifikats
  273. CURLOPT_SSL_VERIFYHOST => false // Akzeptiert alle Hostnamen
  274. ];
  275. if ($method === 'POST' && !empty($data)) {
  276. $options[CURLOPT_POSTFIELDS] = json_encode($data);
  277. }
  278. curl_setopt_array($ch, $options);
  279. $response = curl_exec($ch);
  280. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  281. curl_close($ch);
  282. return [
  283. 'status' => $httpCode,
  284. 'response' => json_decode($response, true)
  285. ];
  286. }
  287. }