zimbraSingle.inc 17 KB

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