-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvideo_embed_field.handlers.inc
750 lines (679 loc) · 21.8 KB
/
video_embed_field.handlers.inc
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
<?php
/**
* @file
* Provide some handlers for video embed field
* Other modules can implement the hook_video_embed_handler_info to provide more
* handlers.
*/
/**
* Implements hook_video_embed_handler_info().
*/
function video_embed_field_video_embed_handler_info() {
$handlers = array();
$handlers['youtube'] = array(
'title' => 'Youtube',
'function' => 'video_embed_field_handle_youtube',
'thumbnail_function' => 'video_embed_field_handle_youtube_thumbnail',
'thumbnail_default' => backdrop_get_path('module', 'video_embed_field') . '/img/youtube.jpg',
'data_function' => 'video_embed_field_handle_youtube_data',
'form' => 'video_embed_field_handler_youtube_form',
'form_validate' => 'video_embed_field_handler_youtube_form_validate',
'domains' => array(
'youtube.com',
'youtu.be',
'm.youtube.com',
'youtube-nocookie.com',
),
'defaults' => array(
'width' => 640,
'height' => 360,
'autoplay' => 0,
'vq' => 'large',
'rel' => 0,
'controls' => 1,
'autohide' => 2,
'showinfo' => 1,
'modestbranding' => 0,
'theme' => 'dark',
'iv_load_policy' => 1,
'class' => '',
'privacy' => 1,
),
);
$handlers['vimeo'] = array(
'title' => 'Vimeo',
'function' => 'video_embed_field_handle_vimeo',
'thumbnail_function' => 'video_embed_field_handle_vimeo_thumbnail',
'thumbnail_default' => backdrop_get_path('module', 'video_embed_field') . '/img/vimeo.jpg',
'data_function' => '_video_embed_field_get_vimeo_data',
'form' => 'video_embed_field_handler_vimeo_form',
'form_validate' => 'video_embed_field_handler_vimeo_form_validate',
'domains' => array(
'vimeo.com',
),
'defaults' => array(
'width' => 640,
'height' => 360,
'color' => '00adef',
'portrait' => 1,
'title' => 1,
'byline' => 1,
'autoplay' => 0,
'loop' => 0,
'froogaloop' => 0,
'background' => 0,
'class' => '',
),
);
return $handlers;
}
/**
* Helper function to get the youtube video's id.
*
* @param string $url
* The video URL.
*
* @return string|bool
* The video ID, or FALSE in case the ID can't be retrieved from the URL.
*/
function _video_embed_field_get_youtube_id($url) {
// Find the ID of the video they want to play from the url.
if (stristr($url, 'http://')) {
$url = substr($url, 7);
}
elseif (stristr($url, 'https://')) {
$url = substr($url, 8);
}
if (stristr($url, 'playlist')) {
// Playlists need the appended ampersand to take the options properly.
$url = $url . '&';
$pos = strripos($url, '?list=');
if ($pos !== FALSE) {
$pos2 = stripos($url, '&');
$pos2++;
}
else {
return FALSE;
}
}
// Alternate playlist link.
elseif (stristr($url, 'view_play_list')) {
$url = $url . '&';
// All playlist ID's are prepended with PL.
if (!stristr($url, '?p=PL')) {
$url = substr_replace($url, 'PL', strpos($url, '?p=') + 3, 0);
}
// Replace the links format with the embed format.
$url = str_ireplace('play_list?p=', 'videoseries?list=', $url);
$pos = strripos($url, 'videoseries?list=');
if ($pos !== FALSE) {
$pos2 = stripos($url, '&');
$pos2++;
}
else {
return FALSE;
}
}
else {
$pos = strripos($url, 'v=');
if ($pos !== FALSE) {
$pos += 2;
$pos2 = stripos($url, '&', $pos);
$pos_hash = stripos($url, '#', $pos);
$pos2 = _video_embed_get_min($pos2, $pos_hash);
}
else {
$pos = strripos($url, '/');
if ($pos !== FALSE) {
$pos++;
$pos2 = stripos($url, '?', $pos);
$pos_hash = stripos($url, '#', $pos);
$pos2 = _video_embed_get_min($pos2, $pos_hash);
}
}
}
if ($pos === FALSE) {
return FALSE;
}
else {
if ($pos2 > 0) {
$id = substr($url, $pos, $pos2 - $pos);
}
else {
$id = substr($url, $pos);
}
}
return check_plain($id);
}
/**
* Handler for Youtube videos.
*
* @param string $url
* The video URL.
* @param array $settings
* The settings array.
*
* @return array
* The video iframe render array.
*/
function video_embed_field_handle_youtube($url, $settings) {
$output = array();
if (preg_match('/#t=((?P<min>\d+)m)?((?P<sec>\d+)s)?((?P<tinsec>\d+))?/', $url, $matches)) {
if (isset($matches['tinsec'])) {
$settings['start'] = $matches['tinsec']; // Url already in form #t=125 for 2 minutes and 5 seconds.
} else {
// Url in form #t=2m5s or with other useless data, this is why we still keep adding the default data..
// give it some default data in case there is no #t=...
$matches += array(
"min" => 0,
"sec" => 0,
);
if ($time = ($matches["min"] * 60) + $matches["sec"]) {
$settings['start'] = $time;
}
}
}
$id = _video_embed_field_get_youtube_id($url);
if (!$id) {
// We can't decode the URL - just return the URL as a link.
$output['#markup'] = l($url, $url);
return $output;
}
// Add class to variable to avoid adding it to URL param string.
$class = isset($settings['class']) ? $settings['class'] : '';
unset($settings['class']);
// Construct the embed code.
$settings['wmode'] = 'opaque';
$settings_str = urlencode(_video_embed_code_get_settings_str($settings));
$data = video_embed_field_handle_youtube_data($url);
$title = !empty($data) ? ' title="' . t('YouTube video') . ': ' . check_plain($data[0]['snippet']['title']) . '"' : ' title="' . t('YouTube video') . '"';
// Check if privacy mode is enabled.
$youtube_domain = (!empty($settings['privacy'])) ? 'www.youtube-nocookie.com' : 'www.youtube.com';
$output['#markup'] = '<iframe' . $title . ' class="' . check_plain($class) . '" width="' . check_plain($settings['width']) . '" height="' . check_plain($settings['height']) . '" src="//' . $youtube_domain . '/embed/' . $id . '?' . $settings_str . '" frameborder="0" allowfullscreen></iframe>';
return $output;
}
/**
* Gets the thumbnail url for youtube videos.
*
* @param string $url
* The video URL.
*
* @return array
* The video thumbnail information.
*/
function video_embed_field_handle_youtube_thumbnail($url) {
$info = array();
$id = _video_embed_field_get_youtube_id($url);
// Playlist.
if (stristr($id, '?list=')) {
// Strip out all but the ID, including the PL behind the ID.
$start = strpos($id, '?list=PL') + 8;
$length = strpos($id, '&') - $start;
$id = substr($id, $start, $length);
$info['id'] = $id;
// Playlist info is stored in XML. The thumbnail is in there.
$xml = backdrop_http_request('https://gdata.youtube.com/feeds/api/playlists/' . $id);
if (!isset($xml->error)) {
$xml = new SimpleXMLElement($xml->data);
$media = $xml->children('https://search.yahoo.com/mrss/');
if ($media->group->thumbnail && $media->group->thumbnail[0]->attributes()) {
$attrs = $media->group->thumbnail[0]->attributes();
$info['url'] = (string) $attrs['url'];
}
}
}
// Regular video.
elseif ($id) {
$info['id'] = $id;
$info['url'] = 'https://img.youtube.com/vi/' . $id . '/0.jpg';
}
return $info;
}
/**
* Gets video data for a YouTube video URL.
*
* @param string $url
* A YouTube video URL to get data for.
*
* @return array|bool
* An array of video data, or FALSE if unable to fetch data
*/
function video_embed_field_handle_youtube_data($url) {
// Get YouTube video ID from URL.
$id = _video_embed_field_get_youtube_id($url);
if ($id) {
$options['v'] = 3;
$options['key'] = config_get('video_embed_field.settings', 'youtube_v3_api_key');
$options['part'] = 'snippet';
$options['id'] = $id;
$response = backdrop_http_request(url('https://www.googleapis.com/youtube/v3/videos', array('query' => $options)));
if (!isset($response->error)) {
$data = json_decode($response->data);
return _video_embed_field_clean_up_youtube_data($data->items);
}
}
return FALSE;
}
/**
* Flattens out some unnecessary nesting in the youtube data.
*
* @param array $data
* The unflattened data.
*
* @return array
* The flattened data.
*/
function _video_embed_field_clean_up_youtube_data($data) {
// Make things a bit nicer for people trying to use the data.
foreach ($data as $key => $value) {
if (is_object($value)) {
$temp = (array) $value;
if (isset($temp['$t'])) {
$data[$key] = $temp['$t'];
}
else {
$data[$key] = _video_embed_field_clean_up_youtube_data($temp);
}
}
elseif (is_array($value)) {
$data[$key] = _video_embed_field_clean_up_youtube_data($value);
}
if ($key === 'category') {
$terms = array();
foreach ($data[$key] as $value) {
if (isset($value['scheme']) && $value['scheme'] == 'http://schemas.google.com/g/2005#kind') {
continue;
}
if (isset($value['term'])) {
$terms[] = $value['term'];
}
}
$data['terms'] = $terms;
}
}
return $data;
}
/**
* Defines the form elements for the Youtube configuration form.
*
* @param array $defaults
* The form default values.
*
* @return array
* The provider settings form array.
*/
function video_embed_field_handler_youtube_form($defaults) {
$form = array();
$form['width'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Width'),
'#description' => t('The width of the youtube player.'),
'#default_value' => $defaults['width'],
);
$form['height'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Height'),
'#description' => t('The height of the youtube player.'),
'#default_value' => $defaults['height'],
);
$form['theme'] = array(
'#type' => 'select',
'#options' => array(
'dark' => t('Dark'),
'light' => t('Light'),
),
'#title' => t('Player theme'),
'#default_value' => $defaults['theme'],
);
$form['autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Autoplay'),
'#description' => t('Play the video immediately.'),
'#default_value' => $defaults['autoplay'],
);
$form['vq'] = array(
'#type' => 'select',
'#title' => t('Video quality'),
'#options' => array(
'small' => t('Small (240p)'),
'medium' => t('Medium (360p)'),
'large' => t('Large (480p)'),
'hd720' => t('HD 720p'),
'hd1080' => t('HD 10800p'),
),
'#default_value' => $defaults['vq'],
'#description' => t('Attempt to play the video in certain quality if available.'),
);
$form['rel'] = array(
'#type' => 'checkbox',
'#title' => t('Show related videos'),
'#description' => t('Show related videos after the video is finished playing.'),
'#default_value' => $defaults['rel'],
);
$form['showinfo'] = array(
'#type' => 'checkbox',
'#title' => t('Show info'),
'#description' => t('Display information like the video title and rating before the video starts playing.'),
'#default_value' => $defaults['showinfo'],
);
$form['modestbranding'] = array(
'#type' => 'checkbox',
'#title' => t('Hide Youtube logo'),
'#description' => t('Hide the Youtube logo button on the player'),
'#default_value' => $defaults['modestbranding'],
);
$form['iv_load_policy'] = array(
'#type' => 'radios',
'#options' => array(
1 => t('Show video annotations.'),
3 => t('Hide video annotations.'),
),
'#title' => t('Display annotations'),
'#description' => t('Controls the display of annotations over the video content. Only works when using the flash player.'),
'#default_value' => $defaults['iv_load_policy'],
);
$form['controls'] = array(
'#type' => 'radios',
'#options' => array(
0 => t('Hide video controls.'),
1 => t('Show video controls. Youtube default.'),
2 => t('Show video controls with performance improvement for iframe embeds.'),
),
'#title' => t('Display Youtube player controls'),
'#description' => t('This parameter indicates whether the video player controls will display.'),
'#default_value' => $defaults['controls'],
);
$form['autohide'] = array(
'#type' => 'radios',
'#options' => array(
0 => t('The video progress bar and player controls will be visible throughout the video.'),
1 => t('Automatically slide the video progress bar and the player controls out of view a couple of seconds after the video starts playing. They will only reappear if the user moves her mouse over the video player or presses a keyboard key.'),
2 => t('The video progress bar will fade out but the player controls (play button, volume control, etc.) remain visible.'),
),
'#title' => t('Autohide progress bar and the player controls'),
'#description' => t('Controls the autohide behavior of the youtube player controls.'),
'#default_value' => $defaults['autohide'],
);
$form['class'] = array(
'#type' => 'textfield',
'#title' => t('Player CSS class'),
'#description' => t('CSS class to add to the player'),
'#default_value' => $defaults['class'],
);
$form['privacy'] = array(
'#type' => 'checkbox',
'#title' => t('Privacy-enhanced mode'),
'#description' => t('Enable privacy-enhanced mode so cookies won\'t be set until after a video is started.'),
'#default_value' => $defaults['privacy'],
);
return $form;
}
/**
* Validates the form elements for the Youtube configuration form.
*
* @param array $element
* The form element to validate.
* @param array $form_state
* The form to validate state.
* @param array $form
* The form to validate structure.
*/
function video_embed_field_handler_youtube_form_validate($element, &$form_state, $form) {
video_embed_field_validate_dimensions($element);
}
/**
* Helper function to get the Vimeo video's data attributes.
*
* @param string $url
* A Vimeo video URL to get the data from.
*
* @return array|false
* An array containing "video_id" and "thumbnail_url", or FALSE if unable to
* get the video data.
*/
function _video_embed_field_get_vimeo_data($url) {
$return = array();
$access_token = config_get('video_embed_field.settings', 'vimeo_access_token');
// If we have an access token, search for the video via the API.
if (!empty($access_token)) {
$response = backdrop_http_request('https://api.vimeo.com/videos?links=' . rawurlencode($url), array(
'headers' => array(
'Authorization' => 'bearer ' . $access_token,
),
));
// Strip the video ID from the URI and find the largest thumbnail.
if (!empty($data['data'][0])) {
$return['video_id'] = str_replace('/videos/', '', $data['data'][0]['uri']);
$largest = 0;
foreach ($data['data'][0]['pictures']['sizes'] as $picture) {
if (($picture['width'] * $picture['height']) > $largest) {
$largest = $picture['width'] * $picture['height'];
$return['thumbnail_url'] = $picture['link'];
}
}
}
}
// If we don't have an access token, get video details via oEmbed.
else {
$response = backdrop_http_request('https://vimeo.com/api/oembed.json?url=' . rawurlencode($url), array(
'headers' => array(
'Referer' => $_SERVER['HTTP_REFERER'],
),
));
try {
$data = json_decode($response->data, TRUE);
}
catch (Exception $e) {
return FALSE;
}
$return['video_id'] = !empty($data['video_id']) ? $data['video_id'] : FALSE;
$return['thumbnail_url'] = !empty($data['thumbnail_url']) ? $data['thumbnail_url'] : '';
}
return !empty($return['video_id']) ? $return : FALSE;
}
/**
* Helper function to get the Vimeo video's data attributes.
*
* @param string $url
* A Vimeo video URL to get the ID of.
*
* @return integer|false
* The video ID, or FALSE if unable to get the video ID.
*/
function _video_embed_field_get_vimeo_id($vimeo_data) {
try {
$video_id = check_plain($vimeo_data['video_id']);
} catch (Exception $e) {
$video_id = FALSE;
}
return $video_id;
}
/**
* Handler for Vimeo videos.
*
* @param string $url
* The video URL.
* @param array $settings
* The settings array.
*
* @return string
* The video iframe.
*/
function video_embed_field_handle_vimeo($url, $settings) {
$vimeo_data = _video_embed_field_get_vimeo_data($url);
// Get ID of video from URL.
$id = _video_embed_field_get_vimeo_id($vimeo_data);
if (empty($id)) {
return array(
'#markup' => l($url, $url),
);
}
// Construct the embed code.
$settings['player_id'] = backdrop_html_id('vimeo-' . $id);
if (!empty($settings['froogaloop'])) {
$settings['api'] = 1;
}
unset($settings['froogaloop']);
// Add class to variable to avoid adding it to URL param string.
$class = isset($settings['class']) ? $settings['class'] : '';
unset($settings['class']);
$settings_str = _video_embed_code_get_settings_str($settings);
$title = !empty($vimeo_data['title']) ? ' title="' . t('Vimeo video') . ': ' . check_plain($vimeo_data['title']) . '"' : ' title="' . t('Vimeo video') . '"';
return array(
'#markup' => '<iframe' . $title . ' class="' . check_plain($class) . '" id="' . $settings['player_id'] . '" width="' . check_plain($settings['width']) . '" height="' . check_plain($settings['height']) . '" src="//player.vimeo.com/video/' . $id .
'?' . $settings_str . '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen></iframe>',
);
}
/**
* Gets the thumbnail url for vimeo videos.
*
* @param string $url
* The video URL.
*
* @return array
* The video thumbnail information.
*/
function video_embed_field_handle_vimeo_thumbnail($url) {
$vimeo_data = _video_embed_field_get_vimeo_data($url);
// Get ID of video from URL.
$id = _video_embed_field_get_vimeo_id($vimeo_data);
$info = array(
'id' => $id,
);
try {
$info['url'] = (!empty($vimeo_data['thumbnail_url'])) ? $vimeo_data['thumbnail_url'] : '';
} catch (Exception $e) {
}
return $info;
}
/**
* Defines the form elements for the Vimeo configuration form.
*
* @param array $defaults
* The form default values.
*
* @return array
* The provider settings form array.
*/
function video_embed_field_handler_vimeo_form($defaults) {
$form = array();
$form['width'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Width'),
'#description' => t('The width of the vimeo player.'),
'#default_value' => $defaults['width'],
);
$form['height'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Height'),
'#description' => t('The height of the vimeo player.'),
'#default_value' => $defaults['height'],
);
$form['color'] = array(
'#type' => 'select',
'#options' => array(
'00adef' => t('Blue'),
'ff9933' => t('Orange'),
'c9ff23' => t('Lime'),
'ff0179' => t('Fuschia'),
'ffffff' => t('White'),
),
'#title' => t('Player Color'),
'#description' => t('The color to use on the vimeo player.'),
'#default_value' => $defaults['color'],
);
$form['portrait'] = array(
'#type' => 'checkbox',
'#title' => t('Overlay Author Thumbnail'),
'#description' => t("Overlay the author's thumbnail before the video is played."),
'#default_value' => $defaults['portrait'],
);
$form['title'] = array(
'#type' => 'checkbox',
'#title' => t("Overlay Video's Title"),
'#description' => t("Overlay the video's title before the video is played."),
'#default_value' => $defaults['title'],
);
$form['byline'] = array(
'#type' => 'checkbox',
'#title' => t("Overlay Video's Byline"),
'#description' => t("Overlay the video's description before the video is played."),
'#default_value' => $defaults['byline'],
);
$form['overridable'] = array(
'#prefix' => '<p class="note"><strong>' . t('Note') . ': </strong><em>',
'#markup' => t('Color, portrait, title and byline can be restricted by Vimeo Plus videos.
Such videos will ignore these settings.'),
'#suffix' => '</em></p>',
);
$form['autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Autoplay'),
'#description' => t('Play the video immediately.'),
'#default_value' => $defaults['autoplay'],
);
$form['loop'] = array(
'#type' => 'checkbox',
'#title' => t('Loop'),
'#description' => t("Loop the video's playback"),
'#default_value' => $defaults['loop'],
);
$form['froogaloop'] = array(
'#type' => 'checkbox',
'#title' => t('Enable froogaloop support'),
'#description' => t("Enables Froogallop Vimeo's library support"),
'#default_value' => $defaults['loop'],
);
$form['background'] = array(
'#type' => 'checkbox',
'#title' => t('Remove the play/pause button when embedding background videos'),
'#description' => t('Functionality only works for plus or pro members. Works by appending background=1 parameter to the end of the player URL in your embed code.'),
'#default_value' => $defaults['background'],
);
$form['class'] = array(
'#type' => 'textfield',
'#title' => t('Player CSS class'),
'#description' => t('CSS class to add to the player'),
'#default_value' => $defaults['class'],
);
return $form;
}
/**
* Validates the form elements for the Vimeo configuration form.
*
* @param array $element
* The form element to validate.
* @param array $form_state
* The form to validate state.
* @param array $form
* The form to validate structure.
*/
function video_embed_field_handler_vimeo_form_validate($element, &$form_state, $form) {
video_embed_field_validate_dimensions($element);
}
/**
* Calculates the min index for use in finding the id of a youtube video.
*
* @param string $pos1
* The first index.
* @param string $pos2
* The second index.
*
* @return string
* The min index.
*/
function _video_embed_get_min($pos1, $pos2) {
if (!$pos1) {
return $pos2;
}
elseif (!$pos2) {
return $pos1;
}
else {
return min($pos1, $pos2);
}
}