zimbraSingle.inc 17 KB

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