|
- <?php
-
- namespace Modules;
-
- class ElementDispatcher {
-
- private $folder = "";
- private $config = [];
-
- function __construct($folder = "",$config = []) {
- if (is_string($folder)) {
- $this->folder = $folder;
- }
- if (is_array($config)) {
- $this->config = $config;
- }
- }
-
- function content_element($request = [], $body = null) {
- $f3 = \Base::instance();
- $md = new \freaParsedown();
- $md->deactivate_ol();
- $new = "";
-
- switch($request[0]) {
- case 'test':
- $new="seems to work";
- break;
- case ';':
- $new="";
- break;
- case 'path':
- $new="/".$this->folder;
- break;
- case 'space':
- $new=sprintf("<div style=\"height:%s;\"></div>",
- $request[1]
- );
- break;
- case 'span':
- $new=sprintf("<span class=\"%s\">%s</span>",
- $request[1],
- $request[2] ? : implode("\n",$body)
- );
- break;
- case 'small-text':
- if(count($body)) {
- $new=sprintf("<div class=\"smalltext\">%s</div>",
- $md->text(implode("\n",$body))
- );
- } else {
- $new=sprintf("<span class=\"smalltext\">%s</span>",
- $request[1]
- );
- }
- break;
-
- case 'CE':
- switch($request[1]) {
- case 'publication':
- $CE = new \Modules\CPublication($this->config,['path'=>$this->folder]);
- if(count($request)>2) {
- $CE->set_layout($request[2]);
- }
- $new = $CE;
- break;
- }
- break;
-
- case 'TOC':
- // throw away TOC part of request, we don't need it
- array_shift($request);
- $toc = new \Modules\TOC($request,$this->folder,$body);
- $toc->dispatch();
-
- $new=sprintf("<div class=\"TOC %s\">%s</div>",
- array_shift($request),
- $toc);
- break;
-
- case 'sql':
- //array_shift($request);
- switch ($request[1]) {
- case 'sqlite':
- $array = [];
- $db = new \DB\SQL('sqlite:' . $this->folder . $request[2]);
- $sql = implode(" ", $body);
- if ( strpos($sql,'insert')===false &&
- strpos($sql,'update')===false &&
- strpos($sql,'drop')===false) {
- $rows = $db->exec($sql);
- foreach ($rows as $res) {
- switch (count($res)) {
- case 1:
- $keys=array_keys($res);
- $array[] = $res[$keys[0]];
- break;
- case 2:
- $keys=array_keys($res);
- $array[$res[$keys[0]]] = $res[$keys[1]];
- break;
- }
- }
- }
-
- $new = sprintf("<ul><li>%s</li></ul>",
- implode("</li><li>",$array));
- unset($array);
- break;
- }
-
- break;
-
- case 'form':
- $token_db = $f3->get('TEMP') . "CEform/";
- $form_config_file = sprintf("%s%s.cfg",
- ROOT.$this->folder,
- $request[1]);
-
- $db = new \DB\Jig($token_db,\DB\Jig::FORMAT_JSON);
- $formcall = new \DB\Jig\Mapper($db,'form_calls');
- $timestamp = time();
- $token = md5($timestamp . $form_config_file . rand(100,999));
-
- $f3->config($form_config_file);
- $fields = $f3->get('fields');
-
- foreach ($fields as $k => $v) {
- if ($v['type'] == 'custom') {
- $fields[$k]['template'] = sprintf(
- "%s%s",
- substr($this->folder,strlen($f3->get('CONTENT_BASE'))),
- $v['template']
- );
- }
- if (array_key_exists('db',$v)) {
- switch ($v['db']['type']) {
- case 'sqlite':
- $array = [];
- $db = new \DB\SQL('sqlite:' . $this->folder . $v['db']['file']);
- $sql = $v['db']['sql'];
- if ( strpos($sql,'insert')===false &&
- strpos($sql,'update')===false &&
- strpos($sql,'drop')===false) {
- $rows = $db->exec($sql);
- foreach ($rows as $res) {
- switch (count($res)) {
- case 2:
- $keys=array_keys($res);
- $array[$res[$keys[0]]] = $res[$keys[1]];
- break;
- }
- }
- }
- $fields[$k]['el'] = $array;
- unset($array);
- break;
- }
- }
- }
-
- $f3->set('fields',
- array_merge(
- $fields,
- ['xss-token'=>[
- 'type'=>'hidden',
- 'value'=>$token,
- 'length'=>strlen($token)
- ]]
- ));
-
- $formcall->token = $token;
- $formcall->timestamp = $timestamp;
- $formcall->form = $form_config_file;
- $formcall->path = ROOT.$this->folder;
- $formcall->save();
-
- $form_view = new \Template;
- $new=$form_view->render('form.htm');
-
- break;
-
- case 'box':
- array_shift($request); //get rid of identifier
- $type = array_shift($request);
- $pics = explode(":",array_shift($body));
- $pic = $pics[0];
- $pic_hover = count($pics) > 1 ? $pics[1] : $pic;
- if (count($body) >= 3) {
- $caption = ['normal'=>['cap1' => array_shift($body),
- 'cap2' => array_shift($body)],
- 'hover'=> ['cap1' => array_shift($body),
- 'cap2' => array_shift($body)]];
- $caption_html=[];
- foreach ($caption as $state => $set) {
- if(!$set['cap2']) {
- $caption_html[$state] = [
- sprintf('<span class="first"> </span>'),
- sprintf('<span class="first">%s</span>',$set['cap1'])
- ];
- } else if (!$set['cap1'] && $set['cap2']) {
- $caption_html[$state] = [
- sprintf('<span class="second">%s</span>',$set['cap2'])
- ];
- } else {
- $caption_html[$state] = [
- sprintf('<span class="first">%s</span>',$set['cap1']?:" "),
- sprintf('<span class="second">%s</span>',$set['cap2'])
- ];
- }
- }
- $has_caption = TRUE;
- } else {
- $has_caption = FALSE;
- }
-
- if (file_exists($this->folder.$pic)) {
- $pic = $this->folder.$pic;
- } else {
- $pic = $f3->get('RESOURCES')."img/default_img.png";
- }
- if (file_exists($this->folder.$pic_hover)) {
- $pic_hover = $this->folder.$pic_hover;
- } else {
- $pic_hover = $pic;
- }
-
- if(0) {
- $PIC = new \Image($pic);
- $orientation = $PIC->width() > $PIC->height()
- ? 'landscape'
- : 'portrait'
- ;
- unset($PIC);
- } else {
- list($wwidth, $hheight) = getimagesize($pic);
- $orientation = $wwidth > $hheight ? 'landscape' : 'portrait';
- }
- $pic = new CachedImage($pic);
- $pic_hover = new CachedImage($pic_hover);
- $class="";
- $add="";
- switch($type) {
- case 'plain':
- $link=false;
- break;
- case 'download':
- $file = "/".$this->folder.implode(":",$request);
- $link='href="'.$file.'" download ';
- break;
- case 'lightbox':
- $body = implode("\n",$body);
- if (count($request) % 2) {
- $class = array_pop($request);
- }
- if (count($request) >= 2) {
- $body=str_replace([$request[0],$request[1]],["{|","|}"],$body);
- $body=$this->content_element_dispatcher($body);
- }
- $hash=md5($body);
- $add=sprintf("<div id=\"%s\" class=\"content_elment_box_body\">\n%s\n</div>",
- $hash,
- $md->text($body));
- $link='href="#" data-featherlight="#'.$hash.'" ';
- break;
- case 'internal':
- $dest=implode(":",$request);
-
- $data = [];
- $base = substr($dest,0,strpos($dest,'?') ? : strlen($dest));
- parse_str(parse_url($dest,PHP_URL_QUERY),$data);
- $fragment = parse_url($dest,PHP_URL_FRAGMENT);
-
- if ($f3->get('LANG') != $f3->get('default_lang')) {
- if (!array_key_exists('lang',$data)) {
- $data['lang'] = $f3->get('LANG');
- }
- }
- $new_dest = $base;
- if (count($data) > 0) {
- $new_dest .= "?" . http_build_query($data);
- }
- if ($fragment) {
- $new_dest .= "#" . $fragment;
- }
-
- //if ($f3->get('LANG') != $f3->get('default_lang')) {
- // $dest .= "?lang=".$f3->get('LANG');
- //}
- $link=sprintf('href="%s" ',$f3->get('SITE_URL')."/".$new_dest);
- break;
- case 'external':
- $dest=implode(":",$request);
- $target = $f3->get('external_links_open_in_new_window')
- ? 'target="_blank"'
- : ''
- ;
- $link=sprintf('href="%s" %s',$dest, $target);
- break;
- default:
- $link='href="#"';
- break;
- }
- $new=sprintf("<div class=\"brick %s\">\n<a class=\"content_element_box\" %s>\n<div class=\"content_element_box\">
- <div class=\"image\">
- <img class=\"standard\" src=\"%s\" /><img class=\"hover\" src=\"%s\" />
- </div>
- %s
-
- %s
- \n</div>\n</a>\n%s\n</div>",
- implode(" ",[$orientation,$class,$type]),
- $link,
- $pic->get_src(1600),
- $pic_hover->get_src(1600),
- ($has_caption ? "<div class=\"caption standard\">".implode("<br>",$caption_html['normal'])."</div>": " "),
- ($has_caption ? "<div class=\"caption hover\">".implode("<br>",$caption_html['hover'])."</div>" : " " ),
- $add
- );
- break;
-
- case 'brick':
- array_shift($request);
- $class = array_shift($request);
- $new = sprintf("<div class=\"content_element brick %s\">\n%s\n</div>",
- $class,
- $md->text(implode("\n",$body))
- );
- break;
- case 'calendar':
- array_shift($request);
- $conf = array('path', $this->folder);
- $v = $this->read_config();
- $cal = new \Modules\CCalendar($v,$conf);
-
- $new=sprintf("<div class=\"calendar\">\n%s\n</div>",
- $cal);
- break;
-
- case 'header':
- array_shift($request);
- $conf = array('path', $this->folder);
- $v = $this->read_config();
- switch (array_shift($request)) {
- case 'event':
- $el = new CEvent($v,$conf);
- $el->set_layout('archive');
- $new = $el;
- break;
- case 'concert':
- $el = new CConcert($v,$conf);
- $el->set_layout('header');
- $new = $el;
- break;
- }
- break;
-
- case 'data':
- $method = strtoupper($request[1]);
- //$keys = [];
-
- if (in_array($method,['POST','GET','SESSION'])) {
- foreach ($body as $line) {
- $key = explode("=",$line);
- $raw = $f3->get($method.'.'.$key[0]);
- if (count($key) >= 2) {
- $type=$key[1];
- } else {
- $type = false;
- }
- switch ($type) {
- case 'base64':
- $new = urlsafe_b64decode($raw);
- break;
- default:
- $new = $raw;
- }
- }
- //var_dump($new);
- }
- break;
-
- case 'buy':
- // synopsis:
- // buy:name:price
- // TODO: become currency aware (now EURO is hardcoded)
- $new = '';
-
- if (count($request)>=2) {
- $name=$request[1];
- } else {
- $name="item";
- }
- if (count($request)>=3) {
- $price=$request[2];
- } else {
- $price=1.0;
- }
-
- $fields = [
- 'caller' => $f3->get('page'),
- 'name'=>$name,
- 'price'=>$price,
- 'amount'=>1
- ];
- $hidden_inputs = [];
- foreach ($fields as $k=>$v) {
- $hidden_inputs[] = sprintf('<input type="hidden" name="%s" value="%s" />',$k,$v);
- }
- $new = sprintf('<form method="post" action="%s">%s<button action="submit" >BUY</button><span class="price"> %s</span></form>',
- '/api/cart/add',
- implode("\n",$hidden_inputs),
- $price ."€"
- );
- break;
-
- case 'checkout':
- //first argument, if present, is a path to folder containing order database
- $path = '';
- if (count($request) > 1) {
- $add_path = $request[1];
- if (strncmp($add_path,"/",1)) {
- $path = $this->folder.$add_path;
- } else {
- $path = $add_path;
- }
- } else {
- $path = $this->folder;
- }
- $checkout = new \Controller\Checkout($path);
- $new = $checkout->index();
- break;
- case 'only_cart':
- $checkout = new \Controller\Checkout($this->folder);
- $new = $checkout->html_cart();
- break;
- case 'image':
- $module = new CEimage($request, $body, $this->structs, $this->content);
- $new = $module->index();
- unset($module);
- break;
-
- case 'devide':
- $contents = explode($request[1],implode("\n",$body));
- $c=count($contents);
- $str="";
-
- for ($iiii=0;$iiii<$c;$iiii++) {
- $str .= sprintf(" %f%%",100/$c);
- }
- $template = sprintf('grid-template-columns:%s;',
- $str);
-
-
- $counter=0;
- $new = sprintf('<div class="content_element_devided" style="%s">',$template);
- foreach($contents as $part) {
- $counter++;
- if (count($request) >= 4) {
- $part=str_replace([$request[2],$request[3]],["{|","|}"],$part);
- $part=$this->content_element_dispatcher($part);
- }
- $new .= sprintf("<div>\n%s\n</div>",
- $md->text($part)
- );
- }
- $new.="</div>";
-
- break;
-
- case 'page':
- array_shift($request);
- $target = array_shift($request);
- $class = array_shift($request);
- $folder = $this->folder.$target."/";
- $anchor_name=array_pop(explode("/",$target));
- $fff = new \Modules\FilesInFolders(
- $folder,
- ['content'=>[
- 'secondary'=>'secondary',
- 'zzz'=>'hidden',
- 'unpublish'=>'hidden'],
- 'keyfiles'=>[
- 'banner'=>[
- 'until'=>$folder,
- 'type'=>'pic']]]
- );
- $fff->prepare_files();
- foreach ($body as $line) {
- $ccc = explode(":",$line);
- if (count($ccc) == 2) {
- if ($ccc[1] == 'false') {
- $ccc[1] = false;
- }
- $fff->config[$ccc[0]] = $ccc[1];
- }
- }
- $fff->fill_content();
- //var_dump($fff->config);
- $banner = "";
- if ($fff->extras['banner']) {
- $banner=sprintf("<img class=\"banner\" src=\"%s\" alt=\"section banner\"/>",
- $fff->extras['banner']);
- }
- $new = sprintf("<div class=\"content_element_page %s\">"
- ."<a name=\"%s\">%s</a>\n%s\n</div>",
- $class,
- $anchor_name,
- $banner,
- implode("\n",$fff->content['default']));
-
- break;
-
- case 'youtube':
- $vid=array_shift($request);
- $pos= array_shift($request);
-
- if( in_array($pos,array('left','right','full'))) {
- $class = $pos;
- } else {
- $class = 'left';
- }
- $video=sprintf("<iframe width=\"700\" height=\"394\" class=\"ytvideo\" "
- ."src=\"https://www.youtube.com/embed/%s\"></iframe>",
- $vid);
-
- $thumbnail = sprintf("<div class=\"video-thumbnail\"><a href=\"%s\" data-featherlight=\"#%s\">"
- ."<img class=\"thumbnail\" src=\"https://img.youtube.com/vi/%s/mqdefault.jpg\" alt=\"video-preview\"/>"
- ."<img class=\"play-button\" src=\"%s\" alt=\"play-button\" />"
- ."</a></div><div class=\"lightbox\" id=\"%s\" >%s</div>",
- "https://www.youtube.com/watch?v=".$vid,
- $vid,
- $vid,
- "/rsc/img/play-button.png",
- $vid,
- $video
- );
-
-
- foreach ($body as $k=>$line) {
- if (strpos($line,"©") !== FALSE
- || strpos($line,"©") !== FALSE) {
- $body[$k] = sprintf("<span class=\"copyright\">%s</span>",$line);
- }
- }
- $new=sprintf("<div class='video-container %s'>"
- ."<div class=\"media\">%s</div>"
- ."<div class=\"caption\">%s</div>"
- ."</div>",
- $class,
- $thumbnail,
- $md->text(implode("\n",$body)));
-
- break;
- default:
- $new=self::warn("Content Module \"".$request[0]."\" unknown");
- break;
- }
-
- return $new;
- }
- }
|