zimbraSingle.php 31 KB

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