zimbraSingle.php 29 KB

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