Completed
Push — master ( 02f2d8...e7d60a )
by Michael
02:40
created

bootstrap-image.php ➔ build_image()   B

Complexity

Conditions 10
Paths 50

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 50
nop 0
dl 0
loc 41
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
include_once('conf/conf.php');
4
5
/* images path for file browser */
6
7
$get_path     = parse_url($_GET['images_path']);
8
$img_dir      = $get_path['path']; // ex : '/dir/img/' with or without slashes
9
$current_path = parse_url($_SERVER['HTTP_REFERER']);
10
$img_path     = ''; // default
11
if (preg_match('`^/`', $img_dir)) { // absolute url sended
12
    $img_path = $img_dir;
13
    if (!preg_match('`/$`', $img_dir)) { // add final slash if needed
14
        $img_path .= '/';
15
    }
16
}
17
$root = rtrim($_SERVER['DOCUMENT_ROOT'], '/'); // remove final slash if needed
18
if (!is_file($root . $img_path . 'secure.php')) {
19
    $security_msg = '<body><div class="container"><p>&nbsp;</p><p class="alert alert-danger">Your images dir is not authorized.</p><p class="text-center">Please create an empty file named <strong><i>secure.php</i></strong> and save it into <strong><i>'
20
                    . $img_path
21
                    . '</i></strong> to allow access.</p></div><body>';
22
}
23
24
if (isset($_GET['imgSrc'])) {
25
    $imgSrc        = preg_replace('/\r|\n/', '', urldecode($_GET['imgSrc']));
26
    $imgAlt        = preg_replace('/\r|\n/', '', urldecode($_GET['imgAlt']));
27
    $imgWidth      = preg_replace('/\r|\n/', '', urldecode($_GET['imgWidth']));
28
    $imgHeight     = preg_replace('/\r|\n/', '', urldecode($_GET['imgHeight']));
29
    $imgStyle      = preg_replace('/\r|\n/', '', urldecode($_GET['imgStyle']));
30
    $imgResponsive = preg_replace('/\r|\n/', '', urldecode($_GET['imgResponsive']));
31
    $imgCode       = build_image();
32
} else {
33
    $imgSrc        = str_replace('bootstrap-image.php', '', $_SERVER['SCRIPT_NAME']) . 'img/tinymce-bootstrap-plugin-small-preview.png';
34
    $imgAlt        = '';
35
    $imgWidth      = '';
36
    $imgHeight     = '';
37
    $imgStyle      = '';
38
    $imgResponsive = 'true';
39
    $imgCode       = build_image();
40
}
41
42
function build_image()
43
{
44
    global $imgSrc;
45
    global $imgAlt;
46
    global $imgWidth;
47
    global $imgHeight;
48
    global $imgStyle;
49
    global $imgResponsive;
50
    if (@getimagesize($imgSrc) || @getimagesize($_SERVER['DOCUMENT_ROOT'] . $imgSrc)) {
51
        $info = new SplFileInfo($imgSrc);
52
        if (preg_match('`jpg|jpeg|png|gif`', strtolower($info))) {
53
            $img = '<img src="' . $imgSrc . '"';
54
            if (!empty($imgWidth)) {
55
                $img .= ' width="' . $imgWidth . '"';
56
            }
57
            if (!empty($imgHeight)) {
58
                $img .= ' height="' . $imgHeight . '"';
59
            }
60
            $imgClass = '';
61
            if (!empty($imgStyle)) {
62
                $imgClass .= $imgStyle;
63
            }
64
            if ('false' !== $imgResponsive) {
65
                if (empty($imgClass)) {
66
                    $imgClass = 'img-responsive';
67
                } else {
68
                    $imgClass .= ' img-responsive';
69
                }
70
            }
71
            if (!empty($imgClass)) {
72
                $img .= ' class="' . $imgClass . '"';
73
            }
74
            $img .= ' alt="' . $imgAlt . '"';
75
            $img .= ' />';
76
77
            return $img;
78
        }
79
    } else {
80
        return false;
81
    }
82
}
83
84
?>
85
<!DOCTYPE html>
86
<html lang="fr">
87
<head>
88
    <link rel="stylesheet" href="<?php echo $bootstrap_css_path; ?>">
89
    <link rel="stylesheet" href="css/plugin.min.css">
90
    <link href='jquery-file-tree/jqueryFileTree.css' rel='stylesheet' type='text/css'>
91
    <link href="<?php echo PRISM_CSS; ?>" type="text/css" rel="stylesheet"/>
92
</head>
93
<?php
94
if (isset($security_msg)) {
95
    exit($security_msg);
96
}
97
?>
98
<body>
99
<div class="container">
100
    <div class="row margin-bottom-md">
101
        <div class="choice-title">
102
            <span><?php echo IMAGE; ?></span>
103
        </div>
104
        <div class="col-sm-6 text-center margin-bottom-md">
105
            <button type="button" class="btn btn-primary" id="browseBtn"><?php echo BROWSE; ?></button>
106
        </div>
107
        <div class="col-sm-6 text-center" id="thumb-div">
108
            <?php echo $imgCode; ?>
109
        </div>
110
    </div>
111
    <div class="row margin-bottom-md">
112
        <div class="choice-title">
113
            <span><?php echo IMAGE_STYLE; ?></span>
114
        </div>
115
        <div class="col-md-12">
116
            <div class="text-center">
117
                <div class="choice selector select-style">
118
                    <img src="<?php echo $imgSrc; ?>" class="" data-attr="none"><br><br>
119
                    <label>none</label>
120
                </div>
121
                <div class="choice selector select-style">
122
                    <img src="<?php echo $imgSrc; ?>" class="img-rounded"><br><br>
123
                    <label>rounded</label>
124
                </div>
125
                <div class="choice selector select-style">
126
                    <img src="<?php echo $imgSrc; ?>" class="img-circle"><br><br>
127
                    <label>circle</label>
128
                </div>
129
                <div class="choice selector select-style">
130
                    <img src="<?php echo $imgSrc; ?>" class="img-thumbnail"><br><br>
131
                    <label>thumbnail</label>
132
                </div>
133
            </div>
134
        </div>
135
    </div>
136
    <div class="row">
137
        <div class="choice-title">
138
            <span><?php echo OTHERS; ?></span>
139
        </div>
140
        <div class="text-center col-xs-10 col-xs-offset-1">
141
            <div class="form-horizontal col-sm-4">
142
                <div class="form-group">
143
                    <label class="col-xs-6 text-right"><?php echo RESPONSIVE; ?> : </label>
144
                    <div class="btn-group btn-toggle col-xs-6" id="responsive">
145
                        <button class="btn btn-sm btn-success active" data-attr="false"><?php echo NO; ?></button>
146
                        <button class="btn btn-sm btn-default" data-attr="true"><?php echo YES; ?></button>
147
                    </div>
148
                </div>
149
            </div>
150
            <div class="form-horizontal col-sm-4">
151
                <div class="form-group">
152
                    <label class="col-xs-6 text-right" id="width-label"><?php echo WIDTH; ?> :</label>
153
                    <div class="input-group col-xs-6">
154
                        <input type="text" class="form-control" name="img-width" id="img-width" value="<?php echo $imgWidth; ?>">
155
                        <span class="input-group-addon">px</span>
156
                    </div>
157
                </div>
158
            </div>
159
            <div class="form-horizontal col-sm-4">
160
                <div class="form-group">
161
                    <label class="col-xs-6 text-right" id="height-label"><?php echo HEIGHT; ?> :</label>
162
                    <div class="input-group col-xs-6">
163
                        <input type="text" class="form-control" name="img-height" id="img-height" value="<?php echo $imgHeight; ?>">
164
                        <span class="input-group-addon">px</span>
165
                    </div>
166
                </div>
167
            </div>
168
            <div class="form-horizontal col-sm-8 col-sm-offset-2">
169
                <div class="form-group">
170
                    <label class="col-sm-4 col-xs-6 text-right" id="alt-label"><?php echo ALT_TEXT; ?> :</label>
171
                    <div class="col-sm-8 col-xs-6">
172
                        <input type="text" class="form-control" name="img-alt" value="<?php echo $imgAlt; ?>">
173
                    </div>
174
                </div>
175
            </div>
176
        </div>
177
    </div>
178
    <div class="row" id="preview">
179
        <div id="preview-title" class="margin-bottom-md">
180
            <span class="btn-primary"><?php echo PREVIEW; ?></span>
181
        </div>
182
        <div class="col-sm-12 margin-bottom-md center-img text-center test-icon-wrapper" id="test-wrapper">
183
            <?php echo $imgCode; ?>
184
        </div>
185
    </div>
186
    <div class="row">
187
        <div id="code-title">
188
            <a href="#" id="code-slide-link">code <i class="glyphicon glyphicon-arrow-up"></i></a>
189
        </div>
190
        <div class="col-sm-12 text-center" id="code-wrapper">
191
                <pre><?php
192
                    echo htmlspecialchars($imgCode, ENT_QUOTES | ENT_HTML5);
193
                    ?></pre>
194
        </div>
195
    </div>
196
</div>
197
<script type="text/javascript" src="<?php echo JQUERY_JS ?>"></script>
198
<script type="text/javascript" src="<?php echo BOOTSTRAP_JS ?>"></script>
199
<script type="text/javascript" src="js/utils.min.js"></script>
200
<script type="text/javascript" src="js/jquery.htmlClean.min.js"></script>
201
<script type="text/javascript" src="jquery-file-tree/jqueryFileTree.js"></script>
202
<script type="text/javascript" src="<?php echo PRISM_JS; ?>"></script>
203
<script type="text/javascript">
204
    var imgSrc = '<?php echo $imgSrc; ?>';
205
    var imgAlt = '<?php echo $imgAlt; ?>';
206
    var imgWidth = '<?php echo $imgWidth; ?>';
207
    var imgHeight = '<?php echo $imgHeight; ?>';
208
    var imgStyle = '<?php echo $imgStyle; ?>';
209
    var imgResponsive = <?php echo $imgResponsive; ?>;
210
    var imgCode = '<?php echo $imgCode; ?>';
211
    var imgPath = '<?php echo $img_path; ?>';
212
    $.noConflict();
213
    jQuery(document).ready(function ($) {
214
215
        makeResponsive();
216
217
        /* file browser */
218
219
        $('#file-tree-wrapper').hide();
220
        $('#browseBtn').on('click', function () {
221
            $('#file-tree-content').fileTree({root: imgPath}, function (file) {
222
                openFile(file);
223
            });
224
            $('#file-tree-wrapper').fadeIn(500);
225
        });
226
        $('#file-tree-wrapper').find('.btn-default').on('click', function () {
227
            $('#file-tree-wrapper').fadeOut(500);
228
        });
229
230
        /* image style */
231
232
        $('.selector.select-style').each(function (event, element) {
233
234
            /* set style on load */
235
236
            if (imgStyle === '') {
237
                if ($(element).find('img').attr('data-attr') == "none") {
238
                    $(element).addClass('active');
239
                }
240
            } else if ($(element).find('img').hasClass(imgStyle)) {
241
                $(element).addClass('active');
242
            }
243
244
            $(element).on('click', function (event) {
245
                $('.selector.select-style').removeClass('active');
246
                imgStyle = $(this).find('img').attr('class');
247
                $(this).addClass('active');
248
                $('#test-wrapper').find('img').removeClass('img-rounded').removeClass('img-circle').removeClass('img-thumbnail').addClass(imgStyle);
249
                updateCode();
250
            });
251
        });
252
253
        $('#responsive').on('click', changeImgResponsive);
254
255
        /* on load */
256
257
        updateCode();
258
259
        function changeImgResponsive() {
260
            var checked = toggleBtn(this);
261
            if (checked) {
262
                imgResponsive = true;
263
                $('#test-wrapper').find('img').addClass('img-responsive');
264
            } else {
265
                imgResponsive = false;
266
                $('#test-wrapper').find('img').removeClass('img-responsive');
267
            }
268
            updateCode();
269
        }
270
271
        /* change image responsive (enabled/disabled) */
272
273
        if (imgResponsive) { // on load
274
            $('#responsive').trigger('click');
275
        }
276
277
        /* image width / height */
278
279
        $('#img-width').on('keyup', function () {
280
            changeImgSize($(this).val(), 'width');
281
        });
282
        $('#img-height').on('keyup', function () {
283
            changeImgSize($(this).val(), 'height');
284
        });
285
286
        /* alt text */
287
288
        $('input[name="img-alt"]').on('keyup', changeImgAlt);
289
290
        function changeImgAlt() {
291
            imgAlt = $('input[name="img-alt"]').prop('value');
292
            $('#test-wrapper').find('img').attr('alt', imgAlt);
293
            updateCode();
294
        }
295
296
        /**
297
         * changeImgSize
298
         * @param  int val
299
         * @param  string widthHeight 'width' or 'height'
300
         * @return void
301
         */
302
        function changeImgSize(val, widthHeight) {
303
            if (val % 1 === 0) { // check if val is an integer
304
                $('#test-wrapper').find('img').attr(widthHeight, val);
305
            } else {
306
                $('#test-wrapper').find('img').removeAttr(widthHeight);
307
            }
308
            updateCode();
309
        }
310
311
        function openFile(file) {
312
            var extension = file.substr((file.lastIndexOf('.') + 1)).toLowerCase();
313
            switch (extension) {
314
                case 'jpg':
315
                case 'jpeg':
316
                case 'png':
317
                case 'gif':
318
                    $('#image-file-preview').html('<img src="' + file + '" alt="">');
319
                    $('#file-tree-wrapper').find('.btn-primary').removeAttr('disabled').on('click', function () {
320
                        imgSrc = file;
321
                        var img = $('<img>');
322
                        img.attr('src', imgSrc).attr('alt', imgAlt);
323
                        if (imgWidth !== '') {
324
                            img.attr('width', imgWidth);
325
                        }
326
                        if (imgHeight !== '') {
327
                            img.attr('width', imgHeight);
328
                        }
329
                        if (imgStyle !== '') {
330
                            img.addClass(imgStyle);
331
                        }
332
                        if (imgResponsive) {
333
                            img.addClass('img-responsive');
334
                        }
335
                        $('#thumb-div').html($('#image-file-preview').html());
336
                        $('#test-wrapper').html(img);
337
                        updateCode();
338
                        $('#file-tree-wrapper').fadeOut(500);
339
                        $('#image-file-preview').find('.btn-primary').attr('disabled');
340
                    });
341
                    break;
342
                default:
343
                    $('#image-file-preview').html('<p class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><?php echo PLEASE_CHOOSE_AN_IMAGE ?></p>');
344
                    $('#image-file-preview').find('.btn-primary').attr('disabled');
345
                    break;
346
            }
347
        }
348
    });
349
</script>
350
<div id="file-tree-wrapper">
351
    <div id="file-tree">
352
        <div id="file-tree-content"></div>
353
        <p></p>
354
        <div class="row">
355
            <div class="choice-title margin-bottom-md">
356
                <span class="btn-primary"><?php echo PREVIEW; ?></span>
357
            </div>
358
            <div class="col-md-12">
359
                <div id="image-file-preview"></div>
360
                <div class="btn-group pull-right">
361
                    <button type="button" class="btn btn-primary" disabled="disabled"><?php echo OK; ?></button>
362
                    <button type="button" class="btn btn-default"><?php echo CANCEL; ?></button>
363
                </div>
364
            </div>
365
        </div>
366
    </div>
367
</div>
368
</body>
369
</html>
370