zimbraSingle.inc 19 KB

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