zimbraSingle.inc 17 KB

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