Admin.php 10 KB

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