CWPAddressAvailable.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Helper script to check the availibility of a Seafile Account useable with ajax requests
  4. *
  5. * @see https://www.seafile.com
  6. * @copyright Copyright (c) Thurdata GmbH 2021
  7. * @license GPL
  8. *
  9. */
  10. $pos = strpos($_SERVER['HTTP_REFERER'],getenv('HTTP_HOST'));
  11. if($pos===false) {
  12. die('Restricted access');
  13. }
  14. $productID = $_GET['pid'];
  15. $accountName = $_GET['username'];
  16. if(!filter_var($accountName, FILTER_VALIDATE_EMAIL)) {
  17. echo "invalid";
  18. exit;
  19. }
  20. error_log("Seafile Address Available: ". $productID . "/" . $accountName);
  21. /**
  22. * Requires the whmcs init
  23. * Requires this PHP api to make soap calls and parse responses
  24. */
  25. require_once(__DIR__ . '/../../../init.php');
  26. require_once(__DIR__ . '/api/Sf/Admin.php');
  27. use WHMCS\Database\Capsule;
  28. $whmcs = App::self();
  29. $accessData = array('seafileURL' => '', 'adminUser' => '', 'adminPass' => '');
  30. $serverGroupIDObj = Capsule::table('tblproducts')
  31. ->select('servergroup')
  32. ->where('id', '=', $productID)
  33. ->get();
  34. $serverGroupID = $serverGroupIDObj[0]->servergroup;
  35. $serverIDObj = Capsule::table('tblservergroupsrel')
  36. ->select('serverid')
  37. ->where('groupid', '=', $serverGroupID)
  38. ->get();
  39. $serverID = $serverIDObj[0]->serverid;
  40. $server = Capsule::table('tblservers')
  41. ->select('hostname', 'secure', 'port', 'username', 'password')
  42. ->where('id', '=', $serverID)
  43. ->where('active', '=', 1)
  44. ->get();
  45. $accessData['seafileServer'] = $server[0]->hostname;
  46. $accessData['prefix'] = $server[0]->secure ? 'https://' : 'http://';
  47. $accessData['port'] = $server[0]->port;
  48. $accessData['adminUser'] = $server[0]->username;
  49. $adminPassCrypt = $server[0]->password;
  50. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $adminPassCrypt));
  51. if ($adminPassDecrypt['result'] == 'success') {
  52. $accessData['adminPass'] = $adminPassDecrypt['password'];
  53. }
  54. if (empty($accessData['port']) || $accessData['port'] == "") {
  55. $seafileURL = $accessData['prefix'] . $accessData['seafileServer'] . ":443";
  56. } else {
  57. $seafileURL = $accessData['prefix'] . $accessData['seafileServer'] . ':' . $accessData['port'];
  58. }
  59. $seafileAPI = new Sf_Admin($seafileURL,$accessData['adminUser'],$accessData['adminPass']);
  60. $response = $seafileAPI->login();
  61. if (isset($response['error_msg'])) {
  62. logModuleCall(
  63. 'seafile',
  64. __FUNCTION__,
  65. $params,
  66. 'Error: could not login to ' . $seafileURL,
  67. $response
  68. );
  69. error_log(" --> ERROR " . print_r($response,true));
  70. echo "error";
  71. exit();
  72. } else {
  73. $existingAccount = $seafileAPI->getAccount($accountName);
  74. if (isset($existingAccount['error_msg'])) {
  75. echo 'yes';
  76. } else {
  77. echo 'no';
  78. }
  79. }