Admin.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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" => $params['domain'],
  113. "user" => $params['user'],
  114. "pass" => base64_encode($params['pass']),
  115. "email" => $params['email'],
  116. "package" => $params['package'],
  117. "autossl" => $params['autossl'],
  118. "encodepass" => true,
  119. );
  120. return $this->doRequest('account', 'add', $data);
  121. }
  122. /**
  123. * modifyAccount
  124. *
  125. * @param array $params account informations, user, e-mail & new package required.
  126. *
  127. * @return array status -> OK or error message
  128. */
  129. public function modifyAccount($params) {
  130. if(!isset($params['user'])) {
  131. return array('error_msg' => 'Error: missing parameter user');
  132. }
  133. if(!isset($params['email'])) {
  134. return array('error_msg' => 'Error: missing parameter email');
  135. }
  136. if(!isset($params['package'])) {
  137. return array('error_msg' => 'Error: missing parameter package');
  138. }
  139. $data = array(
  140. 'user' => $params['user'],
  141. 'email' => $params['email'],
  142. 'package' => $params['package'],
  143. );
  144. return $this->doRequest('account', 'upd', $data);
  145. }
  146. /**
  147. * deleteAccount
  148. *
  149. * @param array user & e-mail required
  150. *
  151. * @return array success => true or error message
  152. */
  153. public function deleteAccount($params)
  154. {
  155. if(!isset($params['user'])) {
  156. return array('error_msg' => 'Error: missing parameter user');
  157. }
  158. if(!isset($params['email'])) {
  159. return array('error_msg' => 'Error: missing parameter email');
  160. }
  161. $data = array(
  162. "user" => $params['user'],
  163. "email" => $params['email'],
  164. );
  165. return $this->doRequest('account', 'del', $data);
  166. }
  167. /**
  168. * suspendAccount
  169. *
  170. * @param string $user user
  171. *
  172. * @return array success => true or error message
  173. */
  174. public function suspendAccount($user)
  175. {
  176. $data = array(
  177. "user" => $user,
  178. );
  179. return $this->doRequest('account', 'susp', $data);
  180. }
  181. /**
  182. * unsuspendAccount
  183. *
  184. * @param string $user user
  185. *
  186. * @return array success => true or error message
  187. */
  188. public function unsuspendAccount($user)
  189. {
  190. $data = array(
  191. 'user' => $user,
  192. );
  193. return $this->doRequest('account', 'unsp', $data);
  194. }
  195. /**
  196. * getPackages
  197. *
  198. * @return array packages
  199. */
  200. public function getPackages()
  201. {
  202. $data = array();
  203. return $this->doRequest('packages', 'list', $data);
  204. }
  205. /**
  206. * changePassword
  207. *
  208. * @return array packages
  209. */
  210. public function changePass($params)
  211. {
  212. if(!isset($params['user'])) {
  213. return array('error_msg' => 'Error: missing parameter user');
  214. }
  215. if(!isset($params['password'])) {
  216. return array('error_msg' => 'Error: missing parameter password');
  217. }
  218. $data = array(
  219. 'user' => $params['user'],
  220. 'password' => $params['password'],
  221. );
  222. return $this->doRequest('changepass', 'upd', $data);
  223. }
  224. /**
  225. * getQuota
  226. *
  227. * @param string $user user
  228. *
  229. * @return array quota details
  230. */
  231. public function getQuota($user)
  232. {
  233. $data = array('user' => $user);
  234. return $this->doRequest('accountquota', 'list', $data);
  235. }
  236. /**
  237. * getAutoSSL
  238. *
  239. * @param string $user user
  240. *
  241. * @return array certificate data or error
  242. */
  243. public function getAutoSSL($user)
  244. {
  245. $data = array('user' => $user);
  246. return $this->doRequest('autossl', 'list', $data);
  247. }
  248. /**
  249. * addAutoSSL
  250. *
  251. * @param array $user user, $name doaminname
  252. *
  253. * @return array status or error
  254. */
  255. public function addAutoSSL($params)
  256. {
  257. if(!isset($params['user'])) {
  258. return array('error_msg' => 'Error: missing parameter user');
  259. }
  260. if(!isset($params['name'])) {
  261. return array('error_msg' => 'Error: missing parameter name');
  262. }
  263. $data = array('user' => $params['user'], 'name' => $params['name']);
  264. return $this->doRequest('autossl', 'add', $data);
  265. }
  266. /**
  267. * renewAutoSSL
  268. *
  269. * @param array $user user, $name cert name
  270. *
  271. * @return array status or error
  272. */
  273. public function updateAutoSSL($params)
  274. {
  275. if(!isset($params['user'])) {
  276. return array('error_msg' => 'Error: missing parameter user');
  277. }
  278. if(!isset($params['name'])) {
  279. return array('error_msg' => 'Error: missing parameter name');
  280. }
  281. $data = array('user' => $params['user'], 'name' => $params['name']);
  282. return $this->doRequest('autossl', 'renew', $data);
  283. }
  284. /**
  285. * delAutoSSL
  286. *
  287. * @param array $user user, $name doaminname
  288. *
  289. * @return array status or error
  290. */
  291. public function delAutoSSL($params)
  292. {
  293. if(!isset($params['user'])) {
  294. return array('error_msg' => 'Error: missing parameter user');
  295. }
  296. if(!isset($params['name'])) {
  297. return array('error_msg' => 'Error: missing parameter name');
  298. }
  299. $data = array('user' => $params['user'], 'name' => $params['name']);
  300. return $this->doRequest('autossl', 'del', $data);
  301. }
  302. /**
  303. * doRequest
  304. *
  305. * @param string $endpoint API endpoint
  306. * @param string $action endpoint action
  307. * @param array $data POST data
  308. *
  309. * @return string API response
  310. */
  311. protected function doRequest($endpoint, $action, $data) {
  312. $data['key'] = $this->cwp7Token;
  313. $data['action'] = $action;
  314. $ch = curl_init();
  315. curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/' . $endpoint);
  316. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  317. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  318. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  319. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  320. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  321. curl_setopt($ch, CURLOPT_POST, 1);
  322. $response = curl_exec($ch);
  323. if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
  324. curl_close($ch);
  325. return array('status' => 'Error', 'err_msg' => curl_error($ch));
  326. };
  327. curl_close($ch);
  328. return json_decode($response, true);
  329. }
  330. }