ArticleService.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Services;
  3. class ArticleService extends BaseService
  4. {
  5. private static $article;
  6. private static $valuables;
  7. public function __construct($article)
  8. {
  9. parent::__construct();
  10. self::$article = $article;
  11. $siteName = sysConfig('website_name');
  12. $siteUrl = sysConfig('website_url');
  13. $subscribe = auth()->user()->subscribe;
  14. $subUrl = route('sub', $subscribe->code);
  15. self::$valuables = [
  16. '{{siteName}}' => $siteName,
  17. '{{urlEndcodeSiteName}}' => urlencode($siteName),
  18. '{{urlEndcodeSiteUrl}}' => urlencode($siteUrl),
  19. '{{siteUrl}}' => $siteUrl,
  20. '{{subUrl}}' => $subUrl,
  21. '{{urlEncodeSubUrl}}' => urlencode($subUrl),
  22. '{{base64SubUrl}}' => base64url_encode($subUrl),
  23. ];
  24. }
  25. /**
  26. * @return mixed
  27. */
  28. public function getContent()
  29. {
  30. $content = self::$article->content;
  31. if (! UserService::getInstance()->isActivePaying()) {
  32. $this->formatAccessable($content);
  33. }
  34. $this->formatValuables($content);
  35. return $content;
  36. }
  37. private function formatAccessable(&$body)
  38. {
  39. while (strpos($body, '<!--access start-->') !== false) {
  40. $accessData = $this->getInBetween($body, '<!--access start-->', '<!--access end-->');
  41. if ($accessData) {
  42. $body = strtr($body, [
  43. $accessData => '<div class="user-no-access"><i class="icon wb-lock" aria-hidden="true"></i>'.__('You must have a valid subscription to view content in this area!').'</div>',
  44. ]);
  45. }
  46. }
  47. }
  48. private function getInBetween($input, $start, $end): string
  49. {
  50. $substr = substr($input, strlen($start) + strpos($input, $start), (strlen($input) - strpos($input, $end)) * (-1));
  51. return $start.$substr.$end;
  52. }
  53. private function formatValuables(&$body)
  54. {
  55. $body = strtr($body, self::$valuables);
  56. }
  57. }