zimbraSingle.php 27 KB

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