cwp7.php 33 KB

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