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