zimbraSingle.inc 18 KB

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