zimbraSingle.inc 17 KB

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