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
|
<?php /** @file */
namespace Zotlabs\Render;
use Zotlabs\Lib\Config;
require_once('include/security.php');
require_once('include/menu.php');
/**
* @brief Comanche Page Description Language.
*
* Comanche is a markup language similar to bbcode with which to create elaborate
* and complex web pages by assembling them from a series of components - some of
* which are pre-built and others which can be defined on the fly. Comanche uses
* a Page Decription Language to create these pages.
*
* Comanche primarily chooses what content will appear in various regions of the
* page. The various regions have names and these names can change depending on
* what layout template you choose.
*/
class Comanche {
function parse($s, $pass = 0) {
$matches = array();
$cnt = preg_match_all("/\[comment\](.*?)\[\/comment\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$s = str_replace($mtch[0], '', $s);
}
}
/*
* This section supports the "switch" statement of the form given by the following
* example. The [default][/default] block must be the last in the arbitrary
* list of cases. The first case that matches the switch variable is used
* and the rest are not evaluated.
*
* [switch observer.language]
* [case de]
* [block]german-content[/block]
* [/case]
* [case es]
* [block]spanish-content[/block]
* [/case]
* [default]
* [block]english-content[/block]
* [/default]
* [/switch]
*/
$cnt = preg_match_all("/\[switch (.*?)\](.*?)\[default\](.*?)\[\/default\]\s*\[\/switch\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$switch_done = 0;
$switch_var = $this->get_condition_var($mtch[1]);
$default = $mtch[3];
$cases = array();
$cntt = preg_match_all("/\[case (.*?)\](.*?)\[\/case\]/ism", $mtch[2], $cases, PREG_SET_ORDER);
if($cntt) {
foreach($cases as $case) {
if($case[1] === $switch_var) {
$switch_done = 1;
$s = str_replace($mtch[0], $case[2], $s);
break;
}
}
if($switch_done === 0) {
$s = str_replace($mtch[0], $default, $s);
}
}
}
}
$cnt = preg_match_all("/\[if (.*?)\](.*?)\[else\](.*?)\[\/if\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
if($this->test_condition($mtch[1])) {
$s = str_replace($mtch[0], $mtch[2], $s);
}
else {
$s = str_replace($mtch[0], $mtch[3], $s);
}
}
}
else {
$cnt = preg_match_all("/\[if (.*?)\](.*?)\[\/if\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
if($this->test_condition($mtch[1])) {
$s = str_replace($mtch[0], $mtch[2], $s);
}
else {
$s = str_replace($mtch[0], '', $s);
}
}
}
}
if($pass == 0)
$this->parse_pass0($s);
else
$this->parse_pass1($s);
}
function parse_pass0($s) {
$matches = null;
$cnt = preg_match("/\[layout\](.*?)\[\/layout\]/ism", $s, $matches);
if($cnt)
\App::$page['template'] = trim($matches[1]);
$cnt = preg_match("/\[template=(.*?)\](.*?)\[\/template\]/ism", $s, $matches);
if($cnt) {
\App::$page['template'] = trim($matches[2]);
\App::$page['template_style'] = trim($matches[2]) . '_' . $matches[1];
}
$cnt = preg_match("/\[template\](.*?)\[\/template\]/ism", $s, $matches);
if($cnt) {
\App::$page['template'] = trim($matches[1]);
}
$cnt = preg_match("/\[theme=(.*?)\](.*?)\[\/theme\]/ism", $s, $matches);
if($cnt) {
\App::$layout['schema'] = trim($matches[1]);
\App::$layout['theme'] = trim($matches[2]);
}
$cnt = preg_match("/\[theme\](.*?)\[\/theme\]/ism", $s, $matches);
if($cnt)
\App::$layout['theme'] = trim($matches[1]);
$cnt = preg_match("/\[navbar\](.*?)\[\/navbar\]/ism", $s, $matches);
if($cnt)
\App::$layout['navbar'] = trim($matches[1]);
$cnt = preg_match_all("/\[webpage\](.*?)\[\/webpage\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
// only the last webpage definition is used if there is more than one
foreach($matches as $mtch) {
\App::$layout['webpage'] = $this->webpage($a,$mtch[1]);
}
}
}
function parse_pass1($s) {
$cnt = preg_match_all("/\[region=(.*?)\](.*?)\[\/region\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
\App::$layout['region_' . $mtch[1]] = $this->region($mtch[2],$mtch[1]);
}
}
}
/**
* @brief Replace conditional variables with real values.
*
* Currently supported condition variables:
* * $config.xxx.yyy - Config::Get with cat = xxx and k = yyy
* * $request - request uri for this page
* * $observer.language - viewer's preferred language (closest match)
* * $observer.address - xchan_addr or false
* * $observer.name - xchan_name or false
* * $observer - xchan_hash of observer or empty string
* * $local_channel - logged in channel_id or false
*
* @param string $v The conditional variable name
* @return string|boolean
*/
function get_condition_var($v) {
if($v) {
$x = explode('.', $v);
if($x[0] == 'config')
return Config::Get($x[1],$x[2]);
elseif($x[0] === 'request')
return $_SERVER['REQUEST_URI'];
elseif($x[0] === 'local_channel') {
return local_channel();
}
elseif($x[0] === 'observer') {
if(count($x) > 1) {
if($x[1] == 'language')
return \App::$language;
$y = \App::get_observer();
if(! $y)
return false;
if($x[1] == 'address')
return $y['xchan_addr'];
elseif($x[1] == 'name')
return $y['xchan_name'];
elseif($x[1] == 'webname')
return substr($y['xchan_addr'],0,strpos($y['xchan_addr'],'@'));
return false;
}
return get_observer_hash();
}
else
return false;
}
return false;
}
/**
* @brief Test for Conditional Execution conditions.
*
* This is extensible. The first version of variable testing supports tests of the forms:
*
* - [if $config.system.foo ~= baz] which will check if Config::Get('system','foo') contains the string 'baz';
* - [if $config.system.foo == baz] which will check if Config::Get('system','foo') is the string 'baz';
* - [if $config.system.foo != baz] which will check if Config::Get('system','foo') is not the string 'baz';
* - [if $config.system.foo >= 3] which will check if Config::Get('system','foo') is greater than or equal to 3;
* - [if $config.system.foo > 3] which will check if Config::Get('system','foo') is greater than 3;
* - [if $config.system.foo <= 3] which will check if Config::Get('system','foo') is less than or equal to 3;
* - [if $config.system.foo < 3] which will check if Config::Get('system','foo') is less than 3;
*
* - [if $config.system.foo {} baz] which will check if 'baz' is an array element in Config::Get('system','foo')
* - [if $config.system.foo {*} baz] which will check if 'baz' is an array key in Config::Get('system','foo')
* - [if $config.system.foo] which will check for a return of a true condition for Config::Get('system','foo');
* - [if !$config.system.foo] which will check for a return of a false condition for Config::Get('system','foo');
*
* The values 0, '', an empty array, and an unset value will all evaluate to false.
*
* @param int|string $s
* @return boolean
*/
function test_condition($s) {
if(preg_match('/[\$](.*?)\s\~\=\s(.*?)$/',$s,$matches)) {
$x = $this->get_condition_var($matches[1]);
if(stripos($x,trim($matches[2])) !== false)
return true;
return false;
}
if(preg_match('/[\$](.*?)\s\=\=\s(.*?)$/',$s,$matches)) {
$x = $this->get_condition_var($matches[1]);
if($x == trim($matches[2]))
return true;
return false;
}
if(preg_match('/[\$](.*?)\s\!\=\s(.*?)$/',$s,$matches)) {
$x = $this->get_condition_var($matches[1]);
if($x != trim($matches[2]))
return true;
return false;
}
if(preg_match('/[\$](.*?)\s\>\=\s(.*?)$/',$s,$matches)) {
$x = $this->get_condition_var($matches[1]);
if($x >= trim($matches[2]))
return true;
return false;
}
if(preg_match('/[\$](.*?)\s\<\=\s(.*?)$/',$s,$matches)) {
$x = $this->get_condition_var($matches[1]);
if($x <= trim($matches[2]))
return true;
return false;
}
if(preg_match('/[\$](.*?)\s\>\s(.*?)$/',$s,$matches)) {
$x = $this->get_condition_var($matches[1]);
if($x > trim($matches[2]))
return true;
return false;
}
if(preg_match('/[\$](.*?)\s\>\s(.*?)$/',$s,$matches)) {
$x = $this->get_condition_var($matches[1]);
if($x < trim($matches[2]))
return true;
return false;
}
if(preg_match('/[\$](.*?)\s\{\}\s(.*?)$/',$s,$matches)) {
$x = $this->get_condition_var($matches[1]);
if(is_array($x) && in_array(trim($matches[2]),$x))
return true;
return false;
}
if(preg_match('/[\$](.*?)\s\{\*\}\s(.*?)$/',$s,$matches)) {
$x = $this->get_condition_var($matches[1]);
if(is_array($x) && array_key_exists(trim($matches[2]),$x))
return true;
return false;
}
// Ordering of this check (for falsiness) with relation to the following one (check for truthiness) is important.
if (preg_match('/[\!\$](.*?)$/', $s, $matches)) {
$x = $this->get_condition_var($matches[1]);
if (!$x) {
return true;
}
return false;
}
if(preg_match('/[\$](.*?)$/',$s,$matches)) {
$x = $this->get_condition_var($matches[1]);
if($x)
return true;
return false;
}
return false;
}
/**
* @brief Return rendered menu for current channel_id.
*
* @see menu_render()
* @param string $s
* @param string $class (optional) default empty
* @return string
*/
function menu($s, $class = '') {
$channel_id = $this->get_channel_id();
$name = $s;
$cnt = preg_match_all("/\[var=(.*?)\](.*?)\[\/var\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$var[$mtch[1]] = $mtch[2];
$name = str_replace($mtch[0], '', $name);
}
}
else
$var = [];
if($channel_id) {
$m = menu_fetch($name, $channel_id, get_observer_hash());
return menu_render($m, $class, $edit = false, $var);
}
}
function replace_region($match) {
if (array_key_exists($match[1], \App::$page)) {
return \App::$page[$match[1]];
}
}
/**
* @brief Returns the channel_id of the profile owner of the page.
*
* Returns the channel_id of the profile owner of the page, or the local_channel
* if there is no profile owner. Otherwise returns 0.
*
* @return int channel_id
*/
function get_channel_id() {
$channel_id = ((is_array(\App::$profile)) ? \App::$profile['profile_uid'] : 0);
if ((! $channel_id) && (local_channel()))
$channel_id = local_channel();
return $channel_id;
}
/**
* @brief Returns a parsed block.
*
* @param string $s
* @param string $class (optional) default empty
* @return string parsed HTML of block
*/
function block($s, $class = '') {
$var = array();
$matches = array();
$name = $s;
$class = (($class) ? $class : 'bblock widget');
$cnt = preg_match_all("/\[var=(.*?)\](.*?)\[\/var\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$var[$mtch[1]] = $mtch[2];
$name = str_replace($mtch[0], '', $name);
}
}
$o = '';
$channel_id = $this->get_channel_id();
if($channel_id) {
$r = q("select * from item inner join iconfig on iconfig.iid = item.id and item.uid = %d
and iconfig.cat = 'system' and iconfig.k = 'BUILDBLOCK' and iconfig.v = '%s' limit 1",
intval($channel_id),
dbesc($name)
);
if($r) {
//check for eventual menus in the block and parse them
$cnt = preg_match_all("/\[menu\](.*?)\[\/menu\]/ism", $r[0]['body'], $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$r[0]['body'] = str_replace($mtch[0], $this->menu(trim($mtch[1])), $r[0]['body']);
}
}
$cnt = preg_match_all("/\[menu=(.*?)\](.*?)\[\/menu\]/ism", $r[0]['body'], $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$r[0]['body'] = str_replace($mtch[0],$this->menu(trim($mtch[2]),$mtch[1]),$r[0]['body']);
}
}
//emit the block
$wrap = (! x($var, 'wrap') || $var['wrap'] == 'none' ? false : true);
$o .= ($wrap ? '' : '<div class="' . $class . '">');
if($r[0]['title'] && trim($r[0]['body']) != '$content') {
$o .= '<h3>' . $r[0]['title'] . '</h3>';
}
if(trim($r[0]['body']) === '$content') {
$o .= \App::$page['content'];
}
else {
$o .= prepare_text($r[0]['body'], $r[0]['mimetype']);
}
$o .= ($wrap ? '' : '</div>');
}
}
return $o;
}
/**
* @brief Include JS depending on framework.
*
* @param string $s
* @return string
*/
function js($s) {
switch($s) {
case 'jquery':
$path = 'view/js/jquery.js';
break;
case 'bootstrap':
$path = 'vendor/twbs/bootstrap/dist/js/bootstrap.bundle.min.js';
break;
case 'foundation':
$path = 'library/foundation/js/foundation.js';
$init = "\r\n" . '<script>$(document).ready(function() { $(document).foundation(); });</script>';
break;
}
$ret = '<script src="' . z_root() . '/' . $path . '" ></script>';
if($init)
$ret .= $init;
return $ret;
}
/**
* @brief Include CSS depending on framework.
*
* @param string $s
* @return string
*/
function css($s) {
switch($s) {
case 'bootstrap':
$path = 'vendor/twbs/bootstrap/dist/css/bootstrap.min.css';
break;
case 'foundation':
$path = 'library/foundation/css/foundation.min.css';
break;
}
$ret = '<link rel="stylesheet" href="' . z_root() . '/' . $path . '" type="text/css" media="screen">';
return $ret;
}
/**
* This doesn't really belong in Comanche, but it could also be argued that it is the perfect place.
* We need to be able to select what kind of template and decoration to use for the webpage at the heart of our content.
* For now we'll allow an '[authored]' element which defaults to name and date, or 'none' to remove these, and perhaps
* 'full' to provide a social network style profile photo.
*
* But leave it open to have richer templating options and perhaps ultimately discard this one, once we have a better idea
* of what template and webpage options we might desire.
*
* @param[in,out] array $a
* @param string $s
* @return array
*/
function webpage(&$a, $s) {
$ret = array();
$matches = array();
$cnt = preg_match_all("/\[authored\](.*?)\[\/authored\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$ret['authored'] = $mtch[1];
}
}
return $ret;
}
/**
* @brief Render a widget.
*
* @param string $name
* @param string $text
*/
function widget($name, $text) {
$vars = array();
$matches = array();
$cnt = preg_match_all("/\[var=(.*?)\](.*?)\[\/var\]/ism", $text, $matches, PREG_SET_ORDER);
if ($cnt) {
foreach ($matches as $mtch) {
$vars[$mtch[1]] = $mtch[2];
}
}
if(! purify_filename($name))
return '';
$clsname = ucfirst($name);
$nsname = "\\Zotlabs\\Widget\\" . $clsname;
$found = false;
$widgets = \Zotlabs\Extend\Widget::get();
if($widgets) {
foreach($widgets as $widget) {
if(is_array($widget) && strtolower($widget[1]) === strtolower($name) && file_exists($widget[0])) {
require_once($widget[0]);
$found = true;
}
}
}
if(! $found) {
if(file_exists('Zotlabs/SiteWidget/' . $clsname . '.php'))
require_once('Zotlabs/SiteWidget/' . $clsname . '.php');
elseif(file_exists('widget/' . $clsname . '/' . $clsname . '.php'))
require_once('widget/' . $clsname . '/' . $clsname . '.php');
elseif(file_exists('Zotlabs/Widget/' . $clsname . '.php'))
require_once('Zotlabs/Widget/' . $clsname . '.php');
else {
$pth = theme_include($clsname . '.php');
if($pth) {
require_once($pth);
}
}
}
if(class_exists($nsname)) {
$x = new $nsname;
$f = 'widget';
if(method_exists($x,$f)) {
return $x->$f($vars);
}
}
$func = 'widget_' . trim($name);
if(! function_exists($func)) {
if(file_exists('widget/' . trim($name) . '.php'))
require_once('widget/' . trim($name) . '.php');
elseif(file_exists('widget/' . trim($name) . '/' . trim($name) . '.php'))
require_once('widget/' . trim($name) . '/' . trim($name) . '.php');
if(! function_exists($func)) {
$theme_widget = $func . '.php';
if(theme_include($theme_widget)) {
require_once(theme_include($theme_widget));
}
}
}
if(function_exists($func))
return $func($vars);
}
function region($s,$region_name) {
$s = str_replace('$region',$region_name,$s);
$matches = array();
$cnt = preg_match_all("/\[menu\](.*?)\[\/menu\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$s = str_replace($mtch[0], $this->menu(trim($mtch[1])), $s);
}
}
// menu class e.g. [menu=horizontal]my_menu[/menu] or [menu=tabbed]my_menu[/menu]
// allows different menu renderings to be applied
$cnt = preg_match_all("/\[menu=(.*?)\](.*?)\[\/menu\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$s = str_replace($mtch[0],$this->menu(trim($mtch[2]),$mtch[1]),$s);
}
}
$cnt = preg_match_all("/\[block\](.*?)\[\/block\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$s = str_replace($mtch[0],$this->block(trim($mtch[1])),$s);
}
}
$cnt = preg_match_all("/\[block=(.*?)\](.*?)\[\/block\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$s = str_replace($mtch[0],$this->block(trim($mtch[2]),trim($mtch[1])),$s);
}
}
$cnt = preg_match_all("/\[js\](.*?)\[\/js\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$s = str_replace($mtch[0],$this->js(trim($mtch[1])),$s);
}
}
$cnt = preg_match_all("/\[css\](.*?)\[\/css\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$s = str_replace($mtch[0],$this->css(trim($mtch[1])),$s);
}
}
// need to modify this to accept parameters
$cnt = preg_match_all("/\[widget=(.*?)\](.*?)\[\/widget\]/ism", $s, $matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
$s = str_replace((string)$mtch[0], (string)$this->widget(trim((string)$mtch[1]), (string)$mtch[2]), $s);
}
}
return $s;
}
/**
* @brief Registers a page template/variant for use by Comanche selectors.
*
* @param array $arr
* 'template' => template name
* 'variant' => array(
* 'name' => variant name
* 'desc' => text description
* 'regions' => array(
* 'name' => name
* 'desc' => text description
* )
* )
*/
function register_page_template($arr) {
\App::$page_layouts[$arr['template']] = array($arr['variant']);
return;
}
}
|