mostly filebased Content Presentation System
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

483 lines
17KB

  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. }
  96. }
  97. ///////////////////////////////////////
  98. // prepare content as per the struct //
  99. ///////////////////////////////////////
  100. function fill_content() {
  101. $md = new \Parsedown();
  102. foreach($this->domains as $domain_key=>$domain) {
  103. $this->state['current_domain'] = $domain_key;
  104. foreach($this->structs[$domain_key]['txt'] as $key=>$file) {
  105. $str = $this->read_textfile($file);
  106. $str = self::content_element_dispatcher($str);
  107. $str = $md->text($str);
  108. $str = sprintf("<div class='post'>%s</div>", $str);
  109. $this->content[$domain][$key] = sprintf(
  110. "<div class=\"item %s %s\">%s</div>",
  111. $page,
  112. $key,
  113. $str
  114. );
  115. }
  116. foreach($this->structs[$domain_key]['tpl'] as $key=>$file) {
  117. $str = file_get_contents($file);
  118. $str = \Template::instance()->render($file);
  119. $str = self::linkify($str);
  120. $this->content[$domain][$key] = sprintf(
  121. "<div class=\"item %s %s\">%s</div>",
  122. $page,
  123. $key,
  124. $str
  125. );
  126. }
  127. foreach($this->structs[$domain_key]['pic'] as $key=>$file) {
  128. $this->content[$domain][$key] = sprintf(
  129. "<img class=\"direct\" src=\"/$file\" />"
  130. );
  131. }
  132. foreach($this->structs[$domain_key]['csv'] as $key=>$file) {
  133. $csv = new \Modules\Ography($file,TRUE);
  134. $str="<table>";
  135. foreach($csv->entries as $entry) {
  136. $tmp="";
  137. foreach($entry as $key=>$value) {
  138. $tmp .= sprintf("<td class=\"%s\">%s</td>", $key, $value);
  139. }
  140. $str .= sprintf("<tr>%s</tr>",$tmp);
  141. }
  142. $str.="</table>";
  143. $this->content[$domain][$key] = $str;
  144. }
  145. }
  146. }
  147. //////////////////////
  148. // read config data //
  149. //////////////////////
  150. function read_config($domain=false) {
  151. foreach ($this->domains as $source=>$destination) {
  152. if (is_string($domain)) {
  153. if ($source != $domain) { continue; }
  154. } elseif (is_array($domain)) {
  155. if (!in_array($source,$domain)) { continue; }
  156. }
  157. foreach ($this->structs[$source]['txt'] as $key=>$file) {
  158. $this->read_textfile($file);
  159. }
  160. }
  161. return $this->config;
  162. }
  163. ////////////////
  164. // recursions //
  165. ////////////////
  166. function search_up($key,$paths,$ext) {
  167. $return = "";
  168. if(count($paths) == 2) {
  169. $current = $paths[0];
  170. $last_try = $paths[1];
  171. $ls=scandir($current);
  172. foreach($ls as $f) {
  173. if(!strncmp($f,'.',1)) continue; // ignore hidden files
  174. $ex=explode(".", $f);
  175. if(in_array(strtolower(end($ex)),$ext)) {
  176. if($ex[0]==$key) {
  177. $return = $current.$f;
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. if ($return) {
  184. return $return;
  185. } elseif($current == $last_try) {
  186. return false;
  187. } else {
  188. $p = explode('/',$current);
  189. array_pop($p);
  190. array_pop($p);
  191. return self::search_up($key,array(implode("/",$p)."/",$last_try),$ext);
  192. }
  193. }
  194. ///////////////////////
  195. // Utility functions //
  196. ///////////////////////
  197. function read_textfile($file) {
  198. $str = file_get_contents($file);
  199. $str = self::linkify($str);
  200. $str = self::strip_comments($str);
  201. $str = self::get_config_from_content($str);
  202. return $str;
  203. }
  204. function strip_comments($str) {
  205. $single_line_comments = "/(^;.*\R)/m";
  206. $str = preg_replace($single_line_comments,"",$str);
  207. $multi_line_comments = "/\/\*.*?\*\//s";
  208. $str = preg_replace($multi_line_comments,"",$str);
  209. return $str;
  210. }
  211. function linkify($string) {
  212. $pattern = "/\s@(\w+)[=]([\w,]+)\s/";
  213. $count = 0;
  214. $new = preg_replace_callback
  215. ($pattern,
  216. function($m){
  217. $f3 = \Base::instance();
  218. return $f3->get('SITE_URL')
  219. .$f3->alias($m[1],self::$keyword."=".$m[2])
  220. ;},
  221. $string);
  222. return $new;
  223. }
  224. function get_config_from_content($string) {
  225. $f3 = \Base::instance();
  226. $f = 0;
  227. $pattern = "/#\+(\w+):\s?(.*)/";
  228. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  229. for ($i=0;$i<$f;$i++) {
  230. $string = str_replace($matches[0][$i],"",$string);
  231. $key = $matches[1][$i];
  232. $value = $matches[2][$i];
  233. if(strtolower($value) == "false") {
  234. $value = FALSE;
  235. }
  236. if(!strncmp(trim($value),'@',1)) {
  237. //var_dump($f3->get('DATA'));
  238. if (array_key_exists($key,$f3->get('DATA'))) {
  239. $DATA = $f3->get('DATA.'.$key);
  240. $conf = array($DATA['type'],$DATA['dir']);
  241. $relation = new \Modules\TOC($conf,$f3->get('CONTENT'),substr($value,1));
  242. $relation->dispatch();
  243. $this->config[$key]=array_shift($relation->entries);
  244. } else {
  245. $this->config[$key]=$value;
  246. }
  247. } else {
  248. $this->config[$key]=$value;
  249. }
  250. }
  251. $pattern = "/§>\s*(\w+):(.*?)\R[\011\040]*\R/s";
  252. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  253. for ($i=0;$i<$f;$i++) {
  254. $string = str_replace($matches[0][$i],"",$string);
  255. $key = $matches[1][$i];
  256. $value = trim($matches[2][$i]);
  257. if(strtolower($value) == "false") {
  258. $value = FALSE;
  259. }
  260. if (!strncmp($value,'@',1)) {
  261. # var_dump();
  262. if (array_key_exists($key,$f3->get('DATA'))) {
  263. $entries = explode("@",$value);
  264. array_shift($entries); // first entry is always empty
  265. $DATA = $f3->get('DATA.'.$key);
  266. $conf = array($DATA['type'],$DATA['dir']);
  267. $relation = new \Modules\TOC($conf,$f3->get('CONTENT'),$entries);
  268. $relation->dispatch();
  269. if(/*count($entries) >*/ 1) {
  270. $this->config[$key]= new CMultiple($relation->entries);
  271. } else {
  272. $this->config[$key]=array_shift($relation->entries);
  273. }
  274. } else {
  275. $this->config[$key]=$value;
  276. }
  277. } else {
  278. $this->config[$key]=$value;
  279. }
  280. }
  281. return $string;
  282. }
  283. function content_element_dispatcher($string) {
  284. $md = new \Parsedown();
  285. $f0 = 0;
  286. // find occorances of {| keyword |}
  287. $pattern = "/\{\|(.+?)\|\}/s";
  288. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  289. for ($i=0;$i<$f;$i++) {
  290. $body = preg_split("/\R/",trim($matches[1][$i]));
  291. $request = explode(":", trim(array_shift($body)));
  292. $new="";
  293. switch($request[0]) {
  294. case 'test':
  295. $new="seems to work";
  296. break;
  297. case ';':
  298. $new="";
  299. break;
  300. case 'path':
  301. $new="/".$this->folder;
  302. break;
  303. case 'space':
  304. $new=sprintf("<div style=\"height:%s;\"></div>",
  305. $request[1]
  306. );
  307. break;
  308. case 'small-text':
  309. if(count($body)) {
  310. $new=sprintf("<div class=\"f1\">%s</div>",
  311. implode("\n",$body)
  312. );
  313. } else {
  314. $new=sprintf("<span class=\"f1\">%s</span>",
  315. $request[1]
  316. );
  317. }
  318. break;
  319. case 'TOC':
  320. // throw away TOC part of request, we don't need it
  321. array_shift($request);
  322. $toc = new \Modules\TOC($request,$this->folder,$body);
  323. $toc->dispatch();
  324. $new=sprintf("<div class=\"TOC %s\">%s</div>",
  325. array_shift($request),
  326. $toc);
  327. break;
  328. case 'header':
  329. array_shift($request);
  330. $conf = array('path', $this->folder);
  331. $v = $this->read_config();
  332. switch (array_shift($request)) {
  333. case 'event':
  334. $el = new CEvent($v,$conf);
  335. $el->set_layout('archive');
  336. $new = $el;
  337. break;
  338. case 'concert':
  339. $el = new CConcert($v,$conf);
  340. $el->set_layout('header');
  341. $new = $el;
  342. break;
  343. }
  344. break;
  345. case 'image':
  346. $key=$request[1];
  347. $image="";
  348. if(!$key) {
  349. $new=self::warn("Content Module \"Image\" needs name of a file (without extension)");
  350. break;
  351. }
  352. foreach($this->structs as $domain=>$destination) {
  353. if(array_key_exists($key, $this->structs[$domain]['pic'])) {
  354. $image = $this->structs[$domain]['pic'][$key];
  355. unset($this->structs[$domain]['pic'][$key]);
  356. unset($this->content[$this->domains[$domain]][$key]);
  357. break;
  358. }
  359. }
  360. if ($image) {
  361. if( in_array($request[2],array('left','right','full'))) {
  362. $class = $request[2];
  363. } else {
  364. $class = 'left';
  365. }
  366. $img = new \Image($image);
  367. $ratio = ($img->width() >= $img->height())
  368. ? "landscape"
  369. : "portrait"
  370. ;
  371. $cached = new CachedImage($image);
  372. foreach ($body as $k=>$line) {
  373. if (strpos($line,"©") !== FALSE
  374. || strpos($line,"&copy;") !== FALSE) {
  375. $body[$k] = sprintf("<span class=\"copyright\">%s</span>",$line);
  376. }
  377. }
  378. $new=sprintf("<div class='image-container %s'>"
  379. ."<div class=\"media %s\"><a href=\"/%s\" data-featherlight=\"image\"><img src=\"/%s\" alt=\"user supplied image\" /></a></div>"
  380. ."<img src=\"/%s\" style=\"display:none;\" alt=\"user supplied image\" />"
  381. ."<div class=\"caption\">%s</div>"
  382. ."</div>",
  383. $class,
  384. $ratio,
  385. $cached->get_src(1400),
  386. $cached->get_src(500),
  387. $cached->get_src(1400),
  388. $md->text(implode("\n",$body))
  389. );
  390. } else {
  391. $new=self::warn("image \"$key\" not found");
  392. }
  393. break;
  394. case 'youtube':
  395. if( in_array($request[2],array('left','right','full'))) {
  396. $class = $request[2];
  397. } else {
  398. $class = 'left';
  399. }
  400. $video=sprintf("<iframe width=\"700\" height=\"394\" class=\"ytvideo\" "
  401. ."src=\"https://www.youtube.com/embed/%s\"></iframe>",
  402. $request[1]);
  403. $thumbnail = sprintf("<div class=\"video-thumbnail\"><a href=\"%s\" data-featherlight=\"#%s\">"
  404. ."<img class=\"thumbnail\" src=\"https://img.youtube.com/vi/%s/mqdefault.jpg\" alt=\"video-preview\"/>"
  405. ."<img class=\"play-button\" src=\"%s\" alt=\"play-button\" />"
  406. ."</a></div><div class=\"lightbox\" id=\"%s\" >%s</div>",
  407. "https://www.youtube.com/watch?v=".$request[1],
  408. $request[1],
  409. $request[1],
  410. "/rsc/img/play-button.png",
  411. $request[1],
  412. $video
  413. );
  414. foreach ($body as $k=>$line) {
  415. if (strpos($line,"©") !== FALSE
  416. || strpos($line,"&copy;") !== FALSE) {
  417. $body[$k] = sprintf("<span class=\"copyright\">%s</span>",$line);
  418. }
  419. }
  420. $new=sprintf("<div class='video-container %s'>"
  421. ."<div class=\"media\">%s</div>"
  422. ."<div class=\"caption\">%s</div>"
  423. ."</div>",
  424. $class,
  425. $thumbnail,
  426. $md->text(implode("\n",$body)));
  427. break;
  428. default:
  429. $new=self::warn("Content Module \"".$request[0]."\" unknown");
  430. break;
  431. }
  432. $string = str_replace($matches[0][$i],$new,$string);
  433. }
  434. return $string;
  435. }
  436. function warn($message) {
  437. return sprintf("<div class=\"warning\">%s</div>",$message);
  438. }
  439. }