Admin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * cwp7_Admin
  4. *
  5. * @author André Genrich <andre.genrich@thurdata.ch>
  6. * @author Roland Käser <roland.keaser@thurdata.ch>
  7. * @version 0.9
  8. * @copyright Copyright (c) 2021, Thurdata
  9. * @example ../test.php
  10. */
  11. /**
  12. * cwp7_Admin class documentation
  13. */
  14. /**
  15. * cwp7_Admin is a class which allows to manage cwp7 accounts via web-api
  16. *
  17. * You may create, modify, migrate, delete and get the attributes of a cwp7 account using this class
  18. *
  19. * For the usage examples of all class methods check the source code of test.php
  20. */
  21. class cwp7_Admin {
  22. private $constructorSuccess;
  23. private $cwp7URL;
  24. private $cwp7ConType;
  25. private $cwp7Port;
  26. private $cwp7Secure;
  27. protected $cwp7Token;
  28. /**
  29. * Constructor
  30. * @param string $cwp7Host cwp7 hostname or IP (example: cwp7.my.lan)
  31. * @param string $token api token
  32. * @param string $secure optional false to force unsecure (default true)
  33. */
  34. function __construct($cwp7Host, $token, $secure=true) {
  35. if(!in_array('curl', get_loaded_extensions())) {
  36. $this->constructorSuccess = false;
  37. return array('error_msg' => 'Error: PHP curl extension not available');
  38. }
  39. if (empty($cwp7Host) || empty($token)) {
  40. $this->constructorSuccess = false;
  41. return array('error_msg' => 'Error: Server login info missing, check server configuration');
  42. }
  43. if($secure) {
  44. $this->cwp7ConType = 'https://';
  45. $this->cwp7Secure = true;
  46. } else {
  47. $this->cwp7ConType = 'http://';
  48. $this->cwp7Secure = false;
  49. }
  50. $cwp7Hostname = explode(':', $cwp7Host);
  51. if (gethostbyname($cwp7Hostname[0]) == $cwp7Hostname[0] && !filter_var($cwp7Hostname[0], FILTER_VALIDATE_IP)) {
  52. $this->constructorSuccess = false;
  53. return array('error_msg' => 'Error: Cannot resolve ' . $cwp7Hostname[0] . ', check server configuration');
  54. }
  55. $this->cwp7Port = (isset($cwp7Hostname[1])) ? $cwp7Hostname[1] : '2304';
  56. $this->cwp7URL = $this->cwp7ConType . $cwp7Hostname[0] . ':' . $this->cwp7Port;
  57. $this->cwp7Token = $token;
  58. $this->constructorSuccess = true;
  59. }
  60. public function constructorSuccess() {
  61. return $this->constructorSuccess;
  62. }
  63. /**
  64. * getAllAccounts
  65. *
  66. * @return array of cwp7 accounts array of informations or error message
  67. */
  68. public function getAllAccounts() {
  69. $data = array();
  70. return $this->doRequest('account', 'list', $data);
  71. }
  72. /**
  73. * getAccount
  74. *
  75. * @param string $user user
  76. *
  77. * @return array of account informations or error message
  78. */
  79. public function getAccount($user) {
  80. $data = array(
  81. "user" => $user
  82. );
  83. return $this->doRequest('accountdetail', 'list', $data);
  84. }
  85. /**
  86. * createAccount
  87. *
  88. * @param array $params avvount informations, email required.
  89. *
  90. * @return array of account informations or error message
  91. */
  92. public function createAccount($params) {
  93. if(!isset($params['domain'])) {
  94. return array('error_msg' => 'Error: missing parameter domain');
  95. }
  96. if(!isset($params['user'])) {
  97. return array('error_msg' => 'Error: missing parameter user');
  98. }
  99. if(!isset($params['pass'])) {
  100. return array('error_msg' => 'Error: missing parameter pass');
  101. }
  102. if(!isset($params['email'])) {
  103. return array('error_msg' => 'Error: missing parameter email');
  104. }
  105. if(!isset($params['package'])) {
  106. return array('error_msg' => 'Error: missing parameter package');
  107. }
  108. if(!isset($params['autossl'])) {
  109. $params['autossl'] = 0;
  110. }
  111. $data = array(
  112. "domain" => 'test.local',
  113. "user" => 'test',
  114. "pass" => 'blubb123',
  115. "email" => 'andre.genrich@thurdata.ch',
  116. "package" => 'small',
  117. "autossl" => 0,
  118. "encodepass" => false,
  119. );
  120. /* $data = array(
  121. "domain" => $params['domain'],
  122. "user" => $params['user'],
  123. "pass" => base64_encode($params['pass']),
  124. "email" => $params['email'],
  125. "package" => $params['package'],
  126. "autossl" => $params['autossl'],
  127. "encodepass" => true,
  128. ); */
  129. return $this->doRequest('account', 'add', $data);
  130. }
  131. /**
  132. * modifyAccount
  133. *
  134. * @param array $params account informations, user, e-mail & new package required.
  135. *
  136. * @return array status -> OK or error message
  137. */
  138. public function modifyAccount($params) {
  139. if(!isset($params['user'])) {
  140. return array('error_msg' => 'Error: missing parameter user');
  141. }
  142. if(!isset($params['email'])) {
  143. return array('error_msg' => 'Error: missing parameter email');
  144. }
  145. if(!isset($params['package'])) {
  146. return array('error_msg' => 'Error: missing parameter package');
  147. }
  148. $data = array(
  149. 'user' => $params['user'],
  150. 'email' => $params['email'],
  151. 'package' => $params['package'],
  152. );
  153. return $this->doRequest('account', 'upd', $data);
  154. }
  155. /**
  156. * deleteAccount
  157. *
  158. * @param array user & e-mail required
  159. *
  160. * @return array success => true or error message
  161. */
  162. public function deleteAccount($params)
  163. {
  164. if(!isset($params['user'])) {
  165. return array('error_msg' => 'Error: missing parameter user');
  166. }
  167. if(!isset($params['email'])) {
  168. return array('error_msg' => 'Error: missing parameter email');
  169. }
  170. $data = array(
  171. "user" => $params['user'],
  172. "email" => $params['email'],
  173. );
  174. return $this->doRequest('account', 'del', $data);
  175. }
  176. /**
  177. * suspendAccount
  178. *
  179. * @param string $user user
  180. *
  181. * @return array success => true or error message
  182. */
  183. public function suspendAccount($user)
  184. {
  185. $data = array(
  186. "user" => $user,
  187. );
  188. return $this->doRequest('account', 'susp', $data);
  189. }
  190. /**
  191. * unsuspendAccount
  192. *
  193. * @param string $user user
  194. *
  195. * @return array success => true or error message
  196. */
  197. public function unsuspendAccount($user)
  198. {
  199. $data = array(
  200. 'user' => $user,
  201. );
  202. return $this->doRequest('account', 'unsp', $data);
  203. }
  204. /**
  205. * getPackages
  206. *
  207. * @return array packages
  208. */
  209. public function getPackages()
  210. {
  211. $data = array();
  212. return $this->doRequest('packages', 'list', $data);
  213. }
  214. /**
  215. * changePassword
  216. *
  217. * @return array packages
  218. */
  219. public function changePass($params)
  220. {
  221. if(!isset($params['user'])) {
  222. return array('error_msg' => 'Error: missing parameter user');
  223. }
  224. if(!isset($params['password'])) {
  225. return array('error_msg' => 'Error: missing parameter password');
  226. }
  227. $data = array(
  228. 'user' => $params['user'],
  229. 'password' => $params['password'],
  230. );
  231. return $this->doRequest('changepass', 'upd', $data);
  232. }
  233. /**
  234. * getQuota
  235. *
  236. * @param string $user user
  237. *
  238. * @return array quota details
  239. */
  240. public function getQuota($user)
  241. {
  242. $data = array('user' => $user);
  243. return $this->doRequest('accountquota', 'list', $data);
  244. }
  245. /**
  246. * getAutoSSL
  247. *
  248. * @param string $user user
  249. *
  250. * @return array certificate data or error
  251. */
  252. public function getAutoSSL($user)
  253. {
  254. $data = array('user' => $user);
  255. return $this->doRequest('autossl', 'list', $data);
  256. }
  257. /**
  258. * addAutoSSL
  259. *
  260. * @param array $user user, $name doaminname
  261. *
  262. * @return array status or error
  263. */
  264. public function addAutoSSL($params)
  265. {
  266. if(!isset($params['user'])) {
  267. return array('error_msg' => 'Error: missing parameter user');
  268. }
  269. if(!isset($params['name'])) {
  270. return array('error_msg' => 'Error: missing parameter name');
  271. }
  272. $data = array('user' => $params['user'], 'name' => $params['name']);
  273. return $this->doRequest('autossl', 'add', $data);
  274. }
  275. /**
  276. * renewAutoSSL
  277. *
  278. * @param array $user user, $name cert name
  279. *
  280. * @return array status or error
  281. */
  282. public function updateAutoSSL($params)
  283. {
  284. if(!isset($params['user'])) {
  285. return array('error_msg' => 'Error: missing parameter user');
  286. }
  287. if(!isset($params['name'])) {
  288. return array('error_msg' => 'Error: missing parameter name');
  289. }
  290. $data = array('user' => $params['user'], 'name' => $params['name']);
  291. return $this->doRequest('autossl', 'renew', $data);
  292. }
  293. /**
  294. * delAutoSSL
  295. *
  296. * @param array $user user, $name doaminname
  297. *
  298. * @return array status or error
  299. */
  300. public function delAutoSSL($params)
  301. {
  302. if(!isset($params['user'])) {
  303. return array('error_msg' => 'Error: missing parameter user');
  304. }
  305. if(!isset($params['name'])) {
  306. return array('error_msg' => 'Error: missing parameter name');
  307. }
  308. $data = array('user' => $params['user'], 'name' => $params['name']);
  309. return $this->doRequest('autossl', 'del', $data);
  310. }
  311. /**
  312. * getServerType
  313. *
  314. * @return array status or error
  315. */
  316. public function getServerType()
  317. {
  318. $data = array();
  319. return $this->doRequest('typeserver', 'list', $data);
  320. }
  321. /**
  322. * doRequest
  323. *
  324. * @param string $endpoint API endpoint
  325. * @param string $action endpoint action
  326. * @param array $data POST data
  327. *
  328. * @return string API response
  329. */
  330. protected function doRequest($endpoint, $action, $data) {
  331. $data['key'] = $this->cwp7Token;
  332. $data['action'] = $action;
  333. $ch = curl_init();
  334. curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/' . $endpoint);
  335. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  336. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  337. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  338. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  339. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  340. curl_setopt($ch, CURLOPT_POST, 1);
  341. $response = curl_exec($ch);
  342. if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
  343. curl_close($ch);
  344. return array('status' => 'Error', 'err_msg' => curl_error($ch));
  345. };
  346. curl_close($ch);
  347. return json_decode($response, true);
  348. }
  349. }