nextCloudEMailAvailable.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @copyright Copyright (c) Thurdata GmbH 2020
  4. * @license GPL
  5. *
  6. */
  7. $pos = strpos($_SERVER['HTTP_REFERER'],getenv('HTTP_HOST'));
  8. if($pos===false) {
  9. die('Restricted access');
  10. }
  11. /**
  12. * Requires the whmcs init
  13. * Requires this PHP api to make soap calls and parse responses
  14. */
  15. require_once(__DIR__ . '/../../../init.php');
  16. use WHMCS\Database\Capsule;
  17. $whmcs = App::self();
  18. $email = $_GET['email'];
  19. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  20. echo "invalid";
  21. exit;
  22. }
  23. $productID = $_GET['pid'];
  24. $accessData = array('nextcloudServer' => '', 'adminUser' => '', 'adminPass' => '');
  25. $serverGroupIDObj = Capsule::table('tblproducts')
  26. ->select('servergroup')
  27. ->where('id', '=', $productID)
  28. ->get();
  29. $serverGroupID = $serverGroupIDObj[0]->servergroup;
  30. $serverIDObj = Capsule::table('tblservergroupsrel')
  31. ->select('serverid')
  32. ->where('groupid', '=', $serverGroupID)
  33. ->get();
  34. $serverID = $serverIDObj[0]->serverid;
  35. $server = Capsule::table('tblservers')
  36. ->select('hostname', 'username', 'password')
  37. ->where('id', '=', $serverID)
  38. ->where('active', '=', 1)
  39. ->get();
  40. $accessData['nextcloudServer'] = $server[0]->hostname;
  41. $accessData['adminUser'] = $server[0]->username;
  42. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  43. if ($adminPassDecrypt['result'] == 'success') {
  44. $accessData['adminPass'] = $adminPassDecrypt['password'];
  45. }
  46. //error_log("NextCloud User Avaialable: ACC " . $accountName);
  47. //error_log("--------------------------");
  48. //error_log("NextCloud User Avaialable: PID " . $productID);
  49. //error_log("NextCloud User Avaialable: GID " . $serverGroupID);
  50. //error_log("NextCloud User Avaialable: SID " . $serverID);
  51. //error_log("NextCloud User Avaialable: SERVER " . $accessData['nextcloudServer']);
  52. //error_log("NextCloud User Avaialable: USER " . $accessData['adminUser']);
  53. //error_log("NextCloud User Avaialable: PASS " . $accessData['adminPass']);
  54. $nextcloudURL = 'https://' . $accessData['nextcloudServer'] . "/ocs/v1.php/cloud/users?search=" . $email;
  55. //error_log("NextCloud User Avaialable: URL " . $nextcloudURL);
  56. $response = nextcloud_send($nextcloudURL,$accessData['adminUser'],$accessData['adminPass'],"GET");
  57. //error_log("NextCloud User Available NextCloud Response: " . $response->ocs->meta->statuscode);
  58. //error_log("NextCloud User Available NextCloud Response Content " . print_r($response,true));
  59. if (is_null($response) == true) {
  60. echo "error";
  61. exit;
  62. }
  63. if ($response->ocs->meta->statuscode != '100') {
  64. echo "no";
  65. } else {
  66. if (count($response->ocs->data->users) > 0) {
  67. echo "no";
  68. exit;
  69. } else {
  70. echo "yes";
  71. exit;
  72. }
  73. }
  74. function nextcloud_send($href, $username, $password, $action, $post = array()) {
  75. $postdata = http_build_query($post);
  76. $ch = curl_init();
  77. if (strtoupper($action) == 'GET') {
  78. curl_setopt($ch, CURLOPT_HTTPGET, true);
  79. }
  80. curl_setopt($ch, CURLOPT_URL, $href);
  81. curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
  82. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  83. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json","OCS-APIRequest: true",'content-type: application/x-www-form-urlencoded'));
  84. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  85. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  86. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  87. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  88. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  89. $result_json = curl_exec($ch);
  90. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  91. curl_close($ch);
  92. if ($httpcode >= 200 && $httpcode < 300) {
  93. //print_r($result_json);die();
  94. $result_bom = nextcloud_remove_utf8bom($result_json);
  95. $result = json_decode($result_bom);
  96. return($result);
  97. } else {
  98. return null;
  99. }
  100. }
  101. function nextcloud_remove_utf8bom($text) {
  102. $bom = pack('H*', 'EFBBBF');
  103. $text = preg_replace("/^$bom/", '', $text);
  104. return $text;
  105. }