zimbraSingle.php 29 KB

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