|
- <?php
-
- namespace Modules;
-
- class FilesInFolders {
-
- private $folder;
- public $content = array();
- public $extras = array();
- private $domains = array('default'=>'default');
- private $keyfiles = array();
- public $structs = array();
- public $EXT=array(
- 'txt'=>array( 'txt', 'text', 'md' ),
- 'pic'=>array( 'jpg', 'jpeg', 'png', 'svg' ),
- 'tpl'=>array( 'html', 'htm' ),
- 'audio'=>array('mp3','wav','ogg'),
- 'csv'=>array( 'csv' )
- );
- public $config = array();
- private $state=array();
-
- function __construct($folder,$conf=array()) {
- debug("about to construct FIF: $folder");
- $f3 = \Base::instance();
-
- if(is_dir($folder)) { // are we given a valid path?
- $this->folder = $folder;
- } else { // and if not?
- $this->folder = $f3->get('CONTENT')."./";
- }
- if(is_array($conf)) {
- foreach($conf as $key=>$value) {
- switch($key) {
- case 'content':
- if(is_array($value)) {
- foreach($value as $k=>$v) {
- $this->domains[$k] = $v;
- }
- }
- break;
- case 'keyfiles':
- if(is_array($value)) {
- foreach($value as $k=>$v) {
- $this->keyfiles[$k] = $v;
- }
- }
- break;
- }
- }
- }
-
- foreach($this->domains as $domain) {
- $this->content[$domain] = array();
- }
- foreach($this->keyfiles as $keys=>$d) {
- $this->extras[$keys] = "";
- }
- debug("constructed files in folder: $folder");
- }
-
-
- /////////////////////////////
- // read folder into struct //
- /////////////////////////////
- function prepare_files() {
-
- foreach ($this->domains as $k=>$v) {
- foreach($this->EXT as $cat=>$endings) {
- $this->structs[$k][$cat]=array();
- }
- }
-
- $ls = scandir($this->folder);
- foreach ($ls as $k=>$f) {
- if (!strncmp($f,'.',1)) continue; // ignore hidden files
-
- $ex=explode(".", $f);
- $ext=strtolower(end($ex));
- if (array_key_exists($ex[0],$this->domains)) {
- $domain_key=$ex[0];
- $sort_key=1;
- } elseif (array_key_exists($ex[0], $this->keyfiles)) {
- if(in_array($ext,$this->EXT[$this->keyfiles[$ex[0]]['type']])) {
- $this->extras[$ex[0]]=$this->folder.$f;
- continue;
- }
- $domain_key='default';
- $sort_key=0;
- } else {
- $domain_key='default';
- $sort_key=0;
- }
- foreach ($this->EXT as $cat=>$endings) {
- if (in_array($ext, $endings)) {
- $this->structs[$domain_key][$cat][$ex[$sort_key]] = $this->folder.$f;
- break;
- }
- }
- }
-
- foreach($this->keyfiles as $key=>$param) {
- if(!$this->extras[$key]) {
- $this->extras[$key] = self::search_up(
- $key,
- array($this->folder,$param['until']),
- $this->EXT[$param['type']]
- );
- }
- if($this->extras[$key]) {
- if ($param['type'] == 'txt') {
- $this->read_textfile($this->extras[$key]);
- }
- }
- }
- }
-
-
- ///////////////////////////////////////
- // prepare content as per the struct //
- ///////////////////////////////////////
- function fill_content() {
- $f3 = \Base::instance();
- $md = new \freaParsedown();
- $md->deactivate_ol();
- //var_dump($md->get_BlockTypes());
- foreach($this->domains as $domain_key=>$domain) {
- // don't act on hidden files
- if ($domain == 'hidden') { continue; }
- $this->state['current_domain'] = $domain_key;
-
- foreach($this->structs[$domain_key]['txt'] as $key=>$file) {
- $str = $this->read_textfile($file);
- $str = self::content_element_dispatcher($str);
- $str = $md->text($str);
- //$str = sprintf("%s", $str);
- $this->content[$domain][$key."30text"] = sprintf(
- "<div class=\"item %s %s\"><a name=\"%s\"></a>%s</div>",
- '',
- $key,
- $key,
- $str
- );
- }
- $includeTemplates = array_key_exists('includeTemplates', $this->config)
- ? $this->config['includeTemplates']
- : ($f3->get('includeTemplates') ? : FALSE)
- ;
- if ($includeTemplates) {
- foreach($this->structs[$domain_key]['tpl'] as $key=>$file) {
- $str = \Template::instance()->render(substr($file,7));
- $str = self::linkify($str);
- $this->content[$domain][$key."00tpl"] = sprintf(
- "<div class=\"item %s %s\">%s</div>",
- '',
- $key,
- $str
- );
- }
- }
- $bulkIncludePic = array_key_exists('bulkIncludePic',$this->config)
- ? $this->config['bulkIncludePic']
- : ($f3->get('bulkIncludePic') ? : FALSE)
- ;
- if ($bulkIncludePic) {
-
- foreach($this->structs[$domain_key]['pic'] as $key=>$file) {
- $image_include_version = 2;
- switch ($image_include_version) {
- case 1:
- $this->content[$domain][$key."10image"] = sprintf(
- "<img class=\"bulkImportedImage $bulkIncludePic\" src=\"/$file\" />"
- );
- break;
- case 2:
- $module = new CEimage(['image',$file,$bulkIncludePic,'gallery']);
- $this->content[$domain][$key."10image"] = $module->index();
- unset($module);
- break;
- case 3:
- //$this->content[$domain][] = "asdasd";
- break;
- }
- }
-
- }
- $bulkIncludeAudio = array_key_exists('bulkIncludeAudio',$this->config)
- ? $this->config['bulkIncludeAudio']
- : ($f3->get('bulkIncludeAudio') ? : FALSE)
- ;
- if ($bulkIncludeAudio) {
- foreach ($this->structs[$domain_key]['audio'] as $key=>$file) {
- $this->content[$domain][$key."20audio"] = sprintf(
- '<a href="%s" class="audio">%s</a><br>',
- $file, $key
- );
- }
- }
- foreach($this->structs[$domain_key]['csv'] as $key=>$file) {
- $csv = new \Modules\Ography($file,TRUE);
- $str="<table>";
- foreach($csv->entries as $entry) {
- $tmp="";
- foreach($entry as $key=>$value) {
- $tmp .= sprintf("<td class=\"%s\">%s</td>", $key, $value);
- }
- $str .= sprintf("<tr>%s</tr>",$tmp);
- }
- $str.="</table>";
- $this->content[$domain][$key."30csv"] = $str;
- }
- }
- }
-
- //////////////////////
- // read config data //
- //////////////////////
- function read_config($domain=false) {
- foreach ($this->domains as $source=>$destination) {
- if (is_string($domain)) {
- if ($source != $domain) { continue; }
- } elseif (is_array($domain)) {
- if (!in_array($source,$domain)) { continue; }
- }
- foreach ($this->structs[$source]['txt'] as $key=>$file) {
- $this->read_textfile($file);
- }
- }
- return $this->config;
- }
-
- ////////////////
- // recursions //
- ////////////////
- function search_up($key,$paths,$ext) {
- $return = "";
-
- if(count($paths) == 2) {
- $current = $paths[0];
- $last_try = $paths[1];
- $ls=scandir($current);
- foreach($ls as $f) {
- if(!strncmp($f,'.',1)) continue; // ignore hidden files
- $ex=explode(".", $f);
- if(in_array(strtolower(end($ex)),$ext)) {
- if($ex[0]==$key) {
- $return = $current.$f;
- break;
- }
- }
- }
- }
-
- if ($return) {
- return $return;
- } elseif($current == $last_try) {
- return false;
- } else {
- $p = explode('/',$current);
- array_pop($p);
- array_pop($p);
- return self::search_up($key,array(implode("/",$p)."/",$last_try),$ext);
- }
- }
-
- ///////////////////////
- // Utility functions //
- ///////////////////////
- function read_textfile($file) {
- debug("about to read file: $file");
- $str = file_get_contents($file);
- debug("read file: $file");
- $str = self::linkify($str);
- $str = self::strip_comments($str);
- $str = self::get_config_from_content($str);
- debug("processed file: $file");
- return $str;
- }
- function strip_comments($str) {
- $single_line_comments = "/(^;.*\R)/m";
- $str = preg_replace($single_line_comments,"",$str);
- $multi_line_comments = "/\/\*.*?\*\//s";
- $str = preg_replace($multi_line_comments,"",$str);
- return $str;
- }
- function linkify($string) {
- $pattern = "/\s@(\w+)[=]([\w,]+)\s/";
- $count = 0;
- $new = preg_replace_callback
- ($pattern,
- function($m){
- $f3 = \Base::instance();
- return $f3->get('SITE_URL')
- .$f3->alias($m[1],self::$keyword."=".$m[2])
- ;},
- $string);
- return $new;
- }
- function get_config_from_content($string) {
- $f3 = \Base::instance();
- $f = 0;
- $tmp = null;
-
- $pattern = "/#\+(\w+):\s?(.*)/";
- $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
- for ($i=0;$i<$f;$i++) {
- $string = str_replace($matches[0][$i],"",$string);
- $key = $matches[1][$i];
- $value = trim($matches[2][$i]);
-
- if(strtolower($value) == "false") {
- $value = FALSE;
- }
-
- if(!strncmp(trim($value),'@',1)) {
- //var_dump($f3->get('DATA'));
- if (array_key_exists($key,$f3->get('DATA'))) {
- $DATA = $f3->get('DATA.'.$key);
- $conf = array($DATA['type'],$DATA['dir']);
- $relation = new \Modules\TOC($conf,$f3->get('CONTENT'),substr($value,1));
- $relation->dispatch();
- $this->config[$key]=array_shift($relation->entries);
- } else {
- $this->config[$key]=$value;
- }
- } else {
- if (array_key_exists($key, $this->config)) {
- if (is_array($this->config[$key])) {
- $this->config[$key][]=$value;
- } else {
- $tmp = $this->config[$key];
- $this->config[$key] = [$tmp,$value];
- $tmp=null;
- }
- } else {
- $this->config[$key]=$value;
- }
- }
- }
-
- $pattern = "/§>\s*(\w+):(.*?)\R[\011\040]*\R/s";
- $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
- for ($i=0;$i<$f;$i++) {
- $string = str_replace($matches[0][$i],"",$string);
- $key = $matches[1][$i];
- $value = trim($matches[2][$i]);
-
- if(strtolower($value) == "false") {
- $value = FALSE;
- }
-
- if (!strncmp($value,'@',1)) {
- # var_dump();
- if (array_key_exists($key,$f3->get('DATA'))) {
- $entries = explode("@",$value);
- array_shift($entries); // first entry is always empty
- $DATA = $f3->get('DATA.'.$key);
- $conf = array($DATA['type'],$DATA['dir']);
- $relation = new \Modules\TOC($conf,$f3->get('CONTENT'),$entries);
- $relation->dispatch();
-
- if(/*count($entries) >*/ 1) {
- $this->config[$key]= new CMultiple($relation->entries);
- } else {
- $this->config[$key]=array_shift($relation->entries);
- }
- } else {
- $this->config[$key]=$value;
- }
- } else {
- $this->config[$key]=$value;
- }
- }
- return $string;
- }
- function content_element_dispatcher($string) {
- $f3 = \Base::instance();
- $ED = new ElementDispatcher($this->folder,$this->config);
-
- // find occorances of {| keyword |}
- $pattern = "/\{\|(.+?)\|\}/s";
- $f = preg_match_all($pattern, $string,$matches,PREG_PATTERN_ORDER);
-
- for ($i=0;$i<$f;$i++) {
- $body = preg_split("/\R/",trim($matches[1][$i]));
- $request = explode(":", trim(array_shift($body)));
- $new = $ED->content_element($request,$body);
- $string = str_replace($matches[0][$i],$new,$string);
- }
- return $string;
- }
-
- function warn($message) {
- return sprintf("<div class=\"warning\">%s</div>",$message);
- }
- }
|