zimbraSingle.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. <?php
  2. /**
  3. * WHMCS Zimbra Provisioning Module
  4. *
  5. * Provisioning for private mailboxes on a Zimbra Server
  6. *
  7. * @see https://www.zimbra.com
  8. * @copyright Copyright (c) Thurdata GmbH 2020
  9. * @license GPL
  10. */
  11. if (!defined("WHMCS")) {
  12. die("This file cannot be accessed directly");
  13. }
  14. use WHMCS\Database\Capsule;
  15. /**
  16. * Requires this PHP api to make soap calls and parse responses
  17. * This is an extend version of:
  18. * @see https://github.com/alloylab/zimbra-admin-api-soap-php
  19. */
  20. require_once("api/Zm/Auth.php");
  21. require_once("api/Zm/Account.php");
  22. require_once("api/Zm/Domain.php");
  23. require_once("api/Zm/Server.php");
  24. /**
  25. * Helper function to get the zimbra server access data from whmcs database
  26. *
  27. * @return array $accessData {
  28. * @type string 'zimbraServer' zimbra server IP address
  29. * @type string 'adminUser' zimbra admin username
  30. * @type string 'password' zimbra admin password
  31. * } or false in case of error
  32. */
  33. function zimbraSingleGetAccess()
  34. {
  35. $accessData = array('zimbraServer' => '', 'adminUser' => '', 'adminPass' => '');
  36. $whmcs = App::self();
  37. $serverGroupID = $whmcs->get_req_var('servergroup');
  38. $action = $whmcs->get_req_var('action');
  39. if(($action == 'module-settings') || ($action == 'ConfigOptions') || ($action == 'save')) {
  40. $productID = $whmcs->get_req_var('id');
  41. $serverGroupIDObj = Capsule::table('tblproducts')
  42. ->select('servergroup')
  43. ->where('id', '=', $productID)
  44. ->get();
  45. $serverGroupID = $serverGroupIDObj[0]->servergroup;
  46. $serverIDObj = Capsule::table('tblservergroupsrel')
  47. ->select('serverid')
  48. ->where('groupid', '=', $serverGroupID)
  49. ->get();
  50. $serverID = $serverIDObj[0]->serverid;
  51. } else {
  52. $id = $whmcs->get_req_var('id');
  53. $serverIDObj = Capsule::table('tblhosting')
  54. ->select('server')
  55. ->where('id', '=', $id)
  56. ->get();
  57. $serverID = $serverIDObj[0]->server;
  58. }
  59. $server = Capsule::table('tblservers')
  60. ->select('ipaddress', 'username', 'password')
  61. ->where('id', '=', $serverID)
  62. ->where('active', '=', 1)
  63. ->get();
  64. $accessData['zimbraServer'] = $server[0]->ipaddress;
  65. $accessData['adminUser'] = $server[0]->username;
  66. $adminPassCrypt = $server[0]->password;
  67. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $adminPassCrypt));
  68. if ($adminPassDecrypt['result'] == 'success') {
  69. $accessData['adminPass'] = $adminPassDecrypt['password'];
  70. } else {
  71. logModuleCall(
  72. 'zimbrasingle',
  73. __FUNCTION__,
  74. $adminPassCrypt,
  75. "Error: cloud not decrypt admin password" ,
  76. $adminPassDecrypt
  77. );
  78. return false;
  79. }
  80. return $accessData;
  81. }
  82. /**
  83. * Helper function creates all necessary custom fields depending on selected configuration options
  84. *
  85. * @param array $packageconfigoption {
  86. * @type string 1 class of service
  87. * @type string 2 comma seperated list of maildomains
  88. * }
  89. * @return bool true in case of success or false on any error
  90. */
  91. function zimbraSingleCreateCustomFields($packageconfigoption)
  92. {
  93. $whmcs = App::self();
  94. $productID = $whmcs->get_req_var('id');
  95. 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. $params,
  489. "Error: cannot login to $accessData[zimbraServer]",
  490. $login
  491. );
  492. return $login->getMessage();
  493. }
  494. $apiAccountManager = new Zm_Account($api);
  495. $accountExists = $apiAccountManager->accountExists($account_name);
  496. if(is_a($accountExists, "Exception")) {
  497. logModuleCall(
  498. 'zimbrasingle',
  499. __FUNCTION__,
  500. $params,
  501. "Error: could not check account availibility",
  502. $account_name
  503. );
  504. return false;
  505. }
  506. logModuleCall(
  507. 'zimbrasingle',
  508. __FUNCTION__,
  509. $account_name,
  510. "Debug",
  511. $accountExists
  512. );
  513. if($accountExists) {
  514. return 'Account already exist';
  515. }
  516. $attrs = array();
  517. $attrs["gn"] = $params['customfields']["givenname"];
  518. $attrs["sn"] = $params['customfields']["sn"];
  519. $attrs["displayName"] = $attrs["gn"] . " " . $attrs["sn"];
  520. $passDecrypt = localAPI('DecryptPassword', array('password2' => $params['customfields']['password']));
  521. if ($passDecrypt['result'] == 'success') {
  522. $params['customfields']['password'] = $passDecrypt['password'];
  523. } else {
  524. logModuleCall(
  525. 'zimbrasingle',
  526. __FUNCTION__,
  527. $params['customfields']['password'],
  528. "Error: could not decrypt password",
  529. $passDecrypt
  530. );
  531. return false;
  532. }
  533. $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  534. $cosID = $apiAccountManager->getCosId($params['customfields']['cos']);
  535. if(is_a($cosID, "Exception")) {
  536. logModuleCall(
  537. 'zimbrasingle',
  538. __FUNCTION__,
  539. $params['customfields']['cos'],
  540. "Error: serviceclass not available",
  541. $cosID
  542. );
  543. return false;
  544. }
  545. $attrs['zimbraCOSId'] = $cosID;
  546. $id = $apiAccountManager->createAccount($account_name, $params['customfields']['password'], $attrs);
  547. if(is_a($id, "Exception")) {
  548. logModuleCall(
  549. 'zimbrasingle',
  550. __FUNCTION__,
  551. $params,
  552. "Error: account $account_name not created",
  553. $id
  554. );
  555. return false;
  556. }
  557. return 'success';
  558. }
  559. /**
  560. * Set a Zimbra account to status locked.
  561. *
  562. * Called when a suspension is requested. This is invoked automatically by WHMCS
  563. * when a product becomes overdue on payment or can be called manually by admin
  564. * user.
  565. *
  566. * @param array $params common module parameters
  567. *
  568. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  569. *
  570. * @return string "success" or an error message
  571. */
  572. function zimbraSingle_SuspendAccount($params)
  573. {
  574. $accessData = zimbraSingleGetAccess();
  575. $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  576. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  577. $login = $api->login();
  578. if(is_a($login, "Exception")) {
  579. logModuleCall(
  580. 'zimbrasingle',
  581. __FUNCTION__,
  582. $params,
  583. "Error: cannot login to " . $accessData['zimbraServer'],
  584. $login
  585. );
  586. return $login->getMessage();
  587. }
  588. $apiAccountManager = new Zm_Account($api);
  589. $response = $apiAccountManager->setAccountStatus($account_name, "locked");
  590. if(is_a($response, "Exception")) {
  591. logModuleCall(
  592. 'zimbrasingle',
  593. __FUNCTION__,
  594. $params,
  595. "Error: account $account_name could not locked",
  596. $response
  597. );
  598. return false;
  599. }
  600. return 'success';
  601. }
  602. /**
  603. * Set a Zimbra account to status active.
  604. *
  605. * Called when an un-suspension is requested. This is invoked
  606. * automatically upon payment of an overdue invoice for a product, or
  607. * can be called manually by admin user.
  608. *
  609. * @param array $params common module parameters
  610. *
  611. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  612. *
  613. * @return string "success" or an error message
  614. */
  615. function zimbraSingle_UnsuspendAccount($params)
  616. {
  617. $accessData = zimbraSingleGetAccess();
  618. $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  619. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  620. $login = $api->login();
  621. if(is_a($login, "Exception")) {
  622. logModuleCall(
  623. 'zimbrasingle',
  624. __FUNCTION__,
  625. $params,
  626. "Error: cannot login to " . $accessData['zimbraServer'],
  627. $login
  628. );
  629. return $login->getMessage();
  630. }
  631. $apiAccountManager = new Zm_Account($api);
  632. $response = $apiAccountManager->setAccountStatus($account_name, "active");
  633. if(is_a($response, "Exception")) {
  634. logModuleCall(
  635. 'zimbrasingle',
  636. __FUNCTION__,
  637. $params,
  638. "Error: account $account_name could not unlocked",
  639. $response
  640. );
  641. return "Error: account $account_name could not unlocked";
  642. }
  643. return 'success';
  644. }
  645. /**
  646. * Removes a Zimbra account.
  647. *
  648. * Called when a termination is requested. This can be invoked automatically for
  649. * overdue products if enabled, or requested manually by an admin user.
  650. *
  651. * @param array $params common module parameters
  652. *
  653. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  654. *
  655. * @return string "success" or an error message
  656. */
  657. function zimbraSingle_TerminateAccount($params)
  658. {
  659. $accessData = zimbraSingleGetAccess();
  660. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  661. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  662. $login = $api->login();
  663. if(is_a($login, "Exception")) {
  664. logModuleCall(
  665. 'zimbrasingle',
  666. __FUNCTION__,
  667. $params,
  668. "Error: cannot login to " . $accessData['zimbraServer'],
  669. $login
  670. );
  671. return $login->getMessage();
  672. }
  673. $apiAccountManager = new Zm_Account($api);
  674. $response = $apiAccountManager->getAccountStatus($accountName);
  675. if(is_a($response, "Exception")) {
  676. logModuleCall(
  677. 'zimbrasingle',
  678. __FUNCTION__,
  679. $params,
  680. "Error: account $accountName could not verified",
  681. $response
  682. );
  683. return "Error : account $accountName could not verified";
  684. }
  685. if ($response != 'locked') {
  686. return "Account $accountName active, suspend account first!";
  687. }
  688. $response = $apiAccountManager->deleteAccount($accountName);
  689. if(is_a($response, "Exception")) {
  690. logModuleCall(
  691. 'zimbrasingle',
  692. __FUNCTION__,
  693. $params,
  694. "Error: account $accountName could not removed",
  695. $response
  696. );
  697. return "Error: account $accountName could not removed";
  698. }
  699. return 'success';
  700. }
  701. /**
  702. * Set a new class of service for a Zimbra account.
  703. *
  704. * Called to apply a change of the class of service. It
  705. * is called to provision upgrade or downgrade orders, as well as being
  706. * able to be invoked manually by an admin user.
  707. *
  708. * This same function is called for upgrades and downgrades of both
  709. * products and configurable options.
  710. *
  711. * @param array $params common module parameters
  712. *
  713. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  714. *
  715. * @return string "success" or an error message
  716. */
  717. function zimbraSingle_ChangePackage($params)
  718. {
  719. $accessData = zimbraSingleGetAccess();
  720. $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  721. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  722. $login = $api->login();
  723. if(is_a($login, "Exception")) {
  724. logModuleCall(
  725. 'zimbrasingle',
  726. __FUNCTION__,
  727. $params,
  728. "Error: cannot login to " . $accessData['zimbraServer'],
  729. $login
  730. );
  731. return $login->getMessage();
  732. }
  733. $apiAccountManager = new Zm_Account($api);
  734. $response = $apiAccountManager->setAccountCos($account_name, $params['customfields']['cos']);
  735. if(is_a($response, "Exception")) {
  736. logModuleCall(
  737. 'zimbrasingle',
  738. __FUNCTION__,
  739. $params,
  740. "Error: class of service for $account_name could not be set",
  741. $response
  742. );
  743. return "Error: class of service for $account_name could not be set";
  744. }
  745. return 'success';
  746. }
  747. /**
  748. * Define Zimbra product configuration options.
  749. *
  750. * Gather classes of service and available mail domains from the Zinbra server.
  751. * Calls a function to create all necessary customfields for the order form using the selected values.
  752. *
  753. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  754. *
  755. * @return array
  756. */
  757. function zimbraSingle_ConfigOptions($params)
  758. {
  759. if(isset($_POST['packageconfigoption'])) {
  760. if(zimbraSingleCreateCustomFields($_POST['packageconfigoption']) == false) {
  761. return false;
  762. };
  763. }
  764. $accessData = zimbraSingleGetAccess();
  765. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  766. $login = $api->login();
  767. if(is_a($login, "Exception")) {
  768. logModuleCall(
  769. 'zimbrasingle',
  770. __FUNCTION__,
  771. $params,
  772. "Error: cannot login to " . $accessData['zimbraServer'],
  773. $login
  774. );
  775. return false;
  776. }
  777. $apiAccountManager = new Zm_Account($api);
  778. $response = $apiAccountManager->getAllCos();
  779. if(is_a($response, "Exception")) {
  780. logModuleCall(
  781. 'zimbrasingle',
  782. __FUNCTION__,
  783. $params,
  784. "Error: could not fetch classes of service",
  785. $response
  786. );
  787. return false;
  788. }
  789. $cosNames = recursiveFindAll($response, 'NAME');
  790. $configOptions = array();
  791. $configOptions['cos'] = array(
  792. "FriendlyName" => "Class of Service",
  793. "Type" => "dropdown",
  794. "Options" => implode(',', $cosNames),
  795. "Description" => "Select COS",
  796. );
  797. $apiDomainManager = new Zm_Domain($api);
  798. $response = $apiDomainManager->getAllDomains();
  799. if(is_a($response, "Exception")) {
  800. logModuleCall(
  801. 'zimbrasingle',
  802. __FUNCTION__,
  803. $params,
  804. "Error: could fetch available maildomains",
  805. $response
  806. );
  807. return false;
  808. }
  809. $domainNames = recursiveFindAll($response, 'NAME');
  810. $configOptions['maildomains'] = array(
  811. "FriendlyName" => "Mail Domain",
  812. "Type" => "dropdown",
  813. "Multiple" => true,
  814. "Options" => implode(',', $domainNames),
  815. "Description" => "select maildomains",
  816. );
  817. return $configOptions;
  818. }