zimbraSingle.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. <?php
  2. /**
  3. * WHMCS Zimbra Provisioning Module
  4. *
  5. * Provisioning for private user accounts on the 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. require_once("api/Zm/Auth.php");
  16. require_once("api/Zm/Account.php");
  17. require_once("api/Zm/Domain.php");
  18. require_once("api/Zm/Server.php");
  19. function zimbraSingleGetAccess()
  20. {
  21. $accessData = array('zimbraServer' => '', 'adminUser' => '', 'adminPass' => '');
  22. $whmcs = App::self();
  23. $serverGroupID = $whmcs->get_req_var('servergroup');
  24. $action = $whmcs->get_req_var('action');
  25. logModuleCall(
  26. 'zimbrasingle',
  27. __FUNCTION__,
  28. $action,
  29. "Debug" ,
  30. $whmcs
  31. );
  32. if(($action == 'module-settings') || ($action == 'ConfigOptions') || ($action == 'save')) {
  33. $productID = $whmcs->get_req_var('id');
  34. $serverGroupIDObj = Capsule::table('tblproducts')
  35. ->select('servergroup')
  36. ->where('id', '=', $productID)
  37. ->get();
  38. $serverGroupID = $serverGroupIDObj[0]->servergroup;
  39. $serverIDObj = Capsule::table('tblservergroupsrel')
  40. ->select('serverid')
  41. ->where('groupid', '=', $serverGroupID)
  42. ->get();
  43. $serverID = $serverIDObj[0]->serverid;
  44. } else {
  45. $id = $whmcs->get_req_var('id');
  46. $serverIDObj = Capsule::table('tblhosting')
  47. ->select('server')
  48. ->where('id', '=', $id)
  49. ->get();
  50. $serverID = $serverIDObj[0]->server;
  51. }
  52. $server = Capsule::table('tblservers')
  53. ->select('ipaddress', 'username', 'password')
  54. ->where('id', '=', $serverID)
  55. ->where('active', '=', 1)
  56. ->get();
  57. $accessData['zimbraServer'] = $server[0]->ipaddress;
  58. $accessData['adminUser'] = $server[0]->username;
  59. $adminPassCrypt = $server[0]->password;
  60. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $adminPassCrypt));
  61. if ($adminPassDecrypt['result'] == 'success') {
  62. $accessData['adminPass'] = $adminPassDecrypt['password'];
  63. }
  64. return $accessData;
  65. }
  66. /**
  67. * Checks if a given email address in the given domain already exists
  68. *
  69. * @param $emailNameOnly The name before the @-sign only
  70. * @param $domainName The domain to search for existance of the email account
  71. * @return true if such an account was found or false if not
  72. */
  73. function zimbraSingleDoesEMailExist($emailNameOnly, $domainName)
  74. {
  75. $account_name = $emailNameOnly . "@" . $domainName;
  76. $accessData = zimbraSingleGetAccess();
  77. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  78. $login = $api->login();
  79. if(is_a($login, "Exception")) {
  80. logModuleCall(
  81. 'zimbrasingle',
  82. __FUNCTION__,
  83. $params,
  84. "Error : cannot login to " . $accessData['zimbraServer'],
  85. "$login->getMessage()"
  86. );
  87. exit();
  88. } else {
  89. $apiAccountManager = new Zm_Account($api);
  90. if( $apiAccountManager->accountExists($account_name)) {
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. }
  96. }
  97. function zimbraSingleCreateCustomFields($packageconfigoption)
  98. {
  99. $whmcs = App::self();
  100. $productID = $whmcs->get_req_var('id');
  101. Capsule::table('tblcustomfields')
  102. ->where('relid', '=', $productID)
  103. ->delete();
  104. Capsule::table('tblcustomfields')
  105. ->insert(
  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. ->insert(
  118. array(
  119. 'type' => 'product',
  120. 'relid' => $productID,
  121. 'fieldname' => 'sn | Nachname',
  122. 'fieldtype' => 'text',
  123. 'required' => 'on',
  124. 'showorder' => 'on',
  125. 'sortorder' => '1'
  126. )
  127. );
  128. Capsule::table('tblcustomfields')
  129. ->insert(
  130. array(
  131. 'type' => 'product',
  132. 'relid' => $productID,
  133. 'fieldname' => 'username | E-Mail Name',
  134. 'fieldtype' => 'text',
  135. 'required' => 'on',
  136. 'showorder' => 'on',
  137. 'sortorder' => '2'
  138. )
  139. );
  140. Capsule::table('tblcustomfields')
  141. ->insert(
  142. array(
  143. 'type' => 'product',
  144. 'relid' => $productID,
  145. 'fieldname' => 'maildomain | Mail Domaine',
  146. 'fieldtype' => 'dropdown',
  147. 'fieldoptions' => implode(',', $packageconfigoption[2]),
  148. 'required' => 'on',
  149. 'showorder' => 'on',
  150. 'sortorder' => '3'
  151. )
  152. );
  153. Capsule::table('tblcustomfields')
  154. ->insert(
  155. array(
  156. 'type' => 'product',
  157. 'relid' => $productID,
  158. 'fieldname' => 'password | Password',
  159. 'fieldtype' => 'password',
  160. 'required' => 'on',
  161. 'showorder' => 'on',
  162. 'sortorder' => '4'
  163. )
  164. );
  165. Capsule::table('tblcustomfields')
  166. ->insert(
  167. array(
  168. 'type' => 'product',
  169. 'relid' => $productID,
  170. 'fieldname' => 'pwrepeat | Password wiederholen',
  171. 'fieldtype' => 'password',
  172. 'required' => 'on',
  173. 'showorder' => 'on',
  174. 'sortorder' => '5'
  175. )
  176. );
  177. Capsule::table('tblcustomfields')
  178. ->insert(
  179. array(
  180. 'type' => 'product',
  181. 'relid' => $productID,
  182. 'fieldname' => 'cos | Class of Service',
  183. 'fieldtype' => 'dropdown',
  184. 'fieldoptions' => $packageconfigoption[1],
  185. 'adminonly' => 'on',
  186. 'required' => 'on',
  187. 'sortorder' => '6'
  188. )
  189. );
  190. }
  191. function recursiveFindAll($haystack, $needle)
  192. {
  193. $values = array();
  194. $iterator = new RecursiveArrayIterator($haystack);
  195. $recursive = new RecursiveIteratorIterator(
  196. $iterator,
  197. RecursiveIteratorIterator::SELF_FIRST
  198. );
  199. foreach ($recursive as $key => $value) {
  200. if ($key === $needle) {
  201. array_push($values, $value);
  202. }
  203. }
  204. return $values;
  205. }
  206. function zimbraSingleCheckPassword($pwd)
  207. {
  208. $message = '';
  209. if (strlen($pwd) < 8) {
  210. $message .= "Das das Passwort ist zu kurz. Es werden mind. 8 Zeichen benötigt" . PHP_EOL;
  211. }
  212. if (!preg_match("#[0-9]+#", $pwd)) {
  213. $message .= "Das Passwort muss mindestens eine Zahl enthalten" . PHP_EOL;
  214. }
  215. if (!preg_match("#[A-Z]+#", $pwd)) {
  216. $message .= "Das Passwort muss mindestens einen Grossbuchstaben (A-Z) enthalten" . PHP_EOL;
  217. }
  218. if (!preg_match("#[a-z]+#", $pwd)) {
  219. $message .= "Das Passwort muss mindestens einen Kleinbuchstaben (a-z) enthalten" . PHP_EOL;
  220. }
  221. if (!preg_match("#[^\w]+#", $pwd)) {
  222. $message .= "Das Passwort muss mindestens ein Sonderzeichen (.,-:=) enthalten" . PHP_EOL;
  223. }
  224. return $message;
  225. }
  226. function bytesToHuman($bytes)
  227. {
  228. $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  229. for ($i = 0; $bytes > 1024; $i++) $bytes /= 1024;
  230. return round($bytes, 2) . ' ' . $units[$i];
  231. }
  232. function zimbraSingle_MetaData()
  233. {
  234. return array(
  235. 'DisplayName' => 'Zimbra Single Mailbox Provisioning',
  236. 'APIVersion' => '1.2',
  237. 'DefaultNonSSLPort' => '7071',
  238. 'DefaultSSLPort' => '7071',
  239. 'RequiresServer' => true,
  240. 'ServiceSingleSignOnLabel' => 'Login to Zimbra',
  241. 'AdminSingleSignOnLabel' => 'Login to Zimbra Admin'
  242. );
  243. }
  244. function zimbraSingle_TestConnection($params)
  245. {
  246. $auth = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
  247. $login = $auth->login();
  248. if(is_a($login, "Exception")) {
  249. logModuleCall(
  250. 'zimbrasingle',
  251. __FUNCTION__,
  252. $params,
  253. "Connection test to " . $params['serverip'] . " failed: Cannot login",
  254. $login->getMessage()
  255. );
  256. return array(
  257. 'success' => false,
  258. 'error' => "Connection test to " . $params['serverip'] . " failed, the error was: " . $login->getMessage(),
  259. );
  260. } else {
  261. return array(
  262. 'success' => true,
  263. 'error' => '',
  264. );
  265. }
  266. }
  267. function zimbraSingle_ClientArea($params)
  268. {
  269. $accessData = zimbraSingleGetAccess();
  270. $clientInfo = array();
  271. $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  272. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  273. $login = $api->login();
  274. if(is_a($login, "Exception")) {
  275. logModuleCall(
  276. 'zimbrasingle',
  277. __FUNCTION__,
  278. $params,
  279. "Error: cannot login to " . $accessData['zimbraServer'],
  280. $login
  281. );
  282. return false;
  283. }
  284. $apiAccountManager = new Zm_Account($api);
  285. $quota = $apiAccountManager->getQuota($account_name);
  286. if(is_a($quota, "Exception")) {
  287. logModuleCall(
  288. 'zimbrasingle',
  289. __FUNCTION__,
  290. $params,
  291. "Error : could not find $account_name",
  292. $quota
  293. );
  294. return false;
  295. }
  296. $response = $apiAccountManager->getMailbox($account_name);
  297. if(is_a($response, "Exception")) {
  298. logModuleCall(
  299. 'zimbrasingle',
  300. __FUNCTION__,
  301. $params,
  302. "Error : could not fetch mailbox info for $account_name",
  303. $response
  304. );
  305. return false;
  306. }
  307. $mboxSize = $response['S'];
  308. $usagePercent = $mboxSize * 100 / $quota;
  309. $clientInfo['quota'] = bytesToHuman($quota);
  310. $clientInfo['size'] = bytesToHuman($mboxSize);
  311. $clientInfo['usage'] = round($usagePercent, 2);
  312. $response = $apiAccountManager->getAccountInfo($account_name);
  313. if(is_a($response, "Exception")) {
  314. logModuleCall(
  315. 'zimbrasingle',
  316. __FUNCTION__,
  317. $params,
  318. "Error : could not gather informations for $account_name",
  319. $response
  320. );
  321. return false;
  322. }
  323. $webmailUrl = recursiveFindAll( $response, 'PUBLICMAILURL');
  324. $clientInfo['webmailurl'] = $webmailUrl[0]['DATA'];
  325. return array(
  326. 'templatefile' => 'clientarea',
  327. 'vars' => $clientInfo,
  328. );
  329. }
  330. function zimbraSingle_ChangePassword($params)
  331. {
  332. $accessData = zimbraSingleGetAccess();
  333. if ($checkPW = zimbraSingleCheckPassword($params['password'])) {
  334. return $checkPW;
  335. }
  336. $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  337. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  338. $login = $api->login();
  339. if(is_a($login, "Exception")) {
  340. logModuleCall(
  341. 'zimbrasingle',
  342. __FUNCTION__,
  343. $params,
  344. "Error : cannot login to " . $accessData['zimbraServer'],
  345. $login
  346. );
  347. return false;
  348. }
  349. $apiAccountManager = new Zm_Account($api);
  350. $response = $apiAccountManager->setAccountPassword($account_name, $params['password']);
  351. if(is_a($response, "Exception")) {
  352. logModuleCall(
  353. 'zimbrasingle',
  354. __FUNCTION__,
  355. $params,
  356. "Error : password for $account_name could not be set",
  357. $response
  358. );
  359. return false;
  360. }
  361. return 'success';
  362. }
  363. function zimbraSingle_CreateAccount($params)
  364. {
  365. $accessData = zimbraSingleGetAccess();
  366. $attrs = array();
  367. $attrs["gn"] = $params['customfields']["givenname"];
  368. $attrs["sn"] = $params['customfields']["sn"];
  369. $attrs["displayName"] = $attrs["gn"] . " " . $attrs["sn"];
  370. $passDecrypt = localAPI('DecryptPassword', array('password2' => $params['customfields']['password']));
  371. if ($passDecrypt['result'] == 'success') {
  372. $params['customfields']['password'] = $passDecrypt['password'];
  373. }
  374. $account_name = $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. ""
  384. );
  385. return false;
  386. }
  387. $apiAccountManager = new Zm_Account($api);
  388. $cosName = $params['customfields']['cos'];
  389. $cosID = $apiAccountManager->getCosId($cosName);
  390. if(is_a($cosID, "Exception")) {
  391. logModuleCall(
  392. 'zimbrasingle',
  393. __FUNCTION__,
  394. $params,
  395. "Error : serviceclass $cosName not available",
  396. $params['customfields']['cos']
  397. );
  398. return false;
  399. }
  400. $attrs['zimbraCOSId'] = $cosID;
  401. $id = $apiAccountManager->createAccount($account_name, $params['customfields']['password'], $attrs);
  402. if(is_a($id, "Exception")) {
  403. logModuleCall(
  404. 'zimbrasingle',
  405. __FUNCTION__,
  406. $params,
  407. "Error : account $account_name not created",
  408. $id
  409. );
  410. return false;
  411. }
  412. return 'success';
  413. }
  414. function zimbraSingle_SuspendAccount($params)
  415. {
  416. $accessData = zimbraSingleGetAccess();
  417. $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  418. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  419. $login = $api->login();
  420. if(is_a($login, "Exception")) {
  421. logModuleCall(
  422. 'zimbrasingle',
  423. __FUNCTION__,
  424. $params,
  425. "Error : cannot login to " . $accessData['zimbraServer'],
  426. $login
  427. );
  428. return false;
  429. }
  430. $apiAccountManager = new Zm_Account($api);
  431. $response = $apiAccountManager->setAccountStatus($account_name, "locked");
  432. if(is_a($response, "Exception")) {
  433. logModuleCall(
  434. 'zimbrasingle',
  435. __FUNCTION__,
  436. $params,
  437. "Error : account $account_name could not locked",
  438. $response
  439. );
  440. return false;
  441. }
  442. return 'success';
  443. }
  444. function zimbraSingle_UnsuspendAccount($params)
  445. {
  446. $accessData = zimbraSingleGetAccess();
  447. $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  448. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  449. $login = $api->login();
  450. if(is_a($login, "Exception")) {
  451. logModuleCall(
  452. 'zimbrasingle',
  453. __FUNCTION__,
  454. $params,
  455. "Error : cannot login to " . $accessData['zimbraServer'],
  456. $login
  457. );
  458. return false;
  459. }
  460. $apiAccountManager = new Zm_Account($api);
  461. $response = $apiAccountManager->setAccountStatus($account_name, "active");
  462. if(is_a($response, "Exception")) {
  463. logModuleCall(
  464. 'zimbrasingle',
  465. __FUNCTION__,
  466. $params,
  467. "Error : account $account_name could not unlocked",
  468. $response
  469. );
  470. return false;
  471. }
  472. return 'success';
  473. }
  474. function zimbraSingle_TerminateAccount($params)
  475. {
  476. $accessData = zimbraSingleGetAccess();
  477. $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  478. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  479. $login = $api->login();
  480. if(is_a($login, "Exception")) {
  481. logModuleCall(
  482. 'zimbrasingle',
  483. __FUNCTION__,
  484. $params,
  485. "Error : cannot login to " . $accessData['zimbraServer'],
  486. $login
  487. );
  488. return false;
  489. }
  490. $apiAccountManager = new Zm_Account($api);
  491. $response = $apiAccountManager->getAccountStatus($accountName);
  492. if(is_a($response, "Exception")) {
  493. logModuleCall(
  494. 'zimbrasingle',
  495. __FUNCTION__,
  496. $params,
  497. "Error : account $accountName could not verified",
  498. $response
  499. );
  500. return false;
  501. }
  502. if ($response != 'locked') {
  503. return "Account $accountName active, suspend account first";
  504. }
  505. $response = $apiAccountManager->deleteAccount($accountName);
  506. if(is_a($response, "Exception")) {
  507. logModuleCall(
  508. 'zimbrasingle',
  509. __FUNCTION__,
  510. $params,
  511. "Error : account $accountName could not removed",
  512. $response
  513. );
  514. return false;
  515. }
  516. return 'success';
  517. }
  518. function zimbraSingle_ChangePackage($params)
  519. {
  520. $accessData = zimbraSingleGetAccess();
  521. $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
  522. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  523. $login = $api->login();
  524. if(is_a($login, "Exception")) {
  525. logModuleCall(
  526. 'zimbrasingle',
  527. __FUNCTION__,
  528. $params,
  529. "Error : cannot login to " . $accessData['zimbraServer'],
  530. $login
  531. );
  532. return false;
  533. }
  534. $apiAccountManager = new Zm_Account($api);
  535. $response = $apiAccountManager->setAccountCos($account_name, $params['customfields']['cos']);
  536. if(is_a($response, "Exception")) {
  537. logModuleCall(
  538. 'zimbrasingle',
  539. __FUNCTION__,
  540. $params,
  541. "Error : class of service for $account_name could not be set",
  542. $response
  543. );
  544. return false;
  545. }
  546. return 'success';
  547. }
  548. function zimbraSingle_ConfigOptions($params)
  549. {
  550. if(isset($_POST['packageconfigoption'])) {
  551. zimbraSingleCreateCustomFields($_POST['packageconfigoption']);
  552. }
  553. $accessData = zimbraSingleGetAccess();
  554. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  555. $login = $api->login();
  556. if(is_a($login, "Exception")) {
  557. logModuleCall(
  558. 'zimbrasingle',
  559. __FUNCTION__,
  560. $params,
  561. "Error: cannot login to " . $accessData['zimbraServer'],
  562. $login
  563. );
  564. return false;
  565. }
  566. $apiAccountManager = new Zm_Account($api);
  567. $response = $apiAccountManager->getAllCos();
  568. if(is_a($response, "Exception")) {
  569. logModuleCall(
  570. 'zimbrasingle',
  571. __FUNCTION__,
  572. $params,
  573. "Error: could not fetch classes of service",
  574. $response
  575. );
  576. return false;
  577. }
  578. $cosNames = recursiveFindAll($response, 'NAME');
  579. $configOptions = array();
  580. $configOptions['cos'] = array(
  581. "FriendlyName" => "Class of Service",
  582. "Type" => "dropdown",
  583. "Options" => implode(',', $cosNames),
  584. "Description" => "Select COS",
  585. );
  586. $apiDomainManager = new Zm_Domain($api);
  587. $response = $apiDomainManager->getAllDomains();
  588. if(is_a($response, "Exception")) {
  589. logModuleCall(
  590. 'zimbrasingle',
  591. __FUNCTION__,
  592. $params,
  593. "Error: could fetch available maildomains",
  594. $response
  595. );
  596. return false;
  597. }
  598. $domainNames = recursiveFindAll($response, 'NAME');
  599. $configOptions['maildomains'] = array(
  600. "FriendlyName" => "Mail Domain",
  601. "Type" => "dropdown",
  602. "Multiple" => true,
  603. "Options" => implode(',', $domainNames),
  604. "Description" => "select maildomains",
  605. );
  606. return $configOptions;
  607. }