zimbraSingle.inc 18 KB

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