2020-10-18 02:51:32 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\User;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Services\UserService;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\Knowledge;
|
|
|
|
|
|
|
|
class KnowledgeController extends Controller
|
|
|
|
{
|
|
|
|
public function fetch(Request $request)
|
|
|
|
{
|
|
|
|
if ($request->input('id')) {
|
|
|
|
$knowledge = Knowledge::where('id', $request->input('id'))
|
|
|
|
->where('show', 1)
|
|
|
|
->first()
|
|
|
|
->toArray();
|
2021-06-12 00:56:39 +08:00
|
|
|
if (!$knowledge) abort(500, __('Article does not exist'));
|
2020-10-18 02:51:32 +08:00
|
|
|
$user = User::find($request->session()->get('id'));
|
|
|
|
$userService = new UserService();
|
2021-05-13 17:08:09 +08:00
|
|
|
if ($userService->isAvailable($user)) {
|
|
|
|
$appleId = config('v2board.apple_id');
|
|
|
|
$appleIdPassword = config('v2board.apple_id_password');
|
|
|
|
} else {
|
2021-06-12 00:56:39 +08:00
|
|
|
$appleId = __('No active subscription. Unable to use our provided Apple ID');
|
|
|
|
$appleIdPassword = __('No active subscription. Unable to use our provided Apple ID');
|
2021-05-13 17:08:09 +08:00
|
|
|
$this->formatAccessData($knowledge['body']);
|
|
|
|
}
|
2021-07-06 17:04:59 +08:00
|
|
|
$subscribeUrl = config('v2board.app_url', env('APP_URL'));
|
|
|
|
$subscribeUrls = explode(',', config('v2board.subscribe_url'));
|
|
|
|
if ($subscribeUrls) {
|
|
|
|
$subscribeUrl = $subscribeUrls[rand(0, count($subscribeUrls) - 1)];
|
|
|
|
}
|
2021-07-08 23:01:13 +08:00
|
|
|
$subscribeUrl = "{$subscribeUrl}/api/v1/client/subscribe?token={$user['token']}";
|
2020-10-18 16:38:11 +08:00
|
|
|
$knowledge['body'] = str_replace('{{siteName}}', config('v2board.app_name', 'V2Board'), $knowledge['body']);
|
2020-10-18 02:51:32 +08:00
|
|
|
$knowledge['body'] = str_replace('{{subscribeUrl}}', $subscribeUrl, $knowledge['body']);
|
2020-10-19 00:37:27 +08:00
|
|
|
$knowledge['body'] = str_replace('{{urlEncodeSubscribeUrl}}', urlencode($subscribeUrl), $knowledge['body']);
|
2020-10-25 00:41:27 +08:00
|
|
|
$knowledge['body'] = str_replace(
|
|
|
|
'{{safeBase64SubscribeUrl}}',
|
|
|
|
str_replace(
|
|
|
|
array('+', '/', '='),
|
|
|
|
array('-', '_', ''),
|
|
|
|
base64_encode($subscribeUrl)
|
|
|
|
),
|
|
|
|
$knowledge['body']
|
|
|
|
);
|
2020-10-18 02:51:32 +08:00
|
|
|
return response([
|
|
|
|
'data' => $knowledge
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
$knowledges = Knowledge::select(['id', 'category', 'title', 'updated_at'])
|
|
|
|
->where('language', $request->input('language'))
|
|
|
|
->where('show', 1)
|
|
|
|
->orderBy('sort', 'ASC')
|
|
|
|
->get()
|
|
|
|
->groupBy('category');
|
|
|
|
return response([
|
|
|
|
'data' => $knowledges
|
|
|
|
]);
|
|
|
|
}
|
2021-05-13 17:08:09 +08:00
|
|
|
|
|
|
|
private function formatAccessData(&$body)
|
|
|
|
{
|
|
|
|
function getBetween($input, $start, $end){$substr = substr($input, strlen($start)+strpos($input, $start),(strlen($input) - strpos($input, $end))*(-1));return $substr;}
|
|
|
|
$accessData = getBetween($body, '<!--access start-->', '<!--access end-->');
|
|
|
|
if ($accessData) {
|
2021-06-12 00:56:39 +08:00
|
|
|
$body = str_replace($accessData, '<div class="v2board-no-access">'. __('You must have a valid subscription to view content in this area') .'</div>', $body);
|
2021-05-13 17:08:09 +08:00
|
|
|
}
|
|
|
|
}
|
2020-10-18 02:51:32 +08:00
|
|
|
}
|