zimbraSingle.php 21 KB

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