zimbraSingle.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 get the zimbra server access data from whmcs database
  26. *
  27. * @return array $accessData {
  28. * @type string 'zimbraServer' zimbra server IP address
  29. * @type string 'adminUser' zimbra admin username
  30. * @type string 'password' zimbra admin password
  31. * } or false in case of error
  32. */
  33. function zimbraSingleGetAccess()
  34. {
  35. $accessData = array('zimbraServer' => '', 'adminUser' => '', 'adminPass' => '');
  36. $whmcs = App::self();
  37. $serverGroupID = $whmcs->get_req_var('servergroup');
  38. $action = $whmcs->get_req_var('action');
  39. if(($action == 'module-settings') || ($action == 'ConfigOptions') || ($action == 'save')) {
  40. $productID = $whmcs->get_req_var('id');
  41. $serverGroupIDObj = Capsule::table('tblproducts')
  42. ->select('servergroup')
  43. ->where('id', '=', $productID)
  44. ->get();
  45. $serverGroupID = $serverGroupIDObj[0]->servergroup;
  46. $serverIDObj = Capsule::table('tblservergroupsrel')
  47. ->select('serverid')
  48. ->where('groupid', '=', $serverGroupID)
  49. ->get();
  50. $serverID = $serverIDObj[0]->serverid;
  51. } else {
  52. $id = $whmcs->get_req_var('id');
  53. $serverIDObj = Capsule::table('tblhosting')
  54. ->select('server')
  55. ->where('id', '=', $id)
  56. ->get();
  57. $serverID = $serverIDObj[0]->server;
  58. }
  59. $server = Capsule::table('tblservers')
  60. ->select('ipaddress', 'username', 'password')
  61. ->where('id', '=', $serverID)
  62. ->where('active', '=', 1)
  63. ->get();
  64. $accessData['zimbraServer'] = $server[0]->ipaddress;
  65. $accessData['adminUser'] = $server[0]->username;
  66. $adminPassCrypt = $server[0]->password;
  67. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $adminPassCrypt));
  68. if ($adminPassDecrypt['result'] == 'success') {
  69. $accessData['adminPass'] = $adminPassDecrypt['password'];
  70. } else {
  71. logModuleCall(
  72. 'zimbrasingle',
  73. __FUNCTION__,
  74. $adminPassCrypt,
  75. "Error: cloud not decrypt admin password" ,
  76. $adminPassDecrypt
  77. );
  78. return false;
  79. }
  80. return $accessData;
  81. }
  82. /**
  83. * Helper function creates all necessary custom fields depending on selected configuration options
  84. *
  85. * @param array $packageconfigoption {
  86. * @type string 1 class of service
  87. * @type string 2 comma seperated list of maildomains
  88. * }
  89. * @return bool true in case of success or false on any error
  90. */
  91. function zimbraSingleCreateCustomFields($packageconfigoption)
  92. {
  93. $whmcs = App::self();
  94. $productID = $whmcs->get_req_var('id');
  95. try {
  96. Capsule::table('tblcustomfields')
  97. ->insert(
  98. array(
  99. 'type' => 'product',
  100. 'relid' => $productID,
  101. 'fieldname' => 'givenname | Vorname',
  102. 'fieldtype' => 'text',
  103. 'required' => 'on',
  104. 'showorder' => 'on',
  105. 'sortorder' => '0'
  106. )
  107. );
  108. Capsule::table('tblcustomfields')
  109. ->insert(
  110. array(
  111. 'type' => 'product',
  112. 'relid' => $productID,
  113. 'fieldname' => 'sn | Nachname',
  114. 'fieldtype' => 'text',
  115. 'required' => 'on',
  116. 'showorder' => 'on',
  117. 'sortorder' => '1'
  118. )
  119. );
  120. Capsule::table('tblcustomfields')
  121. ->insert(
  122. array(
  123. 'type' => 'product',
  124. 'relid' => $productID,
  125. 'fieldname' => 'username | E-Mail Name',
  126. 'fieldtype' => 'text',
  127. 'required' => 'on',
  128. 'showorder' => 'on',
  129. 'sortorder' => '2'
  130. )
  131. );
  132. Capsule::table('tblcustomfields')
  133. ->insert(
  134. array(
  135. 'type' => 'product',
  136. 'relid' => $productID,
  137. 'fieldname' => 'maildomain | Mail Domaine',
  138. 'fieldtype' => 'dropdown',
  139. 'fieldoptions' => implode(',', $packageconfigoption[2]),
  140. 'required' => 'on',
  141. 'showorder' => 'on',
  142. 'sortorder' => '3'
  143. )
  144. );
  145. Capsule::table('tblcustomfields')
  146. ->insert(
  147. array(
  148. 'type' => 'product',
  149. 'relid' => $productID,
  150. 'fieldname' => 'password | Password',
  151. 'fieldtype' => 'password',
  152. 'required' => 'on',
  153. 'showorder' => 'on',
  154. 'sortorder' => '4'
  155. )
  156. );
  157. Capsule::table('tblcustomfields')
  158. ->insert(
  159. array(
  160. 'type' => 'product',
  161. 'relid' => $productID,
  162. 'fieldname' => 'pwrepeat | Password wiederholen',
  163. 'fieldtype' => 'password',
  164. 'required' => 'on',
  165. 'showorder' => 'on',
  166. 'sortorder' => '5'
  167. )
  168. );
  169. Capsule::table('tblcustomfields')
  170. ->insert(
  171. array(
  172. 'type' => 'product',
  173. 'relid' => $productID,
  174. 'fieldname' => 'cos | Class of Service',
  175. 'fieldtype' => 'dropdown',
  176. 'fieldoptions' => $packageconfigoption[1],
  177. 'adminonly' => 'on',
  178. 'required' => 'on',
  179. 'sortorder' => '6'
  180. )
  181. );
  182. return true;
  183. } catch (\Exception $e) {
  184. logModuleCall(
  185. 'zimbrasingle',
  186. __FUNCTION__,
  187. $params,
  188. "Error: could not create custom fields",
  189. $e->getMessage()
  190. );
  191. return false;
  192. }
  193. }
  194. /**
  195. * Helper function to find values of a named key in a multidimensional array or object
  196. *
  197. * @param array $haystack mixed data
  198. * @param string $needle key to search for values
  199. * @return array of values
  200. */
  201. function recursiveFindAll($haystack, $needle)
  202. {
  203. $values = array();
  204. $iterator = new RecursiveArrayIterator($haystack);
  205. $recursive = new RecursiveIteratorIterator(
  206. $iterator,
  207. RecursiveIteratorIterator::SELF_FIRST
  208. );
  209. foreach ($recursive as $key => $value) {
  210. if ($key === $needle) {
  211. array_push($values, $value);
  212. }
  213. }
  214. return $values;
  215. }
  216. /**
  217. * Helper function to check a password strength
  218. *
  219. * @param string $pwd password to check
  220. * @return string $message what is missing in the password (empty if it is okay)
  221. */
  222. function zimbraSingleCheckPassword($pwd)
  223. {
  224. $message = '';
  225. if (strlen($pwd) < 8) {
  226. $message .= "Das das Passwort ist zu kurz. Es werden mind. 8 Zeichen benötigt" . PHP_EOL;
  227. }
  228. if (!preg_match("#[0-9]+#", $pwd)) {
  229. $message .= "Das Passwort muss mindestens eine Zahl enthalten" . PHP_EOL;
  230. }
  231. if (!preg_match("#[A-Z]+#", $pwd)) {
  232. $message .= "Das Passwort muss mindestens einen Grossbuchstaben (A-Z) enthalten" . PHP_EOL;
  233. }
  234. if (!preg_match("#[a-z]+#", $pwd)) {
  235. $message .= "Das Passwort muss mindestens einen Kleinbuchstaben (a-z) enthalten" . PHP_EOL;
  236. }
  237. if (!preg_match("#[^\w]+#", $pwd)) {
  238. $message .= "Das Passwort muss mindestens ein Sonderzeichen (.,-:=) enthalten" . PHP_EOL;
  239. }
  240. return $message;
  241. }
  242. /**
  243. * Convert raw byte value to human readable
  244. *
  245. * Helper function to convert byte in huam readable format
  246. *
  247. * @param int $bytes value in bytes
  248. * @return string value rounded and in human readable units
  249. */
  250. function bytesToHuman($bytes)
  251. {
  252. $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  253. for ($i = 0; $bytes > 1024; $i++) $bytes /= 1024;
  254. return round($bytes, 2) . ' ' . $units[$i];
  255. }
  256. /**
  257. * Define module related meta data.
  258. *
  259. * Values returned here are used to determine module related abilities and
  260. * settings.
  261. *
  262. * @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
  263. *
  264. * @return array
  265. */
  266. function zimbraSingle_MetaData()
  267. {
  268. return array(
  269. 'DisplayName' => 'Zimbra Single Mailbox Provisioning',
  270. 'APIVersion' => '1.2',
  271. 'DefaultNonSSLPort' => '7071',
  272. 'DefaultSSLPort' => '7071',
  273. 'RequiresServer' => true,
  274. 'ServiceSingleSignOnLabel' => 'Login to Zimbra',
  275. 'AdminSingleSignOnLabel' => 'Login to Zimbra Admin'
  276. );
  277. }
  278. /**
  279. * Test connection to a Zimbra server with the given server parameters.
  280. *
  281. * Allows an admin user to verify that an API connection can be
  282. * successfully made with the given configuration parameters for a
  283. * server.
  284. *
  285. * When defined in a module, a Test Connection button will appear
  286. * alongside the Server Type dropdown when adding or editing an
  287. * existing server.
  288. *
  289. * @param array $params common module parameters
  290. *
  291. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  292. *
  293. * @return array
  294. */
  295. function zimbraSingle_TestConnection($params)
  296. {
  297. $auth = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  298. $login = $auth->login();
  299. if(is_a($login, "Exception")) {
  300. logModuleCall(
  301. 'zimbrasingle',
  302. __FUNCTION__,
  303. $params,
  304. "Connection test to " . $params['serverip'] . " failed: Cannot login",
  305. $login->getMessage()
  306. );
  307. return array(
  308. 'success' => false,
  309. 'error' => "Connection test to " . $params['serverip'] . " failed, the error was: " . $login->getMessage(),
  310. );
  311. } else {
  312. return array(
  313. 'success' => true,
  314. 'error' => '',
  315. );
  316. }
  317. }
  318. /**
  319. * Client area output logic handling.
  320. *
  321. * This function is used to define module specific client area output. It should
  322. * return an array consisting of a template file and optional additional
  323. * template variables to make available to that template.
  324. *
  325. * The template file you return can be one of two types:
  326. *
  327. * * tabOverviewModuleOutputTemplate - The output of the template provided here
  328. * will be displayed as part of the default product/service client area
  329. * product overview page.
  330. *
  331. * * tabOverviewReplacementTemplate - Alternatively using this option allows you
  332. * to entirely take control of the product/service overview page within the
  333. * client area.
  334. *
  335. * Whichever option you choose, extra template variables are defined in the same
  336. * way. This demonstrates the use of the full replacement.
  337. *
  338. * Please Note: Using tabOverviewReplacementTemplate means you should display
  339. * the standard information such as pricing and billing details in your custom
  340. * template or they will not be visible to the end user.
  341. *
  342. * @param array $params common module parameters
  343. *
  344. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  345. *
  346. * @return array
  347. */
  348. function zimbraSingle_ClientArea($params)
  349. {
  350. $accessData = zimbraSingleGetAccess();
  351. $clientInfo = array();
  352. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  353. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  354. $login = $api->login();
  355. if(is_a($login, "Exception")) {
  356. logModuleCall(
  357. 'zimbrasingle',
  358. __FUNCTION__,
  359. $params,
  360. "Error: cannot login to " . $accessData['zimbraServer'],
  361. $login
  362. );
  363. return false;
  364. }
  365. $apiAccountManager = new Zm_Account($api);
  366. $quota = $apiAccountManager->getQuota($accountName);
  367. if(is_a($quota, "Exception")) {
  368. logModuleCall(
  369. 'zimbrasingle',
  370. __FUNCTION__,
  371. $params,
  372. "Error : could not find $accountName",
  373. $quota
  374. );
  375. return false;
  376. }
  377. $response = $apiAccountManager->getMailbox($accountName);
  378. if(is_a($response, "Exception")) {
  379. logModuleCall(
  380. 'zimbrasingle',
  381. __FUNCTION__,
  382. $params,
  383. "Error: could not fetch mailbox info for $accountName",
  384. $response
  385. );
  386. return false;
  387. }
  388. $mboxSize = $response['S'];
  389. $usagePercent = $mboxSize * 100 / $quota;
  390. $clientInfo['quota'] = bytesToHuman($quota);
  391. $clientInfo['size'] = bytesToHuman($mboxSize);
  392. $clientInfo['usage'] = round($usagePercent, 2);
  393. $response = $apiAccountManager->getAccountInfo($accountName);
  394. if(is_a($response, "Exception")) {
  395. logModuleCall(
  396. 'zimbrasingle',
  397. __FUNCTION__,
  398. $params,
  399. "Error: could not gather informations for $accountName",
  400. $response
  401. );
  402. return false;
  403. }
  404. $webmailUrl = recursiveFindAll( $response, 'PUBLICMAILURL');
  405. $clientInfo['webmailurl'] = $webmailUrl[0]['DATA'];
  406. return array(
  407. 'templatefile' => 'clientarea',
  408. 'vars' => $clientInfo,
  409. );
  410. }
  411. /**
  412. * Change the password for a Zimbra account.
  413. *
  414. * Called when a password change is requested. This can occur either due to a
  415. * client requesting it via the client area or an admin requesting it from the
  416. * admin side.
  417. *
  418. * This option is only available to client end users when the product is in an
  419. * active status.
  420. *
  421. * @param array $params common module parameters
  422. *
  423. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  424. *
  425. * @return string "success" or an error message
  426. */
  427. function zimbraSingle_ChangePassword($params)
  428. {
  429. $accessData = zimbraSingleGetAccess();
  430. if ($checkPW = zimbraSingleCheckPassword($params['password'])) {
  431. return $checkPW;
  432. }
  433. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  434. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  435. $login = $api->login();
  436. if(is_a($login, "Exception")) {
  437. logModuleCall(
  438. 'zimbrasingle',
  439. __FUNCTION__,
  440. $params,
  441. "Error: cannot login to " . $accessData['zimbraServer'],
  442. $login
  443. );
  444. return false;
  445. }
  446. $apiAccountManager = new Zm_Account($api);
  447. $response = $apiAccountManager->setAccountPassword($accountName, $params['password']);
  448. if(is_a($response, "Exception")) {
  449. logModuleCall(
  450. 'zimbrasingle',
  451. __FUNCTION__,
  452. $params,
  453. "Error: password for $accountName could not be set",
  454. $response
  455. );
  456. return false;
  457. }
  458. return 'success';
  459. }
  460. /**
  461. * Provision a new instance of a Zimbra account.
  462. *
  463. * Attempt to provision a new Zimbra mail account. This is
  464. * called any time provisioning is requested inside of WHMCS. Depending upon the
  465. * configuration, this can be any of:
  466. * * When a new order is placed
  467. * * When an invoice for a new order is paid
  468. * * Upon manual request by an admin user
  469. *
  470. * @param array $params common module parameters
  471. *
  472. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  473. *
  474. * @return string "success" or an error message
  475. */
  476. function zimbraSingle_CreateAccount($params)
  477. {
  478. $accessData = zimbraSingleGetAccess();
  479. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  480. $login = $api->login();
  481. if(is_a($login, "Exception")) {
  482. logModuleCall(
  483. 'zimbrasingle',
  484. __FUNCTION__,
  485. $accessData,
  486. "Error: cannot login to " . $accessData['zimbraServer'],
  487. $login->getMessage()
  488. );
  489. return "Error: cannot login to " . $accessData['zimbraServer'];
  490. }
  491. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  492. $apiAccountManager = new Zm_Account($api);
  493. $accountExists = $apiAccountManager->accountExists($accountName);
  494. if(is_a($accountExists, "Exception")) {
  495. logModuleCall(
  496. 'zimbrasingle',
  497. __FUNCTION__,
  498. $accessData,
  499. "Error: could not verify $accountName",
  500. $accountExists
  501. );
  502. return "Error: could not verify $accountName";
  503. }
  504. if($accountExists === true) {
  505. return "Error: account $accountName already exists";
  506. }
  507. $attrs = array();
  508. $attrs["gn"] = $params['customfields']["givenname"];
  509. $attrs["sn"] = $params['customfields']["sn"];
  510. $attrs["displayName"] = $attrs["gn"] . " " . $attrs["sn"];
  511. $passDecrypt = localAPI('DecryptPassword', array('password2' => $params['customfields']['password']));
  512. if ($passDecrypt['result'] == 'success') {
  513. $params['customfields']['password'] = $passDecrypt['password'];
  514. } else {
  515. logModuleCall(
  516. 'zimbrasingle',
  517. __FUNCTION__,
  518. $params['customfields']['password'],
  519. "Error: could not decrypt password",
  520. $passDecrypt
  521. );
  522. return "Error: could not decrypt password";
  523. }
  524. $cosID = $apiAccountManager->getCosId($params['configoption1']);
  525. if(is_a($cosID, "Exception")) {
  526. logModuleCall(
  527. 'zimbrasingle',
  528. __FUNCTION__,
  529. $params['configoption1'],
  530. "Error: serviceclass not available",
  531. $cosID
  532. );
  533. return "Error: serviceclass not available";
  534. }
  535. $attrs['zimbraCOSId'] = $cosID;
  536. $id = $apiAccountManager->createAccount($accountName, $params['customfields']['password'], $attrs);
  537. if(is_a($id, "Exception")) {
  538. logModuleCall(
  539. 'zimbrasingle',
  540. __FUNCTION__,
  541. $params,
  542. "Error: account $accountName not created",
  543. $id
  544. );
  545. return "Error: account $accountName not created";
  546. }
  547. return 'success';
  548. }
  549. /**
  550. * Set a Zimbra account to status locked.
  551. *
  552. * Called when a suspension is requested. This is invoked automatically by WHMCS
  553. * when a product becomes overdue on payment or can be called manually by admin
  554. * user.
  555. *
  556. * @param array $params common module parameters
  557. *
  558. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  559. *
  560. * @return string "success" or an error message
  561. */
  562. function zimbraSingle_SuspendAccount($params)
  563. {
  564. $accessData = zimbraSingleGetAccess();
  565. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  566. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  567. $login = $api->login();
  568. if(is_a($login, "Exception")) {
  569. logModuleCall(
  570. 'zimbrasingle',
  571. __FUNCTION__,
  572. $params,
  573. "Error: cannot login to " . $accessData['zimbraServer'],
  574. $login
  575. );
  576. return $login->getMessage();
  577. }
  578. $apiAccountManager = new Zm_Account($api);
  579. $response = $apiAccountManager->setAccountStatus($accountName, "locked");
  580. if(is_a($response, "Exception")) {
  581. logModuleCall(
  582. 'zimbrasingle',
  583. __FUNCTION__,
  584. $params,
  585. "Error: account $accountName could not locked",
  586. $response
  587. );
  588. return false;
  589. }
  590. return 'success';
  591. }
  592. /**
  593. * Set a Zimbra account to status active.
  594. *
  595. * Called when an un-suspension is requested. This is invoked
  596. * automatically upon payment of an overdue invoice for a product, or
  597. * can be called manually by admin user.
  598. *
  599. * @param array $params common module parameters
  600. *
  601. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  602. *
  603. * @return string "success" or an error message
  604. */
  605. function zimbraSingle_UnsuspendAccount($params)
  606. {
  607. $accessData = zimbraSingleGetAccess();
  608. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  609. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  610. $login = $api->login();
  611. if(is_a($login, "Exception")) {
  612. logModuleCall(
  613. 'zimbrasingle',
  614. __FUNCTION__,
  615. $params,
  616. "Error: cannot login to " . $accessData['zimbraServer'],
  617. $login
  618. );
  619. return $login->getMessage();
  620. }
  621. $apiAccountManager = new Zm_Account($api);
  622. $response = $apiAccountManager->setAccountStatus($accountName, "active");
  623. if(is_a($response, "Exception")) {
  624. logModuleCall(
  625. 'zimbrasingle',
  626. __FUNCTION__,
  627. $params,
  628. "Error: account $accountName could not unlocked",
  629. $response
  630. );
  631. return "Error: account $accountName could not unlocked";
  632. }
  633. return 'success';
  634. }
  635. /**
  636. * Removes a Zimbra account.
  637. *
  638. * Called when a termination is requested. This can be invoked automatically for
  639. * overdue products if enabled, or requested manually by an admin user.
  640. *
  641. * @param array $params common module parameters
  642. *
  643. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  644. *
  645. * @return string "success" or an error message
  646. */
  647. function zimbraSingle_TerminateAccount($params)
  648. {
  649. $accessData = zimbraSingleGetAccess();
  650. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  651. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  652. $login = $api->login();
  653. if(is_a($login, "Exception")) {
  654. logModuleCall(
  655. 'zimbrasingle',
  656. __FUNCTION__,
  657. $params,
  658. "Error: cannot login to " . $accessData['zimbraServer'],
  659. $login
  660. );
  661. return $login->getMessage();
  662. }
  663. $apiAccountManager = new Zm_Account($api);
  664. $response = $apiAccountManager->getAccountStatus($accountName);
  665. if(is_a($response, "Exception")) {
  666. logModuleCall(
  667. 'zimbrasingle',
  668. __FUNCTION__,
  669. $params,
  670. "Error: account $accountName could not verified",
  671. $response
  672. );
  673. return "Error : account $accountName could not verified";
  674. }
  675. if ($response != 'locked') {
  676. return "Account $accountName active, suspend account first!";
  677. }
  678. $response = $apiAccountManager->deleteAccount($accountName);
  679. if(is_a($response, "Exception")) {
  680. logModuleCall(
  681. 'zimbrasingle',
  682. __FUNCTION__,
  683. $params,
  684. "Error: account $accountName could not removed",
  685. $response
  686. );
  687. return "Error: account $accountName could not removed";
  688. }
  689. return 'success';
  690. }
  691. /**
  692. * Set a new class of service for a Zimbra account.
  693. *
  694. * Called to apply a change of the class of service. It
  695. * is called to provision upgrade or downgrade orders, as well as being
  696. * able to be invoked manually by an admin user.
  697. *
  698. * This same function is called for upgrades and downgrades of both
  699. * products and configurable options.
  700. *
  701. * @param array $params common module parameters
  702. *
  703. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  704. *
  705. * @return string "success" or an error message
  706. */
  707. function zimbraSingle_ChangePackage($params)
  708. {
  709. $accessData = zimbraSingleGetAccess();
  710. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  711. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  712. $login = $api->login();
  713. if(is_a($login, "Exception")) {
  714. logModuleCall(
  715. 'zimbrasingle',
  716. __FUNCTION__,
  717. $params,
  718. "Error: cannot login to " . $accessData['zimbraServer'],
  719. $login
  720. );
  721. return $login->getMessage();
  722. }
  723. $apiAccountManager = new Zm_Account($api);
  724. $response = $apiAccountManager->setAccountCos($accountName, $params['configoption1']);
  725. if(is_a($response, "Exception")) {
  726. logModuleCall(
  727. 'zimbrasingle',
  728. __FUNCTION__,
  729. $params,
  730. "Error: class of service for $accountName could not be set",
  731. $response
  732. );
  733. return "Error: class of service for $accountName could not be set";
  734. }
  735. return 'success';
  736. }
  737. /**
  738. * Define Zimbra product configuration options.
  739. *
  740. * Gather classes of service and available mail domains from the Zinbra server.
  741. * Calls a function to create all necessary customfields for the order form using the selected values.
  742. *
  743. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  744. *
  745. * @return array
  746. */
  747. function zimbraSingle_ConfigOptions($params)
  748. {
  749. if(isset($_POST['packageconfigoption'])) {
  750. if(zimbraSingleCreateCustomFields($_POST['packageconfigoption']) == false) {
  751. return false;
  752. };
  753. }
  754. $accessData = zimbraSingleGetAccess();
  755. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  756. $login = $api->login();
  757. if(is_a($login, "Exception")) {
  758. logModuleCall(
  759. 'zimbrasingle',
  760. __FUNCTION__,
  761. $params,
  762. "Error: cannot login to " . $accessData['zimbraServer'],
  763. $login
  764. );
  765. return false;
  766. }
  767. $apiAccountManager = new Zm_Account($api);
  768. $response = $apiAccountManager->getAllCos();
  769. if(is_a($response, "Exception")) {
  770. logModuleCall(
  771. 'zimbrasingle',
  772. __FUNCTION__,
  773. $params,
  774. "Error: could not fetch classes of service",
  775. $response
  776. );
  777. return false;
  778. }
  779. $cosNames = recursiveFindAll($response, 'NAME');
  780. $configOptions = array();
  781. $configOptions['cos'] = array(
  782. "FriendlyName" => "Class of Service",
  783. "Type" => "dropdown",
  784. "Options" => implode(',', $cosNames),
  785. "Description" => "Select COS",
  786. );
  787. $apiDomainManager = new Zm_Domain($api);
  788. $response = $apiDomainManager->getAllDomains();
  789. if(is_a($response, "Exception")) {
  790. logModuleCall(
  791. 'zimbrasingle',
  792. __FUNCTION__,
  793. $params,
  794. "Error: could fetch available maildomains",
  795. $response
  796. );
  797. return false;
  798. }
  799. $domainNames = recursiveFindAll($response, 'NAME');
  800. $configOptions['maildomains'] = array(
  801. "FriendlyName" => "Mail Domain",
  802. "Type" => "dropdown",
  803. "Multiple" => true,
  804. "Options" => implode(',', $domainNames),
  805. "Description" => "select maildomains",
  806. );
  807. return $configOptions;
  808. }