zimbraSingle.inc 17 KB

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