-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvents.php
More file actions
executable file
·138 lines (116 loc) · 5.94 KB
/
Copy pathEvents.php
File metadata and controls
executable file
·138 lines (116 loc) · 5.94 KB
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
<?php
namespace app\modules\sermonaudio;
use humhub\events\OembedFetchEvent;
use app\modules\sermonaudio\jobs\FetchSermonsJob;
use Yii;
class Events
{
public static function onUrlOembedFetch($event)
{
$url = $event->url;
// Detect if dark mode is enabled
$isDarkMode = self::isDarkMode();
$darkParam = $isDarkMode ? '?dark=true' : '';
// Handle broadcaster browser page
if (preg_match('~sermonaudio\.com/broadcaster/([^/?]+)~', $url, $matches)) {
$broadcaster = $matches[1];
// Parse URL to extract query parameters
$parsedUrl = parse_url($url);
parse_str($parsedUrl['query'] ?? '', $queryParams);
// Set default parameters if not present
if (!isset($queryParams['sort'])) {
$queryParams['sort'] = 'newest';
}
if (!isset($queryParams['page_size'])) {
$queryParams['page_size'] = '25';
}
// Add dark mode parameter
$queryParams['dark'] = $isDarkMode ? 'true' : 'false';
$queryString = '?' . http_build_query($queryParams);
// Build embed URL
$embedUrl = 'https://embed.sermonaudio.com/browser/broadcaster/' . htmlspecialchars($broadcaster, ENT_QUOTES, 'UTF-8') . '/' . $queryString;
// Build the HTML directly
$html = '<div data-guid="' . uniqid('oembed-', true) . '" data-richtext-feature="1" data-oembed-provider="sermonaudio-browser" data-url="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '" class="oembed_snippet">';
$html .= '<iframe tabindex="-1" width="1" height="540" src="' . $embedUrl . '" style="min-width: 100%; max-width: 100%;" allow="autoplay" frameborder="0" scrolling="no"></iframe>';
$html .= '</div>';
// Set the result directly, bypassing the provider endpoint logic
$event->setResult($html);
return;
}
// Handle single sermon - new format: /sermons/{id}
if (preg_match('~sermonaudio\.com/sermons/([0-9]+)~i', $url, $matches)) {
$sermonId = htmlspecialchars($matches[1], ENT_QUOTES, 'UTF-8');
// Build the HTML directly with responsive 16:9 container
$html = '<div data-guid="' . uniqid('oembed-', true) . '" data-richtext-feature="1" data-oembed-provider="sermonaudio-sermon" data-url="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '" class="oembed_snippet" style="margin: 0; padding: 0;">';
$html .= '<div style="position:relative;width:100%;height:0;padding-bottom:56.25%;margin:0;">';
$html .= '<iframe tabindex="-1" width="100%" height="100%" src="https://embed.sermonaudio.com/player/v/' . $sermonId . '/' . $darkParam . '" style="position:absolute;left:0;top:0;border:0;display:block;" allowfullscreen frameborder="0" scrolling="no"></iframe>';
$html .= '</div>';
$html .= '</div>';
// Set the result directly, bypassing the provider endpoint logic
$event->setResult($html);
return;
}
// Handle single sermon - old format: /sermoninfo.asp?SID=
if (preg_match('~sermonaudio\.com/sermoninfo\.asp\?SID=([a-z0-9]+)~i', $url, $matches)) {
$sermonId = htmlspecialchars($matches[1], ENT_QUOTES, 'UTF-8');
// Build the HTML directly with responsive 16:9 container
$html = '<div data-guid="' . uniqid('oembed-', true) . '" data-richtext-feature="1" data-oembed-provider="sermonaudio-sermon" data-url="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '" class="oembed_snippet" style="margin: 0; padding: 0;">';
$html .= '<div style="position:relative;width:100%;height:0;padding-bottom:56.25%;margin:0;">';
$html .= '<iframe tabindex="-1" width="100%" height="100%" src="https://embed.sermonaudio.com/player/v/' . $sermonId . '/' . $darkParam . '" style="position:absolute;left:0;top:0;border:0;display:block;" allowfullscreen frameborder="0" scrolling="no"></iframe>';
$html .= '</div>';
$html .= '</div>';
// Set the result directly, bypassing the provider endpoint logic
$event->setResult($html);
return;
}
}
/**
* Cron event handler - runs hourly
*/
public static function onCronRun($event)
{
Yii::$app->queue->push(new FetchSermonsJob());
}
/**
* Daily cron event handler - runs once per day
* Cleans up soft-deleted sermon posts
*/
public static function onDailyCronRun($event)
{
$module = Yii::$app->getModule('sermonaudio');
if ($module) {
$cleanedCount = $module->cleanupSoftDeletedSermonPosts();
if ($cleanedCount > 0) {
Yii::info("SermonAudio daily cleanup: removed {$cleanedCount} soft-deleted posts", 'sermonaudio');
}
}
}
/**
* Detect if the current theme is dark mode
* @return bool
*/
private static function isDarkMode()
{
// Try to detect from theme name
if (isset(Yii::$app->view->theme) && Yii::$app->view->theme->name) {
$themeName = strtolower(Yii::$app->view->theme->name);
if (strpos($themeName, 'dark') !== false) {
return true;
}
}
// Try to detect from theme variables
if (isset(Yii::$app->view->theme)) {
try {
// Check if the theme has a dark mode variable
$isDark = Yii::$app->view->theme->variable('dark', false);
if ($isDark) {
return true;
}
} catch (\Exception $e) {
// Variable doesn't exist, continue
}
}
// Default to light mode
return false;
}
}