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