zimbraSingle.inc 19 KB

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