zimbraSingle.php 27 KB

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