sitebuilder.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 deployDev($username, $domain, $adminName, $adminPassword) {
  45. $url = "$this->apiUrl/deploydev/$username/$domain";
  46. $data = [
  47. 'admin_name' => $adminName,
  48. 'admin_password' => $adminPassword
  49. ];
  50. return $this->sendRequest('POST', $url, $data);
  51. }
  52. /**
  53. * Revert the development site 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 revertDev($username, $domain, $adminName, $adminPassword) {
  68. $url = "$this->apiUrl/revertdev/$username/$domain";
  69. $data = [
  70. 'admin_name' => $adminName,
  71. 'admin_password' => $adminPassword
  72. ];
  73. return $this->sendRequest('POST', $url, $data);
  74. }
  75. /**
  76. * Removes 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 undeployDev($username, $domain, $adminName, $adminPassword) {
  91. $url = "$this->apiUrl/undeploydev/$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 undeployProd($username, $domain, $adminName, $adminPassword) {
  114. $url = "$this->apiUrl/undeployprod/$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 migrateprod($username, $domain, $adminName, $adminPassword) {
  136. $url = "$this->apiUrl/migrateprod/$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 disableprod($domain,$username) {
  153. $url = "$this->apiUrl/disableprod/$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) {
  166. $url = "$this->apiUrl/create/$username/$domain";
  167. return $this->sendRequest('GET', $url);
  168. }
  169. /**
  170. * Removes a user and placeholder webpage
  171. *
  172. * @param username: The username under which the domain is deployed
  173. * @param domain: The Domain to delete
  174. *
  175. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  176. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  177. */
  178. public function terminate($domain,$username) {
  179. $url = "$this->apiUrl/terminate/$username/$domain";
  180. return $this->sendRequest('GET', $url);
  181. }
  182. /**
  183. * Deletes the webpage (dev & prod)
  184. *
  185. * @param username: The username under which the domain is deployed
  186. * @param domain: The Domain to delete
  187. *
  188. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  189. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  190. */
  191. public function delete($domain,$username) {
  192. $url = "$this->apiUrl/delete/$username/$domain";
  193. return $this->sendRequest('GET', $url);
  194. }
  195. /**
  196. * Enables the prod webpage
  197. *
  198. * @param username: The username under which the domain is deployed
  199. * @param domain: The Domain to migrate
  200. *
  201. * @return a json with ['status' => $httpCode,'response' => ['isenabled' => 'YES']];
  202. * or a json with ['status' => $httpCode,'response' => ['isenabled' => 'NO']];
  203. */
  204. public function enableprod($domain,$username) {
  205. $url = "$this->apiUrl/enableprod/$username/$domain";
  206. return $this->sendRequest('GET', $url);
  207. }
  208. /**
  209. * Disables the prod webpage
  210. *
  211. * @param username: The username under which the domain is deployed
  212. * @param domain: The Domain to migrate
  213. *
  214. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  215. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  216. */
  217. public function isprodenabled($domain,$username) {
  218. $url = "$this->apiUrl/isprodenabled/$username/$domain";
  219. return $this->sendRequest('GET', $url);
  220. }
  221. /**
  222. * Disables the prod webpage
  223. *
  224. * @param username: The username under which the domain is deployed
  225. * @param domain: The Domain to migrate
  226. *
  227. * @return a json with ['status' => $httpCode,'response' => ['ssl_expiry' => 'Datum des Ablaufs des Zertifikats', 'ssl_remaining' => 'Anzahl der Tage bis zum Ablauf des Zertifikats']];
  228. */
  229. public function getSSLDays($domain,$username) {
  230. $url = "$this->apiUrl/getssldays/$username/$domain";
  231. return $this->sendRequest('GET', $url);
  232. }
  233. /**
  234. * Lists the Prod Backups for the prod webpage
  235. *
  236. * @param username: The username under which the domain is deployed
  237. * @param domain: The Domain to migrate
  238. *
  239. * @return a json with ['status' => $httpCode,'response' => [
  240. * 'backups' =>
  241. * [
  242. * ['backup_date' => 'ISO Backup Datum',
  243. * 'swiss_date' => 'Datum im Schweizer Format',
  244. * 'size_mb' => 'Grösse in Megabyte',
  245. * 'filename' => 'Dateiname des tar.gz's'
  246. * ]
  247. * ];
  248. *
  249. */
  250. public function listbackups($domain,$username) {
  251. $url = "$this->apiUrl/listbackups/$username/$domain";
  252. return $this->sendRequest('GET', $url);
  253. }
  254. /**
  255. * Restores a Backup with the given ISO Date
  256. *
  257. * @param username: The username under which the domain is deployed
  258. * @param domain: The Domain to migrate
  259. * @param backupDate: The ISO-Date of the backup (backup_date from listBackups) to restore
  260. *
  261. * @return a json with ['status' => $httpCode,'response' => ['success' => 'Text']];
  262. * or a json with ['status' => $httpCode,'response' => ['error' => 'Error-Description']];
  263. */
  264. public function restorebackup($domain,$username,$backupDate) {
  265. $url = "$this->apiUrl/restorebackup/$username/$domain";
  266. $data = [
  267. 'backup_date' => $backupDate
  268. ];
  269. return $this->sendRequest('POST', $url, $data);
  270. }
  271. private function sendRequest($method, $url, $data = []) {
  272. $ch = curl_init();
  273. $options = [
  274. CURLOPT_URL => $url,
  275. CURLOPT_RETURNTRANSFER => true,
  276. CURLOPT_HTTPHEADER => [
  277. "X-Api-Key: $this->apiKey",
  278. "Content-Type: application/json"
  279. ],
  280. CURLOPT_CUSTOMREQUEST => $method,
  281. CURLOPT_SSL_VERIFYPEER => false, // Deaktiviert die Überprüfung des SSL-Zertifikats
  282. CURLOPT_SSL_VERIFYHOST => false // Akzeptiert alle Hostnamen
  283. ];
  284. if ($method === 'POST' && !empty($data)) {
  285. $options[CURLOPT_POSTFIELDS] = json_encode($data);
  286. }
  287. curl_setopt_array($ch, $options);
  288. $response = curl_exec($ch);
  289. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  290. curl_close($ch);
  291. return [
  292. 'status' => $httpCode,
  293. 'response' => json_decode($response, true)
  294. ];
  295. }
  296. }