KeyHelp.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * FOSSBilling
  4. *
  5. * @copyright FOSSBilling (https://www.fossbilling.org)
  6. * @license Apache-2.0
  7. *
  8. * This source file is subject to the Apache-2.0 License that is bundled
  9. * with this source code in the file LICENSE
  10. */
  11. /**
  12. * KeyHelp API
  13. * Version: 2.4
  14. * @see https://app.swaggerhub.com/apis-docs/keyhelp/api/2.4
  15. */
  16. class Server_Manager_KeyHelp extends Server_Manager {
  17. public function init()
  18. {
  19. if (!extension_loaded('curl')) {
  20. throw new Server_Exception('cURL extension is not enabled');
  21. }
  22. if(empty($this->_config['ip'])) {
  23. throw new Server_Exception('Server manager "KeyHelp" is not configured properly. IP address is not set!');
  24. }
  25. if(empty($this->_config['host'])) {
  26. throw new Server_Exception('Server manager "KeyHelp" is not configured properly. Hostname is not set!');
  27. }
  28. if(empty($this->_config['accesshash'])) {
  29. throw new Server_Exception('Server manager "KeyHelp" is not configured properly. API Key / Access Hash is not set!');
  30. } else {
  31. $this->_config['accesshash'] = preg_replace("'(\r|\n)'","",$this->_config['accesshash']);
  32. }
  33. }
  34. public static function getForm()
  35. {
  36. return [
  37. 'label' => 'KeyHelp',
  38. 'form' => [
  39. 'credentials' => [
  40. 'fields' => [
  41. [
  42. 'name' => 'accesshash',
  43. 'type' => 'text',
  44. 'label' => 'API key',
  45. 'placeholder' => 'API key you generated from within KeyHelp.',
  46. 'required' => true,
  47. ],
  48. ],
  49. ],
  50. ]
  51. ];
  52. }
  53. /**
  54. * curl GET
  55. */
  56. public function getCURL($action, $actionEx)
  57. {
  58. $host = $this->_config['host'];
  59. $apiUrl = "https://".$host."/api/v2/";
  60. $action = $action . $actionEx;
  61. $actionapiUrl = $apiUrl . $action;
  62. $apiKey = $this->_config['accesshash'];
  63. $curl_session = curl_init();
  64. curl_setopt($curl_session ,CURLOPT_URL,$actionapiUrl);
  65. curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
  66. curl_setopt($curl_session, CURLOPT_HTTPHEADER, array(
  67. 'X-API-Key: '.$apiKey,
  68. 'accept: application/json',
  69. 'Content-Type: application/json',
  70. ));
  71. $result = json_decode(curl_exec($curl_session));
  72. curl_close($curl_session );
  73. return $result;
  74. }
  75. /**
  76. * Later: Custom login url for User ;) Waiting for FOSSBilling
  77. */
  78. public function getLoginUrl()
  79. {
  80. $host = $this->_config['host'];
  81. return 'https://'.$host.'';
  82. }
  83. public function getResellerLoginUrl()
  84. {
  85. $host = $this->_config['host'];
  86. return 'https://'.$host.'';
  87. }
  88. /**
  89. * Here we check if the API is working ;)
  90. */
  91. public function testConnection()
  92. {
  93. $action = "ping";
  94. $actionEx = "";
  95. $result = Server_Manager_KeyHelp::getCURL($action, $actionEx);
  96. if(isset($result)) {
  97. return true;
  98. var_dump($a);
  99. } else {
  100. throw new Server_Exception('Failed to connect to server');
  101. }
  102. }
  103. /**
  104. * Not working, because of FOSSBilling
  105. */
  106. public function synchronizeAccount(Server_Account $a)
  107. {
  108. $action = "clients/name/";
  109. $actionEx = $a->getUsername();
  110. $result = Server_Manager_KeyHelp::getCURL($action, $actionEx);
  111. $new = clone $a;
  112. if(empty($result->is_suspended)) {
  113. $new->setSuspended(false);
  114. } else {
  115. $new->setSuspended(true);
  116. }
  117. return $new;
  118. }
  119. /**
  120. * Package name must match on both KeyHelp and FOSSBilling!
  121. */
  122. public function createAccount(Server_Account $a)
  123. {
  124. $apiUrl = "https://".$this->_config['host']."/api/v2/";
  125. $apiKey = $this->_config['accesshash'];
  126. $this->getLog()->info('Creating account '.$a->getUsername());
  127. $client = $a->getClient();
  128. $package = $a->getPackage()->getName();
  129. $action = "hosting-plans/name/";
  130. $actionEx = preg_replace("/\s+/", "%20", $package);
  131. $result = Server_Manager_KeyHelp::getCURL($action, $actionEx);
  132. $packageID = $result->id;
  133. $data = array(
  134. "username"=> $a->getUsername(),
  135. "language"=> "de",
  136. "email"=> $client->getEmail(),
  137. "password"=> $a->getPassword(),
  138. "id_hosting_plan"=> $packageID,
  139. "is_suspended"=> false,
  140. "suspend_on"=> null,
  141. "delete_on"=> null,
  142. "send_login_credentials"=> true,
  143. "create_system_domain"=> false,
  144. );
  145. $action = "clients/";
  146. $actionapiUrl = $apiUrl . $action;
  147. $curl_session_b = curl_init();
  148. curl_setopt($curl_session_b ,CURLOPT_URL,$actionapiUrl);
  149. curl_setopt($curl_session_b, CURLOPT_RETURNTRANSFER, true);
  150. curl_setopt($curl_session_b, CURLOPT_CUSTOMREQUEST, 'POST');
  151. curl_setopt($curl_session_b, CURLOPT_SSL_VERIFYHOST, false);
  152. curl_setopt($curl_session_b, CURLOPT_SSL_VERIFYPEER, false);
  153. curl_setopt($curl_session_b, CURLOPT_HTTPHEADER, array(
  154. 'X-API-Key: '.$apiKey,
  155. 'accept: application/json',
  156. 'Content-Type: application/json',
  157. ));
  158. curl_setopt($curl_session_b, CURLOPT_POST, 1);
  159. curl_setopt($curl_session_b, CURLOPT_POSTFIELDS, json_encode($data));
  160. $result_a = json_decode(curl_exec($curl_session_b));
  161. curl_close($curl_session_b );
  162. $keyhelp_user_id = $result_a->id;
  163. $data = array(
  164. "id_user"=> $keyhelp_user_id,
  165. "domain"=> $a->getDomain()
  166. );
  167. $action = "domains";
  168. $actionapiUrl = $apiUrl . $action;
  169. $curl_session = curl_init();
  170. curl_setopt($curl_session ,CURLOPT_URL,$actionapiUrl);
  171. curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
  172. curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'POST');
  173. curl_setopt($curl_session, CURLOPT_SSL_VERIFYHOST, false);
  174. curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
  175. curl_setopt($curl_session, CURLOPT_HTTPHEADER, array(
  176. 'X-API-Key: '.$apiKey,
  177. 'accept: application/json',
  178. 'Content-Type: application/json',
  179. ));
  180. curl_setopt($curl_session, CURLOPT_POST, 1);
  181. curl_setopt($curl_session, CURLOPT_POSTFIELDS, json_encode($data));
  182. $result_c = json_decode(curl_exec($curl_session));
  183. curl_close($curl_session );
  184. if(isset($result_a)&&isset($result_c)) {
  185. return true;
  186. } else {
  187. throw new Server_Exception('Failed to create the hosting');
  188. }
  189. }
  190. public function suspendAccount(Server_Account $a)
  191. {
  192. $apiUrl = "https://".$this->_config['host']."/api/v2/";
  193. $apiKey = $this->_config['accesshash'];
  194. $action = "clients/name/";
  195. $actionEx = $a->getUsername();
  196. $result = Server_Manager_KeyHelp::getCURL($action, $actionEx);
  197. $userID = $result->id;
  198. $data = array(
  199. "is_suspended"=> true,
  200. );
  201. $action = "clients/".$userID;
  202. $actionapiUrl = $apiUrl . $action;
  203. $curl_session = curl_init();
  204. curl_setopt($curl_session ,CURLOPT_URL,$actionapiUrl);
  205. curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
  206. curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'PUT');
  207. curl_setopt($curl_session, CURLOPT_HTTPHEADER, array(
  208. 'X-API-Key: '.$apiKey,
  209. 'accept: application/json',
  210. 'Content-Type: application/json',
  211. ));
  212. curl_setopt($curl_session, CURLOPT_POSTFIELDS, json_encode($data));
  213. $result_c = json_decode(curl_exec($curl_session));
  214. curl_close($curl_session );
  215. if(isset($result_c)) {
  216. return true;
  217. } else {
  218. throw new Server_Exception('Failed to suspend account!');
  219. }
  220. }
  221. public function unsuspendAccount(Server_Account $a)
  222. {
  223. $apiUrl = "https://".$this->_config['host']."/api/v2/";
  224. $apiKey = $this->_config['accesshash'];
  225. $action = "clients/name/";
  226. $actionEx = $a->getUsername();
  227. $result = Server_Manager_KeyHelp::getCURL($action, $actionEx);
  228. $userID = $result->id;
  229. $data = array(
  230. "is_suspended"=> false,
  231. );
  232. $action = "clients/".$userID;
  233. $actionapiUrl = $apiUrl . $action;
  234. $curl_session = curl_init();
  235. curl_setopt($curl_session ,CURLOPT_URL,$actionapiUrl);
  236. curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
  237. curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'PUT');
  238. curl_setopt($curl_session, CURLOPT_HTTPHEADER, array(
  239. 'X-API-Key: '.$apiKey,
  240. 'accept: application/json',
  241. 'Content-Type: application/json',
  242. ));
  243. curl_setopt($curl_session, CURLOPT_POSTFIELDS, json_encode($data));
  244. $result_c = json_decode(curl_exec($curl_session));
  245. curl_close($curl_session );
  246. if(isset($result_c)) {
  247. return true;
  248. } else {
  249. throw new Server_Exception('Failed to suspend account!');
  250. }
  251. }
  252. public function cancelAccount(Server_Account $a)
  253. {
  254. $apiUrl = "https://".$this->_config['host']."/api/v2/";
  255. $apiKey = $this->_config['accesshash'];
  256. $action = "clients/name/";
  257. $actionEx = $a->getUsername();
  258. $result = Server_Manager_KeyHelp::getCURL($action, $actionEx);
  259. $userID = $result->id;
  260. $action = "clients/".$userID;
  261. $actionapiUrl = $apiUrl . $action;
  262. $curl_session = curl_init();
  263. curl_setopt($curl_session ,CURLOPT_URL,$actionapiUrl);
  264. curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
  265. curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'DELETE');
  266. curl_setopt($curl_session, CURLOPT_HTTPHEADER, array(
  267. 'X-API-Key: '.$apiKey,
  268. 'accept: application/json',
  269. 'Content-Type: application/json',
  270. ));
  271. $result_c = json_decode(curl_exec($curl_session));
  272. curl_close($curl_session );
  273. if(empty($result_c)) {
  274. return true;
  275. } else {
  276. throw new Server_Exception('Failed to cancel / delete account!');
  277. }
  278. }
  279. public function changeAccountPackage(Server_Account $a, Server_Package $p)
  280. {
  281. $this->getLog()->info('Changing password on account '.$a->getUsername());
  282. $apiUrl = "https://".$this->_config['host']."/api/v2/";
  283. $apiKey = $this->_config['accesshash'];
  284. $action = "clients/name/";
  285. $actionEx = $a->getUsername();
  286. $result = Server_Manager_KeyHelp::getCURL($action, $actionEx);
  287. $userID = $result->id;
  288. $package = preg_replace("/\s+/", "%20", $p->getName());
  289. $data = array(
  290. "id_hosting_plan"=> $package,
  291. );
  292. $action = "clients/".$userID;
  293. $actionapiUrl = $apiUrl . $action;
  294. $curl_session = curl_init();
  295. curl_setopt($curl_session ,CURLOPT_URL,$actionapiUrl);
  296. curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
  297. curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'PUT');
  298. curl_setopt($curl_session, CURLOPT_HTTPHEADER, array(
  299. 'X-API-Key: '.$apiKey,
  300. 'accept: application/json',
  301. 'Content-Type: application/json',
  302. ));
  303. curl_setopt($curl_session, CURLOPT_POSTFIELDS, json_encode($data));
  304. $result_c = json_decode(curl_exec($curl_session));
  305. curl_close($curl_session );
  306. if(isset($result_c)) {
  307. return true;
  308. } else {
  309. throw new Server_Exception('Failed to change the account password!');
  310. }
  311. }
  312. public function changeAccountPassword(Server_Account $a, $new)
  313. {
  314. $this->getLog()->info('Changing password on account '.$a->getUsername());
  315. $apiUrl = "https://".$this->_config['host']."/api/v2/";
  316. $apiKey = $this->_config['accesshash'];
  317. $action = "clients/name/";
  318. $actionEx = $a->getUsername();
  319. $result = Server_Manager_KeyHelp::getCURL($action, $actionEx);
  320. $userID = $result->id;
  321. $data = array(
  322. "password"=> $a->getPassword(),
  323. );
  324. $action = "clients/".$userID;
  325. $actionapiUrl = $apiUrl . $action;
  326. $curl_session = curl_init();
  327. curl_setopt($curl_session ,CURLOPT_URL,$actionapiUrl);
  328. curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
  329. curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'PUT');
  330. curl_setopt($curl_session, CURLOPT_HTTPHEADER, array(
  331. 'X-API-Key: '.$apiKey,
  332. 'accept: application/json',
  333. 'Content-Type: application/json',
  334. ));
  335. curl_setopt($curl_session, CURLOPT_POSTFIELDS, json_encode($data));
  336. $result_c = json_decode(curl_exec($curl_session));
  337. curl_close($curl_session );
  338. if(isset($result_c)) {
  339. return true;
  340. } else {
  341. throw new Server_Exception('Failed to change the account password!');
  342. }
  343. }
  344. /*
  345. Maybe upcoming - maybe not....
  346. */
  347. public function changeAccountUsername(Server_Account $a, $new)
  348. {
  349. throw new Server_Exception('KeyHelp does not support username changes');
  350. }
  351. public function changeAccountDomain(Server_Account $a, $new)
  352. {
  353. throw new Server_Exception('KeyHelp does not support changing the primary domain name');
  354. }
  355. public function changeAccountIp(Server_Account $a, $new)
  356. {
  357. throw new Server_Exception('KeyHelp does not support changing the IP');
  358. }
  359. }