zimbraSingle.php 28 KB

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