zimbraSingle.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. {
  33. $values = array();
  34. $iterator = new RecursiveArrayIterator((array)$haystack);
  35. $recursive = new RecursiveIteratorIterator(
  36. $iterator,
  37. RecursiveIteratorIterator::SELF_FIRST
  38. );
  39. foreach ($recursive as $key => $value) {
  40. if ($key === $needle) {
  41. array_push($values, $value);
  42. }
  43. }
  44. return $values;
  45. }
  46. /**
  47. * server side password check
  48. *
  49. * recheck the client side password check
  50. * in case that the client side check has been disabled
  51. *
  52. * @param string $pwd password
  53. *
  54. * @return string missing features or null if the password matches our needs
  55. */
  56. function zimbraSingleCheckPassword($pwd) {
  57. if (strlen($pwd) < 8) {
  58. return 'Das das Passwort ist zu kurz. Es werden mind. 8 Zeichen benötigt';
  59. }
  60. if (!preg_match('#[0-9]+#', $pwd)) {
  61. return 'Das Passwort muss mindestens eine Zahl enthalten';
  62. }
  63. if (!preg_match('#[A-Z]+#', $pwd)) {
  64. return 'Das Passwort muss mindestens einen Grossbuchstaben (A-Z) enthalten';
  65. }
  66. if (!preg_match('#[a-z]+#', $pwd)) {
  67. return 'Das Passwort muss mindestens einen Kleinbuchstaben (a-z) enthalten';
  68. }
  69. if (!preg_match('#[^\w]+#', $pwd)) {
  70. return 'Das Passwort muss mindestens ein Sonderzeichen (.,-:=) enthalten';
  71. }
  72. return null;
  73. }
  74. /**
  75. * Define module related meta data.
  76. *
  77. * Values returned here are used to determine module related abilities and
  78. * settings.
  79. *
  80. * @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
  81. *
  82. * @return array
  83. */
  84. function zimbraSingle_MetaData() {
  85. return array(
  86. 'DisplayName' => 'Zimbra Single Mailbox Provisioning',
  87. 'APIVersion' => '1.2',
  88. 'DefaultNonSSLPort' => '7071',
  89. 'DefaultSSLPort' => '7071',
  90. 'RequiresServer' => true,
  91. 'ServiceSingleSignOnLabel' => 'Login to Zimbra',
  92. 'AdminSingleSignOnLabel' => 'Login to Zimbra Admin'
  93. );
  94. }
  95. /**
  96. * Test connection to a Zimbra server with the given server parameters.
  97. *
  98. * Allows an admin user to verify that an API connection can be
  99. * successfully made with the given configuration parameters for a
  100. * server.
  101. *
  102. * When defined in a module, a Test Connection button will appear
  103. * alongside the Server Type dropdown when adding or editing an
  104. * existing server.
  105. *
  106. * @param array $params common module parameters
  107. *
  108. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  109. *
  110. * @return array
  111. */
  112. function zimbraSingle_TestConnection($params) {
  113. $auth = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  114. $login = $auth->login();
  115. if(is_a($login, 'Exception')) {
  116. logModuleCall(
  117. 'zimbrasingle',
  118. __FUNCTION__,
  119. $params,
  120. 'Connection failed, cannot login to ' . $params['serverhostname'],
  121. $login->getMessage()
  122. );
  123. return array(
  124. 'success' => false,
  125. 'error' => 'Connection failed, cannot login to ' . $params['serverhostname'],
  126. );
  127. }
  128. return array(
  129. 'success' => true,
  130. 'error' => '',
  131. );
  132. }
  133. /**
  134. * Client area output logic handling.
  135. *
  136. * This function is used to define module specific client area output. It should
  137. * return an array consisting of a template file and optional additional
  138. * template variables to make available to that template.
  139. *
  140. * The template file you return can be one of two types:
  141. *
  142. * * tabOverviewModuleOutputTemplate - The output of the template provided here
  143. * will be displayed as part of the default product/service client area
  144. * product overview page.
  145. *
  146. * * tabOverviewReplacementTemplate - Alternatively using this option allows you
  147. * to entirely take control of the product/service overview page within the
  148. * client area.
  149. *
  150. * Whichever option you choose, extra template variables are defined in the same
  151. * way. This demonstrates the use of the full replacement.
  152. *
  153. * Please Note: Using tabOverviewReplacementTemplate means you should display
  154. * the standard information such as pricing and billing details in your custom
  155. * template or they will not be visible to the end user.
  156. *
  157. * @param array $params common module parameters
  158. *
  159. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  160. *
  161. * @return array
  162. */
  163. function zimbraSingle_ClientArea($params) {
  164. $clientInfo = array();
  165. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  166. $login = $api->login();
  167. if(is_a($login, 'Exception')) {
  168. logModuleCall(
  169. 'zimbrasingle',
  170. __FUNCTION__,
  171. $params,
  172. 'Error: cannot login to ' . $accessData['zimbraServer'],
  173. $login->getMessage()
  174. );
  175. return false;
  176. }
  177. $apiAccountManager = new Zm_Account($api);
  178. $accountInfo = $apiAccountManager->getAccountInfo($params['username']);
  179. if(is_a($accountInfo, 'Exception')) {
  180. logModuleCall(
  181. 'zimbrasingle',
  182. __FUNCTION__,
  183. $params,
  184. 'Error: could not gather informations for ' . $params['username'],
  185. $accountInfo->getMessage()
  186. );
  187. return false;
  188. }
  189. $webmailUrl = zimbraSingleFindAll($accountInfo, 'PUBLICMAILURL');
  190. $clientInfo['webmailurl'] = $webmailUrl[0]['DATA'];
  191. return array(
  192. 'templatefile' => 'clientarea',
  193. 'vars' => $clientInfo,
  194. );
  195. }
  196. /**
  197. * Usage Update
  198. *
  199. * Important: Runs daily per server not per product
  200. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  201. * @param array $params common module parameters
  202. *
  203. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  204. */
  205. function zimbraSingle_UsageUpdate($params) {
  206. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  207. $login = $api->login();
  208. if(is_a($login, 'Exception')) {
  209. logModuleCall(
  210. 'zimbrasingle',
  211. __FUNCTION__,
  212. $params,
  213. 'Error: cannot login to ' . $params['serverhostname'],
  214. $login->getMessage()
  215. );
  216. return false;
  217. }
  218. $apiAccountManager = new Zm_Account($api);
  219. $productsObj = Capsule::table('tblhosting')
  220. ->select('*')
  221. ->where('server', '=', $params['serverid'])
  222. ->where('domainstatus', '=', 'Active')
  223. ->get();
  224. foreach((array)$productsObj as $productObj) {
  225. $product = get_object_vars($productObj[0]);
  226. $accountQuota = $apiAccountManager->getQuota($product['username']);
  227. if(is_a($accountQuota, 'Exception')) {
  228. logModuleCall(
  229. 'zimbrasingle',
  230. __FUNCTION__,
  231. $params,
  232. 'Error : could not find quota for ' . $product['username'],
  233. $accountQuota->getMessage()
  234. );
  235. break;
  236. }
  237. $mboxObj = $apiAccountManager->getMailbox($product['username']);
  238. if(is_a($mboxObj, 'Exception')) {
  239. logModuleCall(
  240. 'zimbrasingle',
  241. __FUNCTION__,
  242. $params,
  243. 'Error: could not fetch mailbox info for ' . $product['username'],
  244. $mboxObj->getMessage()
  245. );
  246. break;
  247. }
  248. $mboxVars = get_object_vars($mboxObj);
  249. $mboxSize = $mboxVars['S'];
  250. try {
  251. Capsule::table('tblhosting')
  252. ->where('id', '=', $product['id'])
  253. ->update(
  254. array(
  255. 'diskusage' => round($mboxSize / 1048576,2),
  256. 'disklimit' => round($accountQuota / 1048576,2),
  257. 'lastupdate' => Capsule::raw('now()')
  258. )
  259. );
  260. } catch (\Exception $e) {
  261. logModuleCall(
  262. 'zimbrasingle',
  263. __FUNCTION__,
  264. $params,
  265. 'Error: could update usage information for ' . $product['username'],
  266. $e->getMessage()
  267. );
  268. }
  269. }
  270. }
  271. /**
  272. * Change the password for a Zimbra account.
  273. *
  274. * Called when a password change is requested. This can occur either due to a
  275. * client requesting it via the client area or an admin requesting it from the
  276. * admin side.
  277. *
  278. * This option is only available to client end users when the product is in an
  279. * active status.
  280. *
  281. * @param array $params common module parameters
  282. *
  283. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  284. *
  285. * @return string 'success' or an error message
  286. */
  287. function zimbraSingle_ChangePassword($params) {
  288. $checkPassword = zimbraSingleCheckPassword($params['password']);
  289. if ($checkPassword != null) {
  290. return $checkPassword;
  291. }
  292. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  293. $login = $api->login();
  294. if(is_a($login, 'Exception')) {
  295. logModuleCall(
  296. 'zimbrasingle',
  297. __FUNCTION__,
  298. $params,
  299. 'Error: cannot login to ' . $params['serverhostname'],
  300. $login->getMessage()
  301. );
  302. return false;
  303. }
  304. $apiAccountManager = new Zm_Account($api);
  305. $response = $apiAccountManager->setAccountPassword($params['username'], $params['password']);
  306. if(is_a($response, 'Exception')) {
  307. logModuleCall(
  308. 'zimbrasingle',
  309. __FUNCTION__,
  310. $params,
  311. 'Error: password could not be set for ' . $params['username'],
  312. $response->getMessage()
  313. );
  314. return false;
  315. }
  316. return 'success';
  317. }
  318. /**
  319. * Provision a new instance of a Zimbra account.
  320. *
  321. * Attempt to provision a new Zimbra mail account. This is
  322. * called any time provisioning is requested inside of WHMCS. Depending upon the
  323. * configuration, this can be any of:
  324. * * When a new order is placed
  325. * * When an invoice for a new order is paid
  326. * * Upon manual request by an admin user
  327. *
  328. * @param array $params common module parameters
  329. *
  330. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  331. *
  332. * @return string 'success' or an error message
  333. */
  334. function zimbraSingle_CreateAccount($params) {
  335. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  336. $login = $api->login();
  337. if(is_a($login, 'Exception')) {
  338. logModuleCall(
  339. 'zimbrasingle',
  340. __FUNCTION__,
  341. $params,
  342. 'Error: cannot login to ' . $params['serverhostname'],
  343. $login->getMessage()
  344. );
  345. return $login->getMessage();
  346. }
  347. $params['username'] = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  348. $apiAccountManager = new Zm_Account($api);
  349. $accountExists = $apiAccountManager->accountExists($params['username']);
  350. if(is_a($accountExists, 'Exception')) {
  351. logModuleCall(
  352. 'zimbrasingle',
  353. __FUNCTION__,
  354. $params,
  355. 'Error: could not verify ' . $params['username'],
  356. $accountExists->getMessage()
  357. );
  358. return 'Error: could not verify '. $params['username'];
  359. }
  360. if($accountExists === true) {
  361. return 'Error: account already exists ' . $params['username'];
  362. }
  363. $attrs = array();
  364. $attrs['gn'] = $params['customfields']['givenname'];
  365. $attrs['sn'] = $params['customfields']['sn'];
  366. $attrs['displayName'] = $attrs['gn'] . ' ' . $attrs['sn'];
  367. $passDecrypt = localAPI('DecryptPassword', array('password2' => $params['customfields']['password']));
  368. if ($passDecrypt['result'] == 'success') {
  369. $params['password'] = $passDecrypt['password'];
  370. } else {
  371. logModuleCall(
  372. 'zimbrasingle',
  373. __FUNCTION__,
  374. $params,
  375. 'Error: could not decrypt password',
  376. $passDecrypt['message']
  377. );
  378. return 'Error: could not decrypt password';
  379. }
  380. $cosID = $apiAccountManager->getCosId($params['configoption1']);
  381. if(is_a($cosID, 'Exception')) {
  382. logModuleCall(
  383. 'zimbrasingle',
  384. __FUNCTION__,
  385. $params,
  386. 'Error: could not find serviceclass ' . $params['configoption1'],
  387. $cosID->getMessage()
  388. );
  389. return 'Error: could not find serviceclass ' . $params['configoption1'];
  390. }
  391. $attrs['zimbraCOSId'] = $cosID;
  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->getMessage()
  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. )
  411. );
  412. } catch (\Exception $e) {
  413. logModuleCall(
  414. 'zimbrasingle',
  415. __FUNCTION__,
  416. $params,
  417. 'Error: could save username & password in database',
  418. $e->getMessage()
  419. );
  420. return 'Error: could save username & password in database';
  421. }
  422. return 'success';
  423. }
  424. /**
  425. * Set a Zimbra account to status locked.
  426. *
  427. * Called when a suspension is requested. This is invoked automatically by WHMCS
  428. * when a product becomes overdue on payment or can be called manually by admin
  429. * user.
  430. *
  431. * @param array $params common module parameters
  432. *
  433. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  434. *
  435. * @return string 'success' or an error message
  436. */
  437. function zimbraSingle_SuspendAccount($params) {
  438. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  439. $login = $api->login();
  440. if(is_a($login, 'Exception')) {
  441. logModuleCall(
  442. 'zimbrasingle',
  443. __FUNCTION__,
  444. $params,
  445. 'Error: cannot login to ' . $params['serverhostname'],
  446. $login->getMessage()
  447. );
  448. return $login->getMessage();
  449. }
  450. $apiAccountManager = new Zm_Account($api);
  451. $response = $apiAccountManager->setAccountStatus($params['username'], 'locked');
  452. if(is_a($response, 'Exception')) {
  453. logModuleCall(
  454. 'zimbrasingle',
  455. __FUNCTION__,
  456. $params,
  457. 'Error: could not lock account ' . $params['username'],
  458. $response->getMessage()
  459. );
  460. return false;
  461. }
  462. return 'success';
  463. }
  464. /**
  465. * Set a Zimbra account to status active.
  466. *
  467. * Called when an un-suspension is requested. This is invoked
  468. * automatically upon payment of an overdue invoice for a product, or
  469. * can be called manually by admin user.
  470. *
  471. * @param array $params common module parameters
  472. *
  473. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  474. *
  475. * @return string 'success' or an error message
  476. */
  477. function zimbraSingle_UnsuspendAccount($params) {
  478. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  479. $login = $api->login();
  480. if(is_a($login, 'Exception')) {
  481. logModuleCall(
  482. 'zimbrasingle',
  483. __FUNCTION__,
  484. $params,
  485. 'Error: cannot login to ' . $params['serverhostname'],
  486. $login->getMessage()
  487. );
  488. return $login->getMessage();
  489. }
  490. $apiAccountManager = new Zm_Account($api);
  491. $response = $apiAccountManager->setAccountStatus($params['username'], 'active');
  492. if(is_a($response, 'Exception')) {
  493. logModuleCall(
  494. 'zimbrasingle',
  495. __FUNCTION__,
  496. $params,
  497. 'Error: could not unlock account ' . $params['username'],
  498. $response->getMessage()
  499. );
  500. return 'Error: could not unlock account ' . $params['username'];
  501. }
  502. return 'success';
  503. }
  504. /**
  505. * Removes a Zimbra account.
  506. *
  507. * Called when a termination is requested. This can be invoked automatically for
  508. * overdue products if enabled, or requested manually by an admin user.
  509. *
  510. * @param array $params common module parameters
  511. *
  512. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  513. *
  514. * @return string 'success' or an error message
  515. */
  516. function zimbraSingle_TerminateAccount($params) {
  517. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  518. $login = $api->login();
  519. if(is_a($login, 'Exception')) {
  520. logModuleCall(
  521. 'zimbrasingle',
  522. __FUNCTION__,
  523. $params,
  524. 'Error: cannot login to ' . $params['serverhostname'],
  525. $login->getMessage()
  526. );
  527. return $login->getMessage();
  528. }
  529. $apiAccountManager = new Zm_Account($api);
  530. $accountStatus = $apiAccountManager->getAccountStatus($params['username']);
  531. if(is_a($accountStatus, 'Exception')) {
  532. logModuleCall(
  533. 'zimbrasingle',
  534. __FUNCTION__,
  535. $params,
  536. 'Error: could not verify account '. $params['username'],
  537. $accountStatus->getMessage()
  538. );
  539. return 'Error : account ' . $params['username'] . ' Name could not verified';
  540. }
  541. if ($accountStatus != 'locked') {
  542. return 'Account '. $params['username'] . ' is active, suspend account first!';
  543. }
  544. $response = $apiAccountManager->deleteAccount($params['username']);
  545. if(is_a($response, 'Exception')) {
  546. logModuleCall(
  547. 'zimbrasingle',
  548. __FUNCTION__,
  549. $params,
  550. 'Error: could not remove account '. $params['username'],
  551. $response->getMessage()
  552. );
  553. return 'Error: could not remove account '. $params['username'];
  554. }
  555. return 'success';
  556. }
  557. /**
  558. * Set a new class of service for a Zimbra account.
  559. *
  560. * Called to apply a change of the class of service. It
  561. * is called to provision upgrade or downgrade orders, as well as being
  562. * able to be invoked manually by an admin user.
  563. *
  564. * This same function is called for upgrades and downgrades of both
  565. * products and configurable options.
  566. *
  567. * @param array $params common module parameters
  568. *
  569. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  570. *
  571. * @return string 'success' or an error message
  572. */
  573. function zimbraSingle_ChangePackage($params) {
  574. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  575. $login = $api->login();
  576. if(is_a($login, 'Exception')) {
  577. logModuleCall(
  578. 'zimbrasingle',
  579. __FUNCTION__,
  580. $params,
  581. 'Error: cannot login to ' . $params['serverhostname'],
  582. $login->getMessage()
  583. );
  584. return $login->getMessage();
  585. }
  586. $apiAccountManager = new Zm_Account($api);
  587. $response = $apiAccountManager->setAccountCos($params['username'], $params['configoption1']);
  588. if(is_a($response, 'Exception')) {
  589. logModuleCall(
  590. 'zimbrasingle',
  591. __FUNCTION__,
  592. $params,
  593. 'Error: could not set class of service for '. $params['username'],
  594. $response->getMessage()
  595. );
  596. return 'Error: could not set class of service for '. $params['username'];
  597. }
  598. $accountQuota = $apiAccountManager->getQuota($params['username']);
  599. if(is_a($accountQuota, 'Exception')) {
  600. logModuleCall(
  601. 'zimbrasingle',
  602. __FUNCTION__,
  603. $params,
  604. 'Error : could not find quota for ' . $params['username'],
  605. $accountQuota->getMessage()
  606. );
  607. }
  608. try {
  609. Capsule::table('tblhosting')
  610. ->where('id', '=', $params['serviceid'])
  611. ->update(
  612. array(
  613. 'disklimit' => $accountQuota,
  614. )
  615. );
  616. } catch (\Exception $e) {
  617. logModuleCall(
  618. 'zimbrasingle',
  619. __FUNCTION__,
  620. $params,
  621. 'Error: could not update quota in database',
  622. $e->getMessage()
  623. );
  624. return 'Error: could not update quota in database';
  625. }
  626. return 'success';
  627. }
  628. /**
  629. * Define Zimbra product configuration options.
  630. *
  631. * Gather classes of service from the Zinbra server.
  632. * Calls a function to create all necessary customfields for the order form using the selected values.
  633. *
  634. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  635. *
  636. * @return array
  637. */
  638. function zimbraSingle_ConfigOptions($params) {
  639. $whmcs = App::self();
  640. $serverGroupID = $whmcs->get_req_var('servergroup');
  641. $serverIDObj = Capsule::table('tblservergroupsrel')
  642. ->select('serverid')
  643. ->where('groupid', '=', $serverGroupID)
  644. ->get();
  645. $serverIDArray = zimbraSingleFindAll($serverIDObj,'serverid');
  646. $server = Capsule::table('tblservers')
  647. ->select('ipaddress', 'username', 'password')
  648. ->where('id', $serverIDArray)
  649. ->where('active', '=', 1)
  650. ->get();
  651. $accessData['zimbraServer'] = $server[0]->ipaddress;
  652. $accessData['adminUser'] = $server[0]->username;
  653. $adminPassCrypt = $server[0]->password;
  654. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $adminPassCrypt));
  655. if ($adminPassDecrypt['result'] == 'success') {
  656. $accessData['adminPass'] = $adminPassDecrypt['password'];
  657. } else {
  658. logModuleCall(
  659. 'zimbrasingle',
  660. __FUNCTION__,
  661. $adminPassCrypt,
  662. 'Error: cloud not decrypt admin password' ,
  663. $adminPassDecrypt['message']
  664. );
  665. return false;
  666. }
  667. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], 'admin');
  668. $login = $api->login();
  669. if(is_a($login, 'Exception')) {
  670. logModuleCall(
  671. 'zimbrasingle',
  672. __FUNCTION__,
  673. $params,
  674. 'Error: cannot login to ' . $accessData['zimbraServer'],
  675. $login->getMessage()
  676. );
  677. return false;
  678. }
  679. $apiAccountManager = new Zm_Account($api);
  680. $cosIDs = $apiAccountManager->getAllCos();
  681. if(is_a($cosIDs, 'Exception')) {
  682. logModuleCall(
  683. 'zimbrasingle',
  684. __FUNCTION__,
  685. $params,
  686. 'Error: could not fetch classes of service',
  687. $cosIDs->getMessage()
  688. );
  689. return false;
  690. }
  691. $cosNames = zimbraSingleFindAll($cosIDs, 'NAME');
  692. $configOptions = array();
  693. $configOptions['cos'] = array(
  694. 'FriendlyName' => 'Class of Service',
  695. 'Type' => 'dropdown',
  696. 'Options' => implode(',', $cosNames),
  697. 'Description' => 'Select COS',
  698. );
  699. return $configOptions;
  700. }
  701. /**
  702. * Perform single sign-on for a given instance of a product/service.
  703. *
  704. * Called when single sign-on is requested for an instance of a product/service.
  705. *
  706. * When successful, returns an URL to which the user should be redirected.
  707. *
  708. * @param array $params common module parameters
  709. *
  710. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  711. *
  712. * @return array
  713. */
  714. function zimbraSingle_ServiceSingleSignOn($params) {
  715. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  716. $login = $api->login();
  717. if(is_a($login, 'Exception')) {
  718. logModuleCall(
  719. 'zimbrasingle',
  720. __FUNCTION__,
  721. $params,
  722. 'Error: cannot login to ' . $params['serverhostname'],
  723. $login->getMessage()
  724. );
  725. return $login->getMessage();
  726. }
  727. $apiAccountManager = new Zm_Domain($api);
  728. $accountOptions = $apiAccountManager->getDomainOptions($params['customfields']['maildomain']);
  729. if(is_a($accountOptions, 'Exception')) {
  730. logModuleCall(
  731. 'zimbrasingle',
  732. __FUNCTION__,
  733. $params,
  734. 'Error : could not find quota for ' . $params['username'],
  735. $accountOptions->getMessage()
  736. );
  737. }
  738. logModuleCall(
  739. 'zimbrasingle',
  740. __FUNCTION__,
  741. $params,
  742. 'Debug',
  743. $accountOptions
  744. );
  745. return array(
  746. 'success' => true,
  747. 'redirectTo' => 'https://testserver.test.local/',
  748. );
  749. }