Spaces:
Running
Running
Download app/Controllers/ApiController.php from buddhaword/web: direct link, hf CLI and curl.
- Browser
- Download file 6.44 kB
-
https://huggingface.co/spaces/buddhaword/web/resolve/main/app/Controllers/ApiController.php
- Command line
-
hf download hf://spaces/buddhaword/web/app/Controllers/ApiController.php
-
curl -L -o ApiController.php https://huggingface.co/spaces/buddhaword/web/resolve/main/app/Controllers/ApiController.php
6.44 kB
| 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 ''; | |
| } | |
| } | |