zimbraSingle.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. <?php
  2. /**
  3. * WHMCS Zimbra Provisioning Module
  4. *
  5. * Provisioning for private mailboxes on a Zimbra Server
  6. *
  7. * @see https://www.zimbra.com
  8. * @copyright Copyright (c) Thurdata GmbH 2020
  9. * @license GPL
  10. */
  11. if (!defined('WHMCS')) {
  12. die('This file cannot be accessed directly');
  13. }
  14. use WHMCS\Database\Capsule;
  15. /**
  16. * Requires this PHP api to make soap calls and parse responses
  17. * This is an extend version of:
  18. * @see https://github.com/alloylab/zimbra-admin-api-soap-php
  19. */
  20. require_once(__DIR__ . '/api/Zm/Auth.php');
  21. require_once(__DIR__ . '/api/Zm/Account.php');
  22. require_once(__DIR__ . '/api/Zm/Domain.php');
  23. require_once(__DIR__ . '/api/Zm/Server.php');
  24. /**
  25. * Helper function to find values of a named key in a multidimensional arrays or objects
  26. *
  27. * @param array $haystack mixed data
  28. * @param string $needle key to search for values
  29. * @return array of values
  30. */
  31. function zimbraSingleFindAll($haystack, $needle) {
  32. $values = array();
  33. $iterator = new RecursiveArrayIterator((array)$haystack);
  34. $recursive = new RecursiveIteratorIterator(
  35. $iterator,
  36. RecursiveIteratorIterator::SELF_FIRST
  37. );
  38. foreach ($recursive as $key => $value) {
  39. if ($key === $needle) {
  40. array_push($values, $value);
  41. }
  42. }
  43. return $values;
  44. }
  45. /**
  46. * server side password check
  47. *
  48. * recheck the client side password check
  49. * in case that the client side check has been disabled
  50. *
  51. * @param string $pwd password
  52. *
  53. * @return string missing features or null if the password matches our needs
  54. */
  55. function zimbraSingleCheckPassword($pwd) {
  56. if (strlen($pwd) < 8) {
  57. return 'Das das Passwort ist zu kurz. Es werden mind. 8 Zeichen benötigt';
  58. }
  59. if (!preg_match('#[0-9]+#', $pwd)) {
  60. return 'Das Passwort muss mindestens eine Zahl enthalten';
  61. }
  62. if (!preg_match('#[A-Z]+#', $pwd)) {
  63. return 'Das Passwort muss mindestens einen Grossbuchstaben (A-Z) enthalten';
  64. }
  65. if (!preg_match('#[a-z]+#', $pwd)) {
  66. return 'Das Passwort muss mindestens einen Kleinbuchstaben (a-z) enthalten';
  67. }
  68. if (!preg_match('#[^\w]+#', $pwd)) {
  69. return 'Das Passwort muss mindestens ein Sonderzeichen (.,-:=) enthalten';
  70. }
  71. return null;
  72. }
  73. /**
  74. * Define module related meta data.
  75. *
  76. * Values returned here are used to determine module related abilities and
  77. * settings.
  78. *
  79. * @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
  80. *
  81. * @return array
  82. */
  83. function zimbraSingle_MetaData() {
  84. return array(
  85. 'DisplayName' => 'Zimbra Single Mailbox Provisioning',
  86. 'APIVersion' => '1.2',
  87. 'DefaultNonSSLPort' => '7071',
  88. 'DefaultSSLPort' => '7071',
  89. 'RequiresServer' => true,
  90. 'ServiceSingleSignOnLabel' => 'Login to Zimbra',
  91. 'AdminSingleSignOnLabel' => 'Login to Zimbra Admin'
  92. );
  93. }
  94. /**
  95. * Test connection to a Zimbra server with the given server parameters.
  96. *
  97. * Allows an admin user to verify that an API connection can be
  98. * successfully made with the given configuration parameters for a
  99. * server.
  100. *
  101. * When defined in a module, a Test Connection button will appear
  102. * alongside the Server Type dropdown when adding or editing an
  103. * existing server.
  104. *
  105. * @param array $params common module parameters
  106. *
  107. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  108. *
  109. * @return array
  110. */
  111. function zimbraSingle_TestConnection($params) {
  112. $auth = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  113. $login = $auth->login();
  114. if(is_a($login, 'Exception')) {
  115. logModuleCall(
  116. 'zimbrasingle',
  117. __FUNCTION__,
  118. $params,
  119. 'Connection failed, cannot login to ' . $params['serverhostname'],
  120. $login->getMessage()
  121. );
  122. return array(
  123. 'success' => false,
  124. 'error' => 'Connection failed, cannot login to ' . $params['serverhostname'],
  125. );
  126. }
  127. return array(
  128. 'success' => true,
  129. 'error' => '',
  130. );
  131. }
  132. /**
  133. * Client area output logic handling.
  134. *
  135. * This function is used to define module specific client area output. It should
  136. * return an array consisting of a template file and optional additional
  137. * template variables to make available to that template.
  138. *
  139. * The template file you return can be one of two types:
  140. *
  141. * * tabOverviewModuleOutputTemplate - The output of the template provided here
  142. * will be displayed as part of the default product/service client area
  143. * product overview page.
  144. *
  145. * * tabOverviewReplacementTemplate - Alternatively using this option allows you
  146. * to entirely take control of the product/service overview page within the
  147. * client area.
  148. *
  149. * Whichever option you choose, extra template variables are defined in the same
  150. * way. This demonstrates the use of the full replacement.
  151. *
  152. * Please Note: Using tabOverviewReplacementTemplate means you should display
  153. * the standard information such as pricing and billing details in your custom
  154. * template or they will not be visible to the end user.
  155. *
  156. * @param array $params common module parameters
  157. *
  158. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  159. *
  160. * @return array
  161. */
  162. function zimbraSingle_ClientArea($params) {
  163. $clientInfo = array();
  164. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  165. $login = $api->login();
  166. if(is_a($login, 'Exception')) {
  167. logModuleCall(
  168. 'zimbrasingle',
  169. __FUNCTION__,
  170. $params,
  171. 'Error: cannot login to ' . $accessData['zimbraServer'],
  172. $login->getMessage()
  173. );
  174. return false;
  175. }
  176. $apiAccountManager = new Zm_Account($api);
  177. $accountInfo = $apiAccountManager->getAccountInfo($params['username']);
  178. if(is_a($accountInfo, 'Exception')) {
  179. logModuleCall(
  180. 'zimbrasingle',
  181. __FUNCTION__,
  182. $params,
  183. 'Error: could not gather informations for ' . $params['username'],
  184. $accountInfo
  185. );
  186. return false;
  187. }
  188. $clientInfo['basequota'] = $params['configoption2'] ? $params['configoption2'] : 1;
  189. $clientInfo['addonquota'] = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0;
  190. $clientInfo['userquota'] = $clientInfo['basequota'] + $clientInfo['addonquota'];
  191. $clientInfo['mailaddress'] = $params['username'];
  192. $webmailUrl = zimbraSingleFindAll($accountInfo, 'PUBLICMAILURL');
  193. $clientInfo['webmailurl'] = $webmailUrl[0]['DATA'];
  194. $clientInfo['zimbraserver'] = parse_url($clientInfo['webmailurl'], PHP_URL_HOST);
  195. return array(
  196. 'tabOverviewReplacementTemplate' => 'templates/clientarea',
  197. 'vars' => $clientInfo,
  198. );
  199. }
  200. /**
  201. * Usage Update
  202. *
  203. * Important: Runs daily per server not per product
  204. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  205. * @param array $params common module parameters
  206. *
  207. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  208. */
  209. function zimbraSingle_UsageUpdate($params) {
  210. error_log("Zimbra Single: Update: Staring Usage Update");
  211. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  212. $login = $api->login();
  213. if(is_a($login, 'Exception')) {
  214. error_log("Zimbra Single: Update: Cannot Login to server: " . $params['severhostname'] . " width " . $params['severusername']);
  215. logModuleCall(
  216. 'zimbrasingle',
  217. __FUNCTION__,
  218. $params,
  219. 'Error: cannot login to ' . $params['serverhostname'],
  220. $login->getMessage()
  221. );
  222. return false;
  223. }
  224. $apiAccountManager = new Zm_Account($api);
  225. $productsObj = Capsule::table('tblhosting')
  226. ->select('*')
  227. ->where('server', '=', $params['serverid'])
  228. ->where('domainstatus', '=', 'Active')
  229. ->get();
  230. foreach((array)$productsObj as $productArray) {
  231. foreach($productArray as $product) {
  232. $accountQuota = $apiAccountManager->getQuota($product->username);
  233. if(is_a($accountQuota, 'Exception')) {
  234. error_log("Zimbra Single: Update: Error while getting quota for: " . $product->username);
  235. logModuleCall(
  236. 'zimbrasingle',
  237. __FUNCTION__,
  238. $params,
  239. 'Error : could not find quota for ' . $product->username,
  240. $accountQuota
  241. );
  242. continue;
  243. }
  244. $mboxInfo = $apiAccountManager->getMailbox($product->username);
  245. if(is_a($mboxInfo, 'Exception')) {
  246. error_log("Zimbra Single: Update: Error while getting mailbox info for: " . $product->username);
  247. logModuleCall(
  248. 'zimbrasingle',
  249. __FUNCTION__,
  250. $params,
  251. 'Error: could not fetch mailbox info for ' . $product->username,
  252. $mboxInfo
  253. );
  254. continue;
  255. }
  256. $mboxSize = $mboxInfo['GETMAILBOXRESPONSE']['MBOX']['S'];
  257. try {
  258. Capsule::table('tblhosting')
  259. ->where('id', '=', $product->id)
  260. ->update(
  261. array(
  262. 'diskusage' => round($mboxSize / 1048576),
  263. 'disklimit' => round($accountQuota / 1048576),
  264. 'lastupdate' => Capsule::raw('now()')
  265. )
  266. );
  267. } catch (\Exception $e) {
  268. logModuleCall(
  269. 'zimbrasingle',
  270. __FUNCTION__,
  271. $params,
  272. 'Error: could update usage information for ' . $product->username,
  273. $e->getMessage()
  274. );
  275. }
  276. }
  277. }
  278. }
  279. /**
  280. * Change the password for a Zimbra account.
  281. *
  282. * Called when a password change is requested. This can occur either due to a
  283. * client requesting it via the client area or an admin requesting it from the
  284. * admin side.
  285. *
  286. * This option is only available to client end users when the product is in an
  287. * active status.
  288. *
  289. * @param array $params common module parameters
  290. *
  291. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  292. *
  293. * @return string 'success' or an error message
  294. */
  295. function zimbraSingle_ChangePassword($params) {
  296. $checkPassword = zimbraSingleCheckPassword($params['password']);
  297. if ($checkPassword != null) {
  298. return $checkPassword;
  299. }
  300. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  301. $login = $api->login();
  302. if(is_a($login, 'Exception')) {
  303. logModuleCall(
  304. 'zimbrasingle',
  305. __FUNCTION__,
  306. $params,
  307. 'Error: cannot login to ' . $params['serverhostname'],
  308. $login->getMessage()
  309. );
  310. return false;
  311. }
  312. $apiAccountManager = new Zm_Account($api);
  313. $response = $apiAccountManager->setAccountPassword($params['username'], $params['password']);
  314. if(is_a($response, 'Exception')) {
  315. logModuleCall(
  316. 'zimbrasingle',
  317. __FUNCTION__,
  318. $params,
  319. 'Error: password could not be set for ' . $params['username'],
  320. $response
  321. );
  322. return false;
  323. }
  324. return 'success';
  325. }
  326. /**
  327. * Provision a new instance of a Zimbra account.
  328. *
  329. * Attempt to provision a new Zimbra mail account. This is
  330. * called any time provisioning is requested inside of WHMCS. Depending upon the
  331. * configuration, this can be any of:
  332. * * When a new order is placed
  333. * * When an invoice for a new order is paid
  334. * * Upon manual request by an admin user
  335. *
  336. * @param array $params common module parameters
  337. *
  338. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  339. *
  340. * @return string 'success' or an error message
  341. */
  342. function zimbraSingle_CreateAccount($params) {
  343. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  344. $login = $api->login();
  345. if(is_a($login, 'Exception')) {
  346. logModuleCall(
  347. 'zimbrasingle',
  348. __FUNCTION__,
  349. $params,
  350. 'Error: cannot login to ' . $params['serverhostname'],
  351. $login->getMessage()
  352. );
  353. return $login->getMessage();
  354. }
  355. $params['username'] = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  356. $apiAccountManager = new Zm_Account($api);
  357. $accountExists = $apiAccountManager->accountExists($params['username']);
  358. if(is_a($accountExists, 'Exception')) {
  359. logModuleCall(
  360. 'zimbrasingle',
  361. __FUNCTION__,
  362. $params,
  363. 'Error: could not verify ' . $params['username'],
  364. $accountExists
  365. );
  366. return 'Error: could not verify '. $params['username'];
  367. }
  368. if($accountExists === true) {
  369. return 'Error: account already exists ' . $params['username'];
  370. }
  371. $attrs = array();
  372. $attrs['gn'] = $params['customfields']['givenname'];
  373. $attrs['sn'] = $params['customfields']['sn'];
  374. $attrs['displayName'] = $attrs['gn'] . ' ' . $attrs['sn'];
  375. $params['password'] = $params['customfields']['password'];
  376. $cosID = $apiAccountManager->getCosId($params['configoption1']);
  377. if(is_a($cosID, 'Exception')) {
  378. logModuleCall(
  379. 'zimbrasingle',
  380. __FUNCTION__,
  381. $params,
  382. 'Error: could not find serviceclass ' . $params['configoption1'],
  383. $cosID
  384. );
  385. return 'Error: could not find serviceclass ' . $params['configoption1'];
  386. }
  387. $attrs['zimbraCOSId'] = $cosID;
  388. $baseQuota = $params['configoption2'] ? $params['configoption2'] : 1;
  389. $addonQuota = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0;
  390. $newAddQuota = $params['configoptions']['newAddQuota'] ? $params['configoptions']['newAddQuota'] : 0;
  391. $attrs['zimbraMailQuota'] = ($baseQuota + $addonQuota + $newAddQuota) * 1073741824;
  392. $zimbraID = $apiAccountManager->createAccount($params['username'], $params['password'], $attrs);
  393. if(is_a($zimbraID, 'Exception')) {
  394. logModuleCall(
  395. 'zimbrasingle',
  396. __FUNCTION__,
  397. $params,
  398. 'Error: could not create account ' . $params['username'],
  399. $zimbraID
  400. );
  401. return 'Error: could not create account ' . $params['username'];
  402. }
  403. try {
  404. Capsule::table('tblhosting')
  405. ->where('id', '=', $params['serviceid'])
  406. ->update(
  407. array(
  408. 'username' => $params['username'],
  409. 'password' => $params['customfields']['password'],
  410. 'disklimit' => ($baseQuota + $addonQuota + $newAddQuota) * 1024,
  411. )
  412. );
  413. } catch (\Exception $e) {
  414. logModuleCall(
  415. 'zimbrasingle',
  416. __FUNCTION__,
  417. $params,
  418. 'Error: could save username & password in database',
  419. $e->getMessage()
  420. );
  421. return 'Error: could save username & password in database';
  422. }
  423. if(zimbraSingleUpdateQuota($params) != 'success') {
  424. return 'Error: could not update addonQuota in database';
  425. };
  426. return 'success';
  427. }
  428. /**
  429. * Set a Zimbra account to status locked.
  430. *
  431. * Called when a suspension is requested. This is invoked automatically by WHMCS
  432. * when a product becomes overdue on payment or can be called manually by admin
  433. * user.
  434. *
  435. * @param array $params common module parameters
  436. *
  437. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  438. *
  439. * @return string 'success' or an error message
  440. */
  441. function zimbraSingle_SuspendAccount($params) {
  442. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  443. $login = $api->login();
  444. if(is_a($login, 'Exception')) {
  445. logModuleCall(
  446. 'zimbrasingle',
  447. __FUNCTION__,
  448. $params,
  449. 'Error: cannot login to ' . $params['serverhostname'],
  450. $login->getMessage()
  451. );
  452. return $login->getMessage();
  453. }
  454. $apiAccountManager = new Zm_Account($api);
  455. $response = $apiAccountManager->setAccountStatus($params['username'], 'locked');
  456. if(is_a($response, 'Exception')) {
  457. logModuleCall(
  458. 'zimbrasingle',
  459. __FUNCTION__,
  460. $params,
  461. 'Error: could not lock account ' . $params['username'],
  462. $response
  463. );
  464. return false;
  465. }
  466. return 'success';
  467. }
  468. /**
  469. * Set a Zimbra account to status active.
  470. *
  471. * Called when an un-suspension is requested. This is invoked
  472. * automatically upon payment of an overdue invoice for a product, or
  473. * can be called manually by admin user.
  474. *
  475. * @param array $params common module parameters
  476. *
  477. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  478. *
  479. * @return string 'success' or an error message
  480. */
  481. function zimbraSingle_UnsuspendAccount($params) {
  482. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  483. $login = $api->login();
  484. if(is_a($login, 'Exception')) {
  485. logModuleCall(
  486. 'zimbrasingle',
  487. __FUNCTION__,
  488. $params,
  489. 'Error: cannot login to ' . $params['serverhostname'],
  490. $login->getMessage()
  491. );
  492. return $login->getMessage();
  493. }
  494. $apiAccountManager = new Zm_Account($api);
  495. $response = $apiAccountManager->setAccountStatus($params['username'], 'active');
  496. if(is_a($response, 'Exception')) {
  497. logModuleCall(
  498. 'zimbrasingle',
  499. __FUNCTION__,
  500. $params,
  501. 'Error: could not unlock account ' . $params['username'],
  502. $response
  503. );
  504. return 'Error: could not unlock account ' . $params['username'];
  505. }
  506. return 'success';
  507. }
  508. /**
  509. * Removes a Zimbra account.
  510. *
  511. * Called when a termination is requested. This can be invoked automatically for
  512. * overdue products if enabled, or requested manually by an admin user.
  513. *
  514. * @param array $params common module parameters
  515. *
  516. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  517. *
  518. * @return string 'success' or an error message
  519. */
  520. function zimbraSingle_TerminateAccount($params) {
  521. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  522. $login = $api->login();
  523. if(is_a($login, 'Exception')) {
  524. logModuleCall(
  525. 'zimbrasingle',
  526. __FUNCTION__,
  527. $params,
  528. 'Error: cannot login to ' . $params['serverhostname'],
  529. $login->getMessage()
  530. );
  531. return $login->getMessage();
  532. }
  533. $apiAccountManager = new Zm_Account($api);
  534. $accountStatus = $apiAccountManager->getAccountStatus($params['username']);
  535. if(is_a($accountStatus, 'Exception')) {
  536. logModuleCall(
  537. 'zimbrasingle',
  538. __FUNCTION__,
  539. $params,
  540. 'Error: could not verify account '. $params['username'],
  541. $accountStatus
  542. );
  543. return 'Error : account ' . $params['username'] . ' Name could not verified';
  544. }
  545. //if ($accountStatus != 'locked') {
  546. // return 'Account '. $params['username'] . ' is active, suspend account first!';
  547. //}
  548. $response = $apiAccountManager->deleteAccount($params['username']);
  549. if(is_a($response, 'Exception')) {
  550. logModuleCall(
  551. 'zimbrasingle',
  552. __FUNCTION__,
  553. $params,
  554. 'Error: could not remove account '. $params['username'],
  555. $response
  556. );
  557. return 'Error: could not remove account '. $params['username'];
  558. }
  559. return 'success';
  560. }
  561. /**
  562. * Set a new class of service for a Zimbra account.
  563. *
  564. * Called to apply a change of the class of service. It
  565. * is called to provision upgrade or downgrade orders, as well as being
  566. * able to be invoked manually by an admin user.
  567. *
  568. * This same function is called for upgrades and downgrades of both
  569. * products and configurable options.
  570. *
  571. * @param array $params common module parameters
  572. *
  573. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  574. *
  575. * @return string 'success' or an error message
  576. */
  577. function zimbraSingle_ChangePackage($params) {
  578. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  579. $login = $api->login();
  580. if(is_a($login, 'Exception')) {
  581. logModuleCall(
  582. 'zimbrasingle',
  583. __FUNCTION__,
  584. $params,
  585. 'Error: cannot login to ' . $params['serverhostname'],
  586. $login->getMessage()
  587. );
  588. return $login->getMessage();
  589. }
  590. $apiAccountManager = new Zm_Account($api);
  591. $response = $apiAccountManager->setAccountCos($params['username'], $params['configoption1']);
  592. if(is_a($response, 'Exception')) {
  593. logModuleCall(
  594. 'zimbrasingle',
  595. __FUNCTION__,
  596. $params,
  597. 'Error: could not set class of service for '. $params['username'],
  598. $response
  599. );
  600. return 'Error: could not set class of service for '. $params['username'];
  601. }
  602. $baseQuota = $params['configoption2'] ? $params['configoption2'] : 1;
  603. $addonQuota = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0;
  604. $newAddQuota = $params['configoptions']['newAddQuota'] ? $params['configoptions']['newAddQuota'] : 0;
  605. $attrs['zimbraMailQuota'] = ($baseQuota + $addonQuota + $newAddQuota) * 1073741824;
  606. if(($params['configoptions3'] == 'yes') && ($params['configoptions4'] != '')) {
  607. $attrs['zimbraZimletAvailableZimlets'] = array('!com_zextras_drive_open', '!zimbra_zimlet_nextcloud');
  608. }
  609. $response = $apiAccountManager->modifyAccount($params['username'], $attrs);
  610. if(is_a($response, 'Exception')) {
  611. logModuleCall(
  612. 'zimbrasingle',
  613. __FUNCTION__,
  614. $params,
  615. 'Error: could not update mailbox quota for '. $params['username'],
  616. $response
  617. );
  618. return 'Error: could not update mailbox quota for '. $params['username'];
  619. }
  620. try {
  621. Capsule::table('tblhosting')
  622. ->where('id', '=', $params['serviceid'])
  623. ->update(
  624. array(
  625. 'disklimit' => ($baseQuota + $addonQuota + $newAddQuota) * 1024,
  626. )
  627. );
  628. } catch (\Exception $e) {
  629. logModuleCall(
  630. 'zimbrasingle',
  631. __FUNCTION__,
  632. $params,
  633. 'Error: could not update quota in database',
  634. $e->getMessage()
  635. );
  636. return 'Error: could not update quota in database';
  637. }
  638. if(zimbraSingleUpdateQuota($params) != 'success') {
  639. return 'Error: could not update addonQuota in database';
  640. };
  641. return 'success';
  642. }
  643. /**
  644. * Define Zimbra product configuration options.
  645. *
  646. * Gather classes of service from the Zinbra server.
  647. * Calls a function to create all necessary customfields for the order form using the selected values.
  648. *
  649. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  650. *
  651. * @return array
  652. */
  653. function zimbraSingle_ConfigOptions($params) {
  654. $whmcs = App::self();
  655. $serverGroupID = $whmcs->get_req_var('servergroup');
  656. $serverIDObj = Capsule::table('tblservergroupsrel')
  657. ->select('serverid')
  658. ->where('groupid', '=', $serverGroupID)
  659. ->get();
  660. $serverIDArray = zimbraSingleFindAll($serverIDObj,'serverid');
  661. $server = Capsule::table('tblservers')
  662. ->select('ipaddress', 'username', 'password')
  663. ->where('id', $serverIDArray)
  664. ->where('active', '=', 1)
  665. ->get();
  666. $accessData['zimbraServer'] = $server[0]->ipaddress;
  667. $accessData['adminUser'] = $server[0]->username;
  668. $passDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  669. if ($passDecrypt['result'] == 'success') {
  670. $accessData['adminPass'] = $passDecrypt['password'];
  671. } else {
  672. logModuleCall(
  673. 'zimbrasingle',
  674. __FUNCTION__,
  675. $server,
  676. 'Error: could not decrypt password',
  677. $passDecrypt['message']
  678. );
  679. return false;
  680. }
  681. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], 'admin');
  682. $login = $api->login();
  683. if(is_a($login, 'Exception')) {
  684. logModuleCall(
  685. 'zimbrasingle',
  686. __FUNCTION__,
  687. $server,
  688. 'Error: cannot login to ' . $accessData['zimbraServer'],
  689. $login->getMessage()
  690. );
  691. return false;
  692. }
  693. $apiAccountManager = new Zm_Account($api);
  694. $cosIDs = $apiAccountManager->getAllCos();
  695. if(is_a($cosIDs, 'Exception')) {
  696. logModuleCall(
  697. 'zimbrasingle',
  698. __FUNCTION__,
  699. $params,
  700. 'Error: could not fetch classes of service',
  701. $cosIDs
  702. );
  703. return false;
  704. }
  705. $cosNames = zimbraSingleFindAll($cosIDs, 'NAME');
  706. $configOptions = array();
  707. $configOptions['cos'] = array(
  708. 'FriendlyName' => 'Class of Service',
  709. 'Type' => 'dropdown',
  710. 'Options' => implode(',', $cosNames),
  711. 'Description' => 'Select COS',
  712. );
  713. $configOptions['quota'] = array(
  714. 'Type' => 'text',
  715. 'Description' => 'Basis Mailbox-Quota für dieses Produkt in GB',
  716. 'Default' => '5',
  717. 'Size' => '3',
  718. 'FriendlyName' => 'Mailbox Quota',
  719. );
  720. $configOptions['nc'] = array(
  721. 'Type' => 'yesno',
  722. 'Description' => 'Nextcloud aktiv',
  723. 'FriendlyName' => 'Nextcloud URL anzeigen',
  724. );
  725. $configOptions['ncUrl'] = array(
  726. 'Type' => 'text',
  727. 'Description' => 'Nextcloud URL',
  728. 'Default' => '',
  729. 'Size' => '50',
  730. 'FriendlyName' => 'Nextcloud URL',
  731. );
  732. return $configOptions;
  733. }
  734. /**
  735. * Perform single sign-on for a given instance of a product/service.
  736. *
  737. * Called when single sign-on is requested for an instance of a product/service.
  738. *
  739. * When successful, returns an URL to which the user should be redirected.
  740. *
  741. * @param array $params common module parameters
  742. *
  743. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  744. *
  745. * @return array
  746. */
  747. function zimbraSingle_ServiceSingleSignOn($params) {
  748. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  749. $login = $api->login();
  750. if(is_a($login, 'Exception')) {
  751. logModuleCall(
  752. 'zimbrasingle',
  753. __FUNCTION__,
  754. $params,
  755. 'Error: cannot login to ' . $params['serverhostname'],
  756. $login->getMessage()
  757. );
  758. return array(
  759. 'success' => false,
  760. 'redirectTo' => '',
  761. );
  762. }
  763. $apiDomainManager = new Zm_Domain($api);
  764. $domainOptions = $apiDomainManager->getDomainOptions($params['customfields']['maildomain']);
  765. if(is_a($domainOptions, 'Exception')) {
  766. logModuleCall(
  767. 'zimbrasingle',
  768. __FUNCTION__,
  769. $params,
  770. 'Error : could not fetch options for ' . $params['customfields']['maildomain'],
  771. $domainOptions
  772. );
  773. return array(
  774. 'success' => false,
  775. 'redirectTo' => '',
  776. );
  777. }
  778. $preAuthKey = $domainOptions['zimbraPreAuthKey'];
  779. $apiAccountManager = new Zm_Account($api);
  780. $accountInfo = $apiAccountManager->getAccountInfo($params['username']);
  781. if(is_a($accountInfo, 'Exception')) {
  782. logModuleCall(
  783. 'zimbrasingle',
  784. __FUNCTION__,
  785. $params,
  786. 'Error: could not gather informations for ' . $params['username'],
  787. $accountInfo
  788. );
  789. return array(
  790. 'success' => false,
  791. 'redirectTo' => '',
  792. );
  793. }
  794. $webmailUrl = zimbraSingleFindAll($accountInfo, 'PUBLICMAILURL');
  795. $timestamp=time()*1000;
  796. $preauthToken=hash_hmac('sha1', $params['username'] . '|name|0|' . $timestamp, $preAuthKey);
  797. $preauthURL = $webmailUrl[0]['DATA'] . '/service/preauth?account=' . $params['username'] . '&by=name&timestamp=' . $timestamp .'&expires=0&preauth='. $preauthToken;
  798. return array(
  799. 'success' => true,
  800. 'redirectTo' => $preauthURL,
  801. );
  802. }
  803. /**
  804. * Perform an update of customfields to prevent downgrades.
  805. *
  806. * Called in changePackage or createAccount functions.
  807. *
  808. * @param array $params common module parameters
  809. *
  810. * @return *success* or an error
  811. */
  812. function zimbraSingleUpdateQuota($params) {
  813. if(isset($params['configoptions']['addonQuota'])) {
  814. $addonQuota = $params['configoptions']['addonQuota'] ? $params['configoptions']['addonQuota'] : 0 ;
  815. $newAddQuota = $params['configoptions']['newAddQuota'] ? $params['configoptions']['newAddQuota'] : 0;
  816. $addonQuota = $addonQuota + $newAddQuota;
  817. $addonQuotaFieldIDObj = Capsule::table('tblproductconfigoptions')
  818. ->join('tblhostingconfigoptions', 'tblproductconfigoptions.id', '=', 'tblhostingconfigoptions.configid')
  819. ->where('tblhostingconfigoptions.relid', '=', $params['serviceid'])
  820. ->where('tblproductconfigoptions.optionname', 'like', 'addonQuota%')
  821. ->select('tblhostingconfigoptions.id', 'tblproductconfigoptions.qtymaximum')
  822. ->get();
  823. if($addonQuota > $addonQuotaFieldIDObj[0]->qtymaximum) {
  824. logModuleCall(
  825. 'zimbrasingle',
  826. __FUNCTION__,
  827. $params,
  828. 'Info: someone is trying to exceed the maximum size',
  829. ''
  830. );
  831. $addonQuota = $addonQuotaFieldIDObj[0]->qtymaximum;
  832. }
  833. try {
  834. $updateAddonQuota = Capsule::table('tblhostingconfigoptions')
  835. ->where('id', $addonQuotaFieldIDObj[0]->id)
  836. ->update(
  837. [
  838. 'qty' => $addonQuota,
  839. ]
  840. );
  841. } catch (\Exception $e) {
  842. logModuleCall(
  843. 'zimbrasingle',
  844. __FUNCTION__,
  845. $updateAddonQuota,
  846. 'Error: could not save addonOuota in database.',
  847. $e->getMessage()
  848. );
  849. return 'Error: could not save addonOuota in database.';
  850. }
  851. $newAddQuotaFieldIDObj = Capsule::table('tblproductconfigoptions')
  852. ->join('tblhostingconfigoptions', 'tblproductconfigoptions.id', '=', 'tblhostingconfigoptions.configid')
  853. ->where('tblhostingconfigoptions.relid', '=', $params['serviceid'])
  854. ->where('tblproductconfigoptions.optionname', 'like', 'newAddQuota%')
  855. ->select('tblhostingconfigoptions.id')
  856. ->get();
  857. try {
  858. $updateNewAddQuota = Capsule::table('tblhostingconfigoptions')
  859. ->where('id', $newAddQuotaFieldIDObj[0]->id)
  860. ->update(
  861. [
  862. 'qty' => '0',
  863. ]
  864. );
  865. } catch (\Exception $e) {
  866. logModuleCall(
  867. 'zimbrasingle',
  868. __FUNCTION__,
  869. $updateNewAddQuota,
  870. 'Error: could not reset newAddOuota in database.',
  871. $e->getMessage()
  872. );
  873. return 'Error: could not reset newAddOuota in database.';
  874. }
  875. }
  876. return 'success';
  877. }