File size: 10,863 Bytes
dfb0e6b
 
 
 
 
 
 
e43a832
dfb0e6b
 
 
 
 
 
 
 
 
e43a832
dfb0e6b
 
e43a832
 
 
dfb0e6b
 
 
 
 
 
e43a832
dfb0e6b
e43a832
 
 
 
 
282827b
 
40102c4
dfb0e6b
 
 
40102c4
 
dfb0e6b
 
 
 
 
 
 
 
 
 
 
 
0db9baa
dfb0e6b
 
 
 
 
 
 
 
 
e43a832
dfb0e6b
e43a832
 
 
dfb0e6b
 
 
 
 
 
e43a832
 
 
 
 
 
 
 
 
 
dfb0e6b
893b05c
 
dfb0e6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0db9baa
e43a832
dfb0e6b
 
 
 
 
 
 
 
 
 
 
 
 
 
e43a832
 
 
 
 
 
 
 
7326e89
 
 
6d07399
 
 
893b05c
 
 
dfb0e6b
 
 
 
 
e43a832
dfb0e6b
 
 
 
 
e43a832
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0db9baa
 
 
 
e43a832
 
dfb0e6b
0db9baa
dfb0e6b
 
 
e43a832
dfb0e6b
282827b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e43a832
dfb0e6b
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
namespace App\Controllers;

class UttayarndhamController {
    private const BASE_URL = 'https://uttayarndham.org';

    public function index() {
        $items = $this->fetchTags(0);
        $query = $_GET['q'] ?? '';

        if ($query) {
            $items = array_filter($items, function($item) use ($query) {
                return mb_stripos($item['title'], $query) !== false;
            });
        }

        return view('pages.uttayarndham.index', [
            'items' => array_values($items),
            'query' => $query,
            'seo' => [
                'title' => 'Uttayarndham (ທັມມະ) - ຄຳສອນພຸດທະ',
                'description' => 'Uttayarndham ຮວບຮວມບົດຄວາມທັມມະ',
                'keywords' => 'uttayarndham, ທັມມະ, ທັມ, ຄຳສອນພຸດທະ',
            ]
        ]);
    }

    public function apiList() {
        $page = max(0, intval($_GET['page'] ?? 0));
        $items = $this->fetchTags($page);

        $html = @file_get_contents(
            self::BASE_URL . '/keyword/tags' . ($page > 0 ? '?page=' . $page : ''),
            false,
            $this->getStreamContext()
        );
        // Check if there's a "load more" button or pagination
        $hasMore = $html !== false && (preg_match('/ki-more/', $html) === 1 || preg_match('/rel="next"/', $html) === 1);

        $this->json([
            'items' => $items,
            'page' => $page,
            'total' => ($page * 20) + count($items),
            'hasMore' => $hasMore,
        ]);
    }

    public function apiContent() {
        $url = $_GET['url'] ?? '';
        if (!$url) {
            http_response_code(400);
            $this->json(['error' => 'Missing url parameter']);
            return;
        }

        $fullUrl = strpos($url, 'http') === 0 ? $url : self::BASE_URL . $url;
        $html = @file_get_contents($fullUrl, false, $this->getStreamContext());
        if ($html === false) {
            http_response_code(404);
            $this->json(['error' => 'Content not found']);
            return;
        }

        preg_match('/<h1[^>]*>(.*?)<\/h1>/s', $html, $titleMatches);
        $title = $titleMatches[1] ?? '';

        $content = '';
        preg_match('/<article[^>]*>(.*?)<\/article>/s', $html, $articleMatches);
        if (!empty($articleMatches[1])) {
            $content = $articleMatches[1];
        }

        if (empty($content)) {
            preg_match('/<div class="content"[^>]*>(.*?)<\/div>/s', $html, $divMatches);
            $content = $divMatches[1] ?? '';
        }

        if (empty($content)) {
            preg_match('/<div class="view-content[^>]*>(.*?)<\/div>/s', $html, $viewMatches);
            $content = $viewMatches[1] ?? '';
        }

        if (empty($content)) {
            preg_match('/<div id="content"[^>]*>(.*?)<\/div>/s', $html, $idMatches);
            $content = $idMatches[1] ?? '';
        }

        $content = strip_tags($content, '<p><br><b><i><u><h2><h3><ul><ol><li><strong><em><a><img><blockquote><table><tr><td><th>');
        $content = preg_replace('/<img\s+([^>]*?)src\s*=\s*"((?!https?:|\/\/))([^"]+)"/i', '<img $1src="' . self::BASE_URL . '/$3"', $content);
        $content = preg_replace('/<img\s+([^>]*?)src\s*=\s*\'((?!https?:|\/\/))([^\']+)\'/i', '<img $1src=\'' . self::BASE_URL . '/$3\'', $content);

        $this->json([
            'title' => trim(strip_tags($title)),
            'content' => trim($content),
        ]);
    }

    public function read() {
        $url = $_GET['url'] ?? '';
        if (!$url) {
            header('Location: ' . url('/uttayarndham'));
            exit;
        }

        $fullUrl = strpos($url, 'http') === 0 ? $url : self::BASE_URL . $url;
        $html = @file_get_contents($fullUrl, false, $this->getStreamContext());
        $title = 'Uttayarndham';
        $content = '';

        if ($html !== false) {
            preg_match('/<h1[^>]*>(.*?)<\/h1>/s', $html, $titleMatches);
            $title = $titleMatches[1] ?? '';

            preg_match('/<article[^>]*>(.*?)<\/article>/s', $html, $articleMatches);
            $content = $articleMatches[1] ?? '';

            if (empty($content)) {
                preg_match('/<div class="content"[^>]*>(.*?)<\/div>/s', $html, $divMatches);
                $content = $divMatches[1] ?? '';
            }

            if (empty($content)) {
                preg_match('/<div class="view-content[^>]*>(.*?)<\/div>/s', $html, $viewMatches);
                $content = $viewMatches[1] ?? '';
            }

            if (empty($content)) {
                preg_match('/<div id="content"[^>]*>(.*?)<\/div>/s', $html, $idMatches);
                $content = $idMatches[1] ?? '';
            }
        }

        $content = preg_replace('/<[^>]*class="[^"]*\brdf-meta\b[^"]*"[^>]*>.*?<\/[^>]+>/is', '', $content);
        $content = preg_replace('/<div[^>]*class="[^"]*\bfield-name-title\b[^"]*"[^>]*>.*?<\/div>/is', '', $content);
        $content = strip_tags($content, '<p><br><b><i><u><h2><h3><ul><ol><li><strong><em><a><img><blockquote><table><tr><td><th>');
        $content = preg_replace('/<img\s+([^>]*?)src\s*=\s*"((?!https?:|\/\/))([^"]+)"/i', '<img $1src="' . self::BASE_URL . '/$3"', $content);
        $content = preg_replace('/<img\s+([^>]*?)src\s*=\s*\'((?!https?:|\/\/))([^\']+)\'/i', '<img $1src=\'' . self::BASE_URL . '/$3\'', $content);

        return view('pages.uttayarndham.show', [
            'title' => trim(strip_tags($title)),
            'content' => $content,
            'url' => $url,
            'seo' => [
                'title' => trim(strip_tags($title)) . ' - Uttayarndham',
                'description' => 'ອ່ານບົດຄວາມທັມມະ',
            ]
        ]);
    }

    public function tag() {
        $url = $_GET['url'] ?? '';
        if (!$url) {
            header('Location: ' . url('/uttayarndham'));
            exit;
        }

        $fullUrl = strpos($url, 'http') === 0 ? $url : self::BASE_URL . $url;
        $html = @file_get_contents($fullUrl, false, $this->getStreamContext());
        $tagTitle = 'Uttayarndham';
        $items = [];

        if ($html !== false) {
            preg_match('/<h1[^>]*>(.*?)<\/h1>/s', $html, $h1);
            if (!empty($h1[1])) {
                $tagTitle = trim(strip_tags($h1[1]));
            }
            if (empty($tagTitle) || $tagTitle === 'Uttayarndham') {
                preg_match('/<h3[^>]*>(.*?)<\/h3>/s', $html, $h3);
                $tagTitle = !empty($h3[1]) ? trim(strip_tags($h3[1])) : 'Uttayarndham';
            }

            $items = $this->parseTagItems($html);
        }

        $query = $_GET['q'] ?? '';
        if ($query) {
            $items = array_filter($items, function($item) use ($query) {
                return mb_stripos($item['title'], $query) !== false;
            });
            $items = array_values($items);
        }

        return view('pages.uttayarndham.tag', [
            'tagTitle' => $tagTitle,
            'items' => array_values($items),
            'query' => $query,
            'url' => $url,
            'seo' => [
                'title' => $tagTitle . ' - Uttayarndham',
                'description' => 'ລາຍການໃນ ' . $tagTitle,
            ]
        ]);
    }

    private function parseTagItems(string $html): array {
        $items = [];
        $seen = [];

        preg_match('/<div class="view-content[^>]*>(.*?)<\/div>/s', $html, $viewMatch);
        $listingHtml = $viewMatch[1] ?? $html;

        preg_match_all('/<a\s+href="\s*([^"]+)"[^>]*>\s*([^<]+?)\s*<\/a>/s', $listingHtml, $matches, PREG_SET_ORDER);
        foreach ($matches as $m) {
            $title = trim($m[2]);
            $href = trim($m[1]);
            if (empty($title) || $title === $href) continue;
            if (str_contains($href, '#') || str_starts_with($href, 'javascript')) continue;
            $title = preg_replace('/\s+/', ' ', $title);
            if (!empty($title) && !isset($seen[$href])) {
                $seen[$href] = true;
                $items[] = [
                    'title' => $title,
                    'url' => $href,
                ];
            }
        }

        if (empty($items)) {
            $text = strip_tags($html);
            $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
            $text = preg_replace('/\s+/', ' ', $text);
            $lines = explode("\n", $text);
            foreach ($lines as $line) {
                $line = trim($line);
                if (mb_strlen($line) >= 10 && !isset($seen[$line])) {
                    $seen[$line] = true;
                    $items[] = [
                        'title' => mb_strlen($line) > 80 ? mb_substr($line, 0, 80) . '...' : $line,
                        'url' => '',
                    ];
                }
            }
        }

        return $items;
    }

    private function getStreamContext() {
        return stream_context_create(['http' => ['timeout' => 5]]);
    }

    private function fetchTags(int $page): array {
        $url = self::BASE_URL . '/keyword/tags' . ($page > 0 ? '?page=' . $page : '');

        $html = @file_get_contents($url, false, $this->getStreamContext());
        if ($html === false) return [];

        $items = [];
        $seen = [];

        // Try multiple patterns to handle different HTML structures
        $patterns = [
            '/<a\s+href="(\/taxonomy\/term\/\d+)"[^>]*class="[^"]*ki-term[^"]*"[^>]*>(.*?)<\/a>/s',
            '/<a\s+class="[^"]*ki-term[^"]*"[^>]*href="(\/taxonomy\/term\/\d+)"[^>]*>(.*?)<\/a>/s',
            '/<a\s+href="(\/taxonomy\/term\/\d+)"[^>]*>(.*?)<\/a>/s',
        ];

        foreach ($patterns as $pattern) {
            preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
            if (!empty($matches)) {
                foreach ($matches as $m) {
                    $href = isset($m[1]) ? trim($m[1]) : (isset($m[2]) ? trim($m[2]) : '');
                    $content = isset($m[2]) ? $m[2] : (isset($m[3]) ? $m[3] : '');
                    // Remove the span with count
                    $name = preg_replace('/<span[^>]*>.*?<\/span>/s', '', $content);
                    $name = trim(strip_tags($name));
                    if (!empty($name) && !empty($href) && !isset($seen[$href])) {
                        $seen[$href] = true;
                        $items[] = [
                            'title' => $name,
                            'url' => $href,
                        ];
                    }
                }
                if (!empty($items)) break;
            }
        }

        return $items;
    }

    private function json($data) {
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
    }
}