zimbraSingle.php 27 KB

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