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.

529 lines
19KB

  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 'calendar':
  336. array_shift($request);
  337. $conf = array('path', $this->folder);
  338. $v = $this->read_config();
  339. $cal = new \Modules\CCalendar($v,$conf);
  340. //$cal->dispatch();
  341. //$conf = array('path', $this->folder);
  342. //$v = $this->read_config();
  343. $new=sprintf("<div class=\"calendar\">%s</div>",
  344. $cal);
  345. break;
  346. case 'header':
  347. array_shift($request);
  348. $conf = array('path', $this->folder);
  349. $v = $this->read_config();
  350. switch (array_shift($request)) {
  351. case 'event':
  352. $el = new CEvent($v,$conf);
  353. $el->set_layout('archive');
  354. $new = $el;
  355. break;
  356. case 'concert':
  357. $el = new CConcert($v,$conf);
  358. $el->set_layout('header');
  359. $new = $el;
  360. break;
  361. }
  362. break;
  363. case 'image':
  364. $key=$request[1];
  365. $image="";
  366. if(!$key) {
  367. $new=self::warn("Content Module \"Image\" needs name of a file (without extension)");
  368. break;
  369. }
  370. foreach($this->structs as $domain=>$destination) {
  371. if(array_key_exists($key, $this->structs[$domain]['pic'])) {
  372. $image = $this->structs[$domain]['pic'][$key];
  373. unset($this->structs[$domain]['pic'][$key]);
  374. unset($this->content[$this->domains[$domain]][$key]);
  375. break;
  376. }
  377. }
  378. if ($image) {
  379. if( in_array($request[2],array('left','right','full'))) {
  380. $class = $request[2];
  381. } else {
  382. $class = 'left';
  383. }
  384. $img = new \Image($image);
  385. $ratio = ($img->width() >= $img->height())
  386. ? "landscape"
  387. : "portrait"
  388. ;
  389. $cached = new CachedImage($image);
  390. foreach ($body as $k=>$line) {
  391. if (strpos($line,"©") !== FALSE
  392. || strpos($line,"&copy;") !== FALSE) {
  393. $body[$k] = sprintf("<span class=\"copyright\">%s</span>",$line);
  394. }
  395. }
  396. $new=sprintf("<div class='image-container %s'>"
  397. ."<div class=\"media %s\"><a href=\"/%s\" data-featherlight=\"image\"><img src=\"/%s\" alt=\"user supplied image\" /></a></div>"
  398. ."<img src=\"/%s\" style=\"display:none;\" alt=\"user supplied image\" />"
  399. ."<div class=\"caption\">%s</div>"
  400. ."</div>",
  401. $class,
  402. $ratio,
  403. $cached->get_src(1400),
  404. $cached->get_src(500),
  405. $cached->get_src(1400),
  406. $md->text(implode("\n",$body))
  407. );
  408. } else {
  409. $new=self::warn("image \"$key\" not found");
  410. }
  411. break;
  412. case 'devide':
  413. $new = "<div>";
  414. $contents = explode($request[1],implode("\n",$body));
  415. $counter=0;
  416. foreach($contents as $part) {
  417. $counter++;
  418. if (count($request) >= 4) {
  419. $part=str_replace([$request[2],$request[3]],["{|","|}"],$part);
  420. $part=$this->content_element_dispatcher($part);
  421. }
  422. $new .= sprintf("<div>%s</div>",
  423. $md->text($part)
  424. );
  425. }
  426. $new.="</div>";
  427. break;
  428. case 'page':
  429. $folder = $this->folder.$request[1]."/";
  430. $f = new \Modules\FilesInFolders($folder);
  431. $f->prepare_files();
  432. $f->fill_content();
  433. $new = implode("\n",$f->content['default']);
  434. break;
  435. case 'youtube':
  436. if( in_array($request[2],array('left','right','full'))) {
  437. $class = $request[2];
  438. } else {
  439. $class = 'left';
  440. }
  441. $video=sprintf("<iframe width=\"700\" height=\"394\" class=\"ytvideo\" "
  442. ."src=\"https://www.youtube.com/embed/%s\"></iframe>",
  443. $request[1]);
  444. $thumbnail = sprintf("<div class=\"video-thumbnail\"><a href=\"%s\" data-featherlight=\"#%s\">"
  445. ."<img class=\"thumbnail\" src=\"https://img.youtube.com/vi/%s/mqdefault.jpg\" alt=\"video-preview\"/>"
  446. ."<img class=\"play-button\" src=\"%s\" alt=\"play-button\" />"
  447. ."</a></div><div class=\"lightbox\" id=\"%s\" >%s</div>",
  448. "https://www.youtube.com/watch?v=".$request[1],
  449. $request[1],
  450. $request[1],
  451. "/rsc/img/play-button.png",
  452. $request[1],
  453. $video
  454. );
  455. foreach ($body as $k=>$line) {
  456. if (strpos($line,"©") !== FALSE
  457. || strpos($line,"&copy;") !== FALSE) {
  458. $body[$k] = sprintf("<span class=\"copyright\">%s</span>",$line);
  459. }
  460. }
  461. $new=sprintf("<div class='video-container %s'>"
  462. ."<div class=\"media\">%s</div>"
  463. ."<div class=\"caption\">%s</div>"
  464. ."</div>",
  465. $class,
  466. $thumbnail,
  467. $md->text(implode("\n",$body)));
  468. break;
  469. default:
  470. $new=self::warn("Content Module \"".$request[0]."\" unknown");
  471. break;
  472. }
  473. $string = str_replace($matches[0][$i],$new,$string);
  474. }
  475. return $string;
  476. }
  477. function warn($message) {
  478. return sprintf("<div class=\"warning\">%s</div>",$message);
  479. }
  480. }