zimbraSingle.php 29 KB

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