zimbraSingle.inc 18 KB

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