zimbraSingle.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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
  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
  234. );
  235. continue;
  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
  245. );
  246. continue;
  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
  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
  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. $params['password'] = $params['customfields']['password'];
  368. $cosID = $apiAccountManager->getCosId($params['configoption1']);
  369. if(is_a($cosID, 'Exception')) {
  370. logModuleCall(
  371. 'zimbrasingle',
  372. __FUNCTION__,
  373. $params,
  374. 'Error: could not find serviceclass ' . $params['configoption1'],
  375. $cosID
  376. );
  377. return 'Error: could not find serviceclass ' . $params['configoption1'];
  378. }
  379. $attrs['zimbraCOSId'] = $cosID;
  380. $zimbraID = $apiAccountManager->createAccount($params['username'], $params['password'], $attrs);
  381. if(is_a($zimbraID, 'Exception')) {
  382. logModuleCall(
  383. 'zimbrasingle',
  384. __FUNCTION__,
  385. $params,
  386. 'Error: could not create account ' . $params['username'],
  387. $zimbraID
  388. );
  389. return 'Error: could not create account ' . $params['username'];
  390. }
  391. try {
  392. Capsule::table('tblhosting')
  393. ->where('id', '=', $params['serviceid'])
  394. ->update(
  395. array(
  396. 'username' => $params['username'],
  397. 'password' => $params['customfields']['password'],
  398. )
  399. );
  400. } catch (\Exception $e) {
  401. logModuleCall(
  402. 'zimbrasingle',
  403. __FUNCTION__,
  404. $params,
  405. 'Error: could save username & password in database',
  406. $e->getMessage()
  407. );
  408. return 'Error: could save username & password in database';
  409. }
  410. return 'success';
  411. }
  412. /**
  413. * Set a Zimbra account to status locked.
  414. *
  415. * Called when a suspension is requested. This is invoked automatically by WHMCS
  416. * when a product becomes overdue on payment or can be called manually by admin
  417. * user.
  418. *
  419. * @param array $params common module parameters
  420. *
  421. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  422. *
  423. * @return string 'success' or an error message
  424. */
  425. function zimbraSingle_SuspendAccount($params) {
  426. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  427. $login = $api->login();
  428. if(is_a($login, 'Exception')) {
  429. logModuleCall(
  430. 'zimbrasingle',
  431. __FUNCTION__,
  432. $params,
  433. 'Error: cannot login to ' . $params['serverhostname'],
  434. $login->getMessage()
  435. );
  436. return $login->getMessage();
  437. }
  438. $apiAccountManager = new Zm_Account($api);
  439. $response = $apiAccountManager->setAccountStatus($params['username'], 'locked');
  440. if(is_a($response, 'Exception')) {
  441. logModuleCall(
  442. 'zimbrasingle',
  443. __FUNCTION__,
  444. $params,
  445. 'Error: could not lock account ' . $params['username'],
  446. $response
  447. );
  448. return false;
  449. }
  450. return 'success';
  451. }
  452. /**
  453. * Set a Zimbra account to status active.
  454. *
  455. * Called when an un-suspension is requested. This is invoked
  456. * automatically upon payment of an overdue invoice for a product, or
  457. * can be called manually by admin user.
  458. *
  459. * @param array $params common module parameters
  460. *
  461. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  462. *
  463. * @return string 'success' or an error message
  464. */
  465. function zimbraSingle_UnsuspendAccount($params) {
  466. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  467. $login = $api->login();
  468. if(is_a($login, 'Exception')) {
  469. logModuleCall(
  470. 'zimbrasingle',
  471. __FUNCTION__,
  472. $params,
  473. 'Error: cannot login to ' . $params['serverhostname'],
  474. $login->getMessage()
  475. );
  476. return $login->getMessage();
  477. }
  478. $apiAccountManager = new Zm_Account($api);
  479. $response = $apiAccountManager->setAccountStatus($params['username'], 'active');
  480. if(is_a($response, 'Exception')) {
  481. logModuleCall(
  482. 'zimbrasingle',
  483. __FUNCTION__,
  484. $params,
  485. 'Error: could not unlock account ' . $params['username'],
  486. $response
  487. );
  488. return 'Error: could not unlock account ' . $params['username'];
  489. }
  490. return 'success';
  491. }
  492. /**
  493. * Removes a Zimbra account.
  494. *
  495. * Called when a termination is requested. This can be invoked automatically for
  496. * overdue products if enabled, or requested manually by an admin user.
  497. *
  498. * @param array $params common module parameters
  499. *
  500. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  501. *
  502. * @return string 'success' or an error message
  503. */
  504. function zimbraSingle_TerminateAccount($params) {
  505. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  506. $login = $api->login();
  507. if(is_a($login, 'Exception')) {
  508. logModuleCall(
  509. 'zimbrasingle',
  510. __FUNCTION__,
  511. $params,
  512. 'Error: cannot login to ' . $params['serverhostname'],
  513. $login->getMessage()
  514. );
  515. return $login->getMessage();
  516. }
  517. $apiAccountManager = new Zm_Account($api);
  518. $accountStatus = $apiAccountManager->getAccountStatus($params['username']);
  519. if(is_a($accountStatus, 'Exception')) {
  520. logModuleCall(
  521. 'zimbrasingle',
  522. __FUNCTION__,
  523. $params,
  524. 'Error: could not verify account '. $params['username'],
  525. $accountStatus
  526. );
  527. return 'Error : account ' . $params['username'] . ' Name could not verified';
  528. }
  529. if ($accountStatus != 'locked') {
  530. return 'Account '. $params['username'] . ' is active, suspend account first!';
  531. }
  532. $response = $apiAccountManager->deleteAccount($params['username']);
  533. if(is_a($response, 'Exception')) {
  534. logModuleCall(
  535. 'zimbrasingle',
  536. __FUNCTION__,
  537. $params,
  538. 'Error: could not remove account '. $params['username'],
  539. $response
  540. );
  541. return 'Error: could not remove account '. $params['username'];
  542. }
  543. return 'success';
  544. }
  545. /**
  546. * Set a new class of service for a Zimbra account.
  547. *
  548. * Called to apply a change of the class of service. It
  549. * is called to provision upgrade or downgrade orders, as well as being
  550. * able to be invoked manually by an admin user.
  551. *
  552. * This same function is called for upgrades and downgrades of both
  553. * products and configurable options.
  554. *
  555. * @param array $params common module parameters
  556. *
  557. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  558. *
  559. * @return string 'success' or an error message
  560. */
  561. function zimbraSingle_ChangePackage($params) {
  562. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  563. $login = $api->login();
  564. if(is_a($login, 'Exception')) {
  565. logModuleCall(
  566. 'zimbrasingle',
  567. __FUNCTION__,
  568. $params,
  569. 'Error: cannot login to ' . $params['serverhostname'],
  570. $login->getMessage()
  571. );
  572. return $login->getMessage();
  573. }
  574. $apiAccountManager = new Zm_Account($api);
  575. $response = $apiAccountManager->setAccountCos($params['username'], $params['configoption1']);
  576. if(is_a($response, 'Exception')) {
  577. logModuleCall(
  578. 'zimbrasingle',
  579. __FUNCTION__,
  580. $params,
  581. 'Error: could not set class of service for '. $params['username'],
  582. $response
  583. );
  584. return 'Error: could not set class of service for '. $params['username'];
  585. }
  586. $accountQuota = $apiAccountManager->getQuota($params['username']);
  587. if(is_a($accountQuota, 'Exception')) {
  588. logModuleCall(
  589. 'zimbrasingle',
  590. __FUNCTION__,
  591. $params,
  592. 'Error : could not find quota for ' . $params['username'],
  593. $accountQuota
  594. );
  595. }
  596. try {
  597. Capsule::table('tblhosting')
  598. ->where('id', '=', $params['serviceid'])
  599. ->update(
  600. array(
  601. 'disklimit' => $accountQuota,
  602. )
  603. );
  604. } catch (\Exception $e) {
  605. logModuleCall(
  606. 'zimbrasingle',
  607. __FUNCTION__,
  608. $params,
  609. 'Error: could not update quota in database',
  610. $e->getMessage()
  611. );
  612. return 'Error: could not update quota in database';
  613. }
  614. return 'success';
  615. }
  616. /**
  617. * Define Zimbra product configuration options.
  618. *
  619. * Gather classes of service from the Zinbra server.
  620. * Calls a function to create all necessary customfields for the order form using the selected values.
  621. *
  622. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  623. *
  624. * @return array
  625. */
  626. function zimbraSingle_ConfigOptions($params) {
  627. $whmcs = App::self();
  628. $serverGroupID = $whmcs->get_req_var('servergroup');
  629. $serverIDObj = Capsule::table('tblservergroupsrel')
  630. ->select('serverid')
  631. ->where('groupid', '=', $serverGroupID)
  632. ->get();
  633. $serverIDArray = zimbraSingleFindAll($serverIDObj,'serverid');
  634. $server = Capsule::table('tblservers')
  635. ->select('ipaddress', 'username', 'password')
  636. ->where('id', $serverIDArray)
  637. ->where('active', '=', 1)
  638. ->get();
  639. $accessData['zimbraServer'] = $server[0]->ipaddress;
  640. $accessData['adminUser'] = $server[0]->username;
  641. $passDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  642. if ($passDecrypt['result'] == 'success') {
  643. $accessData['adminPass'] = $passDecrypt['password'];
  644. } else {
  645. logModuleCall(
  646. 'zimbrasingle',
  647. __FUNCTION__,
  648. $server,
  649. 'Error: could not decrypt password',
  650. $passDecrypt['message']
  651. );
  652. return false;
  653. }
  654. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], 'admin');
  655. $login = $api->login();
  656. if(is_a($login, 'Exception')) {
  657. logModuleCall(
  658. 'zimbrasingle',
  659. __FUNCTION__,
  660. $server,
  661. 'Error: cannot login to ' . $accessData['zimbraServer'],
  662. $login->getMessage()
  663. );
  664. return false;
  665. }
  666. $apiAccountManager = new Zm_Account($api);
  667. $cosIDs = $apiAccountManager->getAllCos();
  668. if(is_a($cosIDs, 'Exception')) {
  669. logModuleCall(
  670. 'zimbrasingle',
  671. __FUNCTION__,
  672. $params,
  673. 'Error: could not fetch classes of service',
  674. $cosIDs
  675. );
  676. return false;
  677. }
  678. $cosNames = zimbraSingleFindAll($cosIDs, 'NAME');
  679. $configOptions = array();
  680. $configOptions['cos'] = array(
  681. 'FriendlyName' => 'Class of Service',
  682. 'Type' => 'dropdown',
  683. 'Options' => implode(',', $cosNames),
  684. 'Description' => 'Select COS',
  685. );
  686. return $configOptions;
  687. }
  688. /**
  689. * Perform single sign-on for a given instance of a product/service.
  690. *
  691. * Called when single sign-on is requested for an instance of a product/service.
  692. *
  693. * When successful, returns an URL to which the user should be redirected.
  694. *
  695. * @param array $params common module parameters
  696. *
  697. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  698. *
  699. * @return array
  700. */
  701. function zimbraSingle_ServiceSingleSignOn($params) {
  702. $api = new Zm_Auth($params['serverhostname'], $params['serverusername'], $params['serverpassword'], 'admin');
  703. $login = $api->login();
  704. if(is_a($login, 'Exception')) {
  705. logModuleCall(
  706. 'zimbrasingle',
  707. __FUNCTION__,
  708. $params,
  709. 'Error: cannot login to ' . $params['serverhostname'],
  710. $login->getMessage()
  711. );
  712. return array(
  713. 'success' => false,
  714. 'redirectTo' => '',
  715. );
  716. }
  717. $apiDomainManager = new Zm_Domain($api);
  718. $domainOptions = $apiDomainManager->getDomainOptions($params['customfields']['maildomain']);
  719. if(is_a($domainOptions, 'Exception')) {
  720. logModuleCall(
  721. 'zimbrasingle',
  722. __FUNCTION__,
  723. $params,
  724. 'Error : could not fetch options for ' . $params['customfields']['maildomain'],
  725. $domainOptions->getMessage()
  726. );
  727. return array(
  728. 'success' => false,
  729. 'redirectTo' => '',
  730. );
  731. }
  732. $preAuthKey = $domainOptions['zimbraPreAuthKey'];
  733. $apiAccountManager = new Zm_Account($api);
  734. $accountInfo = $apiAccountManager->getAccountInfo($params['username']);
  735. if(is_a($accountInfo, 'Exception')) {
  736. logModuleCall(
  737. 'zimbrasingle',
  738. __FUNCTION__,
  739. $params,
  740. 'Error: could not gather informations for ' . $params['username'],
  741. $accountInfo
  742. );
  743. return array(
  744. 'success' => false,
  745. 'redirectTo' => '',
  746. );
  747. }
  748. $webmailUrl = zimbraSingleFindAll($accountInfo, 'PUBLICMAILURL');
  749. $timestamp=time()*1000;
  750. $preauthToken=hash_hmac('sha1', $params['username'] . '|name|0|' . $timestamp, $preAuthKey);
  751. $preauthURL = $webmailUrl[0]['DATA'] . '/service/preauth?account=' . $params['username'] . '&by=name&timestamp=' . $timestamp .'&expires=0&preauth='. $preauthToken;
  752. return array(
  753. 'success' => true,
  754. 'redirectTo' => $preauthURL,
  755. );
  756. }