cwp7.php 35 KB

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