zimbraSingle.inc 18 KB

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