1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 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: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29:
30: class XoopsBlock extends XoopsObject
31: {
32: const CUSTOM_HTML = 'H';
33: const CUSTOM_PHP = 'P';
34: const CUSTOM_SMILIE = 'S';
35: const CUSTOM_TEXT = 'T';
36:
37: const BLOCK_TYPE_SYSTEM = 'S';
38: const BLOCK_TYPE_MODULE = 'M';
39: const BLOCK_TYPE_CUSTOM = 'C';
40: const BLOCK_TYPE_CLONED = 'D';
41:
42:
43: 44: 45: 46: 47:
48: public function __construct($id = null)
49: {
50: $this->initVar('bid', Dtype::TYPE_INTEGER, null, false);
51: $this->initVar('mid', Dtype::TYPE_INTEGER, 0, false);
52: $this->initVar('func_num', Dtype::TYPE_INTEGER, 0, false);
53: $this->initVar('options', Dtype::TYPE_TEXT_BOX, null, false, 255);
54: $this->initVar('name', Dtype::TYPE_TEXT_BOX, null, true, 150);
55:
56: $this->initVar('title', Dtype::TYPE_TEXT_BOX, null, false, 150);
57: $this->initVar('content', Dtype::TYPE_TEXT_AREA, null, false);
58: $this->initVar('side', Dtype::TYPE_INTEGER, 0, false);
59: $this->initVar('weight', Dtype::TYPE_INTEGER, 0, false);
60: $this->initVar('visible', Dtype::TYPE_INTEGER, 0, false);
61: $this->initVar('block_type', Dtype::TYPE_OTHER, null, false);
62: $this->initVar('c_type', Dtype::TYPE_OTHER, null, false);
63: $this->initVar('isactive', Dtype::TYPE_INTEGER, null, false);
64: $this->initVar('dirname', Dtype::TYPE_TEXT_BOX, null, false, 50);
65: $this->initVar('func_file', Dtype::TYPE_TEXT_BOX, null, false, 50);
66: $this->initVar('show_func', Dtype::TYPE_TEXT_BOX, null, false, 50);
67: $this->initVar('edit_func', Dtype::TYPE_TEXT_BOX, null, false, 50);
68: $this->initVar('template', Dtype::TYPE_OTHER, null, false);
69: $this->initVar('bcachetime', Dtype::TYPE_INTEGER, 0, false);
70: $this->initVar('last_modified', Dtype::TYPE_INTEGER, 0, false);
71:
72: $xoops = \Xoops::getInstance();
73:
74:
75: if (isset($id)) {
76: if (is_array($id)) {
77: $this->assignVars($id);
78: } else {
79: $blockHandler = $xoops->getHandlerBlock();
80: $obj = $blockHandler->get($id);
81: foreach (array_keys($obj->getVars()) as $i) {
82: $this->assignVar($i, $obj->getVar($i, 'n'));
83: }
84: }
85: }
86: }
87:
88:
89: 90: 91: 92: 93: 94: 95:
96: public function id($format = 'n')
97: {
98: return $this->getVar('bid', $format);
99: }
100:
101: 102: 103: 104: 105: 106: 107:
108: public function bid($format = '')
109: {
110: return $this->getVar('bid', $format);
111: }
112:
113: 114: 115: 116: 117: 118: 119:
120: public function mid($format = '')
121: {
122: return $this->getVar('mid', $format);
123: }
124:
125: 126: 127: 128: 129: 130: 131:
132: public function func_num($format = '')
133: {
134: return $this->getVar('func_num', $format);
135: }
136:
137: 138: 139: 140: 141: 142: 143:
144: public function options($format = '')
145: {
146: return $this->getVar('options', $format);
147: }
148:
149: 150: 151: 152: 153: 154: 155:
156: public function name($format = '')
157: {
158: return $this->getVar('name', $format);
159: }
160:
161: 162: 163: 164: 165: 166: 167:
168: public function title($format = '')
169: {
170: return $this->getVar('title', $format);
171: }
172:
173: 174: 175: 176: 177: 178: 179:
180: public function content($format = '')
181: {
182: return $this->getVar('content', $format);
183: }
184:
185: 186: 187: 188: 189: 190: 191:
192: public function side($format = '')
193: {
194: return $this->getVar('side', $format);
195: }
196:
197: 198: 199: 200: 201: 202: 203:
204: public function weight($format = '')
205: {
206: return $this->getVar('weight', $format);
207: }
208:
209: 210: 211: 212: 213: 214: 215:
216: public function visible($format = '')
217: {
218: return $this->getVar('visible', $format);
219: }
220:
221: 222: 223: 224: 225: 226: 227:
228: public function block_type($format = '')
229: {
230: return $this->getVar('block_type', $format);
231: }
232:
233: 234: 235: 236: 237: 238: 239:
240: public function c_type($format = '')
241: {
242: return $this->getVar('c_type', $format);
243: }
244:
245: 246: 247: 248: 249: 250: 251:
252: public function isactive($format = '')
253: {
254: return $this->getVar('isactive', $format);
255: }
256:
257: 258: 259: 260: 261: 262: 263:
264: public function dirname($format = '')
265: {
266: return $this->getVar('dirname', $format);
267: }
268:
269: 270: 271: 272: 273: 274: 275:
276: public function func_file($format = '')
277: {
278: return $this->getVar('func_file', $format);
279: }
280:
281: 282: 283: 284: 285: 286: 287:
288: public function show_func($format = '')
289: {
290: return $this->getVar('show_func', $format);
291: }
292:
293: 294: 295: 296: 297: 298: 299:
300: public function edit_func($format = '')
301: {
302: return $this->getVar('edit_func', $format);
303: }
304:
305: 306: 307: 308: 309: 310: 311:
312: public function template($format = '')
313: {
314: return $this->getVar('template', $format);
315: }
316:
317: 318: 319: 320: 321: 322: 323:
324: public function bcachetime($format = '')
325: {
326: return $this->getVar('bcachetime', $format);
327: }
328:
329: 330: 331: 332: 333: 334: 335:
336: public function last_modified($format = '')
337: {
338: return $this->getVar('last_modified', $format);
339: }
340:
341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352:
353: public function getContent($format = 's', $c_type = 'T')
354: {
355: $format = strtolower($format);
356: $c_type = strtoupper($c_type);
357: switch ($format) {
358: case Dtype::FORMAT_SHOW:
359: case 's':
360:
361: $content = $this->getVar('content', Dtype::FORMAT_NONE);
362: switch ($c_type) {
363: case XoopsBlock::CUSTOM_HTML:
364: return $this->convertSiteURL($content);
365: case XoopsBlock::CUSTOM_PHP:
366: ob_start();
367: echo eval($content);
368: $content = ob_get_contents();
369: ob_end_clean();
370: return $this->convertSiteURL($content);
371: case XoopsBlock::CUSTOM_SMILIE:
372: $myts = Sanitizer::getInstance();
373: return $myts->filterForDisplay($this->convertSiteURL($content), 1, 1);
374: case XoopsBlock::CUSTOM_TEXT:
375: default:
376: $myts = Sanitizer::getInstance();
377: return $myts->filterForDisplay($this->convertSiteURL($content), 1, 0);
378: }
379: break;
380: case Dtype::FORMAT_EDIT:
381: case 'e':
382: return $this->getVar('content', Dtype::FORMAT_EDIT);
383: break;
384: default:
385: return $this->getVar('content', Dtype::FORMAT_NONE);
386: break;
387: }
388: }
389:
390: 391: 392: 393: 394: 395: 396:
397: protected function convertSiteURL($content)
398: {
399: $content = str_replace('{X_SITEURL}', \Xoops::getInstance()->url('/'), $content);
400: return $content;
401: }
402:
403: 404: 405: 406: 407:
408: public function getOptions()
409: {
410: $xoops = \Xoops::getInstance();
411: if (!$this->isCustom()) {
412: $edit_func = (string)$this->getVar('edit_func');
413: if (!$edit_func) {
414: return false;
415: }
416: $funcFile = $xoops->path(
417: 'modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file')
418: );
419: if (\XoopsLoad::fileExists($funcFile)) {
420: $xoops->loadLanguage('blocks', $this->getVar('dirname'));
421: include_once $funcFile;
422: if (function_exists($edit_func)) {
423:
424: $options = explode('|', $this->getVar('options'));
425: $edit_form = $edit_func($options);
426: if (!$edit_form) {
427: return false;
428: }
429: } else {
430: return false;
431: }
432: return $edit_form;
433: } else {
434: return false;
435: }
436: } else {
437: return false;
438: }
439: }
440:
441: 442: 443: 444: 445:
446: public function isCustom()
447: {
448: return $this->getVar("block_type") === XoopsBlock::BLOCK_TYPE_CUSTOM;
449: }
450:
451:
452:
453:
454: 455: 456: 457: 458:
459: public function buildBlock()
460: {
461: $xoops = \Xoops::getInstance();
462: $block = array();
463: if (!$this->isCustom()) {
464:
465: $show_func = (string)$this->getVar('show_func');
466: if (!$show_func) {
467: return false;
468: }
469: if (!\XoopsLoad::fileExists(
470: $func_file = $xoops->path(
471: 'modules/' . $this->getVar('dirname') . '/blocks/' . $this->getVar('func_file')
472: )
473: )) {
474: return false;
475: }
476:
477:
478: $xoops->loadLanguage('blocks', $this->getVar('dirname'));
479: $xoops->loadLocale($this->getVar('dirname'));
480: include_once $func_file;
481:
482: if (function_exists($show_func)) {
483:
484: $options = explode('|', $this->getVar('options'));
485: $block = $show_func($options);
486: if (!$block) {
487: return false;
488: }
489: } else {
490: return false;
491: }
492: } else {
493:
494: $block['content'] = $this->getContent('s', $this->getVar('c_type'));
495: if (empty($block['content'])) {
496: return false;
497: }
498: }
499: return $block;
500: }
501: }
502: