zimbraSingle.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. $api = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  390. $login = $api->login();
  391. if(is_a($login, "Exception")) {
  392. logModuleCall(
  393. 'zimbrasingle',
  394. __FUNCTION__,
  395. $params,
  396. "Error: cannot login to " . $params['serverip'],
  397. $login
  398. );
  399. return $login->getMessage();
  400. }
  401. $apiAccountManager = new Zm_Account($api);
  402. $response = $apiAccountManager->setAccountStatus($params['username'], "locked");
  403. if(is_a($response, "Exception")) {
  404. logModuleCall(
  405. 'zimbrasingle',
  406. __FUNCTION__,
  407. $params,
  408. "Error: account " . $params['username'] . " could not locked",
  409. $response
  410. );
  411. return false;
  412. }
  413. return 'success';
  414. }
  415. /**
  416. * Set a Zimbra account to status active.
  417. *
  418. * Called when an un-suspension is requested. This is invoked
  419. * automatically upon payment of an overdue invoice for a product, or
  420. * can be called manually by admin user.
  421. *
  422. * @param array $params common module parameters
  423. *
  424. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  425. *
  426. * @return string "success" or an error message
  427. */
  428. function zimbraSingle_UnsuspendAccount($params)
  429. {
  430. $api = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  431. $login = $api->login();
  432. if(is_a($login, "Exception")) {
  433. logModuleCall(
  434. 'zimbrasingle',
  435. __FUNCTION__,
  436. $params,
  437. "Error: cannot login to " . $params['serverip'],
  438. $login
  439. );
  440. return $login->getMessage();
  441. }
  442. $apiAccountManager = new Zm_Account($api);
  443. $response = $apiAccountManager->setAccountStatus($params['username'], "active");
  444. if(is_a($response, "Exception")) {
  445. logModuleCall(
  446. 'zimbrasingle',
  447. __FUNCTION__,
  448. $params,
  449. "Error: account " . $params['username'] . " could not unlocked",
  450. $response
  451. );
  452. return "Error: account " . $params['username'] . " could not unlocked";
  453. }
  454. return 'success';
  455. }
  456. /**
  457. * Removes a Zimbra account.
  458. *
  459. * Called when a termination is requested. This can be invoked automatically for
  460. * overdue products if enabled, or requested manually by an admin user.
  461. *
  462. * @param array $params common module parameters
  463. *
  464. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  465. *
  466. * @return string "success" or an error message
  467. */
  468. function zimbraSingle_TerminateAccount($params)
  469. {
  470. $api = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  471. $login = $api->login();
  472. if(is_a($login, "Exception")) {
  473. logModuleCall(
  474. 'zimbrasingle',
  475. __FUNCTION__,
  476. $params,
  477. "Error: cannot login to " . $params['serverip'],
  478. $login
  479. );
  480. return $login->getMessage();
  481. }
  482. $apiAccountManager = new Zm_Account($api);
  483. $response = $apiAccountManager->getAccountStatus($params['username']);
  484. if(is_a($response, "Exception")) {
  485. logModuleCall(
  486. 'zimbrasingle',
  487. __FUNCTION__,
  488. $params,
  489. "Error: account ". $params['username'] . " could not verified",
  490. $response
  491. );
  492. return "Error : account " . $params['username'] . " Name could not verified";
  493. }
  494. if ($response != 'locked') {
  495. return "Account ". $params['username'] . " active, suspend account first!";
  496. }
  497. $response = $apiAccountManager->deleteAccount($params['username']);
  498. if(is_a($response, "Exception")) {
  499. logModuleCall(
  500. 'zimbrasingle',
  501. __FUNCTION__,
  502. $params,
  503. "Error: account ". $params['username'] . " could not removed",
  504. $response
  505. );
  506. return "Error: account ". $params['username'] . " could not removed";
  507. }
  508. return 'success';
  509. }
  510. /**
  511. * Set a new class of service for a Zimbra account.
  512. *
  513. * Called to apply a change of the class of service. It
  514. * is called to provision upgrade or downgrade orders, as well as being
  515. * able to be invoked manually by an admin user.
  516. *
  517. * This same function is called for upgrades and downgrades of both
  518. * products and configurable options.
  519. *
  520. * @param array $params common module parameters
  521. *
  522. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  523. *
  524. * @return string "success" or an error message
  525. */
  526. function zimbraSingle_ChangePackage($params)
  527. {
  528. // Debug
  529. logModuleCall(
  530. 'zimbrasingle',
  531. __FUNCTION__,
  532. $whmcs,
  533. "Debug",
  534. $params
  535. );
  536. //
  537. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  538. $api = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  539. $login = $api->login();
  540. if(is_a($login, "Exception")) {
  541. logModuleCall(
  542. 'zimbrasingle',
  543. __FUNCTION__,
  544. $params,
  545. "Error: cannot login to " . $params['serverip'],
  546. $login
  547. );
  548. return $login->getMessage();
  549. }
  550. $apiAccountManager = new Zm_Account($api);
  551. $response = $apiAccountManager->setAccountCos($accountName, $params['configoption1']);
  552. if(is_a($response, "Exception")) {
  553. logModuleCall(
  554. 'zimbrasingle',
  555. __FUNCTION__,
  556. $params,
  557. "Error: class of service for $accountName could not be set",
  558. $response
  559. );
  560. return "Error: class of service for $accountName could not be set";
  561. }
  562. return 'success';
  563. }
  564. /**
  565. * Define Zimbra product configuration options.
  566. *
  567. * Gather classes of service and available mail domains from the Zinbra server.
  568. * Calls a function to create all necessary customfields for the order form using the selected values.
  569. *
  570. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  571. *
  572. * @return array
  573. */
  574. function zimbraSingle_ConfigOptions($params)
  575. {
  576. $whmcs = App::self();
  577. $serverGroupID = $whmcs->get_req_var('servergroup');
  578. $serverIDObj = Capsule::table('tblservergroupsrel')
  579. ->select('serverid')
  580. ->where('groupid', '=', $serverGroupID)
  581. ->get();
  582. $serverIDArray = recursiveFindAll($serverIDObj,'serverid');
  583. $server = Capsule::table('tblservers')
  584. ->select('ipaddress', 'username', 'password')
  585. ->where('id', $serverIDArray)
  586. ->where('active', '=', 1)
  587. ->get();
  588. $accessData['zimbraServer'] = $server[0]->ipaddress;
  589. $accessData['adminUser'] = $server[0]->username;
  590. $adminPassCrypt = $server[0]->password;
  591. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $adminPassCrypt));
  592. if ($adminPassDecrypt['result'] == 'success') {
  593. $accessData['adminPass'] = $adminPassDecrypt['password'];
  594. } else {
  595. logModuleCall(
  596. 'zimbrasingle',
  597. __FUNCTION__,
  598. $adminPassCrypt,
  599. "Error: cloud not decrypt admin password" ,
  600. $adminPassDecrypt
  601. );
  602. return false;
  603. }
  604. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  605. $login = $api->login();
  606. if(is_a($login, "Exception")) {
  607. logModuleCall(
  608. 'zimbrasingle',
  609. __FUNCTION__,
  610. $params,
  611. "Error: cannot login to " . $accessData['zimbraServer'],
  612. $login
  613. );
  614. return false;
  615. }
  616. $apiAccountManager = new Zm_Account($api);
  617. $response = $apiAccountManager->getAllCos();
  618. if(is_a($response, "Exception")) {
  619. logModuleCall(
  620. 'zimbrasingle',
  621. __FUNCTION__,
  622. $params,
  623. "Error: could not fetch classes of service",
  624. $response
  625. );
  626. return false;
  627. }
  628. $cosNames = recursiveFindAll($response, 'NAME');
  629. $configOptions = array();
  630. $configOptions['cos'] = array(
  631. "FriendlyName" => "Class of Service",
  632. "Type" => "dropdown",
  633. "Options" => implode(',', $cosNames),
  634. "Description" => "Select COS",
  635. );
  636. return $configOptions;
  637. }