zimbraSingle.inc 19 KB

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