cwp7.php 35 KB

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