zimbraSingle.inc 17 KB

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