zimbraSingle.inc 14 KB

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