zimbraSingle.inc 17 KB

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