zimbraSingle.php 28 KB

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