cwp7.php 34 KB

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