-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.php
383 lines (299 loc) · 8.49 KB
/
helpers.php
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/*
Helpers
Custom modules & snippets
*/
function debug( $thing ) {
ob_start();
?><pre class="notification is-dark"><?php var_dump($thing); ?></pre><?php
$output = ob_get_clean();
echo $output;
}
use phpFastCache\CacheManager;
// Setup File Path on your config files
CacheManager::setDefaultConfig(array(
"path" => __DIR__ . '/cache', // or in windows "C:/tmp/"
));
// In your class, function, you can call the Cache
global $InstanceCache;
$InstanceCache = CacheManager::getInstance('files');
//Cache Times
define("ONE_HOUR", 3600);
define("ONE_DAY", 24 * ONE_HOUR);
define("ONE_YEAR", ONE_DAY * 365);
function isTesting(){
if( !isset( $_GET['testing'] ) ) return false;
return $_GET['testing']===FALSE ? FALSE : filter_var($_GET['testing'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
function isFlushing( $id = false ){
if( !isset( $_GET['flush'] ) ) return false;
$flushing_default = ( empty( $_GET['flush'] ) );
$flushing_id = ( $id == $_GET['flush'] );
// Default Flush( not specific )
if( !$id && $flushing_default ) {
return true;
// Specific flush keyword
} else if( $flushing_id ) {
return true;
} else {
return false;
}
}
function isValidUrl( $url ) {
$parsed_url = parse_url($url);
return isset( $parsed_url['host'] );
}
function startsWith($haystack, $needle)
{
return substr($haystack, 0, strlen($needle)) === $needle;
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
function validateJson($strJson) {
$output = json_decode($strJson);
if(json_last_error() !== JSON_ERROR_NONE) {
debug( json_last_error() );
die();
}
return $output;
}
function in_object($string, $object){
foreach( $object as $item => $item_value ){
if( is_object($item_value) ){
foreach( $item_value as $thing => $thing_value ){
if( $thing_value == $string ) return $item;
}
} else {
if( $item == $string ) return $item;
}
}
return false;
}
//Get Youtube ID of current Stream
function makeYoutubeEmbedLink($html){
//$youtube_id = $youtube_string;
$youtube_id = VideoUrlParser::get_url_id( $html );
//debug($debug);
//If it's a url get the id from url
/*
if( filter_var($youtube_string, FILTER_VALIDATE_URL) ){
$youtube_id = getYTID($youtube_string)
}
*/
//developers.google.com/youtube/player_parameters#Parameters
$embed_data['params'] = array(
// 'autoplay'=>'0',
'rel'=>'0',
'showinfo'=>'0',
'modestbranding'=>'1',
'theme'=>'light',
'autohide'=>'1'
);
$embed_data['id'] = $youtube_id;
$embed_data['youtube-url'] = 'https://youtube.com/embed/'.$youtube_id.'?'.http_build_query($embed_data['params']);
$output = $embed_data['youtube-url'];
return $output;
}
function get_current_url($strip = true) {
// filter function
static $filter;
if ($filter == null) {
$filter = function($input) use($strip) {
$input = str_ireplace(array(
"\0", '%00', "\x0a", '%0a', "\x1a", '%1a'), '', urldecode($input));
if ($strip) {
$input = strip_tags($input);
}
// or any encoding you use instead of utf-8
$input = htmlspecialchars($input, ENT_QUOTES, 'utf-8');
return trim($input);
};
}
return 'http'. (($_SERVER['SERVER_PORT'] == '443') ? 's' : '')
.'://'. $_SERVER['SERVER_NAME'] . $filter($_SERVER['REQUEST_URI']);
}
function get_current_relative_url($strip = true) {
// filter function
static $filter;
if ($filter == null) {
$filter = function($input) use($strip) {
$input = str_ireplace(array(
"\0", '%00', "\x0a", '%0a', "\x1a", '%1a'), '', urldecode($input));
if ($strip) {
$input = strip_tags($input);
}
// or any encoding you use instead of utf-8
$input = htmlspecialchars($input, ENT_QUOTES, 'utf-8');
return trim($input);
};
}
return $filter($_SERVER['REQUEST_URI']);
}
//https://gist.github.com/astockwell/11055104
/*
**
* Video Url Parser
*
* Parses URLs from major cloud video providers. Capable of returning
* keys from various video embed and link urls to manipulate and
* access videos in various ways.
*/
class VideoUrlParser
{
/**
* Determines which cloud video provider is being used based on the passed url.
*
* @param string $url The url
* @return null|string Null on failure to match, the service's name on success
*/
public static function identify_service($url)
{
if (preg_match('%youtube|youtu\.be%i', $url)) {
return 'youtube';
}
elseif (preg_match('%vimeo%i', $url)) {
return 'vimeo';
}
return null;
}
/**
* Determines which cloud video provider is being used based on the passed url,
* and extracts the video id from the url.
*
* @param string $url The url
* @return null|string Null on failure, the video's id on success
*/
public static function get_url_id($url)
{
$service = self::identify_service($url);
if ($service == 'youtube') {
return self::get_youtube_id($url);
}
elseif ($service == 'vimeo') {
return self::get_vimeo_id($url);
}
return null;
}
/**
* Determines which cloud video provider is being used based on the passed url,
* extracts the video id from the url, and builds an embed url.
*
* @param string $url The url
* @return null|string Null on failure, the video's embed url on success
*/
public static function get_url_embed($url)
{
$service = self::identify_service($url);
$id = self::get_url_id($url);
if ($service == 'youtube') {
return self::get_youtube_embed($id);
}
elseif ($service == 'vimeo') {
return self::get_vimeo_embed($id);
}
return null;
}
/**
* Parses various youtube urls and returns video identifier.
*
* @param string $url The url
* @return string the url's id
*/
public static function get_youtube_id($input_url)
{
$url = $input_url;
//Convert Iframe to Link
if( preg_match('/src="([^"]+)"/', $url, $match) ){
$url = $match[1];
}
$youtube_url_keys = array('v','vi');
// Try to get ID from url parameters
$key_from_params = self::parse_url_for_params($url, $youtube_url_keys);
if ($key_from_params) return $key_from_params;
// Try to get ID from last portion of url
return self::parse_url_for_last_element($url);
}
/**
* Builds a Youtube embed url from a video id.
*
* @param string $youtube_video_id The video's id
* @return string the embed url
*/
public static function get_youtube_embed($youtube_video_id, $autoplay = 1)
{
$embed = "http://youtube.com/embed/$youtube_video_id?autoplay=$autoplay";
return $embed;
}
/**
* Parses various vimeo urls and returns video identifier.
*
* @param string $url The url
* @return string The url's id
*/
public static function get_vimeo_id($input_url)
{
$url = $input_url;
//Convert Iframe to Link
if( preg_match('/src="([^"]+)"/', $url, $match) ){
$url = $match[1];
}
// Try to get ID from last portion of url
return self::parse_url_for_last_element($url);
}
/**
* Builds a Vimeo embed url from a video id.
*
* @param string $vimeo_video_id The video's id
* @return string the embed url
*/
public static function get_vimeo_embed($vimeo_video_id, $autoplay = 1)
{
$embed = "http://player.vimeo.com/video/$vimeo_video_id?byline=0&portrait=0&autoplay=$autoplay";
return $embed;
}
/**
* Find the first matching parameter value in a url from the passed params array.
*
* @access private
*
* @param string $url The url
* @param array $target_params Any parameter keys that may contain the id
* @return null|string Null on failure to match a target param, the url's id on success
*/
private static function parse_url_for_params($url, $target_params)
{
parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_params );
foreach ($target_params as $target) {
if (array_key_exists ($target, $my_array_of_params)) {
return $my_array_of_params[$target];
}
}
return null;
}
/**
* Find the last element in a url, without any trailing parameters
*
* @access private
*
* @param string $url The url
* @return string The last element of the url
*/
private static function parse_url_for_last_element($url)
{
$url_parts = explode("/", $url);
$prospect = end($url_parts);
$prospect_and_params = preg_split("/(\?|\=|\&)/", $prospect);
if ($prospect_and_params) {
return $prospect_and_params[0];
} else {
return $prospect;
}
return $url;
}
}