zimbraSingle.inc 18 KB

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