Completed
Push — blocksym ( 35aa32 )
by Richard
07:51
created

XoopsBlock::content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Kernel\Handlers;
13
14
use Xoops\Core\Kernel\Dtype;
15
use Xoops\Core\Kernel\XoopsObject;
16
use \Xoops\Core\Text\Sanitizer;
17
18
/**
19
 * XoopsBlock
20
 *
21
 * @category  Xoops\Core\Kernel\Handlers\XoopsBlock
22
 * @package   Xoops\Core\Kernel
23
 * @author    Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
24
 * @author    Gregory Mage (AKA Mage)
25
 * @author    trabis <[email protected]>
26
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
27
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
28
 * @link      http://xoops.org
29
 */
30
class XoopsBlock extends XoopsObject
31
{
32
    const CUSTOM_HTML   = 'H'; // custom HTML block
33
    const CUSTOM_PHP    = 'P'; // custom PHP block
34
    const CUSTOM_SMILIE = 'S'; // use text sanitizer (smilies enabled)
35
    const CUSTOM_TEXT   = 'T'; // use text sanitizer (smilies disabled)
36
37
    const BLOCK_TYPE_SYSTEM = 'S'; // S - generated by system module
38
    const BLOCK_TYPE_MODULE = 'M'; // M - generated by a non-system module
39
    const BLOCK_TYPE_CUSTOM = 'C'; // C - Custom block
40
    const BLOCK_TYPE_CLONED = 'D'; // D - cloned system/module block
41
    // E - cloned custom block, DON'T use it
42
43
    /**
44
     * Constructor
45
     *
46
     * @param int|array $id object id
47
     */
48 34
    public function __construct($id = null)
49
    {
50 34
        $this->initVar('bid', Dtype::TYPE_INTEGER, null, false);
51 34
        $this->initVar('mid', Dtype::TYPE_INTEGER, 0, false);
52 34
        $this->initVar('func_num', Dtype::TYPE_INTEGER, 0, false);
53 34
        $this->initVar('options', Dtype::TYPE_TEXT_BOX, null, false, 255);
54 34
        $this->initVar('name', Dtype::TYPE_TEXT_BOX, null, true, 150);
55
        //$this->initVar('position', Dtype::TYPE_INTEGER, 0, false);
56 34
        $this->initVar('title', Dtype::TYPE_TEXT_BOX, null, false, 150);
57 34
        $this->initVar('content', Dtype::TYPE_TEXT_AREA, null, false);
58 34
        $this->initVar('side', Dtype::TYPE_INTEGER, 0, false);
59 34
        $this->initVar('weight', Dtype::TYPE_INTEGER, 0, false);
60 34
        $this->initVar('visible', Dtype::TYPE_INTEGER, 0, false);
61 34
        $this->initVar('block_type', Dtype::TYPE_OTHER, null, false);
62 34
        $this->initVar('c_type', Dtype::TYPE_OTHER, null, false);
63 34
        $this->initVar('isactive', Dtype::TYPE_INTEGER, null, false);
64 34
        $this->initVar('dirname', Dtype::TYPE_TEXT_BOX, null, false, 50);
65 34
        $this->initVar('func_file', Dtype::TYPE_TEXT_BOX, null, false, 50);
66 34
        $this->initVar('show_func', Dtype::TYPE_TEXT_BOX, null, false, 50);
67 34
        $this->initVar('edit_func', Dtype::TYPE_TEXT_BOX, null, false, 50);
68 34
        $this->initVar('template', Dtype::TYPE_OTHER, null, false);
69 34
        $this->initVar('bcachetime', Dtype::TYPE_INTEGER, 0, false);
70 34
        $this->initVar('last_modified', Dtype::TYPE_INTEGER, 0, false);
71
72 34
        $xoops = \Xoops::getInstance();
73
74
        // for backward compatibility
75 34
        if (isset($id)) {
76 6
            if (is_array($id)) {
77 5
                $this->assignVars($id);
78
            } else {
79 1
                $blkhandler = $xoops->getHandlerBlock();
80 1
                $obj = $blkhandler->get($id);
81 1
                foreach (array_keys($obj->getVars()) as $i) {
82 1
                    $this->assignVar($i, $obj->getVar($i, 'n'));
83
                }
84
            }
85
        }
86 34
        $this->xoops_url = \XoopsBaseConfig::get('url');
0 ignored issues
show
Bug introduced by
The property xoops_url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
87 34
        $this->xoops_root_path = \XoopsBaseConfig::get('root-path');
0 ignored issues
show
Bug introduced by
The property xoops_root_path does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
88 34
    }
89
90
91
    /**
92
     * getter
93
     *
94
     * @param string $format Dtype::FORMAT_xxxx constant
95
     *
96
     * @return mixed
97
     */
98 1
    public function id($format = 'n')
99
    {
100 1
        return $this->getVar('bid', $format);
101
    }
102
103
    /**
104
     * getter
105
     *
106
     * @param string $format Dtype::FORMAT_xxxx constant
107
     *
108
     * @return mixed
109
     */
110 2
    public function bid($format = '')
111
    {
112 2
        return $this->getVar('bid', $format);
113
    }
114
115
    /**
116
     * getter
117
     *
118
     * @param string $format Dtype::FORMAT_xxxx constant
119
     *
120
     * @return mixed
121
     */
122 1
    public function mid($format = '')
123
    {
124 1
        return $this->getVar('mid', $format);
125
    }
126
127
    /**
128
     * getter
129
     *
130
     * @param string $format Dtype::FORMAT_xxxx constant
131
     *
132
     * @return mixed
133
     */
134 1
    public function func_num($format = '')
135
    {
136 1
        return $this->getVar('func_num', $format);
137
    }
138
139
    /**
140
     * getter
141
     *
142
     * @param string $format Dtype::FORMAT_xxxx constant
143
     *
144
     * @return mixed
145
     */
146 1
    public function options($format = '')
147
    {
148 1
        return $this->getVar('options', $format);
149
    }
150
151
    /**
152
     * getter
153
     *
154
     * @param string $format Dtype::FORMAT_xxxx constant
155
     *
156
     * @return mixed
157
     */
158 1
    public function name($format = '')
159
    {
160 1
        return $this->getVar('name', $format);
161
    }
162
163
    /**
164
     * getter
165
     *
166
     * @param string $format Dtype::FORMAT_xxxx constant
167
     *
168
     * @return mixed
169
     */
170 1
    public function title($format = '')
171
    {
172 1
        return $this->getVar('title', $format);
173
    }
174
175
    /**
176
     * getter
177
     *
178
     * @param string $format Dtype::FORMAT_xxxx constant
179
     *
180
     * @return mixed
181
     */
182 1
    public function content($format = '')
183
    {
184 1
        return $this->getVar('content', $format);
185
    }
186
187
    /**
188
     * getter
189
     *
190
     * @param string $format Dtype::FORMAT_xxxx constant
191
     *
192
     * @return mixed
193
     */
194 1
    public function side($format = '')
195
    {
196 1
        return $this->getVar('side', $format);
197
    }
198
199
    /**
200
     * getter
201
     *
202
     * @param string $format Dtype::FORMAT_xxxx constant
203
     *
204
     * @return mixed
205
     */
206 1
    public function weight($format = '')
207
    {
208 1
        return $this->getVar('weight', $format);
209
    }
210
211
    /**
212
     * getter
213
     *
214
     * @param string $format Dtype::FORMAT_xxxx constant
215
     *
216
     * @return mixed
217
     */
218 1
    public function visible($format = '')
219
    {
220 1
        return $this->getVar('visible', $format);
221
    }
222
223
    /**
224
     * getter
225
     *
226
     * @param string $format Dtype::FORMAT_xxxx constant
227
     *
228
     * @return mixed
229
     */
230 1
    public function block_type($format = '')
231
    {
232 1
        return $this->getVar('block_type', $format);
233
    }
234
235
    /**
236
     * getter custom block type XoopsBlock::CUSTOM_xxxx constant
237
     *
238
     * @param string $format Dtype::FORMAT_xxxx constant
239
     *
240
     * @return mixed
241
     */
242 1
    public function c_type($format = '')
243
    {
244 1
        return $this->getVar('c_type', $format);
245
    }
246
247
    /**
248
     * getter
249
     *
250
     * @param string $format Dtype::FORMAT_xxxx constant
251
     *
252
     * @return mixed
253
     */
254 1
    public function isactive($format = '')
255
    {
256 1
        return $this->getVar('isactive', $format);
257
    }
258
259
    /**
260
     * getter
261
     *
262
     * @param string $format Dtype::FORMAT_xxxx constant
263
     *
264
     * @return mixed
265
     */
266 1
    public function dirname($format = '')
267
    {
268 1
        return $this->getVar('dirname', $format);
269
    }
270
271
    /**
272
     * getter
273
     *
274
     * @param string $format Dtype::FORMAT_xxxx constant
275
     *
276
     * @return mixed
277
     */
278 1
    public function func_file($format = '')
279
    {
280 1
        return $this->getVar('func_file', $format);
281
    }
282
283
    /**
284
     * getter
285
     *
286
     * @param string $format Dtype::FORMAT_xxxx constant
287
     *
288
     * @return mixed
289
     */
290 1
    public function show_func($format = '')
291
    {
292 1
        return $this->getVar('show_func', $format);
293
    }
294
295
    /**
296
     * getter
297
     *
298
     * @param string $format Dtype::FORMAT_xxxx constant
299
     *
300
     * @return mixed
301
     */
302 1
    public function edit_func($format = '')
303
    {
304 1
        return $this->getVar('edit_func', $format);
305
    }
306
307
    /**
308
     * getter
309
     *
310
     * @param string $format Dtype::FORMAT_xxxx constant
311
     *
312
     * @return mixed
313
     */
314 1
    public function template($format = '')
315
    {
316 1
        return $this->getVar('template', $format);
317
    }
318
319
    /**
320
     * getter
321
     *
322
     * @param string $format Dtype::FORMAT_xxxx constant
323
     *
324
     * @return mixed
325
     */
326 1
    public function bcachetime($format = '')
327
    {
328 1
        return $this->getVar('bcachetime', $format);
329
    }
330
331
    /**
332
     * getter
333
     *
334
     * @param string $format Dtype::FORMAT_xxxx constant
335
     *
336
     * @return mixed
337
     */
338 1
    public function last_modified($format = '')
339
    {
340 1
        return $this->getVar('last_modified', $format);
341
    }
342
343
    /**
344
     * return the content of the block for output
345
     *
346
     * @param string $format Dtype::FORMAT_xxxx constant
347
     * @param string $c_type type of custom content, a XoopsBlock::CUSTOM_xxxx constant
348
     *                            H : custom HTML block
349
     *                            P : custom PHP block
350
     *                            S : use text sanitizer (smilies enabled)
351
     *                            T : use text sanitizer (smilies disabled)
352
     *
353
     * @return string content for output
354
     */
355
    public function getContent($format = 's', $c_type = 'T')
356
    {
357
        $format = strtolower($format);
358
        $c_type = strtoupper($c_type);
359
        switch ($format) {
360
            case Dtype::FORMAT_SHOW:
361
            case 's':
362
                // apply c_type rules for content display
363
                $content = $this->getVar('content', Dtype::FORMAT_NONE);
364
                switch ($c_type) {
365
                    case XoopsBlock::CUSTOM_HTML:
366
                        return $this->convertSiteURL($content);
367
                    case XoopsBlock::CUSTOM_PHP:
368
                        ob_start();
369
                        echo eval($content);
370
                        $content = ob_get_contents();
371
                        ob_end_clean();
372
                        return $this->convertSiteURL($content);
373
                    case XoopsBlock::CUSTOM_SMILIE:
374
                        $myts = Sanitizer::getInstance();
375
                        return $myts->filterForDisplay($this->convertSiteURL($content), 1, 1);
376
                    case XoopsBlock::CUSTOM_TEXT:
377
                    default:
378
                        $myts = Sanitizer::getInstance();
379
                        return $myts->filterForDisplay($this->convertSiteURL($content), 1, 0);
380
                }
381
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
382
            case Dtype::FORMAT_EDIT:
383
            case 'e':
384
                return $this->getVar('content', Dtype::FORMAT_EDIT);
385
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
386
            default:
387
                return $this->getVar('content', Dtype::FORMAT_NONE);
388
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
389
        }
390
    }
391
392
    /**
393
     * Convert {X_SITEURL} to actual site URL in content
394
     *
395
     * @param string $content content to convert
396
     *
397
     * @return string
398
     */
399
    protected function convertSiteURL($content)
400
    {
401
        $content = str_replace('{X_SITEURL}', \Xoops::getInstance()->url('/'), $content);
402
        return $content;
403
    }
404
405
    /**
406
     * (HTML-) form for setting the options of the block
407
     *
408
     * @return string|false HTML for the form, FALSE if not defined for this block
409
     */
410 1
    public function getOptions()
411
    {
412 1
        $xoops = \Xoops::getInstance();
413 1
        if (!$this->isCustom()) {
414 1
            $edit_func = (string)$this->getVar('edit_func');
415 1
            if (!$edit_func) {
416 1
                return false;
417
            }
418 1
            if (\XoopsLoad::fileExists(
419 1
                $this->xoops_root_path . '/modules/' . $this->getVar('dirname') . '/blocks/'
420 1
                . $this->getVar('func_file')
421
            )) {
422 1
                $xoops->loadLanguage('blocks', $this->getVar('dirname'));
423 1
                include_once $this->xoops_root_path . '/modules/' . $this->getVar('dirname') . '/blocks/'
424 1
                    . $this->getVar('func_file');
425 1 View Code Duplication
                if (function_exists($edit_func)) {
426
                    // execute the function
427 1
                    $options = explode('|', $this->getVar('options'));
428 1
                    $edit_form = $edit_func($options);
429 1
                    if (!$edit_form) {
430 1
                        return false;
431
                    }
432
                } else {
433 1
                    return false;
434
                }
435 1
                return $edit_form;
436
            } else {
437 1
                return false;
438
            }
439
        } else {
440
            return false;
441
        }
442
    }
443
444
    /**
445
     * Determine if this is a custom block
446
     *
447
     * @return bool true if this is a custom block
448
     */
449 5
    public function isCustom()
450
    {
451 5
        return $this->getVar("block_type") === XoopsBlock::BLOCK_TYPE_CUSTOM;
452
    }
453
454
    /************ADDED**************/
455
456
457
    /**
458
     * Build Block
459
     *
460
     * @return array|bool
461
     */
462 2
    public function buildBlock()
463
    {
464 2
        $xoops = \Xoops::getInstance();
465 2
        $block = array();
466 2
        if (!$this->isCustom()) {
467
            // get block display function
468 2
            $show_func = (string)$this->getVar('show_func');
469 2
            if (!$show_func) {
470
                return false;
471
            }
472 2
            if (!\XoopsLoad::fileExists(
473 2
                $func_file = $xoops->path(
474 2
                    'modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file')
475
                )
476
            )) {
477
                return false;
478
            }
479
            // must get lang files b4 including the file
480
            // some modules require it for code that is outside the function
481 2
            $xoops->loadLanguage('blocks', $this->getVar('dirname'));
482 2
            $xoops->loadLocale($this->getVar('dirname'));
483 2
            include_once $func_file;
484
485 2 View Code Duplication
            if (function_exists($show_func)) {
486
                // execute the function
487 2
                $options = explode('|', $this->getVar('options'));
488 2
                $block = $show_func($options);
489 2
                if (!$block) {
490 2
                    return false;
491
                }
492
            } else {
493 2
                return false;
494
            }
495
        } else {
496
            // it is a custom block, so just return the contents
497
            $block['content'] = $this->getContent('s', $this->getVar('c_type'));
498
            if (empty($block['content'])) {
499
                return false;
500
            }
501
        }
502 2
        return $block;
503
    }
504
}
505