mostly filebased Content Presentation System
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

filesinfolders.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. public $EXT=array(
  11. 'txt'=>array( 'txt', 'text', 'md' ),
  12. 'pic'=>array( 'jpg', 'jpeg', 'png', 'svg' ),
  13. 'tpl'=>array( 'html', 'htm' ),
  14. 'audio'=>array('mp3','wav','ogg'),
  15. 'csv'=>array( 'csv' )
  16. );
  17. public $config = array();
  18. private $state=array();
  19. function __construct($folder,$conf=array()) {
  20. debug("about to construct FIF: $folder");
  21. $f3 = \Base::instance();
  22. if(is_dir($folder)) { // are we given a valid path?
  23. $this->folder = $folder;
  24. } else { // and if not?
  25. $this->folder = $f3->get('CONTENT')."./";
  26. }
  27. if(is_array($conf)) {
  28. foreach($conf as $key=>$value) {
  29. switch($key) {
  30. case 'content':
  31. if(is_array($value)) {
  32. foreach($value as $k=>$v) {
  33. $this->domains[$k] = $v;
  34. }
  35. }
  36. break;
  37. case 'keyfiles':
  38. if(is_array($value)) {
  39. foreach($value as $k=>$v) {
  40. $this->keyfiles[$k] = $v;
  41. }
  42. }
  43. break;
  44. }
  45. }
  46. }
  47. foreach($this->domains as $domain) {
  48. $this->content[$domain] = array();
  49. }
  50. foreach($this->keyfiles as $keys=>$d) {
  51. $this->extras[$keys] = "";
  52. }
  53. debug("constructed files in folder: $folder");
  54. }
  55. /////////////////////////////
  56. // read folder into struct //
  57. /////////////////////////////
  58. function prepare_files() {
  59. foreach ($this->domains as $k=>$v) {
  60. foreach($this->EXT as $cat=>$endings) {
  61. $this->structs[$k][$cat]=array();
  62. }
  63. }
  64. $ls = scandir($this->folder);
  65. foreach ($ls as $k=>$f) {
  66. if (!strncmp($f,'.',1)) continue; // ignore hidden files
  67. $ex=explode(".", $f);
  68. $ext=strtolower(end($ex));
  69. if (array_key_exists($ex[0],$this->domains)) {
  70. $domain_key=$ex[0];
  71. $sort_key=1;
  72. } elseif (array_key_exists($ex[0], $this->keyfiles)) {
  73. if(in_array($ext,$this->EXT[$this->keyfiles[$ex[0]]['type']])) {
  74. $this->extras[$ex[0]]=$this->folder.$f;
  75. continue;
  76. }
  77. $domain_key='default';
  78. $sort_key=0;
  79. } else {
  80. $domain_key='default';
  81. $sort_key=0;
  82. }
  83. foreach ($this->EXT as $cat=>$endings) {
  84. if (in_array($ext, $endings)) {
  85. $this->structs[$domain_key][$cat][$ex[$sort_key]] = $this->folder.$f;
  86. break;
  87. }
  88. }
  89. }
  90. foreach($this->keyfiles as $key=>$param) {
  91. if(!$this->extras[$key]) {
  92. $this->extras[$key] = self::search_up(
  93. $key,
  94. array($this->folder,$param['until']),
  95. $this->EXT[$param['type']]
  96. );
  97. }
  98. if($this->extras[$key]) {
  99. if ($param['type'] == 'txt') {
  100. $this->read_textfile($this->extras[$key]);
  101. }
  102. }
  103. }
  104. }
  105. ///////////////////////////////////////
  106. // prepare content as per the struct //
  107. ///////////////////////////////////////
  108. function fill_content() {
  109. $f3 = \Base::instance();
  110. $md = new \freaParsedown();
  111. $md->deactivate_ol();
  112. //var_dump($md->get_BlockTypes());
  113. foreach($this->domains as $domain_key=>$domain) {
  114. // don't act on hidden files
  115. if ($domain == 'hidden') { continue; }
  116. $this->state['current_domain'] = $domain_key;
  117. foreach($this->structs[$domain_key]['txt'] as $key=>$file) {
  118. $str = $this->read_textfile($file);
  119. $str = self::content_element_dispatcher($str);
  120. $str = $md->text($str);
  121. //$str = sprintf("%s", $str);
  122. $this->content[$domain][$key."30text"] = sprintf(
  123. "<div class=\"item %s %s\"><a name=\"%s\"></a>%s</div>",
  124. '',
  125. $key,
  126. $key,
  127. $str
  128. );
  129. }
  130. $includeTemplates = array_key_exists('includeTemplates', $this->config)
  131. ? $this->config['includeTemplates']
  132. : ($f3->get('includeTemplates') ? : FALSE)
  133. ;
  134. if ($includeTemplates) {
  135. foreach($this->structs[$domain_key]['tpl'] as $key=>$file) {
  136. $str = \Template::instance()->render(substr($file,7));
  137. $str = self::linkify($str);
  138. $this->content[$domain][$key."00tpl"] = sprintf(
  139. "<div class=\"item %s %s\">%s</div>",
  140. '',
  141. $key,
  142. $str
  143. );
  144. }
  145. }
  146. $bulkIncludePic = array_key_exists('bulkIncludePic',$this->config)
  147. ? $this->config['bulkIncludePic']
  148. : ($f3->get('bulkIncludePic') ? : FALSE)
  149. ;
  150. if ($bulkIncludePic) {
  151. foreach($this->structs[$domain_key]['pic'] as $key=>$file) {
  152. $image_include_version = 2;
  153. switch ($image_include_version) {
  154. case 1:
  155. $this->content[$domain][$key."10image"] = sprintf(
  156. "<img class=\"bulkImportedImage $bulkIncludePic\" src=\"/$file\" />"
  157. );
  158. break;
  159. case 2:
  160. $module = new CEimage(['image',$file,$bulkIncludePic,'gallery']);
  161. $this->content[$domain][$key."10image"] = $module->index();
  162. unset($module);
  163. break;
  164. case 3:
  165. //$this->content[$domain][] = "asdasd";
  166. break;
  167. }
  168. }
  169. }
  170. $bulkIncludeAudio = array_key_exists('bulkIncludeAudio',$this->config)
  171. ? $this->config['bulkIncludeAudio']
  172. : ($f3->get('bulkIncludeAudio') ? : FALSE)
  173. ;
  174. if ($bulkIncludeAudio) {
  175. foreach ($this->structs[$domain_key]['audio'] as $key=>$file) {
  176. $this->content[$domain][$key."20audio"] = sprintf(
  177. '<a href="%s" class="audio">%s</a><br>',
  178. $file, $key
  179. );
  180. }
  181. }
  182. foreach($this->structs[$domain_key]['csv'] as $key=>$file) {
  183. $csv = new \Modules\Ography($file,TRUE);
  184. $str="<table>";
  185. foreach($csv->entries as $entry) {
  186. $tmp="";
  187. foreach($entry as $key=>$value) {
  188. $tmp .= sprintf("<td class=\"%s\">%s</td>", $key, $value);
  189. }
  190. $str .= sprintf("<tr>%s</tr>",$tmp);
  191. }
  192. $str.="</table>";
  193. $this->content[$domain][$key."30csv"] = $str;
  194. }
  195. }
  196. }
  197. //////////////////////
  198. // read config data //
  199. //////////////////////
  200. function read_config($domain=false) {
  201. foreach ($this->domains as $source=>$destination) {
  202. if (is_string($domain)) {
  203. if ($source != $domain) { continue; }
  204. } elseif (is_array($domain)) {
  205. if (!in_array($source,$domain)) { continue; }
  206. }
  207. foreach ($this->structs[$source]['txt'] as $key=>$file) {
  208. $this->read_textfile($file);
  209. }
  210. }
  211. return $this->config;
  212. }
  213. ////////////////
  214. // recursions //
  215. ////////////////
  216. function search_up($key,$paths,$ext) {
  217. $return = "";
  218. if(count($paths) == 2) {
  219. $current = $paths[0];
  220. $last_try = $paths[1];
  221. $ls=scandir($current);
  222. foreach($ls as $f) {
  223. if(!strncmp($f,'.',1)) continue; // ignore hidden files
  224. $ex=explode(".", $f);
  225. if(in_array(strtolower(end($ex)),$ext)) {
  226. if($ex[0]==$key) {
  227. $return = $current.$f;
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. if ($return) {
  234. return $return;
  235. } elseif($current == $last_try) {
  236. return false;
  237. } else {
  238. $p = explode('/',$current);
  239. array_pop($p);
  240. array_pop($p);
  241. return self::search_up($key,array(implode("/",$p)."/",$last_try),$ext);
  242. }
  243. }
  244. ///////////////////////
  245. // Utility functions //
  246. ///////////////////////
  247. function read_textfile($file) {
  248. debug("about to read file: $file");
  249. $str = file_get_contents($file);
  250. debug("read file: $file");
  251. $str = self::linkify($str);
  252. $str = self::strip_comments($str);
  253. $str = self::get_config_from_content($str);
  254. debug("processed file: $file");
  255. return $str;
  256. }
  257. function strip_comments($str) {
  258. $single_line_comments = "/(^;.*\R)/m";
  259. $str = preg_replace($single_line_comments,"",$str);
  260. $multi_line_comments = "/\/\*.*?\*\//s";
  261. $str = preg_replace($multi_line_comments,"",$str);
  262. return $str;
  263. }
  264. function linkify($string) {
  265. $pattern = "/\s@(\w+)[=]([\w,]+)\s/";
  266. $count = 0;
  267. $new = preg_replace_callback
  268. ($pattern,
  269. function($m){
  270. $f3 = \Base::instance();
  271. return $f3->get('SITE_URL')
  272. .$f3->alias($m[1],self::$keyword."=".$m[2])
  273. ;},
  274. $string);
  275. return $new;
  276. }
  277. function get_config_from_content($string) {
  278. $f3 = \Base::instance();
  279. $f = 0;
  280. $pattern = "/#\+(\w+):\s?(.*)/";
  281. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  282. for ($i=0;$i<$f;$i++) {
  283. $string = str_replace($matches[0][$i],"",$string);
  284. $key = $matches[1][$i];
  285. $value = trim($matches[2][$i]);
  286. if(strtolower($value) == "false") {
  287. $value = FALSE;
  288. }
  289. if(!strncmp(trim($value),'@',1)) {
  290. //var_dump($f3->get('DATA'));
  291. if (array_key_exists($key,$f3->get('DATA'))) {
  292. $DATA = $f3->get('DATA.'.$key);
  293. $conf = array($DATA['type'],$DATA['dir']);
  294. $relation = new \Modules\TOC($conf,$f3->get('CONTENT'),substr($value,1));
  295. $relation->dispatch();
  296. $this->config[$key]=array_shift($relation->entries);
  297. } else {
  298. $this->config[$key]=$value;
  299. }
  300. } else {
  301. $this->config[$key]=$value;
  302. }
  303. }
  304. $pattern = "/§>\s*(\w+):(.*?)\R[\011\040]*\R/s";
  305. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  306. for ($i=0;$i<$f;$i++) {
  307. $string = str_replace($matches[0][$i],"",$string);
  308. $key = $matches[1][$i];
  309. $value = trim($matches[2][$i]);
  310. if(strtolower($value) == "false") {
  311. $value = FALSE;
  312. }
  313. if (!strncmp($value,'@',1)) {
  314. # var_dump();
  315. if (array_key_exists($key,$f3->get('DATA'))) {
  316. $entries = explode("@",$value);
  317. array_shift($entries); // first entry is always empty
  318. $DATA = $f3->get('DATA.'.$key);
  319. $conf = array($DATA['type'],$DATA['dir']);
  320. $relation = new \Modules\TOC($conf,$f3->get('CONTENT'),$entries);
  321. $relation->dispatch();
  322. if(/*count($entries) >*/ 1) {
  323. $this->config[$key]= new CMultiple($relation->entries);
  324. } else {
  325. $this->config[$key]=array_shift($relation->entries);
  326. }
  327. } else {
  328. $this->config[$key]=$value;
  329. }
  330. } else {
  331. $this->config[$key]=$value;
  332. }
  333. }
  334. return $string;
  335. }
  336. function content_element_dispatcher($string) {
  337. $f3 = \Base::instance();
  338. $ED = new ElementDispatcher($this->folder,$this->config);
  339. // find occorances of {| keyword |}
  340. $pattern = "/\{\|(.+?)\|\}/s";
  341. $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
  342. for ($i=0;$i<$f;$i++) {
  343. $body = preg_split("/\R/",trim($matches[1][$i]));
  344. $request = explode(":", trim(array_shift($body)));
  345. $new = $ED->content_element($request,$body);
  346. $string = str_replace($matches[0][$i],$new,$string);
  347. }
  348. return $string;
  349. }
  350. function warn($message) {
  351. return sprintf("<div class=\"warning\">%s</div>",$message);
  352. }
  353. }