zimbraSingle.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  206. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  207. $login = $api->login();
  208. if(is_a($login, "Exception")) {
  209. logModuleCall(
  210. 'zimbrasingle',
  211. __FUNCTION__,
  212. $params,
  213. "Error : cannot login to " . $accessData['zimbraServer'],
  214. ""
  215. );
  216. return false;
  217. } else {
  218. $apiAccountManager = new Zm_Account($api);
  219. $response = $apiAccountManager->getAccountStatus($account_name);
  220. if(is_a($response, "Exception")) {
  221. logModuleCall(
  222. 'zimbrasingle',
  223. __FUNCTION__,
  224. $params,
  225. "Error : account $account_name could not verified",
  226. ""
  227. );
  228. return false;
  229. }
  230. if ($response != 'locked') {
  231. return "Account $account_name active, suspend account first";
  232. }
  233. $response = $apiAccountManager->deleteAccount($account_name);
  234. if(is_a($response, "Exception")) {
  235. logModuleCall(
  236. 'zimbrasingle',
  237. __FUNCTION__,
  238. $params,
  239. "Error : account $account_name could not removed",
  240. ""
  241. );
  242. return false;
  243. }
  244. return 'success';
  245. }
  246. }
  247. function zimbraSingleChangePassword($userData) {
  248. $accessData = zimbraSingleGetAccess();
  249. $passDecrypt = localAPI('DecryptPassword', array('password2' => $userData['password']));
  250. if ($passDecrypt['result'] == 'success') {
  251. $userData['password'] = $passDecrypt['password'];
  252. }
  253. if ($checkPW = zimbraSingleCheckPassword($userData['password'])) {
  254. return $checkPW;
  255. }
  256. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  257. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  258. $login = $api->login();
  259. if(is_a($login, "Exception")) {
  260. logModuleCall(
  261. 'zimbrasingle',
  262. __FUNCTION__,
  263. $params,
  264. "Error : cannot login to " . $accessData['zimbraServer'],
  265. ""
  266. );
  267. return false;
  268. } else {
  269. $apiAccountManager = new Zm_Account($api);
  270. $response = $apiAccountManager->setAccountPassword($account_name, $userData['password']);
  271. if(is_a($response, "Exception")) {
  272. logModuleCall(
  273. 'zimbrasingle',
  274. __FUNCTION__,
  275. $params,
  276. "Error : password for $account_name could not be set",
  277. ""
  278. );
  279. return false;
  280. } else {
  281. return $response;
  282. }
  283. }
  284. }
  285. function zimbraSingleChangePackage($userData) {
  286. $accessData = zimbraSingleGetAccess();
  287. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  288. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  289. $login = $api->login();
  290. if(is_a($login, "Exception")) {
  291. logModuleCall(
  292. 'zimbrasingle',
  293. __FUNCTION__,
  294. $params,
  295. "Error : cannot login to " . $accessData['zimbraServer'],
  296. ""
  297. );
  298. return false;
  299. }
  300. $apiAccountManager = new Zm_Account($api);
  301. $response = $apiAccountManager->setAccountCos($account_name, $userData['cos']);
  302. if(is_a($response, "Exception")) {
  303. logModuleCall(
  304. 'zimbrasingle',
  305. __FUNCTION__,
  306. $params,
  307. "Error : class of service for $account_name could not be set",
  308. ""
  309. );
  310. return false;
  311. }
  312. return $response;
  313. }
  314. function zimbraSingleClientArea($userData)
  315. {
  316. $accessData = zimbraSingleGetAccess();
  317. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  318. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  319. $login = $api->login();
  320. if(is_a($login, "Exception")) {
  321. logModuleCall(
  322. 'zimbrasingle',
  323. __FUNCTION__,
  324. $params,
  325. "Error : cannot login to " . $accessData['zimbraServer'],
  326. ""
  327. );
  328. return false;
  329. } else {
  330. $apiAccountManager = new Zm_Account($api);
  331. $response = $apiAccountManager->getAccountInfo($account_name);
  332. if(is_a($response, "Exception")) {
  333. logModuleCall(
  334. 'zimbrasingle',
  335. __FUNCTION__,
  336. $params,
  337. "Error : could not gather informations for $account_name",
  338. ""
  339. );
  340. return false;
  341. } else {
  342. $webMailURL = recursiveFindAll( $response, 'PUBLICMAILURL');
  343. logModuleCall(
  344. 'zimbrasingle',
  345. __FUNCTION__,
  346. $params,
  347. "debug",
  348. $webMailURL
  349. );
  350. return $webMailURL;
  351. }
  352. }
  353. }
  354. function zimbraSingleConfigOptions($params) {
  355. $accessData = zimbraSingleGetAccess();
  356. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  357. $login = $api->login();
  358. if(is_a($login, "Exception")) {
  359. logModuleCall(
  360. 'zimbrasingle',
  361. __FUNCTION__,
  362. $params,
  363. "Error : cannot login to " . $accessData['zimbraServer'],
  364. ""
  365. );
  366. return false;
  367. }
  368. $apiAccountManager = new Zm_Account($api);
  369. $response = $apiAccountManager->getAllCos();
  370. if(is_a($response, "Exception")) {
  371. logModuleCall(
  372. 'zimbrasingle',
  373. __FUNCTION__,
  374. $params,
  375. "Error : could not fetch classes of service",
  376. ""
  377. );
  378. return false;
  379. }
  380. $cosNames = recursiveFindAll($response, 'NAME');
  381. $configOptions = array();
  382. $configOptions['cos'] = array(
  383. "FriendlyName" => "Class of Service",
  384. "Type" => "dropdown",
  385. "Options" => implode(',', $cosNames),
  386. "Description" => "Select COS",
  387. );
  388. $apiDomainManager = new Zm_Domain($api);
  389. $response = $apiDomainManager->getAllDomains();
  390. if(is_a($response, "Exception")) {
  391. logModuleCall(
  392. 'zimbrasingle',
  393. __FUNCTION__,
  394. $params,
  395. "Error : could fetch available maildomains",
  396. ""
  397. );
  398. return false;
  399. }
  400. $domainNames = recursiveFindAll($response, 'NAME');
  401. $configOptions['maildomains'] = array(
  402. "FriendlyName" => "Mail Domain",
  403. "Type" => "dropdown",
  404. "Multiple" => true,
  405. "Options" => implode(',', $domainNames),
  406. "Description" => "select maildomains",
  407. );
  408. return $configOptions;
  409. }
  410. function zimbraSingleCreateCustomFields($packageconfigoption)
  411. {
  412. $whmcs = App::self();
  413. $productID = $whmcs->get_req_var('id');
  414. Capsule::table('tblcustomfields')
  415. ->where('relid', '=', $productID)
  416. ->delete();
  417. Capsule::table('tblcustomfields')
  418. ->insert(
  419. array(
  420. 'type' => 'product',
  421. 'relid' => $productID,
  422. 'fieldname' => 'givenname | Vorname',
  423. 'fieldtype' => 'text',
  424. 'required' => 'on',
  425. 'showorder' => 'on',
  426. 'sortorder' => '0'
  427. )
  428. );
  429. Capsule::table('tblcustomfields')
  430. ->insert(
  431. array(
  432. 'type' => 'product',
  433. 'relid' => $productID,
  434. 'fieldname' => 'sn | Nachname',
  435. 'fieldtype' => 'text',
  436. 'required' => 'on',
  437. 'showorder' => 'on',
  438. 'sortorder' => '1'
  439. )
  440. );
  441. Capsule::table('tblcustomfields')
  442. ->insert(
  443. array(
  444. 'type' => 'product',
  445. 'relid' => $productID,
  446. 'fieldname' => 'username | E-Mail Name',
  447. 'fieldtype' => 'text',
  448. 'required' => 'on',
  449. 'showorder' => 'on',
  450. 'sortorder' => '2'
  451. )
  452. );
  453. Capsule::table('tblcustomfields')
  454. ->insert(
  455. array(
  456. 'type' => 'product',
  457. 'relid' => $productID,
  458. 'fieldname' => 'maildomain | Mail Domaine',
  459. 'fieldtype' => 'dropdown',
  460. 'fieldoptions' => implode(',', $packageconfigoption[2]),
  461. 'required' => 'on',
  462. 'showorder' => 'on',
  463. 'sortorder' => '3'
  464. )
  465. );
  466. Capsule::table('tblcustomfields')
  467. ->insert(
  468. array(
  469. 'type' => 'product',
  470. 'relid' => $productID,
  471. 'fieldname' => 'password | Password',
  472. 'fieldtype' => 'password',
  473. 'required' => 'on',
  474. 'showorder' => 'on',
  475. 'sortorder' => '4'
  476. )
  477. );
  478. Capsule::table('tblcustomfields')
  479. ->insert(
  480. array(
  481. 'type' => 'product',
  482. 'relid' => $productID,
  483. 'fieldname' => 'cos | Class of Service',
  484. 'fieldtype' => 'dropdown',
  485. 'fieldoptions' => $packageconfigoption[1],
  486. 'adminonly' => 'on',
  487. 'required' => 'on',
  488. 'sortorder' => '5'
  489. )
  490. );
  491. }
  492. function recursiveFindAll($haystack, $needle)
  493. {
  494. $values = array();
  495. $iterator = new RecursiveArrayIterator($haystack);
  496. $recursive = new RecursiveIteratorIterator(
  497. $iterator,
  498. RecursiveIteratorIterator::SELF_FIRST
  499. );
  500. foreach ($recursive as $key => $value) {
  501. if ($key === $needle) {
  502. array_push($values, $value);
  503. }
  504. }
  505. return $values;
  506. }
  507. function zimbraSingleCheckPassword($pwd)
  508. {
  509. $message = '';
  510. if (strlen($pwd) < 9) {
  511. $message .= "Das das Passwort ist zu kurz. Es werden mind. 9 Zeichen benötigt<br>";
  512. }
  513. if (!preg_match("#[0-9]+#", $pwd)) {
  514. $message .= "Das Passwort muss mindestens eine Zahl enthalten<br>";
  515. }
  516. if (!preg_match("#[A-Z]+#", $pwd)) {
  517. $message .= "Das Passwort muss mindestens einen Grossbuchstaben (A-Z) enthalten<br>";
  518. }
  519. if (!preg_match("#[a-z]+#", $pwd)) {
  520. $message .= "Das Passwort muss mindestens einen Kleinbuchstaben (a-z) enthalten<br>";
  521. }
  522. if (!preg_match("#[^\w]+#", $pwd)) {
  523. $message .= "Das Passwort muss mindestens ein Sonderzeichen (.,-:=) enthalten<br>";
  524. }
  525. return $message;
  526. }
  527. function zimbraSingleTestFunction()
  528. {
  529. return 'blubb';
  530. }