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. $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. $serverGroupID = $whmcs->get_req_var('servergroup');
  33. if($id) {
  34. $serverIDObj = Capsule::table('tblhosting')
  35. ->select('server')
  36. ->where($idSelector, '=', $id)
  37. ->get();
  38. $serverID = $serverIDObj[0]->server;
  39. } elseif($serverGroupID) {
  40. $serverIDObj = Capsule::table('tblservergroupsrel')
  41. ->select('serverid')
  42. ->where('groupid', '=', $serverGroupID)
  43. ->get();
  44. $serverID = $serverIDObj[0]->serverid;
  45. } elseif($packageid) {
  46. $serverIDObj = Capsule::table('tblhosting')
  47. ->select('server')
  48. ->where('packageid', '=', $packageid)
  49. ->get();
  50. $serverID = $serverIDObj[0]->server;
  51. } else {
  52. logModuleCall(
  53. 'zimbrasingle',
  54. __FUNCTION__,
  55. $params,
  56. "Error: could not get accessdata",
  57. ""
  58. );
  59. return false;
  60. }
  61. $server = Capsule::table('tblservers')
  62. ->select('ipaddress', 'username', 'password')
  63. ->where('id', '=', $serverID)
  64. ->where('active', '=', 1)
  65. ->get();
  66. $accessData['zimbraServer'] = $server[0]->ipaddress;
  67. $accessData['adminUser'] = $server[0]->username;
  68. $adminPassCrypt = $server[0]->password;
  69. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $adminPassCrypt));
  70. if ($adminPassDecrypt['result'] == 'success') {
  71. $accessData['adminPass'] = $adminPassDecrypt['password'];
  72. }
  73. return $accessData;
  74. }
  75. /**
  76. * Checks if a given email address in the given domain already exists
  77. *
  78. * @param $emailNameOnly The name before the @-sign only
  79. * @param $domainName The domain to search for existance of the email account
  80. * @return true if such an account was found or false if not
  81. */
  82. function zimbraSingleDoesEMailExist($emailNameOnly, $domainName)
  83. {
  84. $account_name = $emailNameOnly . "@" . $domainName;
  85. $accessData = zimbraSingleGetAccess();
  86. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  87. $login = $api->login();
  88. if(is_a($login, "Exception")) {
  89. logModuleCall(
  90. 'zimbrasingle',
  91. __FUNCTION__,
  92. $params,
  93. "Error : cannot login to " . $accessData['zimbraServer'],
  94. "$login->getMessage()"
  95. );
  96. exit();
  97. } else {
  98. $apiAccountManager = new Zm_Account($api);
  99. if( $apiAccountManager->accountExists($account_name)) {
  100. return true;
  101. } else {
  102. return false;
  103. }
  104. }
  105. }
  106. /**
  107. */
  108. function zimbraSingleCreateAccount($userData)
  109. {
  110. $accessData = zimbraSingleGetAccess();
  111. $attrs = array();
  112. $attrs["gn"] = $userData["givenname"];
  113. $attrs["sn"] = $userData["sn"];
  114. $attrs["displayName"] = $attrs["gn"] . " " . $attrs["sn"];
  115. $passDecrypt = localAPI('DecryptPassword', array('password2' => $userData['password']));
  116. if ($passDecrypt['result'] == 'success') {
  117. $userData['password'] = $passDecrypt['password'];
  118. }
  119. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  120. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  121. $login = $api->login();
  122. if(is_a($login, "Exception")) {
  123. logModuleCall(
  124. 'zimbrasingle',
  125. __FUNCTION__,
  126. $params,
  127. "Error : cannot login to " . $accessData['zimbraServer'],
  128. ""
  129. );
  130. return false;
  131. }
  132. $apiAccountManager = new Zm_Account($api);
  133. $cosName = $userData['cos'];
  134. $cosID = $apiAccountManager->getCosId($cosName);
  135. if(is_a($cosID, "Exception")) {
  136. logModuleCall(
  137. 'zimbrasingle',
  138. __FUNCTION__,
  139. $params,
  140. "Error : serviceclass $cosName not available",
  141. $userData
  142. );
  143. return false;
  144. }
  145. $attrs['zimbraCOSId'] = $cosID;
  146. $id = $apiAccountManager->createAccount($account_name, $userData['password'], $attrs);
  147. if(is_a($id, "Exception")) {
  148. logModuleCall(
  149. 'zimbrasingle',
  150. __FUNCTION__,
  151. $params,
  152. "Error : account $account_name not created",
  153. $id
  154. );
  155. return false;
  156. }
  157. return $id;
  158. }
  159. function zimbraSingleSuspendAccount($userData)
  160. {
  161. $accessData = zimbraSingleGetAccess();
  162. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  163. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  164. $login = $api->login();
  165. if(is_a($login, "Exception")) {
  166. logModuleCall(
  167. 'zimbrasingle',
  168. __FUNCTION__,
  169. $params,
  170. "Error : cannot login to " . $accessData['zimbraServer'],
  171. ""
  172. );
  173. return false;
  174. } else {
  175. $apiAccountManager = new Zm_Account($api);
  176. $response = $apiAccountManager->setAccountStatus($account_name, "locked");
  177. if(is_a($response, "Exception")) {
  178. logModuleCall(
  179. 'zimbrasingle',
  180. __FUNCTION__,
  181. $params,
  182. "Error : account $account_name could not locked",
  183. ""
  184. );
  185. return false;
  186. } else {
  187. return $response;
  188. }
  189. }
  190. }
  191. function zimbraSingleUnsuspendAccount($userData)
  192. {
  193. $accessData = zimbraSingleGetAccess();
  194. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  195. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  196. $login = $api->login();
  197. if(is_a($login, "Exception")) {
  198. logModuleCall(
  199. 'zimbrasingle',
  200. __FUNCTION__,
  201. $params,
  202. "Error : cannot login to " . $accessData['zimbraServer'],
  203. ""
  204. );
  205. return false;
  206. } else {
  207. $apiAccountManager = new Zm_Account($api);
  208. $response = $apiAccountManager->setAccountStatus($account_name, "active");
  209. if(is_a($response, "Exception")) {
  210. logModuleCall(
  211. 'zimbrasingle',
  212. __FUNCTION__,
  213. $params,
  214. "Error : account $account_name could not unlocked",
  215. ""
  216. );
  217. return false;
  218. } else {
  219. return $response;
  220. }
  221. }
  222. }
  223. function zimbraSingleDeleteAccount($userData)
  224. {
  225. $accessData = zimbraSingleGetAccess();
  226. $accountName = $userData['username'] . '@' . $userData['maildomain'];
  227. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  228. $login = $api->login();
  229. if(is_a($login, "Exception")) {
  230. logModuleCall(
  231. 'zimbrasingle',
  232. __FUNCTION__,
  233. $params,
  234. "Error : cannot login to " . $accessData['zimbraServer'],
  235. ""
  236. );
  237. return false;
  238. }
  239. $apiAccountManager = new Zm_Account($api);
  240. $response = $apiAccountManager->getAccountStatus($accountName);
  241. if(is_a($response, "Exception")) {
  242. logModuleCall(
  243. 'zimbrasingle',
  244. __FUNCTION__,
  245. $params,
  246. "Error : account $accountName could not verified",
  247. ""
  248. );
  249. return false;
  250. }
  251. if ($response != 'locked') {
  252. return "Account $accountName active, suspend account first";
  253. }
  254. $response = $apiAccountManager->deleteAccount($accountName);
  255. if(is_a($response, "Exception")) {
  256. logModuleCall(
  257. 'zimbrasingle',
  258. __FUNCTION__,
  259. $params,
  260. "Error : account $accountName could not removed",
  261. ""
  262. );
  263. return false;
  264. }
  265. return 'success';
  266. }
  267. function zimbraSingleChangePassword($userData) {
  268. $accessData = zimbraSingleGetAccess();
  269. $passDecrypt = localAPI('DecryptPassword', array('password2' => $userData['password']));
  270. if ($passDecrypt['result'] == 'success') {
  271. $userData['password'] = $passDecrypt['password'];
  272. }
  273. if ($checkPW = zimbraSingleCheckPassword($userData['password'])) {
  274. return $checkPW;
  275. }
  276. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  277. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  278. $login = $api->login();
  279. if(is_a($login, "Exception")) {
  280. logModuleCall(
  281. 'zimbrasingle',
  282. __FUNCTION__,
  283. $params,
  284. "Error : cannot login to " . $accessData['zimbraServer'],
  285. ""
  286. );
  287. return false;
  288. }
  289. $apiAccountManager = new Zm_Account($api);
  290. $response = $apiAccountManager->setAccountPassword($account_name, $userData['password']);
  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. 9 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. }