KnowledgeController.php 2.9 KB

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