zimbraSingle.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. $testid = $whmcs->get_req_var('id');
  27. logModuleCall(
  28. 'zimbrasingle',
  29. __FUNCTION__,
  30. $params,
  31. "debug: clientarea login",
  32. $testid
  33. );
  34. $serverGroupID = $whmcs->get_req_var('servergroup');
  35. if($serverGroupID) {
  36. $serverIDObj = Capsule::table('tblservergroupsrel')
  37. ->select('serverid')
  38. ->where('groupid', '=', $serverGroupID)
  39. ->get();
  40. $serverID = $serverIDObj[0]->serverid;
  41. } else {
  42. $serverIDObj = Capsule::table('tblhosting')
  43. ->select('server')
  44. ->where('packageid', '=', $packageid)
  45. ->get();
  46. $serverID = $serverIDObj[0]->server;
  47. }
  48. $server = Capsule::table('tblservers')
  49. ->select('ipaddress', 'username', 'password')
  50. ->where('id', '=', $serverID)
  51. ->where('active', '=', 1)
  52. ->get();
  53. $accessData['zimbraServer'] = $server[0]->ipaddress;
  54. $accessData['adminUser'] = $server[0]->username;
  55. $adminPassCrypt = $server[0]->password;
  56. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $adminPassCrypt));
  57. if ($adminPassDecrypt['result'] == 'success') {
  58. $accessData['adminPass'] = $adminPassDecrypt['password'];
  59. }
  60. return $accessData;
  61. }
  62. /**
  63. * Checks if a given email address in the given domain already exists
  64. *
  65. * @param $emailNameOnly The name before the @-sign only
  66. * @param $domainName The domain to search for existance of the email account
  67. * @return true if such an account was found or false if not
  68. */
  69. function zimbraSingleDoesEMailExist($emailNameOnly, $domainName)
  70. {
  71. $account_name = $emailNameOnly . "@" . $domainName;
  72. $accessData = zimbraSingleGetAccess();
  73. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  74. $login = $api->login();
  75. if(is_a($login, "Exception")) {
  76. logModuleCall(
  77. 'zimbrasingle',
  78. __FUNCTION__,
  79. $params,
  80. "Error : cannot login to " . $accessData['zimbraServer'],
  81. "$login->getMessage()"
  82. );
  83. exit();
  84. } else {
  85. $apiAccountManager = new Zm_Account($api);
  86. if( $apiAccountManager->accountExists($account_name)) {
  87. return true;
  88. } else {
  89. return false;
  90. }
  91. }
  92. }
  93. /**
  94. */
  95. function zimbraSingleCreateAccount($userData)
  96. {
  97. $accessData = zimbraSingleGetAccess();
  98. $attrs = array();
  99. $attrs["gn"] = $userData["givenname"];
  100. $attrs["sn"] = $userData["sn"];
  101. $attrs["displayName"] = $attrs["gn"] . " " . $attrs["sn"];
  102. $passDecrypt = localAPI('DecryptPassword', array('password2' => $userData['password']));
  103. if ($passDecrypt['result'] == 'success') {
  104. $userData['password'] = $passDecrypt['password'];
  105. }
  106. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  107. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  108. $login = $api->login();
  109. if(is_a($login, "Exception")) {
  110. logModuleCall(
  111. 'zimbrasingle',
  112. __FUNCTION__,
  113. $params,
  114. "Error : cannot login to " . $accessData['zimbraServer'],
  115. ""
  116. );
  117. return false;
  118. }
  119. $apiAccountManager = new Zm_Account($api);
  120. $cosName = $userData['cos'];
  121. $cosID = $apiAccountManager->getCosId($cosName);
  122. if(is_a($cosID, "Exception")) {
  123. logModuleCall(
  124. 'zimbrasingle',
  125. __FUNCTION__,
  126. $params,
  127. "Error : serviceclass $cosName not available",
  128. $userData
  129. );
  130. return false;
  131. }
  132. $attrs['zimbraCOSId'] = $cosID;
  133. $id = $apiAccountManager->createAccount($account_name, $userData['password'], $attrs);
  134. if(is_a($id, "Exception")) {
  135. logModuleCall(
  136. 'zimbrasingle',
  137. __FUNCTION__,
  138. $params,
  139. "Error : account $account_name not created",
  140. $id
  141. );
  142. return false;
  143. }
  144. return $id;
  145. }
  146. function zimbraSingleSuspendAccount($userData)
  147. {
  148. $accessData = zimbraSingleGetAccess();
  149. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  150. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  151. $login = $api->login();
  152. if(is_a($login, "Exception")) {
  153. logModuleCall(
  154. 'zimbrasingle',
  155. __FUNCTION__,
  156. $params,
  157. "Error : cannot login to " . $accessData['zimbraServer'],
  158. ""
  159. );
  160. return false;
  161. } else {
  162. $apiAccountManager = new Zm_Account($api);
  163. $response = $apiAccountManager->setAccountStatus($account_name, "locked");
  164. if(is_a($response, "Exception")) {
  165. logModuleCall(
  166. 'zimbrasingle',
  167. __FUNCTION__,
  168. $params,
  169. "Error : account $account_name could not locked",
  170. ""
  171. );
  172. return false;
  173. } else {
  174. return $response;
  175. }
  176. }
  177. }
  178. function zimbraSingleUnsuspendAccount($userData)
  179. {
  180. $accessData = zimbraSingleGetAccess();
  181. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  182. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  183. $login = $api->login();
  184. if(is_a($login, "Exception")) {
  185. logModuleCall(
  186. 'zimbrasingle',
  187. __FUNCTION__,
  188. $params,
  189. "Error : cannot login to " . $accessData['zimbraServer'],
  190. ""
  191. );
  192. return false;
  193. } else {
  194. $apiAccountManager = new Zm_Account($api);
  195. $response = $apiAccountManager->setAccountStatus($account_name, "active");
  196. if(is_a($response, "Exception")) {
  197. logModuleCall(
  198. 'zimbrasingle',
  199. __FUNCTION__,
  200. $params,
  201. "Error : account $account_name could not unlocked",
  202. ""
  203. );
  204. return false;
  205. } else {
  206. return $response;
  207. }
  208. }
  209. }
  210. function zimbraSingleDeleteAccount($userData)
  211. {
  212. $accessData = zimbraSingleGetAccess();
  213. $accountName = $userData['username'] . '@' . $userData['maildomain'];
  214. logModuleCall(
  215. 'zimbrasingle',
  216. __FUNCTION__,
  217. $params,
  218. "debug: accountName",
  219. $account_name
  220. );
  221. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  222. $login = $api->login();
  223. if(is_a($login, "Exception")) {
  224. logModuleCall(
  225. 'zimbrasingle',
  226. __FUNCTION__,
  227. $params,
  228. "Error : cannot login to " . $accessData['zimbraServer'],
  229. ""
  230. );
  231. return false;
  232. }
  233. $apiAccountManager = new Zm_Account($api);
  234. $response = $apiAccountManager->getAccountStatus($accountName);
  235. if(is_a($response, "Exception")) {
  236. logModuleCall(
  237. 'zimbrasingle',
  238. __FUNCTION__,
  239. $params,
  240. "Error : account $accountName could not verified",
  241. ""
  242. );
  243. return false;
  244. }
  245. if ($response != 'locked') {
  246. return "Account $accountName active, suspend account first";
  247. }
  248. $response = $apiAccountManager->deleteAccount($accountName);
  249. if(is_a($response, "Exception")) {
  250. logModuleCall(
  251. 'zimbrasingle',
  252. __FUNCTION__,
  253. $params,
  254. "Error : account $accountName could not removed",
  255. ""
  256. );
  257. return false;
  258. }
  259. return 'success';
  260. }
  261. function zimbraSingleChangePassword($userData) {
  262. $accessData = zimbraSingleGetAccess();
  263. $passDecrypt = localAPI('DecryptPassword', array('password2' => $userData['password']));
  264. if ($passDecrypt['result'] == 'success') {
  265. $userData['password'] = $passDecrypt['password'];
  266. }
  267. if ($checkPW = zimbraSingleCheckPassword($userData['password'])) {
  268. return $checkPW;
  269. }
  270. $account_name = $userData['username'] . '@' . $userData['maildomain'];
  271. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
  272. $login = $api->login();
  273. if(is_a($login, "Exception")) {
  274. logModuleCall(
  275. 'zimbrasingle',
  276. __FUNCTION__,
  277. $params,
  278. "Error : cannot login to " . $accessData['zimbraServer'],
  279. ""
  280. );
  281. return false;
  282. }
  283. $apiAccountManager = new Zm_Account($api);
  284. $response = $apiAccountManager->setAccountPassword($account_name, $userData['password']);
  285. if(is_a($response, "Exception")) {
  286. logModuleCall(
  287. 'zimbrasingle',
  288. __FUNCTION__,
  289. $params,
  290. "Error : password for $account_name could not be set",
  291. ""
  292. );
  293. return false;
  294. }
  295. return 'success';
  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" . PHP_EOL;
  524. }
  525. if (!preg_match("#[0-9]+#", $pwd)) {
  526. $message .= "Das Passwort muss mindestens eine Zahl enthalten" . PHP_EOL;
  527. }
  528. if (!preg_match("#[A-Z]+#", $pwd)) {
  529. $message .= "Das Passwort muss mindestens einen Grossbuchstaben (A-Z) enthalten" . PHP_EOL;
  530. }
  531. if (!preg_match("#[a-z]+#", $pwd)) {
  532. $message .= "Das Passwort muss mindestens einen Kleinbuchstaben (a-z) enthalten" . PHP_EOL;
  533. }
  534. if (!preg_match("#[^\w]+#", $pwd)) {
  535. $message .= "Das Passwort muss mindestens ein Sonderzeichen (.,-:=) enthalten" . PHP_EOL;
  536. }
  537. return $message;
  538. }