Admin.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. if (!$this->constructorSuccess()) {
  76. return array('error_msg' => 'Error: not connect');
  77. }
  78. $data = array("key" => $this->cwp7Token, "action" => 'list');
  79. $ch = curl_init();
  80. curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account');
  81. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  82. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  83. if(!$this->cwp7Secure) {
  84. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false);
  85. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false);
  86. }
  87. curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  88. curl_setopt ($ch, CURLOPT_POST, 1);
  89. $response = curl_exec($ch);
  90. if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
  91. curl_close($ch);
  92. return array('error_msg' => $response);
  93. };
  94. curl_close($ch);
  95. return json_decode($response, true);
  96. }
  97. /**
  98. * getAccount
  99. *
  100. * @param string $email login e-mail
  101. *
  102. * @return array of account informations or error message
  103. */
  104. public function getAccount($user) {
  105. if (!$this->constructorSuccess()) {
  106. return array('error_msg' => 'Error: not connect');
  107. }
  108. $data = array("key" => $this->cwp7Token, "action"=>'list', "user" => $user);
  109. $ch = curl_init();
  110. curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/accountdetail');
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  112. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  113. if(!$this->cwp7Secure) {
  114. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false);
  115. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false);
  116. }
  117. curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  118. curl_setopt ($ch, CURLOPT_POST, 1);
  119. $response = curl_exec($ch);
  120. if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
  121. curl_close($ch);
  122. return array('error_msg' => $response);
  123. };
  124. curl_close($ch);
  125. return json_decode($response, true);
  126. }
  127. /**
  128. * createAccount
  129. *
  130. * @param array $params avvount informations, email required.
  131. *
  132. * @return array of account informations or error message
  133. */
  134. public function createAccount($params) {
  135. if (!$this->constructorSuccess()) {
  136. return array('error_msg' => 'Error: not connected');
  137. }
  138. if(!isset($params['domain'])) {
  139. return array('error_msg' => 'Error: missing parameter domain');
  140. }
  141. if(!isset($params['user'])) {
  142. return array('error_msg' => 'Error: missing parameter user');
  143. }
  144. if(!isset($params['pass'])) {
  145. return array('error_msg' => 'Error: missing parameter pass');
  146. }
  147. if(!isset($params['email'])) {
  148. return array('error_msg' => 'Error: missing parameter email');
  149. }
  150. if(!isset($params['package'])) {
  151. return array('error_msg' => 'Error: missing parameter package');
  152. }
  153. if(!isset($params['autossl'])) {
  154. $params['autossl'] = 0;
  155. }
  156. $data = array(
  157. "key" => $this->cwp7Token,
  158. "action" =>'add',
  159. "domain" => $params['domain'],
  160. "user" => $params['user'],
  161. "pass" => base64_encode($params['pass']),
  162. "email" => $params['email'],
  163. "package" => $params['package'],
  164. "autossl" => $params['autossl'],
  165. "encodepass" => true,
  166. );
  167. $ch = curl_init();
  168. curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account');
  169. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  170. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  171. if(!$this->cwp7Secure) {
  172. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false);
  173. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false);
  174. }
  175. curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  176. curl_setopt ($ch, CURLOPT_POST, 1);
  177. $response = curl_exec($ch);
  178. if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
  179. curl_close($ch);
  180. return array('error_msg' => $response);
  181. };
  182. curl_close($ch);
  183. return json_decode($response, true);
  184. }
  185. /**
  186. * modifyAccount
  187. *
  188. * @param array $params account informations, user, e-mail & new package required.
  189. *
  190. * @see https://download.cwp7.com/published/web-api/v2.1-admin/accounts.md#user-content-Add%20User
  191. *
  192. * @return array status -> OK or error message
  193. */
  194. public function modifyAccount($params) {
  195. if (!$this->constructorSuccess()) {
  196. return array('error_msg' => 'Error: not connected');
  197. }
  198. if(!isset($params['user'])) {
  199. return array('error_msg' => 'Error: missing parameter user');
  200. }
  201. if(!isset($params['email'])) {
  202. return array('error_msg' => 'Error: missing parameter email');
  203. }
  204. if(!isset($params['package'])) {
  205. return array('error_msg' => 'Error: missing parameter package');
  206. }
  207. $data = array(
  208. "key" => $this->cwp7Token,
  209. "action" =>'upd',
  210. "user" => $params['user'],
  211. "email" => $params['email'],
  212. "package" => $params['package'],
  213. );
  214. $ch = curl_init();
  215. curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account');
  216. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  217. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  218. if(!$this->cwp7Secure) {
  219. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false);
  220. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false);
  221. }
  222. curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  223. curl_setopt ($ch, CURLOPT_POST, 1);
  224. $response = curl_exec($ch);
  225. if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
  226. curl_close($ch);
  227. return array('error_msg' => $response);
  228. };
  229. curl_close($ch);
  230. return json_decode($response, true);
  231. }
  232. /**
  233. * deleteAccount
  234. *
  235. * @param array user & e-mail required
  236. *
  237. * @return array success => true or error message
  238. */
  239. public function deleteAccount($params)
  240. {
  241. if (!$this->constructorSuccess()) {
  242. return array('error_msg' => 'Error: not connected');
  243. }
  244. if(!isset($params['user'])) {
  245. return array('error_msg' => 'Error: missing parameter user');
  246. }
  247. if(!isset($params['email'])) {
  248. return array('error_msg' => 'Error: missing parameter email');
  249. }
  250. $data = array(
  251. "key" => $this->cwp7Token,
  252. "action" =>'del',
  253. "user" => $params['user'],
  254. "email" => $params['email'],
  255. );
  256. $ch = curl_init();
  257. curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account');
  258. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  259. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  260. if(!$this->cwp7Secure) {
  261. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false);
  262. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false);
  263. }
  264. curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  265. curl_setopt ($ch, CURLOPT_POST, 1);
  266. $response = curl_exec($ch);
  267. if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
  268. curl_close($ch);
  269. return array('error_msg' => $response);
  270. };
  271. curl_close($ch);
  272. return json_decode($response, true);
  273. }
  274. /**
  275. * suspendAccount
  276. *
  277. * @param string $user required
  278. *
  279. * @return array success => true or error message
  280. */
  281. public function suspendAccount($user)
  282. {
  283. if (!$this->constructorSuccess()) {
  284. return array('error_msg' => 'Error: not connected');
  285. }
  286. $data = array(
  287. "key" => $this->cwp7Token,
  288. "action" =>'susp',
  289. "user" => $user,
  290. );
  291. $ch = curl_init();
  292. curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account');
  293. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  294. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  295. if(!$this->cwp7Secure) {
  296. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false);
  297. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false);
  298. }
  299. curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  300. curl_setopt ($ch, CURLOPT_POST, 1);
  301. $response = curl_exec($ch);
  302. if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
  303. curl_close($ch);
  304. return array('error_msg' => $response);
  305. };
  306. curl_close($ch);
  307. return json_decode($response, true);
  308. }
  309. /**
  310. * unsuspendAccount
  311. *
  312. * @param string $user required
  313. *
  314. * @return array success => true or error message
  315. */
  316. public function unsuspendAccount($user)
  317. {
  318. if (!$this->constructorSuccess()) {
  319. return array('error_msg' => 'Error: not connected');
  320. }
  321. $data = array(
  322. "key" => $this->cwp7Token,
  323. "action" =>'unsp',
  324. "user" => $user,
  325. );
  326. $ch = curl_init();
  327. curl_setopt($ch, CURLOPT_URL, $this->cwp7URL . '/v1/account');
  328. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  329. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  330. if(!$this->cwp7Secure) {
  331. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYPEER, false);
  332. curl_setopt($cwp7Client, CURLOPT_SSL_VERIFYHOST, false);
  333. }
  334. curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  335. curl_setopt ($ch, CURLOPT_POST, 1);
  336. $response = curl_exec($ch);
  337. if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
  338. curl_close($ch);
  339. return array('error_msg' => $response);
  340. };
  341. curl_close($ch);
  342. return json_decode($response, true);
  343. }
  344. }