KnowledgeController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use App\Services\UserService;
  6. use Illuminate\Http\Request;
  7. use App\Models\Knowledge;
  8. class KnowledgeController extends Controller
  9. {
  10. public function fetch(Request $request)
  11. {
  12. if ($request->input('id')) {
  13. $knowledge = Knowledge::where('id', $request->input('id'))
  14. ->where('show', 1)
  15. ->first()
  16. ->toArray();
  17. if (!$knowledge) abort(500, __('Article does not exist'));
  18. $user = User::find($request->session()->get('id'));
  19. $userService = new UserService();
  20. if ($userService->isAvailable($user)) {
  21. $appleId = config('v2board.apple_id');
  22. $appleIdPassword = config('v2board.apple_id_password');
  23. } else {
  24. $appleId = __('No active subscription. Unable to use our provided Apple ID');
  25. $appleIdPassword = __('No active subscription. Unable to use our provided Apple ID');
  26. $this->formatAccessData($knowledge['body']);
  27. }
  28. $subscribeUrl = config('v2board.app_url', env('APP_URL'));
  29. $subscribeUrls = explode(',', config('v2board.subscribe_url'));
  30. if ($subscribeUrls) {
  31. $subscribeUrl = $subscribeUrls[rand(0, count($subscribeUrls) - 1)];
  32. }
  33. $subscribeUrl = "{$subscribeUrl}/api/v1/client/subscribe?token={$user['token']}";
  34. $knowledge['body'] = str_replace('{{siteName}}', config('v2board.app_name', 'V2Board'), $knowledge['body']);
  35. $knowledge['body'] = str_replace('{{subscribeUrl}}', $subscribeUrl, $knowledge['body']);
  36. $knowledge['body'] = str_replace('{{urlEncodeSubscribeUrl}}', urlencode($subscribeUrl), $knowledge['body']);
  37. $knowledge['body'] = str_replace(
  38. '{{safeBase64SubscribeUrl}}',
  39. str_replace(
  40. array('+', '/', '='),
  41. array('-', '_', ''),
  42. base64_encode($subscribeUrl)
  43. ),
  44. $knowledge['body']
  45. );
  46. return response([
  47. 'data' => $knowledge
  48. ]);
  49. }
  50. $knowledges = Knowledge::select(['id', 'category', 'title', 'updated_at'])
  51. ->where('language', $request->input('language'))
  52. ->where('show', 1)
  53. ->orderBy('sort', 'ASC')
  54. ->get()
  55. ->groupBy('category');
  56. return response([
  57. 'data' => $knowledges
  58. ]);
  59. }
  60. private function formatAccessData(&$body)
  61. {
  62. function getBetween($input, $start, $end){$substr = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1));return $substr;}
  63. $accessData = getBetween($body, '<!--access start-->', '<!--access end-->');
  64. if ($accessData) {
  65. $body = str_replace($accessData, '<div class="v2board-no-access">'. __('You must have a valid subscription to view content in this area') .'</div>', $body);
  66. }
  67. }
  68. }