zimbraSingle.php 27 KB

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