cwp7.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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' => (int) $params["configoption2"],
  165. 'nofile' => (int) $params["configoption3"],
  166. 'nproc' => (int) $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'] == 'Error') {
  193. return 'Error: ' . $response['msj'];
  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. $data = array(
  394. 'user' => $params['username'],
  395. 'email' => $params['clientsdetails']['email'],
  396. 'package' => $params['configoption1'],
  397. 'inode' => (int) $params["configoption2"],
  398. 'openfiles' => (int) $params["configoption3"],
  399. 'processes' => (int) $params["configoption4"],
  400. 'server_ips'=> $params["serverip"],
  401. );
  402. $response = $cwp7->modifyAccount($data);
  403. if($response['status'] != 'OK') {
  404. return 'Error: ' . $response['error_msg'];
  405. }
  406. return 'success';
  407. }
  408. /**
  409. * Usage Update
  410. *
  411. * Important: Runs daily per server not per product
  412. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  413. * @param array $params common module parameters
  414. *
  415. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  416. */
  417. function cwp7_UsageUpdate($params) {
  418. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  419. $response = $cwp7->getAllAccounts();
  420. if($response['status'] == 'OK'){
  421. $results = $response['msj'];
  422. for($i = 0; $i < count($results); $i++){
  423. if($results[$i]['diskusage'] == '') {
  424. $diskusage = 0;
  425. } else {
  426. $diskusage = trim($results[$i]['diskusage']);
  427. }
  428. if($results[$i]['disklimit'] == '') {
  429. $disklimit = 0;
  430. } else {
  431. $disklimit = trim($results[$i]['disklimit']);
  432. }
  433. if($results[$i]['bandwidth'] == '') {
  434. $bandwidth = 0;
  435. } else {
  436. $bandwidth =trim($results[$i]['bandwidth']);
  437. }
  438. if($results[$i]['bwlimit'] == '') {
  439. $bwlimit = 0;
  440. } else {
  441. $bwlimit = trim($results[$i]['bwlimit']);
  442. }
  443. $domain = trim($results[$i]['domain']);
  444. try {
  445. \WHMCS\Database\Capsule::table('tblhosting')
  446. ->where('server', $params['serverid'])
  447. ->where('domain', $domain)
  448. ->update([
  449. 'diskusage' => $diskusage,
  450. 'disklimit' => $disklimit,
  451. 'bwusage' => $bandwidth,
  452. 'bwlimit' => $bwlimit,
  453. 'lastupdate' => date('Y-m-d H:i:S'),
  454. ]);
  455. } catch (\Exception $e) {
  456. logActivity('ERROR: Unable to update server usage: ' . $e->getMessage());
  457. }
  458. }
  459. }
  460. }
  461. /**
  462. * Additional actions a client user can invoke.
  463. *
  464. * Define additional actions a client user can perform for an instance of a
  465. * product/service.
  466. *
  467. * Any actions you define here will be automatically displayed in the available
  468. * list of actions within the client area.
  469. *
  470. * @return array
  471. */
  472. function cwp7_ClientAreaCustomButtonArray ($params) {
  473. return array(
  474. 'Neue Domain' => 'newDomain',
  475. );
  476. }
  477. /**
  478. * Additional actions a client user can invoke.
  479. *
  480. * Define additional actions a client user is allowed to perform for an instance of a
  481. * product/service.
  482. *
  483. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  484. *
  485. * @return array
  486. */
  487. function cwp7_ClientAreaAllowedFunctions() {
  488. return array(
  489. "Enable SSL" => "enableSSL",
  490. "Renew SSL" => "renewSSL",
  491. "Set DNS" => "setDNS",
  492. "Unset DNS" => "unsetDNS",
  493. "Confirm Enable SSL" => "enableSSLConfirm",
  494. "Confirm Renew SSL" => "renewSSLConfirm",
  495. "Confirm Set DNS" => "setDNSConfirm",
  496. "Confirm Unset DNS" => "unsetDNSConfirm",
  497. "Info DNS" => "infoDNS",
  498. "Info SSL" => "infoSSL",
  499. "Add Domain" => "addDomain",
  500. "Add Subdomain" => "addSubdomain",
  501. "New Subdomain" => "newSubdomain",
  502. "Confirm Delete Domain" => "delDomainConfirm",
  503. "Delete Domain" => "delDomain",
  504. "Confirm Delete Subdomain" => "delSubdomainConfirm",
  505. "Delete Subdomain" => "delSubdomain",
  506. );
  507. }
  508. /**
  509. * Opens a form to add a new domain.
  510. *
  511. * @param array $params common module parameters
  512. *
  513. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  514. *
  515. * @return array template information
  516. */
  517. function cwp7_newDomain($params) {
  518. return array(
  519. 'breadcrumb' => array(
  520. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newDomain' => 'Neue Domain',
  521. ),
  522. 'templatefile' => 'cwp7_add_domain',
  523. );
  524. }
  525. /**
  526. * Adds a new domain to a CWP7 account.
  527. *
  528. * @param array $params common module parameters
  529. *
  530. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  531. *
  532. * @return string "success" or an error message
  533. */
  534. function cwp7_addDomain($params) {
  535. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  536. return 'Error: invalid domain name';
  537. }
  538. $vars['user'] = $params['username'];
  539. $vars['name'] = $_POST['d'];
  540. $vars['type'] = 'domain';
  541. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  542. $response = $cwp7->addDomain($vars);
  543. if($response['status'] != 'OK') {
  544. return 'Error: ' . $response['error_msg'];
  545. }
  546. return 'success';
  547. }
  548. /**
  549. * Opens a form to add a new subdomain to a domain.
  550. *
  551. * @param array $params common module parameters
  552. *
  553. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  554. *
  555. * @return array template information
  556. */
  557. function cwp7_newSubdomain($params) {
  558. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  559. return 'Error: invalid domain name';
  560. }
  561. return array(
  562. 'breadcrumb' => array(
  563. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSubdomain' => 'Neue Subdomain',
  564. ),
  565. 'templatefile' => 'cwp7_add_subdomain',
  566. 'vars' => array(
  567. 'domainselected' => $_POST['d'],
  568. ),
  569. );
  570. }
  571. /**
  572. * Adds a new subdomain to domain of a CWP7 account.
  573. *
  574. * @param array $params common module parameters
  575. *
  576. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  577. *
  578. * @return string "success" or an error message
  579. */
  580. function cwp7_addSubdomain($params) {
  581. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  582. return 'Error: invalid domain name';
  583. }
  584. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  585. return 'Error: invalid subdomain name';
  586. }
  587. if($_POST['s'] == 'www') {
  588. return 'Error: default Subdomain www wurde bereits automatisch erstellt' ;
  589. }
  590. $vars['user'] = $params['username'];
  591. $vars['name'] = $_POST['s'] . '.' . $_POST['d'];
  592. $vars['type'] = 'subdomain';
  593. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  594. $response = $cwp7->addDomain($vars);
  595. if($response['status'] != 'OK') {
  596. return 'Error: ' . $response['error_msg'];
  597. }
  598. return 'success';
  599. }
  600. /**
  601. * Opens a form to delete a domain from a CWP7 account.
  602. *
  603. * @param array $params common module parameters
  604. *
  605. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  606. *
  607. * @return array template information
  608. */
  609. function cwp7_delDomainConfirm($params) {
  610. return array(
  611. 'templatefile' => 'cwp7_del_domain_confirm',
  612. 'vars' => array(
  613. 'deldomain' => $_POST['d'],
  614. ),
  615. );
  616. }
  617. /**
  618. * Removes a domain from a CWP7 account.
  619. *
  620. * @param array $params common module parameters
  621. *
  622. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  623. *
  624. * @return string "success" or an error message
  625. */
  626. function cwp7_delDomain($params) {
  627. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  628. return 'Error: invalid domain name';
  629. }
  630. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  631. $response = $cwp7->getAccount($params['username']);
  632. if($response['status'] != 'OK') {
  633. logModuleCall(
  634. 'cwp7',
  635. __FUNCTION__,
  636. $params,
  637. 'debug',
  638. $response
  639. );
  640. }
  641. $domains = $response['result']['domains'];
  642. $clientdomains = array();
  643. foreach($domains as $domain){
  644. if($domain['domain'] != $params['domain']) {
  645. array_push($clientdomains, $domain['domain']);
  646. }
  647. }
  648. if(!in_array($_POST['d'], $clientdomains)) {
  649. logModuleCall(
  650. 'cwp7',
  651. __FUNCTION__,
  652. $_POST,
  653. 'POST DATA VIOLATION',
  654. $params
  655. );
  656. return 'Error: ' . $_POST['d'] . ' not in client domains';
  657. }
  658. // do delete domain
  659. $vars['user'] = $params['username'];
  660. $vars['name'] = $_POST['d'];
  661. $vars['type'] = 'domain';
  662. $response = $cwp7->deleteDomain($vars);
  663. if($response['status'] != 'OK') {
  664. return 'Error: ' . $response['error_msg'];
  665. }
  666. return 'success';
  667. }
  668. /**
  669. * Opens a form to delete a subdomain from domain of a CWP7 account.
  670. *
  671. * @param array $params common module parameters
  672. *
  673. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  674. *
  675. * @return array template information
  676. */
  677. function cwp7_delSubdomainConfirm($params) {
  678. return array(
  679. 'templatefile' => 'cwp7_del_subdomain_confirm',
  680. 'vars' => array(
  681. 'delsubdomain' => $_POST['d'],
  682. ),
  683. );
  684. }
  685. /**
  686. * Removes a subdomain from a domain of a CWP7 account.
  687. *
  688. * @param array $params common module parameters
  689. *
  690. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  691. *
  692. * @return string "success" or an error message
  693. */
  694. function cwp7_delSubdomain($params) {
  695. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  696. return 'Error: invalid domain name';
  697. }
  698. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  699. $response = $cwp7->getAccount($params['username']);
  700. if($response['status'] != 'OK') {
  701. logModuleCall(
  702. 'cwp7',
  703. __FUNCTION__,
  704. $params,
  705. 'debug',
  706. $response
  707. );
  708. }
  709. $subdomains = $response['result']['subdomins'];
  710. $clientsubdomains = array();
  711. foreach($subdomains as $subdomain){
  712. if($subdomain['domain'] != $params['domain']) {
  713. array_push($clientsubdomains, $subdomain['subdomain'] . "." . $subdomain['domain']);
  714. }
  715. }
  716. if(!in_array($_POST['d'], $clientsubdomains)) {
  717. logModuleCall(
  718. 'cwp7',
  719. __FUNCTION__,
  720. $_POST,
  721. 'POST DATA VIOLATION',
  722. $params
  723. );
  724. return 'Error: ' . $_POST['d'] . ' not in client subdomains';
  725. }
  726. // do delete subdomain
  727. $vars['user'] = $params['username'];
  728. $vars['name'] = $_POST['d'];
  729. $vars['type'] = 'subdomain';
  730. $response = $cwp7->deleteDomain($vars);
  731. if($response['status'] != 'OK') {
  732. return 'Error: ' . $response['error_msg'];
  733. }
  734. return 'success';
  735. }
  736. /**
  737. * Opens a form to enable SSL for a subdomain or domain of a CWP7 account.
  738. *
  739. * @param array $params common module parameters
  740. *
  741. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  742. *
  743. * @return array template information
  744. */
  745. function cwp7_enableSSLConfirm($params) {
  746. return array(
  747. 'templatefile' => 'cwp7_enable_SSL_confirm',
  748. 'vars' => array(
  749. 'SSLdomain' => $_POST['d'],
  750. ),
  751. );
  752. }
  753. /**
  754. * Aktivate CWP7 AutoSSL for a subdomain or domain of a CWP7 account.
  755. *
  756. * @param array $params common module parameters
  757. *
  758. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  759. *
  760. * @return string "success" or an error message
  761. */
  762. function cwp7_enableSSL($params) {
  763. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  764. return 'Error: invalid domain name';
  765. }
  766. $vars['user'] = $params['username'];
  767. $vars['name'] = $_POST['d'];
  768. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  769. $response = $cwp7->addAutoSSL($vars);
  770. if($response['status'] != 'OK') {
  771. return 'Error: ' . $response['error_msg'];
  772. }
  773. return 'success';
  774. }
  775. /**
  776. * Opens a form to renew a SSL certificate for a subdomain or domain of a CWP7 account.
  777. *
  778. * @param array $params common module parameters
  779. *
  780. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  781. *
  782. * @return array template information
  783. */
  784. function cwp7_renewSSLConfirm($params) {
  785. return array(
  786. 'templatefile' => 'cwp7_renew_SSL_confirm',
  787. 'vars' => array(
  788. 'SSLdomain' => $_POST['d'],
  789. ),
  790. );
  791. }
  792. /**
  793. * Renews a SSL certificate for a subdomain or domain of a CWP7 account.
  794. *
  795. * @param array $params common module parameters
  796. *
  797. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  798. *
  799. * @return string "success" or an error message
  800. */
  801. function cwp7_renewSSL($params) {
  802. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  803. return 'Error: invalid domain name';
  804. }
  805. $vars['user'] = $params['username'];
  806. $vars['name'] = $_POST['d'];
  807. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  808. $response = $cwp7->updateAutoSSL($vars);
  809. if($response['status'] != 'OK') {
  810. return 'Error: ' . $response['error_msg'];
  811. }
  812. return 'success';
  813. }
  814. /**
  815. * Opens a form to set a DNS record for a subdomain or domain of a CWP7 account.
  816. *
  817. * @param array $params common module parameters
  818. *
  819. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  820. *
  821. * @return array template information
  822. */
  823. function cwp7_setDNSConfirm($params) {
  824. if(isset($_POST['s'])){
  825. return array(
  826. 'templatefile' => 'cwp7_set_DNS_confirm',
  827. 'vars' => array(
  828. 'DNSdomain' => $_POST['d'],
  829. 'DNSsubdomain' => $_POST['s'],
  830. ),
  831. );
  832. }
  833. return array(
  834. 'templatefile' => 'cwp7_set_DNS_confirm',
  835. 'vars' => array(
  836. 'DNSdomain' => $_POST['d'],
  837. ),
  838. );
  839. }
  840. /**
  841. * Opens a form to unsset a DNS record for a subdomain or domain of a CWP7 account.
  842. *
  843. * @param array $params common module parameters
  844. *
  845. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  846. *
  847. * @return array template information
  848. */
  849. function cwp7_unsetDNSConfirm($params) {
  850. if(isset($_POST['s'])){
  851. return array(
  852. 'templatefile' => 'cwp7_unset_DNS_confirm',
  853. 'vars' => array(
  854. 'DNSdomain' => $_POST['d'],
  855. 'DNSsubdomain' => $_POST['s'],
  856. ),
  857. );
  858. }
  859. return array(
  860. 'templatefile' => 'cwp7_unset_DNS_confirm',
  861. 'vars' => array(
  862. 'DNSdomain' => $_POST['d'],
  863. ),
  864. );
  865. }
  866. /**
  867. * Update a DNS zone for a domain setting a new record for a domain or subdomain of a CWP7 account.
  868. *
  869. * @param array $params common module parameters
  870. *
  871. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  872. *
  873. * @return string "success" or an error message
  874. */
  875. function cwp7_setDNS($params) {
  876. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  877. return 'Error: invalid domain name';
  878. }
  879. $domainName = $_POST['d'];
  880. $zoneRecords = array();
  881. if(isset($_POST['s'])){
  882. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  883. return 'Error: invalid subdomain name';
  884. }
  885. $hostName = $_POST['s'] . '.' . $domainName . '.';
  886. $newRecord = array(
  887. 'line' => $hostName.'|A|0',
  888. 'name' => $hostName,
  889. 'type' => 'A',
  890. 'class' => 'IN',
  891. 'data' => array(
  892. 'address' => $params['serverip'],
  893. ),
  894. );
  895. array_push($zoneRecords, $newRecord);
  896. } else {
  897. $hostName = $domainName . '.';
  898. $domainRecord = array(
  899. 'line' => $hostName.'|A|0',
  900. 'name' => $hostName,
  901. 'type' => 'A',
  902. 'class' => 'IN',
  903. 'data' => array(
  904. 'address' => $params['serverip'],
  905. ),
  906. );
  907. array_push($zoneRecords, $domainRecord);
  908. $wwwRecord = array(
  909. 'line' => 'www'.$hostName.'|A|0',
  910. 'name' => 'www'.$hostName,
  911. 'type' => 'A',
  912. 'class' => 'IN',
  913. 'data' => array(
  914. 'address' => $params['serverip'],
  915. ),
  916. );
  917. array_push($zoneRecords, $wwwRecord);
  918. }
  919. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  920. ->select('id')
  921. ->where('name', '=', $domainName)
  922. ->where('clientid', '=', $params['userid'])
  923. ->get();
  924. $zoneIDobj = $zoneIDcollection[0];
  925. $zoneID = $zoneIDobj->{'id'};
  926. if(!isset($zoneID)) {
  927. return 'Error: Zone for domain ' . $domainName . ' or not owned by client';
  928. }
  929. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  930. foreach($dnsZone['data']->records as $record) {
  931. if(($record->name != $hostName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  932. array_push($zoneRecords, $record);
  933. };
  934. }
  935. $result = localAPI('dnsmanager' ,
  936. array(
  937. 'dnsaction' => 'updateZone',
  938. 'zone_id' => $zoneID,
  939. 'records' => $zoneRecords,
  940. )
  941. );
  942. if($result['result'] != 'success') {
  943. return 'Error: ' . $result['message'];
  944. }
  945. return 'success';
  946. }
  947. /**
  948. * Removing a DNS record for a domain or subdomain of a CWP7 account.
  949. *
  950. * @param array $params common module parameters
  951. *
  952. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  953. *
  954. * @return string "success" or an error message
  955. */
  956. function cwp7_unsetDNS($params) {
  957. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  958. return 'Error: invalid domain name';
  959. }
  960. $domainName = $_POST['d'];
  961. $zoneRecords = array();
  962. if(isset($_POST['s'])){
  963. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  964. return 'Error: invalid subdomain name';
  965. }
  966. $hostName = $_POST['s'] . '.' . $domainName . '.';
  967. } else {
  968. $hostName = $domainName . '.';
  969. }
  970. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  971. ->select('id')
  972. ->where('name', '=', $domainName)
  973. ->where('clientid', '=', $params['userid'])
  974. ->get();
  975. $zoneIDobj = $zoneIDcollection[0];
  976. $zoneID = $zoneIDobj->{'id'};
  977. if(!isset($zoneID)) {
  978. return 'Error: Zone for domain ' . $domainName . ' or not owned by client';
  979. }
  980. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  981. foreach($dnsZone['data']->records as $record) {
  982. if(($record->name != $hostName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  983. array_push($zoneRecords, $record);
  984. };
  985. }
  986. $result = localAPI('dnsmanager' ,
  987. array(
  988. 'dnsaction' => 'updateZone',
  989. 'zone_id' => $zoneID,
  990. 'records' => $zoneRecords,
  991. )
  992. );
  993. if($result['result'] != 'success') {
  994. return 'Error: ' . $result['message'];
  995. }
  996. return 'success';
  997. }
  998. /**
  999. * Opens a form to inform about the DNS status of a subdomain or domain of a CWP7 account.
  1000. *
  1001. * @param array $params common module parameters
  1002. *
  1003. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1004. *
  1005. * @return array template information
  1006. */
  1007. function cwp7_infoDNS($params) {
  1008. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  1009. return 'Error: invalid domain name';
  1010. }
  1011. $cwp7nameserver = cwp7CheckSOA($_POST['d'],$params['configoption5'],$params['configoption6']);
  1012. return array(
  1013. 'templatefile' => 'cwp7_help_dns',
  1014. 'vars' => array(
  1015. 'infodomain' => $_POST['d'],
  1016. 'cwp7nameserver' => $cwp7nameserver,
  1017. ),
  1018. );
  1019. }
  1020. /**
  1021. * Opens a form to inform about the SSL status of a subdomain or domain of a CWP7 account.
  1022. *
  1023. * @param array $params common module parameters
  1024. *
  1025. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1026. *
  1027. * @return array template information
  1028. */
  1029. function cwp7_infoSSL($params) {
  1030. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  1031. return 'Error: invalid domain name';
  1032. }
  1033. return array(
  1034. 'templatefile' => 'cwp7_help_ssl',
  1035. 'vars' => array(
  1036. 'infodomain' => $_POST['d'],
  1037. ),
  1038. );
  1039. }
  1040. /**
  1041. * Ask nameservers for a IP adress of a given host.
  1042. *
  1043. * @param string $host hostname
  1044. * @param string $serverIP CWP7 server IP
  1045. * @param string $nameserverIP polled name server IP
  1046. * @param int $recurse optional -> used to follow CNAME responses
  1047. *
  1048. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1049. *
  1050. * @return bool
  1051. */
  1052. function cwp7CheckA($host, $serverIP, $nameserverIP, $recurse = 0) {
  1053. if($recurse > 3) {
  1054. return false;
  1055. }
  1056. $nameserver = array($nameserverIP);
  1057. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  1058. try {
  1059. $result = $resolver->query($host, 'A');
  1060. } catch(Net_DNS2_Exception $e) {
  1061. logModuleCall(
  1062. 'cwp7',
  1063. __FUNCTION__,
  1064. $e,
  1065. 'DNS lookup exception',
  1066. $e->getMessage()
  1067. );
  1068. }
  1069. $hostA = $result->answer;
  1070. if($hostA[0]->type == 'CNAME') {
  1071. if(cwp7CheckA($hostA[0]->cname, $serverIP, $nameserverIP, $recurse++)) {
  1072. return true;
  1073. }
  1074. }
  1075. if($hostA[0]->type == 'A') {
  1076. if($hostA[0]->address == $serverIP){
  1077. return true;
  1078. }
  1079. }
  1080. return false;
  1081. }
  1082. /**
  1083. * Ask nameservers for the authority of a domain.
  1084. *
  1085. * @param string $domain domain name
  1086. * @param string $nameserverIP polled name server IP
  1087. * @param string $nameserverName name of the own namesever
  1088. *
  1089. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1090. *
  1091. * @return string 'none' -> not registered, 'self' -> registered at own or the name of an other responsible nameserver
  1092. */
  1093. function cwp7CheckSOA($domain, $nameserverIP, $nameserverName ) {
  1094. $nameserver = array($nameserverIP);
  1095. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  1096. try {
  1097. $result = $resolver->query($domain, 'SOA');
  1098. } catch(Net_DNS2_Exception $e) {
  1099. logModuleCall(
  1100. 'cwp7',
  1101. __FUNCTION__,
  1102. $e,
  1103. 'DNS lookup exception',
  1104. $e->getMessage()
  1105. );
  1106. return 'none';
  1107. }
  1108. if($result->answer[0]->mname == $nameserverName) {
  1109. return 'self';
  1110. }
  1111. return $result->answer[0]->mname;
  1112. }