nextCloudUserAvailable.php 3.9 KB

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