zimbraSingle.php 27 KB

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