zimbraSingle.php 21 KB

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