zimbraSingle.php 31 KB

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