zimbraSingle.php 21 KB

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