zimbraSingle.php 24 KB

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