cwp7.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. <?php
  2. /**
  3. * WHMCS cwp7 Provisioning Module
  4. *
  5. * Provisioning for User Account on the cwp7 Server
  6. *
  7. * @see https://centos-webpanel.com/
  8. * @copyright Copyright (c) Thurdata GmbH 2022
  9. * @license GPL
  10. */
  11. use WHMCS\Database\Capsule;
  12. require_once 'Net/DNS2.php';
  13. require_once(__DIR__ . '/api/cwp7/Admin.php');
  14. if (!defined('WHMCS')) {
  15. die('This file cannot be accessed directly');
  16. }
  17. /**
  18. * Define CWP7 product metadata parameters.
  19. *
  20. * @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
  21. *
  22. * @return array
  23. */
  24. function cwp7_MetaData() {
  25. return array(
  26. 'DisplayName' => 'CentOS Web Panel Provisioning',
  27. 'APIVersion' => '1.2',
  28. 'DefaultNonSSLPort' => '2031',
  29. 'DefaultSSLPort' => '2031',
  30. 'RequiresServer' => true,
  31. 'ServiceSingleSignOnLabel' => 'Login to CWP7',
  32. 'AdminSingleSignOnLabel' => 'Login to CWP7 Admin'
  33. );
  34. }
  35. /**
  36. * Test connection to a CWP7 server with the given server parameters.
  37. *
  38. * Allows an admin user to verify that an API connection can be
  39. * successfully made with the given configuration parameters for a
  40. * server.
  41. *
  42. * When defined in a module, a test connection button will appear
  43. * alongside the server type dropdown when adding or editing an
  44. * existing server.
  45. *
  46. * @param array $params common module parameters
  47. *
  48. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  49. *
  50. * @return array
  51. */
  52. function cwp7_Testconnection($params) {
  53. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  54. $response = $cwp7->getServerType();
  55. if($response['status'] == 'OK') {
  56. return array(
  57. 'success' => true,
  58. 'error' => '',
  59. );
  60. }
  61. return array(
  62. 'success' => false,
  63. 'error' => $response['error_msg'],
  64. );
  65. }
  66. /**
  67. * Define CWP7 product configuration options.
  68. *
  69. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  70. *
  71. * @return array
  72. */
  73. function cwp7_ConfigOptions() {
  74. $whmcs = App::self();
  75. $serverGroupID = $whmcs->get_req_var('servergroup');
  76. $serverIDObj = Capsule::table('tblservergroupsrel')
  77. ->select('serverid')
  78. ->where('groupid', '=', $serverGroupID)
  79. ->get();
  80. $serverIDs = array();
  81. foreach($serverIDObj as $serverID) {
  82. array_push($serverIDs, $serverID->serverid);
  83. }
  84. $server = Capsule::table('tblservers')
  85. ->select('hostname', 'accesshash')
  86. ->where('id', $serverIDs)
  87. ->where('active', '=', 1)
  88. ->first();
  89. $cwp7 = new cwp7_Admin($server->hostname, $server->accesshash);
  90. $cwp7Packages = $cwp7->getPackages();
  91. if($cwp7Packages['status'] != 'OK') {
  92. logModuleCall(
  93. 'cwp7',
  94. __FUNCTION__,
  95. $cwp7Packages['status'],
  96. 'Could not fetch packages',
  97. $cwp7Packages['error_msg']
  98. );
  99. return false;
  100. }
  101. $cwp7PackageNames = array();
  102. foreach($cwp7Packages['msj'] as $cwp7Package) {
  103. array_push($cwp7PackageNames, $cwp7Package['package_name']);
  104. }
  105. $configOptions = array();
  106. $configOptions['package'] = array(
  107. 'FriendlyName' => 'CWP7 Package',
  108. 'Type' => 'dropdown',
  109. 'Options' => implode(',', $cwp7PackageNames),
  110. 'Description' => 'Select CWP7 Package',
  111. );
  112. $configOptions['inode'] = array( "Type" => "text" , "Description" => "Max of inode", "Default" => "0",);
  113. $configOptions['nofile'] = array( "Type" => "text", "Description" => "Max of nofile", "Default" => "100",);
  114. $configOptions['nproc'] = array( "Type" => "text" , "Description" => "Nproc limit - 40 suggested", "Default" => "40",);
  115. $configOptions['Nameserver IP for lookups'] = array( "Type" => "text" , "Description" => "Name Server IP", "Default" => "185.163.51.142",);
  116. $configOptions['Name of own Nameserver'] = array( "Type" => "text" , "Description" => "Name Server Name", "Default" => "ns1.thurdata.ch",);
  117. return $configOptions;
  118. }
  119. /**
  120. * Provision a new account of a CWP7 server.
  121. *
  122. * Attempt to provision a new CWP7 account. This is
  123. * called any time provisioning is requested inside of WHMCS. Depending upon the
  124. * configuration, this can be any of:
  125. * * When a new order is placed
  126. * * When an invoice for a new order is paid
  127. * * Upon manual request by an admin user
  128. *
  129. * @param array $params common module parameters
  130. *
  131. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  132. *
  133. * @return string 'success' or an error message
  134. */
  135. function cwp7_CreateAccount($params) {
  136. $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid'];
  137. $userdomain = $username . '.local';
  138. try {
  139. Capsule::table('tblhosting')
  140. ->where('id', '=', $params['serviceid'])
  141. ->update(
  142. array(
  143. 'username' => $username,
  144. 'domain' => $userdomain,
  145. )
  146. );
  147. } catch (\Exception $e) {
  148. logModuleCall(
  149. 'cwp7',
  150. __FUNCTION__,
  151. $params,
  152. 'Error: could save username & domain in database',
  153. $e->getMessage()
  154. );
  155. return 'Error: could save username & password in database';
  156. }
  157. if ($params["server"] == 1) {
  158. $data = array(
  159. 'package' => $params['configoption1'],
  160. 'domain' => $userdomain,
  161. 'user' => $username,
  162. 'pass' => $params['password'],
  163. 'email' => $params['clientsdetails']['email'],
  164. 'inode' => $params["configoption2"],
  165. 'nofile' => $params["configoption3"],
  166. 'nproc' => $params["configoption4"],
  167. 'server_ips'=>$params["serverip"]
  168. );
  169. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  170. $response = $cwp7->createAccount($data);
  171. }
  172. if($response['status'] != 'OK') {
  173. return 'Error: ' . $response['error_msg'];
  174. }
  175. return 'success';
  176. }
  177. /**
  178. * Removes a CWP7 account.
  179. *
  180. * Called when a termination is requested. This can be invoked automatically for
  181. * overdue products if enabled, or requested manually by an admin user.
  182. *
  183. * @param array $params common module parameters
  184. *
  185. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  186. *
  187. * @return string 'success' or an error message
  188. */
  189. function cwp7_TerminateAccount($params) {
  190. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  191. $response = $cwp7->deleteAccount(array('user' => $params['username'], 'email' => $params['clientsdetails']['email']));
  192. if($response['status'] != 'OK') {
  193. return 'Error: ' . $response['error_msg'];
  194. }
  195. return 'success';
  196. }
  197. /**
  198. * Set a CWP7 account to status inactive.
  199. *
  200. * Called when a suspension is requested. This is invoked automatically by WHMCS
  201. * when a product becomes overdue on payment or can be called manually by admin
  202. * user.
  203. *
  204. * @param array $params common module parameters
  205. *
  206. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  207. *
  208. * @return string 'success' or an error message
  209. */
  210. function cwp7_SuspendAccount($params) {
  211. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  212. $response = $cwp7->suspendAccount($params['username']);
  213. if($response['status'] != 'OK') {
  214. return 'Error: ' . $response['error_msg'];
  215. }
  216. return 'success';
  217. }
  218. /**
  219. * Set a CWP7 account to status active.
  220. *
  221. * Called when an un-suspension is requested. This is invoked
  222. * automatically upon payment of an overdue invoice for a product, or
  223. * can be called manually by admin user.
  224. *
  225. * @param array $params common module parameters
  226. *
  227. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  228. *
  229. * @return string 'success' or an error message
  230. */
  231. function cwp7_UnsuspendAccount($params) {
  232. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  233. $response = $cwp7->unsuspendAccount($params['username']);
  234. if($response['status'] != 'OK') {
  235. return 'Error: ' . $response['error_msg'];
  236. }
  237. return 'success';
  238. }
  239. /**
  240. * Client area output logic handling.
  241. *
  242. * This function is used to define module specific client area output. It should
  243. * return an array consisting of a template file and optional additional
  244. * template variables to make available to that template.
  245. *
  246. * @param array $params common module parameters
  247. *
  248. * @see https://developers.whmcs.com/provisioning-modules/client-area-output/
  249. *
  250. * @return array
  251. */
  252. function cwp7_ClientArea($params) {
  253. $clientInfo = array('moduleclientarea' => '1');
  254. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  255. $response = $cwp7->getAutoSSL($params['username']);
  256. if($response['status'] == 'OK') {
  257. $sslSites = array();
  258. foreach($response['msj'] as $sslSite) {
  259. $sslSites[$sslSite['ssl']] = array(
  260. 'auotssl' => $sslSite['autossl'],
  261. 'expire' => $sslSite['exp'],
  262. );
  263. }
  264. }
  265. $response = $cwp7->getAccount($params['username']);
  266. if($response['status'] != 'OK') {
  267. logModuleCall(
  268. 'cwp7',
  269. __FUNCTION__,
  270. $params,
  271. 'debug',
  272. $response
  273. );
  274. }
  275. $domains = $response['result']['domains'];
  276. $subDomains = $response['result']['subdomins'];
  277. $clientInfo['domains'] = array();
  278. foreach($domains as $domain) {
  279. if($domain['path'] == '/home/' . $params['username'] . '/public_html') {
  280. $clientInfo['mgmtDomain'] = $domain['domain'];
  281. $clientInfo['mgmtEmail'] = $domain['email'];
  282. } else {
  283. $domain['relpath'] = str_replace('/home/' . $params['username'], '~', $domain['path']);
  284. if(array_key_exists($domain['domain'], $sslSites)) {
  285. $domain['ssl'] = 1;
  286. $domain['sslexpire'] = $sslSites[$domain['domain']]['expire'];
  287. $domain['autossl'] = $sslSites[$domain['domain']]['auotssl'];
  288. }
  289. if(cwp7CheckA($domain['domain'],$params['serverip'],$params['configoption5']) == 1) {
  290. $domain['DNS'] = 1;
  291. }
  292. $domain['domainNS'] = cwp7CheckSOA($domain['domain'],$params['configoption5'],$params['configoption6']);
  293. $domain['subdomains'] = array();
  294. foreach($subDomains as $subDomain) {
  295. if($subDomain['domain'] == $domain['domain']) {
  296. $subFQDN = $subDomain['subdomain'] . '.' . $subDomain['domain'];
  297. $subDomain['relpath'] = str_replace('/home/' . $params['username'], '~', $subDomain['path']);
  298. if(array_key_exists($subFQDN, $sslSites)) {
  299. $subDomain['ssl'] = 1;
  300. $subDomain['sslexpire'] = $sslSites[$subFQDN]['expire'];
  301. $subDomain['autossl'] = $sslSites[$subFQDN]['auotssl'];
  302. } else {
  303. unset($subDomain['ssl']);
  304. unset($subDomain['sslexpire']);
  305. unset($subDomain['autossl']);
  306. }
  307. if(cwp7CheckA($subFQDN,$params['serverip'],$params['configoption5']) == 1) {
  308. $subDomain['DNS'] = 1;
  309. } else {
  310. unset($subDomain['DNS']);
  311. }
  312. array_push($domain['subdomains'], $subDomain);
  313. }
  314. }
  315. array_push($clientInfo['domains'], $domain);
  316. }
  317. }
  318. return array(
  319. 'tabOverviewReplacementTemplate' => 'clientarea',
  320. 'vars' => $clientInfo,
  321. );
  322. }
  323. /**
  324. * Perform single sign-on for a CWP7 account.
  325. *
  326. * When successful, returns a URL to which the user should be redirected.
  327. *
  328. * @param array $params common module parameters
  329. *
  330. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  331. *
  332. * @return array
  333. */
  334. function cwp7_ServiceSingleSignOn($params) {
  335. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  336. $response = $cwp7->getLoginLink($params['username']);
  337. if($response['status'] == 'OK') {
  338. $link = $response['msj']['details'];
  339. $linkautologin = $link[0]['url'];
  340. return array(
  341. 'success' => true,
  342. 'redirectTo' => $linkautologin,
  343. );
  344. } else {
  345. return array(
  346. 'success' => false,
  347. 'redirectTo' => '',
  348. );
  349. }
  350. }
  351. /**
  352. * Change the password for a CWP7 account.
  353. *
  354. * Called when a password change is requested. This can occur either due to a
  355. * client requesting it via the client area or an admin requesting it from the
  356. * admin side.
  357. *
  358. * This option is only available to client end users when the product is in an
  359. * active status.
  360. *
  361. * @param array $params common module parameters
  362. *
  363. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  364. *
  365. * @return string "success" or an error message
  366. */
  367. function cwp7_ChangePassword($params) {
  368. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  369. $response = $cwp7->changePass(array('user' => $params['username'], 'password' => $params['password']));
  370. if($response['status'] != 'OK') {
  371. return 'Error: ' . $response['error_msg'];
  372. }
  373. return 'success';
  374. }
  375. /**
  376. * Upgrade or downgrade a CWP7 account by package.
  377. *
  378. * Called to apply any change in product assignment or parameters. It
  379. * is called to provision upgrade or downgrade orders, as well as being
  380. * able to be invoked manually by an admin user.
  381. *
  382. * This same function is called for upgrades and downgrades of both
  383. * products and configurable options.
  384. *
  385. * @param array $params common module parameters
  386. *
  387. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  388. *
  389. * @return string "success" or an error message
  390. */
  391. function cwp7_ChangePackage($params) {
  392. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  393. $response = $cwp7->modifyAccount(array('user' => $params['username'], 'email' => $params['clientdetails']['email'], 'package' => $params['configoption1']));
  394. logModuleCall(
  395. 'cwp7',
  396. __FUNCTION__,
  397. $params,
  398. 'debug',
  399. $response
  400. );
  401. if($response['status'] != 'OK') {
  402. return 'Error: ' . $response['error_msg'];
  403. }
  404. return 'success';
  405. }
  406. /**
  407. * Usage Update
  408. *
  409. * Important: Runs daily per server not per product
  410. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  411. * @param array $params common module parameters
  412. *
  413. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  414. */
  415. function cwp7_UsageUpdate($params) {
  416. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  417. $response = $cwp7->getAllAccounts();
  418. if($response['status'] == 'OK'){
  419. $results = $response['msj'];
  420. for($i = 0; $i < count($results); $i++){
  421. if($results[$i]['diskusage'] == '') {
  422. $diskusage = 0;
  423. } else {
  424. $diskusage = trim($results[$i]['diskusage']);
  425. }
  426. if($results[$i]['disklimit'] == '') {
  427. $disklimit = 0;
  428. } else {
  429. $disklimit = trim($results[$i]['disklimit']);
  430. }
  431. if($results[$i]['bandwidth'] == '') {
  432. $bandwidth = 0;
  433. } else {
  434. $bandwidth =trim($results[$i]['bandwidth']);
  435. }
  436. if($results[$i]['bwlimit'] == '') {
  437. $bwlimit = 0;
  438. } else {
  439. $bwlimit = trim($results[$i]['bwlimit']);
  440. }
  441. $domain = trim($results[$i]['domain']);
  442. try {
  443. \WHMCS\Database\Capsule::table('tblhosting')
  444. ->where('server', $params['serverid'])
  445. ->where('domain', $domain)
  446. ->update([
  447. 'diskusage' => $diskusage,
  448. 'disklimit' => $disklimit,
  449. 'bwusage' => $bandwidth,
  450. 'bwlimit' => $bwlimit,
  451. 'lastupdate' => date('Y-m-d H:i:S'),
  452. ]);
  453. } catch (\Exception $e) {
  454. logActivity('ERROR: Unable to update server usage: ' . $e->getMessage());
  455. }
  456. }
  457. }
  458. }
  459. /**
  460. * Additional actions a client user can invoke.
  461. *
  462. * Define additional actions a client user can perform for an instance of a
  463. * product/service.
  464. *
  465. * Any actions you define here will be automatically displayed in the available
  466. * list of actions within the client area.
  467. *
  468. * @return array
  469. */
  470. function cwp7_ClientAreaCustomButtonArray ($params) {
  471. return array(
  472. 'Neue Domain' => 'newDomain',
  473. );
  474. }
  475. /**
  476. * Additional actions a client user can invoke.
  477. *
  478. * Define additional actions a client user is allowed to perform for an instance of a
  479. * product/service.
  480. *
  481. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  482. *
  483. * @return array
  484. */
  485. function cwp7_ClientAreaAllowedFunctions() {
  486. return array(
  487. "Enable SSL" => "enableSSL",
  488. "Renew SSL" => "renewSSL",
  489. "Set DNS" => "setDNS",
  490. "Unset DNS" => "unsetDNS",
  491. "Confirm Enable SSL" => "enableSSLConfirm",
  492. "Confirm Renew SSL" => "renewSSLConfirm",
  493. "Confirm Set DNS" => "setDNSConfirm",
  494. "Confirm Unset DNS" => "unsetDNSConfirm",
  495. "Info DNS" => "infoDNS",
  496. "Info SSL" => "infoSSL",
  497. "Add Domain" => "addDomain",
  498. "Add Subdomain" => "addSubdomain",
  499. "New Subdomain" => "newSubdomain",
  500. "Confirm Delete Domain" => "delDomainConfirm",
  501. "Delete Domain" => "delDomain",
  502. "Confirm Delete Subdomain" => "delSubdomainConfirm",
  503. "Delete Subdomain" => "delSubdomain",
  504. );
  505. }
  506. /**
  507. * Opens a form to add a new domain.
  508. *
  509. * @param array $params common module parameters
  510. *
  511. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  512. *
  513. * @return array template information
  514. */
  515. function cwp7_newDomain($params) {
  516. return array(
  517. 'breadcrumb' => array(
  518. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newDomain' => 'Neue Domain',
  519. ),
  520. 'templatefile' => 'cwp7_add_domain',
  521. );
  522. }
  523. /**
  524. * Adds a new domain to a CWP7 account.
  525. *
  526. * @param array $params common module parameters
  527. *
  528. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  529. *
  530. * @return string "success" or an error message
  531. */
  532. function cwp7_addDomain($params) {
  533. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  534. return 'Error: invalid domain name';
  535. }
  536. $vars['user'] = $params['username'];
  537. $vars['name'] = $_POST['d'];
  538. $vars['type'] = 'domain';
  539. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  540. $response = $cwp7->addDomain($vars);
  541. if($response['status'] != 'OK') {
  542. return 'Error: ' . $response['error_msg'];
  543. }
  544. return 'success';
  545. }
  546. /**
  547. * Opens a form to add a new subdomain to a domain.
  548. *
  549. * @param array $params common module parameters
  550. *
  551. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  552. *
  553. * @return array template information
  554. */
  555. function cwp7_newSubdomain($params) {
  556. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  557. return 'Error: invalid domain name';
  558. }
  559. return array(
  560. 'breadcrumb' => array(
  561. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSubdomain' => 'Neue Subdomain',
  562. ),
  563. 'templatefile' => 'cwp7_add_subdomain',
  564. 'vars' => array(
  565. 'domainselected' => $_POST['d'],
  566. ),
  567. );
  568. }
  569. /**
  570. * Adds a new subdomain to domain of a CWP7 account.
  571. *
  572. * @param array $params common module parameters
  573. *
  574. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  575. *
  576. * @return string "success" or an error message
  577. */
  578. function cwp7_addSubdomain($params) {
  579. logModuleCall(
  580. 'cwp7',
  581. __FUNCTION__,
  582. $_POST,
  583. 'debug addSubdomain',
  584. $params
  585. );
  586. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  587. return 'Error: invalid domain name';
  588. }
  589. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  590. return 'Error: invalid subdomain name';
  591. }
  592. $vars['user'] = $params['username'];
  593. $vars['name'] = $_POST['s'] . '.' . $_POST['d'];
  594. $vars['type'] = 'subdomain';
  595. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  596. $response = $cwp7->addDomain($vars);
  597. if($response['status'] != 'OK') {
  598. return 'Error: ' . $response['error_msg'];
  599. }
  600. return 'success';
  601. }
  602. /**
  603. * Opens a form to delete a domain from a CWP7 account.
  604. *
  605. * @param array $params common module parameters
  606. *
  607. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  608. *
  609. * @return array template information
  610. */
  611. function cwp7_delDomainConfirm($params) {
  612. return array(
  613. 'templatefile' => 'cwp7_del_domain_confirm',
  614. 'vars' => array(
  615. 'deldomain' => $_POST['d'],
  616. ),
  617. );
  618. }
  619. /**
  620. * Removes a domain from a CWP7 account.
  621. *
  622. * @param array $params common module parameters
  623. *
  624. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  625. *
  626. * @return string "success" or an error message
  627. */
  628. function cwp7_delDomain($params) {
  629. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  630. return 'Error: invalid domain name';
  631. }
  632. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  633. $response = $cwp7->getAccount($params['username']);
  634. if($response['status'] != 'OK') {
  635. logModuleCall(
  636. 'cwp7',
  637. __FUNCTION__,
  638. $params,
  639. 'debug',
  640. $response
  641. );
  642. }
  643. $domains = $response['result']['domains'];
  644. $clientdomains = array();
  645. foreach($domains as $domain){
  646. if($domain['domain'] != $params['domain']) {
  647. array_push($clientdomains, $domain['domain']);
  648. }
  649. }
  650. if(!in_array($_POST['d'], $clientdomains)) {
  651. logModuleCall(
  652. 'cwp7',
  653. __FUNCTION__,
  654. $_POST,
  655. 'POST DATA VIOLATION',
  656. $params
  657. );
  658. return 'Error: ' . $_POST['d'] . ' not in client domains';
  659. }
  660. // do delete domain
  661. $vars['user'] = $params['username'];
  662. $vars['name'] = $_POST['d'];
  663. $vars['type'] = 'domain';
  664. $response = $cwp7->deleteDomain($vars);
  665. if($response['status'] != 'OK') {
  666. return 'Error: ' . $response['error_msg'];
  667. }
  668. return 'success';
  669. }
  670. /**
  671. * Opens a form to delete a subdomain from domain of a CWP7 account.
  672. *
  673. * @param array $params common module parameters
  674. *
  675. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  676. *
  677. * @return array template information
  678. */
  679. function cwp7_delSubdomainConfirm($params) {
  680. return array(
  681. 'templatefile' => 'cwp7_del_subdomain_confirm',
  682. 'vars' => array(
  683. 'delsubdomain' => $_POST['d'],
  684. ),
  685. );
  686. }
  687. /**
  688. * Removes a subdomain from a domain of a CWP7 account.
  689. *
  690. * @param array $params common module parameters
  691. *
  692. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  693. *
  694. * @return string "success" or an error message
  695. */
  696. function cwp7_delSubdomain($params) {
  697. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  698. return 'Error: invalid domain name';
  699. }
  700. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  701. $response = $cwp7->getAccount($params['username']);
  702. if($response['status'] != 'OK') {
  703. logModuleCall(
  704. 'cwp7',
  705. __FUNCTION__,
  706. $params,
  707. 'debug',
  708. $response
  709. );
  710. }
  711. $subdomains = $response['result']['subdomins'];
  712. $clientsubdomains = array();
  713. foreach($subdomains as $subdomain){
  714. if($subdomain['domain'] != $params['domain']) {
  715. array_push($clientsubdomains, $subdomain['subdomain'] . "." . $subdomain['domain']);
  716. }
  717. }
  718. if(!in_array($_POST['d'], $clientsubdomains)) {
  719. logModuleCall(
  720. 'cwp7',
  721. __FUNCTION__,
  722. $_POST,
  723. 'POST DATA VIOLATION',
  724. $params
  725. );
  726. return 'Error: ' . $_POST['d'] . ' not in client subdomains';
  727. }
  728. // do delete subdomain
  729. $vars['user'] = $params['username'];
  730. $vars['name'] = $_POST['d'];
  731. $vars['type'] = 'subdomain';
  732. $response = $cwp7->deleteDomain($vars);
  733. if($response['status'] != 'OK') {
  734. return 'Error: ' . $response['error_msg'];
  735. }
  736. return 'success';
  737. }
  738. /**
  739. * Opens a form to enable SSL for a subdomain or domain of a CWP7 account.
  740. *
  741. * @param array $params common module parameters
  742. *
  743. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  744. *
  745. * @return array template information
  746. */
  747. function cwp7_enableSSLConfirm($params) {
  748. return array(
  749. 'templatefile' => 'cwp7_enable_SSL_confirm',
  750. 'vars' => array(
  751. 'SSLdomain' => $_POST['d'],
  752. ),
  753. );
  754. }
  755. /**
  756. * Aktivate CWP7 AutoSSL for a subdomain or domain of a CWP7 account.
  757. *
  758. * @param array $params common module parameters
  759. *
  760. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  761. *
  762. * @return string "success" or an error message
  763. */
  764. function cwp7_enableSSL($params) {
  765. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  766. return 'Error: invalid domain name';
  767. }
  768. $vars['user'] = $params['username'];
  769. $vars['name'] = $_POST['d'];
  770. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  771. $response = $cwp7->addAutoSSL($vars);
  772. if($response['status'] != 'OK') {
  773. return 'Error: ' . $response['error_msg'];
  774. }
  775. return 'success';
  776. }
  777. /**
  778. * Opens a form to renew a SSL certificate for a subdomain or domain of a CWP7 account.
  779. *
  780. * @param array $params common module parameters
  781. *
  782. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  783. *
  784. * @return array template information
  785. */
  786. function cwp7_renewSSLConfirm($params) {
  787. return array(
  788. 'templatefile' => 'cwp7_renew_SSL_confirm',
  789. 'vars' => array(
  790. 'SSLdomain' => $_POST['d'],
  791. ),
  792. );
  793. }
  794. /**
  795. * Renews a SSL certificate for a subdomain or domain of a CWP7 account.
  796. *
  797. * @param array $params common module parameters
  798. *
  799. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  800. *
  801. * @return string "success" or an error message
  802. */
  803. function cwp7_renewSSL($params) {
  804. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  805. return 'Error: invalid domain name';
  806. }
  807. $vars['user'] = $params['username'];
  808. $vars['name'] = $_POST['d'];
  809. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  810. $response = $cwp7->updateAutoSSL($vars);
  811. if($response['status'] != 'OK') {
  812. return 'Error: ' . $response['error_msg'];
  813. }
  814. return 'success';
  815. }
  816. /**
  817. * Opens a form to set a DNS record for a subdomain or domain of a CWP7 account.
  818. *
  819. * @param array $params common module parameters
  820. *
  821. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  822. *
  823. * @return array template information
  824. */
  825. function cwp7_setDNSConfirm($params) {
  826. if(isset($_POST['s'])){
  827. return array(
  828. 'templatefile' => 'cwp7_set_DNS_confirm',
  829. 'vars' => array(
  830. 'DNSdomain' => $_POST['d'],
  831. 'DNSsubdomain' => $_POST['s'],
  832. ),
  833. );
  834. }
  835. return array(
  836. 'templatefile' => 'cwp7_set_DNS_confirm',
  837. 'vars' => array(
  838. 'DNSdomain' => $_POST['d'],
  839. ),
  840. );
  841. }
  842. /**
  843. * Opens a form to unsset a DNS record for a subdomain or domain of a CWP7 account.
  844. *
  845. * @param array $params common module parameters
  846. *
  847. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  848. *
  849. * @return array template information
  850. */
  851. function cwp7_unsetDNSConfirm($params) {
  852. if(isset($_POST['s'])){
  853. return array(
  854. 'templatefile' => 'cwp7_unset_DNS_confirm',
  855. 'vars' => array(
  856. 'DNSdomain' => $_POST['d'],
  857. 'DNSsubdomain' => $_POST['s'],
  858. ),
  859. );
  860. }
  861. return array(
  862. 'templatefile' => 'cwp7_unset_DNS_confirm',
  863. 'vars' => array(
  864. 'DNSdomain' => $_POST['d'],
  865. ),
  866. );
  867. }
  868. /**
  869. * Update a DNS zone for a domain setting a new record for a domain or subdomain of a CWP7 account.
  870. *
  871. * @param array $params common module parameters
  872. *
  873. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  874. *
  875. * @return string "success" or an error message
  876. */
  877. function cwp7_setDNS($params) {
  878. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  879. return 'Error: invalid domain name';
  880. }
  881. $domainName = $_POST['d'];
  882. $zoneRecords = array();
  883. if(isset($_POST['s'])){
  884. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  885. return 'Error: invalid subdomain name';
  886. }
  887. $hostName = $_POST['s'] . '.' . $domainName . '.';
  888. $newRecord = array(
  889. 'line' => $hostName.'|A|0',
  890. 'name' => $hostName,
  891. 'type' => 'A',
  892. 'class' => 'IN',
  893. 'data' => array(
  894. 'address' => $params['serverip'],
  895. ),
  896. );
  897. array_push($zoneRecords, $newRecord);
  898. } else {
  899. $hostName = $domainName . '.';
  900. $domainRecord = array(
  901. 'line' => $hostName.'|A|0',
  902. 'name' => $hostName,
  903. 'type' => 'A',
  904. 'class' => 'IN',
  905. 'data' => array(
  906. 'address' => $params['serverip'],
  907. ),
  908. );
  909. array_push($zoneRecords, $domainRecord);
  910. $wwwRecord = array(
  911. 'line' => 'www'.$hostName.'|A|0',
  912. 'name' => 'www'.$hostName,
  913. 'type' => 'A',
  914. 'class' => 'IN',
  915. 'data' => array(
  916. 'address' => $params['serverip'],
  917. ),
  918. );
  919. array_push($zoneRecords, $wwwRecord);
  920. }
  921. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  922. ->select('id')
  923. ->where('name', '=', $domainName)
  924. ->where('clientid', '=', $params['userid'])
  925. ->get();
  926. $zoneIDobj = $zoneIDcollection[0];
  927. $zoneID = $zoneIDobj->{'id'};
  928. if(!isset($zoneID)) {
  929. return 'Error: Zone for domain ' . $domainName . ' or not owned by client';
  930. }
  931. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  932. foreach($dnsZone['data']->records as $record) {
  933. if(($record->name != $hostName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  934. array_push($zoneRecords, $record);
  935. };
  936. }
  937. $result = localAPI('dnsmanager' ,
  938. array(
  939. 'dnsaction' => 'updateZone',
  940. 'zone_id' => $zoneID,
  941. 'records' => $zoneRecords,
  942. )
  943. );
  944. if($result['result'] != 'success') {
  945. return 'Error: ' . $result['message'];
  946. }
  947. return 'success';
  948. }
  949. /**
  950. * Removing a DNS record for a domain or subdomain of a CWP7 account.
  951. *
  952. * @param array $params common module parameters
  953. *
  954. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  955. *
  956. * @return string "success" or an error message
  957. */
  958. function cwp7_unsetDNS($params) {
  959. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  960. return 'Error: invalid domain name';
  961. }
  962. $domainName = $_POST['d'];
  963. $zoneRecords = array();
  964. if(isset($_POST['s'])){
  965. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  966. return 'Error: invalid subdomain name';
  967. }
  968. $hostName = $_POST['s'] . '.' . $domainName . '.';
  969. } else {
  970. $hostName = $domainName . '.';
  971. }
  972. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  973. ->select('id')
  974. ->where('name', '=', $domainName)
  975. ->where('clientid', '=', $params['userid'])
  976. ->get();
  977. $zoneIDobj = $zoneIDcollection[0];
  978. $zoneID = $zoneIDobj->{'id'};
  979. if(!isset($zoneID)) {
  980. return 'Error: Zone for domain ' . $domainName . ' or not owned by client';
  981. }
  982. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  983. foreach($dnsZone['data']->records as $record) {
  984. if(($record->name != $hostName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  985. array_push($zoneRecords, $record);
  986. };
  987. }
  988. $result = localAPI('dnsmanager' ,
  989. array(
  990. 'dnsaction' => 'updateZone',
  991. 'zone_id' => $zoneID,
  992. 'records' => $zoneRecords,
  993. )
  994. );
  995. if($result['result'] != 'success') {
  996. return 'Error: ' . $result['message'];
  997. }
  998. return 'success';
  999. }
  1000. /**
  1001. * Opens a form to inform about the DNS status of a subdomain or domain of a CWP7 account.
  1002. *
  1003. * @param array $params common module parameters
  1004. *
  1005. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1006. *
  1007. * @return array template information
  1008. */
  1009. function cwp7_infoDNS($params) {
  1010. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  1011. return 'Error: invalid domain name';
  1012. }
  1013. $cwp7nameserver = cwp7CheckSOA($_POST['d'],$params['configoption5'],$params['configoption6']);
  1014. return array(
  1015. 'templatefile' => 'cwp7_help_dns',
  1016. 'vars' => array(
  1017. 'infodomain' => $_POST['d'],
  1018. 'cwp7nameserver' => $cwp7nameserver,
  1019. ),
  1020. );
  1021. }
  1022. /**
  1023. * Opens a form to inform about the SSL status of a subdomain or domain of a CWP7 account.
  1024. *
  1025. * @param array $params common module parameters
  1026. *
  1027. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1028. *
  1029. * @return array template information
  1030. */
  1031. function cwp7_infoSSL($params) {
  1032. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  1033. return 'Error: invalid domain name';
  1034. }
  1035. return array(
  1036. 'templatefile' => 'cwp7_help_ssl',
  1037. 'vars' => array(
  1038. 'infodomain' => $_POST['d'],
  1039. ),
  1040. );
  1041. }
  1042. /**
  1043. * Ask nameservers for a IP adress of a given host.
  1044. *
  1045. * @param string $host hostname
  1046. * @param string $serverIP CWP7 server IP
  1047. * @param string $nameserverIP polled name server IP
  1048. * @param int $recurse optional -> used to follow CNAME responses
  1049. *
  1050. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1051. *
  1052. * @return bool
  1053. */
  1054. function cwp7CheckA($host, $serverIP, $nameserverIP, $recurse = 0) {
  1055. if($recurse > 3) {
  1056. return false;
  1057. }
  1058. $nameserver = array($nameserverIP);
  1059. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  1060. try {
  1061. $result = $resolver->query($host, 'A');
  1062. } catch(Net_DNS2_Exception $e) {
  1063. logModuleCall(
  1064. 'cwp7',
  1065. __FUNCTION__,
  1066. $e,
  1067. 'DNS lookup exception',
  1068. $e->getMessage()
  1069. );
  1070. }
  1071. $hostA = $result->answer;
  1072. if($hostA[0]->type == 'CNAME') {
  1073. if(cwp7CheckA($hostA[0]->cname, $serverIP, $nameserverIP, $recurse++)) {
  1074. return true;
  1075. }
  1076. }
  1077. if($hostA[0]->type == 'A') {
  1078. if($hostA[0]->address == $serverIP){
  1079. return true;
  1080. }
  1081. }
  1082. return false;
  1083. }
  1084. /**
  1085. * Ask nameservers for the authority of a domain.
  1086. *
  1087. * @param string $domain domain name
  1088. * @param string $nameserverIP polled name server IP
  1089. * @param string $nameserverName name of the own namesever
  1090. *
  1091. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1092. *
  1093. * @return string 'none' -> not registered, 'self' -> registered at own or the name of an other responsible nameserver
  1094. */
  1095. function cwp7CheckSOA($domain, $nameserverIP, $nameserverName ) {
  1096. $nameserver = array($nameserverIP);
  1097. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  1098. try {
  1099. $result = $resolver->query($domain, 'SOA');
  1100. } catch(Net_DNS2_Exception $e) {
  1101. logModuleCall(
  1102. 'cwp7',
  1103. __FUNCTION__,
  1104. $e,
  1105. 'DNS lookup exception',
  1106. $e->getMessage()
  1107. );
  1108. return 'none';
  1109. }
  1110. if($result->answer[0]->mname == $nameserverName) {
  1111. return 'self';
  1112. }
  1113. return $result->answer[0]->mname;
  1114. }