/
Aeva-Embed.php
1306 lines (1110 loc) · 47.6 KB
/
Aeva-Embed.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* The original auto-embedder, Aeva!
* Uses portions written by Karl Benson.
*
* @package Wedge
* @copyright 2010 René-Gilles Deberdt, wedge.org
* @license http://wedge.org/license/
* @author see contributors.txt
*/
if (!defined('WEDGE'))
die('Hacking attempt...');
// Main auto embed function
function aeva_main($message)
{
global $context, $settings, $sites, $upto;
// Auto-embedding is disabled. We shouldn't have got this far, but if we have... get out of here.
if (empty($settings['embed_enabled']))
return $message;
// Attempt to load all Enabled Sites, if not already loaded
if (empty($sites) && file_exists(APP_DIR . '/media/Aeva-Sites.php'))
loadSource('media/Aeva-Sites');
// Are we checking a link in the media gallery? We'd best avoid JavaScript then.
if ($context['action'] === 'media' && isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'post')
$context['embed_mg_hack'] = true;
// If we can't use generated version (either just after install, OR permissions meant generated version
// couldn't be created, OR it can't be found), load the full un-optimized version and just keep the popular sites for performance.
if (empty($sites))
{
loadSource(
file_exists(APP_DIR . '/media/Aeva-Sites-Custom.php') ? array('media/Subs-Aeva-Sites', 'media/Aeva-Sites-Custom') : 'media/Subs-Aeva-Sites'
);
// We're using the full version, so we just keep allowed local embeds and popular sites.
foreach ($sites as $a => $b)
{
if (substr($b['id'], 0, 6) == 'local_')
{
// The local plugin for this site is disabled
if (empty($settings['embed_' . $b['plugin']]))
unset($sites[$a]);
}
// No more local ones, from now on only the popular sites will be kept.
elseif ($b['type'] !== 'pop')
unset($sites[$a]);
}
}
// Houston, no sites = no embedding, so get out of here (after reversing any protections in place)
if (empty($sites))
return $message;
// No embeddable links, so reverse protection and get out of here as fast as possible.
if (!empty($context['aeva']['skip']))
{
// Clear this variable, otherwise we won't be able to embed again
unset($context['aeva']['skip']);
// Reverse protection/leftovers
return $message;
}
// Create an array for our vars
if (empty($context['aeva']))
$context['aeva'] = array();
// This array will contain the number of embeddable links we have per site (YouTube, etc.) in $message.
$has = array();
// Set / redo the limits
aeva_limits();
// Find embeddable links for each enabled site to the end
for ($upto = 0, $j = count($sites); $upto < $j; $upto++)
{
$has_any = aeva_match($message);
if ($has_any)
$has[$upto] = $has_any;
// If no active http links remain (making further embedding impossible), then stop
if ($has_any && strpos($message, '<a href="http') === false)
break;
}
// Do we need to ensure the link isn't inside a sentence?
if (empty($settings['embed_incontext']))
{
// First, we start by making a test string with all div tags turned into linebreaks,
// because they act the same way, and all other tags removed (except for <aeva>).
$test = '<br>' . strip_tags(preg_replace('~</?div(?:\s[^>]*)?>~s', '<br>', $message), '<br><aeva>') . '<br>';
$offset = 0;
// Now, we get a list of all <aeva> tags (i.e. videos to embed), as well as their position.
preg_match_all('~<aeva .*?</aeva>~', $test, $matches, PREG_OFFSET_CAPTURE);
preg_match_all('~<aeva .*?</aeva>~', $message, $original_matches, PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $i => &$match)
{
// Now we get the entire test string before the current <aeva> and after it.
$len = strlen($match[0]);
$first = substr($test, 0, $match[1]);
$last = substr($test, $match[1] + $len);
// We remove everything up to the <br>'s closest to the link...
$final = substr($first, strrpos($first, '<br>') + 4) . substr($last, 0, strpos ($last, '<br>'));
// ...and remove whitespace and other <aeva>'s. If there's anything left, it means we're in a sentence. Hail to the king.
if (trim(preg_replace('~<aeva .*?</aeva>~', '', $final)) !== '')
{
$message = substr_replace($message, str_replace(array('<aeva ', '</aeva>'), array('<a ', '</a>'), $match[0]), $original_matches[0][$i][1] - $offset, strlen($original_matches[0][$i][0]));
$offset += 6;
}
}
}
// ... Start embedding ;)
foreach ($has as $upto => $num)
if ($num > 0)
aeva_match($message, true);
// Reverse protections and return the finished embedded content
return aeva_reverse_protection($message);
}
// Protects urls in places we don't want to touch, from being embedded.
// aeva_protection(
// array('<noembed>' => false), = BBC/html item to protect => whether to retain it
// $message, = Content
// true = If no http links remain, return non-protected and set $context['embed_skip']
// );
function aeva_protection($array, $input, $reverse = false)
{
global $context, $settings;
// Return if either is empty
if (empty($input) || empty($array))
return $input;
// Protect each item
foreach ($array as $item => $retain)
{
// Quotes
if ($item == 'quote')
{
// Quotes are not to be protected from embedding inside
if (!empty($settings['embed_quotes']))
continue;
// Always retained, never replaced, we need a preg for this one as well.
$input = preg_replace(
array('~(\[quote[]\s=])~i', '~\[/quote]~i'),
array('[noae]$1', '[/quote][/noae]'),
$input
);
// With quotes they are always retained, never replaced.
// So we've reused this value instead of decide whether to continue to full protection or just wrap
if (empty($retain))
continue;
}
// Html? tag
elseif ($item == '<noembed>')
$input = str_ireplace(
array('<noembed>', '</noembed>'),
array('[noae]<noembed>', '</noembed>[/noae]'),
$input
);
// All other tags apart from [noae]
elseif ($item != 'noae')
$input = str_ireplace(
array('[' . $item . ']', '[/' . $item . ']'),
array('[noae]' . (empty($retain) ? '' : '[' . $item . ']'), (empty($retain) ? '' : '[/' . $item . ']') . '[/noae]'),
$input
);
$unaltered = $input;
// Protect the item.
$input = aeva_protect_recursive($input);
// Recursive error? Check for null/empty.
if (empty($input))
{
// Set this variable to prevent further embedding for this topic.
$context['aeva']['skip'] = true;
// Returned the pre-altered version
return $unaltered;
}
// Oh crumbs! Remove any leftovers which could screw up recursions.
$input = aeva_crumbs($input);
}
// Tidy up
unset($unaltered);
// No links remaining? No further embedding possible, so set this variable before we return.
if ($reverse === true && strpos($input, '<a href="http://') === false)
$context['aeva']['skip'] = true;
return $input;
}
// Remove any breadcrumbs (leftovers) remaining
function aeva_crumbs($input)
{
// Lowercase only as [noae] is only used by this mod.
return str_replace(array('[noae]', '[/noae]'), '', $input);
}
// Reverses protections, and also clears any leftovers in one go.
function aeva_reverse_protection($input)
{
return str_ireplace(array('[noembed]', '[/noembed]'), '', str_replace(array('noae://', '[noae]', '[/noae]'), array('http://'), $input));
}
// Callback, only build the embed on each match
function aeva_match(&$message, $for_real = false)
{
global $sites, $upto, $boardurl, $settings;
static $local_done = false;
// Prevent stupid loop/crash. Also, if loading full version, return if disabled.
if (empty($sites[$upto]['pattern']) || !empty($sites[$upto]['disabled']))
return 0;
// I don't think we'll be saving $sites later, so we'll just
// transform {local} variables in it so it's only done once...
$regex =& $sites[$upto]['pattern'];
// Local files embed - do some magic
if (!$local_done && strpos($regex, '{local}') !== false)
{
// Parse the boardurl to grab the domain we're on
if (!empty($settings['embed_nonlocal']))
$regex = str_replace('{local}', '[a-z]+://[^"]+?/', $regex);
else
{
$x = @parse_url($boardurl);
$x['scheme'] = !empty($x['scheme']) ? $x['scheme'] : 'http';
$x['host'] = !empty($x['host']) ? $x['host'] : null;
// We can't parse your domain, so return to avoid errors
if (empty($x['host']))
return 0;
$regex = str_replace('{local}', preg_quote($x['scheme'], '`') . '://(?:[a-z0-9-]{1,32}\.){0,3}' . preg_quote($x['host'], '`') . '/', $regex);
}
}
// Local sites are at the beginning. If this site isn't local, we can stop looking.
else
$local_done = true;
// Match everything, and do the actual embedding.
if ($for_real)
{
$message = preg_replace_callback('`<aeva href="(' . $regex . '[^"]*)"[^>]*>(.*?)</aeva>`', 'aeva_build_object', $message);
return;
}
// Match everything, and return the number of existing embeddable links for this site.
$message = preg_replace('`<a (href="' . $regex . '[^"]*"[^>]*>.*?)</a>`', '<aeva $1</aeva>', $message, -1, $has_any);
return $has_any;
}
// The core function: replace matched links with the full embedded object.
function aeva_build_object($input)
{
global $context, $settings, $sites, $upto, $boardurl, $txt;
static $swfobjects = 0;
// Load the language files, English if no translated version is available.
if (!isset($txt['aeva']) && loadLanguage('Media') == false)
loadLanguage('Media', 'english');
if ($context['aeva']['remaining'] == 0)
return str_replace(array('<aeva href="http://', '</aeva>'), array('<a href="noae://', '</a>'), preg_replace('`#[\w/.~-]*`', '', $input[0], 1)) . ' ' . $txt['media_too_many_embeds'];
$arr =& $sites[$upto];
$use_object_init = AJAX || INFINITE || WEDGE == 'SSI' || $context['action'] === 'feed' || !empty($settings['embed_noscript']) || !empty($context['embed_mg_hack']);
$use_object = $use_object_init || (!empty($arr['plugin']) && $arr['plugin'] != 'flash') || !empty($arr['allow-script']) || ($arr['id'] == 'yav' && we::is('firefox'));
$object = $extra_js = '';
$link = '<a href="<aeva-link>" target="_blank" class="aeva_link bbc_link new_win"><aeva-title></a>';
// Depending on the plugin, use different parameters
if (empty($arr['plugin']) || $arr['plugin'] == 'flash')
{
$plugin = array(
'classid' => 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',
'type' => 'application/x-shockwave-flash',
'params' => array(
'wmode' => 'transparent', 'quality' => 'high', 'allowFullScreen' => 'true', 'allowScriptAccess' => !empty($arr['allow-script']) ? 'always' : 'never',
'pluginspage' => 'http://get.adobe.com/flashplayer', 'autoplay' => 'false', 'autostart' => 'false',
),
);
}
elseif ($arr['plugin'] == 'divx')
{
$plugin = array(
'classid' => 'clsid:67dabfbf-d0ab-41fa-9c46-cc0f21721616',
'type' => 'video/divx',
'codebase' => 'http://go.divx.com/plugin/DivXBrowserPlugin.cab',
'params' => array(
'allowFullScreen' => 'true', 'allowScriptAccess' => 'never', 'pluginspage' => 'http://go.divx.com/plugin/download/',
'custommode' => 'none', 'autoPlay' => 'false', 'mode' => 'full', 'minVersion' => '1.0.0', 'allowContextMenu' => 'true',
'url' => '$2', 'bannerEnabled' => 'false', 'showpostplaybackad' => 'false',
),
'src' => 'src',
);
}
elseif ($arr['plugin'] == 'wmp')
{
$plugin = array(
'classid' => 'clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95',
'type' => 'application/x-mplayer2',
'params' => array(
'wmode' => 'transparent', 'quality' => 'high', 'allowFullScreen' => 'true', 'allowScriptAccess' => 'never',
'pluginspage' => 'http://www.microsoft.com/Windows/MediaPlayer/', 'ShowControls' => 'True', 'autostart' => 'false', 'autoplay' => 'false',
),
'src' => 'filename', // wmp uses filename instead of src or movie for the param name
);
}
elseif ($arr['plugin'] == 'quicktime')
{
$plugin = array(
'classid' => 'clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b',
'type' => 'video/quicktime',
'codebase' => 'http://www.apple.com/qtactivex/qtplugin.cab',
'params' => array(
'pluginspage' => 'http://www.apple.com/quicktime/download/', 'wmode' => 'transparent', 'quality' => 'high', 'allowFullScreen' => 'true',
'allowScriptAccess' => 'never', 'scale' => 'aspect', 'loop' => 'false', 'controller' => 'true', 'autoplay' => 'false',
),
'src' => 'src',
);
}
elseif ($arr['plugin'] == 'realmedia')
{
$plugin = array(
'classid' => 'clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa',
'type' => 'audio/x-pn-realaudio-plugin',
'params' => array(
'pluginspage' => 'http://www.real.com', 'wmode' => 'transparent', 'quality' => 'high', 'allowFullScreen' => 'true',
'allowScriptAccess' => 'never', 'controls' => 'imagewindow', 'console' => 'video', 'autostart' => 'false',
),
'src' => 'src',
'extra' => (we::is('ie') ? '<object id="rvocx" classid="clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa" width="$8px" height="30px"><param name="src" value="$1"><param name="autostart" value="false"><param name="controls" value="ControlPanel"><param name="console" value="video">' : '') .
'<embed src="$1" width="$8px" height="30" controls="ControlPanel" type="audio/x-pn-realaudio-plugin" console="video" autostart="false"></embed>' . (we::is('ie') ? '</object>' : ''),
);
}
// Build the params with both formats for <object> and <embed>
$swo_params = $object_params = $embed_params = '';
$tentative_title = array_pop($input);
// Get the option list, such as videolink#hq-w500-h400
$options = strrchr($input[1], '#');
$opt = empty($options) ? array() : str_replace('~', '-', explode('-', substr($options, 1)));
// Strip the #options out of the original link
$input[1] = preg_replace('~#.*~', '', $input[1]);
$input = preg_replace_callback('~#[^"<]*~', function ($match) { return strtr($match[0], '~', '-'); }, $input);
if ($tentative_title && substr($tentative_title, 0, 7) !== 'http://')
$title = $tentative_title;
$current_width = isset($_COOKIE['embed_quality']) ? $_COOKIE['embed_quality'] : 1;
$size_type = isset($arr['size']['normal']) ? $arr['size'][isset($opt['ws'], $arr['size']['ws']) ? 'ws' : 'normal'] : (isset($arr['size']) ? $arr['size'] : array(0, 0));
$default_movie = is_array($arr['movie']) ? $arr['movie']['normal'] : $arr['movie'];
$fixed_size = !empty($size_type[2]);
// This is the place where we do our anchor settings magic... Parse the #ws-hd stuff!
foreach ($opt as $ion)
{
if ($ion === '' || $ion === 'id' || $ion === 'key')
continue;
// Add some information for the user that external embedding of this clip is disallowed by the video's author
if ($ion == 'noexternalembed')
return str_replace(array('<aeva href="http://', '</aeva>'), array('<a href="noae://', '</a>'), preg_replace('`#noext[\w/.~-]*`', '', $input[0])) . ' ' . $txt['media_noexternalembedding'];
if (is_array($arr['movie']) && isset($arr['movie'][$ion]))
$movie_type = $ion;
if (isset($arr['size'][$ion]))
$size_type = $arr['size'][$ion];
if ($ion[0] == 'w' && is_numeric(substr($ion, 1)))
$size_type[0] = (int) substr($ion, 1);
elseif ($ion[0] == 'h' && is_numeric($ion[1]))
$size_type[1] = strpos($ion, '+') === false ? (int) substr($ion, 1) : (int) substr($ion, 1, strpos($ion, '+')-1) + (int) substr($ion, strpos($ion, '+')+1);
elseif (empty($title) && substr($ion, 0, 2) == 't=')
$title = base64_decode(substr($ion, 2));
elseif ($ion == 'center')
$center_this = true;
}
if (!empty($plugin))
{
foreach ($plugin['params'] as $a => $b)
{
if ($b === '')
continue;
$swo_params .= ',' . $a . ':"' . $b . '"';
if ($use_object)
{
$embed_params .= ' ' . $a . '="' . $b . '"';
if (we::is('ie'))
$object_params .= '<param name="' . $a . '" value="' . $b . '">';
}
}
}
unset($a, $b);
if (!empty($arr['show-flashvars']) && $use_object)
{
$embed_params .= ' flashvars="<flashvars>"';
if (we::is('ie'))
$object_params .= '<param name="flashvars" value="<flashvars>">';
}
if (!empty($plugin) && (AJAX || INFINITE || WEDGE == 'SSI'))
{
if ($swfobjects++ === 0)
$swfobjects = rand(1000,9999);
}
elseif (!empty($plugin) && !$swfobjects++)
{
add_css('
.maeva { font-size: 8pt; line-height: 11pt; overflow: auto }
.maeva a:link, .maeva a:visited { text-decoration: none !important; border-bottom: 0 !important }
.aeva_dq { font-weight: bold }
.aeva_t { text-align: left; padding-top: 3px }
.aeva_q { text-align: right; padding-top: 3px }
a.aeva_dq:link { color: inherit }');
if (!$use_object_init)
{
add_js_file('http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js', true);
add_js('
aevams = {', substr($swo_params, 1), '};', !empty($settings['embed_expins']) ? '
aeinst = "' . $boardurl . '/expressInstall.swf";' : '');
if (!$fixed_size)
add_js('
function aevatq(q, id, w, h)
{
var yt = $("#aevawi" + id)[0];
$("#sae" + id).css("width", w);
$("#saeva" + id).css("height", h);
var dat = yt.data != "" ? yt.data : yt.movie;
$("#aqc" + id).children("a").each(function () {
this.className = this.href.indexOf("(" + q + ",") > 0 ? "aeva_dq" : "";
});
swfobject.createSWF({ data: dat, width: w, height: h }, aevams, "aevawi" + id);
document.cookie = "aeva_quality=" + q + ";path=/";
}');
}
}
$embed = empty($movie_type) ? $default_movie : (isset($arr['movie'][$movie_type]) ? $arr['movie'][$movie_type] : $default_movie);
// We need to deal with videos where titles can be viewed inside the thumbnail...
$show_video_title = (int) (empty($settings['embed_inlinetitles']) || ($settings['embed_inlinetitles'] == 1 && empty($title)));
if ($arr['id'] == 'ytb')
$embed .= (!empty($settings['embed_yq']) ? '&hd=1' : '') . '&showinfo=' . $show_video_title;
elseif ($arr['id'] == 'vimeo')
$embed .= '&show_title=' . $show_video_title;
$sw = $altw = !$size_type[0] ? '100%' : $size_type[0];
$sh = $alth = !$size_type[1] ? '100%' : $size_type[1];
$max_width = !empty($settings['embed_max_width']) && $sw != '100%' && !$fixed_size ? (int) $settings['embed_max_width'] : $sw;
if ($sw != '100%')
{
$ui_height = isset($arr['ui-height']) ? (int) $arr['ui-height'] : 0;
$altw = $max_width;
$alth = round(($max_width / $sw) * ($sh - $ui_height)) + $ui_height;
// If maximum width is lower than the default embed size, always use the max width.
if ($max_width < $sw)
{
$sw = $altw;
$sh = $alth;
}
if ($current_width == 1)
{
list ($sw, $altw) = array($altw, $sw);
list ($sh, $alth) = array($alth, $sh);
}
}
$swp = $sw == '100%' ? $sw : $sw . 'px';
$shp = $sh == '100%' ? $sh : $sh . 'px';
$show_raw_link = (isset($title) && (empty($settings['embed_titles']) || $settings['embed_titles'] < 2)) ||
(!empty($settings['embed_includeurl']) && !empty($arr['show-link']) && empty($context['embed_mg_calling']));
$show_something_below = $show_raw_link || (!$use_object && $sw != $altw);
if (!empty($plugin))
{
$object .= '
<table class="maeva cp0 cs0' . (!empty($settings['embed_center']) || !empty($center_this) ? ' centered' : '') . '" style="width: ' . $swp . '" id="sae' . $swfobjects . '">
<tr><td style="width: ' . $swp . '; height: ' . $shp . '"' . ($show_something_below ? ' colspan="2"' : '') . ' id="saeva' . $swfobjects . '">';
// Some sites might have stuff to show before
if (!empty($arr['html-before']))
$object .= $arr['html-before'];
}
if (empty($plugin))
$object = $arr['movie'];
// Build the <object> (Non-Mac IE Only)
elseif ($use_object)
{
if (we::is('ie'))
$object .= '<object classid="' . $plugin['classid'] . '" ' .
(!empty($plugin['codebase']) ? 'codebase="' . $plugin['codebase'] . '" ' : '') .
'type="' . $plugin['type'] . '" width="' . $sw . '" height="' . $sh . '">' .
'<param name="' . (empty($plugin['src']) ? 'movie' : $plugin['src']) . '" value="' . $embed . '">' . $object_params;
// Build the <embed>
$object .= '<embed type="' . $plugin['type'] . '" src="' . $embed
. '" width="' . $sw . '" height="' . $sh . '"' . $embed_params . '>';
// <noembed> tag containing raw link in case Flash is disabled. However, don't include it if we're going to show the raw link anyway.
if (!$show_raw_link)
$object .= '<noembed>' . $link . '</noembed>';
// If using <object> remember to close it
if (we::is('ie'))
$object .= '</object>';
}
else
{
$object .= '<div id="aevid' . $swfobjects . '">' . ($show_raw_link ? '' : $link) . '</div>';
$extra_js .= '
swfobject.embedSWF("<aeva-embed>", "aevid' . $swfobjects . '", "' . $sw . '", "' . $sh . '", "9", ' . (!empty($settings['embed_expins']) ? 'aeinst' : '""') . ', {}, ' . (!empty($arr['show-flashvars']) ?
'{' . substr($swo_params, 1) . ',flashvars:"<flashvars>"}' : 'aevams') . ', {id:"aevawi' . $swfobjects . '"});';
}
// Any extra - required for RealPlayer since it needs dual embeds, and fix the width/height while we're at it
if (!empty($plugin['extra']))
$object .= str_replace(array('$8', '$9'), array($sw, $sh), $plugin['extra']);
// Like before, but after ;)
if (!empty($arr['html-after']))
$object .= $arr['html-after'];
if (!empty($plugin))
$object .= '</td>';
if ($show_something_below && empty($plugin))
$object .= '<br>' . $link;
elseif ($show_something_below)
{
$object .= '</tr>
<tr><td class="aeva_t">';
// Include the original link? Don't do it if AM's gallery is calling for integration, because it would create a double embed.
if ($show_raw_link)
$object .= $link;
$object .= '</td><td class="aeva_q" id="aqc' . $swfobjects . '">';
// Only show the Normal | Large links if we can actually enlarge the object and the 'fixed size' parameter isn't set.
if (!$use_object && !$fixed_size && $sw != $altw)
$object .= '<a href="javascript:aevatq(0, ' . $swfobjects . ', ' . min($sw, $altw) . ', ' . min($sh, $alth) . ');" ' . ($sw < $altw ? 'class="aeva_dq" ' : '') . 'title="-">' . $txt['media_small'] . '</a>'
. ' | <a href="javascript:aevatq(1, ' . $swfobjects . ', ' . max($sw, $altw) . ', ' . max($sh, $alth) . ');" ' . ($sw > $altw ? 'class="aeva_dq" ' : '') . 'title="+">' . $txt['media_large'] . '</a>';
$object .= '</td>';
}
// Close our table from earlier.
if (!empty($plugin))
$object .= '</tr></table>';
else
$object = str_replace(array('{width}', '{height}', '{int:width}', '{int:height}'), array($swp, $shp, $sw, $sh), $object);
// Replace the $1, $2, $3 etc
for ($i = 1, $j = count($input); $i < $j; $i++)
{
if (strpos($object, '(') || strpos($embed, '('))
{
// (&option=$1|¬hing) will say &option=test if $1 is 'test', and ¬hing if $1 is empty
$object = preg_replace('~\(([^$()|]*)\$' . $i . '([^$()|]*)\|([^)]*)\)~', empty($input[$i]) ? '$3' : '${1}' . $input[$i] . '$2', $object);
$embed = preg_replace('~\(([^$()|]*)\$' . $i . '([^$()|]*)\|([^)]*)\)~', empty($input[$i]) ? '$3' : '${1}' . str_replace('&', '&', $input[$i]) . '$2', $embed);
}
$object = str_replace('$' . $i, $input[$i], $object);
$embed = str_replace('$' . $i, str_replace('&', '&', $input[$i]), $embed);
}
// All ampersands need to be &, however some may be already encoded. So to prevent double-encoding, convert them back to &, then back again
$object = str_replace(
array('&', '&', '&nbsp;', '&amp;', '<aeva-link>', '<aeva-title>'),
array('&', '&', ' ', '&', $input[1], isset($title) ? $title : $input[1]),
$object
);
add_js(
str_replace('<aeva-embed>', $embed, $extra_js)
);
if (strpos($object, '<flashvars>') !== false)
$object = str_replace('<flashvars>', substr(strrchr($embed, '?'), 1), $object);
// Reduce remaining replacements allowed
if ($context['aeva']['remaining'] != -1)
{
$context['aeva']['remaining'] -= 1;
$context['aeva']['remaining_per_page'] -= 1;
}
return $object;
}
// Limits change, so this keeps track between nolimits vs limits, limits per page vs post
function aeva_limits()
{
global $settings, $context;
// Per post limit is always reset everytime this function is called
$context['aeva']['remaining_per_post'] = empty($settings['embed_max_per_post']) ? -1 : $settings['embed_max_per_post'];
// First time - work out limit per page
if (!isset($context['aeva']['remaining_per_page']))
$context['aeva']['remaining_per_page'] = empty($settings['embed_max_per_page']) ? -1 : $settings['embed_max_per_page'];
// First time - work out the max for this object between the two different ones
// If both are unlimited (-1), then remaining = unlimited as well, otherwise temp value
if (!isset($context['aeva']['remaining']))
$context['aeva']['remaining'] = $context['aeva']['remaining_per_page'] == -1 && $context['aeva']['remaining_per_post'] == -1 ? -1 : 99;
// If it's limited, we need to work out the limit to use
if ($context['aeva']['remaining'] != -1)
{
// Post is unlimited, so use the page limit
if ($context['aeva']['remaining_per_post'] == -1)
$context['aeva']['remaining'] = $context['aeva']['remaining_per_page'];
// Page is unlimited, so use the post limit
elseif ($context['aeva']['remaining_per_page'] == -1)
$context['aeva']['remaining'] = $context['aeva']['remaining_per_post'];
else
$context['aeva']['remaining'] = min($context['aeva']['remaining_per_post'], $context['aeva']['remaining_per_page']);
}
}
// Links urls that haven't already been linked
function aeva_autolink_urls($input)
{
// Parse any URLs.... And ensure they're not already auto-linked!
$input = preg_replace_callback('~(=|\[(?:url|img(?:\s[^]]*)?)])(http://|https://|ftp://|ftps://|www\.)~i', function ($match) { return $match[0] . '!<AEVA_LOOKBEHIND>!'; }, $input);
if (preg_match('~(?:http://|www\.)[^!]~i', $input))
{
$input = preg_replace(
array(
'`(?<=^|[\s>.(;\'"])((?:http|https|ftp|ftps)://[\w%@:|-]+(?:\.[\w%-]+)*(?::\d+)?(?:/[\w~%.@,?&;=#+:\'\\\\-]*|[({][\w~%.@,?&;=#(){}+:\'\\\\-]*)*[/\w~%@?;=#}\\\\-]?)`',
'`(?<=^|[\s>.(;\'"])(www(?:\.[\w-]+)+(?::\d+)?(?:/[\w~%.@,?&;=#+:\'\\\\-]*|[({][\w~%.@,?&;=#(){}+:\'\\\\-]*)*[/\w~%@?;=#}\\\\-])`i'
), array(
'[url]$1[/url]',
'[url=http://$1]$1[/url]'
), $input
);
}
// Return it
return str_replace('!<AEVA_LOOKBEHIND>!', '', $input);
}
// Protects [noae] bbcoded items - recursive. Used instead of the ACTUAL tag to prevent infinite loop.
// The tags are lost on each recursion.
// Known issue: anchor settings remain visible when video isn't embedded. No known "good" solution?
function aeva_protect_recursive($input)
{
// Prevent error if null or empty
if (empty($input))
return false;
// Changing http:// to noae:// should prevent ALL of the links from matching a site.
if (is_array($input))
$input = str_replace('http://', 'noae://', $input[1]);
// The goddess of all regexps - works for complex nested bbcode.
return preg_replace_callback(
'~\[noae]((?>[^[]|\[(?!/?noae])|(?R))+?)\[/noae]~',
'aeva_protect_recursive',
$input
);
}
// The 'Lookup' function to grab a page and match a regex
function aeva_lookup_and_match($regex, $url, $fetch_title = '')
{
// Don't timeout.
@set_time_limit(600);
// Hmm we might need more...
ini_set('memory_limit', '64M');
// Go get 'em tiger
$data = @aeva_fetch($url);
// Return if empty or too short to be a proper response/page
if (empty($data) || strlen($data) < 100)
return false;
$ret = array();
if (is_array($fetch_title))
{
foreach ($fetch_title as $search_title)
{
if (!preg_match('~' . $search_title . '~is', $data, $output) || empty($output[1]) || trim($output[1]) === '')
continue;
$ret['title'] = preg_replace('~\s+~', ' ', trim(strip_tags($output[1])));
break;
}
}
// Find the actual data inside the page. Returns false on failure.
if (!is_array($regex))
return array('inline' => !empty($regex) && preg_match('`' . $regex . '`im', $data, $output) ? $output[1] : false, 'title' => !empty($ret['title']) ? $ret['title'] : '');
foreach ($regex as $a => $b)
if (preg_match('`' . $b . '`i', $data, $output))
$ret[$a] = isset($output[1]) ? $output[1] : '';
return $ret;
}
// Callback, only build the embed on each match
function embed_lookups_obtain_callback($input)
{
global $sites, $upto, $settings;
$arr =& $sites[$upto];
// On callback, this is an array.
if (is_array($input))
{
// Secondary url - we use the variable in another url.
$url = !empty($arr['lookup-actual-url']) ? str_replace('$1', $input[3], $arr['lookup-actual-url']) : $input[2];
// Search for a title if: link has no existing title, current video site has a title lookup, or all video sites
// are set to look for a title, and title storage is enabled (aeva_titles is not 1 or 3). I know, it's a bit scary.
$has_title = preg_match('~(?:<a [^>]+>|\[url=[^]]+])(.+)(?:</a>|\[/url])~', $input[0], $tst);
$lookup_title = (empty($arr['lookup-title']) && (empty($settings['embed_lookup_titles']) || isset($arr['lookup-title']))) || !empty($has_title)
|| (!empty($settings['embed_titles']) && $settings['embed_titles'] != 2) ? false : array('<meta\s+name="title"\s+content="([^"]+)"', '<title>([^<]+)</title>');
if (is_array($lookup_title) && !empty($arr['lookup-title']) && $arr['lookup-title'] !== true)
array_unshift($lookup_title, $arr['lookup-title']);
$actual = empty($arr['lookup-pattern']) && empty($lookup_title) ? false : aeva_lookup_and_match(!empty($arr['lookup-pattern']) ? $arr['lookup-pattern'] : '', $url, $lookup_title);
if (is_array($actual))
{
if (isset($actual['title']))
{
$title = $actual['title'];
unset($actual['title']);
if (isset($actual['inline']))
$actual = empty($actual['inline']) ? $url : $actual['inline'];
}
if (is_array($actual) && isset($actual['error']))
{
$title = '[b][color=red]![/color][/b] ' . $actual['error'] . (isset($title) ? ' - ' . $title : '');
unset($actual['error']);
}
if (isset($actual['w'], $arr['size'][0]) && $actual['w'] == $arr['size'][0])
unset($actual['w']);
if (isset($actual['h'], $arr['size'][1]) && $actual['h'] == $arr['size'][1])
unset($actual['h']);
if (is_array($actual) && (empty($arr['lookup-skip-empty']) || !empty($actual)))
{
if (!empty($arr['lookup-final-url']) && !empty($actual['id']))
{
$url = $arr['lookup-final-url'];
$final_id = $actual['id'];
unset($actual['id']);
}
$opt = '#';
foreach ($actual as $a => $b)
$opt .= $a . str_replace('-', '~', $b) . '-';
$actual = $url . substr($opt, 0, max(1, strlen($opt)-1));
}
}
// Failed so return
if (empty($actual))
return $input[0];
// Url-decode some characters
if (!empty($arr['lookup-urldecode']))
for ($j = 0; $j < $arr['lookup-urldecode']; $j++)
$actual = urldecode($actual);
// Unencode some html characters to prevent double encoding by Wedge
if (!empty($arr['lookup-unencode']))
$actual = un_htmlspecialchars($actual);
// If we were only returned a partial url, we add the remainder/actual bit here
if (!empty($arr['lookup-final-url']))
{
// Normally, all websites should use $input[2] if the ID wasn't found.
// I'm afraid I don't remember why I did something else, so I'm only using $input[2] for YouTube now.
$actual = empty($final_id) ? ($arr['id'] == 'ytb' ? $input[2] . '#' : str_replace('$1', $actual, $arr['lookup-final-url'])) : str_replace('$1', $final_id, $actual);
// Cheat to put the original url looked up back in the link
$actual = str_replace('$2', $url, $actual);
}
if (!empty($title))
$title = html_entity_decode(str_replace('&', '&', westr::force_utf8($title)), ENT_QUOTES, 'UTF-8');
return !empty($title) ? '[' . $input[1] . '=' . $actual . ']' . $title . '[/' . $input[1] . ']' : str_replace($input[2], $actual, $input[0]);
}
else
{
// Complete the regex pattern
$regex = '`\[(i?url)[]=](' . (!empty($arr['lookup-url']) ? $arr['lookup-url'] : $arr['pattern']) . ')(?:[^]#[]*?][^[]*\[/\1\]|[^#[]*?\[/\1\])`i';
return preg_replace_callback($regex, 'embed_lookups_obtain_callback', $input);
}
}
// Goes through each site definition, calling the lookup to match any urls on posting.
function embed_lookups_match($input)
{
global $context, $sites, $upto, $settings;
// Create an array for our vars
if (empty($context['aeva']))
$context['aeva'] = array();
// Count sites
$j = count($sites);
// For each enabled site to the end, attempt to embed
for ($upto = 0; $upto < $j; $upto++)
if (!empty($sites[$upto]['lookup-url']) || ((!empty($sites[$upto]['lookup-title']) || !empty($settings['embed_lookup_titles'])) && (empty($settings['embed_titles']) || $settings['embed_titles'] == 2)))
$input = embed_lookups_obtain_callback($input);
// Undo all protection
$input = str_replace('noae://', 'http://', $input);
// All sites are done.
return $input;
}
// This function is called on Posting to protect bbcode and then obtain any video/audio clips necessary
// Called on both quick reply and full posting
function aeva_onposting($input)
{
global $settings, $sites;
// Exit if all three are disabled:
// - Lookups (retrieve final URL, check whether embeds are allowed, etc.)
// - Fix embed HTML (when n00bs try to post the full embed code rather than just the URL)
if (empty($settings['embed_fix_html']) && empty($settings['embed_lookups']))
return $input;
$array = array(
// bbc => retain
'code' => true,
'html' => true,
'php' => true,
'noembed' => true,
);
// Protect quotes from embedding
// False doesn't mean retain or replace, but whether we're fully protecting it.
if (empty($settings['embed_quote']))
$array['quote'] = true;
// Protect all these items
$input = aeva_protection($array, $input, false);
// Attempt to load - Enabled Sites
if (empty($sites) && file_exists(APP_DIR . '/media/Aeva-Sites.php'))
loadSource('media/Aeva-Sites');
// If we can't use generated version (either just after install, OR permissions meant generated
// version couldn't be created, OR it can't be found), load the full un-optimized version.
if (empty($sites))
loadSource(
file_exists(APP_DIR . '/media/Aeva-Sites-Custom.php') ? array('media/Subs-Aeva-Sites', 'media/Aeva-Sites-Custom') : 'media/Subs-Aeva-Sites'
);
// Noob users might have included the full embed code provided by the site
if (!empty($settings['embed_fix_html']))
$input = aeva_fix_html($input);
// Do lookups
if (!empty($settings['embed_lookups']))
{
// Will surround raw links with [url] tags if not already done, so they get can get looked up/embedded by Aeva.
if (!empty($settings['autoLinkUrls']))
$input = aeva_autolink_urls($input);
$input = embed_lookups_match($input);
}
// Undo all protection and return
return str_replace('noae://', 'http://', $input);
}
// Noobs might use the full embed code provided by the site, so try to fix it on posting (replace with an embeddable link and save some db space as well)
function aeva_fix_html($input)
{
global $sites;
// If no sites, return
if (empty($sites))
return $input;
// No html objects potentially to replace. Different sites use different objects, some <embed> <object> <script> or <iframe>
if (!preg_match('~<(?:embed|object|script|iframe)|\?width=~i', $input))
return $input;
$stripped = false;
while (!$stripped)
{
// For each enabled site attempt to fix the html
foreach ($sites as $site)
{
// If this site doesn't have an pattern to fix, continue
if (empty($site['fix-html-pattern']) || empty($site['pattern']))
continue;
// Re-use the embed pattern
$regex = str_replace('$1', '(' . $site['pattern'] . ')[^">]*?', $site['fix-html-pattern']);
// Complete the pattern with delimiter and utf8 support. If starting with a <, make sure to escape it
$regex = '`' . ($regex[0] == '<' ? '\\' : '') . $regex . '`isu';
// Match, and replace with a valid link
$input = preg_replace($regex, empty($site['fix-html-url']) ? "$1\r\n" : $site['fix-html-url'], $input);
if (!empty($site['fix-html-urldecode']))
$input = urldecode($input);
// No html objects potentially to replace. Then break.
if (!preg_match('~<(?:embed|object|script|iframe)|\?width=~i', $input))
{
$stripped = true;
break;
}
}
// If nothing was changed, maybe backslashes are ruining it? Try again with them stripped. If it still won't work, give up.
if (!$stripped)
if (strpos($input, '\\"') !== false)
$input = stripslashes($input);
else
$stripped = true;
}
// Tidy up
unset($site, $regex);
// All sites are done.
return $input;
}
// This annoying little function is needed when the frigging server really doesn't want to cooperate...
// Inspired (and hopefully improved) from http://www.php.net/manual/en/function.curl-setopt.php#71313
function aeva_curl_redir_exec($ch)
{
static $curl_loops = 0;
if ($curl_loops++ >= 20)
{
$curl_loops = 0;
return false;
}
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
list ($header, $data) = preg_split("/(\n\n|\r\r|\r\n\r\n)/", $data, 2);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302)
{
$matches = array();
preg_match('/Location:(.*?)[\r\n]/', $header, $matches);
if ($url = @parse_url(trim(array_pop($matches))))
{
$last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
$url['scheme'] = $url['scheme'] ? $url['scheme'] : $last_url['scheme'];
$url['host'] = $url['host'] ? $url['host'] : $last_url['host'];
$url['path'] = $url['path'] ? $url['path'] : $last_url['path'];
$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (isset($url['query']) ? '?' . $url['query'] : '');
curl_setopt($ch, CURLOPT_URL, $new_url);
return aeva_curl_redir_exec($ch);
}
}
$curl_loops = 0;
return $data;
}
function aeva_file_get_contents($url)
{
if (!function_exists('curl_init') || !$c = curl_init())
return false;
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_TIMEOUT, 3);
$contents = curl_exec($c);
if (!$contents)