Admin.php 10 KB

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