cwp7.php 35 KB

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