zimbraSingle.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. <?php
  2. /**
  3. * WHMCS Zimbra Provisioning Module
  4. *
  5. * Provisioning for private mailboxes on a Zimbra Server
  6. *
  7. * @see https://www.zimbra.com
  8. * @copyright Copyright (c) Thurdata GmbH 2020
  9. * @license GPL
  10. */
  11. if (!defined('WHMCS')) {
  12. die('This file cannot be accessed directly');
  13. }
  14. use WHMCS\Database\Capsule;
  15. /**
  16. * Requires this PHP api to make soap calls and parse responses
  17. * This is an extend version of:
  18. * @see https://github.com/alloylab/zimbra-admin-api-soap-php
  19. */
  20. require_once(__DIR__ . '/api/Zm/Auth.php');
  21. require_once(__DIR__ . '/api/Zm/Account.php');
  22. require_once(__DIR__ . '/api/Zm/Domain.php');
  23. require_once(__DIR__ . '/api/Zm/Server.php');
  24. /**
  25. * Helper function to find values of a named key in a multidimensional arrays or objects
  26. *
  27. * @param array $haystack mixed data
  28. * @param string $needle key to search for values
  29. * @return array of values
  30. */
  31. function zimbraSingleFindAll($haystack, $needle) {
  32. $values = array();
  33. $iterator = new RecursiveArrayIterator((array)$haystack);
  34. $recursive = new RecursiveIteratorIterator(
  35. $iterator,
  36. RecursiveIteratorIterator::SELF_FIRST
  37. );
  38. foreach ($recursive as $key => $value) {
  39. if ($key === $needle) {
  40. array_push($values, $value);
  41. }
  42. }
  43. return $values;
  44. }
  45. /**
  46. * server side password check
  47. *
  48. * recheck the client side password check
  49. * in case that the client side check has been disabled
  50. *
  51. * @param string $pwd password
  52. *
  53. * @return string missing features or null if the password matches our needs
  54. */
  55. function zimbraSingleCheckPassword($pwd) {
  56. if (strlen($pwd) < 8) {
  57. return 'Das das Passwort ist zu kurz. Es werden mind. 8 Zeichen benötigt';
  58. }
  59. if (!preg_match('#[0-9]+#', $pwd)) {
  60. return 'Das Passwort muss mindestens eine Zahl enthalten';
  61. }
  62. if (!preg_match('#[A-Z]+#', $pwd)) {
  63. return 'Das Passwort muss mindestens einen Grossbuchstaben (A-Z) enthalten';
  64. }
  65. if (!preg_match('#[a-z]+#', $pwd)) {
  66. return 'Das Passwort muss mindestens einen Kleinbuchstaben (a-z) enthalten';
  67. }
  68. if (!preg_match('#[^\w]+#', $pwd)) {
  69. return 'Das Passwort muss mindestens ein Sonderzeichen (.,-:=) enthalten';
  70. }
  71. return null;
  72. }
  73. /**
  74. * Define module related meta data.
  75. *
  76. * Values returned here are used to determine module related abilities and
  77. * settings.
  78. *
  79. * @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
  80. *
  81. * @return array
  82. */
  83. function zimbraSingle_MetaData() {
  84. return array(
  85. 'DisplayName' => 'Zimbra Single Mailbox Provisioning',
  86. 'APIVersion' => '1.2',
  87. 'DefaultNonSSLPort' => '7071',
  88. 'DefaultSSLPort' => '7071',
  89. 'RequiresServer' => true,
  90. 'ServiceSingleSignOnLabel' => 'Login to Zimbra',
  91. 'AdminSingleSignOnLabel' => 'Login to Zimbra Admin'
  92. );
  93. }
  94. /**
  95. * Test connection to a Zimbra server with the given server parameters.
  96. *
  97. * Allows an admin user to verify that an API connection can be
  98. * successfully made with the given configuration parameters for a
  99. * server.
  100. *
  101. * When defined in a module, a Test Connection button will appear
  102. * alongside the Server Type dropdown when adding or editing an
  103. * existing server.
  104. *
  105. * @param array $params common module parameters
  106. *
  107. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  108. *
  109. * @return array
  110. */
  111. function zimbraSingle_TestConnection($params) {
  112. $auth = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  113. $login = $auth->login();
  114. if(is_a($login, 'Exception')) {
  115. logModuleCall(
  116. 'zimbrasingle',
  117. __FUNCTION__,
  118. $params,
  119. 'Connection failed, cannot login to ' . $params['serverhostname'],
  120. $login->getMessage()
  121. );
  122. return array(
  123. 'success' => false,
  124. 'error' => 'Connection failed, cannot login to ' . $params['serverhostname'],
  125. );
  126. }
  127. return array(
  128. 'success' => true,
  129. 'error' => '',
  130. );
  131. }
  132. /**
  133. * Client area output logic handling.
  134. *
  135. * This function is used to define module specific client area output. It should
  136. * return an array consisting of a template file and optional additional
  137. * template variables to make available to that template.
  138. *
  139. * The template file you return can be one of two types:
  140. *
  141. * * tabOverviewModuleOutputTemplate - The output of the template provided here
  142. * will be displayed as part of the default product/service client area
  143. * product overview page.
  144. *
  145. * * tabOverviewReplacementTemplate - Alternatively using this option allows you
  146. * to entirely take control of the product/service overview page within the
  147. * client area.
  148. *
  149. * Whichever option you choose, extra template variables are defined in the same
  150. * way. This demonstrates the use of the full replacement.
  151. *
  152. * Please Note: Using tabOverviewReplacementTemplate means you should display
  153. * the standard information such as pricing and billing details in your custom
  154. * template or they will not be visible to the end user.
  155. *
  156. * @param array $params common module parameters
  157. *
  158. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  159. *
  160. * @return array
  161. */
  162. function zimbraSingle_ClientArea($params) {
  163. logModuleCall(
  164. 'zimbrasingle',
  165. __FUNCTION__,
  166. $params,
  167. 'Debug',
  168. $whmcs
  169. );
  170. $clientInfo = array();
  171. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  172. $login = $api->login();
  173. if(is_a($login, 'Exception')) {
  174. logModuleCall(
  175. 'zimbrasingle',
  176. __FUNCTION__,
  177. $params,
  178. 'Error: cannot login to ' . $accessData['zimbraServer'],
  179. $login->getMessage()
  180. );
  181. return false;
  182. }
  183. $apiAccountManager = new Zm_Account($api);
  184. $accountInfo = $apiAccountManager->getAccountInfo($params['username']);
  185. if(is_a($accountInfo, 'Exception')) {
  186. logModuleCall(
  187. 'zimbrasingle',
  188. __FUNCTION__,
  189. $params,
  190. 'Error: could not gather informations for ' . $params['username'],
  191. $accountInfo
  192. );
  193. return false;
  194. }
  195. $clientInfo['basequota'] = $params['configoption2'] ? $params['configoption2'] : 1;
  196. $clientInfo['addonquota'] = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0;
  197. $clientInfo['userquota'] = $clientInfo['basequota'] + $clientInfo['addonquota'];
  198. $clientInfo['mailaddress'] = $params['username'];
  199. $webmailUrl = zimbraSingleFindAll($accountInfo, 'PUBLICMAILURL');
  200. $clientInfo['webmailurl'] = $webmailUrl[0]['DATA'];
  201. $clientInfo['zimbraserver'] = parse_url($clientInfo['webmailurl'], PHP_URL_HOST);
  202. return array(
  203. 'tabOverviewReplacementTemplate' => 'templates/clientarea',
  204. 'vars' => $clientInfo,
  205. );
  206. }
  207. /**
  208. * Usage Update
  209. *
  210. * Important: Runs daily per server not per product
  211. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  212. * @param array $params common module parameters
  213. *
  214. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  215. */
  216. function zimbraSingle_UsageUpdate($params) {
  217. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  218. $login = $api->login();
  219. if(is_a($login, 'Exception')) {
  220. logModuleCall(
  221. 'zimbrasingle',
  222. __FUNCTION__,
  223. $params,
  224. 'Error: cannot login to ' . $params['serverhostname'],
  225. $login->getMessage()
  226. );
  227. return false;
  228. }
  229. $apiAccountManager = new Zm_Account($api);
  230. $productsObj = Capsule::table('tblhosting')
  231. ->select('*')
  232. ->where('server', '=', $params['serverid'])
  233. ->where('domainstatus', '=', 'Active')
  234. ->get();
  235. foreach((array)$productsObj as $productArray) {
  236. foreach($productArray as $product) {
  237. $accountQuota = $apiAccountManager->getQuota($product->username);
  238. if(is_a($accountQuota, 'Exception')) {
  239. logModuleCall(
  240. 'zimbrasingle',
  241. __FUNCTION__,
  242. $params,
  243. 'Error : could not find quota for ' . $product->username,
  244. $accountQuota
  245. );
  246. continue;
  247. }
  248. $mboxInfo = $apiAccountManager->getMailbox($product->username);
  249. if(is_a($mboxInfo, 'Exception')) {
  250. logModuleCall(
  251. 'zimbrasingle',
  252. __FUNCTION__,
  253. $params,
  254. 'Error: could not fetch mailbox info for ' . $product->username,
  255. $mboxInfo
  256. );
  257. continue;
  258. }
  259. $mboxSize = $mboxInfo['S'];
  260. try {
  261. Capsule::table('tblhosting')
  262. ->where('id', '=', $product->id)
  263. ->update(
  264. array(
  265. 'diskusage' => round($mboxSize / 1048576),
  266. 'disklimit' => round($accountQuota / 1048576),
  267. 'lastupdate' => Capsule::raw('now()')
  268. )
  269. );
  270. } catch (\Exception $e) {
  271. logModuleCall(
  272. 'zimbrasingle',
  273. __FUNCTION__,
  274. $params,
  275. 'Error: could update usage information for ' . $product->username,
  276. $e->getMessage()
  277. );
  278. }
  279. }
  280. }
  281. }
  282. /**
  283. * Change the password for a Zimbra account.
  284. *
  285. * Called when a password change is requested. This can occur either due to a
  286. * client requesting it via the client area or an admin requesting it from the
  287. * admin side.
  288. *
  289. * This option is only available to client end users when the product is in an
  290. * active status.
  291. *
  292. * @param array $params common module parameters
  293. *
  294. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  295. *
  296. * @return string 'success' or an error message
  297. */
  298. function zimbraSingle_ChangePassword($params) {
  299. $checkPassword = zimbraSingleCheckPassword($params['password']);
  300. if ($checkPassword != null) {
  301. return $checkPassword;
  302. }
  303. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  304. $login = $api->login();
  305. if(is_a($login, 'Exception')) {
  306. logModuleCall(
  307. 'zimbrasingle',
  308. __FUNCTION__,
  309. $params,
  310. 'Error: cannot login to ' . $params['serverhostname'],
  311. $login->getMessage()
  312. );
  313. return false;
  314. }
  315. $apiAccountManager = new Zm_Account($api);
  316. $response = $apiAccountManager->setAccountPassword($params['username'], $params['password']);
  317. if(is_a($response, 'Exception')) {
  318. logModuleCall(
  319. 'zimbrasingle',
  320. __FUNCTION__,
  321. $params,
  322. 'Error: password could not be set for ' . $params['username'],
  323. $response
  324. );
  325. return false;
  326. }
  327. return 'success';
  328. }
  329. /**
  330. * Provision a new instance of a Zimbra account.
  331. *
  332. * Attempt to provision a new Zimbra mail account. This is
  333. * called any time provisioning is requested inside of WHMCS. Depending upon the
  334. * configuration, this can be any of:
  335. * * When a new order is placed
  336. * * When an invoice for a new order is paid
  337. * * Upon manual request by an admin user
  338. *
  339. * @param array $params common module parameters
  340. *
  341. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  342. *
  343. * @return string 'success' or an error message
  344. */
  345. function zimbraSingle_CreateAccount($params) {
  346. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  347. $login = $api->login();
  348. if(is_a($login, 'Exception')) {
  349. logModuleCall(
  350. 'zimbrasingle',
  351. __FUNCTION__,
  352. $params,
  353. 'Error: cannot login to ' . $params['serverhostname'],
  354. $login->getMessage()
  355. );
  356. return $login->getMessage();
  357. }
  358. $params['username'] = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  359. $apiAccountManager = new Zm_Account($api);
  360. $accountExists = $apiAccountManager->accountExists($params['username']);
  361. if(is_a($accountExists, 'Exception')) {
  362. logModuleCall(
  363. 'zimbrasingle',
  364. __FUNCTION__,
  365. $params,
  366. 'Error: could not verify ' . $params['username'],
  367. $accountExists
  368. );
  369. return 'Error: could not verify '. $params['username'];
  370. }
  371. if($accountExists === true) {
  372. return 'Error: account already exists ' . $params['username'];
  373. }
  374. $attrs = array();
  375. $attrs['gn'] = $params['customfields']['givenname'];
  376. $attrs['sn'] = $params['customfields']['sn'];
  377. $attrs['displayName'] = $attrs['gn'] . ' ' . $attrs['sn'];
  378. $params['password'] = $params['customfields']['password'];
  379. $cosID = $apiAccountManager->getCosId($params['configoption1']);
  380. if(is_a($cosID, 'Exception')) {
  381. logModuleCall(
  382. 'zimbrasingle',
  383. __FUNCTION__,
  384. $params,
  385. 'Error: could not find serviceclass ' . $params['configoption1'],
  386. $cosID
  387. );
  388. return 'Error: could not find serviceclass ' . $params['configoption1'];
  389. }
  390. $attrs['zimbraCOSId'] = $cosID;
  391. $baseQuota = $params['configoption2'] ? $params['configoption2'] : 1;
  392. $addonQuota = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0;
  393. $newAddQuota = $params['configoptions']['newAddQuota'] ? $params['configoptions']['newAddQuota'] : 0;
  394. $attrs['zimbraMailQuota'] = ($baseQuota + $addonQuota + $newAddQuota) * 1073741824;
  395. $zimbraID = $apiAccountManager->createAccount($params['username'], $params['password'], $attrs);
  396. if(is_a($zimbraID, 'Exception')) {
  397. logModuleCall(
  398. 'zimbrasingle',
  399. __FUNCTION__,
  400. $params,
  401. 'Error: could not create account ' . $params['username'],
  402. $zimbraID
  403. );
  404. return 'Error: could not create account ' . $params['username'];
  405. }
  406. try {
  407. Capsule::table('tblhosting')
  408. ->where('id', '=', $params['serviceid'])
  409. ->update(
  410. array(
  411. 'username' => $params['username'],
  412. 'password' => $params['customfields']['password'],
  413. 'disklimit' => ($baseQuota + $addonQuota + $newAddQuota) * 1024,
  414. )
  415. );
  416. } catch (\Exception $e) {
  417. logModuleCall(
  418. 'zimbrasingle',
  419. __FUNCTION__,
  420. $params,
  421. 'Error: could save username & password in database',
  422. $e->getMessage()
  423. );
  424. return 'Error: could save username & password in database';
  425. }
  426. if(zimbraSingleUpdateQuota($params) != 'success') {
  427. return 'Error: could not update addonQuota in database';
  428. };
  429. return 'success';
  430. }
  431. /**
  432. * Set a Zimbra account to status locked.
  433. *
  434. * Called when a suspension is requested. This is invoked automatically by WHMCS
  435. * when a product becomes overdue on payment or can be called manually by admin
  436. * user.
  437. *
  438. * @param array $params common module parameters
  439. *
  440. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  441. *
  442. * @return string 'success' or an error message
  443. */
  444. function zimbraSingle_SuspendAccount($params) {
  445. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  446. $login = $api->login();
  447. if(is_a($login, 'Exception')) {
  448. logModuleCall(
  449. 'zimbrasingle',
  450. __FUNCTION__,
  451. $params,
  452. 'Error: cannot login to ' . $params['serverhostname'],
  453. $login->getMessage()
  454. );
  455. return $login->getMessage();
  456. }
  457. $apiAccountManager = new Zm_Account($api);
  458. $response = $apiAccountManager->setAccountStatus($params['username'], 'locked');
  459. if(is_a($response, 'Exception')) {
  460. logModuleCall(
  461. 'zimbrasingle',
  462. __FUNCTION__,
  463. $params,
  464. 'Error: could not lock account ' . $params['username'],
  465. $response
  466. );
  467. return false;
  468. }
  469. return 'success';
  470. }
  471. /**
  472. * Set a Zimbra account to status active.
  473. *
  474. * Called when an un-suspension is requested. This is invoked
  475. * automatically upon payment of an overdue invoice for a product, or
  476. * can be called manually by admin user.
  477. *
  478. * @param array $params common module parameters
  479. *
  480. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  481. *
  482. * @return string 'success' or an error message
  483. */
  484. function zimbraSingle_UnsuspendAccount($params) {
  485. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  486. $login = $api->login();
  487. if(is_a($login, 'Exception')) {
  488. logModuleCall(
  489. 'zimbrasingle',
  490. __FUNCTION__,
  491. $params,
  492. 'Error: cannot login to ' . $params['serverhostname'],
  493. $login->getMessage()
  494. );
  495. return $login->getMessage();
  496. }
  497. $apiAccountManager = new Zm_Account($api);
  498. $response = $apiAccountManager->setAccountStatus($params['username'], 'active');
  499. if(is_a($response, 'Exception')) {
  500. logModuleCall(
  501. 'zimbrasingle',
  502. __FUNCTION__,
  503. $params,
  504. 'Error: could not unlock account ' . $params['username'],
  505. $response
  506. );
  507. return 'Error: could not unlock account ' . $params['username'];
  508. }
  509. return 'success';
  510. }
  511. /**
  512. * Removes a Zimbra account.
  513. *
  514. * Called when a termination is requested. This can be invoked automatically for
  515. * overdue products if enabled, or requested manually by an admin user.
  516. *
  517. * @param array $params common module parameters
  518. *
  519. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  520. *
  521. * @return string 'success' or an error message
  522. */
  523. function zimbraSingle_TerminateAccount($params) {
  524. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  525. $login = $api->login();
  526. if(is_a($login, 'Exception')) {
  527. logModuleCall(
  528. 'zimbrasingle',
  529. __FUNCTION__,
  530. $params,
  531. 'Error: cannot login to ' . $params['serverhostname'],
  532. $login->getMessage()
  533. );
  534. return $login->getMessage();
  535. }
  536. $apiAccountManager = new Zm_Account($api);
  537. $accountStatus = $apiAccountManager->getAccountStatus($params['username']);
  538. if(is_a($accountStatus, 'Exception')) {
  539. logModuleCall(
  540. 'zimbrasingle',
  541. __FUNCTION__,
  542. $params,
  543. 'Error: could not verify account '. $params['username'],
  544. $accountStatus
  545. );
  546. return 'Error : account ' . $params['username'] . ' Name could not verified';
  547. }
  548. if ($accountStatus != 'locked') {
  549. return 'Account '. $params['username'] . ' is active, suspend account first!';
  550. }
  551. $response = $apiAccountManager->deleteAccount($params['username']);
  552. if(is_a($response, 'Exception')) {
  553. logModuleCall(
  554. 'zimbrasingle',
  555. __FUNCTION__,
  556. $params,
  557. 'Error: could not remove account '. $params['username'],
  558. $response
  559. );
  560. return 'Error: could not remove account '. $params['username'];
  561. }
  562. return 'success';
  563. }
  564. /**
  565. * Set a new class of service for a Zimbra account.
  566. *
  567. * Called to apply a change of the class of service. It
  568. * is called to provision upgrade or downgrade orders, as well as being
  569. * able to be invoked manually by an admin user.
  570. *
  571. * This same function is called for upgrades and downgrades of both
  572. * products and configurable options.
  573. *
  574. * @param array $params common module parameters
  575. *
  576. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  577. *
  578. * @return string 'success' or an error message
  579. */
  580. function zimbraSingle_ChangePackage($params) {
  581. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  582. $login = $api->login();
  583. if(is_a($login, 'Exception')) {
  584. logModuleCall(
  585. 'zimbrasingle',
  586. __FUNCTION__,
  587. $params,
  588. 'Error: cannot login to ' . $params['serverhostname'],
  589. $login->getMessage()
  590. );
  591. return $login->getMessage();
  592. }
  593. $apiAccountManager = new Zm_Account($api);
  594. $response = $apiAccountManager->setAccountCos($params['username'], $params['configoption1']);
  595. if(is_a($response, 'Exception')) {
  596. logModuleCall(
  597. 'zimbrasingle',
  598. __FUNCTION__,
  599. $params,
  600. 'Error: could not set class of service for '. $params['username'],
  601. $response
  602. );
  603. return 'Error: could not set class of service for '. $params['username'];
  604. }
  605. $baseQuota = $params['configoption2'] ? $params['configoption2'] : 1;
  606. $addonQuota = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0;
  607. $newAddQuota = $params['configoptions']['newAddQuota'] ? $params['configoptions']['newAddQuota'] : 0;
  608. $attrs['zimbraMailQuota'] = ($baseQuota + $addonQuota + $newAddQuota) * 1073741824;
  609. if(($params['configoptions3'] == 'yes') && ($params['configoptions4'] != '')) {
  610. $attrs['zimbraZimletAvailableZimlets'] = array('!com_zextras_drive_open', '!zimbra_zimlet_nextcloud');
  611. }
  612. $response = $apiAccountManager->modifyAccount($params['username'], $attrs);
  613. if(is_a($response, 'Exception')) {
  614. logModuleCall(
  615. 'zimbrasingle',
  616. __FUNCTION__,
  617. $params,
  618. 'Error: could not update mailbox quota for '. $params['username'],
  619. $response
  620. );
  621. return 'Error: could not update mailbox quota for '. $params['username'];
  622. }
  623. try {
  624. Capsule::table('tblhosting')
  625. ->where('id', '=', $params['serviceid'])
  626. ->update(
  627. array(
  628. 'disklimit' => ($baseQuota + $addonQuota + $newAddQuota) * 1024,
  629. )
  630. );
  631. } catch (\Exception $e) {
  632. logModuleCall(
  633. 'zimbrasingle',
  634. __FUNCTION__,
  635. $params,
  636. 'Error: could not update quota in database',
  637. $e->getMessage()
  638. );
  639. return 'Error: could not update quota in database';
  640. }
  641. if(zimbraSingleUpdateQuota($params) != 'success') {
  642. return 'Error: could not update addonQuota in database';
  643. };
  644. return 'success';
  645. }
  646. /**
  647. * Define Zimbra product configuration options.
  648. *
  649. * Gather classes of service from the Zinbra server.
  650. * Calls a function to create all necessary customfields for the order form using the selected values.
  651. *
  652. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  653. *
  654. * @return array
  655. */
  656. function zimbraSingle_ConfigOptions($params) {
  657. $whmcs = App::self();
  658. $serverGroupID = $whmcs->get_req_var('servergroup');
  659. $serverIDObj = Capsule::table('tblservergroupsrel')
  660. ->select('serverid')
  661. ->where('groupid', '=', $serverGroupID)
  662. ->get();
  663. $serverIDArray = zimbraSingleFindAll($serverIDObj,'serverid');
  664. $server = Capsule::table('tblservers')
  665. ->select('ipaddress', 'username', 'password')
  666. ->where('id', $serverIDArray)
  667. ->where('active', '=', 1)
  668. ->get();
  669. $accessData['zimbraServer'] = $server[0]->ipaddress;
  670. $accessData['adminUser'] = $server[0]->username;
  671. $passDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  672. if ($passDecrypt['result'] == 'success') {
  673. $accessData['adminPass'] = $passDecrypt['password'];
  674. } else {
  675. logModuleCall(
  676. 'zimbrasingle',
  677. __FUNCTION__,
  678. $server,
  679. 'Error: could not decrypt password',
  680. $passDecrypt['message']
  681. );
  682. return false;
  683. }
  684. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], 'admin');
  685. $login = $api->login();
  686. if(is_a($login, 'Exception')) {
  687. logModuleCall(
  688. 'zimbrasingle',
  689. __FUNCTION__,
  690. $server,
  691. 'Error: cannot login to ' . $accessData['zimbraServer'],
  692. $login->getMessage()
  693. );
  694. return false;
  695. }
  696. $apiAccountManager = new Zm_Account($api);
  697. $cosIDs = $apiAccountManager->getAllCos();
  698. if(is_a($cosIDs, 'Exception')) {
  699. logModuleCall(
  700. 'zimbrasingle',
  701. __FUNCTION__,
  702. $params,
  703. 'Error: could not fetch classes of service',
  704. $cosIDs
  705. );
  706. return false;
  707. }
  708. $cosNames = zimbraSingleFindAll($cosIDs, 'NAME');
  709. $configOptions = array();
  710. $configOptions['cos'] = array(
  711. 'FriendlyName' => 'Class of Service',
  712. 'Type' => 'dropdown',
  713. 'Options' => implode(',', $cosNames),
  714. 'Description' => 'Select COS',
  715. );
  716. $configOptions['quota'] = array(
  717. 'Type' => 'text',
  718. 'Description' => 'Basis Mailbox-Quota für dieses Produkt in GB',
  719. 'Default' => '5',
  720. 'Size' => '3',
  721. 'FriendlyName' => 'Mailbox Quota',
  722. );
  723. $configOptions['nc'] = array(
  724. 'Type' => 'yesno',
  725. 'Description' => 'Nextcloud aktiv',
  726. 'FriendlyName' => 'Nextcloud URL anzeigen',
  727. );
  728. $configOptions['ncUrl'] = array(
  729. 'Type' => 'text',
  730. 'Description' => 'Nextcloud URL',
  731. 'Default' => '',
  732. 'Size' => '50',
  733. 'FriendlyName' => 'Nextcloud URL',
  734. );
  735. return $configOptions;
  736. }
  737. /**
  738. * Perform single sign-on for a given instance of a product/service.
  739. *
  740. * Called when single sign-on is requested for an instance of a product/service.
  741. *
  742. * When successful, returns an URL to which the user should be redirected.
  743. *
  744. * @param array $params common module parameters
  745. *
  746. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  747. *
  748. * @return array
  749. */
  750. function zimbraSingle_ServiceSingleSignOn($params) {
  751. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  752. $login = $api->login();
  753. if(is_a($login, 'Exception')) {
  754. logModuleCall(
  755. 'zimbrasingle',
  756. __FUNCTION__,
  757. $params,
  758. 'Error: cannot login to ' . $params['serverhostname'],
  759. $login->getMessage()
  760. );
  761. return array(
  762. 'success' => false,
  763. 'redirectTo' => '',
  764. );
  765. }
  766. $apiDomainManager = new Zm_Domain($api);
  767. $domainOptions = $apiDomainManager->getDomainOptions($params['customfields']['maildomain']);
  768. if(is_a($domainOptions, 'Exception')) {
  769. logModuleCall(
  770. 'zimbrasingle',
  771. __FUNCTION__,
  772. $params,
  773. 'Error : could not fetch options for ' . $params['customfields']['maildomain'],
  774. $domainOptions
  775. );
  776. return array(
  777. 'success' => false,
  778. 'redirectTo' => '',
  779. );
  780. }
  781. $preAuthKey = $domainOptions['zimbraPreAuthKey'];
  782. $apiAccountManager = new Zm_Account($api);
  783. $accountInfo = $apiAccountManager->getAccountInfo($params['username']);
  784. if(is_a($accountInfo, 'Exception')) {
  785. logModuleCall(
  786. 'zimbrasingle',
  787. __FUNCTION__,
  788. $params,
  789. 'Error: could not gather informations for ' . $params['username'],
  790. $accountInfo
  791. );
  792. return array(
  793. 'success' => false,
  794. 'redirectTo' => '',
  795. );
  796. }
  797. $webmailUrl = zimbraSingleFindAll($accountInfo, 'PUBLICMAILURL');
  798. $timestamp=time()*1000;
  799. $preauthToken=hash_hmac('sha1', $params['username'] . '|name|0|' . $timestamp, $preAuthKey);
  800. $preauthURL = $webmailUrl[0]['DATA'] . '/service/preauth?account=' . $params['username'] . '&by=name&timestamp=' . $timestamp .'&expires=0&preauth='. $preauthToken;
  801. return array(
  802. 'success' => true,
  803. 'redirectTo' => $preauthURL,
  804. );
  805. }
  806. /**
  807. * Perform an update of customfields to prevent downgrades.
  808. *
  809. * Called in changePackage or createAccount functions.
  810. *
  811. * @param array $params common module parameters
  812. *
  813. * @return *success* or an error
  814. */
  815. function zimbraSingleUpdateQuota($params) {
  816. if(isset($params['configoptions']['addonQuota'])) {
  817. $addonQuota = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0 ;
  818. $newAddQuota = $params['configoptions']['newAddQuota'] ? $params['configoptions']['newAddQuota'] : 0;
  819. $addonQuota = $addonQuota + $newAddQuota;
  820. $addonQuotaFieldIDObj = Capsule::table('tblproductconfigoptions')
  821. ->join('tblhostingconfigoptions', 'tblproductconfigoptions.id', '=', 'tblhostingconfigoptions.configid')
  822. ->where('tblhostingconfigoptions.relid', '=', $params['serviceid'])
  823. ->where('tblproductconfigoptions.optionname', 'like', 'addonQuota%')
  824. ->select('tblhostingconfigoptions.id', 'tblproductconfigoptions.qtymaximum')
  825. ->get();
  826. if($addonQuota > $addonQuotaFieldIDObj[0]->qtymaximum) {
  827. logModuleCall(
  828. 'zimbrasingle',
  829. __FUNCTION__,
  830. $params,
  831. 'Info: someone is trying to exceed the maximum size',
  832. ''
  833. );
  834. $addonQuota = $addonQuotaFieldIDObj[0]->qtymaximum;
  835. }
  836. try {
  837. $updateAddonQuota = Capsule::table('tblhostingconfigoptions')
  838. ->where('id', $addonQuotaFieldIDObj[0]->id)
  839. ->update(
  840. [
  841. 'qty' => $addonQuota,
  842. ]
  843. );
  844. } catch (\Exception $e) {
  845. logModuleCall(
  846. 'zimbrasingle',
  847. __FUNCTION__,
  848. $updateAddonQuota,
  849. 'Error: could not save addonOuota in database.',
  850. $e->getMessage()
  851. );
  852. return 'Error: could not save addonOuota in database.';
  853. }
  854. $newAddQuotaFieldIDObj = Capsule::table('tblproductconfigoptions')
  855. ->join('tblhostingconfigoptions', 'tblproductconfigoptions.id', '=', 'tblhostingconfigoptions.configid')
  856. ->where('tblhostingconfigoptions.relid', '=', $params['serviceid'])
  857. ->where('tblproductconfigoptions.optionname', 'like', 'newAddQuota%')
  858. ->select('tblhostingconfigoptions.id')
  859. ->get();
  860. try {
  861. $updateNewAddQuota = Capsule::table('tblhostingconfigoptions')
  862. ->where('id', $newAddQuotaFieldIDObj[0]->id)
  863. ->update(
  864. [
  865. 'qty' => '0',
  866. ]
  867. );
  868. } catch (\Exception $e) {
  869. logModuleCall(
  870. 'zimbrasingle',
  871. __FUNCTION__,
  872. $updateNewAddQuota,
  873. 'Error: could not reset newAddOuota in database.',
  874. $e->getMessage()
  875. );
  876. return 'Error: could not reset newAddOuota in database.';
  877. }
  878. }
  879. return 'success';
  880. }