zimbraSingle.php 27 KB

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