zimbraSingle.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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($userData) {
  261. $accessData = zimbraSingleGetAccess();
  262. $passDecrypt = localAPI('DecryptPassword', array('password2' => $userData['password']));
  263. if ($passDecrypt['result'] == 'success') {
  264. $userData['password'] = $passDecrypt['password'];
  265. }
  266. if ($checkPW = zimbraSingleCheckPassword($userData['password'])) {
  267. return $checkPW;
  268. }
  269. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  270. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  271. $login = $api->login();
  272. if(is_a($login, "Exception")) {
  273. logModuleCall(
  274. 'zimbrasingle',
  275. __FUNCTION__,
  276. $params,
  277. "Error : cannot login to " . $accessData['zimbraServer'],
  278. ""
  279. );
  280. return false;
  281. }
  282. $apiAccountManager = new Zm_Account($api);
  283. $response = $apiAccountManager->setAccountPassword($account_name, $userData['password']);
  284. if(is_a($response, "Exception")) {
  285. logModuleCall(
  286. 'zimbrasingle',
  287. __FUNCTION__,
  288. $params,
  289. "Error : password for $account_name could not be set",
  290. ""
  291. );
  292. return false;
  293. }
  294. return 'success';
  295. }
  296. function zimbraSingleChangePackage($userData) {
  297. $accessData = zimbraSingleGetAccess();
  298. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  299. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  300. $login = $api->login();
  301. if(is_a($login, "Exception")) {
  302. logModuleCall(
  303. 'zimbrasingle',
  304. __FUNCTION__,
  305. $params,
  306. "Error : cannot login to " . $accessData['zimbraServer'],
  307. ""
  308. );
  309. return false;
  310. }
  311. $apiAccountManager = new Zm_Account($api);
  312. $response = $apiAccountManager->setAccountCos($account_name, $userData['cos']);
  313. if(is_a($response, "Exception")) {
  314. logModuleCall(
  315. 'zimbrasingle',
  316. __FUNCTION__,
  317. $params,
  318. "Error : class of service for $account_name could not be set",
  319. ""
  320. );
  321. return false;
  322. }
  323. return $response;
  324. }
  325. function zimbraSingleClientArea($userData)
  326. {
  327. $accessData = zimbraSingleGetAccess();
  328. $clientInfo = array();
  329. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  330. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  331. $login = $api->login();
  332. if(is_a($login, "Exception")) {
  333. logModuleCall(
  334. 'zimbrasingle',
  335. __FUNCTION__,
  336. $params,
  337. "Error : cannot login to " . $accessData['zimbraServer'],
  338. ""
  339. );
  340. return false;
  341. }
  342. $apiAccountManager = new Zm_Account($api);
  343. $quota = $apiAccountManager->getQuota($account_name);
  344. if(is_a($quota, "Exception")) {
  345. logModuleCall(
  346. 'zimbrasingle',
  347. __FUNCTION__,
  348. $params,
  349. "Error : could not find $account_name",
  350. ""
  351. );
  352. return false;
  353. }
  354. $response = $apiAccountManager->getMailbox($account_name);
  355. if(is_a($response, "Exception")) {
  356. logModuleCall(
  357. 'zimbrasingle',
  358. __FUNCTION__,
  359. $params,
  360. "Error : could not fetch mailbox info for $account_name",
  361. ""
  362. );
  363. return false;
  364. }
  365. $mboxSize = $response['S'];
  366. $usagePercent = $mboxSize * 100 / $quota;
  367. $clientInfo['quota'] = bytesToHuman($quota);
  368. $clientInfo['size'] = bytesToHuman($mboxSize);
  369. $clientInfo['usage'] = round($usagePercent, 2);
  370. $response = $apiAccountManager->getAccountInfo($account_name);
  371. if(is_a($response, "Exception")) {
  372. logModuleCall(
  373. 'zimbrasingle',
  374. __FUNCTION__,
  375. $params,
  376. "Error : could not gather informations for $account_name",
  377. ""
  378. );
  379. return false;
  380. }
  381. $webmailUrl = recursiveFindAll( $response, 'PUBLICMAILURL');
  382. $clientInfo['webmailurl'] = $webmailUrl[0]['DATA'];
  383. return $clientInfo;
  384. }
  385. function zimbraSingleConfigOptions($params) {
  386. $accessData = zimbraSingleGetAccess();
  387. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  388. $login = $api->login();
  389. if(is_a($login, "Exception")) {
  390. logModuleCall(
  391. 'zimbrasingle',
  392. __FUNCTION__,
  393. $params,
  394. "Error : cannot login to " . $accessData['zimbraServer'],
  395. ""
  396. );
  397. return false;
  398. }
  399. $apiAccountManager = new Zm_Account($api);
  400. $response = $apiAccountManager->getAllCos();
  401. if(is_a($response, "Exception")) {
  402. logModuleCall(
  403. 'zimbrasingle',
  404. __FUNCTION__,
  405. $params,
  406. "Error : could not fetch classes of service",
  407. ""
  408. );
  409. return false;
  410. }
  411. $cosNames = recursiveFindAll($response, 'NAME');
  412. $configOptions = array();
  413. $configOptions['cos'] = array(
  414. "FriendlyName" => "Class of Service",
  415. "Type" => "dropdown",
  416. "Options" => implode(',', $cosNames),
  417. "Description" => "Select COS",
  418. );
  419. $apiDomainManager = new Zm_Domain($api);
  420. $response = $apiDomainManager->getAllDomains();
  421. if(is_a($response, "Exception")) {
  422. logModuleCall(
  423. 'zimbrasingle',
  424. __FUNCTION__,
  425. $params,
  426. "Error : could fetch available maildomains",
  427. ""
  428. );
  429. return false;
  430. }
  431. $domainNames = recursiveFindAll($response, 'NAME');
  432. $configOptions['maildomains'] = array(
  433. "FriendlyName" => "Mail Domain",
  434. "Type" => "dropdown",
  435. "Multiple" => true,
  436. "Options" => implode(',', $domainNames),
  437. "Description" => "select maildomains",
  438. );
  439. return $configOptions;
  440. }
  441. function zimbraSingleCreateCustomFields($packageconfigoption)
  442. {
  443. $whmcs = App::self();
  444. $productID = $whmcs->get_req_var('id');
  445. Capsule::table('tblcustomfields')
  446. ->where('relid', '=', $productID)
  447. ->delete();
  448. Capsule::table('tblcustomfields')
  449. ->insert(
  450. array(
  451. 'type' => 'product',
  452. 'relid' => $productID,
  453. 'fieldname' => 'givenname | Vorname',
  454. 'fieldtype' => 'text',
  455. 'required' => 'on',
  456. 'showorder' => 'on',
  457. 'sortorder' => '0'
  458. )
  459. );
  460. Capsule::table('tblcustomfields')
  461. ->insert(
  462. array(
  463. 'type' => 'product',
  464. 'relid' => $productID,
  465. 'fieldname' => 'sn | Nachname',
  466. 'fieldtype' => 'text',
  467. 'required' => 'on',
  468. 'showorder' => 'on',
  469. 'sortorder' => '1'
  470. )
  471. );
  472. Capsule::table('tblcustomfields')
  473. ->insert(
  474. array(
  475. 'type' => 'product',
  476. 'relid' => $productID,
  477. 'fieldname' => 'username | E-Mail Name',
  478. 'fieldtype' => 'text',
  479. 'required' => 'on',
  480. 'showorder' => 'on',
  481. 'sortorder' => '2'
  482. )
  483. );
  484. Capsule::table('tblcustomfields')
  485. ->insert(
  486. array(
  487. 'type' => 'product',
  488. 'relid' => $productID,
  489. 'fieldname' => 'maildomain | Mail Domaine',
  490. 'fieldtype' => 'dropdown',
  491. 'fieldoptions' => implode(',', $packageconfigoption[2]),
  492. 'required' => 'on',
  493. 'showorder' => 'on',
  494. 'sortorder' => '3'
  495. )
  496. );
  497. Capsule::table('tblcustomfields')
  498. ->insert(
  499. array(
  500. 'type' => 'product',
  501. 'relid' => $productID,
  502. 'fieldname' => 'password | Password',
  503. 'fieldtype' => 'password',
  504. 'required' => 'on',
  505. 'showorder' => 'on',
  506. 'sortorder' => '4'
  507. )
  508. );
  509. Capsule::table('tblcustomfields')
  510. ->insert(
  511. array(
  512. 'type' => 'product',
  513. 'relid' => $productID,
  514. 'fieldname' => 'pwrepeat | Password wiederholen',
  515. 'fieldtype' => 'password',
  516. 'required' => 'on',
  517. 'showorder' => 'on',
  518. 'sortorder' => '5'
  519. )
  520. );
  521. Capsule::table('tblcustomfields')
  522. ->insert(
  523. array(
  524. 'type' => 'product',
  525. 'relid' => $productID,
  526. 'fieldname' => 'cos | Class of Service',
  527. 'fieldtype' => 'dropdown',
  528. 'fieldoptions' => $packageconfigoption[1],
  529. 'adminonly' => 'on',
  530. 'required' => 'on',
  531. 'sortorder' => '6'
  532. )
  533. );
  534. }
  535. function recursiveFindAll($haystack, $needle)
  536. {
  537. $values = array();
  538. $iterator = new RecursiveArrayIterator($haystack);
  539. $recursive = new RecursiveIteratorIterator(
  540. $iterator,
  541. RecursiveIteratorIterator::SELF_FIRST
  542. );
  543. foreach ($recursive as $key => $value) {
  544. if ($key === $needle) {
  545. array_push($values, $value);
  546. }
  547. }
  548. return $values;
  549. }
  550. function zimbraSingleCheckPassword($pwd)
  551. {
  552. $message = '';
  553. if (strlen($pwd) < 8) {
  554. $message .= "Das das Passwort ist zu kurz. Es werden mind. 8 Zeichen benötigt" . PHP_EOL;
  555. }
  556. if (!preg_match("#[0-9]+#", $pwd)) {
  557. $message .= "Das Passwort muss mindestens eine Zahl enthalten" . PHP_EOL;
  558. }
  559. if (!preg_match("#[A-Z]+#", $pwd)) {
  560. $message .= "Das Passwort muss mindestens einen Grossbuchstaben (A-Z) enthalten" . PHP_EOL;
  561. }
  562. if (!preg_match("#[a-z]+#", $pwd)) {
  563. $message .= "Das Passwort muss mindestens einen Kleinbuchstaben (a-z) enthalten" . PHP_EOL;
  564. }
  565. if (!preg_match("#[^\w]+#", $pwd)) {
  566. $message .= "Das Passwort muss mindestens ein Sonderzeichen (.,-:=) enthalten" . PHP_EOL;
  567. }
  568. return $message;
  569. }
  570. function bytesToHuman($bytes)
  571. {
  572. $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  573. for ($i = 0; $bytes > 1024; $i++) $bytes /= 1024;
  574. return round($bytes, 2) . ' ' . $units[$i];
  575. }