zimbraSingle.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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. $clientInfo = array();
  164. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  165. $login = $api->login();
  166. if(is_a($login, 'Exception')) {
  167. logModuleCall(
  168. 'zimbrasingle',
  169. __FUNCTION__,
  170. $params,
  171. 'Error: cannot login to ' . $accessData['zimbraServer'],
  172. $login->getMessage()
  173. );
  174. return false;
  175. }
  176. $apiAccountManager = new Zm_Account($api);
  177. $accountInfo = $apiAccountManager->getAccountInfo($params['username']);
  178. if(is_a($accountInfo, 'Exception')) {
  179. logModuleCall(
  180. 'zimbrasingle',
  181. __FUNCTION__,
  182. $params,
  183. 'Error: could not gather informations for ' . $params['username'],
  184. $accountInfo
  185. );
  186. return false;
  187. }
  188. $clientInfo['basequota'] = $params['configoption2'] ? $params['configoption2'] : 1;
  189. $clientInfo['addonquota'] = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0;
  190. $clientInfo['userquota'] = $clientInfo['basequota'] + $clientInfo['addonquota'];
  191. $clientInfo['mailaddress'] = $params['username'];
  192. $webmailUrl = zimbraSingleFindAll($accountInfo, 'PUBLICMAILURL');
  193. $clientInfo['webmailurl'] = $webmailUrl[0]['DATA'];
  194. $clientInfo['zimbraserver'] = parse_url($clientInfo['webmailurl'], PHP_URL_HOST);
  195. return array(
  196. 'tabOverviewReplacementTemplate' => 'templates/clientarea',
  197. 'vars' => $clientInfo,
  198. );
  199. }
  200. /**
  201. * Usage Update
  202. *
  203. * Important: Runs daily per server not per product
  204. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  205. * @param array $params common module parameters
  206. *
  207. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  208. */
  209. function zimbraSingle_UsageUpdate($params) {
  210. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  211. $login = $api->login();
  212. if(is_a($login, 'Exception')) {
  213. logModuleCall(
  214. 'zimbrasingle',
  215. __FUNCTION__,
  216. $params,
  217. 'Error: cannot login to ' . $params['serverhostname'],
  218. $login->getMessage()
  219. );
  220. return false;
  221. }
  222. $apiAccountManager = new Zm_Account($api);
  223. $productsObj = Capsule::table('tblhosting')
  224. ->select('*')
  225. ->where('server', '=', $params['serverid'])
  226. ->where('domainstatus', '=', 'Active')
  227. ->get();
  228. foreach((array)$productsObj as $productObj) {
  229. $product = get_object_vars($productObj[0]);
  230. logModuleCall(
  231. 'zimbrasingle',
  232. __FUNCTION__,
  233. $productObj,
  234. 'Debug',
  235. $product
  236. );
  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. * Change the password for a Zimbra account.
  283. *
  284. * Called when a password change is requested. This can occur either due to a
  285. * client requesting it via the client area or an admin requesting it from the
  286. * admin side.
  287. *
  288. * This option is only available to client end users when the product is in an
  289. * active status.
  290. *
  291. * @param array $params common module parameters
  292. *
  293. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  294. *
  295. * @return string 'success' or an error message
  296. */
  297. function zimbraSingle_ChangePassword($params) {
  298. $checkPassword = zimbraSingleCheckPassword($params['password']);
  299. if ($checkPassword != null) {
  300. return $checkPassword;
  301. }
  302. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  303. $login = $api->login();
  304. if(is_a($login, 'Exception')) {
  305. logModuleCall(
  306. 'zimbrasingle',
  307. __FUNCTION__,
  308. $params,
  309. 'Error: cannot login to ' . $params['serverhostname'],
  310. $login->getMessage()
  311. );
  312. return false;
  313. }
  314. $apiAccountManager = new Zm_Account($api);
  315. $response = $apiAccountManager->setAccountPassword($params['username'], $params['password']);
  316. if(is_a($response, 'Exception')) {
  317. logModuleCall(
  318. 'zimbrasingle',
  319. __FUNCTION__,
  320. $params,
  321. 'Error: password could not be set for ' . $params['username'],
  322. $response
  323. );
  324. return false;
  325. }
  326. return 'success';
  327. }
  328. /**
  329. * Provision a new instance of a Zimbra account.
  330. *
  331. * Attempt to provision a new Zimbra mail account. This is
  332. * called any time provisioning is requested inside of WHMCS. Depending upon the
  333. * configuration, this can be any of:
  334. * * When a new order is placed
  335. * * When an invoice for a new order is paid
  336. * * Upon manual request by an admin user
  337. *
  338. * @param array $params common module parameters
  339. *
  340. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  341. *
  342. * @return string 'success' or an error message
  343. */
  344. function zimbraSingle_CreateAccount($params) {
  345. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  346. $login = $api->login();
  347. if(is_a($login, 'Exception')) {
  348. logModuleCall(
  349. 'zimbrasingle',
  350. __FUNCTION__,
  351. $params,
  352. 'Error: cannot login to ' . $params['serverhostname'],
  353. $login->getMessage()
  354. );
  355. return $login->getMessage();
  356. }
  357. $params['username'] = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  358. $apiAccountManager = new Zm_Account($api);
  359. $accountExists = $apiAccountManager->accountExists($params['username']);
  360. if(is_a($accountExists, 'Exception')) {
  361. logModuleCall(
  362. 'zimbrasingle',
  363. __FUNCTION__,
  364. $params,
  365. 'Error: could not verify ' . $params['username'],
  366. $accountExists
  367. );
  368. return 'Error: could not verify '. $params['username'];
  369. }
  370. if($accountExists === true) {
  371. return 'Error: account already exists ' . $params['username'];
  372. }
  373. $attrs = array();
  374. $attrs['gn'] = $params['customfields']['givenname'];
  375. $attrs['sn'] = $params['customfields']['sn'];
  376. $attrs['displayName'] = $attrs['gn'] . ' ' . $attrs['sn'];
  377. $params['password'] = $params['customfields']['password'];
  378. $cosID = $apiAccountManager->getCosId($params['configoption1']);
  379. if(is_a($cosID, 'Exception')) {
  380. logModuleCall(
  381. 'zimbrasingle',
  382. __FUNCTION__,
  383. $params,
  384. 'Error: could not find serviceclass ' . $params['configoption1'],
  385. $cosID
  386. );
  387. return 'Error: could not find serviceclass ' . $params['configoption1'];
  388. }
  389. $attrs['zimbraCOSId'] = $cosID;
  390. $baseQuota = $params['configoption2'] ? $params['configoption2'] : 1;
  391. $addonQuota = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0;
  392. $newAddQuota = $params['configoptions']['newAddQuota'] ? $params['configoptions']['newAddQuota'] : 0;
  393. $attrs['zimbraMailQuota'] = ($baseQuota + $addonQuota + $newAddQuota) * 1073741824;
  394. $zimbraID = $apiAccountManager->createAccount($params['username'], $params['password'], $attrs);
  395. if(is_a($zimbraID, 'Exception')) {
  396. logModuleCall(
  397. 'zimbrasingle',
  398. __FUNCTION__,
  399. $params,
  400. 'Error: could not create account ' . $params['username'],
  401. $zimbraID
  402. );
  403. return 'Error: could not create account ' . $params['username'];
  404. }
  405. try {
  406. Capsule::table('tblhosting')
  407. ->where('id', '=', $params['serviceid'])
  408. ->update(
  409. array(
  410. 'username' => $params['username'],
  411. 'password' => $params['customfields']['password'],
  412. )
  413. );
  414. } catch (\Exception $e) {
  415. logModuleCall(
  416. 'zimbrasingle',
  417. __FUNCTION__,
  418. $params,
  419. 'Error: could save username & password in database',
  420. $e->getMessage()
  421. );
  422. return 'Error: could save username & password in database';
  423. }
  424. if(zimbraSingleUpdateQuota($params) != 'success') {
  425. return 'Error: could not update addonQuota in database';
  426. };
  427. return 'success';
  428. }
  429. /**
  430. * Set a Zimbra account to status locked.
  431. *
  432. * Called when a suspension is requested. This is invoked automatically by WHMCS
  433. * when a product becomes overdue on payment or can be called manually by admin
  434. * user.
  435. *
  436. * @param array $params common module parameters
  437. *
  438. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  439. *
  440. * @return string 'success' or an error message
  441. */
  442. function zimbraSingle_SuspendAccount($params) {
  443. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  444. $login = $api->login();
  445. if(is_a($login, 'Exception')) {
  446. logModuleCall(
  447. 'zimbrasingle',
  448. __FUNCTION__,
  449. $params,
  450. 'Error: cannot login to ' . $params['serverhostname'],
  451. $login->getMessage()
  452. );
  453. return $login->getMessage();
  454. }
  455. $apiAccountManager = new Zm_Account($api);
  456. $response = $apiAccountManager->setAccountStatus($params['username'], 'locked');
  457. if(is_a($response, 'Exception')) {
  458. logModuleCall(
  459. 'zimbrasingle',
  460. __FUNCTION__,
  461. $params,
  462. 'Error: could not lock account ' . $params['username'],
  463. $response
  464. );
  465. return false;
  466. }
  467. return 'success';
  468. }
  469. /**
  470. * Set a Zimbra account to status active.
  471. *
  472. * Called when an un-suspension is requested. This is invoked
  473. * automatically upon payment of an overdue invoice for a product, or
  474. * can be called manually by admin user.
  475. *
  476. * @param array $params common module parameters
  477. *
  478. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  479. *
  480. * @return string 'success' or an error message
  481. */
  482. function zimbraSingle_UnsuspendAccount($params) {
  483. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  484. $login = $api->login();
  485. if(is_a($login, 'Exception')) {
  486. logModuleCall(
  487. 'zimbrasingle',
  488. __FUNCTION__,
  489. $params,
  490. 'Error: cannot login to ' . $params['serverhostname'],
  491. $login->getMessage()
  492. );
  493. return $login->getMessage();
  494. }
  495. $apiAccountManager = new Zm_Account($api);
  496. $response = $apiAccountManager->setAccountStatus($params['username'], 'active');
  497. if(is_a($response, 'Exception')) {
  498. logModuleCall(
  499. 'zimbrasingle',
  500. __FUNCTION__,
  501. $params,
  502. 'Error: could not unlock account ' . $params['username'],
  503. $response
  504. );
  505. return 'Error: could not unlock account ' . $params['username'];
  506. }
  507. return 'success';
  508. }
  509. /**
  510. * Removes a Zimbra account.
  511. *
  512. * Called when a termination is requested. This can be invoked automatically for
  513. * overdue products if enabled, or requested manually by an admin user.
  514. *
  515. * @param array $params common module parameters
  516. *
  517. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  518. *
  519. * @return string 'success' or an error message
  520. */
  521. function zimbraSingle_TerminateAccount($params) {
  522. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  523. $login = $api->login();
  524. if(is_a($login, 'Exception')) {
  525. logModuleCall(
  526. 'zimbrasingle',
  527. __FUNCTION__,
  528. $params,
  529. 'Error: cannot login to ' . $params['serverhostname'],
  530. $login->getMessage()
  531. );
  532. return $login->getMessage();
  533. }
  534. $apiAccountManager = new Zm_Account($api);
  535. $accountStatus = $apiAccountManager->getAccountStatus($params['username']);
  536. if(is_a($accountStatus, 'Exception')) {
  537. logModuleCall(
  538. 'zimbrasingle',
  539. __FUNCTION__,
  540. $params,
  541. 'Error: could not verify account '. $params['username'],
  542. $accountStatus
  543. );
  544. return 'Error : account ' . $params['username'] . ' Name could not verified';
  545. }
  546. if ($accountStatus != 'locked') {
  547. return 'Account '. $params['username'] . ' is active, suspend account first!';
  548. }
  549. $response = $apiAccountManager->deleteAccount($params['username']);
  550. if(is_a($response, 'Exception')) {
  551. logModuleCall(
  552. 'zimbrasingle',
  553. __FUNCTION__,
  554. $params,
  555. 'Error: could not remove account '. $params['username'],
  556. $response
  557. );
  558. return 'Error: could not remove account '. $params['username'];
  559. }
  560. return 'success';
  561. }
  562. /**
  563. * Set a new class of service for a Zimbra account.
  564. *
  565. * Called to apply a change of the class of service. It
  566. * is called to provision upgrade or downgrade orders, as well as being
  567. * able to be invoked manually by an admin user.
  568. *
  569. * This same function is called for upgrades and downgrades of both
  570. * products and configurable options.
  571. *
  572. * @param array $params common module parameters
  573. *
  574. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  575. *
  576. * @return string 'success' or an error message
  577. */
  578. function zimbraSingle_ChangePackage($params) {
  579. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  580. $login = $api->login();
  581. if(is_a($login, 'Exception')) {
  582. logModuleCall(
  583. 'zimbrasingle',
  584. __FUNCTION__,
  585. $params,
  586. 'Error: cannot login to ' . $params['serverhostname'],
  587. $login->getMessage()
  588. );
  589. return $login->getMessage();
  590. }
  591. $apiAccountManager = new Zm_Account($api);
  592. $response = $apiAccountManager->setAccountCos($params['username'], $params['configoption1']);
  593. if(is_a($response, 'Exception')) {
  594. logModuleCall(
  595. 'zimbrasingle',
  596. __FUNCTION__,
  597. $params,
  598. 'Error: could not set class of service for '. $params['username'],
  599. $response
  600. );
  601. return 'Error: could not set class of service for '. $params['username'];
  602. }
  603. $baseQuota = $params['configoption2'] ? $params['configoption2'] : 1;
  604. $addonQuota = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0;
  605. $newAddQuota = $params['configoptions']['newAddQuota'] ? $params['configoptions']['newAddQuota'] : 0;
  606. $attrs['zimbraMailQuota'] = ($baseQuota + $addonQuota +$newAddQuota) * 1073741824;
  607. if(($params['configoptions3'] == 'yes') && ($params['configoptions4'] != '')) {
  608. $attrs['zimbraZimletAvailableZimlets'] = array('!com_zextras_drive_open', '!zimbra_zimlet_nextcloud');
  609. }
  610. $response = $apiAccountManager->modifyAccount($params['username'], $attrs);
  611. if(is_a($response, 'Exception')) {
  612. logModuleCall(
  613. 'zimbrasingle',
  614. __FUNCTION__,
  615. $params,
  616. 'Error: could not update mailbox quota for '. $params['username'],
  617. $response
  618. );
  619. return 'Error: could not update mailbox quota for '. $params['username'];
  620. }
  621. try {
  622. Capsule::table('tblhosting')
  623. ->where('id', '=', $params['serviceid'])
  624. ->update(
  625. array(
  626. 'disklimit' => $attrs['zimbraMailQuota'],
  627. )
  628. );
  629. } catch (\Exception $e) {
  630. logModuleCall(
  631. 'zimbrasingle',
  632. __FUNCTION__,
  633. $params,
  634. 'Error: could not update quota in database',
  635. $e->getMessage()
  636. );
  637. return 'Error: could not update quota in database';
  638. }
  639. if(zimbraSingleUpdateQuota($params) != 'success') {
  640. return 'Error: could not update addonQuota in database';
  641. };
  642. return 'success';
  643. }
  644. /**
  645. * Define Zimbra product configuration options.
  646. *
  647. * Gather classes of service from the Zinbra server.
  648. * Calls a function to create all necessary customfields for the order form using the selected values.
  649. *
  650. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  651. *
  652. * @return array
  653. */
  654. function zimbraSingle_ConfigOptions($params) {
  655. $whmcs = App::self();
  656. $serverGroupID = $whmcs->get_req_var('servergroup');
  657. $serverIDObj = Capsule::table('tblservergroupsrel')
  658. ->select('serverid')
  659. ->where('groupid', '=', $serverGroupID)
  660. ->get();
  661. $serverIDArray = zimbraSingleFindAll($serverIDObj,'serverid');
  662. $server = Capsule::table('tblservers')
  663. ->select('ipaddress', 'username', 'password')
  664. ->where('id', $serverIDArray)
  665. ->where('active', '=', 1)
  666. ->get();
  667. $accessData['zimbraServer'] = $server[0]->ipaddress;
  668. $accessData['adminUser'] = $server[0]->username;
  669. $passDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  670. if ($passDecrypt['result'] == 'success') {
  671. $accessData['adminPass'] = $passDecrypt['password'];
  672. } else {
  673. logModuleCall(
  674. 'zimbrasingle',
  675. __FUNCTION__,
  676. $server,
  677. 'Error: could not decrypt password',
  678. $passDecrypt['message']
  679. );
  680. return false;
  681. }
  682. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], 'admin');
  683. $login = $api->login();
  684. if(is_a($login, 'Exception')) {
  685. logModuleCall(
  686. 'zimbrasingle',
  687. __FUNCTION__,
  688. $server,
  689. 'Error: cannot login to ' . $accessData['zimbraServer'],
  690. $login->getMessage()
  691. );
  692. return false;
  693. }
  694. $apiAccountManager = new Zm_Account($api);
  695. $cosIDs = $apiAccountManager->getAllCos();
  696. if(is_a($cosIDs, 'Exception')) {
  697. logModuleCall(
  698. 'zimbrasingle',
  699. __FUNCTION__,
  700. $params,
  701. 'Error: could not fetch classes of service',
  702. $cosIDs
  703. );
  704. return false;
  705. }
  706. $cosNames = zimbraSingleFindAll($cosIDs, 'NAME');
  707. $configOptions = array();
  708. $configOptions['cos'] = array(
  709. 'FriendlyName' => 'Class of Service',
  710. 'Type' => 'dropdown',
  711. 'Options' => implode(',', $cosNames),
  712. 'Description' => 'Select COS',
  713. );
  714. $configOptions['quota'] = array(
  715. 'Type' => 'text',
  716. 'Description' => 'Basis Mailbox-Quota für dieses Produkt in GB',
  717. 'Default' => '5',
  718. 'Size' => '3',
  719. 'FriendlyName' => 'Mailbox Quota',
  720. );
  721. $configOptions['nc'] = array(
  722. 'Type' => 'yesno',
  723. 'Description' => 'Nextcloud aktiv',
  724. 'FriendlyName' => 'Nextcloud URL anzeigen',
  725. );
  726. $configOptions['ncUrl'] = array(
  727. 'Type' => 'text',
  728. 'Description' => 'Nextcloud URL',
  729. 'Default' => '',
  730. 'Size' => '50',
  731. 'FriendlyName' => 'Nextcloud URL',
  732. );
  733. return $configOptions;
  734. }
  735. /**
  736. * Perform single sign-on for a given instance of a product/service.
  737. *
  738. * Called when single sign-on is requested for an instance of a product/service.
  739. *
  740. * When successful, returns an URL to which the user should be redirected.
  741. *
  742. * @param array $params common module parameters
  743. *
  744. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  745. *
  746. * @return array
  747. */
  748. function zimbraSingle_ServiceSingleSignOn($params) {
  749. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  750. $login = $api->login();
  751. if(is_a($login, 'Exception')) {
  752. logModuleCall(
  753. 'zimbrasingle',
  754. __FUNCTION__,
  755. $params,
  756. 'Error: cannot login to ' . $params['serverhostname'],
  757. $login->getMessage()
  758. );
  759. return array(
  760. 'success' => false,
  761. 'redirectTo' => '',
  762. );
  763. }
  764. $apiDomainManager = new Zm_Domain($api);
  765. $domainOptions = $apiDomainManager->getDomainOptions($params['customfields']['maildomain']);
  766. if(is_a($domainOptions, 'Exception')) {
  767. logModuleCall(
  768. 'zimbrasingle',
  769. __FUNCTION__,
  770. $params,
  771. 'Error : could not fetch options for ' . $params['customfields']['maildomain'],
  772. $domainOptions
  773. );
  774. return array(
  775. 'success' => false,
  776. 'redirectTo' => '',
  777. );
  778. }
  779. $preAuthKey = $domainOptions['zimbraPreAuthKey'];
  780. $apiAccountManager = new Zm_Account($api);
  781. $accountInfo = $apiAccountManager->getAccountInfo($params['username']);
  782. if(is_a($accountInfo, 'Exception')) {
  783. logModuleCall(
  784. 'zimbrasingle',
  785. __FUNCTION__,
  786. $params,
  787. 'Error: could not gather informations for ' . $params['username'],
  788. $accountInfo
  789. );
  790. return array(
  791. 'success' => false,
  792. 'redirectTo' => '',
  793. );
  794. }
  795. $webmailUrl = zimbraSingleFindAll($accountInfo, 'PUBLICMAILURL');
  796. $timestamp=time()*1000;
  797. $preauthToken=hash_hmac('sha1', $params['username'] . '|name|0|' . $timestamp, $preAuthKey);
  798. $preauthURL = $webmailUrl[0]['DATA'] . '/service/preauth?account=' . $params['username'] . '&by=name&timestamp=' . $timestamp .'&expires=0&preauth='. $preauthToken;
  799. return array(
  800. 'success' => true,
  801. 'redirectTo' => $preauthURL,
  802. );
  803. }
  804. /**
  805. * Perform an update of customfields to prevent downgrades.
  806. *
  807. * Called in changePackage or createAccount functions.
  808. *
  809. * @param array $params common module parameters
  810. *
  811. * @return *success* or an error
  812. */
  813. function zimbraSingleUpdateQuota($params) {
  814. if(isset($params['configoptions']['addonQuota'])) {
  815. $addonQuota = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0 ;
  816. $newAddQuota = $params['configoptions']['newAddQuota'] ? $params['configoptions']['newAddQuota'] : 0;
  817. $addonQuota = $addonQuota + $newAddQuota;
  818. $addonQuotaFieldIDObj = Capsule::table('tblproductconfigoptions')
  819. ->join('tblhostingconfigoptions', 'tblproductconfigoptions.id', '=', 'tblhostingconfigoptions.configid')
  820. ->where('tblhostingconfigoptions.relid', '=', $params['serviceid'])
  821. ->where('tblproductconfigoptions.optionname', 'like', 'addonQuota%')
  822. ->select('tblhostingconfigoptions.id', 'tblproductconfigoptions.qtymaximum')
  823. ->get();
  824. if($addonQuota > $addonQuotaFieldIDObj[0]->qtymaximum) {
  825. logModuleCall(
  826. 'zimbrasingle',
  827. __FUNCTION__,
  828. $params,
  829. 'Info: someone is trying to exceed the maximum size',
  830. ''
  831. );
  832. $addonQuota = $addonQuotaFieldIDObj[0]->qtymaximum;
  833. }
  834. try {
  835. $updateAddonQuota = Capsule::table('tblhostingconfigoptions')
  836. ->where('id', $addonQuotaFieldIDObj[0]->id)
  837. ->update(
  838. [
  839. 'qty' => $addonQuota,
  840. ]
  841. );
  842. } catch (\Exception $e) {
  843. logModuleCall(
  844. 'zimbrasingle',
  845. __FUNCTION__,
  846. $updateAddonQuota,
  847. 'Error: could not save addonOuota in database.',
  848. $e->getMessage()
  849. );
  850. return 'Error: could not save addonOuota in database.';
  851. }
  852. $newAddQuotaFieldIDObj = Capsule::table('tblproductconfigoptions')
  853. ->join('tblhostingconfigoptions', 'tblproductconfigoptions.id', '=', 'tblhostingconfigoptions.configid')
  854. ->where('tblhostingconfigoptions.relid', '=', $params['serviceid'])
  855. ->where('tblproductconfigoptions.optionname', 'like', 'newAddQuota%')
  856. ->select('tblhostingconfigoptions.id')
  857. ->get();
  858. try {
  859. $updateNewAddQuota = Capsule::table('tblhostingconfigoptions')
  860. ->where('id', $newAddQuotaFieldIDObj[0]->id)
  861. ->update(
  862. [
  863. 'qty' => '0',
  864. ]
  865. );
  866. } catch (\Exception $e) {
  867. logModuleCall(
  868. 'zimbrasingle',
  869. __FUNCTION__,
  870. $updateNewAddQuota,
  871. 'Error: could not reset newAddOuota in database.',
  872. $e->getMessage()
  873. );
  874. return 'Error: could not reset newAddOuota in database.';
  875. }
  876. }
  877. return 'success';
  878. }