zimbraSingle.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. // Debug
  253. logModuleCall(
  254. 'zimbrasingle',
  255. __FUNCTION__,
  256. $whmcs,
  257. "Debug",
  258. $params
  259. );
  260. //
  261. $api = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  262. $login = $api->login();
  263. if(is_a($login, "Exception")) {
  264. logModuleCall(
  265. 'zimbrasingle',
  266. __FUNCTION__,
  267. $params,
  268. "Error: cannot login to " . $accessData['zimbraServer'],
  269. $login
  270. );
  271. return false;
  272. }
  273. $apiAccountManager = new Zm_Account($api);
  274. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  275. $response = $apiAccountManager->setAccountPassword($accountName, $params['password']);
  276. if(is_a($response, "Exception")) {
  277. logModuleCall(
  278. 'zimbrasingle',
  279. __FUNCTION__,
  280. $params,
  281. "Error: password for $accountName could not be set",
  282. $response
  283. );
  284. return false;
  285. }
  286. return 'success';
  287. }
  288. /**
  289. * Provision a new instance of a Zimbra account.
  290. *
  291. * Attempt to provision a new Zimbra mail account. This is
  292. * called any time provisioning is requested inside of WHMCS. Depending upon the
  293. * configuration, this can be any of:
  294. * * When a new order is placed
  295. * * When an invoice for a new order is paid
  296. * * Upon manual request by an admin user
  297. *
  298. * @param array $params common module parameters
  299. *
  300. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  301. *
  302. * @return string "success" or an error message
  303. */
  304. function zimbraSingle_CreateAccount($params)
  305. {
  306. $api = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  307. $login = $api->login();
  308. if(is_a($login, "Exception")) {
  309. logModuleCall(
  310. 'zimbrasingle',
  311. __FUNCTION__,
  312. $params,
  313. "Error: cannot login to " . $params['serverip'],
  314. $login
  315. );
  316. return $login->getMessage();
  317. }
  318. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  319. $apiAccountManager = new Zm_Account($api);
  320. $accountExists = $apiAccountManager->accountExists($accountName);
  321. if(is_a($accountExists, "Exception")) {
  322. logModuleCall(
  323. 'zimbrasingle',
  324. __FUNCTION__,
  325. $accessData,
  326. "Error: could not verify $accountName",
  327. $accountExists
  328. );
  329. return "Error: could not verify $accountName";
  330. }
  331. if($accountExists === true) {
  332. return "Error: account $accountName already exists";
  333. }
  334. $attrs = array();
  335. $attrs["gn"] = $params['customfields']["givenname"];
  336. $attrs["sn"] = $params['customfields']["sn"];
  337. $attrs["displayName"] = $attrs["gn"] . " " . $attrs["sn"];
  338. $passDecrypt = localAPI('DecryptPassword', array('password2' => $params['customfields']['password']));
  339. if ($passDecrypt['result'] == 'success') {
  340. $password = $passDecrypt['password'];
  341. } else {
  342. logModuleCall(
  343. 'zimbrasingle',
  344. __FUNCTION__,
  345. $params['customfields']['password'],
  346. "Error: could not decrypt password",
  347. $passDecrypt
  348. );
  349. return "Error: could not decrypt password";
  350. }
  351. $cosID = $apiAccountManager->getCosId($params['configoption1']);
  352. if(is_a($cosID, "Exception")) {
  353. logModuleCall(
  354. 'zimbrasingle',
  355. __FUNCTION__,
  356. $params['configoption1'],
  357. "Error: serviceclass not available",
  358. $cosID
  359. );
  360. return "Error: serviceclass not available";
  361. }
  362. $attrs['zimbraCOSId'] = $cosID;
  363. $id = $apiAccountManager->createAccount($accountName, $password, $attrs);
  364. if(is_a($id, "Exception")) {
  365. logModuleCall(
  366. 'zimbrasingle',
  367. __FUNCTION__,
  368. $params,
  369. "Error: account $accountName not created",
  370. $id
  371. );
  372. return "Error: account $accountName not created";
  373. }
  374. Capsule::table('tblhosting')
  375. ->where('id', '=', $params['serviceid'])
  376. ->update(
  377. array(
  378. 'username' => $accountName,
  379. 'password' => $params['customfields']['password'],
  380. )
  381. );
  382. return 'success';
  383. }
  384. /**
  385. * Set a Zimbra account to status locked.
  386. *
  387. * Called when a suspension is requested. This is invoked automatically by WHMCS
  388. * when a product becomes overdue on payment or can be called manually by admin
  389. * user.
  390. *
  391. * @param array $params common module parameters
  392. *
  393. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  394. *
  395. * @return string "success" or an error message
  396. */
  397. function zimbraSingle_SuspendAccount($params)
  398. {
  399. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  400. $api = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  401. $login = $api->login();
  402. if(is_a($login, "Exception")) {
  403. logModuleCall(
  404. 'zimbrasingle',
  405. __FUNCTION__,
  406. $params,
  407. "Error: cannot login to " . $params['serverip'],
  408. $login
  409. );
  410. return $login->getMessage();
  411. }
  412. $apiAccountManager = new Zm_Account($api);
  413. $response = $apiAccountManager->setAccountStatus($accountName, "locked");
  414. if(is_a($response, "Exception")) {
  415. logModuleCall(
  416. 'zimbrasingle',
  417. __FUNCTION__,
  418. $params,
  419. "Error: account $accountName could not locked",
  420. $response
  421. );
  422. return false;
  423. }
  424. return 'success';
  425. }
  426. /**
  427. * Set a Zimbra account to status active.
  428. *
  429. * Called when an un-suspension is requested. This is invoked
  430. * automatically upon payment of an overdue invoice for a product, or
  431. * can be called manually by admin user.
  432. *
  433. * @param array $params common module parameters
  434. *
  435. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  436. *
  437. * @return string "success" or an error message
  438. */
  439. function zimbraSingle_UnsuspendAccount($params)
  440. {
  441. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  442. $api = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  443. $login = $api->login();
  444. if(is_a($login, "Exception")) {
  445. logModuleCall(
  446. 'zimbrasingle',
  447. __FUNCTION__,
  448. $params,
  449. "Error: cannot login to " . $params['serverip'],
  450. $login
  451. );
  452. return $login->getMessage();
  453. }
  454. $apiAccountManager = new Zm_Account($api);
  455. $response = $apiAccountManager->setAccountStatus($accountName, "active");
  456. if(is_a($response, "Exception")) {
  457. logModuleCall(
  458. 'zimbrasingle',
  459. __FUNCTION__,
  460. $params,
  461. "Error: account $accountName could not unlocked",
  462. $response
  463. );
  464. return "Error: account $accountName could not unlocked";
  465. }
  466. return 'success';
  467. }
  468. /**
  469. * Removes a Zimbra account.
  470. *
  471. * Called when a termination is requested. This can be invoked automatically for
  472. * overdue products if enabled, or requested manually by an admin user.
  473. *
  474. * @param array $params common module parameters
  475. *
  476. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  477. *
  478. * @return string "success" or an error message
  479. */
  480. function zimbraSingle_TerminateAccount($params)
  481. {
  482. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  483. $api = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  484. $login = $api->login();
  485. if(is_a($login, "Exception")) {
  486. logModuleCall(
  487. 'zimbrasingle',
  488. __FUNCTION__,
  489. $params,
  490. "Error: cannot login to " . $params['serverip'],
  491. $login
  492. );
  493. return $login->getMessage();
  494. }
  495. $apiAccountManager = new Zm_Account($api);
  496. $response = $apiAccountManager->getAccountStatus($accountName);
  497. if(is_a($response, "Exception")) {
  498. logModuleCall(
  499. 'zimbrasingle',
  500. __FUNCTION__,
  501. $params,
  502. "Error: account $accountName could not verified",
  503. $response
  504. );
  505. return "Error : account $accountName could not verified";
  506. }
  507. if ($response != 'locked') {
  508. return "Account $accountName active, suspend account first!";
  509. }
  510. $response = $apiAccountManager->deleteAccount($accountName);
  511. if(is_a($response, "Exception")) {
  512. logModuleCall(
  513. 'zimbrasingle',
  514. __FUNCTION__,
  515. $params,
  516. "Error: account $accountName could not removed",
  517. $response
  518. );
  519. return "Error: account $accountName could not removed";
  520. }
  521. return 'success';
  522. }
  523. /**
  524. * Set a new class of service for a Zimbra account.
  525. *
  526. * Called to apply a change of the class of service. It
  527. * is called to provision upgrade or downgrade orders, as well as being
  528. * able to be invoked manually by an admin user.
  529. *
  530. * This same function is called for upgrades and downgrades of both
  531. * products and configurable options.
  532. *
  533. * @param array $params common module parameters
  534. *
  535. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  536. *
  537. * @return string "success" or an error message
  538. */
  539. function zimbraSingle_ChangePackage($params)
  540. {
  541. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  542. $api = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  543. $login = $api->login();
  544. if(is_a($login, "Exception")) {
  545. logModuleCall(
  546. 'zimbrasingle',
  547. __FUNCTION__,
  548. $params,
  549. "Error: cannot login to " . $params['serverip'],
  550. $login
  551. );
  552. return $login->getMessage();
  553. }
  554. $apiAccountManager = new Zm_Account($api);
  555. $response = $apiAccountManager->setAccountCos($accountName, $params['configoption1']);
  556. if(is_a($response, "Exception")) {
  557. logModuleCall(
  558. 'zimbrasingle',
  559. __FUNCTION__,
  560. $params,
  561. "Error: class of service for $accountName could not be set",
  562. $response
  563. );
  564. return "Error: class of service for $accountName could not be set";
  565. }
  566. return 'success';
  567. }
  568. /**
  569. * Define Zimbra product configuration options.
  570. *
  571. * Gather classes of service and available mail domains from the Zinbra server.
  572. * Calls a function to create all necessary customfields for the order form using the selected values.
  573. *
  574. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  575. *
  576. * @return array
  577. */
  578. function zimbraSingle_ConfigOptions($params)
  579. {
  580. $whmcs = App::self();
  581. $serverGroupID = $whmcs->get_req_var('servergroup');
  582. $serverIDObj = Capsule::table('tblservergroupsrel')
  583. ->select('serverid')
  584. ->where('groupid', '=', $serverGroupID)
  585. ->get();
  586. $serverIDArray = recursiveFindAll($serverIDObj,'serverid');
  587. $server = Capsule::table('tblservers')
  588. ->select('ipaddress', 'username', 'password')
  589. ->where('id', $serverIDArray)
  590. ->where('active', '=', 1)
  591. ->get();
  592. $accessData['zimbraServer'] = $server[0]->ipaddress;
  593. $accessData['adminUser'] = $server[0]->username;
  594. $adminPassCrypt = $server[0]->password;
  595. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $adminPassCrypt));
  596. if ($adminPassDecrypt['result'] == 'success') {
  597. $accessData['adminPass'] = $adminPassDecrypt['password'];
  598. } else {
  599. logModuleCall(
  600. 'zimbrasingle',
  601. __FUNCTION__,
  602. $adminPassCrypt,
  603. "Error: cloud not decrypt admin password" ,
  604. $adminPassDecrypt
  605. );
  606. return false;
  607. }
  608. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  609. $login = $api->login();
  610. if(is_a($login, "Exception")) {
  611. logModuleCall(
  612. 'zimbrasingle',
  613. __FUNCTION__,
  614. $params,
  615. "Error: cannot login to " . $accessData['zimbraServer'],
  616. $login
  617. );
  618. return false;
  619. }
  620. $apiAccountManager = new Zm_Account($api);
  621. $response = $apiAccountManager->getAllCos();
  622. if(is_a($response, "Exception")) {
  623. logModuleCall(
  624. 'zimbrasingle',
  625. __FUNCTION__,
  626. $params,
  627. "Error: could not fetch classes of service",
  628. $response
  629. );
  630. return false;
  631. }
  632. $cosNames = recursiveFindAll($response, 'NAME');
  633. $configOptions = array();
  634. $configOptions['cos'] = array(
  635. "FriendlyName" => "Class of Service",
  636. "Type" => "dropdown",
  637. "Options" => implode(',', $cosNames),
  638. "Description" => "Select COS",
  639. );
  640. return $configOptions;
  641. }