zimbraSingle.inc 17 KB

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