zimbraSingle.inc 14 KB

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