mostly filebased Content Presentation System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

549 lines
20KB

  1. <?php
  2. namespace Modules;
  3. class FilesInFolders {
  4. private $folder;
  5. public $content = array();
  6. public $extras = array();
  7. private $domains = array('default'=>'default');
  8. private $keyfiles = array();
  9. public $structs = array();
  10. private $EXT=array(
  11. 'txt'=>array( 'txt', 'text', 'md' ),
  12. 'pic'=>array( 'jpg', 'jpeg', 'png', 'svg' ),
  13. 'tpl'=>array( 'html', 'htm', 'tpl' ),
  14. 'csv'=>array( 'csv' )
  15. );
  16. public $config = array();
  17. private $state=array();
  18. function __construct($folder,$conf=array()) {
  19. $f3 = \Base::instance();
  20. if(is_dir($folder)) { // are we given a valid path?
  21. $this->folder = $folder;
  22. } else { // and if not?
  23. $this->folder = $f3->get('CONTENT')."./";
  24. }
  25. if(is_array($conf)) {
  26. foreach($conf as $key=>$value) {
  27. switch($key) {
  28. case 'content':
  29. if(is_array($value)) {
  30. foreach($value as $k=>$v) {
  31. $this->domains[$k] = $v;
  32. }
  33. }
  34. break;
  35. case 'keyfiles':
  36. if(is_array($value)) {
  37. foreach($value as $k=>$v) {
  38. $this->keyfiles[$k] = $v;
  39. }
  40. }
  41. break;
  42. }
  43. }
  44. }
  45. foreach($this->domains as $domain) {
  46. $this->content[$domain] = array();
  47. }
  48. foreach($this->keyfiles as $keys=>$d) {
  49. $this->extras[$keys] = "";
  50. }
  51. }
  52. /////////////////////////////
  53. // read folder into struct //
  54. /////////////////////////////
  55. function prepare_files() {
  56. foreach ($this->domains as $k=>$v) {
  57. foreach($this->EXT as $cat=>$endings) {
  58. $this->structs[$k][$cat]=array();
  59. }
  60. }
  61. $ls = scandir($this->folder);
  62. foreach ($ls as $k=>$f) {
  63. if (!strncmp($f,'.',1)) continue; // ignore hidden files
  64. $ex=explode(".", $f);
  65. $ext=strtolower(end($ex));
  66. if (array_key_exists($ex[0],$this->domains)) {
  67. $domain_key=$ex[0];
  68. $sort_key=1;
  69. } elseif (array_key_exists($ex[0], $this->keyfiles)) {
  70. if(in_array($ext,$this->EXT[$this->keyfiles[$ex[0]]['type']])) {
  71. $this->extras[$ex[0]]=$this->folder.$f;
  72. continue;
  73. }
  74. $domain_key='default';
  75. $sort_key=0;
  76. } else {
  77. $domain_key='default';
  78. $sort_key=0;
  79. }
  80. foreach ($this->EXT as $cat=>$endings) {
  81. if (in_array($ext, $endings)) {
  82. $this->structs[$domain_key][$cat][$ex[$sort_key]] = $this->folder.$f;
  83. break;
  84. }
  85. }
  86. }
  87. foreach($this->keyfiles as $key=>$param) {
  88. if(!$this->extras[$key]) {
  89. $this->extras[$key] = self::search_up(
  90. $key,
  91. array($this->folder,$param['until']),
  92. $this->EXT[$param['type']]
  93. );
  94. }
  95. if($this->extras[$key]) {
  96. if ($param['type'] == 'txt') {
  97. $this->read_textfile($this->extras[$key]);
  98. }
  99. }
  100. }
  101. }
  102. ///////////////////////////////////////
  103. // prepare content as per the struct //
  104. ///////////////////////////////////////
  105. function fill_content() {
  106. $md = new \Parsedown();
  107. foreach($this->domains as $domain_key=>$domain) {
  108. $this->state['current_domain'] = $domain_key;
  109. foreach($this->structs[$domain_key]['txt'] as $key=>$file) {
  110. $str = $this->read_textfile($file);
  111. $str = self::content_element_dispatcher($str);
  112. $str = $md->text($str);
  113. $str = sprintf("<div class='post'>%s</div>", $str);
  114. $this->content[$domain][$key] = sprintf(
  115. "<div class=\"item %s %s\">%s</div>",
  116. $page,
  117. $key,
  118. $str
  119. );
  120. }
  121. foreach($this->structs[$domain_key]['tpl'] as $key=>$file) {
  122. $str = file_get_contents($file);
  123. $str = \Template::instance()->render($file);
  124. $str = self::linkify($str);
  125. $this->content[$domain][$key] = sprintf(
  126. "<div class=\"item %s %s\">%s</div>",
  127. $page,
  128. $key,
  129. $str
  130. );
  131. }
  132. if (false) { // TODO: make this configurable in main.cfg
  133. foreach($this->structs[$domain_key]['pic'] as $key=>$file) {
  134. $this->content[$domain][$key] = sprintf(
  135. "<img class=\"direct\" src=\"/$file\" />"
  136. );
  137. }
  138. }
  139. foreach($this->structs[$domain_key]['csv'] as $key=>$file) {
  140. $csv = new \Modules\Ography($file,TRUE);
  141. $str="<table>";
  142. foreach($csv->entries as $entry) {
  143. $tmp="";
  144. foreach($entry as $key=>$value) {
  145. $tmp .= sprintf("<td class=\"%s\">%s</td>", $key, $value);
  146. }
  147. $str .= sprintf("<tr>%s</tr>",$tmp);
  148. }
  149. $str.="</table>";
  150. $this->content[$domain][$key] = $str;
  151. }
  152. }
  153. }
  154. //////////////////////
  155. // read config data //
  156. //////////////////////
  157. function read_config($domain=false) {
  158. foreach ($this->domains as $source=>$destination) {
  159. if (is_string($domain)) {
  160. if ($source != $domain) { continue; }
  161. } elseif (is_array($domain)) {
  162. if (!in_array($source,$domain)) { continue; }
  163. }
  164. foreach ($this->structs[$source]['txt'] as $key=>$file) {
  165. $this->read_textfile($file);
  166. }
  167. }
  168. return $this->config;
  169. }
  170. ////////////////
  171. // recursions //
  172. ////////////////
  173. function search_up($key,$paths,$ext) {
  174. $return = "";
  175. if(count($paths) == 2) {
  176. $current = $paths[0];
  177. $last_try = $paths[1];
  178. $ls=scandir($current);
  179. foreach($ls as $f) {
  180. if(!strncmp($f,'.',1)) continue; // ignore hidden files
  181. $ex=explode(".", $f);
  182. if(in_array(strtolower(end($ex)),$ext)) {
  183. if($ex[0]==$key) {
  184. $return = $current.$f;
  185. break;
  186. }
  187. }
  188. }
  189. }
  190. if ($return) {
  191. return $return;
  192. } elseif($current == $last_try) {
  193. return false;
  194. } else {
  195. $p = explode('/',$current);
  196. array_pop($p);
  197. array_pop($p);
  198. return self::search_up($key,array(implode("/",$p)."/",$last_try),$ext);
  199. }
  200. }
  201. ///////////////////////
  202. // Utility functions //
  203. ///////////////////////
  204. function read_textfile($file) {
  205. $str = file_get_contents($file);
  206. $str = self::linkify($str);
  207. $str = self::strip_comments($str);
  208. $str = self::get_config_from_content($str);
  209. return $str;
  210. }
  211. function strip_comments($str) {
  212. $single_line_comments = "/(^;.*\R)/m";
  213. $str = preg_replace($single_line_comments,"",$str);
  214. $multi_line_comments = "/\/\*.*?\*\//s";
  215. $str = preg_replace($multi_line_comments,"",$str);
  216. return $str;
  217. }
  218. function linkify($string) {
  219. $pattern = "/\s@(\w+)[=]([\w,]+)\s/";
  220. $count = 0;
  221. $new = preg_replace_callback
  222. ($pattern,
  223. function($m){
  224. $f3 = \Base::instance();
  225. return $f3->get('SITE_URL')
  226. .$f3->alias($m[1],self::$keyword."=".$m[2])
  227. ;},
  228. $string);
  229. return $new;
  230. }
  231. function get_config_from_content($string) {
  232. $f3 = \Base::instance();
  233. $f = 0;
  234. $pattern = "/#\+(\w+):\s?(.*)/";
  235. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  236. for ($i=0;$i<$f;$i++) {
  237. $string = str_replace($matches[0][$i],"",$string);
  238. $key = $matches[1][$i];
  239. $value = $matches[2][$i];
  240. if(strtolower($value) == "false") {
  241. $value = FALSE;
  242. }
  243. if(!strncmp(trim($value),'@',1)) {
  244. //var_dump($f3->get('DATA'));
  245. if (array_key_exists($key,$f3->get('DATA'))) {
  246. $DATA = $f3->get('DATA.'.$key);
  247. $conf = array($DATA['type'],$DATA['dir']);
  248. $relation = new \Modules\TOC($conf,$f3->get('CONTENT'),substr($value,1));
  249. $relation->dispatch();
  250. $this->config[$key]=array_shift($relation->entries);
  251. } else {
  252. $this->config[$key]=$value;
  253. }
  254. } else {
  255. $this->config[$key]=$value;
  256. }
  257. }
  258. $pattern = "/§>\s*(\w+):(.*?)\R[\011\040]*\R/s";
  259. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  260. for ($i=0;$i<$f;$i++) {
  261. $string = str_replace($matches[0][$i],"",$string);
  262. $key = $matches[1][$i];
  263. $value = trim($matches[2][$i]);
  264. if(strtolower($value) == "false") {
  265. $value = FALSE;
  266. }
  267. if (!strncmp($value,'@',1)) {
  268. # var_dump();
  269. if (array_key_exists($key,$f3->get('DATA'))) {
  270. $entries = explode("@",$value);
  271. array_shift($entries); // first entry is always empty
  272. $DATA = $f3->get('DATA.'.$key);
  273. $conf = array($DATA['type'],$DATA['dir']);
  274. $relation = new \Modules\TOC($conf,$f3->get('CONTENT'),$entries);
  275. $relation->dispatch();
  276. if(/*count($entries) >*/ 1) {
  277. $this->config[$key]= new CMultiple($relation->entries);
  278. } else {
  279. $this->config[$key]=array_shift($relation->entries);
  280. }
  281. } else {
  282. $this->config[$key]=$value;
  283. }
  284. } else {
  285. $this->config[$key]=$value;
  286. }
  287. }
  288. return $string;
  289. }
  290. function content_element_dispatcher($string) {
  291. $md = new \Parsedown();
  292. $f0 = 0;
  293. // find occorances of {| keyword |}
  294. $pattern = "/\{\|(.+?)\|\}/s";
  295. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  296. for ($i=0;$i<$f;$i++) {
  297. $body = preg_split("/\R/",trim($matches[1][$i]));
  298. $request = explode(":", trim(array_shift($body)));
  299. $new="";
  300. switch($request[0]) {
  301. case 'test':
  302. $new="seems to work";
  303. break;
  304. case ';':
  305. $new="";
  306. break;
  307. case 'path':
  308. $new="/".$this->folder;
  309. break;
  310. case 'space':
  311. $new=sprintf("<div style=\"height:%s;\"></div>",
  312. $request[1]
  313. );
  314. break;
  315. case 'small-text':
  316. if(count($body)) {
  317. $new=sprintf("<div class=\"f1\">%s</div>",
  318. implode("\n",$body)
  319. );
  320. } else {
  321. $new=sprintf("<span class=\"f1\">%s</span>",
  322. $request[1]
  323. );
  324. }
  325. break;
  326. case 'TOC':
  327. // throw away TOC part of request, we don't need it
  328. array_shift($request);
  329. $toc = new \Modules\TOC($request,$this->folder,$body);
  330. $toc->dispatch();
  331. $new=sprintf("<div class=\"TOC %s\">%s</div>",
  332. array_shift($request),
  333. $toc);
  334. break;
  335. case 'box':
  336. $type = $this->$request[1];
  337. $pics = explode(":",array_shift($body));
  338. $pic = $pics[0];
  339. $pic_hover = count($pics) > 1 ? $pics[1] : $pic;
  340. $cap1 = array_shift($body);
  341. $cap2 = array_shift($body);
  342. $cap1_hover = array_shift($body);
  343. $cap2_hover = array_shift($body);
  344. $body = implode("\n",$body);
  345. if (count($request) >= 4) {
  346. $body=str_replace([$request[2],$request[3]],["{|","|}"],$body);
  347. $body=$this->content_element_dispatcher($body);
  348. }
  349. $new=sprintf("pic: %s (%s)<br>cap1: %s (%s)<br>cap2: %s (%s)<br>body:<br>%s",
  350. $pic,$pic_hover,$cap1,$cap1_hover,$cap2,$cap2_hover,$body
  351. );
  352. break;
  353. case 'calendar':
  354. array_shift($request);
  355. $conf = array('path', $this->folder);
  356. $v = $this->read_config();
  357. $cal = new \Modules\CCalendar($v,$conf);
  358. //$cal->dispatch();
  359. //$conf = array('path', $this->folder);
  360. //$v = $this->read_config();
  361. $new=sprintf("<div class=\"calendar\">%s</div>",
  362. $cal);
  363. break;
  364. case 'header':
  365. array_shift($request);
  366. $conf = array('path', $this->folder);
  367. $v = $this->read_config();
  368. switch (array_shift($request)) {
  369. case 'event':
  370. $el = new CEvent($v,$conf);
  371. $el->set_layout('archive');
  372. $new = $el;
  373. break;
  374. case 'concert':
  375. $el = new CConcert($v,$conf);
  376. $el->set_layout('header');
  377. $new = $el;
  378. break;
  379. }
  380. break;
  381. case 'image':
  382. $key=$request[1];
  383. $image="";
  384. if(!$key) {
  385. $new=self::warn("Content Module \"Image\" needs name of a file (without extension)");
  386. break;
  387. }
  388. foreach($this->structs as $domain=>$destination) {
  389. if(array_key_exists($key, $this->structs[$domain]['pic'])) {
  390. $image = $this->structs[$domain]['pic'][$key];
  391. unset($this->structs[$domain]['pic'][$key]);
  392. unset($this->content[$this->domains[$domain]][$key]);
  393. break;
  394. }
  395. }
  396. if ($image) {
  397. if( in_array($request[2],array('left','right','full'))) {
  398. $class = $request[2];
  399. } else {
  400. $class = 'left';
  401. }
  402. $img = new \Image($image);
  403. $ratio = ($img->width() >= $img->height())
  404. ? "landscape"
  405. : "portrait"
  406. ;
  407. $cached = new CachedImage($image);
  408. foreach ($body as $k=>$line) {
  409. if (strpos($line,"©") !== FALSE
  410. || strpos($line,"&copy;") !== FALSE) {
  411. $body[$k] = sprintf("<span class=\"copyright\">%s</span>",$line);
  412. }
  413. }
  414. $new=sprintf("<div class='image-container %s'>"
  415. ."<div class=\"media %s\"><a href=\"/%s\" data-featherlight=\"image\"><img src=\"/%s\" alt=\"user supplied image\" /></a></div>"
  416. ."<img src=\"/%s\" style=\"display:none;\" alt=\"user supplied image\" />"
  417. ."<div class=\"caption\">%s</div>"
  418. ."</div>",
  419. $class,
  420. $ratio,
  421. $cached->get_src(1400),
  422. $cached->get_src(500),
  423. $cached->get_src(1400),
  424. $md->text(implode("\n",$body))
  425. );
  426. } else {
  427. $new=self::warn("image \"$key\" not found");
  428. }
  429. break;
  430. case 'devide':
  431. $new = "<div>";
  432. $contents = explode($request[1],implode("\n",$body));
  433. $counter=0;
  434. foreach($contents as $part) {
  435. $counter++;
  436. if (count($request) >= 4) {
  437. $part=str_replace([$request[2],$request[3]],["{|","|}"],$part);
  438. $part=$this->content_element_dispatcher($part);
  439. }
  440. $new .= sprintf("<div>%s</div>",
  441. $md->text($part)
  442. );
  443. }
  444. $new.="</div>";
  445. break;
  446. case 'page':
  447. $folder = $this->folder.$request[1]."/";
  448. $f = new \Modules\FilesInFolders($folder);
  449. $f->prepare_files();
  450. $f->fill_content();
  451. $new = implode("\n",$f->content['default']);
  452. break;
  453. case 'youtube':
  454. if( in_array($request[2],array('left','right','full'))) {
  455. $class = $request[2];
  456. } else {
  457. $class = 'left';
  458. }
  459. $video=sprintf("<iframe width=\"700\" height=\"394\" class=\"ytvideo\" "
  460. ."src=\"https://www.youtube.com/embed/%s\"></iframe>",
  461. $request[1]);
  462. $thumbnail = sprintf("<div class=\"video-thumbnail\"><a href=\"%s\" data-featherlight=\"#%s\">"
  463. ."<img class=\"thumbnail\" src=\"https://img.youtube.com/vi/%s/mqdefault.jpg\" alt=\"video-preview\"/>"
  464. ."<img class=\"play-button\" src=\"%s\" alt=\"play-button\" />"
  465. ."</a></div><div class=\"lightbox\" id=\"%s\" >%s</div>",
  466. "https://www.youtube.com/watch?v=".$request[1],
  467. $request[1],
  468. $request[1],
  469. "/rsc/img/play-button.png",
  470. $request[1],
  471. $video
  472. );
  473. foreach ($body as $k=>$line) {
  474. if (strpos($line,"©") !== FALSE
  475. || strpos($line,"&copy;") !== FALSE) {
  476. $body[$k] = sprintf("<span class=\"copyright\">%s</span>",$line);
  477. }
  478. }
  479. $new=sprintf("<div class='video-container %s'>"
  480. ."<div class=\"media\">%s</div>"
  481. ."<div class=\"caption\">%s</div>"
  482. ."</div>",
  483. $class,
  484. $thumbnail,
  485. $md->text(implode("\n",$body)));
  486. break;
  487. default:
  488. $new=self::warn("Content Module \"".$request[0]."\" unknown");
  489. break;
  490. }
  491. $string = str_replace($matches[0][$i],$new,$string);
  492. }
  493. return $string;
  494. }
  495. function warn($message) {
  496. return sprintf("<div class=\"warning\">%s</div>",$message);
  497. }
  498. }