zimbraSingle.php 29 KB

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