zimbraSingle.inc 19 KB

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