zimbraSingle.inc 17 KB

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