File size: 6,444 Bytes
7731906
 
 
 
 
 
 
 
03bdc4a
 
7731906
 
 
03bdc4a
7731906
 
 
 
 
 
 
03bdc4a
d41d880
03bdc4a
95c417b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7731906
 
 
 
 
 
 
 
 
 
03bdc4a
d41d880
7731906
 
 
 
 
 
 
 
 
 
 
03bdc4a
d41d880
7731906
 
 
 
 
 
 
 
 
 
 
03bdc4a
d41d880
7731906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d8cf3e
7731906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace App\Controllers;

use App\Models\Sutra;
use App\Models\Book;
use App\Models\Video;
use App\Models\Calendar;

require_once __DIR__ . '/../Helpers/cache.php';

class ApiController {
    public function checkUpdate() {
        header('Content-Type: application/json');
        $version = getCacheVersion('sutra_api.json');
        echo json_encode(['version' => $version]);
    }

    public function syncSutras() {
        header('Content-Type: application/json');
        try {
            set_time_limit(60);
            $refresh = shouldRefreshCache('sutra_api.json', 30);
            $sutras = Sutra::getAll($refresh);
            $version = getCacheVersion('sutra_api.json');

            $page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : null;
            $perPage = 150;
            $total = count($sutras);
            $totalPages = (int)ceil($total / $perPage);

            if ($page !== null) {
                $start = ($page - 1) * $perPage;
                $pageData = array_slice($sutras, $start, $perPage);
                echo json_encode([
                    'success' => true,
                    'data' => $pageData,
                    'version' => $version,
                    'page' => $page,
                    'totalPages' => $totalPages,
                    'total' => $total,
                ], JSON_UNESCAPED_UNICODE);
            } else {
                echo json_encode([
                    'success' => true,
                    'data' => $sutras,
                    'version' => $version,
                    'totalPages' => $totalPages,
                    'total' => $total,
                ], JSON_UNESCAPED_UNICODE);
            }
        } catch (\Exception $e) {
            http_response_code(500);
            echo json_encode(['success' => false, 'message' => $e->getMessage()], JSON_UNESCAPED_UNICODE);
        }
    }

    public function syncBooks() {
        header('Content-Type: application/json');
        try {
            set_time_limit(30);
            $refresh = shouldRefreshCache('book_api.json', 30);
            $books = Book::getAll($refresh);
            echo json_encode(['success' => true, 'data' => $books], JSON_UNESCAPED_UNICODE);
        } catch (\Exception $e) {
            http_response_code(500);
            echo json_encode(['success' => false, 'message' => $e->getMessage()], JSON_UNESCAPED_UNICODE);
        }
    }

    public function syncVideos() {
        header('Content-Type: application/json');
        try {
            set_time_limit(30);
            $refresh = shouldRefreshCache('video_api.json', 30);
            $videos = Video::getAll($refresh);
            echo json_encode(['success' => true, 'data' => $videos], JSON_UNESCAPED_UNICODE);
        } catch (\Exception $e) {
            http_response_code(500);
            echo json_encode(['success' => false, 'message' => $e->getMessage()], JSON_UNESCAPED_UNICODE);
        }
    }

    public function syncCalendar() {
        header('Content-Type: application/json');
        try {
            set_time_limit(30);
            $refresh = shouldRefreshCache('calendar_api.json', 30);
            $events = Calendar::getAll($refresh);
            echo json_encode(['success' => true, 'data' => $events], JSON_UNESCAPED_UNICODE);
        } catch (\Exception $e) {
            http_response_code(500);
            echo json_encode(['success' => false, 'message' => $e->getMessage()], JSON_UNESCAPED_UNICODE);
        }
    }

    public function searchVideos() {
        header('Content-Type: application/json');
        try {
            $search = $_GET['search'] ?? '';
            $category = $_GET['category'] ?? '';

            $videos = Video::getAll();

            if ($search !== '') {
                $searchTerm = mb_strtolower(trim($search));
                $videos = array_filter($videos, function($v) use ($searchTerm) {
                    return mb_strpos(mb_strtolower($v['ຊື່ພຣະສູດ'] ?? ''), $searchTerm) !== false
                        || mb_strpos(mb_strtolower($v['ໝວດທັມ'] ?? ''), $searchTerm) !== false;
                });
            }

            if ($category !== '') {
                $categoryTerm = mb_strtolower(trim($category));
                $videos = array_filter($videos, function($v) use ($categoryTerm) {
                    return mb_strtolower(trim($v['ໝວດທັມ'] ?? '')) === $categoryTerm;
                });
            }

            $videos = array_values($videos);

            echo json_encode(['success' => true, 'data' => $videos], JSON_UNESCAPED_UNICODE);
        } catch (\Exception $e) {
            http_response_code(500);
            echo json_encode(['success' => false, 'message' => $e->getMessage()], JSON_UNESCAPED_UNICODE);
        }
    }

    public function manifest() {
        $file = __DIR__ . '/../../public/manifest.json';
        if (file_exists($file)) {
            header('Content-Type: application/json');
            header('Cache-Control: public, max-age=86400');
            readfile($file);
            exit;
        } else {
            http_response_code(404);
            echo json_encode(['error' => 'Manifest not found']);
            exit;
        }
    }

    public function serviceWorker() {
        $file = __DIR__ . '/../../public/sw.js';
        if (file_exists($file)) {
            header('Content-Type: application/javascript');
            header('Cache-Control: no-cache, no-store, must-revalidate');
            readfile($file);
            exit;
        } else {
            http_response_code(404);
            echo "/* Service Worker not found */";
            exit;
        }
    }

    private function getThumbnailUrl($link) {
        if (strpos($link, 'youtube.com') !== false || strpos($link, 'youtu.be') !== false) {
            preg_match('/(?:youtu\.be\/|youtube\.com\/(?:.*v=|.*\/|.*embed\/|.*shorts\/))([\w-]+)/', $link, $matches);
            $videoId = $matches[1] ?? null;
            return $videoId ? "https://img.youtube.com/vi/{$videoId}/hqdefault.jpg" : '';
        } elseif (strpos($link, 'drive.google.com') !== false) {
            preg_match('/(?:drive\.google\.com\/(?:.*\/d\/|file\/d\/))([a-zA-Z0-9_-]+)/', $link, $matches);
            $fileId = $matches[1] ?? null;
            return $fileId ? "https://lh3.googleusercontent.com/d/{$fileId}=s320?authuser=0" : '';
        }
        return '';
    }
}