zimbraSingle.php 28 KB

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