cwp7.php 35 KB

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