zimbraSingle.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. $accessData = zimbraSingleGetAccess();
  239. $clientInfo = array();
  240. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  241. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  242. $login = $api->login();
  243. if(is_a($login, "Exception")) {
  244. logModuleCall(
  245. 'zimbrasingle',
  246. __FUNCTION__,
  247. $params,
  248. "Error: cannot login to " . $accessData['zimbraServer'],
  249. $login
  250. );
  251. return false;
  252. }
  253. $apiAccountManager = new Zm_Account($api);
  254. $response = $apiAccountManager->getAccountInfo($accountName);
  255. if(is_a($response, "Exception")) {
  256. logModuleCall(
  257. 'zimbrasingle',
  258. __FUNCTION__,
  259. $params,
  260. "Error: could not gather informations for $accountName",
  261. $response
  262. );
  263. return false;
  264. }
  265. $webmailUrl = recursiveFindAll( $response, 'PUBLICMAILURL');
  266. $clientInfo['webmailurl'] = $webmailUrl[0]['DATA'];
  267. return array(
  268. 'templatefile' => 'clientarea',
  269. 'vars' => $clientInfo,
  270. );
  271. }
  272. function zimbraSingle_UsageUpdate($params) {
  273. $api = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  274. $login = $api->login();
  275. if(is_a($login, "Exception")) {
  276. logModuleCall(
  277. 'zimbrasingle',
  278. __FUNCTION__,
  279. $params,
  280. "Error: cannot login to " . $params['serverip'],
  281. $login->getMessage()
  282. );
  283. return false;
  284. }
  285. $apiAccountManager = new Zm_Account($api);
  286. $productsObj = Capsule::table('tblhosting')
  287. ->select('*')
  288. ->where('server', '=', $params['serverid'])
  289. ->where('domainstatus', '=', 'Active')
  290. ->get();
  291. foreach((array)$productsObj as $productObj) {
  292. $product = get_object_vars($productObj[0]);
  293. $quota = $apiAccountManager->getQuota($product['username']);
  294. if(is_a($quota, "Exception")) {
  295. logModuleCall(
  296. 'zimbrasingle',
  297. __FUNCTION__,
  298. $product,
  299. "Error : could not find " . $product['username'],
  300. $quota->getMessage()
  301. );
  302. }
  303. $response = $apiAccountManager->getMailbox($product['username']);
  304. if(is_a($response, "Exception")) {
  305. logModuleCall(
  306. 'zimbrasingle',
  307. __FUNCTION__,
  308. $params,
  309. "Error: could not fetch mailbox info for " . $product['username'],
  310. $response->getMessage()
  311. );
  312. }
  313. $mbox = get_object_vars($response);
  314. $mboxSize = $mbox['S'];
  315. Capsule::table('tblhosting')
  316. ->where('id', '=', $product['id'])
  317. ->update(
  318. array(
  319. 'diskusage' => round($mboxSize / 1048576,2),
  320. 'disklimit' => round($quota / 1048576,2),
  321. 'lastupdate' => Capsule::raw('now()')
  322. )
  323. );
  324. }
  325. }
  326. /**
  327. * Change the password for a Zimbra account.
  328. *
  329. * Called when a password change is requested. This can occur either due to a
  330. * client requesting it via the client area or an admin requesting it from the
  331. * admin side.
  332. *
  333. * This option is only available to client end users when the product is in an
  334. * active status.
  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_ChangePassword($params)
  343. {
  344. $accessData = zimbraSingleGetAccess();
  345. if ($checkPW = zimbraSingleCheckPassword($params['password'])) {
  346. return $checkPW;
  347. }
  348. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  349. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  350. $login = $api->login();
  351. if(is_a($login, "Exception")) {
  352. logModuleCall(
  353. 'zimbrasingle',
  354. __FUNCTION__,
  355. $params,
  356. "Error: cannot login to " . $accessData['zimbraServer'],
  357. $login
  358. );
  359. return false;
  360. }
  361. $apiAccountManager = new Zm_Account($api);
  362. $response = $apiAccountManager->setAccountPassword($accountName, $params['password']);
  363. if(is_a($response, "Exception")) {
  364. logModuleCall(
  365. 'zimbrasingle',
  366. __FUNCTION__,
  367. $params,
  368. "Error: password for $accountName could not be set",
  369. $response
  370. );
  371. return false;
  372. }
  373. return 'success';
  374. }
  375. /**
  376. * Provision a new instance of a Zimbra account.
  377. *
  378. * Attempt to provision a new Zimbra mail account. This is
  379. * called any time provisioning is requested inside of WHMCS. Depending upon the
  380. * configuration, this can be any of:
  381. * * When a new order is placed
  382. * * When an invoice for a new order is paid
  383. * * Upon manual request by an admin user
  384. *
  385. * @param array $params common module parameters
  386. *
  387. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  388. *
  389. * @return string "success" or an error message
  390. */
  391. function zimbraSingle_CreateAccount($params)
  392. {
  393. logModuleCall(
  394. 'zimbrasingle',
  395. __FUNCTION__,
  396. $params,
  397. "Debug",
  398. $whmcs
  399. );
  400. $accessData = zimbraSingleGetAccess();
  401. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  402. $login = $api->login();
  403. if(is_a($login, "Exception")) {
  404. logModuleCall(
  405. 'zimbrasingle',
  406. __FUNCTION__,
  407. $accessData,
  408. "Error: cannot login to " . $accessData['zimbraServer'],
  409. $login->getMessage()
  410. );
  411. return "Error: cannot login to " . $accessData['zimbraServer'];
  412. }
  413. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  414. $apiAccountManager = new Zm_Account($api);
  415. $accountExists = $apiAccountManager->accountExists($accountName);
  416. if(is_a($accountExists, "Exception")) {
  417. logModuleCall(
  418. 'zimbrasingle',
  419. __FUNCTION__,
  420. $accessData,
  421. "Error: could not verify $accountName",
  422. $accountExists
  423. );
  424. return "Error: could not verify $accountName";
  425. }
  426. if($accountExists === true) {
  427. return "Error: account $accountName already exists";
  428. }
  429. $attrs = array();
  430. $attrs["gn"] = $params['customfields']["givenname"];
  431. $attrs["sn"] = $params['customfields']["sn"];
  432. $attrs["displayName"] = $attrs["gn"] . " " . $attrs["sn"];
  433. $passDecrypt = localAPI('DecryptPassword', array('password2' => $params['customfields']['password']));
  434. if ($passDecrypt['result'] == 'success') {
  435. $params['customfields']['password'] = $passDecrypt['password'];
  436. } else {
  437. logModuleCall(
  438. 'zimbrasingle',
  439. __FUNCTION__,
  440. $params['customfields']['password'],
  441. "Error: could not decrypt password",
  442. $passDecrypt
  443. );
  444. return "Error: could not decrypt password";
  445. }
  446. $cosID = $apiAccountManager->getCosId($params['configoption1']);
  447. if(is_a($cosID, "Exception")) {
  448. logModuleCall(
  449. 'zimbrasingle',
  450. __FUNCTION__,
  451. $params['configoption1'],
  452. "Error: serviceclass not available",
  453. $cosID
  454. );
  455. return "Error: serviceclass not available";
  456. }
  457. $attrs['zimbraCOSId'] = $cosID;
  458. $id = $apiAccountManager->createAccount($accountName, $params['customfields']['password'], $attrs);
  459. if(is_a($id, "Exception")) {
  460. logModuleCall(
  461. 'zimbrasingle',
  462. __FUNCTION__,
  463. $params,
  464. "Error: account $accountName not created",
  465. $id
  466. );
  467. return "Error: account $accountName not created";
  468. }
  469. return 'success';
  470. }
  471. /**
  472. * Set a Zimbra account to status locked.
  473. *
  474. * Called when a suspension is requested. This is invoked automatically by WHMCS
  475. * when a product becomes overdue on payment or can be called manually by admin
  476. * user.
  477. *
  478. * @param array $params common module parameters
  479. *
  480. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  481. *
  482. * @return string "success" or an error message
  483. */
  484. function zimbraSingle_SuspendAccount($params)
  485. {
  486. $accessData = zimbraSingleGetAccess();
  487. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  488. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  489. $login = $api->login();
  490. if(is_a($login, "Exception")) {
  491. logModuleCall(
  492. 'zimbrasingle',
  493. __FUNCTION__,
  494. $params,
  495. "Error: cannot login to " . $accessData['zimbraServer'],
  496. $login
  497. );
  498. return $login->getMessage();
  499. }
  500. $apiAccountManager = new Zm_Account($api);
  501. $response = $apiAccountManager->setAccountStatus($accountName, "locked");
  502. if(is_a($response, "Exception")) {
  503. logModuleCall(
  504. 'zimbrasingle',
  505. __FUNCTION__,
  506. $params,
  507. "Error: account $accountName could not locked",
  508. $response
  509. );
  510. return false;
  511. }
  512. return 'success';
  513. }
  514. /**
  515. * Set a Zimbra account to status active.
  516. *
  517. * Called when an un-suspension is requested. This is invoked
  518. * automatically upon payment of an overdue invoice for a product, or
  519. * can be called manually by admin user.
  520. *
  521. * @param array $params common module parameters
  522. *
  523. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  524. *
  525. * @return string "success" or an error message
  526. */
  527. function zimbraSingle_UnsuspendAccount($params)
  528. {
  529. $accessData = zimbraSingleGetAccess();
  530. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  531. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  532. $login = $api->login();
  533. if(is_a($login, "Exception")) {
  534. logModuleCall(
  535. 'zimbrasingle',
  536. __FUNCTION__,
  537. $params,
  538. "Error: cannot login to " . $accessData['zimbraServer'],
  539. $login
  540. );
  541. return $login->getMessage();
  542. }
  543. $apiAccountManager = new Zm_Account($api);
  544. $response = $apiAccountManager->setAccountStatus($accountName, "active");
  545. if(is_a($response, "Exception")) {
  546. logModuleCall(
  547. 'zimbrasingle',
  548. __FUNCTION__,
  549. $params,
  550. "Error: account $accountName could not unlocked",
  551. $response
  552. );
  553. return "Error: account $accountName could not unlocked";
  554. }
  555. return 'success';
  556. }
  557. /**
  558. * Removes a Zimbra account.
  559. *
  560. * Called when a termination is requested. This can be invoked automatically for
  561. * overdue products if enabled, or requested manually by an admin user.
  562. *
  563. * @param array $params common module parameters
  564. *
  565. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  566. *
  567. * @return string "success" or an error message
  568. */
  569. function zimbraSingle_TerminateAccount($params)
  570. {
  571. $accessData = zimbraSingleGetAccess();
  572. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  573. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  574. $login = $api->login();
  575. if(is_a($login, "Exception")) {
  576. logModuleCall(
  577. 'zimbrasingle',
  578. __FUNCTION__,
  579. $params,
  580. "Error: cannot login to " . $accessData['zimbraServer'],
  581. $login
  582. );
  583. return $login->getMessage();
  584. }
  585. $apiAccountManager = new Zm_Account($api);
  586. $response = $apiAccountManager->getAccountStatus($accountName);
  587. if(is_a($response, "Exception")) {
  588. logModuleCall(
  589. 'zimbrasingle',
  590. __FUNCTION__,
  591. $params,
  592. "Error: account $accountName could not verified",
  593. $response
  594. );
  595. return "Error : account $accountName could not verified";
  596. }
  597. if ($response != 'locked') {
  598. return "Account $accountName active, suspend account first!";
  599. }
  600. $response = $apiAccountManager->deleteAccount($accountName);
  601. if(is_a($response, "Exception")) {
  602. logModuleCall(
  603. 'zimbrasingle',
  604. __FUNCTION__,
  605. $params,
  606. "Error: account $accountName could not removed",
  607. $response
  608. );
  609. return "Error: account $accountName could not removed";
  610. }
  611. return 'success';
  612. }
  613. /**
  614. * Set a new class of service for a Zimbra account.
  615. *
  616. * Called to apply a change of the class of service. It
  617. * is called to provision upgrade or downgrade orders, as well as being
  618. * able to be invoked manually by an admin user.
  619. *
  620. * This same function is called for upgrades and downgrades of both
  621. * products and configurable options.
  622. *
  623. * @param array $params common module parameters
  624. *
  625. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  626. *
  627. * @return string "success" or an error message
  628. */
  629. function zimbraSingle_ChangePackage($params)
  630. {
  631. $accessData = zimbraSingleGetAccess();
  632. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  633. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  634. $login = $api->login();
  635. if(is_a($login, "Exception")) {
  636. logModuleCall(
  637. 'zimbrasingle',
  638. __FUNCTION__,
  639. $params,
  640. "Error: cannot login to " . $accessData['zimbraServer'],
  641. $login
  642. );
  643. return $login->getMessage();
  644. }
  645. $apiAccountManager = new Zm_Account($api);
  646. $response = $apiAccountManager->setAccountCos($accountName, $params['configoption1']);
  647. if(is_a($response, "Exception")) {
  648. logModuleCall(
  649. 'zimbrasingle',
  650. __FUNCTION__,
  651. $params,
  652. "Error: class of service for $accountName could not be set",
  653. $response
  654. );
  655. return "Error: class of service for $accountName could not be set";
  656. }
  657. return 'success';
  658. }
  659. /**
  660. * Define Zimbra product configuration options.
  661. *
  662. * Gather classes of service and available mail domains from the Zinbra server.
  663. * Calls a function to create all necessary customfields for the order form using the selected values.
  664. *
  665. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  666. *
  667. * @return array
  668. */
  669. function zimbraSingle_ConfigOptions($params)
  670. {
  671. if(isset($_POST['packageconfigoption'])) {
  672. if(zimbraSingleCreateCustomFields($_POST['packageconfigoption']) == false) {
  673. return false;
  674. };
  675. }
  676. $accessData = zimbraSingleGetAccess();
  677. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  678. $login = $api->login();
  679. if(is_a($login, "Exception")) {
  680. logModuleCall(
  681. 'zimbrasingle',
  682. __FUNCTION__,
  683. $params,
  684. "Error: cannot login to " . $accessData['zimbraServer'],
  685. $login
  686. );
  687. return false;
  688. }
  689. $apiAccountManager = new Zm_Account($api);
  690. $response = $apiAccountManager->getAllCos();
  691. if(is_a($response, "Exception")) {
  692. logModuleCall(
  693. 'zimbrasingle',
  694. __FUNCTION__,
  695. $params,
  696. "Error: could not fetch classes of service",
  697. $response
  698. );
  699. return false;
  700. }
  701. $cosNames = recursiveFindAll($response, 'NAME');
  702. $configOptions = array();
  703. $configOptions['cos'] = array(
  704. "FriendlyName" => "Class of Service",
  705. "Type" => "dropdown",
  706. "Options" => implode(',', $cosNames),
  707. "Description" => "Select COS",
  708. );
  709. $apiDomainManager = new Zm_Domain($api);
  710. $response = $apiDomainManager->getAllDomains();
  711. if(is_a($response, "Exception")) {
  712. logModuleCall(
  713. 'zimbrasingle',
  714. __FUNCTION__,
  715. $params,
  716. "Error: could fetch available maildomains",
  717. $response
  718. );
  719. return false;
  720. }
  721. $domainNames = recursiveFindAll($response, 'NAME');
  722. $configOptions['maildomains'] = array(
  723. "FriendlyName" => "Mail Domain",
  724. "Type" => "dropdown",
  725. "Multiple" => true,
  726. "Options" => implode(',', $domainNames),
  727. "Description" => "Info: Available Maildomains",
  728. );
  729. return $configOptions;
  730. }