zimbraSingle.inc 18 KB

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