Completed
Push — master ( e68bf6...b6c858 )
by Michael
52:25 queued 39:37
created

Embed::enableResponsive()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 12
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Core\Text\Sanitizer\Extensions;
13
14
use Xoops\Core\Text\Sanitizer;
15
use Xoops\Core\Text\Sanitizer\FilterAbstract;
16
17
/**
18
 * TextSanitizer filter - clean up HTML text
19
 *
20
 * @category  Sanitizer
21
 * @package   Xoops\Core\Text
22
 * @author    Richard Griffith <[email protected]>
23
 * @copyright 2015-2016 XOOPS Project (http://xoops.org)
24
 * @license   GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @link      http://xoops.org
26
 */
27
class Embed extends FilterAbstract
28
{
29
    /**
30
     * @var array default configuration values
31
     */
32
    protected static $defaultConfiguration = [
33
        'enabled' => true,
34
        'cache_time' => 15552000, // 180 days in seconds
35
    ];
36
37
    /**
38
     * Make and URL's in the text clickable links
39
     *
40
     * @param string $text text string to filter
41
     *
42
     * @return mixed
43
     */
44 15
    public function applyFilter($text)
45
    {
46 15
        if (!$this->config['enabled']) {
47
            return $text;
48
        }
49
50 15
        $pattern = '/^(https?:\/\/([\p{L}\p{N}]{1,}\.){1,}[\p{L}\p{N}]{2,}[\/\?\.\-_&=:#\p{L}\p{N}]{0,})$/m';
51
52 15
        $text = preg_replace_callback(
53 15
            $pattern,
54 15
            [$this, 'decorateUrl'],
55
            $text
56
        );
57
58 15
        return $text;
59
    }
60
61
    /**
62
     * decorate a bare url with the help of embed/embed
63
     *
64
     * @param string $match string to be truncated
65
     *
66
     * @return string
67
     */
68
    protected function decorateUrl($match) {
69
        $url = $match[1];
70
        $decorated = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $decorated is dead and can be removed.
Loading history...
71
        $xoops = \Xoops::getInstance();
72
        $md5 = md5($url);
73
        $crc = hash("crc32b", $url);
74
        $key = implode('/', ['embed', substr($crc, -2), $md5]);
75
        //$xoops->cache()->delete($key);
76
        $decorated = $xoops->cache()->cacheRead(
77
            $key,
78
            function ($url) {
79
                $return = null;
80
                try {
81
                    $info = \Embed\Embed::create($url);
82
                } catch (\Exception $e) {
83
                    $info = null;
84
                }
85
                if (is_object($info)) {
86
                    $return = $info->code;
87
                    if (empty($return)) {
88
                        return $this->mediaBox($info->url, $info->image, $info->title, $info->description);
89
                    }
90
                    $height = $info->getHeight();
91
                    $width = $info->getWidth();
92
                    if ($this->enableResponsive($return) && !empty($height) && !empty($width)) {
93
                        $ratio = (1.5 > ($width/$height)) ? '4by3' : '16by9';
94
                        $return = '<div class="embed-responsive embed-responsive-' . $ratio . '">' . $return . '</div>';
95
                    }
96
                }
97
                if (empty($return)) {
98
                    $return = $url;
99
                }
100
                return $return;
101
            },
102
            $this->config['cache_time'],
103
            $url
104
        );
105
        return $decorated;
106
    }
107
108
    protected function mediaBox($link, $imageSrc, $title, $description)
109
    {
110
        $htmlTemplate = <<<'EOT'
111
<div class="media">
112
  <a class="pull-left" href="%1$s" rel="external">
113
    <img src="%2$s" class="media-object" style="max-height: 128px; max-width: 128px;">
114
  </a>
115
  <div class="media-body">
116
    <h4 class="media-heading">%3$s</h4>
117
%4$s
118
  </div>
119
</div>
120
EOT;
121
122
        if(empty($imageSrc)) {
123
            $imageSrc = \Xoops::getInstance()->url('media/xoops/images/icons/link-ext.svg');
124
        }
125
        $box = sprintf($htmlTemplate, $link, $imageSrc, $title, $description);
126
        return $box;
127
    }
128
129
    /**
130
     * Check for know issues if wrapped in embed-responsive div
131
     *
132
     * @param string $code embed code to stest
133
     *
134
     * @return bool true if responsive should be enabled, false otherwise
135
     */
136
    protected function enableResponsive($code)
137
    {
138
        // sites in this list are known to have problems
139
        $excludeList = [
140
            'circuitlab.com',
141
        ];
142
143
        foreach ($excludeList as $test) {
144
            if (false !== stripos($code, $test)) {
145
                return false;
146
            }
147
        }
148
        return true;
149
    }
150
}
151