zimbraSingle.inc 15 KB

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