zimbraSingle.inc 19 KB

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